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 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 FormDataMgr formDataMgr;
42 FormHostRecord record;
43 int64_t formId = static_cast<int64_t>(GetU32Data(data));
44 std::vector<int64_t> recordTempForms;
45 recordTempForms.emplace_back(formId);
46 formDataMgr.HandleHostDiedForTempForms(record, recordTempForms);
47 formDataMgr.PaddingUdidHash(formId);
48 formDataMgr.GenerateFormId();
49 formDataMgr.GenerateUdidHash();
50 formDataMgr.GetUdidHash();
51 int64_t udidHash = static_cast<int64_t>(GetU32Data(data));
52 formDataMgr.SetUdidHash(udidHash);
53 sptr<IRemoteObject> callerToken = nullptr;
54 formDataMgr.GetMatchedHostClient(callerToken, record);
55 bool needRefresh = *data % ENABLE;
56 formDataMgr.SetNeedRefresh(formId, needRefresh);
57 bool countTimerRefresh = *data % ENABLE;
58 formDataMgr.SetCountTimerRefresh(formId, countTimerRefresh);
59 FormRecord records;
60 std::vector<FormInfo> targetForms;
61 FormInfo updatedForm;
62 targetForms.emplace_back(updatedForm);
63 formDataMgr.GetUpdatedForm(records, targetForms, updatedForm);
64 bool enableUpdate = *data % ENABLE;
65 formDataMgr.SetEnableUpdate(formId, enableUpdate);
66 long updateDuration = static_cast<long>(GetU32Data(data));
67 int updateAtHour = static_cast<int>(GetU32Data(data));
68 int updateAtMin = static_cast<int>(GetU32Data(data));
69 formDataMgr.SetUpdateInfo(formId, enableUpdate, updateDuration, updateAtHour, updateAtMin);
70 formDataMgr.IsSameForm(records, updatedForm);
71 std::string bundleName(data, size);
72 std::set<int64_t> removedForms;
73 removedForms.insert(formId);
74 formDataMgr.CleanRemovedFormRecords(bundleName, removedForms);
75 int32_t userId = static_cast<int32_t>(GetU32Data(data));
76 formDataMgr.CleanRemovedTempFormRecords(bundleName, userId, removedForms);
77 formDataMgr.GetReCreateFormRecordsByBundleName(bundleName, removedForms);
78 return true;
79 }
80 }
81
82 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)83 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
84 {
85 /* Run your code on data */
86 if (data == nullptr) {
87 return 0;
88 }
89
90 if (size < OHOS::U32_AT_SIZE) {
91 return 0;
92 }
93
94 /* Validate the length of size */
95 if (size == 0 || size > OHOS::FOO_MAX_LEN) {
96 return 0;
97 }
98
99 char* ch = (char *)malloc(size + 1);
100 if (ch == nullptr) {
101 return 0;
102 }
103
104 (void)memset_s(ch, size + 1, 0x00, size + 1);
105 if (memcpy_s(ch, size, data, size) != EOK) {
106 free(ch);
107 ch = nullptr;
108 return 0;
109 }
110
111 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
112 free(ch);
113 ch = nullptr;
114 return 0;
115 }
116
117