• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "dataabilitymanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #define private public
23 #include "data_ability_manager.h"
24 #undef private
25 
26 #include "ability_record.h"
27 
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::AppExecFwk;
30 
31 namespace OHOS {
32 namespace {
33 constexpr size_t STRING_MAX_LENGTH = 128;
34 }
35 
GetU32Data(const char * ptr)36 uint32_t GetU32Data(const char* ptr)
37 {
38     // convert fuzz input data to an integer
39     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
40 }
41 
GetFuzzAbilityRecord()42 std::shared_ptr<AbilityRecord> GetFuzzAbilityRecord()
43 {
44     sptr<Token> token = nullptr;
45     AbilityRequest abilityRequest;
46     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
47     abilityRequest.abilityInfo.name = "MainAbility";
48     abilityRequest.abilityInfo.type = AbilityType::DATA;
49     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
50     if (!abilityRecord) {
51         return nullptr;
52     }
53     return abilityRecord;
54 }
55 
GetFuzzAbilityToken()56 sptr<Token> GetFuzzAbilityToken()
57 {
58     sptr<Token> token = nullptr;
59     std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
60     if (abilityRecord) {
61         token = abilityRecord->GetToken();
62     }
63     return token;
64 }
65 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)66 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
67 {
68     auto dataAbilityManager = std::make_shared<DataAbilityManager>();
69     bool boolParam;
70     bool isClient;
71     int intParam;
72     int32_t int32Param;
73     int64_t int64Param;
74     std::string stringParam;
75     std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
76     sptr<IRemoteObject> token = GetFuzzAbilityToken();
77     std::vector<std::string> info;
78     AbilityRequest abilityRequest;
79     FuzzedDataProvider fdp(data, size);
80     boolParam = fdp.ConsumeBool();
81     isClient = fdp.ConsumeBool();
82     intParam = fdp.ConsumeIntegral<int>();
83     int32Param = fdp.ConsumeIntegral<int32_t>();
84     int64Param = fdp.ConsumeIntegral<int64_t>();
85     stringParam = fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH);
86     sptr<IRemoteObject> client;
87     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
88     abilityRequest.abilityInfo.type = AppExecFwk::AbilityType::DATA;
89     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
90     abilityRequest.abilityInfo.type = AppExecFwk::AbilityType::UNKNOWN;
91     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
92     abilityRequest.abilityInfo.bundleName = "";
93     abilityRequest.abilityInfo.name = "";
94     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
95     boolParam = true;
96     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
97     boolParam = false;
98     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
99     sptr<IAbilityScheduler> scheduler;
100     dataAbilityManager->Release(scheduler, client, boolParam);
101     dataAbilityManager->ContainsDataAbility(scheduler);
102     dataAbilityManager->AttachAbilityThread(scheduler, token);
103     dataAbilityManager->AbilityTransitionDone(token, intParam);
104     dataAbilityManager->OnAbilityRequestDone(token, int32Param);
105     dataAbilityManager->OnAbilityDied(abilityRecord);
106     AppInfo appInfo;
107     dataAbilityManager->OnAppStateChanged(appInfo);
108     appInfo.bundleName = abilityRecord->GetApplicationInfo().bundleName;
109     appInfo.appIndex = abilityRecord->GetAppIndex();
110     appInfo.instanceKey = abilityRecord->GetInstanceKey();
111     dataAbilityManager->OnAppStateChanged(appInfo);
112     dataAbilityManager->GetAbilityRecordById(int64Param);
113     dataAbilityManager->GetAbilityRecordByToken(token);
114     dataAbilityManager->GetAbilityRecordByScheduler(scheduler);
115     char *func = new char[stringParam.length() + 1];
116     dataAbilityManager->Dump(func, intParam);
117     dataAbilityManager->LoadLocked(stringParam, abilityRequest);
118     dataAbilityManager->DumpLocked(func, intParam);
119     dataAbilityManager->DumpState(info, stringParam);
120     std::shared_ptr<DataAbilityRecord> record;
121     dataAbilityManager->DumpClientInfo(info, isClient, record);
122     dataAbilityManager->DumpSysState(info, boolParam, stringParam);
123     std::vector<AbilityRunningInfo> AbilityRunningInfoVector;
124     dataAbilityManager->GetAbilityRunningInfos(AbilityRunningInfoVector, boolParam);
125     dataAbilityManager->RestartDataAbility(abilityRecord);
126     dataAbilityManager->ReportDataAbilityAcquired(client, boolParam, record);
127     dataAbilityManager->ReportDataAbilityReleased(client, boolParam, record);
128 
129     return true;
130 }
131 }
132 
133 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)134 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
135 {
136     // Run your code on data.
137     OHOS::DoSomethingInterestingWithMyAPI(data, size);
138     return 0;
139 }