1 /*
2 * Copyright (c) 2024-2025 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 "abilityinterfacesappmgrchildschedulerstub_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21
22 #define private public
23 #include "child_scheduler_stub.h"
24 #include "child_scheduler_interface.h"
25 #undef private
26
27 #include "securec.h"
28 #include "parcel.h"
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 STRING_MAX_LENGTH = 128;
37 }
38 const std::u16string CHILD_SCHEDULER_TOKEN = u"ohos.appexecfwk.ChildScheduler";
39
40 class ChildSchedulerStubFUZZ : public ChildSchedulerStub {
41 public:
ChildSchedulerStubFUZZ()42 explicit ChildSchedulerStubFUZZ() {};
~ChildSchedulerStubFUZZ()43 virtual ~ ChildSchedulerStubFUZZ() {};
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int OnRemoteRequest(
45 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override{ return 0; };
ScheduleLoadChild()46 bool ScheduleLoadChild() override{ return true; };
ScheduleExitProcessSafely()47 bool ScheduleExitProcessSafely() override{ return true; };
ScheduleRunNativeProc(const sptr<IRemoteObject> & mainProcessCb)48 bool ScheduleRunNativeProc(const sptr<IRemoteObject> &mainProcessCb) override{ return true; };
49 };
50
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)51 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
52 {
53 std::shared_ptr<ChildSchedulerStub> stub = std::make_shared<ChildSchedulerStubFUZZ>();
54 uint32_t code;
55 MessageParcel parcel;
56 MessageParcel reply;
57 MessageOption option;
58 FuzzedDataProvider fdp(data, size);
59 parcel.WriteInterfaceToken(CHILD_SCHEDULER_TOKEN);
60 parcel.WriteString(fdp.ConsumeRandomLengthString(STRING_MAX_LENGTH));
61 parcel.WriteInt32(fdp.ConsumeIntegral<int32_t>());
62 parcel.RewindRead(0);
63 code = static_cast<uint32_t>(IChildScheduler::Message::SCHEDULE_LOAD_JS);
64 stub->OnRemoteRequest(code, parcel, reply, option);
65 code = static_cast<uint32_t>(IChildScheduler::Message::SCHEDULE_EXIT_PROCESS_SAFELY);
66 stub->OnRemoteRequest(code, parcel, reply, option);
67 code = static_cast<uint32_t>(IChildScheduler::Message::SCHEDULE_RUN_NATIVE_PROC);
68 stub->OnRemoteRequest(code, parcel, reply, option);
69 stub->HandleScheduleLoadChild(parcel, reply);
70 stub->HandleScheduleExitProcessSafely(parcel, reply);
71 stub->HandleScheduleRunNativeProc(parcel, reply);
72 return true;
73 }
74 }
75
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79 // Run your code on data.
80 OHOS::DoSomethingInterestingWithMyAPI(data, size);
81 return 0;
82 }