1 /*
2 * Copyright (c) 2022 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 "pendingwantmanager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #define private public
22 #include "pending_want_key.h"
23 #include "pending_want_manager.h"
24 #include "resident_process_manager.h"
25 #include "sa_mgr_client.h"
26 #include "task_data_persistence_mgr.h"
27 #undef private
28
29 #include "ability_record.h"
30
31 using namespace OHOS::AAFwk;
32 using namespace OHOS::AppExecFwk;
33
34 namespace OHOS {
35 namespace {
36 constexpr size_t FOO_MAX_LEN = 1024;
37 constexpr size_t U32_AT_SIZE = 4;
38 constexpr uint8_t ENABLE = 2;
39 }
40
GetU32Data(const char * ptr)41 uint32_t GetU32Data(const char* ptr)
42 {
43 // convert fuzz input data to an integer
44 return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
45 }
46
GetFuzzAbilityToken()47 sptr<Token> GetFuzzAbilityToken()
48 {
49 AbilityRequest abilityRequest;
50 abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
51 abilityRequest.abilityInfo.name = "MainAbility";
52 abilityRequest.abilityInfo.type = AbilityType::DATA;
53 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
54 if (!abilityRecord) {
55 return nullptr;
56 }
57 return abilityRecord->GetToken();
58 }
59
DoSomethingInterestingWithMyAPI(const char * data,size_t size)60 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
61 {
62 bool boolParam = *data % ENABLE;
63 int intParam = static_cast<int>(GetU32Data(data));
64 int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
65 size_t sizeParam = static_cast<size_t>(GetU32Data(data));
66 std::string stringParam(data, size);
67 Parcel wantParcel;
68 Want *want = nullptr;
69 if (wantParcel.WriteBuffer(data, size)) {
70 want = Want::Unmarshalling(wantParcel);
71 if (!want) {
72 return false;
73 }
74 }
75 sptr<IRemoteObject> token = GetFuzzAbilityToken();
76
77 // fuzz for PendingWantKey
78 auto pendingWantKey = std::make_shared<PendingWantKey>();
79 pendingWantKey->SetType(int32Param);
80 pendingWantKey->SetBundleName(stringParam);
81 pendingWantKey->SetRequestWho(stringParam);
82 pendingWantKey->SetRequestCode(int32Param);
83 pendingWantKey->SetRequestWant(*want);
84 pendingWantKey->SetRequestResolvedType(stringParam);
85 std::vector<WantsInfo> allWantsInfos;
86 pendingWantKey->SetAllWantsInfos(allWantsInfos);
87 pendingWantKey->SetFlags(int32Param);
88 pendingWantKey->SetCode(int32Param);
89 pendingWantKey->SetUserId(int32Param);
90 pendingWantKey->GetType();
91 pendingWantKey->GetBundleName();
92 pendingWantKey->GetRequestWho();
93 pendingWantKey->GetRequestCode();
94 pendingWantKey->GetRequestWant();
95 pendingWantKey->GetRequestResolvedType();
96 pendingWantKey->GetAllWantsInfos();
97 pendingWantKey->GetFlags();
98 pendingWantKey->GetCode();
99 pendingWantKey->GetUserId();
100
101 // fuzz for PendingWantManager
102 auto pendingWantManager = std::make_shared<PendingWantManager>();
103 WantSenderInfo wantSenderInfo;
104 pendingWantManager->GetWantSender(int32Param, int32Param, stringParam, wantSenderInfo, token);
105 pendingWantManager->GetWantSenderLocked(int32Param, int32Param, int32Param, wantSenderInfo, token);
106 PendingWantRecord pendingWantRecord;
107 pendingWantManager->MakeWantSenderCanceledLocked(pendingWantRecord);
108 pendingWantManager->GetPendingWantRecordByKey(pendingWantKey);
109 pendingWantManager->CheckPendingWantRecordByKey(pendingWantKey, pendingWantKey);
110 sptr<IWantSender> wantSenderPtr;
111 SenderInfo senderInfo;
112 pendingWantManager->SendWantSender(wantSenderPtr, senderInfo);
113 pendingWantManager->CancelWantSender(stringParam, wantSenderPtr);
114 pendingWantManager->CancelWantSenderLocked(pendingWantRecord, boolParam);
115 pendingWantManager->PendingWantStartAbilitys(allWantsInfos, token, int32Param, int32Param);
116 pendingWantManager->PendingWantPublishCommonEvent(*want, senderInfo, int32Param, int32Param);
117 pendingWantManager->PendingRecordIdCreate();
118 pendingWantManager->GetPendingWantRecordByCode(int32Param);
119 pendingWantManager->GetPendingWantUid(wantSenderPtr);
120 pendingWantManager->GetPendingWantUserId(wantSenderPtr);
121 pendingWantManager->GetPendingWantBundleName(wantSenderPtr);
122 pendingWantManager->GetPendingWantCode(wantSenderPtr);
123 pendingWantManager->GetPendingWantType(wantSenderPtr);
124 sptr<IWantReceiver> wantReceiverPtr;
125 pendingWantManager->RegisterCancelListener(wantSenderPtr, wantReceiverPtr);
126 pendingWantManager->UnregisterCancelListener(wantSenderPtr, wantReceiverPtr);
127 std::shared_ptr<Want> wantPtr;
128 pendingWantManager->GetPendingRequestWant(wantSenderPtr, wantPtr);
129 std::shared_ptr<WantSenderInfo> wantSenderInfoPtr;
130 pendingWantManager->GetWantSenderInfo(wantSenderPtr, wantSenderInfoPtr);
131 pendingWantManager->ClearPendingWantRecord(stringParam, int32Param);
132 pendingWantManager->ClearPendingWantRecordTask(stringParam, int32Param);
133
134 // fuzz for ResidentProcessManager
135 auto residentProcessManager = std::make_shared<ResidentProcessManager>();
136 std::vector<AppExecFwk::BundleInfo> bundleInfos;
137 residentProcessManager->StartResidentProcess(bundleInfos);
138 residentProcessManager->StartResidentProcessWithMainElement(bundleInfos);
139 AppExecFwk::HapModuleInfo hapModuleInfo;
140 std::set<uint32_t> needEraseIndexSet;
141 residentProcessManager->CheckMainElement(hapModuleInfo, stringParam, stringParam, needEraseIndexSet, sizeParam);
142
143 // fuzz for SaMgrClient
144 auto saMgrClient = std::make_shared<SaMgrClient>();
145 saMgrClient->GetSystemAbility(int32Param);
146 saMgrClient->RegisterSystemAbility(int32Param, token);
147
148 // fuzz for TaskDataPersistenceMgr
149 auto taskDataPersistenceMgr = std::make_shared<TaskDataPersistenceMgr>();
150 std::list<InnerMissionInfo> missionInfoList;
151 taskDataPersistenceMgr->LoadAllMissionInfo(missionInfoList);
152 InnerMissionInfo innerMissionInfo;
153 taskDataPersistenceMgr->SaveMissionInfo(innerMissionInfo);
154 taskDataPersistenceMgr->DeleteMissionInfo(intParam);
155 taskDataPersistenceMgr->RemoveUserDir(int32Param);
156 MissionSnapshot missionSnapshot;
157 taskDataPersistenceMgr->SaveMissionSnapshot(intParam, missionSnapshot);
158 taskDataPersistenceMgr->GetSnapshot(intParam);
159 taskDataPersistenceMgr->GetMissionSnapshot(intParam, missionSnapshot, boolParam);
160 if (want) {
161 delete want;
162 want = nullptr;
163 }
164 if (wantSenderPtr) {
165 delete wantSenderPtr;
166 wantSenderPtr = nullptr;
167 }
168 if (wantReceiverPtr) {
169 delete wantReceiverPtr;
170 wantReceiverPtr = nullptr;
171 }
172
173 return true;
174 }
175 }
176
177 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)178 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
179 {
180 /* Run your code on data */
181 if (data == nullptr) {
182 return 0;
183 }
184
185 /* Validate the length of size */
186 if (size < OHOS::U32_AT_SIZE || size > OHOS::FOO_MAX_LEN) {
187 return 0;
188 }
189
190 char* ch = (char *)malloc(size + 1);
191 if (ch == nullptr) {
192 std::cout << "malloc failed." << std::endl;
193 return 0;
194 }
195
196 (void)memset_s(ch, size + 1, 0x00, size + 1);
197 if (memcpy_s(ch, size, data, size) != EOK) {
198 std::cout << "copy failed." << std::endl;
199 free(ch);
200 ch = nullptr;
201 return 0;
202 }
203
204 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
205 free(ch);
206 ch = nullptr;
207 return 0;
208 }
209
210