1 /*
2 * Copyright (c) 2024 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 "bundlemgrhelper_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #define protected public
23 #include "bundle_mgr_helper.h"
24 #undef protected
25 #undef private
26
27 #include "ability_record.h"
28
29 using namespace OHOS::AAFwk;
30 using namespace OHOS::AppExecFwk;
31 using namespace OHOS::AbilityRuntime;
32
33 namespace OHOS {
34 namespace {
35 constexpr int INPUT_ZERO = 0;
36 constexpr int INPUT_ONE = 1;
37 constexpr int INPUT_THREE = 3;
38 constexpr size_t U32_AT_SIZE = 4;
39 constexpr uint8_t ENABLE = 2;
40 constexpr size_t OFFSET_ZERO = 24;
41 constexpr size_t OFFSET_ONE = 16;
42 constexpr size_t OFFSET_TWO = 8;
43 }
44
GetU32Data(const char * ptr)45 uint32_t GetU32Data(const char* ptr)
46 {
47 // convert fuzz input data to an integer
48 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[ENABLE] << OFFSET_TWO) |
49 ptr[INPUT_THREE];
50 }
51
GetFuzzAbilityToken()52 sptr<Token> GetFuzzAbilityToken()
53 {
54 sptr<Token> token = nullptr;
55 AbilityRequest abilityRequest;
56 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
57 abilityRequest.abilityInfo.name = "MainAbility";
58 abilityRequest.abilityInfo.type = AbilityType::DATA;
59 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
60 if (abilityRecord) {
61 token = abilityRecord->GetToken();
62 }
63 return token;
64 }
65
BundleMgrHelperFuzztest1(bool boolParam,std::string & stringParam,int32_t int32Param)66 void BundleMgrHelperFuzztest1(bool boolParam, std::string &stringParam, int32_t int32Param)
67 {
68 std::shared_ptr<BundleMgrHelper> bmHelper = std::make_shared<BundleMgrHelper>(); // branch constructor
69 bmHelper->GetNameForUid(int32Param, stringParam); // branch
70 BundleInfo bundleInfo;
71 bmHelper->GetBundleInfo(stringParam, boolParam, bundleInfo, int32Param); // branch
72 bmHelper->InstallSandboxApp(stringParam, int32Param, int32Param, int32Param); // branch
73 bmHelper->UninstallSandboxApp(stringParam, int32Param, int32Param); // branch
74 bmHelper->GetUninstalledBundleInfo(stringParam, bundleInfo); // branch
75 bmHelper->GetSandboxBundleInfo(stringParam, int32Param, int32Param, bundleInfo); // branch
76 Want want;
77 AbilityInfo abilityInfo;
78 bmHelper->GetSandboxAbilityInfo(want, int32Param, int32Param, int32Param, abilityInfo);
79 std::vector<ExtensionAbilityInfo> extensionInfos;
80 bmHelper->GetSandboxExtAbilityInfos(want, int32Param, int32Param, int32Param, extensionInfos);
81 HapModuleInfo hapModuleInfo;
82 bmHelper->GetSandboxHapModuleInfo(abilityInfo, int32Param, int32Param, hapModuleInfo);
83 bmHelper->Connect();
84 bmHelper->ConnectBundleInstaller();
85 bmHelper->OnDeath();
86 bmHelper->GetBundleInfo(stringParam, int32Param, bundleInfo, int32Param);
87 bmHelper->GetHapModuleInfo(abilityInfo, hapModuleInfo);
88 bmHelper->GetAbilityLabel(stringParam, stringParam);
89 bmHelper->GetAppType(stringParam);
90 std::vector<BaseSharedBundleInfo> baseSharedBundleInfos;
91 bmHelper->GetBaseSharedBundleInfos(
92 stringParam, baseSharedBundleInfos, static_cast<GetDependentBundleInfoFlag>(int32Param));
93 bmHelper->GetBundleInfoForSelf(int32Param, bundleInfo);
94 bmHelper->GetDependentBundleInfo(stringParam, bundleInfo, static_cast<GetDependentBundleInfoFlag>(int32Param));
95 bmHelper->GetGroupDir(stringParam, stringParam);
96 bmHelper->GetOverlayManagerProxy();
97 bmHelper->QueryAbilityInfo(want, abilityInfo);
98 bmHelper->QueryAbilityInfo(want, int32Param, int32Param, abilityInfo);
99 std::vector<BundleInfo> bundleInfos;
100 bmHelper->GetBundleInfos(int32Param, bundleInfos, int32Param);
101 bmHelper->GetBundleInfos(static_cast<BundleFlag>(int32Param), bundleInfos, int32Param);
102 bmHelper->GetQuickFixManagerProxy();
103 bmHelper->ProcessPreload(want);
104 bmHelper->GetAppControlProxy();
105 bmHelper->QueryExtensionAbilityInfos(want, int32Param, int32Param, extensionInfos);
106 bmHelper->GetBundleInfoV9(stringParam, int32Param, bundleInfo, int32Param);
107 }
108
BundleMgrHelperFuzztest2(bool boolParam,std::string & stringParam,int32_t int32Param)109 void BundleMgrHelperFuzztest2(bool boolParam, std::string &stringParam, int32_t int32Param)
110 {
111 std::shared_ptr<BundleMgrHelper> bmHelper = std::make_shared<BundleMgrHelper>(); // branch constructor
112 ApplicationInfo appInfo;
113 Want want;
114 bmHelper->GetApplicationInfo(stringParam, static_cast<ApplicationFlag>(int32Param), int32Param, appInfo);
115 bmHelper->GetApplicationInfo(stringParam, int32Param, int32Param, appInfo);
116 bmHelper->GetApplicationInfoWithAppIndex(stringParam, int32Param, int32Param, appInfo);
117 bmHelper->UnregisterBundleEventCallback(nullptr); // branch null
118 ExtensionAbilityInfo extensionAbilityInfo;
119 bmHelper->QueryExtensionAbilityInfoByUri(stringParam, int32Param, extensionAbilityInfo);
120 AbilityInfo abilityInfo;
121 bmHelper->ImplicitQueryInfoByPriority(want, int32Param, int32Param, abilityInfo, extensionAbilityInfo);
122 bmHelper->QueryAbilityInfoByUri(stringParam, int32Param, abilityInfo);
123 bmHelper->QueryAbilityInfo(want, int32Param, int32Param, abilityInfo, nullptr);
124 bmHelper->UpgradeAtomicService(want, int32Param);
125 std::vector<AbilityInfo> abilityInfos;
126 std::vector<ExtensionAbilityInfo> extensionInfos;
127 bmHelper->ImplicitQueryInfos(want, int32Param, int32Param, boolParam, abilityInfos, extensionInfos, boolParam);
128 bmHelper->CleanBundleDataFiles(stringParam, int32Param, int32Param);
129 std::vector<DataGroupInfo> infos;
130 bmHelper->QueryDataGroupInfos(stringParam, int32Param, infos);
131 bmHelper->RegisterBundleEventCallback(nullptr);
132 HapModuleInfo hapModuleInfo;
133 bmHelper->GetHapModuleInfo(abilityInfo, int32Param, hapModuleInfo);
134 bmHelper->QueryAppGalleryBundleName(stringParam);
135 bmHelper->GetUidByBundleName(stringParam, int32Param, int32Param);
136 bmHelper->QueryExtensionAbilityInfosOnlyWithTypeName(stringParam, int32Param, int32Param, extensionInfos);
137 bmHelper->GetDefaultAppProxy();
138 bmHelper->GetJsonProfile(static_cast<ProfileType>(int32Param), stringParam, stringParam, stringParam, int32Param);
139 bmHelper->GetLaunchWantForBundle(stringParam, want, int32Param);
140 ElementName element;
141 bmHelper->QueryCloneAbilityInfo(element, int32Param, int32Param, abilityInfo, int32Param);
142 BundleInfo bundleInfo;
143 bmHelper->GetCloneBundleInfo(stringParam, int32Param, int32Param, bundleInfo, int32Param);
144 ExtensionAbilityInfo extensionInfo;
145 bmHelper->QueryCloneExtensionAbilityInfoWithAppIndex(element, int32Param, int32Param, extensionInfo, int32Param);
146 }
147
DoSomethingInterestingWithMyAPI(const char * data,size_t size)148 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
149 {
150 bool boolParam = *data % ENABLE;
151 std::string stringParam(data, size);
152 int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
153 BundleMgrHelperFuzztest1(boolParam, stringParam, int32Param);
154 BundleMgrHelperFuzztest2(boolParam, stringParam, int32Param);
155 return true;
156 }
157 }
158
159 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)160 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
161 {
162 /* Run your code on data */
163 if (data == nullptr) {
164 return 0;
165 }
166
167 /* Validate the length of size */
168 if (size < OHOS::U32_AT_SIZE) {
169 return 0;
170 }
171
172 char* ch = static_cast<char*>(malloc(size + 1));
173 if (ch == nullptr) {
174 std::cout << "malloc failed." << std::endl;
175 return 0;
176 }
177
178 (void)memset_s(ch, size + 1, 0x00, size + 1);
179 if (memcpy_s(ch, size, data, size) != EOK) {
180 std::cout << "copy failed." << std::endl;
181 free(ch);
182 ch = nullptr;
183 return 0;
184 }
185
186 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
187 free(ch);
188 ch = nullptr;
189 return 0;
190 }
191
192