• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "submanagershelper_fuzzer.h"
17 #include "sub_managers_helper.h"
18 
19 #include <dlfcn.h>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 using namespace OHOS::AAFwk;
23 
24 namespace OHOS {
25 namespace AAFwk {
26 constexpr int32_t USER0_ID = 0;
27 constexpr size_t STRING_MAX_LENGTH = 128;
28 constexpr size_t U32_AT_SIZE = 4;
29 }
30 
GetFuzzAbilityToken()31 sptr<Token> GetFuzzAbilityToken()
32 {
33     sptr<Token> token = nullptr;
34     AbilityRequest abilityRequest;
35     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
36     abilityRequest.abilityInfo.name = "MainAbility";
37     abilityRequest.abilityInfo.type = AbilityType::DATA;
38     std::shared_ptr<AbilityRecord> abilityRecord =
39         AbilityRecord::CreateAbilityRecord(abilityRequest);
40     if (abilityRecord) {
41         token = abilityRecord->GetToken();
42     }
43     return token;
44 }
45 
SubManagersHelperFuzztest(bool switchUser,int64_t abilityRecordId,int32_t uid,const std::string & bundleName)46 void SubManagersHelperFuzztest(bool switchUser, int64_t abilityRecordId,
47     int32_t uid, const std::string &bundleName)
48 {
49     std::shared_ptr<SubManagersHelper> helper =
50         std::make_shared<SubManagersHelper>(nullptr, nullptr);
51     sptr<IAbilityScheduler> scheduler;
52     sptr<IRemoteObject> token = GetFuzzAbilityToken();
53 
54     helper->InitSubManagers(USER0_ID, switchUser);
55     helper->InitConnectManager(USER0_ID, switchUser);
56     helper->InitPendWantManager(USER0_ID, switchUser);
57     helper->InitUIAbilityManager(USER0_ID, switchUser);
58     helper->ClearSubManagers(USER0_ID);
59     helper->GetDataAbilityManager(scheduler);
60     helper->GetDataAbilityManagerByUserId(USER0_ID);
61     helper->GetDataAbilityManagerByToken(token);
62     helper->GetConnectManagerByToken(token);
63     helper->GetConnectManagerByAbilityRecordId(abilityRecordId);
64 }
65 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)66 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
67 {
68     FuzzedDataProvider fdp(data, size);
69     bool switchUser = fdp.ConsumeBool();
70     int64_t abilityRecordId = fdp.ConsumeIntegral<int64_t>();
71     int32_t uid = fdp.ConsumeIntegral<int32_t>();
72     std::string bundleName = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
73     SubManagersHelperFuzztest(switchUser, abilityRecordId, uid, bundleName);
74 
75     return true;
76 }
77 }
78 
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82     // Run your code on data.
83     OHOS::DoSomethingInterestingWithMyAPI(data, size);
84     return 0;
85 }