Chromium Code Reviews| Index: net/http/http_stream_parser_fuzzer.cc |
| diff --git a/net/http/http_stream_parser_fuzzer.cc b/net/http/http_stream_parser_fuzzer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7e539efd64e8cd9bef4a7c82cce740571dcdb757 |
| --- /dev/null |
| +++ b/net/http/http_stream_parser_fuzzer.cc |
| @@ -0,0 +1,123 @@ |
| +// 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/http/http_stream_parser.h" |
| + |
| +#include <stddef.h> |
| +#include <stdint.h> |
| + |
| +#include <algorithm> |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/numerics/safe_conversions.h" |
| +#include "net/base/address_list.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/base/test_completion_callback.h" |
| +#include "net/http/http_request_headers.h" |
| +#include "net/http/http_request_info.h" |
| +#include "net/http/http_response_info.h" |
| +#include "net/log/net_log.h" |
| +#include "net/log/test_net_log.h" |
| +#include "net/socket/client_socket_handle.h" |
| +#include "net/socket/socket_test_util.h" |
| +#include "url/gurl.h" |
| + |
| +// Entry point for LibFuzzer. |
| +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
|
eroman
2016/03/26 01:39:29
It would be helpful to have a high-level overview
mmenke
2016/03/28 15:26:26
Added a short comment. Happy to add more, just no
|
| + // Needed for thread checks and waits. |
| + base::MessageLoopForIO message_loop; |
|
eroman
2016/03/26 01:39:28
I wonder if it would be faster to extract the mess
mmenke
2016/03/28 15:26:26
I think MessageLoops aren't too heavy weight? Onl
|
| + |
| + net::MockWrite writes[] = { |
| + net::MockWrite(net::ASYNC, 0, "GET / HTTP/1.1\r\n\r\n"), |
| + }; |
| + |
| + // Break the buffer into a sequence of variable sized sync and async |
|
eroman
2016/03/28 22:54:57
optional nit:
"sync and async reads of various si
|
| + // reads. Use the last bytes of |data| exclusively for determining |
| + // the size of type of each read. |
|
eroman
2016/03/26 01:39:28
"size of type" ==> "size and type"
mmenke
2016/03/28 15:26:26
Done.
|
| + std::vector<net::MockRead> reads; |
| + // Sequence number for socket operations. |
| + int last_sequence_number = 0; |
| + // Each read needs a one byte seed to determine read size and whether it |
| + // should be sync or async, so if there's only one byte left unused, can't use |
| + // it here. |
| + while (size > 1) { |
| + size_t read_seed = data[size - 1]; |
|
eroman
2016/03/26 01:39:29
Max can advise on what works best for the fuzzer.
mmenke
2016/03/28 15:26:26
You're misunderstanding the code. This code uses
eroman
2016/03/28 22:54:57
I see, I had indeed read the code wrong.
The new
|
| + size--; |
| + net::IoMode io_mode = net::ASYNC; |
| + // High order byte determines IoMode. |
|
eroman
2016/03/26 01:39:29
"high order byte" ==> "high order bit"
mmenke
2016/03/28 15:26:26
Done.
|
| + if (read_seed & 0x80) |
|
eroman
2016/03/26 01:39:28
Just thinking out loud, but if we are generating l
mmenke
2016/03/28 15:26:26
Done. Since these bytes are not used as part of t
|
| + io_mode = net::SYNCHRONOUS; |
| + |
| + // Low order 6 bytes determine how many bytes are returned by the read. |
|
eroman
2016/03/26 01:39:29
bytes ==> bits
mmenke
2016/03/28 15:26:26
Done.
|
| + int read_size = static_cast<int>(std::min(1 + (0x3F & read_seed), size)); |
| + reads.push_back(net::MockRead(io_mode, reinterpret_cast<const char*>(data), |
| + read_size, ++last_sequence_number)); |
| + |
| + data += read_size; |
| + size -= read_size; |
| + } |
| + |
| + // Use whether or not there's a left over byte to determine IoMode of final |
| + // operation. |
| + net::IoMode io_mode = net::ASYNC; |
| + if (size > 0) |
|
eroman
2016/03/26 01:39:28
Isn't |size| going to be 0 after the while-loop ab
mmenke
2016/03/28 15:26:26
data[0] here is almost always 'H', and the last by
mmenke
2016/03/28 22:42:50
Erm...that "special properties / I guess not" was
|
| + io_mode = net::SYNCHRONOUS; |
| + reads.push_back(net::MockRead(io_mode, net::ERR_CONNECTION_CLOSED, |
| + ++last_sequence_number)); |
| + net::SequencedSocketData socket_data(reads.data(), reads.size(), writes, |
| + arraysize(writes)); |
| + socket_data.set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK)); |
| + |
| + scoped_ptr<net::MockTCPClientSocket> socket( |
| + new net::MockTCPClientSocket(net::AddressList(), nullptr, &socket_data)); |
| + |
| + net::TestCompletionCallback callback; |
| + CHECK_EQ(net::OK, socket->Connect(callback.callback())); |
| + |
| + net::ClientSocketHandle socket_handle; |
| + socket_handle.SetSocket(std::move(socket)); |
| + |
| + net::HttpRequestInfo request_info; |
| + request_info.method = "GET"; |
| + request_info.url = GURL("http://localhost/"); |
| + |
| + scoped_refptr<net::GrowableIOBuffer> read_buffer(new net::GrowableIOBuffer()); |
| + // Use a NetLog that listens to events, to get coverage of logging |
| + // callbacks. |
| + net::BoundTestNetLog net_log; |
| + net::HttpStreamParser parser(&socket_handle, &request_info, read_buffer.get(), |
| + net_log.bound()); |
| + |
| + net::HttpResponseInfo response_info; |
| + int result = |
| + parser.SendRequest("GET / HTTP/1.1\r\n", net::HttpRequestHeaders(), |
| + &response_info, callback.callback()); |
| + CHECK_EQ(net::OK, callback.GetResult(result)); |
| + |
| + result = parser.ReadResponseHeaders(callback.callback()); |
| + result = callback.GetResult(result); |
| + |
| + if (result != net::OK) |
| + return 0; |
| + |
| + while (true) { |
| + // 64 exactly matches the maximum amount of data returned by a single |
| + // MockRead, as created above. |
| + scoped_refptr<net::IOBufferWithSize> io_buffer( |
|
eroman
2016/03/26 01:39:29
Would it make sense to allocate this once outside
mmenke
2016/03/28 15:26:26
Freeing it is more likely to catch a use-after-fre
|
| + new net::IOBufferWithSize(64)); |
| + result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(), |
| + callback.callback()); |
| + if (callback.GetResult(result) <= 0) |
| + break; |
| + } |
| + |
| + return 0; |
| +} |