blob: 31b27ab47b198742f64d5cc909ba379b1ab1b484 [file] [log] [blame]
Steven Moreland46e0da72019-09-05 15:52:02 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#define FUZZ_LOG_TAG "main"
17
18#include "binder.h"
Steven Moreland28f81422019-10-03 10:40:59 -070019#include "binder_ndk.h"
Steven Moreland46e0da72019-09-05 15:52:02 -070020#include "hwbinder.h"
21#include "util.h"
22
23#include <android-base/logging.h>
24
25#include <cstdlib>
26#include <ctime>
27
28template <typename P>
29void doFuzz(
30 const std::vector<ParcelRead<P>>& reads,
31 const std::vector<uint8_t>& input,
32 const std::vector<uint8_t>& instructions) {
33
34 P p;
35 p.setData(input.data(), input.size());
36
37 for (size_t i = 0; i < instructions.size() - 1; i += 2) {
38 uint8_t a = instructions[i];
Steven Morelanddc449dc2019-10-10 10:06:58 -070039 uint8_t readIdx = a % reads.size();
40
Steven Moreland46e0da72019-09-05 15:52:02 -070041 uint8_t b = instructions[i + 1];
42
Steven Morelanddc449dc2019-10-10 10:06:58 -070043 FUZZ_LOG() << "Instruction: " << (i / 2) + 1 << "/" << instructions.size() / 2
44 << " cmd: " << static_cast<size_t>(a) << " (" << static_cast<size_t>(readIdx)
45 << ") arg: " << static_cast<size_t>(b) << " size: " << p.dataSize()
46 << " avail: " << p.dataAvail() << " pos: " << p.dataPosition()
47 << " cap: " << p.dataCapacity();
Steven Moreland46e0da72019-09-05 15:52:02 -070048
Steven Morelanddc449dc2019-10-10 10:06:58 -070049 reads[readIdx](p, b);
Steven Moreland46e0da72019-09-05 15:52:02 -070050 }
51}
52
53void fuzz(uint8_t options, const std::vector<uint8_t>& input, const std::vector<uint8_t>& instructions) {
54 (void) options;
55
56 // although they will do completely different things, might as well fuzz both
57 doFuzz<::android::hardware::Parcel>(HWBINDER_PARCEL_READ_FUNCTIONS, input, instructions);
58 doFuzz<::android::Parcel>(BINDER_PARCEL_READ_FUNCTIONS, input, instructions);
Steven Moreland28f81422019-10-03 10:40:59 -070059 doFuzz<NdkParcelAdapter>(BINDER_NDK_PARCEL_READ_FUNCTIONS, input, instructions);
Steven Moreland46e0da72019-09-05 15:52:02 -070060}
61
62extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
63 if (size <= 1) return 0; // no use
64 uint8_t options = *data;
65 data++;
66 size--;
67
68 // TODO: generate 'objects' data
69
70 // data to fill out parcel
71 size_t inputLen = size / 2;
72 std::vector<uint8_t> input(data, data + inputLen);
73 data += inputLen;
74 size -= inputLen;
75
76 // data to use to determine what to do
77 size_t instructionLen = size;
78 std::vector<uint8_t> instructions(data, data + instructionLen);
79 data += instructionLen;
80 size -= instructionLen;
81
82 CHECK(size == 0) << "size: " << size;
83
84 FUZZ_LOG() << "options: " << (int)options << " inputLen: " << inputLen << " instructionLen: " << instructionLen;
85 FUZZ_LOG() << "input: " << hexString(input);
86 FUZZ_LOG() << "instructions: " << hexString(instructions);
87
88 fuzz(options, input, instructions);
89 return 0;
90}