Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Unified Diff: net/base/fuzzed_data_provider.cc

Issue 1917503002: URLRequest fuzzer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fuzz
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/base/fuzzed_data_provider.cc
diff --git a/net/base/fuzzed_data_provider.cc b/net/base/fuzzed_data_provider.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6c940c78dc11bf792ff624b6cb09053c95887d62
--- /dev/null
+++ b/net/base/fuzzed_data_provider.cc
@@ -0,0 +1,67 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/base/fuzzed_data_provider.h"
+
+#include <algorithm>
+#include <limits>
+#include <string>
+
+#include "base/logging.h"
+
+namespace net {
+
+FuzzedDataProvider::FuzzedDataProvider(const uint8_t* data, size_t size)
+ : remaining_data_(reinterpret_cast<const char*>(data), size) {}
+
+FuzzedDataProvider::~FuzzedDataProvider() {}
+
+size_t FuzzedDataProvider::ConsumeBytes(char* dest, size_t bytes) {
+ size_t bytes_to_write = std::min(bytes, remaining_data_.length());
+ 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.
+ remaining_data_ = remaining_data_.substr(bytes_to_write);
+ return bytes_to_write;
+}
+
+uint32_t FuzzedDataProvider::ConsumeValueInRange(uint32_t min, uint32_t max) {
+ CHECK_LE(min, max);
+
+ uint32_t range = max - min;
+ uint32_t offset = 0;
+ uint32_t result = 0;
+
+ while ((range >> offset) > 0 && !remaining_data_.empty()) {
eroman 2016/04/28 01:32:35 much simpler than earlier impl!
+ // Pull bytes off the end of the seed data. Experimentally, this seems to
+ // allow the fuzzer to more easily explore the input space. This makes
+ // sense, since it works by modifying inputs that caused new code to run,
+ // and this data is often used to encode length of data read by
+ // ConsumeBytes. Separating out read lengths makes it easier modify the
+ // contents of the data that is actually read.
+ uint8_t next_byte = remaining_data_.data()[remaining_data_.length() - 1];
+ result = (result << 8) | next_byte;
+ remaining_data_ = remaining_data_.substr(0, remaining_data_.length() - 1);
+ offset += 8;
+ }
+
+ // Avoid division by 0, in the case the entire range is covered.
+ 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.
+ return result;
+
+ return min + result % (range + 1);
+}
+
+uint32_t FuzzedDataProvider::ConsumeBits(size_t num_bits) {
+ CHECK_NE(0u, num_bits);
+ CHECK_LE(num_bits, 32u);
+
+ if (num_bits == 32)
+ return ConsumeValueInRange(0, std::numeric_limits<uint32_t>::max());
+ return ConsumeValueInRange(0, (1 << num_bits) - 1);
+}
+
+bool FuzzedDataProvider::ConsumeBool() {
+ 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.
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698