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 FOO_MAX_LEN = 1024;
33 constexpr size_t U32_AT_SIZE = 4;
34 constexpr uint8_t ENABLE = 2;
35 }
36 
GetU32Data(const char * ptr)37 uint32_t GetU32Data(const char* ptr)
38 {
39     // convert fuzz input data to an integer
40     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
41 }
42 
GetFuzzAbilityRecord()43 std::shared_ptr<AbilityRecord> GetFuzzAbilityRecord()
44 {
45     sptr<Token> token = nullptr;
46     AbilityRequest abilityRequest;
47     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
48     abilityRequest.abilityInfo.name = "MainAbility";
49     abilityRequest.abilityInfo.type = AbilityType::DATA;
50     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
51     if (!abilityRecord) {
52         return nullptr;
53     }
54     return abilityRecord;
55 }
56 
GetFuzzAbilityToken()57 sptr<Token> GetFuzzAbilityToken()
58 {
59     sptr<Token> token = nullptr;
60     std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
61     if (abilityRecord) {
62         token = abilityRecord->GetToken();
63     }
64     return token;
65 }
66 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)67 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
68 {
69     bool boolParam = *data % ENABLE;
70     int intParam = static_cast<int>(GetU32Data(data));
71     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
72     int64_t int64Param = static_cast<int64_t>(GetU32Data(data));
73     std::string stringParam(data, size);
74     std::shared_ptr<AbilityRecord> abilityRecord = GetFuzzAbilityRecord();
75     sptr<IRemoteObject> token = GetFuzzAbilityToken();
76     std::vector<std::string> info;
77     AbilityRequest abilityRequest;
78 
79     // fuzz for DataAbilityManager
80     auto dataAbilityManager = std::make_shared<DataAbilityManager>();
81     sptr<IRemoteObject> client;
82     dataAbilityManager->Acquire(abilityRequest, boolParam, client, boolParam);
83     sptr<IAbilityScheduler> scheduler;
84     dataAbilityManager->Release(scheduler, client, boolParam);
85     dataAbilityManager->ContainsDataAbility(scheduler);
86     dataAbilityManager->AttachAbilityThread(scheduler, token);
87     dataAbilityManager->AbilityTransitionDone(token, intParam);
88     dataAbilityManager->OnAbilityRequestDone(token, int32Param);
89     dataAbilityManager->OnAbilityDied(abilityRecord);
90     AppInfo appInfo;
91     dataAbilityManager->OnAppStateChanged(appInfo);
92     dataAbilityManager->GetAbilityRecordById(int64Param);
93     dataAbilityManager->GetAbilityRecordByToken(token);
94     dataAbilityManager->GetAbilityRecordByScheduler(scheduler);
95     dataAbilityManager->Dump(data, intParam);
96     dataAbilityManager->LoadLocked(stringParam, abilityRequest);
97     dataAbilityManager->DumpLocked(data, intParam);
98     dataAbilityManager->DumpState(info, stringParam);
99     dataAbilityManager->DumpSysState(info, boolParam, stringParam);
100     std::vector<AbilityRunningInfo> AbilityRunningInfoVector;
101     dataAbilityManager->GetAbilityRunningInfos(AbilityRunningInfoVector, boolParam);
102     dataAbilityManager->RestartDataAbility(abilityRecord);
103     std::shared_ptr<DataAbilityRecord> record;
104     dataAbilityManager->ReportDataAbilityAcquired(client, boolParam, record);
105     dataAbilityManager->ReportDataAbilityReleased(client, boolParam, record);
106 
107     return true;
108 }
109 }
110 
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114     /* Run your code on data */
115     if (data == nullptr) {
116         return 0;
117     }
118 
119     /* Validate the length of size */
120     if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
121         return 0;
122     }
123 
124     char* ch = static_cast<char*>(malloc(size + 1));
125     if (ch == nullptr) {
126         std::cout << "malloc failed." << std::endl;
127         return 0;
128     }
129 
130     (void)memset_s(ch, size + 1, 0x00, size + 1);
131     if (memcpy_s(ch, size, data, size) != EOK) {
132         std::cout << "copy failed." << std::endl;
133         free(ch);
134         ch = nullptr;
135         return 0;
136     }
137 
138     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
139     free(ch);
140     ch = nullptr;
141     return 0;
142 }
143 
144