1 /*
2  * Copyright (c) 2022-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 "sensor_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include "hdf_log.h"
20 #include "v2_0/sensor_interface_stub.h"
21 
22 using namespace OHOS::HDI::Sensor::V2_0;
23 
24 namespace OHOS {
25 constexpr size_t THRESHOLD = 10;
26 constexpr int32_t OFFSET = 4;
27 constexpr int32_t ARRAY0 = 0;
28 constexpr int32_t ARRAY1 = 1;
29 constexpr int32_t ARRAY2 = 2;
30 constexpr int32_t ARRAY3 = 3;
31 constexpr int32_t DIGIT8 = 8;
32 constexpr int32_t DIGIT16 = 16;
33 constexpr int32_t DIGIT24 = 24;
34 const std::u16string SENSOR_INTERFACE_TOKEN = u"ohos.hdi.sensor.v2_0.ISensorInterface";
35 
Convert2Uint32(const uint8_t * ptr)36 uint32_t Convert2Uint32(const uint8_t* ptr)
37 {
38     if (ptr == nullptr) {
39         return 0;
40     }
41     /*
42      * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
43      * and the third digit no left
44      */
45     return (ptr[ARRAY0] << DIGIT24) | (ptr[ARRAY1] << DIGIT16) | (ptr[ARRAY2] << DIGIT8) | (ptr[ARRAY3]);
46 }
47 
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)48 bool DoSomethingInterestingWithMyAPI(const uint8_t* rawData, size_t size)
49 {
50     if (rawData == nullptr) {
51         return false;
52     }
53     uint32_t code = Convert2Uint32(rawData);
54     rawData = rawData + OFFSET;
55     size = size - OFFSET;
56 
57     MessageParcel data;
58     data.WriteInterfaceToken(SENSOR_INTERFACE_TOKEN);
59     data.WriteBuffer(rawData, size);
60     data.RewindRead(0);
61     MessageParcel reply;
62     MessageOption option;
63     sptr<ISensorInterface> g_sensorInterface = ISensorInterface::Get(false);
64     if (g_sensorInterface == nullptr) {
65         HDF_LOGE("%{public}s:ISensorInterface::Get() failed.", __func__);
66         return false;
67     }
68     sptr<SensorInterfaceStub> sensorInterface = new SensorInterfaceStub(g_sensorInterface);
69     if (sensorInterface == nullptr) {
70         HDF_LOGE("%{public}s:new SensorInterfaceStub failed.", __func__);
71         return false;
72     }
73     sensorInterface->OnRemoteRequest(code, data, reply, option);
74 
75     return true;
76 }
77 }
78 
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82     if (size < OHOS::THRESHOLD) {
83         return 0;
84     }
85 
86     /* Run your code on data */
87     OHOS::DoSomethingInterestingWithMyAPI(data, size);
88     return 0;
89 }
90 
91