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 "net/http/http_stream_parser.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <algorithm> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/message_loop/message_loop.h" | |
| 19 #include "base/numerics/safe_conversions.h" | |
| 20 #include "net/base/address_list.h" | |
| 21 #include "net/base/io_buffer.h" | |
| 22 #include "net/base/net_errors.h" | |
| 23 #include "net/base/test_completion_callback.h" | |
| 24 #include "net/http/http_request_headers.h" | |
| 25 #include "net/http/http_request_info.h" | |
| 26 #include "net/http/http_response_info.h" | |
| 27 #include "net/log/net_log.h" | |
| 28 #include "net/log/test_net_log.h" | |
| 29 #include "net/socket/client_socket_handle.h" | |
| 30 #include "net/socket/socket_test_util.h" | |
| 31 #include "url/gurl.h" | |
| 32 | |
| 33 // Fuzzer for HttpStreamParser. | |
| 34 // | |
| 35 // |data| is the data received over a mock HTTP connection through one or more | |
| 36 // reads, along with metadata about the size of each read, and whether or not | |
| 37 // the read completely synchronously. | |
| 38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { | |
| 39 // Needed for thread checks and waits. | |
| 40 base::MessageLoopForIO message_loop; | |
| 41 | |
| 42 net::MockWrite writes[] = { | |
|
eroman
2016/03/28 22:54:57
Consider extracting this to a constant or function
mmenke
2016/03/29 16:38:37
Note that we're actually calling the initializer h
mmenke
2016/03/29 16:40:52
Oops...SequencedSocketData doesn't like const argu
| |
| 43 net::MockWrite(net::ASYNC, 0, "GET / HTTP/1.1\r\n\r\n"), | |
| 44 }; | |
| 45 | |
| 46 // Break the buffer into a sequence of variable sized sync and async | |
| 47 // reads. Use the last bytes of |data| exclusively for determining | |
| 48 // the size and type of each read. | |
| 49 std::vector<net::MockRead> reads; | |
| 50 // Sequence number for socket operations. | |
| 51 int last_sequence_number = 0; | |
| 52 | |
| 53 // IoMode for the final read, where the server closes the mock socket. | |
| 54 net::IoMode close_socket_io_mode = net::ASYNC; | |
| 55 | |
| 56 // Break |data| up into reads. The test may or may not make to the final | |
| 57 // read, where the server closes the socket. | |
| 58 | |
| 59 // Each read needs a one byte seed to determine read size and whether it | |
| 60 // should be sync or async, so if there's only one byte left unused, can't use | |
| 61 // it here. The bytes used to get this metadata are not used as over-the-wire | |
| 62 // bytes. | |
| 63 while (size > 0) { | |
| 64 size_t read_seed = data[size - 1]; | |
|
eroman
2016/03/28 22:54:57
Why size_t rather than uint8_t ? Is this so it pla
mmenke
2016/03/29 16:38:37
That's exactly it. I prefer to avoid casts unless
| |
| 65 size--; | |
| 66 net::IoMode io_mode = net::ASYNC; | |
| 67 // Low order bit determines IoMode. | |
| 68 if (read_seed & 0x1) | |
| 69 io_mode = net::SYNCHRONOUS; | |
| 70 | |
| 71 // Use second bit determine if the last read, when the socket is closed, is | |
|
eroman
2016/03/28 22:54:57
bit determine --> bit to determine
mmenke
2016/03/29 16:38:37
Done.
| |
| 72 // schronous. Only the second bit of the last time this loop runs matters. | |
|
eroman
2016/03/28 22:54:57
sycronous --> synchronous
(and yes, misspelled t
mmenke
2016/03/29 16:38:37
Done (And fixed the next sentence as well)
| |
| 73 if (read_seed & 0x2) { | |
| 74 close_socket_io_mode = net::SYNCHRONOUS; | |
| 75 } else { | |
| 76 close_socket_io_mode = net::ASYNC; | |
| 77 } | |
| 78 | |
| 79 // If there are no more bytes in |data|, next read is the connection close. | |
| 80 if (size == 0) | |
| 81 break; | |
| 82 | |
| 83 read_seed >>= 2; | |
| 84 | |
| 85 // Last 6 bits determine how many bytes are returned by the read. | |
| 86 int read_size = static_cast<int>(std::min(1 + read_seed, size)); | |
| 87 reads.push_back(net::MockRead(io_mode, reinterpret_cast<const char*>(data), | |
| 88 read_size, ++last_sequence_number)); | |
| 89 | |
| 90 data += read_size; | |
| 91 size -= read_size; | |
| 92 } | |
| 93 | |
| 94 // Server closes the socket. | |
| 95 reads.push_back(net::MockRead(close_socket_io_mode, | |
| 96 net::ERR_CONNECTION_CLOSED, | |
| 97 ++last_sequence_number)); | |
| 98 net::SequencedSocketData socket_data(reads.data(), reads.size(), writes, | |
| 99 arraysize(writes)); | |
| 100 socket_data.set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK)); | |
| 101 | |
| 102 scoped_ptr<net::MockTCPClientSocket> socket( | |
| 103 new net::MockTCPClientSocket(net::AddressList(), nullptr, &socket_data)); | |
| 104 | |
| 105 net::TestCompletionCallback callback; | |
| 106 CHECK_EQ(net::OK, socket->Connect(callback.callback())); | |
| 107 | |
| 108 net::ClientSocketHandle socket_handle; | |
| 109 socket_handle.SetSocket(std::move(socket)); | |
| 110 | |
| 111 net::HttpRequestInfo request_info; | |
| 112 request_info.method = "GET"; | |
| 113 request_info.url = GURL("http://localhost/"); | |
| 114 | |
| 115 scoped_refptr<net::GrowableIOBuffer> read_buffer(new net::GrowableIOBuffer()); | |
| 116 // Use a NetLog that listens to events, to get coverage of logging | |
| 117 // callbacks. | |
| 118 net::BoundTestNetLog net_log; | |
| 119 net::HttpStreamParser parser(&socket_handle, &request_info, read_buffer.get(), | |
| 120 net_log.bound()); | |
| 121 | |
| 122 net::HttpResponseInfo response_info; | |
| 123 int result = | |
| 124 parser.SendRequest("GET / HTTP/1.1\r\n", net::HttpRequestHeaders(), | |
| 125 &response_info, callback.callback()); | |
| 126 CHECK_EQ(net::OK, callback.GetResult(result)); | |
| 127 | |
| 128 result = parser.ReadResponseHeaders(callback.callback()); | |
| 129 result = callback.GetResult(result); | |
| 130 | |
| 131 if (result != net::OK) | |
| 132 return 0; | |
| 133 | |
| 134 while (true) { | |
| 135 // 64 exactly matches the maximum amount of data returned by a single | |
| 136 // MockRead, as created above. | |
| 137 scoped_refptr<net::IOBufferWithSize> io_buffer( | |
| 138 new net::IOBufferWithSize(64)); | |
| 139 result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(), | |
| 140 callback.callback()); | |
| 141 | |
| 142 // Releasing the pointer to IOBuffer immediately is more likely to lead to a | |
| 143 // use-after-free. | |
| 144 io_buffer = nullptr; | |
| 145 | |
| 146 if (callback.GetResult(result) <= 0) | |
| 147 break; | |
| 148 } | |
| 149 | |
| 150 return 0; | |
| 151 } | |
| OLD | NEW |