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 "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 FOO_MAX_LEN = 1024;
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr uint8_t ENABLE = 2;
GetU32Data(const char * ptr)34 uint32_t GetU32Data(const char* ptr)
35 {
36 // convert fuzz input data to an integer
37 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
38 }
DoSomethingInterestingWithMyAPI(const char * data,size_t size)39 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
40 {
41 FormTimerMgr formTimerMgr;
42 FormTimer task;
43 formTimerMgr.AddFormTimer(task);
44 int64_t formId = static_cast<int64_t>(GetU32Data(data));
45 long updateDuration = static_cast<long>(GetU32Data(data));
46 int32_t userId = static_cast<int32_t>(GetU32Data(data));
47 formTimerMgr.AddFormTimer(formId, updateDuration, userId);
48 long updateAtHour = static_cast<long>(GetU32Data(data));
49 long updateAtMin = static_cast<long>(GetU32Data(data));
50 formTimerMgr.AddFormTimer(formId, updateAtHour, updateAtMin, userId);
51 formTimerMgr.RemoveFormTimer(formId);
52 UpdateType type = UpdateType::TYPE_INTERVAL_CHANGE;
53 FormTimerCfg timerCfg;
54 formTimerMgr.UpdateFormTimer(formId, type, timerCfg);
55 formTimerMgr.UpdateIntervalValue(formId, timerCfg);
56 formTimerMgr.UpdateAtTimerValue(formId, timerCfg);
57 formTimerMgr.IntervalToAtTimer(formId, timerCfg);
58 formTimerMgr.AtTimerToIntervalTimer(formId, timerCfg);
59 formTimerMgr.IsLimiterEnableRefresh(formId);
60 formTimerMgr.IncreaseRefreshCount(formId);
61 long nextGapTime = static_cast<long>(GetU32Data(data));
62 formTimerMgr.SetNextRefreshTime(formId, nextGapTime, userId);
63 bool flag = *data % ENABLE;
64 formTimerMgr.SetEnableFlag(formId, flag);
65 formTimerMgr.GetRefreshCount(formId);
66 formTimerMgr.MarkRemind(formId);
67 formTimerMgr.AddUpdateAtTimer(task);
68 formTimerMgr.AddIntervalTimer(task);
69 UpdateAtItem atItem;
70 formTimerMgr.AddUpdateAtItem(atItem);
71 formTimerMgr.HandleSystemTimeChanged();
72 formTimerMgr.HandleResetLimiter();
73 long updateTime = static_cast<long>(GetU32Data(data));
74 formTimerMgr.OnUpdateAtTrigger(updateTime);
75 int64_t updateTimes = static_cast<int64_t>(GetU32Data(data));
76 formTimerMgr.OnDynamicTimeTrigger(updateTimes);
77 std::vector<FormTimer> remindTasks;
78 remindTasks.emplace_back(task);
79 formTimerMgr.GetRemindTasks(remindTasks);
80 formTimerMgr.SetIntervalEnableFlag(formId, flag);
81 formTimerMgr.GetIntervalTimer(formId, task);
82 formTimerMgr.GetUpdateAtTimer(formId, atItem);
83 DynamicRefreshItem dynamicItem;
84 formTimerMgr.GetDynamicItem(formId, dynamicItem);
85 int32_t timeSpeed = static_cast<int32_t>(GetU32Data(data));
86 formTimerMgr.SetTimeSpeed(timeSpeed);
87 formTimerMgr.DeleteIntervalTimer(formId);
88 formTimerMgr.DeleteUpdateAtTimer(formId);
89 formTimerMgr.DeleteDynamicItem(formId);
90 formTimerMgr.OnIntervalTimeOut();
91 formTimerMgr.UpdateAtTimerAlarm();
92 long updateAtTime = static_cast<long>(GetU32Data(data));
93 formTimerMgr.GetUpdateAtWantAgent(updateAtTime, userId);
94 formTimerMgr.ClearUpdateAtTimerResource();
95 formTimerMgr.RefreshWhenFormVisible(formId, userId);
96 return formTimerMgr.UpdateLimiterAlarm();
97 }
98 }
99
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
102 {
103 /* Run your code on data */
104 if (data == nullptr) {
105 return 0;
106 }
107
108 if (size < OHOS::U32_AT_SIZE) {
109 return 0;
110 }
111
112 /* Validate the length of size */
113 if (size == 0 || size > OHOS::FOO_MAX_LEN) {
114 return 0;
115 }
116
117 char* ch = static_cast<char*>(malloc(size + 1));
118 if (ch == nullptr) {
119 return 0;
120 }
121
122 (void)memset_s(ch, size + 1, 0x00, size + 1);
123 if (memcpy_s(ch, size + 1, data, size) != EOK) {
124 free(ch);
125 ch = nullptr;
126 return 0;
127 }
128
129 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
130 free(ch);
131 ch = nullptr;
132 return 0;
133 }
134
135