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 "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 }
83
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87 /* Run your code on data */
88 if (data == nullptr) {
89 return 0;
90 }
91
92 if (size < OHOS::U32_AT_SIZE) {
93 return 0;
94 }
95
96 char* ch = static_cast<char*>(malloc(size + 1));
97 if (ch == nullptr) {
98 return 0;
99 }
100
101 (void)memset_s(ch, size + 1, 0x00, size + 1);
102 if (memcpy_s(ch, size + 1, data, size) != EOK) {
103 free(ch);
104 ch = nullptr;
105 return 0;
106 }
107
108 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
109 free(ch);
110 ch = nullptr;
111 return 0;
112 }
113
114