OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/fuzzed_data_provider.h" | 5 #include "net/base/fuzzed_data_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 | 11 |
12 namespace net { | 12 namespace net { |
13 | 13 |
14 FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size) | 14 FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size) |
15 : remaining_data_(reinterpret_cast<const char*>(data), size) {} | 15 : remaining_data_(reinterpret_cast<const char*>(data), size) {} |
16 | 16 |
17 FuzzedDataProvider::~FuzzedDataProvider() {} | 17 FuzzedDataProvider::~FuzzedDataProvider() {} |
18 | 18 |
19 base::StringPiece FuzzedDataProvider::ConsumeBytes(size_t num_bytes) { | 19 base::StringPiece FuzzedDataProvider::ConsumeBytes(size_t num_bytes) { |
20 num_bytes = std::min(num_bytes, remaining_data_.length()); | 20 num_bytes = std::min(num_bytes, remaining_data_.length()); |
21 base::StringPiece result(remaining_data_.data(), num_bytes); | 21 base::StringPiece result(remaining_data_.data(), num_bytes); |
22 remaining_data_ = remaining_data_.substr(num_bytes); | 22 remaining_data_ = remaining_data_.substr(num_bytes); |
23 return result; | 23 return result; |
24 } | 24 } |
25 | 25 |
26 base::StringPiece FuzzedDataProvider::ConsumeRemainingBytes() { | 26 base::StringPiece FuzzedDataProvider::ConsumeRemainingBytes() { |
27 return ConsumeBytes(remaining_data_.length()); | 27 return ConsumeBytes(remaining_data_.length()); |
28 } | 28 } |
29 | 29 |
30 uint32_t FuzzedDataProvider::ConsumeValueInRange(uint32_t min, uint32_t max) { | 30 uint32_t FuzzedDataProvider::ConsumeUint32InRange(uint32_t min, uint32_t max) { |
31 CHECK_LE(min, max); | 31 CHECK_LE(min, max); |
32 | 32 |
33 uint32_t range = max - min; | 33 uint32_t range = max - min; |
34 uint32_t offset = 0; | 34 uint32_t offset = 0; |
35 uint32_t result = 0; | 35 uint32_t result = 0; |
36 | 36 |
37 while ((range >> offset) > 0 && !remaining_data_.empty()) { | 37 while ((range >> offset) > 0 && !remaining_data_.empty()) { |
38 // Pull bytes off the end of the seed data. Experimentally, this seems to | 38 // Pull bytes off the end of the seed data. Experimentally, this seems to |
39 // allow the fuzzer to more easily explore the input space. This makes | 39 // allow the fuzzer to more easily explore the input space. This makes |
40 // sense, since it works by modifying inputs that caused new code to run, | 40 // sense, since it works by modifying inputs that caused new code to run, |
41 // and this data is often used to encode length of data read by | 41 // and this data is often used to encode length of data read by |
42 // ConsumeBytes. Separating out read lengths makes it easier modify the | 42 // ConsumeBytes. Separating out read lengths makes it easier modify the |
43 // contents of the data that is actually read. | 43 // contents of the data that is actually read. |
44 uint8_t next_byte = remaining_data_.back(); | 44 uint8_t next_byte = remaining_data_.back(); |
45 remaining_data_.remove_suffix(1); | 45 remaining_data_.remove_suffix(1); |
46 result = (result << 8) | next_byte; | 46 result = (result << 8) | next_byte; |
47 offset += 8; | 47 offset += 8; |
48 } | 48 } |
49 | 49 |
50 // Avoid division by 0, in the case |range + 1| results in overflow. | 50 // Avoid division by 0, in the case |range + 1| results in overflow. |
51 if (range == std::numeric_limits<uint32_t>::max()) | 51 if (range == std::numeric_limits<uint32_t>::max()) |
52 return result; | 52 return result; |
53 | 53 |
54 return min + result % (range + 1); | 54 return min + result % (range + 1); |
55 } | 55 } |
56 | 56 |
| 57 int FuzzedDataProvider::ConsumeInt32InRange(int min, int max) { |
| 58 CHECK_LE(min, max); |
| 59 |
| 60 uint32_t range = max - min; |
| 61 return min + ConsumeUint32InRange(0, range); |
| 62 } |
| 63 |
57 bool FuzzedDataProvider::ConsumeBool() { | 64 bool FuzzedDataProvider::ConsumeBool() { |
58 return (ConsumeUint8() & 0x01) == 0x01; | 65 return (ConsumeUint8() & 0x01) == 0x01; |
59 } | 66 } |
60 | 67 |
61 uint8_t FuzzedDataProvider::ConsumeUint8() { | 68 uint8_t FuzzedDataProvider::ConsumeUint8() { |
62 return ConsumeValueInRange(0, 0xFF); | 69 return ConsumeUint32InRange(0, 0xFF); |
63 } | 70 } |
64 | 71 |
65 uint16_t FuzzedDataProvider::ConsumeUint16() { | 72 uint16_t FuzzedDataProvider::ConsumeUint16() { |
66 return ConsumeValueInRange(0, 0xFFFF); | 73 return ConsumeUint32InRange(0, 0xFFFF); |
67 } | 74 } |
68 | 75 |
69 } // namespace net | 76 } // namespace net |
OLD | NEW |