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 "formdatamgrthree_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 int64_t formId = static_cast<int64_t>(GetU32Data(data));
43 bool versionUpgrade = *data % ENABLE;
44 formDataMgr.SetVersionUpgrade(formId, versionUpgrade);
45 FormRecord formRecord;
46 formDataMgr.UpdateHostForm(formId, formRecord);
47 std::vector<int64_t> formIds;
48 formIds.emplace_back(formId);
49 bool flag = *data % ENABLE;
50 bool isOnlyEnableUpdate = *data % ENABLE;
51 FormHostRecord formHostRecord;
52 std::vector<int64_t> refreshForms;
53 refreshForms.emplace_back(formId);
54 formDataMgr.HandleUpdateHostFormFlag(formIds, flag, isOnlyEnableUpdate, formHostRecord, refreshForms);
55 sptr<IRemoteObject> callerToken = nullptr;
56 formDataMgr.UpdateHostFormFlag(formIds, callerToken, flag, isOnlyEnableUpdate, refreshForms);
57 formDataMgr.FindMatchedFormId(formId);
58 int uId = static_cast<int>(GetU32Data(data));
59 formDataMgr.ClearHostDataByUId(uId);
60 std::map<FormIdKey, std::set<int64_t>> noHostTempFormsMap;
61 std::string bundleName(data, size);
62 std::string abilityName(data, size);
63 FormIdKey formIdKey(bundleName, abilityName);
64 std::set<int64_t> aa;
65 aa.insert(formId);
66 noHostTempFormsMap.emplace(formIdKey, aa);
67 std::map<int64_t, bool> foundFormsMap;
68 foundFormsMap.emplace(formId, flag);
69 formDataMgr.GetNoHostTempForms(uId, noHostTempFormsMap, foundFormsMap);
70 FormItemInfo info;
71 formDataMgr.ParseUpdateConfig(formRecord, info);
72 int configDuration = static_cast<int>(GetU32Data(data));
73 formDataMgr.ParseIntervalConfig(formRecord, configDuration);
74 formDataMgr.ParseAtTimerConfig(formRecord, info);
75 formDataMgr.IsFormCached(formRecord);
76 std::string provider(data, size);
77 int callingUid = static_cast<int>(GetU32Data(data));
78 formDataMgr.CreateFormStateRecord(provider, info, callerToken, callingUid);
79 bool isVisible = *data % ENABLE;
80 formDataMgr.NotifyFormsVisible(formIds, isVisible, callerToken);
81 int64_t matchedFormId = static_cast<int64_t>(GetU32Data(data));
82 formDataMgr.SetRecordVisible(matchedFormId, isVisible);
83 return true;
84 }
85 }
86
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
89 {
90 /* Run your code on data */
91 if (data == nullptr) {
92 return 0;
93 }
94
95 if (size < OHOS::U32_AT_SIZE) {
96 return 0;
97 }
98
99 /* Validate the length of size */
100 if (size > OHOS::FOO_MAX_LEN) {
101 return 0;
102 }
103
104 char* ch = reinterpret_cast<char *>(malloc(size + 1));
105 if (ch == nullptr) {
106 return 0;
107 }
108
109 (void)memset_s(ch, size + 1, 0x00, size + 1);
110 if (memcpy_s(ch, size + 1, data, size) != EOK) {
111 free(ch);
112 ch = nullptr;
113 return 0;
114 }
115
116 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
117 free(ch);
118 ch = nullptr;
119 return 0;
120 }
121
122