1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include <fuzzer/FuzzedDataProvider.h>
7 #include "base/macros.h"
8 #include "base/pickle.h"
11 constexpr int kIterations = 16;
12 constexpr int kReadControlBytes = 32;
13 constexpr int kReadDataTypes = 17;
14 constexpr int kMaxReadLength = 1024;
15 constexpr int kMaxSkipBytes = 1024;
18 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
19 if (size < kReadControlBytes) {
22 // Use the first kReadControlBytes bytes of the fuzzer input to control how
23 // the pickled data is read.
24 FuzzedDataProvider data_provider(data, kReadControlBytes);
25 data += kReadControlBytes;
26 size -= kReadControlBytes;
28 base::Pickle pickle(reinterpret_cast<const char*>(data), size);
29 base::PickleIterator iter(pickle);
30 for (int i = 0; i < kIterations; i++) {
31 uint8_t read_type = data_provider.ConsumeIntegral<uint8_t>();
32 switch (read_type % kReadDataTypes) {
35 ignore_result(iter.ReadBool(&result));
40 ignore_result(iter.ReadInt(&result));
45 ignore_result(iter.ReadLong(&result));
50 ignore_result(iter.ReadUInt16(&result));
55 ignore_result(iter.ReadUInt32(&result));
60 ignore_result(iter.ReadInt64(&result));
65 ignore_result(iter.ReadUInt64(&result));
70 ignore_result(iter.ReadFloat(&result));
75 ignore_result(iter.ReadDouble(&result));
80 ignore_result(iter.ReadString(&result));
84 base::StringPiece result;
85 ignore_result(iter.ReadStringPiece(&result));
89 std::u16string result;
90 ignore_result(iter.ReadString16(&result));
94 base::StringPiece16 result;
95 ignore_result(iter.ReadStringPiece16(&result));
99 const char* data_result = nullptr;
100 int length_result = 0;
101 ignore_result(iter.ReadData(&data_result, &length_result));
105 const char* data_result = nullptr;
107 data_provider.ConsumeIntegralInRange(0, kMaxReadLength);
108 ignore_result(iter.ReadBytes(&data_result, read_length));
113 ignore_result(iter.ReadLength(&result));
117 ignore_result(iter.SkipBytes(
118 data_provider.ConsumeIntegralInRange(0, kMaxSkipBytes)));