/src/curl_fuzzer/proto_fuzzer/pingpong_mock_server.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 shared line-oriented pingpong peer. |
9 | | |
10 | | #include "proto_fuzzer/pingpong_mock_server.h" |
11 | | |
12 | | #include <algorithm> |
13 | | #include <cstddef> |
14 | | #include <cstdint> |
15 | | #include <limits> |
16 | | #include <memory> |
17 | | #include <string> |
18 | | #include <string_view> |
19 | | |
20 | | #include "proto_fuzzer/mock_server.h" |
21 | | |
22 | | namespace proto_fuzzer { |
23 | | |
24 | | /// Construct the shared transport state without a borrowed protobuf or open |
25 | | /// socketpair. RunLoop borrows a Scenario only for the synchronous |
26 | | /// DriveScenario call in which socket callbacks can run. |
27 | | /// @param drive_policy Multi-loop policy inherited from MockServerBase. |
28 | | PingpongMockServer::PingpongMockServer(MultiDrivePolicy drive_policy) |
29 | 27.5k | : MockServerBase(drive_policy), |
30 | 27.5k | scenario_(nullptr), |
31 | 27.5k | control_reply_count_(0), |
32 | 27.5k | next_control_reply_(0), |
33 | 27.5k | active_pollset_probe_count_(0), |
34 | 27.5k | writable_pollset_probe_count_(0), |
35 | 27.5k | body_length_remaining_(0), |
36 | 27.5k | body_terminator_remaining_(0), |
37 | 27.5k | body_mode_(BodyMode::kCommand), |
38 | 27.5k | control_socket_(CURL_SOCKET_BAD), |
39 | 27.5k | control_opened_(false) {} |
40 | | |
41 | | /// Release the server-owned socketpair half after curl has returned its client |
42 | | /// descriptor. The out-of-line destructor keeps MockConnection's complete type |
43 | | /// local to this translation unit. |
44 | 27.5k | PingpongMockServer::~PingpongMockServer() = default; |
45 | | |
46 | | /// Expose the bounded command capture from the most recent drive so tests can |
47 | | /// verify protocol states without coupling assertions to curl debug output. |
48 | | /// @return A prefix of complete control commands, including line endings. |
49 | 27.3k | const std::string& PingpongMockServer::control_transcript() const { return control_transcript_; } |
50 | | |
51 | | /// Expose a bounded prefix of bytes drained during the most recent body phase |
52 | | /// while continuing to drain every client byte, so test observability cannot |
53 | | /// create backpressure. |
54 | | /// @return Body bytes curl sent, or an empty string when the peer has no |
55 | | /// upload direction. |
56 | 19.5k | const std::string& PingpongMockServer::uploaded_data() const { return uploaded_data_; } |
57 | | |
58 | | /// Report how many requested zero-timeout multi-poll probes ran during the |
59 | | /// most recent drive. RunLoop increments this only after a successful perform |
60 | | /// turn while curl still owns an active transfer and the control socket is |
61 | | /// open, making the count a focused test assertion rather than a syscall |
62 | | /// counter for default scenarios. |
63 | | /// @return Number of active, connected pollset probes in the previous drive. |
64 | 0 | std::size_t PingpongMockServer::active_pollset_probe_count() const { return active_pollset_probe_count_; } |
65 | | |
66 | | /// Report how often curl retained writable interest while the peer held an |
67 | | /// incomplete control command. Send-pressure tests use this conjunction to |
68 | | /// prove a protocol write remained pending rather than counting generic |
69 | | /// perform turns or transient writable states. |
70 | | /// @return Writable pollset observations in the previous drive. |
71 | 0 | std::size_t PingpongMockServer::writable_pollset_probe_count() const { return writable_pollset_probe_count_; } |
72 | | |
73 | | /// Create the default plaintext transport. The SMTPS subclass overrides this |
74 | | /// factory while retaining the command/reply parser. |
75 | | /// @return A new socketpair-backed connection. |
76 | 12.2k | std::unique_ptr<MockConnection> PingpongMockServer::CreateConnection() { return std::make_unique<MockConnection>(); } |
77 | | |
78 | | /// Enter marker-terminated body mode. The marker and any body bytes before it |
79 | | /// are captured together, matching SMTP's inclusion of its end-of-body CRLF. |
80 | | /// @param terminator Byte sequence that ends the body phase. |
81 | 978 | void PingpongMockServer::BeginMarkerBody(std::string_view terminator) { |
82 | 978 | body_mode_ = BodyMode::kMarker; |
83 | 978 | body_marker_.assign(terminator.data(), terminator.size()); |
84 | 978 | body_length_remaining_ = 0; |
85 | 978 | body_terminator_remaining_ = 0; |
86 | 978 | } |
87 | | |
88 | | /// Enter length-announced literal body mode. Exactly `length` bytes are |
89 | | /// captured; the CRLF that follows the literal is consumed but not captured. |
90 | | /// A zero length is valid and waits directly for that terminator. |
91 | | /// @param length Announced literal byte count. |
92 | 1.13k | void PingpongMockServer::BeginLiteralBody(std::size_t length) { |
93 | 1.13k | body_mode_ = BodyMode::kLiteral; |
94 | 1.13k | body_marker_.clear(); |
95 | 1.13k | body_length_remaining_ = length; |
96 | 1.13k | body_terminator_remaining_ = 2; |
97 | 1.13k | } |
98 | | |
99 | | /// Enter exact-length body mode. Exactly `length` bytes are captured and no |
100 | | /// terminator is consumed, so the next byte after the body starts the next |
101 | | /// command. A zero length completes immediately. |
102 | | /// @param length Announced body byte count. |
103 | 982 | void PingpongMockServer::BeginExactBody(std::size_t length) { |
104 | 982 | body_mode_ = BodyMode::kExact; |
105 | 982 | body_marker_.clear(); |
106 | 982 | body_length_remaining_ = length; |
107 | 982 | body_terminator_remaining_ = 0; |
108 | 982 | } |
109 | | |
110 | | /// Enter an opaque drain after a transport upgrade. Every subsequent carrier |
111 | | /// byte is consumed without command parsing or test-facing capture. |
112 | 3 | void PingpongMockServer::BeginOpaqueDrain() { |
113 | 3 | body_mode_ = BodyMode::kOpaque; |
114 | 3 | body_marker_.clear(); |
115 | 3 | body_length_remaining_ = 0; |
116 | 3 | body_terminator_remaining_ = 0; |
117 | 3 | } |
118 | | |
119 | | /// Append decoded application bytes to the pending parser buffer. The outer |
120 | | /// service loop drains this buffer after every transport call, so it remains |
121 | | /// bounded by the scenario's command and upload budgets. |
122 | | /// @param data First decoded byte. |
123 | | /// @param size Number of bytes available at `data`. |
124 | 131k | void PingpongMockServer::ObserveIncomingData(const unsigned char* data, std::size_t size) { |
125 | 131k | if (data == nullptr || size == 0) { |
126 | 0 | return; |
127 | 0 | } |
128 | 131k | pending_control_bytes_.append(reinterpret_cast<const char*>(data), size); |
129 | 131k | } |
130 | | |
131 | | /// Create the single control socketpair and queue the server greeting. These |
132 | | /// peers never open a second application socket, so any later request is |
133 | | /// rejected rather than silently replacing the live peer. |
134 | | /// @param purpose Socket role requested by curl. |
135 | | /// @param address Unused stream destination metadata required by the callback |
136 | | /// signature. |
137 | | /// @return Client descriptor for the control channel, or CURL_SOCKET_BAD. |
138 | 24.6k | curl_socket_t PingpongMockServer::HandleOpenSocket(curlsocktype purpose, struct curl_sockaddr* /*address*/) { |
139 | 24.6k | if (scenario_ == nullptr || purpose != CURLSOCKTYPE_IPCXN || control_opened_) { |
140 | 0 | return CURL_SOCKET_BAD; |
141 | 0 | } |
142 | | |
143 | 24.6k | connection_ = CreateConnection(); |
144 | 24.6k | if (!connection_ || !connection_->ok()) { |
145 | 0 | connection_.reset(); |
146 | 0 | return CURL_SOCKET_BAD; |
147 | 0 | } |
148 | 24.6k | connection_->SetIncomingDataObserver(this); |
149 | | |
150 | 24.6k | ApplyScriptBackpressure(connection_.get(), scenario_->connection()); |
151 | 24.6k | const std::string& greeting = scenario_->connection().initial_response(); |
152 | 24.6k | if (!greeting.empty() && |
153 | 9.16k | !connection_->WriteAll(reinterpret_cast<const unsigned char*>(greeting.data()), greeting.size())) { |
154 | 9 | connection_.reset(); |
155 | 9 | return CURL_SOCKET_BAD; |
156 | 9 | } |
157 | | |
158 | 24.6k | control_opened_ = true; |
159 | 24.6k | control_socket_ = connection_->take_client_fd(); |
160 | 24.6k | return control_socket_; |
161 | 24.6k | } |
162 | | |
163 | | /// Reset every borrowed pointer, descriptor, parser cursor, and observable |
164 | | /// capture before borrowing the next Scenario, then let the subclass clear its |
165 | | /// own protocol state. |
166 | | /// @param scenario Scenario whose Connection supplies the greeting and replies. |
167 | 26.9k | void PingpongMockServer::ResetForScenario(const curl::fuzzer::proto::Scenario& scenario) { |
168 | 26.9k | connection_.reset(); |
169 | 26.9k | scenario_ = &scenario; |
170 | 26.9k | control_reply_count_ = std::min<std::size_t>(scenario_limits::kMaxResponseChunks, |
171 | 26.9k | static_cast<std::size_t>(scenario.connection().on_readable_size())); |
172 | 26.9k | next_control_reply_ = 0; |
173 | 26.9k | pending_control_bytes_.clear(); |
174 | 26.9k | control_transcript_.clear(); |
175 | 26.9k | uploaded_data_.clear(); |
176 | 26.9k | active_pollset_probe_count_ = 0; |
177 | 26.9k | writable_pollset_probe_count_ = 0; |
178 | 26.9k | body_marker_.clear(); |
179 | 26.9k | body_length_remaining_ = 0; |
180 | 26.9k | body_terminator_remaining_ = 0; |
181 | 26.9k | body_mode_ = BodyMode::kCommand; |
182 | 26.9k | control_socket_ = CURL_SOCKET_BAD; |
183 | 26.9k | control_opened_ = false; |
184 | 26.9k | ResetProtocolState(); |
185 | 26.9k | } |
186 | | |
187 | | /// Apply bounded transport knobs when creating the scripted socket. Fast IMAP |
188 | | /// and POP3 retain bounded receive-buffer pressure; direct corpus replay must |
189 | | /// also retain deterministic, well-defined behavior. |
190 | | /// @param connection Peer receiving the socket settings. |
191 | | /// @param script Connection script carrying optional backpressure values. |
192 | | void PingpongMockServer::ApplyScriptBackpressure(MockConnection* connection, |
193 | 24.6k | const curl::fuzzer::proto::Connection& script) { |
194 | 24.6k | if (connection == nullptr) { |
195 | 0 | return; |
196 | 0 | } |
197 | | |
198 | 24.6k | const std::uint32_t int_max = static_cast<std::uint32_t>(std::numeric_limits<int>::max()); |
199 | 24.6k | const auto& backpressure = script.backpressure(); |
200 | 24.6k | const int receive_buffer = static_cast<int>(std::min(backpressure.recv_buf_bytes(), int_max)); |
201 | 24.6k | connection->ApplyBackpressure(receive_buffer, static_cast<std::size_t>(backpressure.drain_limit())); |
202 | 24.6k | } |
203 | | |
204 | | /// Return the next command-aligned response within the shared response-count |
205 | | /// budget. Empty strings still advance the cursor: they are meaningful |
206 | | /// truncation mutations for ordinary command states. |
207 | | /// @return Scenario-owned reply, or nullptr after the bounded prefix. |
208 | 1.28M | const std::string* PingpongMockServer::NextControlReply() { |
209 | 1.28M | if (scenario_ == nullptr || next_control_reply_ >= control_reply_count_) { |
210 | 1.25M | return nullptr; |
211 | 1.25M | } |
212 | 29.4k | return &scenario_->connection().on_readable(static_cast<int>(next_control_reply_++)); |
213 | 1.28M | } |
214 | | |
215 | | /// Keep failed peer writes local to the mock. Response cursors must still |
216 | | /// advance after curl closes early, otherwise a replacement socket could see |
217 | | /// a reply intended for an earlier command. |
218 | | /// @param bytes Response fragment to queue. |
219 | | /// @return true when the complete fragment was queued. |
220 | 44.6k | bool PingpongMockServer::WriteControlBytes(std::string_view bytes) { |
221 | 44.6k | return bytes.empty() || (connection_ != nullptr && |
222 | 44.1k | connection_->WriteAll(reinterpret_cast<const unsigned char*>(bytes.data()), bytes.size())); |
223 | 44.6k | } |
224 | | |
225 | | /// Consume the next scripted reply. Protocol peers use this when the reply |
226 | | /// that completes an upload must be queued at command-acceptance time because |
227 | | /// curl performs a blocking completion wait. |
228 | | /// @return true when a scripted reply existed and was successfully written. |
229 | 19.8k | bool PingpongMockServer::WriteNextReply() { |
230 | 19.8k | const std::string* response = NextControlReply(); |
231 | 19.8k | return response == nullptr ? false : WriteControlBytes(*response); |
232 | 19.8k | } |
233 | | |
234 | | /// Half-close the server-to-client direction while retaining the read half. |
235 | | /// STARTTLS peers use this after their plaintext acceptance response so curl's |
236 | | /// newly installed TLS filter observes deterministic EOF instead of waiting. |
237 | 3 | void PingpongMockServer::ShutdownControlWrite() { |
238 | 3 | if (connection_ != nullptr) { |
239 | 3 | connection_->ShutdownWrite(); |
240 | 3 | } |
241 | 3 | } |
242 | | |
243 | | /// Remove optional leading horizontal whitespace and return the first token |
244 | | /// without allocating. Protocol arguments are never interpreted here. |
245 | | /// @param line Complete command line. |
246 | | /// @return View of its first non-whitespace token. |
247 | 1.27M | std::string_view PingpongMockServer::FirstToken(std::string_view line) { |
248 | 1.27M | std::size_t begin = 0; |
249 | 1.50M | while (begin < line.size() && (line[begin] == ' ' || line[begin] == '\t')) { |
250 | 231k | ++begin; |
251 | 231k | } |
252 | | |
253 | 1.27M | std::size_t end = begin; |
254 | 71.5M | while (end < line.size() && line[end] != ' ' && line[end] != '\t' && line[end] != '\r' && line[end] != '\n') { |
255 | 70.2M | ++end; |
256 | 70.2M | } |
257 | 1.27M | return line.substr(begin, end - begin); |
258 | 1.27M | } |
259 | | |
260 | | /// Skip the first token and return the second whitespace-delimited token |
261 | | /// without allocating. IMAP uses this to read the verb after the command tag. |
262 | | /// @param line Complete command line. |
263 | | /// @return View of its second whitespace-delimited token, or an empty view. |
264 | 26.9k | std::string_view PingpongMockServer::SecondToken(std::string_view line) { |
265 | 26.9k | std::size_t position = 0; |
266 | 84.4k | while (position < line.size() && (line[position] == ' ' || line[position] == '\t')) { |
267 | 57.5k | ++position; |
268 | 57.5k | } |
269 | 4.24M | while (position < line.size() && line[position] != ' ' && line[position] != '\t' && line[position] != '\r' && |
270 | 4.21M | line[position] != '\n') { |
271 | 4.21M | ++position; |
272 | 4.21M | } |
273 | 50.2k | while (position < line.size() && (line[position] == ' ' || line[position] == '\t')) { |
274 | 23.3k | ++position; |
275 | 23.3k | } |
276 | | |
277 | 26.9k | std::size_t end = position; |
278 | 216k | while (end < line.size() && line[end] != ' ' && line[end] != '\t' && line[end] != '\r' && line[end] != '\n') { |
279 | 189k | ++end; |
280 | 189k | } |
281 | 26.9k | return line.substr(position, end - position); |
282 | 26.9k | } |
283 | | |
284 | | /// Fold only ASCII lowercase command letters. Protocol verbs are ASCII tokens, |
285 | | /// so locale-aware case conversion would add state and undefined signed-char |
286 | | /// behavior without accepting anything curl can legitimately send. |
287 | | /// @param token Parsed command token. |
288 | | /// @param expected_uppercase Literal uppercase command name. |
289 | | /// @return true when the tokens are equal ignoring ASCII case. |
290 | 64.5k | bool PingpongMockServer::TokenEquals(std::string_view token, std::string_view expected_uppercase) { |
291 | 64.5k | if (token.size() != expected_uppercase.size()) { |
292 | 27.5k | return false; |
293 | 27.5k | } |
294 | 94.6k | for (std::size_t index = 0; index < token.size(); ++index) { |
295 | 83.4k | unsigned char actual = static_cast<unsigned char>(token[index]); |
296 | 83.4k | if (actual >= 'a' && actual <= 'z') { |
297 | 361 | actual = static_cast<unsigned char>(actual - 'a' + 'A'); |
298 | 361 | } |
299 | 83.4k | if (actual != static_cast<unsigned char>(expected_uppercase[index])) { |
300 | 25.7k | return false; |
301 | 25.7k | } |
302 | 83.4k | } |
303 | 11.2k | return true; |
304 | 36.9k | } |
305 | | |
306 | | /// Retain only a bounded prefix for assertions. Parsing and draining continue |
307 | | /// against the full transport bytes, so reaching the cap cannot change curl's |
308 | | /// behavior or manufacture socket backpressure. |
309 | | /// @param source Newly observed bytes. |
310 | | /// @param limit Maximum retained destination size. |
311 | | /// @param destination Capture buffer receiving the available prefix. |
312 | 1.35M | void PingpongMockServer::CapturePrefix(std::string_view source, std::size_t limit, std::string* destination) { |
313 | 1.35M | if (destination == nullptr || destination->size() >= limit) { |
314 | 712k | return; |
315 | 712k | } |
316 | 638k | const std::size_t available = limit - destination->size(); |
317 | 638k | destination->append(source.data(), std::min(source.size(), available)); |
318 | 638k | } |
319 | | |
320 | | /// Advance the carrier without waiting, then process every complete command or |
321 | | /// body segment already decoded into the pending buffer. A plaintext |
322 | | /// connection yields raw socket bytes; a TLS carrier completes its handshake |
323 | | /// and yields decrypted application bytes through ObserveIncomingData. |
324 | | /// @return true when bytes, transport state, or a command/reply cursor advanced. |
325 | 308k | bool PingpongMockServer::ServiceControlConnection() { |
326 | 308k | if (connection_ == nullptr) { |
327 | 10.8k | return false; |
328 | 10.8k | } |
329 | | |
330 | 297k | const std::size_t size_before_read = pending_control_bytes_.size(); |
331 | 297k | const std::size_t transport_progress = connection_->DrainIncoming(); |
332 | 297k | bool made_progress = pending_control_bytes_.size() != size_before_read || transport_progress != 0; |
333 | | |
334 | 297k | std::size_t consumed = 0; |
335 | 1.64M | while (consumed < pending_control_bytes_.size()) { |
336 | 1.39M | if (body_mode_ == BodyMode::kOpaque) { |
337 | 1 | consumed = pending_control_bytes_.size(); |
338 | 1 | made_progress = true; |
339 | 1 | break; |
340 | 1 | } |
341 | | |
342 | 1.39M | if (body_mode_ == BodyMode::kMarker) { |
343 | 17.1k | const std::size_t marker = pending_control_bytes_.find(body_marker_, consumed); |
344 | 17.1k | if (marker == std::string::npos) { |
345 | 16.4k | break; |
346 | 16.4k | } |
347 | 699 | const std::size_t body_end = marker + body_marker_.size(); |
348 | 699 | CapturePrefix(std::string_view(pending_control_bytes_.data() + consumed, body_end - consumed), |
349 | 699 | kMaxCapturedUploadBytes, &uploaded_data_); |
350 | 699 | consumed = body_end; |
351 | 699 | body_mode_ = BodyMode::kCommand; |
352 | 699 | made_progress = true; |
353 | 699 | HandleBodyComplete(); |
354 | 699 | continue; |
355 | 17.1k | } |
356 | | |
357 | 1.37M | if (body_mode_ == BodyMode::kExact) { |
358 | 1.47k | const std::size_t available = pending_control_bytes_.size() - consumed; |
359 | 1.47k | const std::size_t take = std::min(available, body_length_remaining_); |
360 | 1.47k | CapturePrefix(std::string_view(pending_control_bytes_.data() + consumed, take), kMaxCapturedUploadBytes, |
361 | 1.47k | &uploaded_data_); |
362 | 1.47k | consumed += take; |
363 | 1.47k | body_length_remaining_ -= take; |
364 | 1.47k | if (take != 0) { |
365 | 1.47k | made_progress = true; |
366 | 1.47k | } |
367 | 1.47k | if (body_length_remaining_ == 0) { |
368 | 715 | body_mode_ = BodyMode::kCommand; |
369 | 715 | made_progress = true; |
370 | 715 | HandleBodyComplete(); |
371 | 715 | continue; |
372 | 715 | } |
373 | 757 | break; |
374 | 1.47k | } |
375 | | |
376 | 1.37M | if (body_mode_ == BodyMode::kLiteral) { |
377 | 18.5k | if (body_length_remaining_ != 0) { |
378 | 17.9k | const std::size_t available = pending_control_bytes_.size() - consumed; |
379 | 17.9k | const std::size_t take = std::min(available, body_length_remaining_); |
380 | 17.9k | CapturePrefix(std::string_view(pending_control_bytes_.data() + consumed, take), kMaxCapturedUploadBytes, |
381 | 17.9k | &uploaded_data_); |
382 | 17.9k | consumed += take; |
383 | 17.9k | body_length_remaining_ -= take; |
384 | 17.9k | made_progress = true; |
385 | 17.9k | continue; |
386 | 17.9k | } |
387 | | |
388 | 623 | const std::size_t available = pending_control_bytes_.size() - consumed; |
389 | 623 | if (body_terminator_remaining_ == 2 && available >= 2 && pending_control_bytes_[consumed] == '\r' && |
390 | 617 | pending_control_bytes_[consumed + 1] == '\n') { |
391 | 617 | consumed += 2; |
392 | 617 | body_terminator_remaining_ = 0; |
393 | 617 | body_mode_ = BodyMode::kCommand; |
394 | 617 | made_progress = true; |
395 | 617 | HandleBodyComplete(); |
396 | 617 | continue; |
397 | 617 | } |
398 | 6 | if (available >= 1) { |
399 | 6 | const char byte = pending_control_bytes_[consumed]; |
400 | 6 | if (byte == '\r' || byte == '\n') { |
401 | 1 | ++consumed; |
402 | 1 | if (byte == '\n' || body_terminator_remaining_ == 0) { |
403 | 1 | body_terminator_remaining_ = 0; |
404 | 1 | body_mode_ = BodyMode::kCommand; |
405 | 1 | HandleBodyComplete(); |
406 | 1 | } else { |
407 | 0 | body_terminator_remaining_ = 1; |
408 | 0 | } |
409 | 1 | made_progress = true; |
410 | 1 | continue; |
411 | 1 | } |
412 | | // No terminating empty line is recognisable. Resume command parsing so |
413 | | // malformed uploads still make bounded progress. |
414 | 5 | body_terminator_remaining_ = 0; |
415 | 5 | body_mode_ = BodyMode::kCommand; |
416 | 5 | continue; |
417 | 6 | } |
418 | 0 | break; |
419 | 6 | } |
420 | | |
421 | 1.35M | const std::size_t newline = pending_control_bytes_.find('\n', consumed); |
422 | 1.35M | if (newline == std::string::npos) { |
423 | 25.7k | break; |
424 | 25.7k | } |
425 | 1.33M | const std::size_t command_size = newline - consumed + 1; |
426 | 1.33M | const std::string_view command(pending_control_bytes_.data() + consumed, command_size); |
427 | 1.33M | CapturePrefix(command, kMaxCapturedControlBytes, &control_transcript_); |
428 | 1.33M | HandleLine(command); |
429 | 1.33M | consumed += command_size; |
430 | 1.33M | made_progress = true; |
431 | 1.33M | } |
432 | | |
433 | 297k | if (consumed != 0) { |
434 | 65.7k | pending_control_bytes_.erase(0, consumed); |
435 | 65.7k | } |
436 | 297k | return made_progress; |
437 | 308k | } |
438 | | |
439 | | /// Prepare cleanup while the mock can still write to curl's control socket. |
440 | | /// Let the peer queue its protocol-specific liveness reply, then half-close so |
441 | | /// an unfinished transfer cannot wait on a reply that will never arrive. |
442 | 26.9k | void PingpongMockServer::FinishConnections() { |
443 | 26.9k | if (connection_ != nullptr) { |
444 | 24.6k | QueueShutdownReply(); |
445 | 24.6k | connection_->ShutdownWrite(); |
446 | 24.6k | } |
447 | 26.9k | } |
448 | | |
449 | | /// Give curl's graceful shutdown one last chance to emit its teardown command |
450 | | /// while the peer still owns its read half, then release the multi as the base |
451 | | /// class would. The matching liveness reply was already queued by |
452 | | /// FinishConnections. |
453 | | /// @param multi Detached multi handle whose connection cache may be shutting |
454 | | /// the protocol connection down. |
455 | 26.9k | void PingpongMockServer::HandleDetachedMulti(CurlMultiPtr multi) { |
456 | 26.9k | multi.reset(); |
457 | 26.9k | (void)ServiceControlConnection(); |
458 | 26.9k | } |
459 | | |
460 | | /// Alternate curl transitions with immediate peer service until the transfer |
461 | | /// completes or deterministic idle/operation caps win. No select(), poll(), or |
462 | | /// sleep is needed: every transport is in-process, and malformed scripts are |
463 | | /// terminated by half-close after the bounded loop. |
464 | | /// @param multi Multi handle containing `easy`. |
465 | | /// @param easy Easy handle already installed on this mock. |
466 | | /// @param scenario Control script to borrow. |
467 | 26.9k | void PingpongMockServer::RunLoop(CURLM* multi, CURL* easy, const curl::fuzzer::proto::Scenario& scenario) { |
468 | 26.9k | (void)easy; |
469 | 26.9k | ResetForScenario(scenario); |
470 | | |
471 | 26.9k | int still_running = 1; |
472 | 26.9k | int idle_iterations = 0; |
473 | 26.9k | int drive_iterations = 0; |
474 | 26.9k | CURLMcode result = CURLM_OK; |
475 | 281k | while (still_running && idle_iterations < kMaxIdleIterations && drive_iterations++ < kMaxDriveIterations) { |
476 | 254k | const int running_before = still_running; |
477 | 254k | result = curl_multi_perform(multi, &still_running); |
478 | 254k | if (result != CURLM_OK) { |
479 | 0 | break; |
480 | 0 | } |
481 | | |
482 | | // Pollset construction is coverage work, not part of the default driver: |
483 | | // keep the extra public-API call behind the scenario opt-in. Waiting until |
484 | | // the socket callback has run and the transfer remains active ensures the |
485 | | // pollset belongs to a live protocol state rather than setup or cleanup. |
486 | 254k | if (scenario.probe_multi_pollset() && still_running && control_opened_) { |
487 | 28.2k | ProbeMultiPollset(multi); |
488 | 28.2k | ++active_pollset_probe_count_; |
489 | 28.2k | const bool incomplete_command = body_mode_ == BodyMode::kCommand && !pending_control_bytes_.empty() && |
490 | 6.55k | pending_control_bytes_.find('\n') == std::string::npos; |
491 | 28.2k | if (incomplete_command && MultiPollsetWantsWrite(multi, control_socket_)) { |
492 | 6.46k | ++writable_pollset_probe_count_; |
493 | 6.46k | } |
494 | 28.2k | } |
495 | | |
496 | 254k | bool made_progress = still_running != running_before; |
497 | 254k | made_progress = ServiceControlConnection() || made_progress; |
498 | 254k | if (made_progress) { |
499 | 129k | idle_iterations = 0; |
500 | 129k | } else { |
501 | 125k | ++idle_iterations; |
502 | 125k | } |
503 | 254k | } |
504 | | |
505 | | // The final perform may have emitted the last command or body bytes |
506 | | // immediately before marking the transfer done. Drain them before cleanup. |
507 | 26.9k | (void)ServiceControlConnection(); |
508 | 26.9k | FinishConnections(); |
509 | 26.9k | scenario_ = nullptr; |
510 | 26.9k | } |
511 | | |
512 | | } // namespace proto_fuzzer |