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