• 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 #include "dataabilitymanager_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #define private public
22 #include "data_ability_manager.h"
23 #undef private
24 
25 #include "ability_record.h"
26 
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AppExecFwk;
29 
30 namespace OHOS {
31 namespace {
32 constexpr size_t U32_AT_SIZE = 4;
33 constexpr uint8_t ENABLE = 2;
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 char * data,size_t size)66 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
67 {
68     bool boolParam = *data % ENABLE;
69     int intParam = static_cast<int>(GetU32Data(data));
70     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
71     int64_t int64Param = static_cast<int64_t>(GetU32Data(data));
72     std::string stringParam(data, size);
73     std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
74     sptr<IRemoteObject> token = GetFuzzAbilityToken();
75     std::vector<std::string> info;
76     AbilityRequest abilityRequest;
77 
78     // fuzz for DataAbilityManager
79     auto dataAbilityManager = std::make_shared<DataAbilityManager>();
80     sptr<IRemoteObject> client;
81     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
82     sptr<IAbilityScheduler> scheduler;
83     dataAbilityManager->Release(scheduler, client, boolParam);
84     dataAbilityManager->ContainsDataAbility(scheduler);
85     dataAbilityManager->AttachAbilityThread(scheduler, token);
86     dataAbilityManager->AbilityTransitionDone(token, intParam);
87     dataAbilityManager->OnAbilityRequestDone(token, int32Param);
88     dataAbilityManager->OnAbilityDied(abilityRecord);
89     AppInfo appInfo;
90     dataAbilityManager->OnAppStateChanged(appInfo);
91     dataAbilityManager->GetAbilityRecordById(int64Param);
92     dataAbilityManager->GetAbilityRecordByToken(token);
93     dataAbilityManager->GetAbilityRecordByScheduler(scheduler);
94     dataAbilityManager->Dump(data, intParam);
95     dataAbilityManager->LoadLocked(stringParam, abilityRequest);
96     dataAbilityManager->DumpLocked(data, intParam);
97     dataAbilityManager->DumpState(info, stringParam);
98     dataAbilityManager->DumpSysState(info, boolParam, stringParam);
99     std::vector<AbilityRunningInfo> AbilityRunningInfoVector;
100     dataAbilityManager->GetAbilityRunningInfos(AbilityRunningInfoVector, boolParam);
101     dataAbilityManager->RestartDataAbility(abilityRecord);
102     std::shared_ptr<DataAbilityRecord> record;
103     dataAbilityManager->ReportDataAbilityAcquired(client, boolParam, record);
104     dataAbilityManager->ReportDataAbilityReleased(client, boolParam, record);
105 
106     return true;
107 }
108 }
109 
110 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)111 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
112 {
113     /* Run your code on data */
114     if (data == nullptr) {
115         return 0;
116     }
117 
118     /* Validate the length of size */
119     if (size < OHOS::U32_AT_SIZE) {
120         return 0;
121     }
122 
123     char* ch = static_cast<char*>(malloc(size + 1));
124     if (ch == nullptr) {
125         std::cout << "malloc failed." << std::endl;
126         return 0;
127     }
128 
129     (void)memset_s(ch, size + 1, 0x00, size + 1);
130     if (memcpy_s(ch, size, data, size) != EOK) {
131         std::cout << "copy failed." << std::endl;
132         free(ch);
133         ch = nullptr;
134         return 0;
135     }
136 
137     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
138     free(ch);
139     ch = nullptr;
140     return 0;
141 }
142 
143