• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "formtimermgr_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "common/timer_mgr/form_timer_mgr.h"
24 #undef private
25 #undef protected
26 #include "securec.h"
27 
28 using namespace OHOS::AppExecFwk;
29 
30 namespace OHOS {
31 constexpr size_t U32_AT_SIZE = 4;
32 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)33 uint32_t GetU32Data(const char* ptr)
34 {
35     // convert fuzz input data to an integer
36     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
37 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)38 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
39 {
40     FormTimerMgr formTimerMgr;
41     FormTimer task;
42     formTimerMgr.AddFormTimer(task);
43     int64_t formId = static_cast<int64_t>(GetU32Data(data));
44     long updateDuration = static_cast<long>(GetU32Data(data));
45     int32_t userId = static_cast<int32_t>(GetU32Data(data));
46     formTimerMgr.AddFormTimer(formId, updateDuration, userId);
47     long updateAtHour = static_cast<long>(GetU32Data(data));
48     long updateAtMin = static_cast<long>(GetU32Data(data));
49     formTimerMgr.AddFormTimer(formId, updateAtHour, updateAtMin, userId);
50     formTimerMgr.RemoveFormTimer(formId);
51     UpdateType type = UpdateType::TYPE_INTERVAL_CHANGE;
52     FormTimerCfg timerCfg;
53     formTimerMgr.UpdateFormTimer(formId, type, timerCfg);
54     formTimerMgr.UpdateIntervalValue(formId, timerCfg);
55     formTimerMgr.UpdateAtTimerValue(formId, timerCfg);
56     formTimerMgr.IntervalToAtTimer(formId, timerCfg);
57     formTimerMgr.AtTimerToIntervalTimer(formId, timerCfg);
58     formTimerMgr.IsLimiterEnableRefresh(formId);
59     formTimerMgr.IncreaseRefreshCount(formId);
60     long nextGapTime = static_cast<long>(GetU32Data(data));
61     formTimerMgr.SetNextRefreshTime(formId, nextGapTime, userId);
62     bool flag = *data % ENABLE;
63     formTimerMgr.SetEnableFlag(formId, flag);
64     formTimerMgr.GetRefreshCount(formId);
65     formTimerMgr.MarkRemind(formId);
66     formTimerMgr.AddUpdateAtTimer(task);
67     formTimerMgr.AddIntervalTimer(task);
68     UpdateAtItem atItem;
69     formTimerMgr.AddUpdateAtItem(atItem);
70     formTimerMgr.HandleSystemTimeChanged();
71     formTimerMgr.HandleResetLimiter();
72     long updateTime = static_cast<long>(GetU32Data(data));
73     formTimerMgr.OnUpdateAtTrigger(updateTime);
74     int64_t updateTimes = static_cast<int64_t>(GetU32Data(data));
75     formTimerMgr.OnDynamicTimeTrigger(updateTimes);
76     std::vector<FormTimer> remindTasks;
77     remindTasks.emplace_back(task);
78     formTimerMgr.GetRemindTasks(remindTasks);
79     formTimerMgr.SetIntervalEnableFlag(formId, flag);
80     formTimerMgr.GetIntervalTimer(formId, task);
81     formTimerMgr.GetUpdateAtTimer(formId, atItem);
82     DynamicRefreshItem dynamicItem;
83     formTimerMgr.GetDynamicItem(formId, dynamicItem);
84     int32_t timeSpeed = static_cast<int32_t>(GetU32Data(data));
85     formTimerMgr.SetTimeSpeed(timeSpeed);
86     formTimerMgr.DeleteIntervalTimer(formId);
87     formTimerMgr.DeleteUpdateAtTimer(formId);
88     formTimerMgr.DeleteDynamicItem(formId);
89     formTimerMgr.OnIntervalTimeOut();
90     formTimerMgr.UpdateAtTimerAlarm();
91     long updateAtTime = static_cast<long>(GetU32Data(data));
92     formTimerMgr.GetUpdateAtWantAgent(updateAtTime, userId);
93     formTimerMgr.ClearUpdateAtTimerResource();
94     return formTimerMgr.UpdateLimiterAlarm();
95 }
96 }
97 
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
100 {
101     /* Run your code on data */
102     if (data == nullptr) {
103         return 0;
104     }
105 
106     if (size < OHOS::U32_AT_SIZE) {
107         return 0;
108     }
109 
110     char* ch = static_cast<char*>(malloc(size + 1));
111     if (ch == nullptr) {
112         return 0;
113     }
114 
115     (void)memset_s(ch, size + 1, 0x00, size + 1);
116     if (memcpy_s(ch, size + 1, data, size) != EOK) {
117         free(ch);
118         ch = nullptr;
119         return 0;
120     }
121 
122     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
123     free(ch);
124     ch = nullptr;
125     return 0;
126 }
127 
128