blob: 4a7afebc2e25fadb4e6cf67d998d91319596b16f [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2018 The Chromium Authors
Anand K. Mistry7404c0582018-11-21 16:17:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Max Moroz4905fa42019-08-29 13:52:365#include <fuzzer/FuzzedDataProvider.h>
6
Avi Drissman933398e2022-01-22 00:55:427#include <tuple>
8
Anand K. Mistry7404c0582018-11-21 16:17:519#include "base/pickle.h"
Anand K. Mistry7404c0582018-11-21 16:17:5110
11namespace {
12constexpr int kIterations = 16;
13constexpr int kReadControlBytes = 32;
14constexpr int kReadDataTypes = 17;
15constexpr int kMaxReadLength = 1024;
16constexpr int kMaxSkipBytes = 1024;
17} // namespace
18
19extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
20 if (size < kReadControlBytes) {
21 return 0;
22 }
23 // Use the first kReadControlBytes bytes of the fuzzer input to control how
24 // the pickled data is read.
Max Morozdc9c1c132019-07-01 23:45:1525 FuzzedDataProvider data_provider(data, kReadControlBytes);
Anand K. Mistry7404c0582018-11-21 16:17:5126 data += kReadControlBytes;
27 size -= kReadControlBytes;
28
29 base::Pickle pickle(reinterpret_cast<const char*>(data), size);
30 base::PickleIterator iter(pickle);
31 for (int i = 0; i < kIterations; i++) {
Abhishek Arya3ca20a12018-11-28 18:56:0232 uint8_t read_type = data_provider.ConsumeIntegral<uint8_t>();
Anand K. Mistry7404c0582018-11-21 16:17:5133 switch (read_type % kReadDataTypes) {
34 case 0: {
35 bool result = 0;
Avi Drissman933398e2022-01-22 00:55:4236 std::ignore = iter.ReadBool(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5137 break;
38 }
39 case 1: {
40 int result = 0;
Avi Drissman933398e2022-01-22 00:55:4241 std::ignore = iter.ReadInt(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5142 break;
43 }
44 case 2: {
45 long result = 0;
Avi Drissman933398e2022-01-22 00:55:4246 std::ignore = iter.ReadLong(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5147 break;
48 }
49 case 3: {
50 uint16_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4251 std::ignore = iter.ReadUInt16(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5152 break;
53 }
54 case 4: {
55 uint32_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4256 std::ignore = iter.ReadUInt32(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5157 break;
58 }
59 case 5: {
60 int64_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4261 std::ignore = iter.ReadInt64(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5162 break;
63 }
64 case 6: {
65 uint64_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4266 std::ignore = iter.ReadUInt64(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5167 break;
68 }
69 case 7: {
70 float result = 0;
Avi Drissman933398e2022-01-22 00:55:4271 std::ignore = iter.ReadFloat(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5172 break;
73 }
74 case 8: {
75 double result = 0;
Avi Drissman933398e2022-01-22 00:55:4276 std::ignore = iter.ReadDouble(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5177 break;
78 }
79 case 9: {
80 std::string result;
Avi Drissman933398e2022-01-22 00:55:4281 std::ignore = iter.ReadString(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5182 break;
83 }
84 case 10: {
85 base::StringPiece result;
Avi Drissman933398e2022-01-22 00:55:4286 std::ignore = iter.ReadStringPiece(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5187 break;
88 }
89 case 11: {
Jan Wilken Dörrie85285b02021-03-11 23:38:4790 std::u16string result;
Avi Drissman933398e2022-01-22 00:55:4291 std::ignore = iter.ReadString16(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5192 break;
93 }
94 case 12: {
95 base::StringPiece16 result;
Avi Drissman933398e2022-01-22 00:55:4296 std::ignore = iter.ReadStringPiece16(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5197 break;
98 }
99 case 13: {
100 const char* data_result = nullptr;
Peter Kasting28b51cf2022-06-28 15:02:43101 size_t length_result = 0;
Avi Drissman933398e2022-01-22 00:55:42102 std::ignore = iter.ReadData(&data_result, &length_result);
Anand K. Mistry7404c0582018-11-21 16:17:51103 break;
104 }
105 case 14: {
106 const char* data_result = nullptr;
Abhishek Arya5b644f62018-11-28 00:47:17107 int read_length =
108 data_provider.ConsumeIntegralInRange(0, kMaxReadLength);
Peter Kasting28b51cf2022-06-28 15:02:43109 std::ignore =
110 iter.ReadBytes(&data_result, static_cast<size_t>(read_length));
Anand K. Mistry7404c0582018-11-21 16:17:51111 break;
112 }
113 case 15: {
Peter Kasting28b51cf2022-06-28 15:02:43114 size_t result = 0;
Avi Drissman933398e2022-01-22 00:55:42115 std::ignore = iter.ReadLength(&result);
Anand K. Mistry7404c0582018-11-21 16:17:51116 break;
117 }
118 case 16: {
Peter Kasting28b51cf2022-06-28 15:02:43119 std::ignore = iter.SkipBytes(static_cast<size_t>(
120 data_provider.ConsumeIntegralInRange(0, kMaxSkipBytes)));
Anand K. Mistry7404c0582018-11-21 16:17:51121 break;
122 }
123 }
124 }
125
126 return 0;
127}