• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <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::Power;
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 V1_3::PowerInterfaceImpl();
39         impl_->SuspendBlock("PowerStubFuzzTest"); // Prevent device sleep
40     }
~PowerFuzzTest()41     ~PowerFuzzTest()
42     {
43         impl_->SuspendUnblock("PowerStubFuzzTest");
44     }
GetImpl() const45     sptr<V1_3::PowerInterfaceImpl> GetImpl() const
46     {
47         return impl_;
48     }
49 
50 private:
51     sptr<V1_3::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 = CMD_POWER_INTERFACE_GET_POWER_CONFIG + 1;
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 = 0;
63     if (size < sizeof(code)) {
64         return;
65     }
66     if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
67         return;
68     }
69     OHOS::HDI::Power::V1_2::IPowerInterface::Get(true);
70 
71     MessageParcel datas;
72     MessageParcel reply;
73     MessageOption option;
74     if (g_fuzzService == nullptr) {
75         g_fuzzTest = make_shared<PowerFuzzTest>();
76         g_fuzzService = make_shared<PowerInterfaceStub>(g_fuzzTest->GetImpl());
77     }
78     for (code = CMD_POWER_INTERFACE_GET_VERSION; code < POWER_INTERFACE_STUB_FUNC_MAX_SIZE; code++) {
79         // Filter force sleep calls
80         if (CMD_POWER_INTERFACE_FORCE_SUSPEND == code) {
81             continue;
82         }
83         datas.WriteInterfaceToken(IPowerInterface::GetDescriptor());
84         datas.WriteBuffer(data, size);
85         datas.RewindRead(REWIND_READ_DATA);
86         g_fuzzService->OnRemoteRequest(code, datas, reply, option);
87     }
88 }
89 } // namespace V1_2
90 } // namespace Power
91 } // namespace HDI
92 } // namespace OHOS
93 
94 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)95 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
96 {
97     /* Run your code on data */
98     OHOS::HDI::Power::V1_2::PowerStubFuzzTest(data, size);
99     return 0;
100 }
101