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 "formdatamgrfour_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #define private public
23 #define protected public
24 #include "data_center/form_data_mgr.h"
25 #undef private
26 #undef protected
27 #include "securec.h"
28
29 using namespace OHOS::AppExecFwk;
30
31 namespace OHOS {
32 constexpr size_t U32_AT_SIZE = 4;
DoSomethingInterestingWithMyAPI(FuzzedDataProvider * fdp)33 bool DoSomethingInterestingWithMyAPI(FuzzedDataProvider *fdp)
34 {
35 if (fdp == nullptr) {
36 return true;
37 }
38 int32_t userId = fdp->ConsumeIntegral<int32_t>();
39 int64_t formId = fdp->ConsumeIntegral<int64_t>();
40 std::vector<int64_t> removedFormIds;
41 removedFormIds.emplace_back(formId);
42 FormDataMgr::GetInstance().DeleteFormsByUserId(userId, removedFormIds);
43 FormDataMgr::GetInstance().ClearFormRecords();
44 int32_t callingUid = fdp->ConsumeIntegral<int32_t>();
45 std::set<int64_t> matchedFormIds;
46 matchedFormIds.insert(formId);
47 std::string bundleName = fdp->ConsumeRandomLengthString();
48 std::string abilityName = fdp->ConsumeRandomLengthString();
49 FormIdKey formIdKey(bundleName, abilityName);
50 std::map<FormIdKey, std::set<int64_t>> noHostTempFormsMap;
51 noHostTempFormsMap.emplace(formIdKey, matchedFormIds);
52 std::map<int64_t, bool> foundFormsMap;
53 bool flag = fdp->ConsumeBool();
54 foundFormsMap.emplace(formId, flag);
55 FormDataMgr::GetInstance().GetNoHostInvalidTempForms(userId,
56 callingUid, matchedFormIds, noHostTempFormsMap, foundFormsMap);
57 FormDataMgr::GetInstance().BatchDeleteNoHostTempForms(callingUid, noHostTempFormsMap, foundFormsMap);
58 FormDataMgr::GetInstance().DeleteInvalidTempForms(userId, callingUid, matchedFormIds, foundFormsMap);
59 FormDataMgr::GetInstance().DeleteInvalidPublishForms(userId, bundleName, matchedFormIds);
60 FormDataMgr::GetInstance().ClearHostDataByInvalidForms(callingUid, foundFormsMap);
61 Want want;
62 std::unique_ptr<FormProviderData> formProviderData = nullptr;
63 FormDataMgr::GetInstance().AddRequestPublishFormInfo(formId, want, formProviderData);
64 FormDataMgr::GetInstance().RemoveRequestPublishFormInfo(formId);
65 FormDataMgr::GetInstance().IsRequestPublishForm(formId);
66 FormDataMgr::GetInstance().GetRequestPublishFormInfo(formId, want, formProviderData);
67 FormRecord record;
68 BundlePackInfo bundlePackInfo;
69 AbilityFormInfo abilityFormInfo;
70 FormDataMgr::GetInstance().GetPackageForm(record, bundlePackInfo, abilityFormInfo);
71 FormDataMgr::GetInstance().IsSameForm(record, abilityFormInfo);
72 bool isNeedFreeInstall = fdp->ConsumeBool();
73 FormDataMgr::GetInstance().SetRecordNeedFreeInstall(formId, isNeedFreeInstall);
74 return true;
75 }
76 }
77
78 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)79 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
80 {
81 /* Run your code on data */
82 if (data == nullptr) {
83 return 0;
84 }
85
86 if (size < OHOS::U32_AT_SIZE) {
87 return 0;
88 }
89
90 char* ch = static_cast<char*>(malloc(size + 1));
91 if (ch == nullptr) {
92 return 0;
93 }
94
95 (void)memset_s(ch, size + 1, 0x00, size + 1);
96 if (memcpy_s(ch, size + 1, data, size) != EOK) {
97 free(ch);
98 ch = nullptr;
99 return 0;
100 }
101
102 FuzzedDataProvider fdp(data, size);
103 OHOS::DoSomethingInterestingWithMyAPI(&fdp);
104 free(ch);
105 ch = nullptr;
106 return 0;
107 }