Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/base/fuzzed_data_provider.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <limits> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 | |
| 13 namespace net { | |
| 14 | |
| 15 FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size) | |
| 16 : remaining_data_(reinterpret_cast<const char*>(data), size) {} | |
| 17 | |
| 18 FuzzedDataProvider::~FuzzedDataProvider() {} | |
| 19 | |
| 20 size_t FuzzedDataProvider::ConsumeBytes(char* dest, size_t bytes) { | |
| 21 size_t bytes_to_write = std::min(bytes, remaining_data_.length()); | |
| 22 memcpy(dest, remaining_data_.data(), bytes_to_write); | |
|
eroman
2016/04/28 01:32:35
side-rant: unfortunately because the definition of
mmenke
2016/04/28 16:01:38
remaining_data_.data() cannot be NULL, I believe.
| |
| 23 remaining_data_ = remaining_data_.substr(bytes_to_write); | |
| 24 return bytes_to_write; | |
| 25 } | |
| 26 | |
| 27 uint32_t FuzzedDataProvider::ConsumeValueInRange(uint32_t min, uint32_t max) { | |
| 28 CHECK_LE(min, max); | |
| 29 | |
| 30 uint32_t range = max - min; | |
| 31 uint32_t offset = 0; | |
| 32 uint32_t result = 0; | |
| 33 | |
| 34 while ((range >> offset) > 0 && !remaining_data_.empty()) { | |
|
eroman
2016/04/28 01:32:35
much simpler than earlier impl!
| |
| 35 // Pull bytes off the end of the seed data. Experimentally, this seems to | |
| 36 // allow the fuzzer to more easily explore the input space. This makes | |
| 37 // sense, since it works by modifying inputs that caused new code to run, | |
| 38 // and this data is often used to encode length of data read by | |
| 39 // ConsumeBytes. Separating out read lengths makes it easier modify the | |
| 40 // contents of the data that is actually read. | |
| 41 uint8_t next_byte = remaining_data_.data()[remaining_data_.length() - 1]; | |
| 42 result = (result << 8) | next_byte; | |
| 43 remaining_data_ = remaining_data_.substr(0, remaining_data_.length() - 1); | |
| 44 offset += 8; | |
| 45 } | |
| 46 | |
| 47 // Avoid division by 0, in the case the entire range is covered. | |
| 48 if (min == 0 && max == std::numeric_limits<uint32_t>::max()) | |
|
eroman
2016/04/28 01:32:36
nit: sufficient to test range == std::numeric_limi
mmenke
2016/04/28 16:01:38
Done.
| |
| 49 return result; | |
| 50 | |
| 51 return min + result % (range + 1); | |
| 52 } | |
| 53 | |
| 54 uint32_t FuzzedDataProvider::ConsumeBits(size_t num_bits) { | |
| 55 CHECK_NE(0u, num_bits); | |
| 56 CHECK_LE(num_bits, 32u); | |
| 57 | |
| 58 if (num_bits == 32) | |
| 59 return ConsumeValueInRange(0, std::numeric_limits<uint32_t>::max()); | |
| 60 return ConsumeValueInRange(0, (1 << num_bits) - 1); | |
| 61 } | |
| 62 | |
| 63 bool FuzzedDataProvider::ConsumeBool() { | |
| 64 return !ConsumeBits(1); | |
|
eroman
2016/04/28 01:32:36
So before in some places you were using !!ConsumeB
mmenke
2016/04/28 16:01:38
Ahh, good point. Changed, and added comment.
| |
| 65 } | |
| 66 | |
| 67 } // namespace net | |
| OLD | NEW |