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 "bundlereshostimpl_fuzzer.h"
18
19 #include "bundle_resource_host_impl.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::shared_ptr<BundleResourceHostImpl> bundleResourceHostImpl = std::make_shared<BundleResourceHostImpl>();
33 std::string bundleName(data, size);
34 BundleResourceInfo info;
35 bundleResourceHostImpl->GetBundleResourceInfo(bundleName, 0, info);
36 std::vector<LauncherAbilityResourceInfo> launcherInfos;
37 bundleResourceHostImpl->GetLauncherAbilityResourceInfo(bundleName, 0, launcherInfos);
38 std::vector<BundleResourceInfo> infos;
39 bundleResourceHostImpl->GetAllBundleResourceInfo(0, infos);
40 bundleResourceHostImpl->GetAllLauncherAbilityResourceInfo(0, launcherInfos);
41 bundleResourceHostImpl->AddResourceInfoByBundleName(bundleName, USERID);
42 bundleResourceHostImpl->AddResourceInfoByAbility(bundleName, MODULE_NAME, ABILITY_NAME, USERID);
43 std::string key(data, size);
44 bundleResourceHostImpl->DeleteResourceInfo(key);
45 int32_t appIndex = 1;
46 bundleResourceHostImpl->CheckBundleNameValid(bundleName, appIndex);
47 return true;
48 }
49 }
50
51 // Fuzzer entry point.
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)52 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
53 {
54 /* Run your code on data */
55 if (data == nullptr) {
56 return 0;
57 }
58
59 if (size < OHOS::U32_AT_SIZE) {
60 return 0;
61 }
62
63 char* ch = static_cast<char*>(malloc(size + 1));
64 if (ch == nullptr) {
65 return 0;
66 }
67
68 (void)memset_s(ch, size + 1, 0x00, size + 1);
69 if (memcpy_s(ch, size, data, size) != EOK) {
70 free(ch);
71 ch = nullptr;
72 return 0;
73 }
74 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
75 free(ch);
76 ch = nullptr;
77 return 0;
78 }