blob: 46bf4178d0262a68f0f9e59b9595d23cce4070a2 [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"
Steven Moreland5e561af2020-10-08 02:16:03 +000021#include "random_parcel.h"
Steven Moreland46e0da72019-09-05 15:52:02 -070022#include "util.h"
23
24#include <android-base/logging.h>
Steven Moreland842b5932020-10-08 00:23:35 +000025#include <fuzzer/FuzzedDataProvider.h>
Steven Moreland46e0da72019-09-05 15:52:02 -070026
27#include <cstdlib>
28#include <ctime>
29
Steven Moreland5e561af2020-10-08 02:16:03 +000030using android::fillRandomParcel;
31
32void fillRandomParcel(::android::hardware::Parcel* p, FuzzedDataProvider&& provider) {
33 std::vector<uint8_t> input = provider.ConsumeRemainingBytes<uint8_t>();
34 p->setData(input.data(), input.size());
35}
36
Steven Moreland46e0da72019-09-05 15:52:02 -070037template <typename P>
Steven Moreland842b5932020-10-08 00:23:35 +000038void doFuzz(const char* backend, const std::vector<ParcelRead<P>>& reads,
Steven Moreland5e561af2020-10-08 02:16:03 +000039 FuzzedDataProvider&& provider) {
Steven Moreland842b5932020-10-08 00:23:35 +000040 // Allow some majority of the bytes to be dedicated to telling us what to
41 // do. The fixed value added here represents that we want to test doing a
42 // lot of 'instructions' even on really short parcels.
Steven Moreland5e561af2020-10-08 02:16:03 +000043 size_t maxInstructions = 20 + (provider.remaining_bytes() * 2 / 3);
Steven Moreland842b5932020-10-08 00:23:35 +000044 // but don't always use that many instructions. We want to allow the fuzzer
45 // to explore large parcels with few instructions if it wants to.
Steven Moreland5e561af2020-10-08 02:16:03 +000046 std::vector<uint8_t> instructions = provider.ConsumeBytes<uint8_t>(
47 provider.ConsumeIntegralInRange<size_t>(0, maxInstructions));
Steven Moreland46e0da72019-09-05 15:52:02 -070048
49 P p;
Steven Moreland5e561af2020-10-08 02:16:03 +000050 fillRandomParcel(&p, std::move(provider));
Steven Moreland46e0da72019-09-05 15:52:02 -070051
Steven Moreland7eac78a2019-10-11 18:46:24 -070052 // since we are only using a byte to index
53 CHECK(reads.size() <= 255) << reads.size();
54
Steven Moreland842b5932020-10-08 00:23:35 +000055 FUZZ_LOG() << "backend: " << backend;
Steven Moreland5e561af2020-10-08 02:16:03 +000056 FUZZ_LOG() << "input: " << hexString(p.data(), p.dataSize());
Steven Moreland842b5932020-10-08 00:23:35 +000057 FUZZ_LOG() << "instructions: " << hexString(instructions);
58
59 for (size_t i = 0; i + 1 < instructions.size(); i += 2) {
Steven Moreland46e0da72019-09-05 15:52:02 -070060 uint8_t a = instructions[i];
Steven Morelanddc449dc2019-10-10 10:06:58 -070061 uint8_t readIdx = a % reads.size();
62
Steven Moreland46e0da72019-09-05 15:52:02 -070063 uint8_t b = instructions[i + 1];
64
Steven Morelanddc449dc2019-10-10 10:06:58 -070065 FUZZ_LOG() << "Instruction: " << (i / 2) + 1 << "/" << instructions.size() / 2
66 << " cmd: " << static_cast<size_t>(a) << " (" << static_cast<size_t>(readIdx)
67 << ") arg: " << static_cast<size_t>(b) << " size: " << p.dataSize()
68 << " avail: " << p.dataAvail() << " pos: " << p.dataPosition()
69 << " cap: " << p.dataCapacity();
Steven Moreland46e0da72019-09-05 15:52:02 -070070
Steven Morelanddc449dc2019-10-10 10:06:58 -070071 reads[readIdx](p, b);
Steven Moreland46e0da72019-09-05 15:52:02 -070072 }
73}
74
Steven Moreland46e0da72019-09-05 15:52:02 -070075extern "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 Moreland842b5932020-10-08 00:23:35 +000081 FuzzedDataProvider provider = FuzzedDataProvider(data, size);
Steven Moreland46e0da72019-09-05 15:52:02 -070082
Steven Moreland5e561af2020-10-08 02:16:03 +000083 const std::function<void(FuzzedDataProvider &&)> fuzzBackend[3] = {
84 [](FuzzedDataProvider&& provider) {
Steven Moreland842b5932020-10-08 00:23:35 +000085 doFuzz<::android::hardware::Parcel>("hwbinder", HWBINDER_PARCEL_READ_FUNCTIONS,
Steven Moreland5e561af2020-10-08 02:16:03 +000086 std::move(provider));
Steven Moreland842b5932020-10-08 00:23:35 +000087 },
Steven Moreland5e561af2020-10-08 02:16:03 +000088 [](FuzzedDataProvider&& provider) {
89 doFuzz<::android::Parcel>("binder", BINDER_PARCEL_READ_FUNCTIONS,
90 std::move(provider));
Steven Moreland842b5932020-10-08 00:23:35 +000091 },
Steven Moreland5e561af2020-10-08 02:16:03 +000092 [](FuzzedDataProvider&& provider) {
93 doFuzz<NdkParcelAdapter>("binder_ndk", BINDER_NDK_PARCEL_READ_FUNCTIONS,
94 std::move(provider));
Steven Moreland842b5932020-10-08 00:23:35 +000095 },
96 };
Steven Moreland46e0da72019-09-05 15:52:02 -070097
Steven Moreland5e561af2020-10-08 02:16:03 +000098 provider.PickValueInArray(fuzzBackend)(std::move(provider));
Steven Moreland46e0da72019-09-05 15:52:02 -070099
Steven Moreland46e0da72019-09-05 15:52:02 -0700100 return 0;
101}