1 /*
2 * Copyright (c) 2023 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 "securec.h"
17 #include <cstdint>
18 #include <cstdlib>
19 #include <memory>
20
21 #include "power_interface_impl.h"
22 #include "v1_2/ipower_interface.h"
23 #include "v1_2/power_interface_stub.h"
24 #include "v1_2/power_types.h"
25
26 using namespace OHOS::HDI;
27 using namespace OHOS::HDI::Power::V1_2;
28 using namespace std;
29
30 namespace OHOS {
31 namespace HDI {
32 namespace Power {
33 namespace V1_2 {
34 class PowerFuzzTest {
35 public:
PowerFuzzTest()36 PowerFuzzTest()
37 {
38 impl_ = new PowerInterfaceImpl();
39 impl_->SuspendBlock("PowerStubFuzzTest"); // Prevent device sleep
40 }
~PowerFuzzTest()41 ~PowerFuzzTest()
42 {
43 impl_->SuspendUnblock("PowerStubFuzzTest");
44 }
GetImpl() const45 sptr<PowerInterfaceImpl> GetImpl() const
46 {
47 return impl_;
48 }
49
50 private:
51 sptr<PowerInterfaceImpl> impl_ = nullptr;
52 };
53 namespace {
54 const int32_t REWIND_READ_DATA = 0;
55 shared_ptr<PowerInterfaceStub> g_fuzzService = nullptr;
56 shared_ptr<PowerFuzzTest> g_fuzzTest = nullptr;
57 const uint32_t POWER_INTERFACE_STUB_FUNC_MAX_SIZE = 15;
58 } // namespace
59
PowerStubFuzzTest(const uint8_t * data,size_t size)60 static void PowerStubFuzzTest(const uint8_t *data, size_t size)
61 {
62 uint32_t code;
63 if (size < sizeof(code)) {
64 return;
65 }
66 if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
67 return;
68 }
69
70 MessageParcel datas;
71 MessageParcel reply;
72 MessageOption option;
73 if (g_fuzzService == nullptr) {
74 g_fuzzTest = make_shared<PowerFuzzTest>();
75 g_fuzzService = make_shared<PowerInterfaceStub>(g_fuzzTest->GetImpl());
76 }
77 for (code = CMD_POWER_INTERFACE_GET_VERSION; code < POWER_INTERFACE_STUB_FUNC_MAX_SIZE; code++) {
78 // Filter force sleep calls
79 if (CMD_POWER_INTERFACE_FORCE_SUSPEND == code) {
80 continue;
81 }
82 datas.WriteInterfaceToken(IPowerInterface::GetDescriptor());
83 datas.WriteBuffer(data, size);
84 datas.RewindRead(REWIND_READ_DATA);
85 g_fuzzService->OnRemoteRequest(code, datas, reply, option);
86 }
87 }
88 } // namespace V1_2
89 } // namespace Power
90 } // namespace HDI
91 } // namespace OHOS
92
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
95 {
96 /* Run your code on data */
97 OHOS::HDI::Power::V1_2::PowerStubFuzzTest(data, size);
98 return 0;
99 }
100