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

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: Add comment 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 // Entry point for LibFuzzer.
34 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
35 // Needed for thread checks and waits.
36 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
37
38 net::MockWrite writes[] = {
39 net::MockWrite(net::ASYNC, 0, "GET / HTTP/1.1\r\n\r\n"),
40 };
41
42 // 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
43 // reads. Use the last bytes of |data| exclusively for determining
44 // 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.
45 std::vector<net::MockRead> reads;
46 // Sequence number for socket operations.
47 int last_sequence_number = 0;
48 // Each read needs a one byte seed to determine read size and whether it
49 // should be sync or async, so if there's only one byte left unused, can't use
50 // it here.
51 while (size > 1) {
52 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
53 size--;
54 net::IoMode io_mode = net::ASYNC;
55 // 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.
56 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
57 io_mode = net::SYNCHRONOUS;
58
59 // 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.
60 int read_size = static_cast<int>(std::min(1 + (0x3F & read_seed), size));
61 reads.push_back(net::MockRead(io_mode, reinterpret_cast<const char*>(data),
62 read_size, ++last_sequence_number));
63
64 data += read_size;
65 size -= read_size;
66 }
67
68 // Use whether or not there's a left over byte to determine IoMode of final
69 // operation.
70 net::IoMode io_mode = net::ASYNC;
71 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
72 io_mode = net::SYNCHRONOUS;
73 reads.push_back(net::MockRead(io_mode, net::ERR_CONNECTION_CLOSED,
74 ++last_sequence_number));
75 net::SequencedSocketData socket_data(reads.data(), reads.size(), writes,
76 arraysize(writes));
77 socket_data.set_connect_data(net::MockConnect(net::SYNCHRONOUS, net::OK));
78
79 scoped_ptr<net::MockTCPClientSocket> socket(
80 new net::MockTCPClientSocket(net::AddressList(), nullptr, &socket_data));
81
82 net::TestCompletionCallback callback;
83 CHECK_EQ(net::OK, socket->Connect(callback.callback()));
84
85 net::ClientSocketHandle socket_handle;
86 socket_handle.SetSocket(std::move(socket));
87
88 net::HttpRequestInfo request_info;
89 request_info.method = "GET";
90 request_info.url = GURL("http://localhost/");
91
92 scoped_refptr<net::GrowableIOBuffer> read_buffer(new net::GrowableIOBuffer());
93 // Use a NetLog that listens to events, to get coverage of logging
94 // callbacks.
95 net::BoundTestNetLog net_log;
96 net::HttpStreamParser parser(&socket_handle, &request_info, read_buffer.get(),
97 net_log.bound());
98
99 net::HttpResponseInfo response_info;
100 int result =
101 parser.SendRequest("GET / HTTP/1.1\r\n", net::HttpRequestHeaders(),
102 &response_info, callback.callback());
103 CHECK_EQ(net::OK, callback.GetResult(result));
104
105 result = parser.ReadResponseHeaders(callback.callback());
106 result = callback.GetResult(result);
107
108 if (result != net::OK)
109 return 0;
110
111 while (true) {
112 // 64 exactly matches the maximum amount of data returned by a single
113 // MockRead, as created above.
114 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
115 new net::IOBufferWithSize(64));
116 result = parser.ReadResponseBody(io_buffer.get(), io_buffer->size(),
117 callback.callback());
118 if (callback.GetResult(result) <= 0)
119 break;
120 }
121
122 return 0;
123 }
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