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
25using namespace OHOS::AAFwk;
26
27namespace OHOS {
28namespace {
29constexpr int INPUT_ZERO = 0;
30constexpr int INPUT_ONE = 1;
31constexpr int INPUT_TWO = 2;
32constexpr int INPUT_THREE = 3;
33constexpr size_t FOO_MAX_LEN = 1024;
34constexpr size_t U32_AT_SIZE = 4;
35constexpr size_t OFFSET_ZERO = 24;
36constexpr size_t OFFSET_ONE = 16;
37constexpr size_t OFFSET_TWO = 8;
38const std::u16string ABILITYMGR_INTERFACE_TOKEN = u"ohos.aafwk.AbilityScheduler";
39}
40class AbilitySchedulerStubFuzzTest : public AbilitySchedulerStub {
41public:
42    AbilitySchedulerStubFuzzTest() = default;
43    virtual ~AbilitySchedulerStubFuzzTest()
44    {};
45    bool ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState,
46        sptr<SessionInfo> sessionInfo = nullptr) override
47    {
48        return true;
49    }
50    void ScheduleShareData(const int32_t &uniqueId) override
51    {}
52    void SendResult(int requestCode, int resultCode, const Want& resultWant) override
53    {}
54    void ScheduleConnectAbility(const Want& want) override
55    {}
56    void ScheduleDisconnectAbility(const Want& want) override
57    {}
58    void ScheduleCommandAbility(const Want& want, bool restart, int startId) override
59    {}
60    void ScheduleCommandAbilityWindow(const Want &want, const sptr<SessionInfo> &sessionInfo,
61        WindowCommand winCmd) override
62    {}
63    bool SchedulePrepareTerminateAbility() override
64    {
65        return true;
66    }
67    void ScheduleSaveAbilityState() override
68    {}
69    void ScheduleRestoreAbilityState(const PacMap& inState) override
70    {}
71    std::vector<std::string> GetFileTypes(const Uri& uri, const std::string& mimeTypeFilter) override
72    {
73        return {};
74    }
75    int OpenFile(const Uri& uri, const std::string& mode) override
76    {
77        return 0;
78    }
79    int OpenRawFile(const Uri& uri, const std::string& mode) override
80    {
81        return 0;
82    }
83    int Insert(const Uri& uri, const NativeRdb::ValuesBucket& value) override
84    {
85        return 0;
86    }
87    int Update(const Uri& uri, const NativeRdb::ValuesBucket& value,
88        const NativeRdb::DataAbilityPredicates& predicates) override
89    {
90        return 0;
91    }
92    int Delete(const Uri& uri, const NativeRdb::DataAbilityPredicates& predicates) override
93    {
94        return 0;
95    }
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    }
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    }
106    std::string GetType(const Uri& uri) override
107    {
108        return {};
109    }
110    bool Reload(const Uri& uri, const PacMap& extras) override
111    {
112        return true;
113    }
114    int BatchInsert(const Uri& uri, const std::vector<NativeRdb::ValuesBucket>& values) override
115    {
116        return 0;
117    }
118    bool ScheduleRegisterObserver(const Uri& uri, const sptr<IDataAbilityObserver>& dataObserver) override
119    {
120        return true;
121    }
122    bool ScheduleUnregisterObserver(const Uri& uri, const sptr<IDataAbilityObserver>& dataObserver) override
123    {
124        return true;
125    }
126    bool ScheduleNotifyChange(const Uri& uri) override
127    {
128        return true;
129    }
130    Uri NormalizeUri(const Uri& uri) override
131    {
132        return Uri{ "abilityschedulerstub" };
133    }
134
135    Uri DenormalizeUri(const Uri& uri) override
136    {
137        return Uri{ "abilityschedulerstub" };
138    }
139    std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> ExecuteBatch(
140        const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>>& operations) override
141    {
142        return {};
143    }
144    void ContinueAbility(const std::string& deviceId, uint32_t versionCode) override
145    {}
146    void NotifyContinuationResult(int32_t result) override
147    {}
148    void DumpAbilityInfo(const std::vector<std::string>& params, std::vector<std::string>& info) override
149    {}
150    void CallRequest() override
151    {
152        return;
153    }
154    void OnExecuteIntent(const Want &want) override
155    {}
156};
157
158uint32_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
165bool 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 */
187extern "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 = (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