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