blob: a29bcf330f113ac7326c6c767f0fdd0d2bbb1ca8 [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
danakj51d26a42024-04-25 14:23:565#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7#pragma allow_unsafe_buffers
8#endif
9
Max Moroz4905fa42019-08-29 13:52:3610#include <fuzzer/FuzzedDataProvider.h>
11
Aquibuzzaman Md. Sayem42abceb72024-05-08 18:48:2712#include <string_view>
Avi Drissman933398e2022-01-22 00:55:4213#include <tuple>
14
Avi Drissman516e1922024-03-21 18:11:2815#include "base/containers/span.h"
Anand K. Mistry7404c0582018-11-21 16:17:5116#include "base/pickle.h"
Anand K. Mistry7404c0582018-11-21 16:17:5117
18namespace {
19constexpr int kIterations = 16;
20constexpr int kReadControlBytes = 32;
21constexpr int kReadDataTypes = 17;
22constexpr int kMaxReadLength = 1024;
23constexpr int kMaxSkipBytes = 1024;
24} // namespace
25
26extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
27 if (size < kReadControlBytes) {
28 return 0;
29 }
30 // Use the first kReadControlBytes bytes of the fuzzer input to control how
31 // the pickled data is read.
Max Morozdc9c1c132019-07-01 23:45:1532 FuzzedDataProvider data_provider(data, kReadControlBytes);
Anand K. Mistry7404c0582018-11-21 16:17:5133 data += kReadControlBytes;
34 size -= kReadControlBytes;
35
Avi Drissman516e1922024-03-21 18:11:2836 base::Pickle pickle =
37 base::Pickle::WithUnownedBuffer(UNSAFE_BUFFERS(base::span(data, size)));
Anand K. Mistry7404c0582018-11-21 16:17:5138 base::PickleIterator iter(pickle);
39 for (int i = 0; i < kIterations; i++) {
Abhishek Arya3ca20a12018-11-28 18:56:0240 uint8_t read_type = data_provider.ConsumeIntegral<uint8_t>();
Anand K. Mistry7404c0582018-11-21 16:17:5141 switch (read_type % kReadDataTypes) {
42 case 0: {
43 bool result = 0;
Avi Drissman933398e2022-01-22 00:55:4244 std::ignore = iter.ReadBool(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5145 break;
46 }
47 case 1: {
48 int result = 0;
Avi Drissman933398e2022-01-22 00:55:4249 std::ignore = iter.ReadInt(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5150 break;
51 }
52 case 2: {
53 long result = 0;
Avi Drissman933398e2022-01-22 00:55:4254 std::ignore = iter.ReadLong(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5155 break;
56 }
57 case 3: {
58 uint16_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4259 std::ignore = iter.ReadUInt16(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5160 break;
61 }
62 case 4: {
63 uint32_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4264 std::ignore = iter.ReadUInt32(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5165 break;
66 }
67 case 5: {
68 int64_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4269 std::ignore = iter.ReadInt64(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5170 break;
71 }
72 case 6: {
73 uint64_t result = 0;
Avi Drissman933398e2022-01-22 00:55:4274 std::ignore = iter.ReadUInt64(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5175 break;
76 }
77 case 7: {
78 float result = 0;
Avi Drissman933398e2022-01-22 00:55:4279 std::ignore = iter.ReadFloat(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5180 break;
81 }
82 case 8: {
83 double result = 0;
Avi Drissman933398e2022-01-22 00:55:4284 std::ignore = iter.ReadDouble(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5185 break;
86 }
87 case 9: {
88 std::string result;
Avi Drissman933398e2022-01-22 00:55:4289 std::ignore = iter.ReadString(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5190 break;
91 }
92 case 10: {
Aquibuzzaman Md. Sayem42abceb72024-05-08 18:48:2793 std::string_view result;
Avi Drissman933398e2022-01-22 00:55:4294 std::ignore = iter.ReadStringPiece(&result);
Anand K. Mistry7404c0582018-11-21 16:17:5195 break;
96 }
97 case 11: {
Jan Wilken Dörrie85285b02021-03-11 23:38:4798 std::u16string result;
Avi Drissman933398e2022-01-22 00:55:4299 std::ignore = iter.ReadString16(&result);
Anand K. Mistry7404c0582018-11-21 16:17:51100 break;
101 }
102 case 12: {
Aquibuzzaman Md. Sayem42abceb72024-05-08 18:48:27103 std::u16string_view result;
Avi Drissman933398e2022-01-22 00:55:42104 std::ignore = iter.ReadStringPiece16(&result);
Anand K. Mistry7404c0582018-11-21 16:17:51105 break;
106 }
107 case 13: {
108 const char* data_result = nullptr;
Peter Kasting28b51cf2022-06-28 15:02:43109 size_t length_result = 0;
Avi Drissman933398e2022-01-22 00:55:42110 std::ignore = iter.ReadData(&data_result, &length_result);
Anand K. Mistry7404c0582018-11-21 16:17:51111 break;
112 }
113 case 14: {
114 const char* data_result = nullptr;
Abhishek Arya5b644f62018-11-28 00:47:17115 int read_length =
116 data_provider.ConsumeIntegralInRange(0, kMaxReadLength);
Peter Kasting28b51cf2022-06-28 15:02:43117 std::ignore =
118 iter.ReadBytes(&data_result, static_cast<size_t>(read_length));
Anand K. Mistry7404c0582018-11-21 16:17:51119 break;
120 }
121 case 15: {
Peter Kasting28b51cf2022-06-28 15:02:43122 size_t result = 0;
Avi Drissman933398e2022-01-22 00:55:42123 std::ignore = iter.ReadLength(&result);
Anand K. Mistry7404c0582018-11-21 16:17:51124 break;
125 }
126 case 16: {
Peter Kasting28b51cf2022-06-28 15:02:43127 std::ignore = iter.SkipBytes(static_cast<size_t>(
128 data_provider.ConsumeIntegralInRange(0, kMaxSkipBytes)));
Anand K. Mistry7404c0582018-11-21 16:17:51129 break;
130 }
131 }
132 }
133
134 return 0;
135}