blob: 386c70ba80935c9bac9c1c1db9562d4ce05167a9 [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>
Steven Moreland362e4da2020-10-16 19:49:39 +000024#include <fuzzbinder/random_parcel.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) {
Steven Moreland362e4da2020-10-16 19:49:39 +000033 // TODO: functionality to create random parcels for libhwbinder parcels
Steven Moreland5e561af2020-10-08 02:16:03 +000034 std::vector<uint8_t> input = provider.ConsumeRemainingBytes<uint8_t>();
35 p->setData(input.data(), input.size());
36}
Steven Moreland362e4da2020-10-16 19:49:39 +000037static void fillRandomParcel(NdkParcelAdapter* p, FuzzedDataProvider&& provider) {
38 // fill underlying parcel using functions to fill random libbinder parcel
39 fillRandomParcel(p->parcel(), std::move(provider));
40}
Steven Moreland5e561af2020-10-08 02:16:03 +000041
Steven Moreland46e0da72019-09-05 15:52:02 -070042template <typename P>
Steven Moreland842b5932020-10-08 00:23:35 +000043void doFuzz(const char* backend, const std::vector<ParcelRead<P>>& reads,
Steven Moreland5e561af2020-10-08 02:16:03 +000044 FuzzedDataProvider&& provider) {
Steven Moreland842b5932020-10-08 00:23:35 +000045 // Allow some majority of the bytes to be dedicated to telling us what to
46 // do. The fixed value added here represents that we want to test doing a
47 // lot of 'instructions' even on really short parcels.
Steven Moreland5e561af2020-10-08 02:16:03 +000048 size_t maxInstructions = 20 + (provider.remaining_bytes() * 2 / 3);
Steven Moreland842b5932020-10-08 00:23:35 +000049 // but don't always use that many instructions. We want to allow the fuzzer
50 // to explore large parcels with few instructions if it wants to.
Steven Moreland5e561af2020-10-08 02:16:03 +000051 std::vector<uint8_t> instructions = provider.ConsumeBytes<uint8_t>(
52 provider.ConsumeIntegralInRange<size_t>(0, maxInstructions));
Steven Moreland46e0da72019-09-05 15:52:02 -070053
54 P p;
Steven Moreland5e561af2020-10-08 02:16:03 +000055 fillRandomParcel(&p, std::move(provider));
Steven Moreland46e0da72019-09-05 15:52:02 -070056
Steven Moreland7eac78a2019-10-11 18:46:24 -070057 // since we are only using a byte to index
58 CHECK(reads.size() <= 255) << reads.size();
59
Steven Moreland842b5932020-10-08 00:23:35 +000060 FUZZ_LOG() << "backend: " << backend;
Steven Moreland5e561af2020-10-08 02:16:03 +000061 FUZZ_LOG() << "input: " << hexString(p.data(), p.dataSize());
Steven Moreland842b5932020-10-08 00:23:35 +000062 FUZZ_LOG() << "instructions: " << hexString(instructions);
63
64 for (size_t i = 0; i + 1 < instructions.size(); i += 2) {
Steven Moreland46e0da72019-09-05 15:52:02 -070065 uint8_t a = instructions[i];
Steven Morelanddc449dc2019-10-10 10:06:58 -070066 uint8_t readIdx = a % reads.size();
67
Steven Moreland46e0da72019-09-05 15:52:02 -070068 uint8_t b = instructions[i + 1];
69
Steven Morelanddc449dc2019-10-10 10:06:58 -070070 FUZZ_LOG() << "Instruction: " << (i / 2) + 1 << "/" << instructions.size() / 2
71 << " cmd: " << static_cast<size_t>(a) << " (" << static_cast<size_t>(readIdx)
72 << ") arg: " << static_cast<size_t>(b) << " size: " << p.dataSize()
73 << " avail: " << p.dataAvail() << " pos: " << p.dataPosition()
74 << " cap: " << p.dataCapacity();
Steven Moreland46e0da72019-09-05 15:52:02 -070075
Steven Morelanddc449dc2019-10-10 10:06:58 -070076 reads[readIdx](p, b);
Steven Moreland46e0da72019-09-05 15:52:02 -070077 }
78}
79
Steven Moreland46e0da72019-09-05 15:52:02 -070080extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
81 if (size <= 1) return 0; // no use
Steven Moreland98947412019-10-15 10:36:05 -070082
83 // avoid timeouts, see b/142617274, b/142473153
84 if (size > 50000) return 0;
85
Steven Moreland842b5932020-10-08 00:23:35 +000086 FuzzedDataProvider provider = FuzzedDataProvider(data, size);
Steven Moreland46e0da72019-09-05 15:52:02 -070087
Steven Moreland5e561af2020-10-08 02:16:03 +000088 const std::function<void(FuzzedDataProvider &&)> fuzzBackend[3] = {
89 [](FuzzedDataProvider&& provider) {
Steven Moreland842b5932020-10-08 00:23:35 +000090 doFuzz<::android::hardware::Parcel>("hwbinder", HWBINDER_PARCEL_READ_FUNCTIONS,
Steven Moreland5e561af2020-10-08 02:16:03 +000091 std::move(provider));
Steven Moreland842b5932020-10-08 00:23:35 +000092 },
Steven Moreland5e561af2020-10-08 02:16:03 +000093 [](FuzzedDataProvider&& provider) {
94 doFuzz<::android::Parcel>("binder", BINDER_PARCEL_READ_FUNCTIONS,
95 std::move(provider));
Steven Moreland842b5932020-10-08 00:23:35 +000096 },
Steven Moreland5e561af2020-10-08 02:16:03 +000097 [](FuzzedDataProvider&& provider) {
98 doFuzz<NdkParcelAdapter>("binder_ndk", BINDER_NDK_PARCEL_READ_FUNCTIONS,
99 std::move(provider));
Steven Moreland842b5932020-10-08 00:23:35 +0000100 },
101 };
Steven Moreland46e0da72019-09-05 15:52:02 -0700102
Steven Moreland5e561af2020-10-08 02:16:03 +0000103 provider.PickValueInArray(fuzzBackend)(std::move(provider));
Steven Moreland46e0da72019-09-05 15:52:02 -0700104
Steven Moreland46e0da72019-09-05 15:52:02 -0700105 return 0;
106}