/src/curl_fuzzer/proto_fuzzer/multi_transfer_runner.cc
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al. |
3 | | * |
4 | | * SPDX-License-Identifier: curl |
5 | | */ |
6 | | |
7 | | /// @file |
8 | | /// @brief Implementation of the bounded concurrent easy-handle runner. |
9 | | |
10 | | #include "proto_fuzzer/multi_transfer_runner.h" |
11 | | |
12 | | #define CURL_ALLOW_OLD_MULTI_SOCKET |
13 | | #include <curl/curl.h> |
14 | | #include <curl/multi.h> |
15 | | |
16 | | #include <algorithm> |
17 | | #include <array> |
18 | | #include <cstddef> |
19 | | #include <cstdio> |
20 | | #include <memory> |
21 | | #include <string> |
22 | | |
23 | | #include "proto_fuzzer/curl_raii.h" |
24 | | #include "proto_fuzzer/mock_server.h" |
25 | | #include "proto_fuzzer/multi_socket_driver.h" |
26 | | #include "proto_fuzzer/option_apply.h" |
27 | | #include "proto_fuzzer/request_data.h" |
28 | | #include "proto_fuzzer/scenario_limits.h" |
29 | | #include "proto_fuzzer/transfer_session.h" |
30 | | |
31 | | namespace proto_fuzzer { |
32 | | |
33 | | namespace { |
34 | | |
35 | | /// Track shared-multi membership separately from each transfer's resources. |
36 | | struct TransferState { |
37 | | TransferSession session; |
38 | | bool attached = false; |
39 | | }; |
40 | | |
41 | | constexpr int kMaxDriveIterations = 512; |
42 | | constexpr int kMaxIdleIterations = 8; |
43 | | constexpr curl_off_t kMaxDecodedResponseBytes = 1024 * 1024; |
44 | | constexpr curl_off_t kStateProbeReceiveRate = 1; |
45 | | constexpr long kStateProbeTimeoutMs = 20; |
46 | | |
47 | | struct FileCloser { |
48 | 1.49k | void operator()(FILE* file) const { |
49 | 1.49k | if (file != nullptr) { |
50 | 1.49k | std::fclose(file); |
51 | 1.49k | } |
52 | 1.49k | } |
53 | | }; |
54 | | |
55 | | using FilePtr = std::unique_ptr<FILE, FileCloser>; |
56 | | |
57 | | /// Install the one intentionally slow transfer shape without exposing its |
58 | | /// values through generic SetOption mutations. The anonymous stream is owned |
59 | | /// by RunMultiTransferScenario and outlives every configured easy handle. |
60 | 79.1k | bool ApplyStateProbeOptions(CURL* easy, curl::fuzzer::proto::MultiStateProbeProfile profile, FILE* progress_sink) { |
61 | 79.1k | if (profile != curl::fuzzer::proto::MULTI_STATE_PROBE_PROGRESS_RATE_LIMIT || progress_sink == nullptr) { |
62 | 74.0k | return false; |
63 | 74.0k | } |
64 | 5.03k | return curl_easy_setopt(easy, CURLOPT_STDERR, progress_sink) == CURLE_OK && |
65 | 5.03k | curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, kStateProbeTimeoutMs) == CURLE_OK && |
66 | 5.03k | curl_easy_setopt(easy, CURLOPT_NOPROGRESS, 0L) == CURLE_OK && |
67 | 5.03k | curl_easy_setopt(easy, CURLOPT_MAX_RECV_SPEED_LARGE, kStateProbeReceiveRate) == CURLE_OK; |
68 | 79.1k | } |
69 | | |
70 | 26.1k | std::size_t RuntimeTransferCount(const curl::fuzzer::proto::MultiPlan& plan) { |
71 | 26.1k | return std::max(scenario_limits::kMinMultiTransfers, |
72 | 26.1k | std::min(static_cast<std::size_t>(plan.transfer_count()), scenario_limits::kMaxMultiTransfers)); |
73 | 26.1k | } |
74 | | |
75 | 108k | void RecordNotification(CURLM*, unsigned int notification, CURL*, void* userdata) { |
76 | 108k | auto* stats = static_cast<MultiTransferRunStats*>(userdata); |
77 | 108k | if (stats == nullptr) { |
78 | 0 | return; |
79 | 0 | } |
80 | 108k | if (notification == CURLMNOTIFY_INFO_READ) { |
81 | 34.7k | ++stats->info_read_notifications; |
82 | 73.8k | } else if (notification == CURLMNOTIFY_EASY_DONE) { |
83 | 73.8k | ++stats->easy_done_notifications; |
84 | 73.8k | } |
85 | 108k | } |
86 | | |
87 | | /// Group the operations that share one multi and its bounded transfer set. |
88 | | /// RunMultiTransferScenario owns every borrowed object and performs cleanup; |
89 | | /// the driver never destroys or detaches a transfer when its scope ends. |
90 | | class MultiTransferDriver { |
91 | | public: |
92 | | MultiTransferDriver(CURLM* multi, std::array<TransferState, scenario_limits::kMaxMultiTransfers>& transfers, |
93 | | std::size_t transfer_count, MultiTransferRunStats& stats) |
94 | 26.0k | : multi_(multi), transfers_(transfers), transfer_count_(transfer_count), stats_(stats) {} |
95 | | |
96 | | /// Consume every queued completion before handle removal can discard it. |
97 | | /// Re-added handles may complete more than once, so count messages rather |
98 | | /// than only distinct easy pointers. |
99 | 264k | bool DrainCompletionMessages() { |
100 | 264k | bool consumed = false; |
101 | 264k | int messages_remaining = 0; |
102 | 264k | CURLMsg* message = nullptr; |
103 | 337k | while ((message = curl_multi_info_read(multi_, &messages_remaining)) != nullptr) { |
104 | 73.3k | consumed = true; |
105 | 73.3k | if (message->msg != CURLMSG_DONE) { |
106 | 0 | continue; |
107 | 0 | } |
108 | 158k | for (std::size_t index = 0; index < transfer_count_; ++index) { |
109 | 158k | TransferState& transfer = transfers_[index]; |
110 | 158k | if (transfer.session.easy() == message->easy_handle) { |
111 | 73.3k | ++stats_.completion_messages; |
112 | 73.3k | if (message->data.result == CURLE_OK) { |
113 | 2.26k | ++stats_.successful_completions; |
114 | 71.1k | } else if (message->data.result == CURLE_FILESIZE_EXCEEDED) { |
115 | 190 | ++stats_.response_limit_completions; |
116 | 190 | } |
117 | 73.3k | break; |
118 | 73.3k | } |
119 | 158k | } |
120 | 73.3k | } |
121 | 264k | return consumed; |
122 | 264k | } |
123 | | |
124 | | /// Apply one schema-safe lifecycle transition. Return true only when |
125 | | /// libcurl accepted a state change; the caller tracks action consumption |
126 | | /// separately so later ordered actions remain reachable. |
127 | 24.6k | bool ApplyAction(const curl::fuzzer::proto::MultiAction& action) { |
128 | 24.6k | if (transfer_count_ == 0) { |
129 | 0 | return false; |
130 | 0 | } |
131 | 24.6k | if (action.kind() == curl::fuzzer::proto::MULTI_ACTION_NETWORK_CHANGED) { |
132 | | // A network change applies to the shared multi, not a selected easy. |
133 | | // Invalidate active and idle connections as well as cached DNS results. |
134 | 391 | return curl_multi_setopt(multi_, CURLMOPT_NETWORK_CHANGED, CURLMNWC_CLEAR_ALL) == CURLM_OK; |
135 | 391 | } |
136 | 24.2k | TransferState& transfer = transfers_[static_cast<std::size_t>(action.transfer_selector()) % transfer_count_]; |
137 | 24.2k | CURL* easy = transfer.session.easy(); |
138 | 24.2k | if (easy == nullptr) { |
139 | 88 | return false; |
140 | 88 | } |
141 | | |
142 | 24.1k | switch (action.kind()) { |
143 | 1.09k | case curl::fuzzer::proto::MULTI_ACTION_PAUSE_RECV: |
144 | 1.09k | return curl_easy_pause(easy, CURLPAUSE_RECV) == CURLE_OK; |
145 | 698 | case curl::fuzzer::proto::MULTI_ACTION_PAUSE_SEND: |
146 | 698 | return curl_easy_pause(easy, CURLPAUSE_SEND) == CURLE_OK; |
147 | 924 | case curl::fuzzer::proto::MULTI_ACTION_PAUSE_ALL: |
148 | 924 | return curl_easy_pause(easy, CURLPAUSE_ALL) == CURLE_OK; |
149 | 519 | case curl::fuzzer::proto::MULTI_ACTION_RESUME: |
150 | 519 | return curl_easy_pause(easy, CURLPAUSE_CONT) == CURLE_OK; |
151 | 7.19k | case curl::fuzzer::proto::MULTI_ACTION_REMOVE: |
152 | 7.19k | if (transfer.attached && curl_multi_remove_handle(multi_, easy) == CURLM_OK) { |
153 | 6.77k | transfer.attached = false; |
154 | 6.77k | return true; |
155 | 6.77k | } |
156 | 421 | return false; |
157 | 7.24k | case curl::fuzzer::proto::MULTI_ACTION_READD: |
158 | 7.24k | if (!transfer.attached && curl_multi_add_handle(multi_, easy) == CURLM_OK) { |
159 | 6.28k | transfer.attached = true; |
160 | 6.28k | return true; |
161 | 6.28k | } |
162 | 961 | return false; |
163 | 6.45k | case curl::fuzzer::proto::MULTI_ACTION_NONE: |
164 | 6.45k | case curl::fuzzer::proto::MULTI_ACTION_NETWORK_CHANGED: |
165 | 6.45k | default: |
166 | 6.45k | return false; |
167 | 24.1k | } |
168 | 24.1k | } |
169 | | |
170 | | private: |
171 | | CURLM* multi_; |
172 | | std::array<TransferState, scenario_limits::kMaxMultiTransfers>& transfers_; |
173 | | const std::size_t transfer_count_; |
174 | | MultiTransferRunStats& stats_; |
175 | | }; |
176 | | |
177 | | /// Cover the public wait/poll/fdset queries from a valid shared-multi state |
178 | | /// without introducing wall-clock delay. |
179 | 26.0k | void ProbeMultiWaitApis(CURLM* multi) { |
180 | 26.0k | int numfds = 0; |
181 | 26.0k | (void)curl_multi_poll(multi, nullptr, 0, 0, &numfds); |
182 | 26.0k | (void)curl_multi_wait(multi, nullptr, 0, 0, &numfds); |
183 | | |
184 | 26.0k | unsigned int waitfd_count = 0; |
185 | 26.0k | (void)curl_multi_waitfds(multi, nullptr, 0, &waitfd_count); |
186 | 26.0k | std::array<curl_waitfd, scenario_limits::kMaxMultiTransfers * 2> waitfds{}; |
187 | 26.0k | (void)curl_multi_waitfds(multi, waitfds.data(), static_cast<unsigned int>(waitfds.size()), &waitfd_count); |
188 | | |
189 | 26.0k | fd_set readfds; |
190 | 26.0k | fd_set writefds; |
191 | 26.0k | fd_set exceptfds; |
192 | 26.0k | FD_ZERO(&readfds); |
193 | 26.0k | FD_ZERO(&writefds); |
194 | 26.0k | FD_ZERO(&exceptfds); |
195 | 26.0k | int maxfd = -1; |
196 | 26.0k | (void)curl_multi_fdset(multi, &readfds, &writefds, &exceptfds, &maxfd); |
197 | 26.0k | long timeout_ms = -1; |
198 | 26.0k | (void)curl_multi_timeout(multi, &timeout_ms); |
199 | | |
200 | 26.0k | CURL** handles = curl_multi_get_handles(multi); |
201 | 26.0k | curl_free(handles); |
202 | | |
203 | 26.0k | constexpr CURLMinfo_offt kOffsetInfo[] = { |
204 | 26.0k | CURLMINFO_XFERS_CURRENT, CURLMINFO_XFERS_RUNNING, CURLMINFO_XFERS_PENDING, |
205 | 26.0k | CURLMINFO_XFERS_DONE, CURLMINFO_XFERS_ADDED, |
206 | 26.0k | }; |
207 | 130k | for (const CURLMinfo_offt info : kOffsetInfo) { |
208 | 130k | curl_off_t value = 0; |
209 | 130k | (void)curl_multi_get_offt(multi, info, &value); |
210 | 130k | } |
211 | 26.0k | } |
212 | | |
213 | | /// Exercise the two deprecated public entrypoints as real symbols rather than |
214 | | /// curl_multi_socket's compatibility macro. Their output is deliberately not |
215 | | /// used to select the primary runner: either call may advance a transfer, and |
216 | | /// the normal bounded loop consumes whatever state remains. |
217 | 26.0k | void ProbeLegacyMultiSocketApis(CURLM* multi) { |
218 | 26.0k | int running_handles = 0; |
219 | 26.0k | #if defined(__GNUC__) |
220 | 26.0k | #pragma GCC diagnostic push |
221 | 26.0k | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
222 | 26.0k | #endif |
223 | 26.0k | (void)curl_multi_socket(multi, CURL_SOCKET_TIMEOUT, &running_handles); |
224 | 26.0k | (void)curl_multi_socket_all(multi, &running_handles); |
225 | 26.0k | #if defined(__GNUC__) |
226 | 26.0k | #pragma GCC diagnostic pop |
227 | 26.0k | #endif |
228 | 26.0k | } |
229 | | |
230 | | } // namespace |
231 | | |
232 | | /// Drive two to four easy handles through one CURLM and one bounded in-process |
233 | | /// HTTP peer. Runtime bounds repeat the mutation policy's limits so direct |
234 | | /// unit-test and standalone calls cannot make work proportional to unchecked |
235 | | /// input. |
236 | | /// @param scenario HTTP transfer shape and concurrent lifecycle plan. |
237 | | /// @return Bounded setup, action, and completion counts. |
238 | 26.1k | MultiTransferRunStats RunMultiTransferScenario(const curl::fuzzer::proto::Scenario& scenario) { |
239 | 26.1k | MultiTransferRunStats stats; |
240 | 26.1k | const auto& plan = scenario.multi_plan(); |
241 | 26.1k | const std::size_t transfer_count = RuntimeTransferCount(plan); |
242 | 26.1k | stats.configured_handles = transfer_count; |
243 | | |
244 | 26.1k | FilePtr progress_sink; |
245 | 26.1k | if (plan.state_probe_profile() == curl::fuzzer::proto::MULTI_STATE_PROBE_PROGRESS_RATE_LIMIT) { |
246 | 1.49k | progress_sink.reset(std::tmpfile()); |
247 | 1.49k | } |
248 | | |
249 | | // The peer and socket callback state must outlive multi cleanup, which can |
250 | | // synchronously announce socket removals and close cached connections. |
251 | 26.1k | MockServer mock; |
252 | 26.1k | mock.SetScripts(scenario); |
253 | 26.1k | mock.SetKeepConnectionsOpen(plan.keep_connections_open()); |
254 | 26.1k | MultiSocketDriver socket_driver; |
255 | 26.1k | CurlMultiPtr multi(curl_multi_init()); |
256 | 26.1k | if (!multi) { |
257 | 73 | return stats; |
258 | 73 | } |
259 | | |
260 | 26.0k | const long max_host_connections = |
261 | 26.0k | static_cast<long>(std::min<std::size_t>(plan.max_host_connections(), transfer_count)); |
262 | 26.0k | const long max_total_connections = |
263 | 26.0k | static_cast<long>(std::min<std::size_t>(plan.max_total_connections(), transfer_count)); |
264 | 26.0k | const long cache_size = |
265 | 26.0k | static_cast<long>(std::min<std::size_t>(plan.connection_cache_size(), scenario_limits::kMaxMultiTransfers * 2)); |
266 | 26.0k | (void)curl_multi_setopt(multi.get(), CURLMOPT_MAX_HOST_CONNECTIONS, max_host_connections); |
267 | 26.0k | (void)curl_multi_setopt(multi.get(), CURLMOPT_MAX_TOTAL_CONNECTIONS, max_total_connections); |
268 | 26.0k | (void)curl_multi_setopt(multi.get(), CURLMOPT_MAXCONNECTS, cache_size); |
269 | 26.0k | (void)curl_multi_setopt(multi.get(), CURLMOPT_PIPELINING, |
270 | 26.0k | plan.multiplex() ? static_cast<long>(CURLPIPE_MULTIPLEX) : 0L); |
271 | 26.0k | (void)curl_multi_setopt(multi.get(), CURLMOPT_NOTIFYFUNCTION, &RecordNotification); |
272 | 26.0k | (void)curl_multi_setopt(multi.get(), CURLMOPT_NOTIFYDATA, &stats); |
273 | 26.0k | (void)curl_multi_notify_enable(multi.get(), CURLMNOTIFY_INFO_READ); |
274 | 26.0k | (void)curl_multi_notify_enable(multi.get(), CURLMNOTIFY_EASY_DONE); |
275 | 26.0k | (void)curl_multi_notify_disable(multi.get(), CURLMNOTIFY_INFO_READ); |
276 | 26.0k | (void)curl_multi_notify_enable(multi.get(), CURLMNOTIFY_INFO_READ); |
277 | 26.0k | (void)curl_multi_notify_disable(multi.get(), CURLMNOTIFY_EASY_DONE); |
278 | 26.0k | (void)curl_multi_notify_enable(multi.get(), CURLMNOTIFY_EASY_DONE); |
279 | | |
280 | 26.0k | const bool socket_mode = plan.drive_mode() == curl::fuzzer::proto::MULTI_DRIVE_SOCKET; |
281 | 26.0k | const bool socket_driver_installed = socket_mode && socket_driver.Install(multi.get()); |
282 | 26.0k | std::array<TransferState, scenario_limits::kMaxMultiTransfers> transfers; |
283 | 26.0k | const std::string url = "http://" + scenario.host_path(); |
284 | 105k | for (std::size_t index = 0; index < transfer_count; ++index) { |
285 | 79.7k | TransferState& transfer = transfers[index]; |
286 | 79.7k | if (!transfer.session.Initialize()) { |
287 | 595 | continue; |
288 | 595 | } |
289 | 79.1k | transfer.session.ApplyBaseline(scenario); |
290 | 79.1k | (void)curl_easy_setopt(transfer.session.easy(), CURLOPT_URL, url.c_str()); |
291 | 79.1k | mock.Install(transfer.session.easy()); |
292 | 79.1k | (void)ApplyScenarioOptions(transfer.session.easy(), scenario); |
293 | | // Apply this after the mutation-controlled options. A small compressed |
294 | | // response can otherwise expand into unbounded decoder work on every |
295 | | // concurrent handle, and an input MAXFILESIZE value could remove the cap. |
296 | 79.1k | (void)curl_easy_setopt(transfer.session.easy(), CURLOPT_MAXFILESIZE_LARGE, kMaxDecodedResponseBytes); |
297 | 79.1k | if (ApplyStateProbeOptions(transfer.session.easy(), plan.state_probe_profile(), progress_sink.get())) { |
298 | 5.03k | ++stats.state_probe_handles; |
299 | 5.03k | } |
300 | 79.1k | mock.ConfigureRequestData(transfer.session.InstallRequestData(scenario)); |
301 | 79.1k | if (curl_multi_add_handle(multi.get(), transfer.session.easy()) == CURLM_OK) { |
302 | 79.1k | transfer.attached = true; |
303 | 79.1k | ++stats.added_handles; |
304 | 79.1k | } |
305 | 79.1k | } |
306 | | |
307 | 26.0k | if (stats.added_handles == 0) { |
308 | 2 | multi.reset(); |
309 | 2 | return stats; |
310 | 2 | } |
311 | | |
312 | 26.0k | MultiTransferDriver driver(multi.get(), transfers, transfer_count, stats); |
313 | 26.0k | ProbeLegacyMultiSocketApis(multi.get()); |
314 | | |
315 | 26.0k | const std::size_t action_count = std::min<std::size_t>(plan.actions_size(), scenario_limits::kMaxMultiActions); |
316 | 26.0k | std::size_t next_action = 0; |
317 | 26.0k | bool initial_action_changed = false; |
318 | 26.0k | if (next_action < action_count) { |
319 | 3.56k | initial_action_changed = driver.ApplyAction(plan.actions(static_cast<int>(next_action++))); |
320 | 3.56k | ++stats.actions_consumed; |
321 | 3.56k | } |
322 | | |
323 | 26.0k | int running_handles = 0; |
324 | 26.0k | CURLMcode result = socket_driver_installed ? socket_driver.Start(&running_handles) |
325 | 26.0k | : curl_multi_perform(multi.get(), &running_handles); |
326 | 26.0k | bool completion_progress = driver.DrainCompletionMessages(); |
327 | 26.0k | ProbeMultiWaitApis(multi.get()); |
328 | 26.0k | if (plan.wake_multi()) { |
329 | 497 | if (socket_driver_installed) { |
330 | 140 | socket_driver.ProbeControlApis(); |
331 | 357 | } else { |
332 | 357 | (void)curl_multi_wakeup(multi.get()); |
333 | 357 | } |
334 | 497 | } |
335 | | |
336 | 26.0k | int idle_iterations = initial_action_changed || completion_progress ? 0 : 1; |
337 | 235k | for (int iteration = 0; result == CURLM_OK && iteration < kMaxDriveIterations; ++iteration) { |
338 | 234k | if (running_handles == 0 && next_action >= action_count) { |
339 | 22.7k | break; |
340 | 22.7k | } |
341 | | |
342 | 212k | bool made_progress = mock.ServiceConnections(); |
343 | 212k | if (next_action < action_count) { |
344 | 21.0k | made_progress = driver.ApplyAction(plan.actions(static_cast<int>(next_action++))) || made_progress; |
345 | 21.0k | ++stats.actions_consumed; |
346 | | // Consuming an ordered no-op is still harness progress: continue to the |
347 | | // next action instead of letting an idle transport hide its suffix. |
348 | 21.0k | made_progress = true; |
349 | 21.0k | } |
350 | | |
351 | 212k | const int running_before = running_handles; |
352 | 212k | if (socket_driver_installed) { |
353 | 133k | const MultiSocketDriver::DriveResult drive = socket_driver.DriveReady(&running_handles); |
354 | 133k | result = drive.code; |
355 | 133k | made_progress = drive.made_progress || made_progress; |
356 | 133k | } else { |
357 | 78.4k | result = curl_multi_perform(multi.get(), &running_handles); |
358 | 78.4k | made_progress = running_handles != running_before || made_progress; |
359 | 78.4k | } |
360 | 212k | made_progress = driver.DrainCompletionMessages() || made_progress; |
361 | | |
362 | 212k | if (made_progress) { |
363 | 187k | idle_iterations = 0; |
364 | 187k | } else if (++idle_iterations >= kMaxIdleIterations) { |
365 | 3.10k | break; |
366 | 3.10k | } |
367 | 212k | } |
368 | | |
369 | 26.0k | (void)driver.DrainCompletionMessages(); |
370 | 26.0k | stats.opened_connections = mock.opened_connection_count(); |
371 | 105k | for (std::size_t index = 0; index < transfer_count; ++index) { |
372 | 79.7k | TransferState& transfer = transfers[index]; |
373 | 79.7k | if (transfer.session.easy() == nullptr) { |
374 | 591 | continue; |
375 | 591 | } |
376 | 79.1k | (void)curl_easy_pause(transfer.session.easy(), CURLPAUSE_CONT); |
377 | 79.1k | if (transfer.attached) { |
378 | 78.6k | (void)curl_multi_remove_handle(multi.get(), transfer.session.easy()); |
379 | 78.6k | transfer.attached = false; |
380 | 78.6k | } |
381 | 79.1k | } |
382 | | |
383 | | // Destroy the multi while both its socket callback storage and mock peer are |
384 | | // live, then detach request-data pointers before cleaning each easy handle. |
385 | 26.0k | multi.reset(); |
386 | 26.0k | stats.watch_exhausted = socket_driver.watch_exhausted(); |
387 | 26.0k | if (stats.watch_exhausted) { |
388 | 0 | std::fputs("curl-fuzzer: socket watch capacity exhausted\n", stderr); |
389 | 0 | } |
390 | 105k | for (std::size_t index = 0; index < transfer_count; ++index) { |
391 | 79.7k | transfers[index].session.Close(); |
392 | 79.7k | } |
393 | 26.0k | return stats; |
394 | 26.0k | } |
395 | | |
396 | | } // namespace proto_fuzzer |