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 <memory> | 5 #include <memory> |
| 6 #include <string> |
6 | 7 |
7 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
8 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
10 #include "net/base/fuzzed_data_provider.h" | 11 #include "net/base/fuzzed_data_provider.h" |
11 #include "net/http/http_request_headers.h" | 12 #include "net/http/http_request_headers.h" |
12 #include "net/url_request/data_protocol_handler.h" | 13 #include "net/url_request/data_protocol_handler.h" |
13 #include "net/url_request/url_request.h" | 14 #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_job_factory_impl.h" |
15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 base::StringPiece range(provider.ConsumeBytes(kMaxLengthForFuzzedRange)); | 57 base::StringPiece range(provider.ConsumeBytes(kMaxLengthForFuzzedRange)); |
57 | 58 |
58 // Generate a sequence of reads sufficient to read the entire data URL. | 59 // Generate a sequence of reads sufficient to read the entire data URL. |
59 size_t simulated_bytes_read = 0; | 60 size_t simulated_bytes_read = 0; |
60 while (simulated_bytes_read < provider.remaining_bytes()) { | 61 while (simulated_bytes_read < provider.remaining_bytes()) { |
61 size_t read_length = provider.ConsumeUint32InRange(1, buf_size); | 62 size_t read_length = provider.ConsumeUint32InRange(1, buf_size); |
62 read_lengths_.push_back(read_length); | 63 read_lengths_.push_back(read_length); |
63 simulated_bytes_read += read_length; | 64 simulated_bytes_read += read_length; |
64 } | 65 } |
65 | 66 |
66 // The data URL is the rest of the fuzzed data. If the URL is invalid just | 67 // The data URL is the rest of the fuzzed data with "data:" prepended, to |
| 68 // ensure that if it's a URL, it's a data URL. If the URL is invalid just |
67 // use a test variant, so the fuzzer has a chance to execute something. | 69 // use a test variant, so the fuzzer has a chance to execute something. |
68 base::StringPiece data_bytes(provider.ConsumeRemainingBytes()); | 70 std::string data_url_string = |
69 GURL data_url(data_bytes); | 71 std::string("data:") + provider.ConsumeRemainingBytes().as_string(); |
| 72 GURL data_url(data_url_string); |
70 if (!data_url.is_valid()) | 73 if (!data_url.is_valid()) |
71 data_url = GURL("data:text/html;charset=utf-8,<p>test</p>"); | 74 data_url = GURL("data:text/html;charset=utf-8,<p>test</p>"); |
72 | 75 |
73 // Create a URLRequest with the given data URL and start reading | 76 // Create a URLRequest with the given data URL and start reading |
74 // from it. | 77 // from it. |
75 std::unique_ptr<net::URLRequest> request = | 78 std::unique_ptr<net::URLRequest> request = |
76 context_.CreateRequest(data_url, net::DEFAULT_PRIORITY, this); | 79 context_.CreateRequest(data_url, net::DEFAULT_PRIORITY, this); |
77 if (use_range) { | 80 if (use_range) { |
78 std::string range_str = range.as_string(); | 81 std::string range_str = range.as_string(); |
79 if (!net::HttpUtil::IsValidHeaderValue(range_str)) | 82 if (!net::HttpUtil::IsValidHeaderValue(range_str)) |
(...skipping 22 matching lines...) Expand all Loading... |
102 // be the last call to Read. | 105 // be the last call to Read. |
103 bool using_populated_read = read_lengths_.size() > 0; | 106 bool using_populated_read = read_lengths_.size() > 0; |
104 size_t read_size = 1; | 107 size_t read_size = 1; |
105 if (using_populated_read) { | 108 if (using_populated_read) { |
106 read_size = read_lengths_.back(); | 109 read_size = read_lengths_.back(); |
107 read_lengths_.pop_back(); | 110 read_lengths_.pop_back(); |
108 } | 111 } |
109 | 112 |
110 int bytes_read = 0; | 113 int bytes_read = 0; |
111 sync = request->Read(buf_.get(), read_size, &bytes_read); | 114 sync = request->Read(buf_.get(), read_size, &bytes_read); |
112 // No more populated reads implies !bytes_read. | |
113 DCHECK(using_populated_read || !bytes_read); | |
114 } while (sync); | 115 } while (sync); |
115 | 116 |
116 if (!request->status().is_io_pending()) | 117 if (!request->status().is_io_pending()) |
117 QuitLoop(); | 118 QuitLoop(); |
118 } | 119 } |
119 | 120 |
120 // net::URLRequest::Delegate: | 121 // net::URLRequest::Delegate: |
121 void OnReceivedRedirect(net::URLRequest* request, | 122 void OnReceivedRedirect(net::URLRequest* request, |
122 const net::RedirectInfo& redirect_info, | 123 const net::RedirectInfo& redirect_info, |
123 bool* defer_redirect) override {} | 124 bool* defer_redirect) override {} |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 base::RunLoop* read_loop_; | 162 base::RunLoop* read_loop_; |
162 | 163 |
163 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness); | 164 DISALLOW_COPY_AND_ASSIGN(URLRequestDataJobFuzzerHarness); |
164 }; | 165 }; |
165 | 166 |
166 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | 167 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
167 // Using a static singleton test harness lets the test run ~3-4x faster. | 168 // Using a static singleton test harness lets the test run ~3-4x faster. |
168 return URLRequestDataJobFuzzerHarness::GetInstance() | 169 return URLRequestDataJobFuzzerHarness::GetInstance() |
169 ->CreateAndReadFromDataURLRequest(data, size); | 170 ->CreateAndReadFromDataURLRequest(data, size); |
170 } | 171 } |
OLD | NEW |