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 "abilityschedulerstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "ability_scheduler_stub.h"
22 #include "message_parcel.h"
23 #include "securec.h"
24
25 using namespace OHOS::AAFwk;
26
27 namespace OHOS {
28 namespace {
29 constexpr int INPUT_ZERO = 0;
30 constexpr int INPUT_ONE = 1;
31 constexpr int INPUT_TWO = 2;
32 constexpr int INPUT_THREE = 3;
33 constexpr size_t FOO_MAX_LEN = 1024;
34 constexpr size_t U32_AT_SIZE = 4;
35 constexpr size_t OFFSET_ZERO = 24;
36 constexpr size_t OFFSET_ONE = 16;
37 constexpr size_t OFFSET_TWO = 8;
38 const std::u16string ABILITYMGR_INTERFACE_TOKEN = u"ohos.aafwk.AbilityScheduler";
39 }
40 class AbilitySchedulerStubFuzzTest : public AbilitySchedulerStub {
41 public:
42 AbilitySchedulerStubFuzzTest() = default;
~AbilitySchedulerStubFuzzTest()43 virtual ~AbilitySchedulerStubFuzzTest()
44 {};
ScheduleAbilityTransaction(const Want & want,const LifeCycleStateInfo & targetState,sptr<SessionInfo> sessionInfo=nullptr)45 void ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState,
46 sptr<SessionInfo> sessionInfo = nullptr) override
47 {}
ScheduleShareData(const int32_t & uniqueId)48 void ScheduleShareData(const int32_t &uniqueId) override
49 {}
SendResult(int requestCode,int resultCode,const Want & resultWant)50 void SendResult(int requestCode, int resultCode, const Want& resultWant) override
51 {}
ScheduleConnectAbility(const Want & want)52 void ScheduleConnectAbility(const Want& want) override
53 {}
ScheduleDisconnectAbility(const Want & want)54 void ScheduleDisconnectAbility(const Want& want) override
55 {}
ScheduleCommandAbility(const Want & want,bool restart,int startId)56 void ScheduleCommandAbility(const Want& want, bool restart, int startId) override
57 {}
ScheduleCommandAbilityWindow(const Want & want,const sptr<SessionInfo> & sessionInfo,WindowCommand winCmd)58 void ScheduleCommandAbilityWindow(const Want &want, const sptr<SessionInfo> &sessionInfo,
59 WindowCommand winCmd) override
60 {}
SchedulePrepareTerminateAbility()61 bool SchedulePrepareTerminateAbility() override
62 {
63 return true;
64 }
ScheduleSaveAbilityState()65 void ScheduleSaveAbilityState() override
66 {}
ScheduleRestoreAbilityState(const PacMap & inState)67 void ScheduleRestoreAbilityState(const PacMap& inState) override
68 {}
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)69 std::vector<std::string> GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter) override
70 {
71 return {};
72 }
OpenFile(const Uri & uri,const std::string & mode)73 int OpenFile(const Uri& uri, const std::string& mode) override
74 {
75 return 0;
76 }
OpenRawFile(const Uri & uri,const std::string & mode)77 int OpenRawFile(const Uri& uri, const std::string& mode) override
78 {
79 return 0;
80 }
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)81 int Insert(const Uri& uri, const NativeRdb::ValuesBucket& value) override
82 {
83 return 0;
84 }
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)85 int Update(const Uri& uri, const NativeRdb::ValuesBucket& value,
86 const NativeRdb::DataAbilityPredicates& predicates) override
87 {
88 return 0;
89 }
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)90 int Delete(const Uri& uri, const NativeRdb::DataAbilityPredicates& predicates) override
91 {
92 return 0;
93 }
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)94 std::shared_ptr<AppExecFwk::PacMap> Call(
95 const Uri& uri, const std::string& method, const std::string& arg, const AppExecFwk::PacMap& pacMap) override
96 {
97 return {};
98 }
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)99 std::shared_ptr<NativeRdb::AbsSharedResultSet> Query(const Uri& uri,
100 std::vector<std::string>& columns, const NativeRdb::DataAbilityPredicates& predicates) override
101 {
102 return {};
103 }
GetType(const Uri & uri)104 std::string GetType(const Uri& uri) override
105 {
106 return {};
107 }
Reload(const Uri & uri,const PacMap & extras)108 bool Reload(const Uri& uri, const PacMap& extras) override
109 {
110 return true;
111 }
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)112 int BatchInsert(const Uri& uri, const std::vector<NativeRdb::ValuesBucket>& values) override
113 {
114 return 0;
115 }
ScheduleRegisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)116 bool ScheduleRegisterObserver(const Uri& uri, const sptr<IDataAbilityObserver>& dataObserver) override
117 {
118 return true;
119 }
ScheduleUnregisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)120 bool ScheduleUnregisterObserver(const Uri& uri, const sptr<IDataAbilityObserver>& dataObserver) override
121 {
122 return true;
123 }
ScheduleNotifyChange(const Uri & uri)124 bool ScheduleNotifyChange(const Uri& uri) override
125 {
126 return true;
127 }
NormalizeUri(const Uri & uri)128 Uri NormalizeUri(const Uri& uri) override
129 {
130 return Uri{ "abilityschedulerstub" };
131 }
132
DenormalizeUri(const Uri & uri)133 Uri DenormalizeUri(const Uri& uri) override
134 {
135 return Uri{ "abilityschedulerstub" };
136 }
ExecuteBatch(const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> & operations)137 std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> ExecuteBatch(
138 const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>>& operations) override
139 {
140 return {};
141 }
ContinueAbility(const std::string & deviceId,uint32_t versionCode)142 void ContinueAbility(const std::string& deviceId, uint32_t versionCode) override
143 {}
NotifyContinuationResult(int32_t result)144 void NotifyContinuationResult(int32_t result) override
145 {}
DumpAbilityInfo(const std::vector<std::string> & params,std::vector<std::string> & info)146 void DumpAbilityInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override
147 {}
BlockAbility()148 int BlockAbility() override
149 {
150 return 0;
151 }
CallRequest()152 void CallRequest() override
153 {
154 return;
155 }
OnExecuteIntent(const Want & want)156 void OnExecuteIntent(const Want &want) override
157 {}
158 };
159
GetU32Data(const char * ptr)160 uint32_t GetU32Data(const char* ptr)
161 {
162 // convert fuzz input data to an integer
163 return (ptr[INPUT_ZERO] << OFFSET_ZERO) | (ptr[INPUT_ONE] << OFFSET_ONE) | (ptr[INPUT_TWO] << OFFSET_TWO) |
164 ptr[INPUT_THREE];
165 }
166
DoSomethingInterestingWithMyAPI(const char * data,size_t size)167 bool DoSomethingInterestingWithMyAPI(const char* data, size_t size)
168 {
169 uint32_t code = GetU32Data(data);
170
171 MessageParcel parcel;
172 parcel.WriteInterfaceToken(ABILITYMGR_INTERFACE_TOKEN);
173 parcel.WriteBuffer(data, size);
174 parcel.RewindRead(0);
175 MessageParcel reply;
176 MessageOption option;
177
178 std::shared_ptr<AbilitySchedulerStub> abilityschedulerstub = std::make_shared<AbilitySchedulerStubFuzzTest>();
179
180 if (abilityschedulerstub->OnRemoteRequest(code, parcel, reply, option) != 0) {
181 return false;
182 }
183
184 return true;
185 }
186 }
187
188 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)189 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
190 {
191 /* Run your code on data */
192 if (data == nullptr) {
193 std::cout << "invalid data" << std::endl;
194 return 0;
195 }
196
197 /* Validate the length of size */
198 if (size > OHOS::FOO_MAX_LEN || size < OHOS::U32_AT_SIZE) {
199 return 0;
200 }
201
202 char* ch = (char*)malloc(size + 1);
203 if (ch == nullptr) {
204 std::cout << "malloc failed." << std::endl;
205 return 0;
206 }
207
208 (void)memset_s(ch, size + 1, 0x00, size + 1);
209 if (memcpy_s(ch, size, data, size) != EOK) {
210 std::cout << "copy failed." << std::endl;
211 free(ch);
212 ch = nullptr;
213 return 0;
214 }
215
216 OHOS::DoSomethingInterestingWithMyAPI(ch, size);
217 free(ch);
218 ch = nullptr;
219 return 0;
220 }
221
222