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->AddResourceInfoByBundleName(bundleName, USERID);
36 manager->DeleteResourceInfo(bundleName);
37 manager->AddAllResourceInfo(USERID, 0, 0);
38 manager->DeleteAllResourceInfo();
39 manager->AddResourceInfoByAbility(bundleName, MODULE_NAME, ABILITY_NAME, USERID);
40 std::vector<std::string> keyNames;
41 manager->GetAllResourceName(keyNames);
42 BundleResourceInfo info;
43 manager->GetBundleResourceInfo(bundleName, 0, info);
44 std::vector<LauncherAbilityResourceInfo> launcherInfos;
45 manager->GetLauncherAbilityResourceInfo(bundleName,
46 static_cast<uint32_t>(ResourceFlag::GET_RESOURCE_INFO_ALL), launcherInfos);
47 std::vector<BundleResourceInfo> infos;
48 manager->GetAllBundleResourceInfo(0, infos);
49 manager->GetAllLauncherAbilityResourceInfo(0, launcherInfos);
50 std::vector<ResourceInfo> resourceInfos;
51 manager->SaveResourceInfos(resourceInfos);
52 std::string targetBundleName(data, size);
53 manager->GetTargetBundleName(bundleName, targetBundleName);
54 ResourceInfo resourceInfo;
55 resourceInfo.bundleName_ = bundleName;
56 resourceInfo.label_ = bundleName;
57 manager->UpdateBundleIcon(bundleName, resourceInfo);
58 int32_t appIndex = 1;
59 manager->AddCloneBundleResourceInfo(bundleName, appIndex);
60 manager->DeleteCloneBundleResourceInfo(bundleName, appIndex);
61 manager->DeleteNotExistResourceInfo();
62 manager->AddResourceInfo(USERID, resourceInfo);
63 manager->AddResourceInfos(USERID, resourceInfos);
64 std::map<std::string, std::vector<ResourceInfo>> resourceInfosMap;
65 resourceInfosMap[BUNDLE_NAME_NO_ICON] = resourceInfos;
66 manager->AddResourceInfosByMap(resourceInfosMap, manager->currentTaskNum_,
67 static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE), USERID, USERID);
68 manager->InnerProcessResourceInfoByResourceUpdateType(resourceInfosMap,
69 static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE), USERID, USERID);
70 manager->ProcessResourceInfoWhenParseFailed(resourceInfo);
71 manager->ProcessResourceInfo(resourceInfos, resourceInfo);
72 manager->GetDefaultIcon(resourceInfo);
73 manager->CheckResourceFlags(static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE));
74 manager->SendBundleResourcesChangedEvent(USERID,
75 static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_LANGUE_CHANGE));
76 manager->InnerProcessResourceInfoBySystemLanguageChanged(resourceInfosMap);
77 manager->InnerProcessResourceInfoBySystemThemeChanged(resourceInfosMap, USERID);
78 manager->InnerProcessResourceInfoByUserIdChanged(resourceInfosMap, USERID, USERID);
79 std::vector<std::string> existResourceNames;
80 manager->DeleteNotExistResourceInfo(resourceInfosMap, existResourceNames);
81 manager->InnerProcessWhetherThemeExist(bundleName, USERID);
82 manager->GetBundleResourceInfoForCloneBundle(bundleName, appIndex, resourceInfos);
83 manager->UpdateCloneBundleResourceInfo(bundleName, appIndex,
84 static_cast<uint32_t>(BundleResourceChangeType::SYSTEM_THEME_CHANGE));
85 manager->DeleteNotExistResourceInfo(bundleName, appIndex, resourceInfos);
86 manager->ProcessResourceInfoNoNeedToParseOtherIcon(resourceInfos);
87 return true;
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, data, size) != EOK) {
110 free(ch);
111 ch = nullptr;
112 return 0;
113 }
114 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
115 free(ch);
116 ch = nullptr;
117 return 0;
118 }