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

Side by Side Diff: net/url_request/url_request_data_job_fuzzer.cc

Issue 1919013003: Add fuzzer to test Fuzz URLRequestDataJob (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@url_request_fuzzer
Patch Set: use vector<char> instead of constant size array Created 4 years, 7 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 unified diff | Download patch
« no previous file with comments | « net/base/fuzzed_data_provider.cc ('k') | net/url_request/url_request_test_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 <memory>
6
7 #include "base/memory/ptr_util.h"
8 #include "base/memory/singleton.h"
9 #include "base/run_loop.h"
10 #include "net/base/fuzzed_data_provider.h"
11 #include "net/http/http_request_headers.h"
12 #include "net/url_request/data_protocol_handler.h"
13 #include "net/url_request/url_request.h"
14 #include "net/url_request/url_request_job_factory_impl.h"
15 #include "net/url_request/url_request_test_util.h"
16
17 namespace {
18
19 const size_t kMaxLengthForFuzzedRange = 32;
20
21 } // namespace
22
23 // This class tests creating and reading to completion a URLRequest with fuzzed
24 // input. The fuzzer provides a data: URL and optionally generates custom Range
25 // headers. The amount of data read in each Read call is also fuzzed, as is
26 // the size of the IOBuffer to read data into.
27 class URLRequestDataJobFuzzerHarness : public net::URLRequest::Delegate {
28 public:
29 URLRequestDataJobFuzzerHarness()
30 : context_(true), task_runner_(base::ThreadTaskRunnerHandle::Get()) {
31 job_factory_.SetProtocolHandler(
32 "data", base::WrapUnique(new net::DataProtocolHandler()));
33 context_.set_job_factory(&job_factory_);
34 context_.Init();
35 }
36
37 static URLRequestDataJobFuzzerHarness* GetInstance() {
38 return base::Singleton<URLRequestDataJobFuzzerHarness>::get();
39 }
40
41 int CreateAndReadFromDataURLRequest(const uint8_t* data, size_t size) {
42 net::FuzzedDataProvider provider(data, size);
43 read_lengths_.clear();
44
45 // Allocate an IOBuffer with fuzzed size.
46 uint32_t buf_size = provider.ConsumeValueInRange(1, 127); // 7 bits.
47 scoped_refptr<net::IOBuffer> buf(
48 new net::IOBuffer(static_cast<size_t>(buf_size)));
49 buf_.swap(buf);
50
51 // Generate a range header, and a bool determining whether to use it.
52 // Generate the header regardless of the bool value to keep the data URL and
53 // header in consistent byte addresses so the fuzzer doesn't have to work as
54 // hard.
55 bool use_range = provider.ConsumeBool();
56 size_t range_size =
57 provider.ConsumeBytes(range_bytes_, kMaxLengthForFuzzedRange);
58
59 // Generate a sequence of reads sufficient to read the entire data URL.
60 size_t simulated_bytes_read = 0;
61 while (simulated_bytes_read < provider.remaining_bytes()) {
62 size_t read_length = provider.ConsumeValueInRange(1, buf_size);
63 read_lengths_.push_back(read_length);
64 simulated_bytes_read += read_length;
65 }
66
67 // The data URL is the rest of the fuzzed data. If the URL is invalid just
68 // use a test variant, so the fuzzer has a chance to execute something.
69 if (data_bytes_.size() < provider.remaining_bytes())
70 data_bytes_.resize(provider.remaining_bytes());
71 provider.ConsumeBytes(data_bytes_.data(), provider.remaining_bytes());
72 GURL data_url(std::string(data_bytes_.begin(), data_bytes_.end()));
73 if (!data_url.is_valid())
74 data_url = GURL("data:text/html;charset=utf-8,<p>test</p>");
75
76 // Create a URLRequest with the given data URL and start reading
77 // from it.
78 std::unique_ptr<net::URLRequest> request =
79 context_.CreateRequest(data_url, net::DEFAULT_PRIORITY, this);
80 if (use_range) {
81 std::string range = std::string(range_bytes_, range_size);
82 if (!net::HttpUtil::IsValidHeaderValue(range))
83 range = "bytes=3-";
84 request->SetExtraRequestHeaderByName("Range", range, true);
85 }
86
87 // Block the thread while the request is read.
88 base::RunLoop read_loop;
89 read_loop_ = &read_loop;
90 request->Start();
91 read_loop.Run();
92 read_loop_ = nullptr;
93 return 0;
94 }
95
96 void QuitLoop() {
97 DCHECK(read_loop_);
98 task_runner_->PostTask(FROM_HERE, read_loop_->QuitClosure());
99 }
100
101 void ReadFromRequest(net::URLRequest* request) {
102 bool sync = false;
103 do {
104 // If possible, pop the next read size. If none exists, then this should
105 // be the last call to Read.
106 bool using_populated_read = read_lengths_.size() > 0;
107 size_t read_size = 1;
108 if (using_populated_read) {
109 read_size = read_lengths_.back();
110 read_lengths_.pop_back();
111 }
112
113 int bytes_read = 0;
114 sync = request->Read(buf_.get(), read_size, &bytes_read);
115 // No more populated reads implies !bytes_read.
116 DCHECK(using_populated_read || !bytes_read);
117 } while (sync);
118
119 if (!request->status().is_io_pending())
120 QuitLoop();
121 }
122
123 // net::URLRequest::Delegate:
124 void OnReceivedRedirect(net::URLRequest* request,
125 const net::RedirectInfo& redirect_info,
126 bool* defer_redirect) override {}
127 void OnAuthRequired(net::URLRequest* request,
128 net::AuthChallengeInfo* auth_info) override {}
129 void OnCertificateRequested(
130 net::URLRequest* request,
131 net::SSLCertRequestInfo* cert_request_info) override {}
132 void OnSSLCertificateError(net::URLRequest* request,
133 const net::SSLInfo& ssl_info,
134 bool fatal) override {}
135 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override {}
136 void OnResponseStarted(net::URLRequest* request) override {
137 DCHECK(!request->status().is_io_pending());
138 DCHECK(buf_.get());
139 DCHECK(read_loop_);
140 if (request->status().is_success()) {
141 ReadFromRequest(request);
142 } else {
143 QuitLoop();
144 }
145 }
146 void OnReadCompleted(net::URLRequest* request, int bytes_read) override {
147 DCHECK(!request->status().is_io_pending());
148 DCHECK(buf_.get());
149 DCHECK(read_loop_);
150 if (request->status().is_success() && bytes_read > 0) {
151 ReadFromRequest(request);
152 } else {
153 QuitLoop();
154 }
155 }
156
157 private:
158 friend struct base::DefaultSingletonTraits<URLRequestDataJobFuzzerHarness>;
159
160 std::vector<char> data_bytes_;
161 char range_bytes_[kMaxLengthForFuzzedRange];
162 net::TestURLRequestContext context_;
163 net::URLRequestJobFactoryImpl job_factory_;
164 std::vector<size_t> read_lengths_;
165 scoped_refptr<net::IOBuffer> buf_;
166 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
167 base::RunLoop* read_loop_;
168
169 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness);
170 };
171
172 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
173 // Using a static singleton test harness lets the test run ~3-4x faster.
174 return URLRequestDataJobFuzzerHarness::GetInstance()
175 ->CreateAndReadFromDataURLRequest(data, size);
176 }
OLDNEW
« no previous file with comments | « net/base/fuzzed_data_provider.cc ('k') | net/url_request/url_request_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698