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 "thermal_interface_impl.h"
22 #include "v1_1/ithermal_callback.h"
23 #include "v1_1/ithermal_interface.h"
24 #include "v1_1/thermal_interface_stub.h"
25 #include "v1_1/thermal_types.h"
26 
27 using namespace OHOS::HDI;
28 using namespace OHOS::HDI::Thermal::V1_1;
29 using namespace std;
30 
31 namespace OHOS {
32 namespace HDI {
33 namespace Thermal {
34 namespace V1_1 {
35 namespace {
36 const int32_t REWIND_READ_DATA = 0;
37 shared_ptr<ThermalInterfaceStub> g_fuzzService = nullptr;
38 const uint32_t THERMAL_INTERFACE_STUB_FUNC_MAX_SIZE = 10;
39 } // namespace
40 
ThermalStubFuzzTest(const uint8_t * data,size_t size)41 static void ThermalStubFuzzTest(const uint8_t *data, size_t size)
42 {
43     uint32_t code;
44     if (size < sizeof(code)) {
45         return;
46     }
47     if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
48         return;
49     }
50 
51     MessageParcel datas;
52     MessageParcel reply;
53     MessageOption option;
54     if (g_fuzzService == nullptr) {
55         g_fuzzService = make_shared<ThermalInterfaceStub>(new ThermalInterfaceImpl());
56     }
57     for (code = CMD_THERMAL_INTERFACE_GET_VERSION; code < THERMAL_INTERFACE_STUB_FUNC_MAX_SIZE; code++) {
58         datas.WriteInterfaceToken(IThermalInterface::GetDescriptor());
59         datas.WriteBuffer(data, size);
60         datas.RewindRead(REWIND_READ_DATA);
61         g_fuzzService->OnRemoteRequest(code, datas, reply, option);
62     }
63 }
64 } // namespace V1_1
65 } // namespace Thermal
66 } // namespace HDI
67 } // namespace OHOS
68 
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
71 {
72     /* Run your code on data */
73     OHOS::HDI::Thermal::V1_1::ThermalStubFuzzTest(data, size);
74     return 0;
75 }
76