1 /*
2 * Copyright (c) 2022-2025 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 "abilityapppreloader_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <iostream>
21 #include <fuzzer/FuzzedDataProvider.h>
22
23 #define private public
24 #define protected public
25 #include "app_preloader.h"
26 #include "bundle_mgr_helper.h"
27 #undef protected
28 #undef private
29
30 #include "configuration.h"
31 #include "parcel.h"
32 #include "securec.h"
33
34 using namespace OHOS::AppExecFwk;
35
36 namespace OHOS {
37 namespace {
38 constexpr size_t STRING_MAX_LENGTH = 128;
39 }
40
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)41 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
42 {
43 std::shared_ptr<RemoteClientManager> remoteClientManager = std::make_shared<RemoteClientManager>();
44 auto bundleMgrHelper = std::make_shared<AppExecFwk::BundleMgrHelper>();
45 remoteClientManager->SetBundleManagerHelper(bundleMgrHelper);
46 auto appPreloader = std::make_shared<AppPreloader>(remoteClientManager);
47 std::string bundleName;
48 int32_t userId;
49 int32_t appIndex;
50 PreloadRequest request;
51 AAFwk::Want launchWant;
52 AbilityInfo abilityInfo;
53 BundleInfo bundleInfo;
54 HapModuleInfo hapModuleInfo;
55 FuzzedDataProvider fdp(data, size);
56 bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
57 userId = fdp.ConsumeIntegral<int32_t>();
58 appIndex = fdp.ConsumeIntegral<int32_t>();
59 appPreloader->GeneratePreloadRequest(bundleName, userId, appIndex, request);
60 appPreloader->GetBundleManagerHelper();
61 appPreloader->CheckPreloadConditions(abilityInfo);
62 appPreloader->GetLaunchWant(bundleName, userId, launchWant);
63 appPreloader->GetLaunchAbilityInfo(launchWant, userId, abilityInfo);
64 appPreloader->GetBundleAndHapInfo(bundleName, userId, abilityInfo, bundleInfo, hapModuleInfo);
65 appPreloader->PreCheck(bundleName, PreloadMode::PRE_MAKE);
66 return true;
67 }
68 }
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 // Run your code on data.
74 OHOS::DoSomethingInterestingWithMyAPI(data, size);
75 return 0;
76 }