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