blob: 369aa34a9b32d85907f890ee6e97936c88c7349c [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];
39 uint8_t b = instructions[i + 1];
40
41 FUZZ_LOG() << "size: " << p.dataSize() << " avail: " << p.dataAvail()
42 << " pos: " << p.dataPosition() << " cap: " << p.dataCapacity();
43
44 reads[a % reads.size()](p, b);
45 }
46}
47
48void fuzz(uint8_t options, const std::vector<uint8_t>& input, const std::vector<uint8_t>& instructions) {
49 (void) options;
50
51 // although they will do completely different things, might as well fuzz both
52 doFuzz<::android::hardware::Parcel>(HWBINDER_PARCEL_READ_FUNCTIONS, input, instructions);
53 doFuzz<::android::Parcel>(BINDER_PARCEL_READ_FUNCTIONS, input, instructions);
Steven Moreland28f81422019-10-03 10:40:59 -070054 doFuzz<NdkParcelAdapter>(BINDER_NDK_PARCEL_READ_FUNCTIONS, input, instructions);
Steven Moreland46e0da72019-09-05 15:52:02 -070055}
56
57extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
58 if (size <= 1) return 0; // no use
59 uint8_t options = *data;
60 data++;
61 size--;
62
63 // TODO: generate 'objects' data
64
65 // data to fill out parcel
66 size_t inputLen = size / 2;
67 std::vector<uint8_t> input(data, data + inputLen);
68 data += inputLen;
69 size -= inputLen;
70
71 // data to use to determine what to do
72 size_t instructionLen = size;
73 std::vector<uint8_t> instructions(data, data + instructionLen);
74 data += instructionLen;
75 size -= instructionLen;
76
77 CHECK(size == 0) << "size: " << size;
78
79 FUZZ_LOG() << "options: " << (int)options << " inputLen: " << inputLen << " instructionLen: " << instructionLen;
80 FUZZ_LOG() << "input: " << hexString(input);
81 FUZZ_LOG() << "instructions: " << hexString(instructions);
82
83 fuzz(options, input, instructions);
84 return 0;
85}