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 <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 kMaxLengthForFuzzedData = 512; | |
|
eroman
2016/04/28 00:38:24
Can you remove this constant?
I think instead wha
Charlie Harrison
2016/04/29 14:24:16
Done. Good idea.
| |
| 20 const size_t kMaxLengthForFuzzedRange = 32; | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 // This class tests creating and reading to completion a URLRequest with fuzzed | |
| 25 // input. The fuzzer provides a data: URL and optionally generates custom Range | |
| 26 // headers. The amount of data read in each Read call is also fuzzed, as is | |
| 27 // the size of the IOBuffer to read data into. | |
| 28 class URLRequestDataJobFuzzerHarness : public net::URLRequest::Delegate { | |
| 29 public: | |
| 30 URLRequestDataJobFuzzerHarness() | |
| 31 : context_(true), task_runner_(base::ThreadTaskRunnerHandle::Get()) { | |
| 32 job_factory_.SetProtocolHandler( | |
| 33 "data", base::WrapUnique(new net::DataProtocolHandler())); | |
| 34 context_.set_job_factory(&job_factory_); | |
| 35 context_.Init(); | |
| 36 } | |
| 37 | |
| 38 static URLRequestDataJobFuzzerHarness* GetInstance() { | |
| 39 return base::Singleton<URLRequestDataJobFuzzerHarness>::get(); | |
| 40 } | |
| 41 | |
| 42 int CreateAndReadFromDataURLRequest(const uint8_t* data, size_t size) { | |
| 43 net::FuzzedDataProvider provider(data, size); | |
| 44 read_lengths_.clear(); | |
|
eroman
2016/04/28 00:38:24
optional:
I seem to remember that resize(0) runs
Charlie Harrison
2016/04/29 14:24:16
Keeping this as is for clarity. From what I read o
| |
| 45 | |
| 46 // Allocate an IOBuffer with fuzzed size. | |
| 47 uint32_t buf_size = provider.ConsumeValueInRange(1, 127); // 7 bits. | |
| 48 scoped_refptr<net::IOBuffer> buf( | |
| 49 new net::IOBuffer(static_cast<size_t>(buf_size))); | |
| 50 buf_.swap(buf); | |
| 51 | |
| 52 // Generate a range header, and a bool determining whether to use it. | |
| 53 // Generate the header regardless of the bool value to keep the data url and | |
| 54 // header in consistent byte addresses so the fuzzer doesn't have to work as | |
| 55 // hard. | |
| 56 bool use_range = provider.ConsumeBool(); | |
| 57 size_t range_size = | |
| 58 provider.ConsumeBytes(range_bytes_, kMaxLengthForFuzzedRange); | |
| 59 | |
| 60 // Generate a sequence of reads sufficient to read the entire data url. | |
|
eroman
2016/04/28 00:38:24
nit: url --> URL throughout.
Charlie Harrison
2016/04/29 14:24:16
Done.
| |
| 61 size_t simulated_bytes_read = 0; | |
| 62 while (simulated_bytes_read < provider.remaining_bytes()) { | |
| 63 size_t read_length = provider.ConsumeValueInRange(1, buf_size); | |
| 64 read_lengths_.push_back(read_length); | |
| 65 simulated_bytes_read += read_length; | |
| 66 } | |
| 67 | |
| 68 // The data url is the rest of the fuzzed data. | |
| 69 size_t data_size = | |
| 70 provider.ConsumeBytes(data_bytes_, kMaxLengthForFuzzedData); | |
| 71 GURL data_url = GURL(std::string(data_bytes_, data_size)); | |
|
eroman
2016/04/28 00:38:24
nit:
GURL data_url(std::string(...));
Charlie Harrison
2016/04/29 14:24:16
Done.
| |
| 72 if (!data_url.is_valid()) | |
| 73 return 0; | |
|
eroman
2016/04/28 00:38:24
rather than return 0, I would suggest using some d
Charlie Harrison
2016/04/29 14:24:17
Done.
| |
| 74 | |
| 75 // Create a URLRequest with the given data url and start reading | |
| 76 // from it. | |
| 77 std::unique_ptr<net::URLRequest> request = | |
| 78 context_.CreateRequest(data_url, net::DEFAULT_PRIORITY, this); | |
| 79 if (use_range) { | |
| 80 std::string range = std::string(range_bytes_, range_size); | |
| 81 if (!net::HttpUtil::IsValidHeaderValue(range)) | |
| 82 return 0; | |
|
eroman
2016/04/28 00:38:24
Same here -- might as well just proceed with no he
Charlie Harrison
2016/04/29 14:24:16
Done.
| |
| 83 request->SetExtraRequestHeaderByName("Range", range, true); | |
| 84 } | |
| 85 | |
| 86 // Block the thread while the request is read. | |
| 87 base::RunLoop read_loop; | |
| 88 read_loop_ = &read_loop; | |
| 89 request->Start(); | |
| 90 read_loop.Run(); | |
| 91 read_loop_ = nullptr; | |
| 92 return 0; | |
| 93 } | |
| 94 | |
| 95 void QuitLoop() { | |
| 96 DCHECK(read_loop_); | |
| 97 task_runner_->PostTask(FROM_HERE, read_loop_->QuitClosure()); | |
| 98 } | |
| 99 | |
| 100 void ReadFromRequest(net::URLRequest* request) { | |
| 101 bool sync = false; | |
| 102 do { | |
| 103 // If possible, pop the next read size. If none exists, then this should | |
| 104 // be the last call to Read. | |
| 105 bool using_populated_read = read_lengths_.size() > 0; | |
| 106 size_t read_size = 1; | |
| 107 if (using_populated_read) { | |
| 108 read_size = read_lengths_.back(); | |
| 109 read_lengths_.pop_back(); | |
| 110 } | |
| 111 | |
| 112 int bytes_read = 0; | |
| 113 sync = request->Read(buf_.get(), read_size, &bytes_read); | |
| 114 // No more populated reads implies !bytes_read. | |
| 115 DCHECK(using_populated_read || !bytes_read); | |
| 116 } while (sync); | |
| 117 | |
| 118 if (!request->status().is_io_pending()) | |
| 119 QuitLoop(); | |
| 120 } | |
| 121 | |
| 122 // net::URLRequest::Delegate: | |
| 123 void OnReceivedRedirect(net::URLRequest* request, | |
| 124 const net::RedirectInfo& redirect_info, | |
| 125 bool* defer_redirect) override {} | |
| 126 void OnAuthRequired(net::URLRequest* request, | |
| 127 net::AuthChallengeInfo* auth_info) override {} | |
| 128 void OnCertificateRequested( | |
| 129 net::URLRequest* request, | |
| 130 net::SSLCertRequestInfo* cert_request_info) override {} | |
| 131 void OnSSLCertificateError(net::URLRequest* request, | |
| 132 const net::SSLInfo& ssl_info, | |
| 133 bool fatal) override {} | |
| 134 void OnBeforeNetworkStart(net::URLRequest* request, bool* defer) override {} | |
| 135 void OnResponseStarted(net::URLRequest* request) override { | |
| 136 DCHECK(!request->status().is_io_pending()); | |
| 137 DCHECK(buf_.get()); | |
| 138 DCHECK(read_loop_); | |
| 139 if (request->status().is_success()) { | |
| 140 ReadFromRequest(request); | |
| 141 } else { | |
| 142 QuitLoop(); | |
| 143 } | |
| 144 } | |
| 145 void OnReadCompleted(net::URLRequest* request, int bytes_read) override { | |
| 146 DCHECK(!request->status().is_io_pending()); | |
| 147 DCHECK(buf_.get()); | |
| 148 DCHECK(read_loop_); | |
| 149 if (request->status().is_success() && bytes_read > 0) { | |
| 150 ReadFromRequest(request); | |
| 151 } else { | |
| 152 QuitLoop(); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 private: | |
| 157 friend struct base::DefaultSingletonTraits<URLRequestDataJobFuzzerHarness>; | |
| 158 | |
| 159 char data_bytes_[kMaxLengthForFuzzedData]; | |
| 160 char range_bytes_[kMaxLengthForFuzzedRange]; | |
| 161 net::TestURLRequestContext context_; | |
| 162 net::URLRequestJobFactoryImpl job_factory_; | |
| 163 std::vector<size_t> read_lengths_; | |
| 164 scoped_refptr<net::IOBuffer> buf_; | |
| 165 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 166 base::RunLoop* read_loop_; | |
| 167 | |
| 168 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness); | |
| 169 }; | |
| 170 | |
| 171 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
| 172 // Using a static singleton test harness lets the test run ~3-4x faster. | |
| 173 return URLRequestDataJobFuzzerHarness::GetInstance() | |
| 174 ->CreateAndReadFromDataURLRequest(data, size); | |
| 175 } | |
| OLD | NEW |