• 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 "formdatamgrtwo_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "data_center/form_data_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     FormDataMgr formDataMgr;
41     FormHostRecord record;
42     int64_t formId = static_cast<int64_t>(GetU32Data(data));
43     std::vector<int64_t> recordTempForms;
44     recordTempForms.emplace_back(formId);
45     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
46     formDataMgr.PaddingUdidHash(formId);
47     formDataMgr.GenerateFormId();
48     formDataMgr.GenerateUdidHash();
49     formDataMgr.GetUdidHash();
50     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
51     formDataMgr.SetUdidHash(udidHash);
52     sptr<IRemoteObject> callerToken = nullptr;
53     formDataMgr.GetMatchedHostClient(callerToken, record);
54     bool needRefresh = *data % ENABLE;
55     formDataMgr.SetNeedRefresh(formId, needRefresh);
56     bool countTimerRefresh = *data % ENABLE;
57     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
58     FormRecord records;
59     std::vector<FormInfo> targetForms;
60     FormInfo updatedForm;
61     targetForms.emplace_back(updatedForm);
62     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
63     bool enableUpdate = *data % ENABLE;
64     formDataMgr.SetEnableUpdate(formId, enableUpdate);
65     long updateDuration = static_cast<long>(GetU32Data(data));
66     int updateAtHour = static_cast<int>(GetU32Data(data));
67     int updateAtMin = static_cast<int>(GetU32Data(data));
68     std::vector<std::vector<int>> updateAtTimes;
69     std::vector<int> newElement = {updateAtHour, updateAtMin};
70     updateAtTimes.push_back(newElement);
71     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
72     formDataMgr.IsSameForm(records, updatedForm);
73     std::string bundleName(data, size);
74     std::set<int64_t> removedForms;
75     removedForms.insert(formId);
76     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
77     int32_t userId = static_cast<int32_t>(GetU32Data(data));
78     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
79     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
80     return true;
81 }
82 
DoSomethingInterestingWithMyAPI1(const char * data,size_t size)83 bool DoSomethingInterestingWithMyAPI1(const char* data, size_t size)
84 {
85     FormDataMgr formDataMgr;
86     FormHostRecord record;
87     int64_t formId = static_cast<int64_t>(GetU32Data(data));
88     std::vector<int64_t> recordTempForms;
89     recordTempForms.emplace_back(formId);
90     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
91     formDataMgr.PaddingUdidHash(formId);
92     formDataMgr.GenerateFormId();
93     formDataMgr.GenerateUdidHash();
94     formDataMgr.GetUdidHash();
95     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
96     formDataMgr.SetUdidHash(udidHash);
97     sptr<IRemoteObject> callerToken = nullptr;
98     formDataMgr.GetMatchedHostClient(callerToken, record);
99     bool needRefresh = *data % ENABLE;
100     formDataMgr.SetNeedRefresh(formId, needRefresh);
101     bool countTimerRefresh = *data % ENABLE;
102     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
103     FormRecord records;
104     std::vector<FormInfo> targetForms;
105     FormInfo updatedForm;
106     targetForms.emplace_back(updatedForm);
107     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
108     bool enableUpdate = *data % ENABLE;
109     formDataMgr.SetEnableUpdate(formId, enableUpdate);
110     long updateDuration = static_cast<long>(GetU32Data(data));
111     int updateAtHour = static_cast<int>(GetU32Data(data));
112     int updateAtMin = static_cast<int>(GetU32Data(data));
113     std::vector<std::vector<int>> updateAtTimes;
114     std::vector<int> newElement = {updateAtHour, updateAtMin};
115     updateAtTimes.push_back(newElement);
116     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
117     formDataMgr.IsSameForm(records, updatedForm);
118     std::string bundleName(data, size);
119     std::set<int64_t> removedForms;
120     removedForms.insert(formId);
121     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
122     int32_t userId = static_cast<int32_t>(GetU32Data(data));
123     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
124     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
125     return true;
126 }
127 
DoSomethingInterestingWithMyAPI2(const char * data,size_t size)128 bool DoSomethingInterestingWithMyAPI2(const char* data, size_t size)
129 {
130     FormDataMgr formDataMgr;
131     FormHostRecord record;
132     int64_t formId = static_cast<int64_t>(GetU32Data(data));
133     std::vector<int64_t> recordTempForms;
134     recordTempForms.emplace_back(formId);
135     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
136     formDataMgr.PaddingUdidHash(formId);
137     formDataMgr.GenerateFormId();
138     formDataMgr.GenerateUdidHash();
139     formDataMgr.GetUdidHash();
140     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
141     formDataMgr.SetUdidHash(udidHash);
142     sptr<IRemoteObject> callerToken = nullptr;
143     formDataMgr.GetMatchedHostClient(callerToken, record);
144     bool needRefresh = *data % ENABLE;
145     formDataMgr.SetNeedRefresh(formId, needRefresh);
146     bool countTimerRefresh = *data % ENABLE;
147     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
148     FormRecord records;
149     std::vector<FormInfo> targetForms;
150     FormInfo updatedForm;
151     targetForms.emplace_back(updatedForm);
152     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
153     bool enableUpdate = *data % ENABLE;
154     formDataMgr.SetEnableUpdate(formId, enableUpdate);
155     long updateDuration = static_cast<long>(GetU32Data(data));
156     int updateAtHour = static_cast<int>(GetU32Data(data));
157     int updateAtMin = static_cast<int>(GetU32Data(data));
158     std::vector<std::vector<int>> updateAtTimes;
159     std::vector<int> newElement = {updateAtHour, updateAtMin};
160     updateAtTimes.push_back(newElement);
161     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
162     formDataMgr.IsSameForm(records, updatedForm);
163     std::string bundleName(data, size);
164     std::set<int64_t> removedForms;
165     removedForms.insert(formId);
166     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
167     int32_t userId = static_cast<int32_t>(GetU32Data(data));
168     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
169     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
170     return true;
171 }
172 
DoSomethingInterestingWithMyAPI3(const char * data,size_t size)173 bool DoSomethingInterestingWithMyAPI3(const char* data, size_t size)
174 {
175     FormDataMgr formDataMgr;
176     FormHostRecord record;
177     int64_t formId = static_cast<int64_t>(GetU32Data(data));
178     std::vector<int64_t> recordTempForms;
179     recordTempForms.emplace_back(formId);
180     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
181     formDataMgr.PaddingUdidHash(formId);
182     formDataMgr.GenerateFormId();
183     formDataMgr.GenerateUdidHash();
184     formDataMgr.GetUdidHash();
185     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
186     formDataMgr.SetUdidHash(udidHash);
187     sptr<IRemoteObject> callerToken = nullptr;
188     formDataMgr.GetMatchedHostClient(callerToken, record);
189     bool needRefresh = *data % ENABLE;
190     formDataMgr.SetNeedRefresh(formId, needRefresh);
191     bool countTimerRefresh = *data % ENABLE;
192     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
193     FormRecord records;
194     std::vector<FormInfo> targetForms;
195     FormInfo updatedForm;
196     targetForms.emplace_back(updatedForm);
197     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
198     bool enableUpdate = *data % ENABLE;
199     formDataMgr.SetEnableUpdate(formId, enableUpdate);
200     long updateDuration = static_cast<long>(GetU32Data(data));
201     int updateAtHour = static_cast<int>(GetU32Data(data));
202     int updateAtMin = static_cast<int>(GetU32Data(data));
203     std::vector<std::vector<int>> updateAtTimes;
204     std::vector<int> newElement = {updateAtHour, updateAtMin};
205     updateAtTimes.push_back(newElement);
206     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
207     formDataMgr.IsSameForm(records, updatedForm);
208     std::string bundleName(data, size);
209     std::set<int64_t> removedForms;
210     removedForms.insert(formId);
211     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
212     int32_t userId = static_cast<int32_t>(GetU32Data(data));
213     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
214     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
215     return true;
216 }
217 
DoSomethingInterestingWithMyAPI4(const char * data,size_t size)218 bool DoSomethingInterestingWithMyAPI4(const char* data, size_t size)
219 {
220     FormDataMgr formDataMgr;
221     FormHostRecord record;
222     int64_t formId = static_cast<int64_t>(GetU32Data(data));
223     std::vector<int64_t> recordTempForms;
224     recordTempForms.emplace_back(formId);
225     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
226     formDataMgr.PaddingUdidHash(formId);
227     formDataMgr.GenerateFormId();
228     formDataMgr.GenerateUdidHash();
229     formDataMgr.GetUdidHash();
230     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
231     formDataMgr.SetUdidHash(udidHash);
232     sptr<IRemoteObject> callerToken = nullptr;
233     formDataMgr.GetMatchedHostClient(callerToken, record);
234     bool needRefresh = *data % ENABLE;
235     formDataMgr.SetNeedRefresh(formId, needRefresh);
236     bool countTimerRefresh = *data % ENABLE;
237     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
238     FormRecord records;
239     std::vector<FormInfo> targetForms;
240     FormInfo updatedForm;
241     targetForms.emplace_back(updatedForm);
242     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
243     bool enableUpdate = *data % ENABLE;
244     formDataMgr.SetEnableUpdate(formId, enableUpdate);
245     long updateDuration = static_cast<long>(GetU32Data(data));
246     int updateAtHour = static_cast<int>(GetU32Data(data));
247     int updateAtMin = static_cast<int>(GetU32Data(data));
248     std::vector<std::vector<int>> updateAtTimes;
249     std::vector<int> newElement = {updateAtHour, updateAtMin};
250     updateAtTimes.push_back(newElement);
251     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
252     formDataMgr.IsSameForm(records, updatedForm);
253     std::string bundleName(data, size);
254     std::set<int64_t> removedForms;
255     removedForms.insert(formId);
256     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
257     int32_t userId = static_cast<int32_t>(GetU32Data(data));
258     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
259     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
260     return true;
261 }
262 
DoSomethingInterestingWithMyAPI5(const char * data,size_t size)263 bool DoSomethingInterestingWithMyAPI5(const char* data, size_t size)
264 {
265     FormDataMgr formDataMgr;
266     FormHostRecord record;
267     int64_t formId = static_cast<int64_t>(GetU32Data(data));
268     std::vector<int64_t> recordTempForms;
269     recordTempForms.emplace_back(formId);
270     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
271     formDataMgr.PaddingUdidHash(formId);
272     formDataMgr.GenerateFormId();
273     formDataMgr.GenerateUdidHash();
274     formDataMgr.GetUdidHash();
275     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
276     formDataMgr.SetUdidHash(udidHash);
277     sptr<IRemoteObject> callerToken = nullptr;
278     formDataMgr.GetMatchedHostClient(callerToken, record);
279     bool needRefresh = *data % ENABLE;
280     formDataMgr.SetNeedRefresh(formId, needRefresh);
281     bool countTimerRefresh = *data % ENABLE;
282     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
283     FormRecord records;
284     std::vector<FormInfo> targetForms;
285     FormInfo updatedForm;
286     targetForms.emplace_back(updatedForm);
287     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
288     bool enableUpdate = *data % ENABLE;
289     formDataMgr.SetEnableUpdate(formId, enableUpdate);
290     long updateDuration = static_cast<long>(GetU32Data(data));
291     int updateAtHour = static_cast<int>(GetU32Data(data));
292     int updateAtMin = static_cast<int>(GetU32Data(data));
293     std::vector<std::vector<int>> updateAtTimes;
294     std::vector<int> newElement = {updateAtHour, updateAtMin};
295     updateAtTimes.push_back(newElement);
296     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
297     formDataMgr.IsSameForm(records, updatedForm);
298     std::string bundleName(data, size);
299     std::set<int64_t> removedForms;
300     removedForms.insert(formId);
301     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
302     int32_t userId = static_cast<int32_t>(GetU32Data(data));
303     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
304     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
305     return true;
306 }
307 
DoSomethingInterestingWithMyAPI6(const char * data,size_t size)308 bool DoSomethingInterestingWithMyAPI6(const char* data, size_t size)
309 {
310     FormDataMgr formDataMgr;
311     FormHostRecord record;
312     int64_t formId = static_cast<int64_t>(GetU32Data(data));
313     std::vector<int64_t> recordTempForms;
314     recordTempForms.emplace_back(formId);
315     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
316     formDataMgr.PaddingUdidHash(formId);
317     formDataMgr.GenerateFormId();
318     formDataMgr.GenerateUdidHash();
319     formDataMgr.GetUdidHash();
320     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
321     formDataMgr.SetUdidHash(udidHash);
322     sptr<IRemoteObject> callerToken = nullptr;
323     formDataMgr.GetMatchedHostClient(callerToken, record);
324     bool needRefresh = *data % ENABLE;
325     formDataMgr.SetNeedRefresh(formId, needRefresh);
326     bool countTimerRefresh = *data % ENABLE;
327     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
328     FormRecord records;
329     std::vector<FormInfo> targetForms;
330     FormInfo updatedForm;
331     targetForms.emplace_back(updatedForm);
332     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
333     bool enableUpdate = *data % ENABLE;
334     formDataMgr.SetEnableUpdate(formId, enableUpdate);
335     long updateDuration = static_cast<long>(GetU32Data(data));
336     int updateAtHour = static_cast<int>(GetU32Data(data));
337     int updateAtMin = static_cast<int>(GetU32Data(data));
338     std::vector<std::vector<int>> updateAtTimes;
339     std::vector<int> newElement = {updateAtHour, updateAtMin};
340     updateAtTimes.push_back(newElement);
341     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
342     formDataMgr.IsSameForm(records, updatedForm);
343     std::string bundleName(data, size);
344     std::set<int64_t> removedForms;
345     removedForms.insert(formId);
346     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
347     int32_t userId = static_cast<int32_t>(GetU32Data(data));
348     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
349     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
350     return true;
351 }
352 
DoSomethingInterestingWithMyAPI7(const char * data,size_t size)353 bool DoSomethingInterestingWithMyAPI7(const char* data, size_t size)
354 {
355     FormDataMgr formDataMgr;
356     FormHostRecord record;
357     int64_t formId = static_cast<int64_t>(GetU32Data(data));
358     std::vector<int64_t> recordTempForms;
359     recordTempForms.emplace_back(formId);
360     formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
361     formDataMgr.PaddingUdidHash(formId);
362     formDataMgr.GenerateFormId();
363     formDataMgr.GenerateUdidHash();
364     formDataMgr.GetUdidHash();
365     int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
366     formDataMgr.SetUdidHash(udidHash);
367     sptr<IRemoteObject> callerToken = nullptr;
368     formDataMgr.GetMatchedHostClient(callerToken, record);
369     bool needRefresh = *data % ENABLE;
370     formDataMgr.SetNeedRefresh(formId, needRefresh);
371     bool countTimerRefresh = *data % ENABLE;
372     formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
373     FormRecord records;
374     std::vector<FormInfo> targetForms;
375     FormInfo updatedForm;
376     targetForms.emplace_back(updatedForm);
377     formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
378     bool enableUpdate = *data % ENABLE;
379     formDataMgr.SetEnableUpdate(formId, enableUpdate);
380     long updateDuration = static_cast<long>(GetU32Data(data));
381     int updateAtHour = static_cast<int>(GetU32Data(data));
382     int updateAtMin = static_cast<int>(GetU32Data(data));
383     std::vector<std::vector<int>> updateAtTimes;
384     std::vector<int> newElement = {updateAtHour, updateAtMin};
385     updateAtTimes.push_back(newElement);
386     formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin, updateAtTimes);
387     formDataMgr.IsSameForm(records, updatedForm);
388     std::string bundleName(data, size);
389     std::set<int64_t> removedForms;
390     removedForms.insert(formId);
391     formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
392     int32_t userId = static_cast<int32_t>(GetU32Data(data));
393     formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
394     formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
395     return true;
396 }
397 }
398 
399 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)400 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
401 {
402     /* Run your code on data */
403     if (data == nullptr) {
404         return 0;
405     }
406 
407     if (size < OHOS::U32_AT_SIZE) {
408         return 0;
409     }
410 
411     char* ch = static_cast<char*>(malloc(size + 1));
412     if (ch == nullptr) {
413         return 0;
414     }
415 
416     (void)memset_s(ch, size + 1, 0x00, size + 1);
417     if (memcpy_s(ch, size + 1, data, size) != EOK) {
418         free(ch);
419         ch = nullptr;
420         return 0;
421     }
422 
423     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
424     free(ch);
425     ch = nullptr;
426     return 0;
427 }
428 
429