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 #define private public
17 #include "bundleresmgr_fuzzer.h"
18
19 #include "bundle_resource_manager.h"
20 #include "securec.h"
21
22 using namespace OHOS::AppExecFwk;
23 namespace OHOS {
24 constexpr size_t U32_AT_SIZE = 4;
25 constexpr uint32_t CODE_MAX = 8;
26 const int32_t USERID = 100;
27 const std::string MODULE_NAME = "entry";
28 const std::string ABILITY_NAME = "com.example.bmsaccesstoken1.MainAbility";
29 const std::string BUNDLE_NAME_NO_ICON = "com.third.hiworld.example1";
30
DoSomethingInterestingWithMyAPI(const char * data,size_t size)31 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
32 {
33 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
34 std::string bundleName(data, size);
35 manager->DeleteResourceInfo(bundleName);
36 manager->AddAllResourceInfo(USERID, 0);
37 manager->DeleteAllResourceInfo();
38 std::vector<std::string> keyNames;
39 manager->GetAllResourceName(keyNames);
40 BundleResourceInfo info;
41 manager->GetBundleResourceInfo(bundleName, 0, info);
42 std::vector<LauncherAbilityResourceInfo> launcherInfos;
43 manager->GetLauncherAbilityResourceInfo(bundleName,
44 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL), launcherInfos);
45 std::vector<BundleResourceInfo> infos;
46 manager->GetAllBundleResourceInfo(0, infos);
47 manager->GetAllLauncherAbilityResourceInfo(0, launcherInfos);
48 std::vector<ResourceInfo> resourceInfos;
49 std::string targetBundleName(data, size);
50 manager->GetTargetBundleName(bundleName, targetBundleName);
51 ResourceInfo resourceInfo;
52 resourceInfo.bundleName_ = bundleName;
53 resourceInfo.label_ = bundleName;
54 int32_t appIndex = 1;
55 manager->DeleteNotExistResourceInfo();
56 std::map<std::string, std::vector<ResourceInfo>> resourceInfosMap;
57 resourceInfosMap[BUNDLE_NAME_NO_ICON] = resourceInfos;
58 manager->ProcessResourceInfoWhenParseFailed(resourceInfo);
59 manager->GetDefaultIcon(resourceInfo);
60 manager->CheckResourceFlags(static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE));
61 manager->SendBundleResourcesChangedEvent(USERID,
62 static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE));
63 manager->InnerProcessResourceInfoBySystemLanguageChanged(resourceInfosMap);
64 manager->InnerProcessResourceInfoBySystemThemeChanged(resourceInfosMap, USERID);
65 std::vector<std::string> existResourceNames;
66 manager->DeleteNotExistResourceInfo(resourceInfosMap, existResourceNames);
67 manager->GetBundleResourceInfoForCloneBundle(bundleName, appIndex,
68 static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE), resourceInfos);
69 manager->DeleteNotExistResourceInfo(bundleName, appIndex, resourceInfos);
70 manager->ProcessResourceInfoNoNeedToParseOtherIcon(resourceInfos);
71 return true;
72 }
73 }
74
75 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78 /* Run your code on data */
79 if (data == nullptr) {
80 return 0;
81 }
82
83 if (size < OHOS::U32_AT_SIZE) {
84 return 0;
85 }
86
87 char* ch = static_cast<char*>(malloc(size + 1));
88 if (ch == nullptr) {
89 return 0;
90 }
91
92 (void)memset_s(ch, size + 1, 0x00, size + 1);
93 if (memcpy_s(ch, size, data, size) != EOK) {
94 free(ch);
95 ch = nullptr;
96 return 0;
97 }
98 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
99 free(ch);
100 ch = nullptr;
101 return 0;
102 }