blob: bedab86190ff1aa78d7e7695de219f9fdd55045d [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) {
Steven Morelandea9ed862019-10-10 12:43:29 -070054 uint8_t parcelType = options & 0x3;
Steven Moreland46e0da72019-09-05 15:52:02 -070055
Steven Morelandea9ed862019-10-10 12:43:29 -070056 switch (parcelType) {
57 case 0x0:
58 doFuzz<::android::hardware::Parcel>(HWBINDER_PARCEL_READ_FUNCTIONS, input,
59 instructions);
60 break;
61 case 0x1:
62 doFuzz<::android::Parcel>(BINDER_PARCEL_READ_FUNCTIONS, input, instructions);
63 break;
64 case 0x2:
65 doFuzz<NdkParcelAdapter>(BINDER_NDK_PARCEL_READ_FUNCTIONS, input, instructions);
66 break;
67 case 0x3:
68 /*reserved for future use*/
69 break;
70 default:
71 LOG_ALWAYS_FATAL("unknown parcel type %d", static_cast<int>(parcelType));
72 }
Steven Moreland46e0da72019-09-05 15:52:02 -070073}
74
75extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
76 if (size <= 1) return 0; // no use
Steven Moreland98947412019-10-15 10:36:05 -070077
78 // avoid timeouts, see b/142617274, b/142473153
79 if (size > 50000) return 0;
80
Steven Moreland46e0da72019-09-05 15:52:02 -070081 uint8_t options = *data;
82 data++;
83 size--;
84
85 // TODO: generate 'objects' data
86
87 // data to fill out parcel
88 size_t inputLen = size / 2;
89 std::vector<uint8_t> input(data, data + inputLen);
90 data += inputLen;
91 size -= inputLen;
92
93 // data to use to determine what to do
94 size_t instructionLen = size;
95 std::vector<uint8_t> instructions(data, data + instructionLen);
96 data += instructionLen;
97 size -= instructionLen;
98
99 CHECK(size == 0) << "size: " << size;
100
101 FUZZ_LOG() << "options: " << (int)options << " inputLen: " << inputLen << " instructionLen: " << instructionLen;
102 FUZZ_LOG() << "input: " << hexString(input);
103 FUZZ_LOG() << "instructions: " << hexString(instructions);
104
105 fuzz(options, input, instructions);
106 return 0;
107}