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 /* This files contains faultlog fuzzer test modules. */
17
18 #include "power_fuzzer.h"
19
20 #include <cstdlib>
21 #include <ctime>
22 #include <iostream>
23 #include <random>
24 #include <cstddef>
25 #include "securec.h"
26
27 #include "power_mgr_client.h"
28
29 using namespace OHOS::PowerMgr;
30 using namespace std;
31
32 namespace {
33 auto& g_powerMgrClient = PowerMgrClient::GetInstance();
34 constexpr int32_t SIZE = 1;
35 constexpr int32_t INDEX_0 = 0;
36 }
37
SuspendDevice(const uint8_t * data)38 static void SuspendDevice(const uint8_t* data)
39 {
40 int32_t type[1];
41 int32_t idSize = 4;
42 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
43 return;
44 }
45
46 g_powerMgrClient.SuspendDevice(static_cast<SuspendDeviceType>(type[0]));
47 }
48
WakeupDevice(const uint8_t * data)49 static void WakeupDevice(const uint8_t* data)
50 {
51 int32_t type[1];
52 int32_t idSize = 4;
53 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
54 return;
55 }
56
57 g_powerMgrClient.WakeupDevice(static_cast<WakeupDeviceType>(type[0]));
58 }
59
RefreshActivity(const uint8_t * data)60 static void RefreshActivity(const uint8_t* data)
61 {
62 int32_t type[1];
63 int32_t idSize = 4;
64 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
65 return;
66 }
67
68 g_powerMgrClient.RefreshActivity(static_cast<UserActivityType>(type[0]));
69 }
70
IsScreenOn(const uint8_t * data)71 static void IsScreenOn(const uint8_t* data)
72 {
73 int32_t type[1];
74 int32_t idSize = 4;
75 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
76 return;
77 }
78
79 g_powerMgrClient.IsScreenOn();
80 }
81
GetState(const uint8_t * data)82 static void GetState(const uint8_t* data)
83 {
84 int32_t type[1];
85 int32_t idSize = 4;
86 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
87 return;
88 }
89
90 g_powerMgrClient.GetState();
91 }
92
SetDeviceMode(const uint8_t * data)93 static void SetDeviceMode(const uint8_t* data)
94 {
95 int32_t type[1];
96 int32_t idSize = 4;
97 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
98 return;
99 }
100
101 g_powerMgrClient.SetDeviceMode(static_cast<PowerMode>(type[0]));
102 }
103
GetDeviceMode(const uint8_t * data)104 static void GetDeviceMode(const uint8_t* data)
105 {
106 int32_t type[1];
107 int32_t idSize = 4;
108 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
109 return;
110 }
111
112 g_powerMgrClient.GetDeviceMode();
113 }
114
CreateRunningLock(const uint8_t * data)115 static void CreateRunningLock(const uint8_t* data)
116 {
117 int32_t type[1];
118 int32_t idSize = 4;
119 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
120 return;
121 }
122
123 g_powerMgrClient.CreateRunningLock("FUZZ", static_cast <RunningLockType>(type[0]));
124 }
125
RegisterPowerStateCallback(const uint8_t * data)126 static void RegisterPowerStateCallback(const uint8_t* data)
127 {
128 static OHOS::sptr<IPowerStateCallback> callback;
129 int32_t idSize = 4;
130 if ((memcpy_s(callback, sizeof(callback), data, idSize)) != EOK) {
131 return;
132 }
133
134 g_powerMgrClient.RegisterPowerStateCallback(callback);
135 g_powerMgrClient.UnRegisterPowerStateCallback(callback);
136 }
137
RegisterPowerModeCallback(const uint8_t * data)138 static void RegisterPowerModeCallback(const uint8_t* data)
139 {
140 static OHOS::sptr<IPowerModeCallback> callback;
141 int32_t idSize = 4;
142 if ((memcpy_s(callback, sizeof(callback), data, idSize)) != EOK) {
143 return;
144 }
145
146 g_powerMgrClient.RegisterPowerModeCallback(callback);
147 g_powerMgrClient.UnRegisterPowerModeCallback(callback);
148 }
149
RegisterShutdownCallback(const uint8_t * data)150 static void RegisterShutdownCallback(const uint8_t* data)
151 {
152 static OHOS::sptr<IShutdownCallback> callback;
153 int32_t idSize = 4;
154 if ((memcpy_s(callback, sizeof(callback), data, idSize)) != EOK) {
155 return;
156 }
157 uint32_t intPriority[SIZE];
158 if (memcpy_s(intPriority, sizeof(intPriority), data, idSize) != EOK) {
159 return;
160 }
161 IShutdownCallback::ShutdownPriority priority =
162 static_cast<IShutdownCallback::ShutdownPriority>(intPriority[INDEX_0]);
163 g_powerMgrClient.RegisterShutdownCallback(callback, priority);
164 g_powerMgrClient.UnRegisterShutdownCallback(callback);
165 }
166
Lock(const uint8_t * data)167 static void Lock(const uint8_t* data)
168 {
169 int32_t type[1];
170 int32_t idSize = 4;
171 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
172 return;
173 }
174
175 auto fuzzLock = g_powerMgrClient.CreateRunningLock("FUZZ", static_cast <RunningLockType>(type[0]));
176 fuzzLock -> Lock();
177 }
178
UnLock(const uint8_t * data)179 static void UnLock(const uint8_t* data)
180 {
181 int32_t type[1];
182 int32_t idSize = 4;
183 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
184 return;
185 }
186
187 auto fuzzLock = g_powerMgrClient.CreateRunningLock("FUZZ", static_cast <RunningLockType>(type[0]));
188 fuzzLock -> Lock();
189 fuzzLock -> UnLock();
190 }
191
OverrideScreenOffTime(const uint8_t * data)192 static void OverrideScreenOffTime(const uint8_t* data)
193 {
194 int64_t type[1];
195 int32_t idSize = 4;
196 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
197 return;
198 }
199
200 g_powerMgrClient.OverrideScreenOffTime(type[0]);
201 }
202
RestoreScreenOffTime(const uint8_t * data)203 static void RestoreScreenOffTime(const uint8_t* data)
204 {
205 int32_t type[1];
206 int32_t idSize = 4;
207 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
208 return;
209 }
210
211 g_powerMgrClient.RestoreScreenOffTime();
212 }
213
IsRunningLockTypeSupported(const uint8_t * data)214 static void IsRunningLockTypeSupported(const uint8_t* data)
215 {
216 uint32_t type[SIZE];
217 int32_t idSize = 4;
218 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
219 return;
220 }
221
222 g_powerMgrClient.IsRunningLockTypeSupported(type[INDEX_0]);
223 }
224
SetDisplaySuspend(const uint8_t * data)225 static void SetDisplaySuspend(const uint8_t* data)
226 {
227 char type[SIZE];
228 int32_t idSize = 1;
229 if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
230 return;
231 }
232
233 g_powerMgrClient.SetDisplaySuspend(static_cast<bool>(type[INDEX_0]));
234 }
235
randNum()236 static int32_t randNum()
237 {
238 std::random_device rd;
239 std::default_random_engine engine(rd());
240 std::uniform_int_distribution<int32_t> randomNum(static_cast<int32_t>(ApiNumber::NUM_ZERO),
241 static_cast<int32_t>(ApiNumber::NUM_END) - 1);
242 return randomNum(engine);
243 }
244
245 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)246 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
247 {
248 int32_t idSize = 4;
249 int32_t cond[1];
250 if (static_cast<int32_t>(size) > idSize) {
251 if ((memcpy_s(cond, sizeof(cond), data, idSize)) != EOK) {
252 return false;
253 }
254 int32_t number = randNum();
255
256 switch (static_cast<ApiNumber>(number)) {
257 case ApiNumber::NUM_ZERO:
258 SuspendDevice(data);
259 break;
260 case ApiNumber::NUM_ONE:
261 WakeupDevice(data);
262 break;
263 case ApiNumber::NUM_TWO:
264 RefreshActivity(data);
265 break;
266 case ApiNumber::NUM_THREE:
267 IsScreenOn(data);
268 break;
269 case ApiNumber::NUM_FOUR:
270 GetState(data);
271 break;
272 case ApiNumber::NUM_EIGHT:
273 SetDeviceMode(data);
274 break;
275 case ApiNumber::NUM_NINE:
276 GetDeviceMode(data);
277 break;
278 case ApiNumber::NUM_TEN:
279 CreateRunningLock(data);
280 break;
281 case ApiNumber::NUM_ELEVEN:
282 RegisterPowerStateCallback(data);
283 break;
284 case ApiNumber::NUM_THIRTEEN:
285 RegisterPowerModeCallback(data);
286 break;
287 case ApiNumber::NUM_FIFTEEN:
288 Lock(data);
289 break;
290 case ApiNumber::NUM_SIXTEEN:
291 UnLock(data);
292 break;
293 case ApiNumber::NUM_SEVENTEEN:
294 OverrideScreenOffTime(data);
295 break;
296 case ApiNumber::NUM_EIGHTEEN:
297 RestoreScreenOffTime(data);
298 break;
299 case ApiNumber::NUM_NINETEEN:
300 IsRunningLockTypeSupported(data);
301 break;
302 case ApiNumber::NUM_TWENTY:
303 SetDisplaySuspend(data);
304 break;
305 case ApiNumber::NUM_TWENTY_ONE:
306 RegisterShutdownCallback(data);
307 break;
308 default:
309 break;
310 }
311 }
312
313 return true;
314 }
315 }
316
317 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)318 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
319 {
320 /* Run your code on data */
321 OHOS::DoSomethingInterestingWithMyAPI(data, size);
322 return 0;
323 }
324
325