1 /******************************************************************************
2 *
3 * Copyright (C) 2022 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 */
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 #include <sensor/Sensor.h>
22 using namespace android;
23
24 const int MAX_STR_LEN = 32;
25
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)26 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
27 FuzzedDataProvider fdp(data, size);
28 struct sensor_t sensor_type;
29 std::string name = fdp.ConsumeBytesAsString(MAX_STR_LEN);
30 sensor_type.name = name.c_str();
31 std::string vendor = fdp.ConsumeBytesAsString(MAX_STR_LEN);
32 sensor_type.vendor = vendor.c_str();
33 sensor_type.stringType = "";
34 sensor_type.requiredPermission = "";
35 sensor_type.version = fdp.ConsumeIntegral<int>();
36 sensor_type.handle = fdp.ConsumeIntegral<int>();
37 sensor_type.type = fdp.ConsumeIntegral<int>();
38 sensor_type.maxRange = fdp.ConsumeFloatingPoint<float>();
39 sensor_type.resolution = fdp.ConsumeFloatingPoint<float>();
40 sensor_type.power = fdp.ConsumeFloatingPoint<float>();
41 sensor_type.minDelay = fdp.ConsumeIntegral<int32_t>();
42 sensor_type.fifoReservedEventCount = fdp.ConsumeIntegral<uint32_t>();
43 sensor_type.fifoMaxEventCount = fdp.ConsumeIntegral<uint32_t>();
44 int halVersion = fdp.ConsumeIntegral<int>();
45 Sensor sensor1(&sensor_type, halVersion);
46 uint8_t buffer[size];
47 for (int i = 0; i < size; i++) buffer[i] = data[i];
48 sensor1.flatten(buffer, size);
49 std::vector<uint8_t> buffer1(sensor1.getFlattenedSize());
50 auto ab = sensor1.unflatten(buffer1.data(), buffer1.size());
51 return 0;
52 }
53
54