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