• 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 "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 U32_AT_SIZE = 4;
37 constexpr uint8_t ENABLE = 2;
38 }
39 
GetU32Data(const char * ptr)40 uint32_t GetU32Data(const char* ptr)
41 {
42     // convert fuzz input data to an integer
43     return (ptr[0] << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3];
44 }
45 
GetFuzzAbilityToken()46 sptr<Token> GetFuzzAbilityToken()
47 {
48     AbilityRequest abilityRequest;
49     abilityRequest.appInfo.bundleName = "com.example.fuzzTest";
50     abilityRequest.abilityInfo.name = "MainAbility";
51     abilityRequest.abilityInfo.type = AbilityType::DATA;
52     std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
53     if (!abilityRecord) {
54         return nullptr;
55     }
56     return abilityRecord->GetToken();
57 }
58 
DoSomethingInterestingWithMyAPI(const char * data,size_t size)59 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
60 {
61     bool boolParam = *data % ENABLE;
62     int intParam = static_cast<int>(GetU32Data(data));
63     int32_t int32Param = static_cast<int32_t>(GetU32Data(data));
64     size_t sizeParam = static_cast<size_t>(GetU32Data(data));
65     std::string stringParam(data, size);
66     Parcel wantParcel;
67     Want* want = nullptr;
68     if (wantParcel.WriteBuffer(data, size)) {
69         want = Want::Unmarshalling(wantParcel);
70         if (!want) {
71             return false;
72         }
73     }
74     sptr<IRemoteObject> token = GetFuzzAbilityToken();
75 
76     // fuzz for PendingWantKey
77     auto pendingWantKey = std::make_shared<PendingWantKey>();
78     pendingWantKey->SetType(int32Param);
79     pendingWantKey->SetBundleName(stringParam);
80     pendingWantKey->SetRequestWho(stringParam);
81     pendingWantKey->SetRequestCode(int32Param);
82     pendingWantKey->SetRequestWant(*want);
83     pendingWantKey->SetRequestResolvedType(stringParam);
84     std::vector<WantsInfo> allWantsInfos;
85     pendingWantKey->SetAllWantsInfos(allWantsInfos);
86     pendingWantKey->SetFlags(int32Param);
87     pendingWantKey->SetCode(int32Param);
88     pendingWantKey->SetUserId(int32Param);
89     pendingWantKey->GetType();
90     pendingWantKey->GetBundleName();
91     pendingWantKey->GetRequestWho();
92     pendingWantKey->GetRequestCode();
93     pendingWantKey->GetRequestWant();
94     pendingWantKey->GetRequestResolvedType();
95     pendingWantKey->GetAllWantsInfos();
96     pendingWantKey->GetFlags();
97     pendingWantKey->GetCode();
98     pendingWantKey->GetUserId();
99 
100     // fuzz for PendingWantManager
101     auto pendingWantManager = std::make_shared<PendingWantManager>();
102     WantSenderInfo wantSenderInfo;
103     pendingWantManager->GetWantSender(int32Param, int32Param, boolParam, wantSenderInfo, token);
104     pendingWantManager->GetWantSenderLocked(int32Param, int32Param, int32Param, wantSenderInfo, token);
105     PendingWantRecord pendingWantRecord;
106     pendingWantManager->MakeWantSenderCanceledLocked(pendingWantRecord);
107     pendingWantManager->GetPendingWantRecordByKey(pendingWantKey);
108     pendingWantManager->CheckPendingWantRecordByKey(pendingWantKey, pendingWantKey);
109     sptr<IWantSender> wantSenderPtr;
110     SenderInfo senderInfo;
111     pendingWantManager->SendWantSender(wantSenderPtr, senderInfo);
112     pendingWantManager->CancelWantSender(boolParam, wantSenderPtr);
113     pendingWantManager->CancelWantSenderLocked(pendingWantRecord, boolParam);
114     sptr<StartOptions> startoptions;
115     pendingWantManager->PendingWantStartAbilitys(allWantsInfos, startoptions,
116                                                  token, int32Param, int32Param, int32Param);
117     pendingWantManager->PendingWantPublishCommonEvent(*want, senderInfo, int32Param, int32Param);
118     pendingWantManager->PendingRecordIdCreate();
119     pendingWantManager->GetPendingWantRecordByCode(int32Param);
120     pendingWantManager->GetPendingWantUid(wantSenderPtr);
121     pendingWantManager->GetPendingWantUserId(wantSenderPtr);
122     pendingWantManager->GetPendingWantBundleName(wantSenderPtr);
123     pendingWantManager->GetPendingWantCode(wantSenderPtr);
124     pendingWantManager->GetPendingWantType(wantSenderPtr);
125     sptr<IWantReceiver> wantReceiverPtr;
126     pendingWantManager->RegisterCancelListener(wantSenderPtr, wantReceiverPtr);
127     pendingWantManager->UnregisterCancelListener(wantSenderPtr, wantReceiverPtr);
128     std::shared_ptr<Want> wantPtr;
129     pendingWantManager->GetPendingRequestWant(wantSenderPtr, wantPtr);
130     std::shared_ptr<WantSenderInfo> wantSenderInfoPtr;
131     pendingWantManager->GetWantSenderInfo(wantSenderPtr, wantSenderInfoPtr);
132     pendingWantManager->ClearPendingWantRecord(stringParam, int32Param);
133     pendingWantManager->ClearPendingWantRecordTask(stringParam, int32Param);
134 
135     // fuzz for ResidentProcessManager
136     auto residentProcessManager = std::make_shared<ResidentProcessManager>();
137     std::vector<AppExecFwk::BundleInfo> bundleInfos;
138     residentProcessManager->StartResidentProcess(bundleInfos);
139     residentProcessManager->StartResidentProcessWithMainElement(bundleInfos, 0);
140     AppExecFwk::HapModuleInfo hapModuleInfo;
141     std::set<uint32_t> needEraseIndexSet;
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 
165     return true;
166 }
167 }
168 
169 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)170 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
171 {
172     /* Run your code on data */
173     if (data == nullptr) {
174         return 0;
175     }
176 
177     /* Validate the length of size */
178     if (size < OHOS::U32_AT_SIZE) {
179         return 0;
180     }
181 
182     char* ch = static_cast<char*>(malloc(size + 1));
183     if (ch == nullptr) {
184         std::cout << "malloc failed." << std::endl;
185         return 0;
186     }
187 
188     (void)memset_s(ch, size + 1, 0x00, size + 1);
189     if (memcpy_s(ch, size, data, size) != EOK) {
190         std::cout << "copy failed." << std::endl;
191         free(ch);
192         ch = nullptr;
193         return 0;
194     }
195 
196     OHOS::DoSomethingInterestingWithMyAPI(ch, size);
197     free(ch);
198     ch = nullptr;
199     return 0;
200 }