1 /*
2 * Copyright (c) 2024 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 "sensorgetsdcsensorInfo_fuzzer.h"
17 #include "hdf_base.h"
18 #include "v2_0/sensor_interface_proxy.h"
19 #include <hdf_log.h>
20 #include <securec.h>
21
22 using namespace OHOS::HDI::Sensor::V2_0;
23
24 namespace {
25 struct AllParameters {
26 SdcSensorInfo sdcSensorInfo;
27 };
28 }
29
30 namespace OHOS {
SensorGetSdcSensorInfoFuzzTest(const uint8_t * data,size_t size)31 bool SensorGetSdcSensorInfoFuzzTest(const uint8_t* data, size_t size)
32 {
33 bool result = false;
34 struct AllParameters params;
35 sptr<ISensorInterface> g_sensorInterface = ISensorInterface::Get();
36
37 if (size < sizeof(params)) {
38 return 0;
39 }
40
41 if (memcpy_s(reinterpret_cast<void *>(¶ms), sizeof(params), data, sizeof(params)) != 0) {
42 HDF_LOGE("%{public}s: memcpy_s failed", __func__);
43 return false;
44 }
45 std::vector<SdcSensorInfo> sdcSensorInfos;
46 sdcSensorInfos.push_back(std::move(params.sdcSensorInfo));
47
48 if (!g_sensorInterface->GetSdcSensorInfo(sdcSensorInfos)) {
49 result = true;
50 }
51 return result;
52 }
53 }
54
55 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)56 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
57 {
58 if (data == nullptr) {
59 return 0;
60 }
61
62 if (size < sizeof(int32_t)) {
63 return 0;
64 }
65 /* Run your code on data */
66 OHOS::SensorGetSdcSensorInfoFuzzTest(data, size);
67 return 0;
68 }
69
70