• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundleresprocess_fuzzer.h"
18 
19 #include "bundle_resource_process.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 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)30 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
31 {
32     std::string bundleName(data, size);
33     std::vector<ResourceInfo> resourceInfos;
34     BundleResourceProcess::GetResourceInfoByBundleName(bundleName, USERID, resourceInfos);
35     ResourceInfo resourceInfo;
36     BundleResourceProcess::GetLauncherResourceInfoByAbilityName(bundleName, MODULE_NAME, ABILITY_NAME,
37         USERID, resourceInfo);
38     std::map<std::string, std::vector<ResourceInfo>> resourceMapInfos;
39     BundleResourceProcess::GetAllResourceInfo(USERID, resourceMapInfos);
40     std::vector<std::string> resourceNames;
41     BundleResourceProcess::GetResourceInfoByColorModeChanged(resourceNames, USERID, resourceInfos);
42     std::string targetBundleName(data, size);
43     BundleResourceProcess::GetTargetBundleName(bundleName, targetBundleName);
44     ApplicationInfo applicationInfo;
45     applicationInfo.bundleName = bundleName;
46     applicationInfo.bundleType = BundleType::APP;
47     InnerBundleInfo bundleInfo;
48     bundleInfo.SetBaseApplicationInfo(applicationInfo);
49     BundleResourceProcess::GetBundleResourceInfo(bundleInfo, USERID, resourceInfo);
50     BundleResourceProcess::GetLauncherAbilityResourceInfos(bundleInfo, USERID, resourceInfos);
51     bundleInfo.SetOverlayType(OverlayType::OVERLAY_INTERNAL_BUNDLE);
52     BundleResourceProcess::GetAbilityResourceInfos(bundleInfo, USERID, resourceInfos);
53     BundleResourceProcess::ConvertToBundleResourceInfo(bundleInfo);
54     BundleResourceProcess::InnerGetResourceInfo(bundleInfo, USERID, resourceInfos);
55     BundleResourceProcess::OnGetResourceInfo(bundleInfo, USERID, resourceInfos);
56     applicationInfo.hideDesktopIcon = false;
57     bundleInfo.SetBaseApplicationInfo(applicationInfo);
58     BundleResourceProcess::CheckIsNeedProcessAbilityResource(bundleInfo);
59     InnerBundleUserInfo innerUserInfo;
60     innerUserInfo.bundleUserInfo.userId = USERID;
61     innerUserInfo.bundleUserInfo.overlayModulesState.push_back("1_overlay_1");
62     innerUserInfo.bundleUserInfo.overlayModulesState.push_back("2_overlay_2");
63     innerUserInfo.bundleName = bundleName;
64     bundleInfo.AddInnerBundleUserInfo(innerUserInfo);
65     std::vector<std::string> overlayHapPaths;
66     BundleResourceProcess::GetOverlayModuleHapPaths(bundleInfo, MODULE_NAME, USERID, overlayHapPaths);
67     BundleResourceProcess::ChangeDynamicIcon(resourceInfos, resourceInfo);
68     BundleResourceProcess::GetDynamicIcon(bundleInfo, resourceInfo);
69     return true;
70 }
71 }
72 
73 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76     /* Run your code on data */
77     if (data == nullptr) {
78         return 0;
79     }
80 
81     if (size < OHOS::U32_AT_SIZE) {
82         return 0;
83     }
84 
85     char* ch = static_cast<char*>(malloc(size + 1));
86     if (ch == nullptr) {
87         return 0;
88     }
89 
90     (void)memset_s(ch, size + 1, 0x00, size + 1);
91     if (memcpy_s(ch, size, data, size) != EOK) {
92         free(ch);
93         ch = nullptr;
94         return 0;
95     }
96     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
97     free(ch);
98     ch = nullptr;
99     return 0;
100 }