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

Side by Side Diff: net/http/http_stream_parser_fuzzer.cc

Issue 1836573002: Add an HttpStreamParser fuzzer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Oops Created 4 years, 8 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/BUILD.gn ('k') | no next file » | 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 "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[] = {
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];
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 to determine if the last read, when the socket is closed,
72 // is synchronous. The second bit only matters the last time this loop is
73 // runs.
74 if (read_seed & 0x2) {
75 close_socket_io_mode = net::SYNCHRONOUS;
76 } else {
77 close_socket_io_mode = net::ASYNC;
78 }
79
80 // If there are no more bytes in |data|, next read is the connection close.
81 if (size == 0)
82 break;
83
84 read_seed >>= 2;
85
86 // Last 6 bits determine how many bytes are returned by the read.
87 int read_size = static_cast<int>(std::min(1 + read_seed, size));
88 reads.push_back(net::MockRead(io_mode, reinterpret_cast<const char*>(data),
89 read_size, ++last_sequence_number));
90
91 data += read_size;
92 size -= read_size;
93 }
94
95 // Server closes the socket.
96 reads.push_back(net::MockRead(close_socket_io_mode,
97 net::ERR_CONNECTION_CLOSED,
98 ++last_sequence_number));
99 net::SequencedSocketData socket_data(reads.data(), reads.size(), writes,
100 arraysize(writes));
101 socket_data.set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK));
102
103 scoped_ptr<net::MockTCPClientSocket> socket(
104 new net::MockTCPClientSocket(net::AddressList(), nullptr, &socket_data));
105
106 net::TestCompletionCallback callback;
107 CHECK_EQ(net::OK, socket->Connect(callback.callback()));
108
109 net::ClientSocketHandle socket_handle;
110 socket_handle.SetSocket(std::move(socket));
111
112 net::HttpRequestInfo request_info;
113 request_info.method = "GET";
114 request_info.url = GURL("http://localhost/");
115
116 scoped_refptr<net::GrowableIOBuffer> read_buffer(new net::GrowableIOBuffer());
117 // Use a NetLog that listens to events, to get coverage of logging
118 // callbacks.
119 net::BoundTestNetLog net_log;
120 net::HttpStreamParser parser(&socket_handle, &request_info, read_buffer.get(),
121 net_log.bound());
122
123 net::HttpResponseInfo response_info;
124 int result =
125 parser.SendRequest("GET / HTTP/1.1\r\n", net::HttpRequestHeaders(),
126 &response_info, callback.callback());
127 CHECK_EQ(net::OK, callback.GetResult(result));
128
129 result = parser.ReadResponseHeaders(callback.callback());
130 result = callback.GetResult(result);
131
132 if (result != net::OK)
133 return 0;
134
135 while (true) {
136 // 64 exactly matches the maximum amount of data returned by a single
137 // MockRead, as created above.
138 scoped_refptr<net::IOBufferWithSize> io_buffer(
139 new net::IOBufferWithSize(64));
140 result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(),
141 callback.callback());
142
143 // Releasing the pointer to IOBuffer immediately is more likely to lead to a
144 // use-after-free.
145 io_buffer = nullptr;
146
147 if (callback.GetResult(result) <= 0)
148 break;
149 }
150
151 return 0;
152 }
OLDNEW
« no previous file with comments | « net/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698