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 "formsyseventreceiver_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #define protected public
23 #include "form_sys_event_receiver.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     EventFwk::CommonEventSubscribeInfo subscriberInfo;
42     FormSysEventReceiver formSysEventReceiver(subscriberInfo);
43     EventFwk::CommonEventData eventData;
44     formSysEventReceiver.OnReceiveEvent(eventData);
45     int64_t formId = static_cast<int64_t>(GetU32Data(data));
46     FormRecord formRecord;
47     FormInfo formInfo;
48     std::vector<FormInfo> targetForms;
49     targetForms.emplace_back(formInfo);
50     std::string bundleName(data, size);
51     int userId = static_cast<int>(GetU32Data(data));
52     FormEventUtil::HandleProviderUpdated(bundleName, userId);
53     int32_t userIds = static_cast<int32_t>(GetU32Data(data));
54     FormEventUtil::HandleProviderRemoved(bundleName, userIds);
55     BundlePackInfo bundlePackInfo;
56     BundleInfo bundleInfo;
57     FormEventUtil::ProviderFormUpdated(formId, formRecord, bundlePackInfo, bundleInfo);
58     FormEventUtil::HandleBundleFormInfoChanged(bundleName, userIds);
59     FormEventUtil::HandleUpdateFormCloud(bundleName);
60     FormEventUtil::HandleBundleFormInfoRemoved(bundleName, userIds);
61     FormEventUtil::HandleBundleDataCleared(bundleName, userIds);
62     int uid = static_cast<int>(GetU32Data(data));
63     FormEventUtil::HandleFormHostDataCleared(uid);
64     AAFwk::Want want;
65     formSysEventReceiver.HandleAbilityUpdate(want, bundleName);
66     formSysEventReceiver.HandlePackageDataCleared(bundleName, userId);
67     formSysEventReceiver.HandleUserUnlocked();
68     formSysEventReceiver.HandleUserSwitched(eventData);
69     bool flag = *data % ENABLE;
70     std::map<int64_t, bool> removedFormsMap;
71     removedFormsMap.emplace(formId, flag);
72     FormEventUtil::ClearFormDBRecordData(uid, removedFormsMap);
73     FormEventUtil::ClearTempFormRecordData(uid, removedFormsMap);
74     std::string abilityName;
75     FormIdKey formIdKey(bundleName, abilityName);
76     std::set<int64_t> formNums;
77     formNums.insert(formId);
78     std::map<FormIdKey, std::set<int64_t>> noHostFormDbMap;
79     noHostFormDbMap.emplace(formIdKey, formNums);
80     FormEventUtil::BatchDeleteNoHostDBForms(uid, noHostFormDbMap, removedFormsMap);
81     FormEventUtil::BatchDeleteNoHostTempForms(uid, noHostFormDbMap, removedFormsMap);
82     FormEventUtil::ReCreateForm(formId);
83     FormTimerCfg cfg;
84     FormEventUtil::HandleTimerUpdate(formId, formRecord, cfg);
85     formSysEventReceiver.HandleUserIdRemoved(userIds);
86     formSysEventReceiver.HandleBundleScanFinished();
87     eventData.SetCode(userIds);
88     return FormEventUtil::ProviderFormUpdated(formId, formRecord, targetForms, bundleInfo);
89 }
90 }
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
94 {
95     /* Run your code on data */
96     if (data == nullptr) {
97         return 0;
98     }
99 
100     if (size < OHOS::U32_AT_SIZE) {
101         return 0;
102     }
103 
104     /* Validate the length of size */
105     if (size == 0 || size > OHOS::FOO_MAX_LEN) {
106         return 0;
107     }
108 
109     char* ch = static_cast<char*>(malloc(size + 1));
110     if (ch == nullptr) {
111         return 0;
112     }
113 
114     (void)memset_s(ch, size + 1, 0x00, size + 1);
115     if (memcpy_s(ch, size + 1, data, size) != EOK) {
116         free(ch);
117         ch = nullptr;
118         return 0;
119     }
120 
121     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
122     free(ch);
123     ch = nullptr;
124     return 0;
125 }
126 
127