Coverage Report

Created: 2026-10-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/scenario_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 RunScenario.
9
10
#include "proto_fuzzer/scenario_runner.h"
11
12
#include <curl/curl.h>
13
#include <curl/header.h>
14
15
#include <cassert>
16
#include <chrono>
17
#include <cstddef>
18
#include <memory>
19
#include <string>
20
#include <string_view>
21
22
#include "proto_fuzzer/api_lifecycle.h"
23
#include "proto_fuzzer/dict_mock_server.h"
24
#include "proto_fuzzer/ftp_mock_server.h"
25
#include "proto_fuzzer/gopher_mock_server.h"
26
#include "proto_fuzzer/h2_cleartext_mock_server.h"
27
#include "proto_fuzzer/happy_eyeballs_mock_server.h"
28
#include "proto_fuzzer/http_transport_mock_server.h"
29
#include "proto_fuzzer/imap_mock_server.h"
30
#include "proto_fuzzer/ipfs_url.h"
31
#include "proto_fuzzer/ldap_mock_server.h"
32
#include "proto_fuzzer/mock_server.h"
33
#include "proto_fuzzer/mock_server_base.h"
34
#include "proto_fuzzer/mqtt_mock_server.h"
35
#include "proto_fuzzer/multi_transfer_runner.h"
36
#include "proto_fuzzer/option_apply.h"
37
#include "proto_fuzzer/pop3_mock_server.h"
38
#include "proto_fuzzer/proxy_option_mock_server.h"
39
#include "proto_fuzzer/request_data.h"
40
#include "proto_fuzzer/resolver_cancel_gate.h"
41
#include "proto_fuzzer/rtsp_mock_server.h"
42
#include "proto_fuzzer/smtp_mock_server.h"
43
#include "proto_fuzzer/socks4_mock_server.h"
44
#include "proto_fuzzer/telnet_mock_server.h"
45
#include "proto_fuzzer/tftp_mock_server.h"
46
#include "proto_fuzzer/transfer_session.h"
47
#include "proto_fuzzer/websocket_mock_server.h"
48
49
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
50
#include "proto_fuzzer/doh_https_rr_mock_server.h"
51
#include "proto_fuzzer/ftps_mock_server.h"
52
#include "proto_fuzzer/h2_origin_mock_server.h"
53
#include "proto_fuzzer/h2_proxy_mock_server.h"
54
#include "proto_fuzzer/imaps_mock_server.h"
55
#include "proto_fuzzer/ldaps_mock_server.h"
56
#include "proto_fuzzer/mqtts_mock_server.h"
57
#include "proto_fuzzer/pop3s_mock_server.h"
58
#include "proto_fuzzer/smtps_mock_server.h"
59
#include "proto_fuzzer/tls_mock_server.h"
60
#include "proto_fuzzer/tls_websocket_mock_server.h"
61
#endif
62
#if defined(PROTO_FUZZER_HAS_HTTP3_MOCK_SERVER)
63
#include "proto_fuzzer/connect_udp_proxy_mock_server.h"
64
#include "proto_fuzzer/http3_mock_server.h"
65
#endif
66
67
namespace proto_fuzzer {
68
69
namespace {
70
71
constexpr unsigned int kAllHeaderOrigins = CURLH_HEADER | CURLH_TRAILER | CURLH_CONNECT | CURLH_1XX | CURLH_PSEUDO;
72
constexpr std::size_t kMaxResultHeaders = 16;
73
constexpr char kResolverUnixSocketPath[] = "/curl-fuzzer-resolver.sock";
74
constexpr char kResolverCancelDuplicateUrl[] = "http://resolver-queued-cancel.test:81/queued-cancel-duplicate";
75
constexpr auto kResolverCancelGateWait = std::chrono::milliseconds(500);
76
constexpr int kResolverIdlePollAttempts = 4;
77
constexpr int kResolverIdlePollTimeoutMs = 250;
78
79
/// Return whether a fixed plaintext lane requests an explicit TLS upgrade.
80
/// Compatibility preserves these append-only selectors without interpreting
81
/// them, so the run mode remains part of the decision.
82
313k
bool ScenarioRequestsStartTls(const curl::fuzzer::proto::Scenario& scenario, ScenarioRunMode mode) {
83
313k
  switch (mode) {
84
6.66k
    case ScenarioRunMode::kLdapCoverage:
85
6.66k
      return scenario.scheme() == curl::fuzzer::proto::SCHEME_LDAP && scenario.ldap_starttls();
86
7.44k
    case ScenarioRunMode::kFtpCoverage:
87
7.44k
      return scenario.scheme() == curl::fuzzer::proto::SCHEME_FTP && scenario.ftp_auth_tls_filter_allocation_failure();
88
4.11k
    case ScenarioRunMode::kPop3Coverage:
89
4.11k
      return scenario.scheme() == curl::fuzzer::proto::SCHEME_POP3 &&
90
954
             scenario.pop3_starttls_filter_allocation_failure();
91
295k
    default:
92
295k
      return false;
93
313k
  }
94
313k
}
95
96
struct ResolverIdleTraceState {
97
261
  explicit ResolverIdleTraceState(CURL* handle) : easy(handle) {}
98
99
522
  ~ResolverIdleTraceState() {
100
    // The callback is static, but its data is stack-owned. Disable tracing and
101
    // clear that pointer before the easy handle outlives this focused drive.
102
522
    (void)curl_easy_setopt(easy, CURLOPT_VERBOSE, 0L);
103
522
    (void)curl_easy_setopt(easy, CURLOPT_DEBUGDATA, nullptr);
104
522
  }
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::ResolverIdleTraceState::~ResolverIdleTraceState()
Line
Count
Source
99
261
  ~ResolverIdleTraceState() {
100
    // The callback is static, but its data is stack-owned. Disable tracing and
101
    // clear that pointer before the easy handle outlives this focused drive.
102
261
    (void)curl_easy_setopt(easy, CURLOPT_VERBOSE, 0L);
103
261
    (void)curl_easy_setopt(easy, CURLOPT_DEBUGDATA, nullptr);
104
261
  }
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::ResolverIdleTraceState::~ResolverIdleTraceState()
Line
Count
Source
99
261
  ~ResolverIdleTraceState() {
100
    // The callback is static, but its data is stack-owned. Disable tracing and
101
    // clear that pointer before the easy handle outlives this focused drive.
102
261
    (void)curl_easy_setopt(easy, CURLOPT_VERBOSE, 0L);
103
261
    (void)curl_easy_setopt(easy, CURLOPT_DEBUGDATA, nullptr);
104
261
  }
105
106
  CURL* easy;
107
  bool observe_idle = false;
108
  bool saw_idle = false;
109
};
110
111
19.6k
int ObserveResolverIdleTrace(CURL* /*easy*/, curl_infotype type, char* data, std::size_t size, void* client_data) {
112
19.6k
  auto* state = static_cast<ResolverIdleTraceState*>(client_data);
113
19.6k
  if (state == nullptr || !state->observe_idle || type != CURLINFO_TEXT || data == nullptr) {
114
2.23k
    return 0;
115
2.23k
  }
116
17.3k
  const std::string_view trace(data, size);
117
17.3k
  if (trace.find("[TPOOL-DNS]") != std::string_view::npos && trace.find("idle for") != std::string_view::npos) {
118
0
    state->saw_idle = true;
119
0
  }
120
17.3k
  return 0;
121
19.6k
}
122
123
223
int CountResolverStart(void* /*resolver_state*/, void* /*reserved*/, void* client_data) {
124
223
  ++*static_cast<int*>(client_data);
125
223
  return 0;
126
223
}
127
128
/// Apply only harness-owned resolver controls. Target policy has already
129
/// replaced the URL authority with a fixed name understood by the target-local
130
/// getaddrinfo shim and removed fuzzed cache entries for these modes.
131
20.9k
bool ApplyResolverMode(CURL* easy, curl::fuzzer::proto::ResolverMode mode) {
132
20.9k
  switch (mode) {
133
676
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_POSITIVE:
134
676
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4) == CURLE_OK &&
135
676
             curl_easy_setopt(easy, CURLOPT_DNS_SHUFFLE_ADDRESSES, 1L) == CURLE_OK;
136
946
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_PARTIAL:
137
1.46k
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_QUEUED_CANCEL:
138
1.46k
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER) == CURLE_OK;
139
46
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_NEGATIVE:
140
46
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER) == CURLE_OK;
141
416
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_LINK_LOCAL:
142
416
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6) == CURLE_OK;
143
636
    case curl::fuzzer::proto::RESOLVER_MODE_UNIX_SOCKET:
144
636
      return curl_easy_setopt(easy, CURLOPT_UNIX_SOCKET_PATH, kResolverUnixSocketPath) == CURLE_OK;
145
16.1k
    case curl::fuzzer::proto::RESOLVER_MODE_DEFAULT:
146
17.6k
    case curl::fuzzer::proto::RESOLVER_MODE_GLOBAL_LIFECYCLE:
147
17.6k
    default:
148
17.6k
      return true;
149
20.9k
  }
150
20.9k
}
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::ApplyResolverMode(void*, curl::fuzzer::proto::ResolverMode)
Line
Count
Source
131
10.4k
bool ApplyResolverMode(CURL* easy, curl::fuzzer::proto::ResolverMode mode) {
132
10.4k
  switch (mode) {
133
338
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_POSITIVE:
134
338
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4) == CURLE_OK &&
135
338
             curl_easy_setopt(easy, CURLOPT_DNS_SHUFFLE_ADDRESSES, 1L) == CURLE_OK;
136
473
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_PARTIAL:
137
734
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_QUEUED_CANCEL:
138
734
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER) == CURLE_OK;
139
23
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_NEGATIVE:
140
23
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER) == CURLE_OK;
141
208
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_LINK_LOCAL:
142
208
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6) == CURLE_OK;
143
318
    case curl::fuzzer::proto::RESOLVER_MODE_UNIX_SOCKET:
144
318
      return curl_easy_setopt(easy, CURLOPT_UNIX_SOCKET_PATH, kResolverUnixSocketPath) == CURLE_OK;
145
8.07k
    case curl::fuzzer::proto::RESOLVER_MODE_DEFAULT:
146
8.83k
    case curl::fuzzer::proto::RESOLVER_MODE_GLOBAL_LIFECYCLE:
147
8.83k
    default:
148
8.83k
      return true;
149
10.4k
  }
150
10.4k
}
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::ApplyResolverMode(void*, curl::fuzzer::proto::ResolverMode)
Line
Count
Source
131
10.4k
bool ApplyResolverMode(CURL* easy, curl::fuzzer::proto::ResolverMode mode) {
132
10.4k
  switch (mode) {
133
338
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_POSITIVE:
134
338
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4) == CURLE_OK &&
135
338
             curl_easy_setopt(easy, CURLOPT_DNS_SHUFFLE_ADDRESSES, 1L) == CURLE_OK;
136
473
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_PARTIAL:
137
734
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_QUEUED_CANCEL:
138
734
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER) == CURLE_OK;
139
23
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_NEGATIVE:
140
23
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_WHATEVER) == CURLE_OK;
141
208
    case curl::fuzzer::proto::RESOLVER_MODE_THREADED_LINK_LOCAL:
142
208
      return curl_easy_setopt(easy, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6) == CURLE_OK;
143
318
    case curl::fuzzer::proto::RESOLVER_MODE_UNIX_SOCKET:
144
318
      return curl_easy_setopt(easy, CURLOPT_UNIX_SOCKET_PATH, kResolverUnixSocketPath) == CURLE_OK;
145
8.07k
    case curl::fuzzer::proto::RESOLVER_MODE_DEFAULT:
146
8.83k
    case curl::fuzzer::proto::RESOLVER_MODE_GLOBAL_LIFECYCLE:
147
8.83k
    default:
148
8.83k
      return true;
149
10.4k
  }
150
10.4k
}
151
152
/// Resolve two public easy handles through one worker. The first handle queues
153
/// AAAA then A. Its first getaddrinfo call cannot return through the target
154
/// gate, so the sole worker cannot take the second family. Only then is the
155
/// duplicate added on a distinct origin and allowed to queue its own AAAA and
156
/// A work behind the blocked lookup.
157
///
158
/// The resolver-start callback fires before curl submits the duplicate's
159
/// queries, but curl_multi_perform cannot return until that synchronous
160
/// submission finishes. A successful perform, a live duplicate, and the
161
/// observed callback therefore prove the duplicate resolver work is queued.
162
/// Removing that active duplicate is the public cancellation operation: curl
163
/// synchronously shuts down its live async resolver, scans the queue, and
164
/// destroys the matching items before clearing the handle's multi pointer.
165
/// The gate's scope ends after this removal and before cleanup can join the
166
/// worker, so teardown cannot wait in a cycle with the shim.
167
522
CURLcode RunQueuedResolverCancellation(CURL* easy, MockServer* mock, const curl::fuzzer::proto::Scenario& scenario) {
168
522
  int duplicate_resolver_starts = 0;
169
522
  ResolverIdleTraceState idle_trace(easy);
170
  // Keep callback data alive until both handles are gone. Declaring the
171
  // duplicate before the multi also makes exceptional teardown destroy the
172
  // multi (and detach any handle whose public removal failed) first.
173
522
  CurlEasyPtr duplicate(curl_easy_duphandle(easy));
174
522
  CurlMultiPtr multi(curl_multi_init());
175
  // This drive is bounded by the gate wait and explicit removals. Clear the
176
  // ordinary 200 ms transfer/connect settings (zero selects no operation
177
  // deadline and curl's default connect deadline) so host scheduling cannot
178
  // cancel either handle before the queue invariant is established.
179
522
  if (multi == nullptr || duplicate == nullptr ||
180
478
      curl_multi_setopt(multi.get(), CURLMOPT_RESOLVE_THREADS_MAX, 1L) != CURLM_OK ||
181
478
      curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, 0L) != CURLE_OK ||
182
478
      curl_easy_setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, 0L) != CURLE_OK ||
183
478
      curl_easy_setopt(duplicate.get(), CURLOPT_TIMEOUT_MS, 0L) != CURLE_OK ||
184
478
      curl_easy_setopt(duplicate.get(), CURLOPT_CONNECTTIMEOUT_MS, 0L) != CURLE_OK ||
185
478
      curl_easy_setopt(duplicate.get(), CURLOPT_URL, kResolverCancelDuplicateUrl) != CURLE_OK ||
186
476
      curl_easy_setopt(duplicate.get(), CURLOPT_FRESH_CONNECT, 1L) != CURLE_OK ||
187
476
      curl_easy_setopt(duplicate.get(), CURLOPT_RESOLVER_START_FUNCTION, &CountResolverStart) != CURLE_OK ||
188
476
      curl_easy_setopt(duplicate.get(), CURLOPT_RESOLVER_START_DATA, &duplicate_resolver_starts) != CURLE_OK ||
189
476
      curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, &ObserveResolverIdleTrace) != CURLE_OK ||
190
476
      curl_easy_setopt(easy, CURLOPT_DEBUGDATA, &idle_trace) != CURLE_OK ||
191
476
      curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L) != CURLE_OK) {
192
46
    return CURLE_FAILED_INIT;
193
46
  }
194
195
476
  mock->SetScripts(scenario);
196
476
  CURLMcode add_easy_result = curl_multi_add_handle(multi.get(), easy);
197
476
  CURLMcode add_duplicate_result = CURLM_BAD_HANDLE;
198
476
  CURLMcode perform_result = CURLM_BAD_HANDLE;
199
476
  CURLMcode remove_duplicate_result = CURLM_BAD_HANDLE;
200
476
  bool lookup_started = false;
201
476
  bool duplicate_resolver_queued = false;
202
476
  bool resolver_worker_idle = false;
203
476
  int still_running = 0;
204
476
  if (add_easy_result == CURLM_OK) {
205
476
    ScopedResolverCancelGate gate;
206
476
    perform_result = curl_multi_perform(multi.get(), &still_running);
207
476
    if (perform_result == CURLM_OK && still_running > 0) {
208
462
      lookup_started = WaitForResolverCancelLookup(kResolverCancelGateWait);
209
462
    }
210
    // The signal is sent from inside Q1's getaddrinfo. Q1 is worker-owned,
211
    // and the only worker cannot return to the queue to take original Q2.
212
476
    if (lookup_started) {
213
460
      add_duplicate_result = curl_multi_add_handle(multi.get(), duplicate.get());
214
460
    }
215
476
    if (add_duplicate_result == CURLM_OK) {
216
460
      perform_result = curl_multi_perform(multi.get(), &still_running);
217
460
    }
218
    // Resolver-start precedes the two sends, but the public perform call does
219
    // not return until Curl_async_getaddrinfo has submitted them. Since Q1 is
220
    // still blocked, a live duplicate here has queue-owned work to cancel.
221
476
    duplicate_resolver_queued = perform_result == CURLM_OK && still_running == 2 && duplicate_resolver_starts > 0;
222
    // Always detach a successfully added duplicate before its easy handle can
223
    // be destroyed. On the proved path this is the intentional cancellation;
224
    // on a failed proof it is bounded cleanup while Q1 still owns the worker.
225
476
    if (add_duplicate_result == CURLM_OK) {
226
460
      remove_duplicate_result = curl_multi_remove_handle(multi.get(), duplicate.get());
227
460
    }
228
476
  }
229
230
  // Releasing the gate lets the worker finish the original handle's two
231
  // queries. Wait on curl's public internal wakeup and perform until the
232
  // THREADS trace reports that the pool is idle. Curl emits that observation
233
  // while holding the pool lock; the main thread can acquire it only after the
234
  // worker has entered Curl_cond_timedwait and released the lock. The trace is
235
  // therefore the proof, while the poll timeout merely bounds a failed run.
236
476
  if (lookup_started && duplicate_resolver_queued && remove_duplicate_result == CURLM_OK) {
237
440
    idle_trace.observe_idle = true;
238
2.20k
    for (int attempt = 0; attempt < kResolverIdlePollAttempts && !idle_trace.saw_idle; ++attempt) {
239
1.76k
      int numfds = 0;
240
1.76k
      const CURLMcode poll_result = curl_multi_poll(multi.get(), nullptr, 0, kResolverIdlePollTimeoutMs, &numfds);
241
1.76k
      if (poll_result != CURLM_OK) {
242
0
        perform_result = poll_result;
243
0
        break;
244
0
      }
245
1.76k
      perform_result = curl_multi_perform(multi.get(), &still_running);
246
1.76k
      if (perform_result != CURLM_OK) {
247
0
        break;
248
0
      }
249
1.76k
    }
250
440
    resolver_worker_idle = idle_trace.saw_idle;
251
440
  }
252
253
476
  const CURLMcode remove_easy_result =
254
476
      add_easy_result == CURLM_OK ? curl_multi_remove_handle(multi.get(), easy) : CURLM_BAD_HANDLE;
255
476
  return lookup_started && duplicate_resolver_queued && resolver_worker_idle && perform_result == CURLM_OK &&
256
0
                 remove_duplicate_result == CURLM_OK && remove_easy_result == CURLM_OK
257
476
             ? CURLE_OK
258
476
             : CURLE_FAILED_INIT;
259
522
}
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::RunQueuedResolverCancellation(void*, proto_fuzzer::MockServer*, curl::fuzzer::proto::Scenario const&)
Line
Count
Source
167
261
CURLcode RunQueuedResolverCancellation(CURL* easy, MockServer* mock, const curl::fuzzer::proto::Scenario& scenario) {
168
261
  int duplicate_resolver_starts = 0;
169
261
  ResolverIdleTraceState idle_trace(easy);
170
  // Keep callback data alive until both handles are gone. Declaring the
171
  // duplicate before the multi also makes exceptional teardown destroy the
172
  // multi (and detach any handle whose public removal failed) first.
173
261
  CurlEasyPtr duplicate(curl_easy_duphandle(easy));
174
261
  CurlMultiPtr multi(curl_multi_init());
175
  // This drive is bounded by the gate wait and explicit removals. Clear the
176
  // ordinary 200 ms transfer/connect settings (zero selects no operation
177
  // deadline and curl's default connect deadline) so host scheduling cannot
178
  // cancel either handle before the queue invariant is established.
179
261
  if (multi == nullptr || duplicate == nullptr ||
180
239
      curl_multi_setopt(multi.get(), CURLMOPT_RESOLVE_THREADS_MAX, 1L) != CURLM_OK ||
181
239
      curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, 0L) != CURLE_OK ||
182
239
      curl_easy_setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, 0L) != CURLE_OK ||
183
239
      curl_easy_setopt(duplicate.get(), CURLOPT_TIMEOUT_MS, 0L) != CURLE_OK ||
184
239
      curl_easy_setopt(duplicate.get(), CURLOPT_CONNECTTIMEOUT_MS, 0L) != CURLE_OK ||
185
239
      curl_easy_setopt(duplicate.get(), CURLOPT_URL, kResolverCancelDuplicateUrl) != CURLE_OK ||
186
238
      curl_easy_setopt(duplicate.get(), CURLOPT_FRESH_CONNECT, 1L) != CURLE_OK ||
187
238
      curl_easy_setopt(duplicate.get(), CURLOPT_RESOLVER_START_FUNCTION, &CountResolverStart) != CURLE_OK ||
188
238
      curl_easy_setopt(duplicate.get(), CURLOPT_RESOLVER_START_DATA, &duplicate_resolver_starts) != CURLE_OK ||
189
238
      curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, &ObserveResolverIdleTrace) != CURLE_OK ||
190
238
      curl_easy_setopt(easy, CURLOPT_DEBUGDATA, &idle_trace) != CURLE_OK ||
191
238
      curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L) != CURLE_OK) {
192
23
    return CURLE_FAILED_INIT;
193
23
  }
194
195
238
  mock->SetScripts(scenario);
196
238
  CURLMcode add_easy_result = curl_multi_add_handle(multi.get(), easy);
197
238
  CURLMcode add_duplicate_result = CURLM_BAD_HANDLE;
198
238
  CURLMcode perform_result = CURLM_BAD_HANDLE;
199
238
  CURLMcode remove_duplicate_result = CURLM_BAD_HANDLE;
200
238
  bool lookup_started = false;
201
238
  bool duplicate_resolver_queued = false;
202
238
  bool resolver_worker_idle = false;
203
238
  int still_running = 0;
204
238
  if (add_easy_result == CURLM_OK) {
205
238
    ScopedResolverCancelGate gate;
206
238
    perform_result = curl_multi_perform(multi.get(), &still_running);
207
238
    if (perform_result == CURLM_OK && still_running > 0) {
208
231
      lookup_started = WaitForResolverCancelLookup(kResolverCancelGateWait);
209
231
    }
210
    // The signal is sent from inside Q1's getaddrinfo. Q1 is worker-owned,
211
    // and the only worker cannot return to the queue to take original Q2.
212
238
    if (lookup_started) {
213
230
      add_duplicate_result = curl_multi_add_handle(multi.get(), duplicate.get());
214
230
    }
215
238
    if (add_duplicate_result == CURLM_OK) {
216
230
      perform_result = curl_multi_perform(multi.get(), &still_running);
217
230
    }
218
    // Resolver-start precedes the two sends, but the public perform call does
219
    // not return until Curl_async_getaddrinfo has submitted them. Since Q1 is
220
    // still blocked, a live duplicate here has queue-owned work to cancel.
221
238
    duplicate_resolver_queued = perform_result == CURLM_OK && still_running == 2 && duplicate_resolver_starts > 0;
222
    // Always detach a successfully added duplicate before its easy handle can
223
    // be destroyed. On the proved path this is the intentional cancellation;
224
    // on a failed proof it is bounded cleanup while Q1 still owns the worker.
225
238
    if (add_duplicate_result == CURLM_OK) {
226
230
      remove_duplicate_result = curl_multi_remove_handle(multi.get(), duplicate.get());
227
230
    }
228
238
  }
229
230
  // Releasing the gate lets the worker finish the original handle's two
231
  // queries. Wait on curl's public internal wakeup and perform until the
232
  // THREADS trace reports that the pool is idle. Curl emits that observation
233
  // while holding the pool lock; the main thread can acquire it only after the
234
  // worker has entered Curl_cond_timedwait and released the lock. The trace is
235
  // therefore the proof, while the poll timeout merely bounds a failed run.
236
238
  if (lookup_started && duplicate_resolver_queued && remove_duplicate_result == CURLM_OK) {
237
220
    idle_trace.observe_idle = true;
238
1.10k
    for (int attempt = 0; attempt < kResolverIdlePollAttempts && !idle_trace.saw_idle; ++attempt) {
239
880
      int numfds = 0;
240
880
      const CURLMcode poll_result = curl_multi_poll(multi.get(), nullptr, 0, kResolverIdlePollTimeoutMs, &numfds);
241
880
      if (poll_result != CURLM_OK) {
242
0
        perform_result = poll_result;
243
0
        break;
244
0
      }
245
880
      perform_result = curl_multi_perform(multi.get(), &still_running);
246
880
      if (perform_result != CURLM_OK) {
247
0
        break;
248
0
      }
249
880
    }
250
220
    resolver_worker_idle = idle_trace.saw_idle;
251
220
  }
252
253
238
  const CURLMcode remove_easy_result =
254
238
      add_easy_result == CURLM_OK ? curl_multi_remove_handle(multi.get(), easy) : CURLM_BAD_HANDLE;
255
238
  return lookup_started && duplicate_resolver_queued && resolver_worker_idle && perform_result == CURLM_OK &&
256
0
                 remove_duplicate_result == CURLM_OK && remove_easy_result == CURLM_OK
257
238
             ? CURLE_OK
258
238
             : CURLE_FAILED_INIT;
259
261
}
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::RunQueuedResolverCancellation(void*, proto_fuzzer::MockServer*, curl::fuzzer::proto::Scenario const&)
Line
Count
Source
167
261
CURLcode RunQueuedResolverCancellation(CURL* easy, MockServer* mock, const curl::fuzzer::proto::Scenario& scenario) {
168
261
  int duplicate_resolver_starts = 0;
169
261
  ResolverIdleTraceState idle_trace(easy);
170
  // Keep callback data alive until both handles are gone. Declaring the
171
  // duplicate before the multi also makes exceptional teardown destroy the
172
  // multi (and detach any handle whose public removal failed) first.
173
261
  CurlEasyPtr duplicate(curl_easy_duphandle(easy));
174
261
  CurlMultiPtr multi(curl_multi_init());
175
  // This drive is bounded by the gate wait and explicit removals. Clear the
176
  // ordinary 200 ms transfer/connect settings (zero selects no operation
177
  // deadline and curl's default connect deadline) so host scheduling cannot
178
  // cancel either handle before the queue invariant is established.
179
261
  if (multi == nullptr || duplicate == nullptr ||
180
239
      curl_multi_setopt(multi.get(), CURLMOPT_RESOLVE_THREADS_MAX, 1L) != CURLM_OK ||
181
239
      curl_easy_setopt(easy, CURLOPT_TIMEOUT_MS, 0L) != CURLE_OK ||
182
239
      curl_easy_setopt(easy, CURLOPT_CONNECTTIMEOUT_MS, 0L) != CURLE_OK ||
183
239
      curl_easy_setopt(duplicate.get(), CURLOPT_TIMEOUT_MS, 0L) != CURLE_OK ||
184
239
      curl_easy_setopt(duplicate.get(), CURLOPT_CONNECTTIMEOUT_MS, 0L) != CURLE_OK ||
185
239
      curl_easy_setopt(duplicate.get(), CURLOPT_URL, kResolverCancelDuplicateUrl) != CURLE_OK ||
186
238
      curl_easy_setopt(duplicate.get(), CURLOPT_FRESH_CONNECT, 1L) != CURLE_OK ||
187
238
      curl_easy_setopt(duplicate.get(), CURLOPT_RESOLVER_START_FUNCTION, &CountResolverStart) != CURLE_OK ||
188
238
      curl_easy_setopt(duplicate.get(), CURLOPT_RESOLVER_START_DATA, &duplicate_resolver_starts) != CURLE_OK ||
189
238
      curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, &ObserveResolverIdleTrace) != CURLE_OK ||
190
238
      curl_easy_setopt(easy, CURLOPT_DEBUGDATA, &idle_trace) != CURLE_OK ||
191
238
      curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L) != CURLE_OK) {
192
23
    return CURLE_FAILED_INIT;
193
23
  }
194
195
238
  mock->SetScripts(scenario);
196
238
  CURLMcode add_easy_result = curl_multi_add_handle(multi.get(), easy);
197
238
  CURLMcode add_duplicate_result = CURLM_BAD_HANDLE;
198
238
  CURLMcode perform_result = CURLM_BAD_HANDLE;
199
238
  CURLMcode remove_duplicate_result = CURLM_BAD_HANDLE;
200
238
  bool lookup_started = false;
201
238
  bool duplicate_resolver_queued = false;
202
238
  bool resolver_worker_idle = false;
203
238
  int still_running = 0;
204
238
  if (add_easy_result == CURLM_OK) {
205
238
    ScopedResolverCancelGate gate;
206
238
    perform_result = curl_multi_perform(multi.get(), &still_running);
207
238
    if (perform_result == CURLM_OK && still_running > 0) {
208
231
      lookup_started = WaitForResolverCancelLookup(kResolverCancelGateWait);
209
231
    }
210
    // The signal is sent from inside Q1's getaddrinfo. Q1 is worker-owned,
211
    // and the only worker cannot return to the queue to take original Q2.
212
238
    if (lookup_started) {
213
230
      add_duplicate_result = curl_multi_add_handle(multi.get(), duplicate.get());
214
230
    }
215
238
    if (add_duplicate_result == CURLM_OK) {
216
230
      perform_result = curl_multi_perform(multi.get(), &still_running);
217
230
    }
218
    // Resolver-start precedes the two sends, but the public perform call does
219
    // not return until Curl_async_getaddrinfo has submitted them. Since Q1 is
220
    // still blocked, a live duplicate here has queue-owned work to cancel.
221
238
    duplicate_resolver_queued = perform_result == CURLM_OK && still_running == 2 && duplicate_resolver_starts > 0;
222
    // Always detach a successfully added duplicate before its easy handle can
223
    // be destroyed. On the proved path this is the intentional cancellation;
224
    // on a failed proof it is bounded cleanup while Q1 still owns the worker.
225
238
    if (add_duplicate_result == CURLM_OK) {
226
230
      remove_duplicate_result = curl_multi_remove_handle(multi.get(), duplicate.get());
227
230
    }
228
238
  }
229
230
  // Releasing the gate lets the worker finish the original handle's two
231
  // queries. Wait on curl's public internal wakeup and perform until the
232
  // THREADS trace reports that the pool is idle. Curl emits that observation
233
  // while holding the pool lock; the main thread can acquire it only after the
234
  // worker has entered Curl_cond_timedwait and released the lock. The trace is
235
  // therefore the proof, while the poll timeout merely bounds a failed run.
236
238
  if (lookup_started && duplicate_resolver_queued && remove_duplicate_result == CURLM_OK) {
237
220
    idle_trace.observe_idle = true;
238
1.10k
    for (int attempt = 0; attempt < kResolverIdlePollAttempts && !idle_trace.saw_idle; ++attempt) {
239
880
      int numfds = 0;
240
880
      const CURLMcode poll_result = curl_multi_poll(multi.get(), nullptr, 0, kResolverIdlePollTimeoutMs, &numfds);
241
880
      if (poll_result != CURLM_OK) {
242
0
        perform_result = poll_result;
243
0
        break;
244
0
      }
245
880
      perform_result = curl_multi_perform(multi.get(), &still_running);
246
880
      if (perform_result != CURLM_OK) {
247
0
        break;
248
0
      }
249
880
    }
250
220
    resolver_worker_idle = idle_trace.saw_idle;
251
220
  }
252
253
238
  const CURLMcode remove_easy_result =
254
238
      add_easy_result == CURLM_OK ? curl_multi_remove_handle(multi.get(), easy) : CURLM_BAD_HANDLE;
255
238
  return lookup_started && duplicate_resolver_queued && resolver_worker_idle && perform_result == CURLM_OK &&
256
0
                 remove_duplicate_result == CURLM_OK && remove_easy_result == CURLM_OK
257
238
             ? CURLE_OK
258
238
             : CURLE_FAILED_INIT;
259
261
}
260
261
/// Probe each public getinfo return family and the response-header API after
262
/// curl has settled the transfer. Applications commonly inspect these APIs,
263
/// but a harness that only drives I/O leaves their type dispatch and
264
/// post-transfer state unexecuted even when the corresponding parser ran.
265
/// The chosen values are handle-owned or scalar: notably CERTINFO exercises
266
/// the pointer/slist dispatch family without materialising a separately-owned
267
/// cookie/engine list. Header iteration is capped independently of response
268
/// size so this unconditional coverage cannot dominate a fuzz iteration.
269
541k
void ProbeTransferResults(CURL* easy) {
270
541k
  char* string_result = nullptr;
271
541k
  long long_result = 0;
272
541k
  double double_result = 0;
273
541k
  curl_off_t offset_result = 0;
274
541k
  curl_socket_t socket_result = CURL_SOCKET_BAD;
275
541k
  struct curl_certinfo* certinfo_result = nullptr;
276
277
541k
  (void)curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &string_result);
278
541k
  (void)curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &long_result);
279
541k
  (void)curl_easy_getinfo(easy, CURLINFO_TOTAL_TIME, &double_result);
280
541k
  (void)curl_easy_getinfo(easy, CURLINFO_SIZE_DOWNLOAD_T, &offset_result);
281
541k
  (void)curl_easy_getinfo(easy, CURLINFO_ACTIVESOCKET, &socket_result);
282
541k
  (void)curl_easy_getinfo(easy, CURLINFO_CERTINFO, &certinfo_result);
283
284
541k
  struct curl_header* header = nullptr;
285
541k
  (void)curl_easy_header(easy, "Content-Type", 0, kAllHeaderOrigins, -1, &header);
286
541k
  header = nullptr;
287
1.16M
  for (std::size_t index = 0; index < kMaxResultHeaders; ++index) {
288
1.14M
    header = curl_easy_nextheader(easy, kAllHeaderOrigins, -1, header);
289
1.14M
    if (header == nullptr) {
290
524k
      break;
291
524k
    }
292
1.14M
  }
293
541k
}
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::ProbeTransferResults(void*)
Line
Count
Source
269
270k
void ProbeTransferResults(CURL* easy) {
270
270k
  char* string_result = nullptr;
271
270k
  long long_result = 0;
272
270k
  double double_result = 0;
273
270k
  curl_off_t offset_result = 0;
274
270k
  curl_socket_t socket_result = CURL_SOCKET_BAD;
275
270k
  struct curl_certinfo* certinfo_result = nullptr;
276
277
270k
  (void)curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &string_result);
278
270k
  (void)curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &long_result);
279
270k
  (void)curl_easy_getinfo(easy, CURLINFO_TOTAL_TIME, &double_result);
280
270k
  (void)curl_easy_getinfo(easy, CURLINFO_SIZE_DOWNLOAD_T, &offset_result);
281
270k
  (void)curl_easy_getinfo(easy, CURLINFO_ACTIVESOCKET, &socket_result);
282
270k
  (void)curl_easy_getinfo(easy, CURLINFO_CERTINFO, &certinfo_result);
283
284
270k
  struct curl_header* header = nullptr;
285
270k
  (void)curl_easy_header(easy, "Content-Type", 0, kAllHeaderOrigins, -1, &header);
286
270k
  header = nullptr;
287
581k
  for (std::size_t index = 0; index < kMaxResultHeaders; ++index) {
288
572k
    header = curl_easy_nextheader(easy, kAllHeaderOrigins, -1, header);
289
572k
    if (header == nullptr) {
290
262k
      break;
291
262k
    }
292
572k
  }
293
270k
}
scenario_runner.cc:proto_fuzzer::(anonymous namespace)::ProbeTransferResults(void*)
Line
Count
Source
269
270k
void ProbeTransferResults(CURL* easy) {
270
270k
  char* string_result = nullptr;
271
270k
  long long_result = 0;
272
270k
  double double_result = 0;
273
270k
  curl_off_t offset_result = 0;
274
270k
  curl_socket_t socket_result = CURL_SOCKET_BAD;
275
270k
  struct curl_certinfo* certinfo_result = nullptr;
276
277
270k
  (void)curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &string_result);
278
270k
  (void)curl_easy_getinfo(easy, CURLINFO_RESPONSE_CODE, &long_result);
279
270k
  (void)curl_easy_getinfo(easy, CURLINFO_TOTAL_TIME, &double_result);
280
270k
  (void)curl_easy_getinfo(easy, CURLINFO_SIZE_DOWNLOAD_T, &offset_result);
281
270k
  (void)curl_easy_getinfo(easy, CURLINFO_ACTIVESOCKET, &socket_result);
282
270k
  (void)curl_easy_getinfo(easy, CURLINFO_CERTINFO, &certinfo_result);
283
284
270k
  struct curl_header* header = nullptr;
285
270k
  (void)curl_easy_header(easy, "Content-Type", 0, kAllHeaderOrigins, -1, &header);
286
270k
  header = nullptr;
287
581k
  for (std::size_t index = 0; index < kMaxResultHeaders; ++index) {
288
572k
    header = curl_easy_nextheader(easy, kAllHeaderOrigins, -1, header);
289
572k
    if (header == nullptr) {
290
262k
      break;
291
262k
    }
292
572k
  }
293
270k
}
294
295
/// Map a Scheme enum to the URL scheme literal.
296
314k
const char* SchemePrefix(curl::fuzzer::proto::Scheme scheme) {
297
314k
  switch (scheme) {
298
142k
    case curl::fuzzer::proto::SCHEME_HTTP:
299
142k
      return "http";
300
64.6k
    case curl::fuzzer::proto::SCHEME_HTTPS:
301
64.6k
      return "https";
302
17.1k
    case curl::fuzzer::proto::SCHEME_WS:
303
17.1k
      return "ws";
304
10.8k
    case curl::fuzzer::proto::SCHEME_WSS:
305
10.8k
      return "wss";
306
5.72k
    case curl::fuzzer::proto::SCHEME_TELNET:
307
5.72k
      return "telnet";
308
5.45k
    case curl::fuzzer::proto::SCHEME_FTP:
309
5.45k
      return "ftp";
310
2.03k
    case curl::fuzzer::proto::SCHEME_FTPS:
311
2.03k
      return "ftps";
312
2.39k
    case curl::fuzzer::proto::SCHEME_TFTP:
313
2.39k
      return "tftp";
314
2.54k
    case curl::fuzzer::proto::SCHEME_SMTP:
315
2.54k
      return "smtp";
316
2.12k
    case curl::fuzzer::proto::SCHEME_IMAP:
317
2.12k
      return "imap";
318
4.36k
    case curl::fuzzer::proto::SCHEME_IMAPS:
319
4.36k
      return "imaps";
320
975
    case curl::fuzzer::proto::SCHEME_POP3:
321
975
      return "pop3";
322
3.17k
    case curl::fuzzer::proto::SCHEME_POP3S:
323
3.17k
      return "pop3s";
324
739
    case curl::fuzzer::proto::SCHEME_MQTT:
325
739
      return "mqtt";
326
3.47k
    case curl::fuzzer::proto::SCHEME_MQTTS:
327
3.47k
      return "mqtts";
328
2.16k
    case curl::fuzzer::proto::SCHEME_LDAP:
329
2.16k
      return "ldap";
330
4.53k
    case curl::fuzzer::proto::SCHEME_LDAPS:
331
4.53k
      return "ldaps";
332
7.85k
    case curl::fuzzer::proto::SCHEME_RTSP:
333
7.85k
      return "rtsp";
334
12.0k
    case curl::fuzzer::proto::SCHEME_IPFS:
335
12.0k
      return "ipfs";
336
14
    case curl::fuzzer::proto::SCHEME_IPNS:
337
14
      return "ipns";
338
3.48k
    case curl::fuzzer::proto::SCHEME_SMTPS:
339
3.48k
      return "smtps";
340
3.04k
    case curl::fuzzer::proto::SCHEME_DICT:
341
3.04k
      return "dict";
342
4.58k
    case curl::fuzzer::proto::SCHEME_GOPHER:
343
4.58k
      return "gopher";
344
4.38k
    case curl::fuzzer::proto::SCHEME_GOPHERS:
345
4.38k
      return "gophers";
346
4.13k
    case curl::fuzzer::proto::SCHEME_FILE:
347
4.13k
      return "file";
348
395
    case curl::fuzzer::proto::SCHEME_UNSPECIFIED:
349
400
    default:
350
400
      return nullptr;
351
314k
  }
352
314k
}
353
354
/// Build the URL curl actually receives. For IPFS/IPNS this reproduces curl's
355
/// command-line gateway rewrite: `<gateway>/<protocol>/<cid-or-name><suffix>`.
356
/// The gateway itself is the fixed loopback authority above; host_path already
357
/// holds the canonical CID/name followed by the mutation-controlled suffix.
358
/// @param scenario Scenario whose scheme and gateway path select the URL.
359
/// @param prefix Scheme literal returned by SchemePrefix().
360
/// @return Effective URL before CONNECT_TO and OPENSOCKET confinement.
361
307k
std::string ScenarioUrl(const curl::fuzzer::proto::Scenario& scenario, const char* prefix) {
362
307k
  if (scenario.scheme() == curl::fuzzer::proto::SCHEME_IPFS || scenario.scheme() == curl::fuzzer::proto::SCHEME_IPNS) {
363
12.0k
    return BuildIpfsGatewayUrl(scenario.scheme(), scenario.host_path());
364
12.0k
  }
365
295k
  return std::string(prefix) + "://" + scenario.host_path();
366
307k
}
367
368
/// Detect the ECH modes that ask the TLS backend to obtain its configuration
369
/// from an HTTPS resource record. A literal ECHConfigList already supplies the
370
/// configuration, while GREASE deliberately does not wait for DNS.
371
5
bool EchNeedsPendingHttpsRr(const curl::fuzzer::proto::Scenario& scenario) {
372
5
  const std::size_t option_count = RuntimeOptionCount(scenario);
373
5
  for (std::size_t index = 0; index < option_count; ++index) {
374
1
    const auto& option = scenario.options(static_cast<int>(index));
375
1
    if (option.option_id() != curl::fuzzer::proto::CURLOPT_ECH) {
376
0
      continue;
377
0
    }
378
1
    const std::string& value = option.string_value();
379
1
    if (value == "true" || value == "hard" || value.rfind("pn:", 0) == 0) {
380
1
      return true;
381
1
    }
382
1
  }
383
4
  return false;
384
5
}
385
386
/// Pick the peer implementation authorized by both protocol and target mode.
387
/// The compatibility target must keep its historical raw-wire interpretation
388
/// of secure schemes, while dedicated TLS lanes expose decrypted application
389
/// protocols. Keeping that semantic boundary in the closed run-mode enum
390
/// prevents a new peer from silently changing old OSS-Fuzz reproducers.
391
std::unique_ptr<MockServerBase> MakeMockServerForScenario(const curl::fuzzer::proto::Scenario& scenario,
392
304k
                                                          ScenarioRunMode mode) {
393
304k
  if (mode == ScenarioRunMode::kHttp3Coverage) {
394
#if defined(PROTO_FUZZER_HAS_HTTP3_MOCK_SERVER)
395
    if (scenario.http3_plan().use_h1_connect_udp_proxy()) {
396
      return std::make_unique<ConnectUdpProxyMockServer>();
397
    }
398
    return std::make_unique<Http3MockServer>(scenario.tls_certificate_chain());
399
#else
400
0
    return nullptr;
401
0
#endif
402
0
  }
403
404
304k
  if (mode == ScenarioRunMode::kH2ProxyCoverage) {
405
13.5k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
406
13.5k
    return std::make_unique<H2ProxyMockServer>(
407
13.5k
        scenario.h2_forward_proxy() ? H2ProxyMode::kForward : H2ProxyMode::kTunnel,
408
13.5k
        scenario.connection().proxy_protocol());
409
#else
410
    // MemorySanitizer builds deliberately omit OpenSSL. Keep the target
411
    // binary available to OSS-Fuzz, but do not pretend a plaintext mock can
412
    // negotiate the ALPN gate required to enter cf-h2-proxy.
413
    return nullptr;
414
#endif
415
13.5k
  }
416
417
291k
  if (mode == ScenarioRunMode::kHttp2Coverage) {
418
17.0k
    return std::make_unique<H2CleartextMockServer>(scenario.http2_connect_flush_probe());
419
17.0k
  }
420
421
274k
  if (mode == ScenarioRunMode::kTlsHttp2Coverage) {
422
14.7k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
423
14.7k
    return std::make_unique<H2OriginMockServer>(scenario.tls_certificate_chain(), scenario.tls_group_profile());
424
#else
425
    // This target remains buildable under MemorySanitizer, whose curl build
426
    // omits the OpenSSL server dependency required for TLS/ALPN h2.
427
    return nullptr;
428
#endif
429
14.7k
  }
430
431
259k
  if (mode == ScenarioRunMode::kProxyTransportCoverage) {
432
14.1k
    return std::make_unique<ProxyTransportMockServer>(scenario.socks_proxy_mode(), scenario.haproxy_protocol());
433
14.1k
  }
434
435
245k
  if (mode == ScenarioRunMode::kDeepHttpCoverage) {
436
21.5k
    if (scenario.proxy_socket_option_mode() == curl::fuzzer::proto::PROXY_SOCKET_OPTION_LOOPBACK_INTERFACE_KEEPALIVE) {
437
159
      return std::make_unique<InterfaceOptionMockServer>();
438
159
    }
439
21.4k
    if (scenario.proxy_socket_option_mode() != curl::fuzzer::proto::PROXY_SOCKET_OPTION_DEFAULT) {
440
5.70k
      return std::make_unique<ProxyOptionMockServer>(scenario.proxy_socket_option_mode());
441
5.70k
    }
442
15.7k
    if (scenario.http_transport_mode() != curl::fuzzer::proto::HTTP_TRANSPORT_DIRECT) {
443
2.28k
      return std::make_unique<HttpTransportMockServer>(scenario.http_transport_mode(), scenario.haproxy_protocol());
444
2.28k
    }
445
15.7k
  }
446
447
236k
  if (mode == ScenarioRunMode::kResolverCoverage && scenario.has_doh()) {
448
    // The DoH responder answers both the address and HTTPS resource-record
449
    // queries; its TLS/ALPN h2 carrier then completes the outer HTTPS
450
    // transfer selected from the record.
451
5
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
452
5
    return std::make_unique<DohHttpsRrMockServer>(scenario.tls_certificate_chain(), scenario.doh().response_mode(),
453
5
                                                  EchNeedsPendingHttpsRr(scenario));
454
#else
455
    // MemorySanitizer builds deliberately omit OpenSSL, so no verified TLS
456
    // peer exists for the HTTPS-RR lane. Keep the target available while
457
    // refusing to fall through to an unrelated transport.
458
    return nullptr;
459
#endif
460
5
  }
461
462
236k
  if (mode == ScenarioRunMode::kResolverCoverage && scenario.happy_eyeballs()) {
463
    // The loopback race peer returns real unconnected sockets, so curl's
464
    // happy-eyeballs filter can keep several connect attempts in progress
465
    // instead of accepting the first preconnected socketpair.
466
39
    return std::make_unique<HappyEyeballsMockServer>();
467
39
  }
468
469
236k
  switch (scenario.scheme()) {
470
88.2k
    case curl::fuzzer::proto::SCHEME_HTTP:
471
88.2k
      if (mode == ScenarioRunMode::kApiLifecycle) {
472
19.6k
        return std::make_unique<MockServer>(MultiDrivePolicy::kFromApiPlan);
473
19.6k
      }
474
68.5k
      return std::make_unique<MockServer>();
475
42.1k
    case curl::fuzzer::proto::SCHEME_HTTPS:
476
42.1k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
477
42.1k
      if (mode == ScenarioRunMode::kTlsCoverage) {
478
40.6k
        return std::make_unique<TlsMockServer>(scenario.tls_certificate_chain(), scenario.tls_handshake_profile());
479
40.6k
      }
480
#else
481
      (void)mode;
482
#endif
483
1.49k
      return std::make_unique<MockServer>();
484
4.58k
    case curl::fuzzer::proto::SCHEME_GOPHER:
485
4.58k
      return mode == ScenarioRunMode::kGopherCoverage ? std::make_unique<GopherMockServer>(scenario.gopher_probe_mode())
486
4.58k
                                                      : nullptr;
487
4.38k
    case curl::fuzzer::proto::SCHEME_GOPHERS:
488
4.38k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
489
4.38k
      if (mode == ScenarioRunMode::kGopherCoverage) {
490
4.38k
        return std::make_unique<TlsMockServer>(scenario.tls_certificate_chain());
491
4.38k
      }
492
2
      return nullptr;
493
#else
494
      return nullptr;
495
#endif
496
16.7k
    case curl::fuzzer::proto::SCHEME_WS:
497
16.7k
      return std::make_unique<WebSocketMockServer>();
498
10.8k
    case curl::fuzzer::proto::SCHEME_WSS:
499
10.8k
      if (mode == ScenarioRunMode::kTlsWebSocketCoverage) {
500
9.88k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
501
9.88k
        return std::make_unique<TlsWebSocketMockServer>(scenario.tls_certificate_chain());
502
#else
503
        // MemorySanitizer builds deliberately omit OpenSSL. Keep the target
504
        // available without pretending plaintext can carry a WSS exchange.
505
        return nullptr;
506
#endif
507
9.88k
      }
508
      // Preserve the compatibility target's historical interpretation of WSS
509
      // inputs, which exposed TLS bytes directly to the plaintext WS peer.
510
928
      return std::make_unique<WebSocketMockServer>();
511
5.40k
    case curl::fuzzer::proto::SCHEME_TELNET:
512
5.40k
      return std::make_unique<TelnetMockServer>();
513
5.45k
    case curl::fuzzer::proto::SCHEME_FTP:
514
      // New numeric enum values may already occur in the historical mixed
515
      // corpus as unknown fields. Only the fixed FTP profile may reinterpret
516
      // one as a live two-channel protocol exchange.
517
5.45k
      if (mode == ScenarioRunMode::kFtpCoverage) {
518
5.44k
        return std::make_unique<FtpMockServer>();
519
5.44k
      }
520
3
      return nullptr;
521
2.03k
    case curl::fuzzer::proto::SCHEME_FTPS:
522
      // Implicit TLS shares the FTP lane, parser, and bounded data-channel
523
      // budget with the plaintext scheme; only the transport factory and the
524
      // passive-only restriction differ. Keep it gated on the same run mode so
525
      // no other target can turn an appended enum value into a TLS exchange.
526
2.03k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
527
2.03k
      if (mode == ScenarioRunMode::kFtpCoverage) {
528
2.02k
        return std::make_unique<FtpsMockServer>();
529
2.02k
      }
530
3
#endif
531
3
      return nullptr;
532
2.38k
    case curl::fuzzer::proto::SCHEME_TFTP:
533
      // TFTP changes the callback transport from a preconnected stream to a
534
      // real UDP endpoint, so compatibility inputs must not opt into it merely
535
      // because this build learned a new enum value.
536
2.38k
      if (mode == ScenarioRunMode::kTftpCoverage) {
537
2.38k
        return std::make_unique<TftpMockServer>();
538
2.38k
      }
539
1
      return nullptr;
540
2.54k
    case curl::fuzzer::proto::SCHEME_SMTP:
541
      // SMTP reuses the stream socketpair but needs command-aligned replies
542
      // and a DATA body drain. Only the dedicated lane may reinterpret an
543
      // append-only scheme value that historical corpus inputs could carry.
544
2.54k
      if (mode == ScenarioRunMode::kSmtpCoverage) {
545
2.54k
        return std::make_unique<SmtpMockServer>(scenario.smtp_probe_mode());
546
2.54k
      }
547
1
      return nullptr;
548
2.12k
    case curl::fuzzer::proto::SCHEME_IMAP:
549
      // IMAP replies are tag-aligned. Only the dedicated lane may reinterpret
550
      // an append-only scheme value that historical inputs could carry.
551
2.12k
      if (mode == ScenarioRunMode::kImapCoverage) {
552
2.11k
        return std::make_unique<ImapMockServer>();
553
2.11k
      }
554
3
      return nullptr;
555
974
    case curl::fuzzer::proto::SCHEME_POP3:
556
974
      if (mode == ScenarioRunMode::kPop3Coverage) {
557
973
        return std::make_unique<Pop3MockServer>();
558
973
      }
559
1
      return nullptr;
560
738
    case curl::fuzzer::proto::SCHEME_MQTT:
561
      // MQTT reuses the stream socketpair but speaks a binary packet grammar
562
      // with server-initiated packets. Only the dedicated lane may
563
      // reinterpret an append-only scheme value that historical corpus
564
      // inputs could carry.
565
738
      if (mode == ScenarioRunMode::kMqttCoverage) {
566
736
        return std::make_unique<MqttMockServer>(scenario.mqtt_probe_mode());
567
736
      }
568
2
      return nullptr;
569
3.04k
    case curl::fuzzer::proto::SCHEME_DICT:
570
      // DICT sends its command sequence in one write and reads an EOF-framed
571
      // text body. Only the dedicated lane may reinterpret an append-only
572
      // scheme value that historical inputs could carry.
573
3.04k
      if (mode == ScenarioRunMode::kDictCoverage) {
574
3.03k
        return std::make_unique<DictMockServer>();
575
3.03k
      }
576
3
      return nullptr;
577
3.48k
    case curl::fuzzer::proto::SCHEME_SMTPS:
578
      // Implicit TLS shares the SMTP lane, parser, and command budget with the
579
      // plaintext scheme; only the transport factory differs. Keep it gated on
580
      // the same fixed run mode so no other target can turn an appended enum
581
      // value into a TLS exchange.
582
3.48k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
583
3.48k
      if (mode == ScenarioRunMode::kSmtpCoverage) {
584
3.48k
        return std::make_unique<SmtpsMockServer>();
585
3.48k
      }
586
3
#endif
587
3
      return nullptr;
588
4.36k
    case curl::fuzzer::proto::SCHEME_IMAPS:
589
      // Implicit TLS reuses the IMAP lane and tag-aware parser; only the
590
      // transport factory differs. Gate it on the same fixed run mode so no
591
      // other target can turn the appended enum value into a TLS exchange.
592
4.36k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
593
4.36k
      if (mode == ScenarioRunMode::kImapCoverage) {
594
4.36k
        return std::make_unique<ImapsMockServer>();
595
4.36k
      }
596
2
#endif
597
2
      return nullptr;
598
3.17k
    case curl::fuzzer::proto::SCHEME_POP3S:
599
      // Implicit TLS reuses the POP3 lane and status-line parser; only the
600
      // transport factory differs. Gate it on the same fixed run mode so no
601
      // other target can turn the appended enum value into a TLS exchange.
602
3.17k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
603
3.17k
      if (mode == ScenarioRunMode::kPop3Coverage) {
604
3.17k
        return std::make_unique<Pop3sMockServer>();
605
3.17k
      }
606
1
#endif
607
1
      return nullptr;
608
3.47k
    case curl::fuzzer::proto::SCHEME_MQTTS:
609
      // Implicit TLS shares the MQTT lane, packet parser, and server-packet
610
      // budget with the plaintext scheme; only the transport factory differs.
611
      // Keep it gated on the same fixed run mode so no other target can turn
612
      // an appended enum value into a TLS exchange.
613
3.47k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
614
3.47k
      if (mode == ScenarioRunMode::kMqttCoverage) {
615
3.47k
        return std::make_unique<MqttsMockServer>();
616
3.47k
      }
617
1
#endif
618
1
      return nullptr;
619
4.13k
    case curl::fuzzer::proto::SCHEME_FILE:
620
      // FILE reads a session-owned anonymous descriptor directly, so there is
621
      // no socket or scriptable peer to install.
622
4.13k
      return nullptr;
623
2.16k
    case curl::fuzzer::proto::SCHEME_LDAP:
624
      // LDAP is BER/ASN.1 rather than line-oriented and OpenLDAP performs its
625
      // own reads on a duplicate of curl's socket. Only the dedicated lane
626
      // may turn an appended scheme value into a live bind/search exchange.
627
2.16k
      if (mode == ScenarioRunMode::kLdapCoverage) {
628
2.16k
        return std::make_unique<LdapMockServer>();
629
2.16k
      }
630
2
      return nullptr;
631
4.53k
    case curl::fuzzer::proto::SCHEME_LDAPS:
632
      // Implicit TLS shares the LDAP lane, BER parser, and scripted messages
633
      // with the plaintext scheme; only the transport factory differs. Keep it
634
      // gated on the same fixed run mode so no other target can turn an
635
      // appended enum value into a TLS exchange.
636
4.53k
#if defined(PROTO_FUZZER_HAS_TLS_MOCK_SERVER)
637
4.53k
      if (mode == ScenarioRunMode::kLdapCoverage) {
638
4.52k
        return std::make_unique<LdapsMockServer>();
639
4.52k
      }
640
2
#endif
641
2
      return nullptr;
642
7.85k
    case curl::fuzzer::proto::SCHEME_RTSP:
643
      // RTSP is request-aligned rather than greeting-aligned, so the
644
      // dedicated lane is required to select the peer that waits for the
645
      // request terminator before releasing each scripted response.
646
7.85k
      if (mode == ScenarioRunMode::kRtspCoverage) {
647
7.85k
        return std::make_unique<RtspMockServer>();
648
7.85k
      }
649
2
      return nullptr;
650
12.0k
    case curl::fuzzer::proto::SCHEME_IPFS:
651
12.1k
    case curl::fuzzer::proto::SCHEME_IPNS:
652
      // The rewrite above turns these into ordinary HTTP GETs. Only the
653
      // dedicated fast lane may reinterpret an append-only scheme value, and
654
      // its compatibility peer is the plaintext MockServer.
655
12.1k
      if (mode == ScenarioRunMode::kFastProtocol) {
656
12.1k
        return std::make_unique<MockServer>();
657
12.1k
      }
658
2
      return nullptr;
659
0
    case curl::fuzzer::proto::SCHEME_UNSPECIFIED:
660
0
    default:
661
0
      return nullptr;
662
236k
  }
663
236k
}
664
665
}  // namespace
666
667
/// Execute one scenario under a complete target behaviour. An enum makes the
668
/// supported fast, coverage, and API paths explicit and prevents callers from
669
/// constructing meaningless combinations of independent switches.
670
/// @param scenario Structured transfer and optional API plan.
671
/// @param mode Runtime coverage and lifecycle policy for this invocation.
672
/// @return zero after either a bounded run or an ignored invalid scenario.
673
664k
int RunScenario(const curl::fuzzer::proto::Scenario& scenario, ScenarioRunMode mode) {
674
  // Every input starts from a known off trace baseline (and returns to it when
675
  // the scope exits), so one input's trace selection cannot leak into another.
676
664k
  proto_fuzzer::ScopedTraceReset trace_reset;
677
664k
  if (mode == ScenarioRunMode::kMultiTransfer) {
678
36.4k
    (void)proto_fuzzer::RunMultiTransferScenario(scenario);
679
36.4k
    return 0;
680
36.4k
  }
681
682
628k
  const char* prefix = SchemePrefix(scenario.scheme());
683
628k
  if (prefix == nullptr) {
684
800
    return 0;
685
800
  }
686
687
  // FILE is the one peerless lane. Gate it on the dedicated run mode so the
688
  // compatibility target cannot reinterpret the append-only enum value as a
689
  // local filesystem read from an accumulated corpus entry.
690
627k
  const bool file_lane =
691
627k
      mode == ScenarioRunMode::kFileCoverage && scenario.scheme() == curl::fuzzer::proto::SCHEME_FILE;
692
627k
  if (scenario.scheme() == curl::fuzzer::proto::SCHEME_FILE && !file_lane) {
693
4
    return 0;
694
4
  }
695
627k
  if (!file_lane && scenario.host_path().empty()) {
696
3.25k
    return 0;
697
3.25k
  }
698
699
624k
  std::unique_ptr<MockServerBase> mock = MakeMockServerForScenario(scenario, mode);
700
624k
  if (!mock && !file_lane) {
701
74
    return 0;
702
74
  }
703
704
  // The peer survives every easy option/callback, including cleanup of a
705
  // bounded incomplete transfer. TransferSession owns the rest of that graph.
706
624k
  TransferSession transfer;
707
624k
  if (!transfer.PrepareInputFiles(scenario, mode)) {
708
0
    return 0;
709
0
  }
710
624k
  if (!transfer.Initialize()) {
711
1.55k
    return 0;
712
1.55k
  }
713
622k
  CURL* easy = transfer.easy();
714
622k
  std::string url;
715
622k
  if (file_lane) {
716
8.16k
    const char* source_path = transfer.FileSourcePath();
717
8.16k
    if (source_path == nullptr) {
718
0
      return 0;
719
0
    }
720
8.16k
    url = std::string("file://") + source_path;
721
614k
  } else {
722
614k
    url = ScenarioUrl(scenario, prefix);
723
614k
  }
724
627k
  const auto configure_easy = [&] {
725
627k
    transfer.ApplyBaseline(scenario);
726
627k
    (void)curl_easy_setopt(easy, CURLOPT_URL, url.c_str());
727
627k
    if (mock) {
728
619k
      mock->Install(easy);
729
619k
    }
730
627k
    transfer.ApplyInputFiles();
731
    // Repeat the option-count bound for compatibility and direct callers.
732
627k
    (void)ApplyScenarioOptions(easy, scenario);
733
627k
    if (auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get())) {
734
15.6k
      rtsp->ConfigureClient(easy, scenario.rtsp_probe_mode());
735
15.6k
    }
736
627k
    if (auto* smtp = dynamic_cast<SmtpMockServer*>(mock.get())) {
737
12.0k
      smtp->ConfigureClient(easy);
738
12.0k
    }
739
627k
    if (mode == ScenarioRunMode::kGopherCoverage &&
740
17.8k
        scenario.gopher_probe_mode() == curl::fuzzer::proto::GOPHER_PROBE_TLS_SESSION_KEY_LIMIT) {
741
32
      (void)curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
742
32
      (void)curl_easy_setopt(easy, CURLOPT_SSL_OPTIONS, static_cast<long>(CURLSSLOPT_ALLOW_BEAST));
743
32
    }
744
627k
    if (ScenarioRequestsStartTls(scenario, mode)) {
745
528
      (void)curl_easy_setopt(easy, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
746
528
    }
747
627k
    if (mode == ScenarioRunMode::kResolverCoverage && !ApplyResolverMode(easy, scenario.resolver_mode())) {
748
4
      return false;
749
4
    }
750
627k
    if (mode == ScenarioRunMode::kTlsCoverage) {
751
81.2k
      (void)ApplyTlsClientCredentialProfile(easy, scenario.tls_client_credential_profile());
752
81.2k
    }
753
627k
    return true;
754
627k
  };
scenario_runner.cc:proto_fuzzer::RunScenario(curl::fuzzer::proto::Scenario const&, proto_fuzzer::ScenarioRunMode)::$_1::operator()() const
Line
Count
Source
724
313k
  const auto configure_easy = [&] {
725
313k
    transfer.ApplyBaseline(scenario);
726
313k
    (void)curl_easy_setopt(easy, CURLOPT_URL, url.c_str());
727
313k
    if (mock) {
728
309k
      mock->Install(easy);
729
309k
    }
730
313k
    transfer.ApplyInputFiles();
731
    // Repeat the option-count bound for compatibility and direct callers.
732
313k
    (void)ApplyScenarioOptions(easy, scenario);
733
313k
    if (auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get())) {
734
7.82k
      rtsp->ConfigureClient(easy, scenario.rtsp_probe_mode());
735
7.82k
    }
736
313k
    if (auto* smtp = dynamic_cast<SmtpMockServer*>(mock.get())) {
737
6.00k
      smtp->ConfigureClient(easy);
738
6.00k
    }
739
313k
    if (mode == ScenarioRunMode::kGopherCoverage &&
740
8.92k
        scenario.gopher_probe_mode() == curl::fuzzer::proto::GOPHER_PROBE_TLS_SESSION_KEY_LIMIT) {
741
16
      (void)curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
742
16
      (void)curl_easy_setopt(easy, CURLOPT_SSL_OPTIONS, static_cast<long>(CURLSSLOPT_ALLOW_BEAST));
743
16
    }
744
313k
    if (ScenarioRequestsStartTls(scenario, mode)) {
745
264
      (void)curl_easy_setopt(easy, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
746
264
    }
747
313k
    if (mode == ScenarioRunMode::kResolverCoverage && !ApplyResolverMode(easy, scenario.resolver_mode())) {
748
2
      return false;
749
2
    }
750
313k
    if (mode == ScenarioRunMode::kTlsCoverage) {
751
40.6k
      (void)ApplyTlsClientCredentialProfile(easy, scenario.tls_client_credential_profile());
752
40.6k
    }
753
313k
    return true;
754
313k
  };
scenario_runner.cc:proto_fuzzer::RunScenario(curl::fuzzer::proto::Scenario const&, proto_fuzzer::ScenarioRunMode)::$_1::operator()() const
Line
Count
Source
724
313k
  const auto configure_easy = [&] {
725
313k
    transfer.ApplyBaseline(scenario);
726
313k
    (void)curl_easy_setopt(easy, CURLOPT_URL, url.c_str());
727
313k
    if (mock) {
728
309k
      mock->Install(easy);
729
309k
    }
730
313k
    transfer.ApplyInputFiles();
731
    // Repeat the option-count bound for compatibility and direct callers.
732
313k
    (void)ApplyScenarioOptions(easy, scenario);
733
313k
    if (auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get())) {
734
7.82k
      rtsp->ConfigureClient(easy, scenario.rtsp_probe_mode());
735
7.82k
    }
736
313k
    if (auto* smtp = dynamic_cast<SmtpMockServer*>(mock.get())) {
737
6.00k
      smtp->ConfigureClient(easy);
738
6.00k
    }
739
313k
    if (mode == ScenarioRunMode::kGopherCoverage &&
740
8.92k
        scenario.gopher_probe_mode() == curl::fuzzer::proto::GOPHER_PROBE_TLS_SESSION_KEY_LIMIT) {
741
16
      (void)curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
742
16
      (void)curl_easy_setopt(easy, CURLOPT_SSL_OPTIONS, static_cast<long>(CURLSSLOPT_ALLOW_BEAST));
743
16
    }
744
313k
    if (ScenarioRequestsStartTls(scenario, mode)) {
745
264
      (void)curl_easy_setopt(easy, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
746
264
    }
747
313k
    if (mode == ScenarioRunMode::kResolverCoverage && !ApplyResolverMode(easy, scenario.resolver_mode())) {
748
2
      return false;
749
2
    }
750
313k
    if (mode == ScenarioRunMode::kTlsCoverage) {
751
40.6k
      (void)ApplyTlsClientCredentialProfile(easy, scenario.tls_client_credential_profile());
752
40.6k
    }
753
313k
    return true;
754
313k
  };
755
622k
  if (!configure_easy()) {
756
4
    return 0;
757
4
  }
758
622k
  transfer.ConfigureAltSvcRouting(scenario.host_path());
759
760
622k
  if (file_lane) {
761
    // The file:// handler reads or writes only the session-owned anonymous
762
    // descriptor and never opens a socket. Install the bounded request-data
763
    // owner before perform so retained upload options consume Scenario.upload
764
    // through curl's read callback instead of falling back to stdin. The
765
    // baseline's short timeout still bounds the transfer.
766
8.16k
    ScenarioRequestData* request_data = transfer.InstallRequestData(scenario);
767
8.16k
    assert(request_data != nullptr);
768
8.16k
    (void)request_data;
769
8.16k
    const CURLcode transfer_result = curl_easy_perform(easy);
770
8.16k
    (void)transfer_result;
771
8.16k
    ProbeTransferResults(easy);
772
8.16k
    return 0;
773
8.16k
  }
774
775
614k
  if (mode == ScenarioRunMode::kResolverCoverage) {
776
    // The normal CONNECT_TO baseline deliberately bypasses DNS. This lane
777
    // removes only that override; OPENSOCKET still returns the in-process
778
    // socketpair, so no resolved address can receive network traffic.
779
20.9k
    (void)curl_easy_setopt(easy, CURLOPT_CONNECT_TO, nullptr);
780
20.9k
  }
781
782
614k
  const curl::fuzzer::proto::ApiPlan* api_plan =
783
614k
      mode == ScenarioRunMode::kApiLifecycle && scenario.has_api_plan() ? &scenario.api_plan() : nullptr;
784
614k
  if (api_plan != nullptr && api_plan->reset_easy() && transfer.ResetConfiguration()) {
785
4.85k
    configure_easy();
786
4.85k
  }
787
788
614k
  ApiLifecycle* api_lifecycle = nullptr;
789
614k
  if (api_plan != nullptr) {
790
15.8k
    api_lifecycle = transfer.InstallApiLifecycle(*api_plan, url);
791
15.8k
    assert(api_lifecycle != nullptr);
792
15.8k
    mock->SetMultiObserver([api_lifecycle](CURLM* multi) { api_lifecycle->SetActiveMulti(multi); });
793
15.8k
  }
794
795
  // The DoH lane lets the loopback responder answer every lookup, including
796
  // the address query, so it must not install the resolver lane's fixed
797
  // CURLOPT_RESOLVE cache entries.
798
614k
  const bool apply_resolve_entries = mode == ScenarioRunMode::kResolverCoverage && !scenario.has_doh() &&
799
20.8k
                                     scenario.resolver_mode() == curl::fuzzer::proto::RESOLVER_MODE_DEFAULT;
800
614k
  ScenarioRequestData* request_data = transfer.InstallRequestData(scenario, apply_resolve_entries);
801
614k
  assert(request_data != nullptr);
802
614k
  if (mode == ScenarioRunMode::kResolverCoverage && !request_data->resolve_entries_ready()) {
803
32
    return 0;
804
32
  }
805
614k
  mock->ConfigureRequestData(request_data);
806
614k
  const auto drive_mode = api_plan == nullptr ? curl::fuzzer::proto::API_DRIVE_MULTI_PERFORM : api_plan->drive_mode();
807
614k
  CURLcode transfer_result = CURLE_FAILED_INIT;
808
614k
  if (mode == ScenarioRunMode::kResolverCoverage &&
809
20.8k
      scenario.resolver_mode() == curl::fuzzer::proto::RESOLVER_MODE_THREADED_QUEUED_CANCEL) {
810
522
    auto* resolver_mock = dynamic_cast<MockServer*>(mock.get());
811
522
    if (resolver_mock != nullptr) {
812
522
      transfer_result = RunQueuedResolverCancellation(easy, resolver_mock, scenario);
813
522
    }
814
614k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_PERFORM) {
815
2.68k
    transfer_result = mock->DriveEasyScenario(easy, scenario);
816
611k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_EVENTS) {
817
574
    transfer_result = mock->DriveEasyScenario(easy, scenario, true);
818
610k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_CONNECT_ONLY) {
819
572
    transfer_result = mock->DriveConnectOnlyScenario(easy, scenario).connect_result;
820
610k
  } else if (mode == ScenarioRunMode::kRtspCoverage &&
821
15.6k
             scenario.rtsp_probe_mode() == curl::fuzzer::proto::RTSP_PROBE_CROSS_ORIGIN_REUSE) {
822
2
    auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get());
823
2
    if (rtsp != nullptr) {
824
2
      transfer_result = rtsp->DriveCrossOriginReuseScenario(easy, scenario);
825
2
    }
826
610k
  } else {
827
610k
    transfer_result = mock->DriveScenario(easy, scenario);
828
610k
  }
829
614k
  if (api_plan != nullptr && api_plan->repeat_mime_post() && transfer_result == CURLE_OK) {
830
    // Re-enter the public transfer setup with the same easy handle, MIME root,
831
    // and callback cursor. A completed first body leaves a non-rewindable
832
    // callback at EOF; curl's second MIME reader setup must then report its
833
    // normal public rewind failure and clean up the unattached reader before
834
    // it can send another request.
835
1.69k
    transfer_result = mock->DriveScenario(easy, scenario);
836
1.69k
  }
837
614k
  if (api_lifecycle != nullptr) {
838
15.8k
    const bool retains_internal_multi = drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_PERFORM ||
839
13.1k
                                        drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_EVENTS ||
840
12.5k
                                        drive_mode == curl::fuzzer::proto::API_DRIVE_CONNECT_ONLY;
841
    // An easy entrypoint can fail before attaching its private multi. The
842
    // drive mode alone therefore cannot establish curl_easy_upkeep's state
843
    // precondition. These are the two pre-attachment failure results.
844
15.8k
    const bool probe_upkeep =
845
15.8k
        retains_internal_multi && transfer_result != CURLE_OUT_OF_MEMORY && transfer_result != CURLE_FAILED_INIT;
846
15.8k
    api_lifecycle->ProbeTransferResults(probe_upkeep);
847
15.8k
    api_lifecycle->ProbeEasyDuplication();
848
15.8k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
849
15.8k
    const volatile std::size_t api_observation[] = {
850
15.8k
        api_lifecycle->response_pause_returned(), api_lifecycle->response_bytes_received(),
851
15.8k
        api_lifecycle->reentrant_probes_run(), api_lifecycle->reentrant_recursive_rejections()};
852
15.8k
    (void)api_observation;
853
15.8k
#endif
854
598k
  } else if (mode != ScenarioRunMode::kFastProtocol) {
855
532k
    ProbeTransferResults(easy);
856
532k
  }
857
858
614k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
859
  // TransferSession still owns these completed callback states. Local volatile
860
  // stores retain their getter calls during profiling without adding work to
861
  // ordinary fuzz builds or changing the transfer's callback configuration.
862
614k
  const RequestBuildStats& stats = request_data->stats();
863
614k
  const UploadScriptState& upload = request_data->upload_state();
864
614k
  const volatile std::size_t request_observation[] = {stats.request_headers,
865
614k
                                                      stats.resolve_entries,
866
614k
                                                      stats.telnet_options,
867
614k
                                                      stats.mime_parts,
868
614k
                                                      stats.mime_callback_parts,
869
614k
                                                      stats.mime_headers,
870
614k
                                                      stats.generated_mime_bytes,
871
614k
                                                      request_data->upload_callbacks_installed(),
872
614k
                                                      upload.data_size(),
873
614k
                                                      upload.read_step_count(),
874
614k
                                                      upload.offset(),
875
614k
                                                      upload.seek_calls(),
876
614k
                                                      upload.scripted()};
877
614k
  (void)request_observation;
878
614k
  mock->ObserveState();
879
614k
#endif
880
614k
  return 0;
881
614k
}
proto_fuzzer::RunScenario(curl::fuzzer::proto::Scenario const&, proto_fuzzer::ScenarioRunMode)
Line
Count
Source
673
332k
int RunScenario(const curl::fuzzer::proto::Scenario& scenario, ScenarioRunMode mode) {
674
  // Every input starts from a known off trace baseline (and returns to it when
675
  // the scope exits), so one input's trace selection cannot leak into another.
676
332k
  proto_fuzzer::ScopedTraceReset trace_reset;
677
332k
  if (mode == ScenarioRunMode::kMultiTransfer) {
678
18.2k
    (void)proto_fuzzer::RunMultiTransferScenario(scenario);
679
18.2k
    return 0;
680
18.2k
  }
681
682
314k
  const char* prefix = SchemePrefix(scenario.scheme());
683
314k
  if (prefix == nullptr) {
684
400
    return 0;
685
400
  }
686
687
  // FILE is the one peerless lane. Gate it on the dedicated run mode so the
688
  // compatibility target cannot reinterpret the append-only enum value as a
689
  // local filesystem read from an accumulated corpus entry.
690
313k
  const bool file_lane =
691
313k
      mode == ScenarioRunMode::kFileCoverage && scenario.scheme() == curl::fuzzer::proto::SCHEME_FILE;
692
313k
  if (scenario.scheme() == curl::fuzzer::proto::SCHEME_FILE && !file_lane) {
693
2
    return 0;
694
2
  }
695
313k
  if (!file_lane && scenario.host_path().empty()) {
696
1.62k
    return 0;
697
1.62k
  }
698
699
312k
  std::unique_ptr<MockServerBase> mock = MakeMockServerForScenario(scenario, mode);
700
312k
  if (!mock && !file_lane) {
701
37
    return 0;
702
37
  }
703
704
  // The peer survives every easy option/callback, including cleanup of a
705
  // bounded incomplete transfer. TransferSession owns the rest of that graph.
706
312k
  TransferSession transfer;
707
312k
  if (!transfer.PrepareInputFiles(scenario, mode)) {
708
0
    return 0;
709
0
  }
710
312k
  if (!transfer.Initialize()) {
711
777
    return 0;
712
777
  }
713
311k
  CURL* easy = transfer.easy();
714
311k
  std::string url;
715
311k
  if (file_lane) {
716
4.08k
    const char* source_path = transfer.FileSourcePath();
717
4.08k
    if (source_path == nullptr) {
718
0
      return 0;
719
0
    }
720
4.08k
    url = std::string("file://") + source_path;
721
307k
  } else {
722
307k
    url = ScenarioUrl(scenario, prefix);
723
307k
  }
724
311k
  const auto configure_easy = [&] {
725
311k
    transfer.ApplyBaseline(scenario);
726
311k
    (void)curl_easy_setopt(easy, CURLOPT_URL, url.c_str());
727
311k
    if (mock) {
728
311k
      mock->Install(easy);
729
311k
    }
730
311k
    transfer.ApplyInputFiles();
731
    // Repeat the option-count bound for compatibility and direct callers.
732
311k
    (void)ApplyScenarioOptions(easy, scenario);
733
311k
    if (auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get())) {
734
311k
      rtsp->ConfigureClient(easy, scenario.rtsp_probe_mode());
735
311k
    }
736
311k
    if (auto* smtp = dynamic_cast<SmtpMockServer*>(mock.get())) {
737
311k
      smtp->ConfigureClient(easy);
738
311k
    }
739
311k
    if (mode == ScenarioRunMode::kGopherCoverage &&
740
311k
        scenario.gopher_probe_mode() == curl::fuzzer::proto::GOPHER_PROBE_TLS_SESSION_KEY_LIMIT) {
741
311k
      (void)curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
742
311k
      (void)curl_easy_setopt(easy, CURLOPT_SSL_OPTIONS, static_cast<long>(CURLSSLOPT_ALLOW_BEAST));
743
311k
    }
744
311k
    if (ScenarioRequestsStartTls(scenario, mode)) {
745
311k
      (void)curl_easy_setopt(easy, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
746
311k
    }
747
311k
    if (mode == ScenarioRunMode::kResolverCoverage && !ApplyResolverMode(easy, scenario.resolver_mode())) {
748
311k
      return false;
749
311k
    }
750
311k
    if (mode == ScenarioRunMode::kTlsCoverage) {
751
311k
      (void)ApplyTlsClientCredentialProfile(easy, scenario.tls_client_credential_profile());
752
311k
    }
753
311k
    return true;
754
311k
  };
755
311k
  if (!configure_easy()) {
756
2
    return 0;
757
2
  }
758
311k
  transfer.ConfigureAltSvcRouting(scenario.host_path());
759
760
311k
  if (file_lane) {
761
    // The file:// handler reads or writes only the session-owned anonymous
762
    // descriptor and never opens a socket. Install the bounded request-data
763
    // owner before perform so retained upload options consume Scenario.upload
764
    // through curl's read callback instead of falling back to stdin. The
765
    // baseline's short timeout still bounds the transfer.
766
4.08k
    ScenarioRequestData* request_data = transfer.InstallRequestData(scenario);
767
4.08k
    assert(request_data != nullptr);
768
4.08k
    (void)request_data;
769
4.08k
    const CURLcode transfer_result = curl_easy_perform(easy);
770
4.08k
    (void)transfer_result;
771
4.08k
    ProbeTransferResults(easy);
772
4.08k
    return 0;
773
4.08k
  }
774
775
307k
  if (mode == ScenarioRunMode::kResolverCoverage) {
776
    // The normal CONNECT_TO baseline deliberately bypasses DNS. This lane
777
    // removes only that override; OPENSOCKET still returns the in-process
778
    // socketpair, so no resolved address can receive network traffic.
779
10.4k
    (void)curl_easy_setopt(easy, CURLOPT_CONNECT_TO, nullptr);
780
10.4k
  }
781
782
307k
  const curl::fuzzer::proto::ApiPlan* api_plan =
783
307k
      mode == ScenarioRunMode::kApiLifecycle && scenario.has_api_plan() ? &scenario.api_plan() : nullptr;
784
307k
  if (api_plan != nullptr && api_plan->reset_easy() && transfer.ResetConfiguration()) {
785
2.42k
    configure_easy();
786
2.42k
  }
787
788
307k
  ApiLifecycle* api_lifecycle = nullptr;
789
307k
  if (api_plan != nullptr) {
790
7.92k
    api_lifecycle = transfer.InstallApiLifecycle(*api_plan, url);
791
7.92k
    assert(api_lifecycle != nullptr);
792
7.92k
    mock->SetMultiObserver([api_lifecycle](CURLM* multi) { api_lifecycle->SetActiveMulti(multi); });
793
7.92k
  }
794
795
  // The DoH lane lets the loopback responder answer every lookup, including
796
  // the address query, so it must not install the resolver lane's fixed
797
  // CURLOPT_RESOLVE cache entries.
798
307k
  const bool apply_resolve_entries = mode == ScenarioRunMode::kResolverCoverage && !scenario.has_doh() &&
799
10.4k
                                     scenario.resolver_mode() == curl::fuzzer::proto::RESOLVER_MODE_DEFAULT;
800
307k
  ScenarioRequestData* request_data = transfer.InstallRequestData(scenario, apply_resolve_entries);
801
307k
  assert(request_data != nullptr);
802
307k
  if (mode == ScenarioRunMode::kResolverCoverage && !request_data->resolve_entries_ready()) {
803
16
    return 0;
804
16
  }
805
307k
  mock->ConfigureRequestData(request_data);
806
307k
  const auto drive_mode = api_plan == nullptr ? curl::fuzzer::proto::API_DRIVE_MULTI_PERFORM : api_plan->drive_mode();
807
307k
  CURLcode transfer_result = CURLE_FAILED_INIT;
808
307k
  if (mode == ScenarioRunMode::kResolverCoverage &&
809
10.4k
      scenario.resolver_mode() == curl::fuzzer::proto::RESOLVER_MODE_THREADED_QUEUED_CANCEL) {
810
261
    auto* resolver_mock = dynamic_cast<MockServer*>(mock.get());
811
261
    if (resolver_mock != nullptr) {
812
261
      transfer_result = RunQueuedResolverCancellation(easy, resolver_mock, scenario);
813
261
    }
814
307k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_PERFORM) {
815
1.34k
    transfer_result = mock->DriveEasyScenario(easy, scenario);
816
305k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_EVENTS) {
817
287
    transfer_result = mock->DriveEasyScenario(easy, scenario, true);
818
305k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_CONNECT_ONLY) {
819
286
    transfer_result = mock->DriveConnectOnlyScenario(easy, scenario).connect_result;
820
305k
  } else if (mode == ScenarioRunMode::kRtspCoverage &&
821
7.82k
             scenario.rtsp_probe_mode() == curl::fuzzer::proto::RTSP_PROBE_CROSS_ORIGIN_REUSE) {
822
1
    auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get());
823
1
    if (rtsp != nullptr) {
824
1
      transfer_result = rtsp->DriveCrossOriginReuseScenario(easy, scenario);
825
1
    }
826
305k
  } else {
827
305k
    transfer_result = mock->DriveScenario(easy, scenario);
828
305k
  }
829
307k
  if (api_plan != nullptr && api_plan->repeat_mime_post() && transfer_result == CURLE_OK) {
830
    // Re-enter the public transfer setup with the same easy handle, MIME root,
831
    // and callback cursor. A completed first body leaves a non-rewindable
832
    // callback at EOF; curl's second MIME reader setup must then report its
833
    // normal public rewind failure and clean up the unattached reader before
834
    // it can send another request.
835
846
    transfer_result = mock->DriveScenario(easy, scenario);
836
846
  }
837
307k
  if (api_lifecycle != nullptr) {
838
7.92k
    const bool retains_internal_multi = drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_PERFORM ||
839
6.58k
                                        drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_EVENTS ||
840
6.29k
                                        drive_mode == curl::fuzzer::proto::API_DRIVE_CONNECT_ONLY;
841
    // An easy entrypoint can fail before attaching its private multi. The
842
    // drive mode alone therefore cannot establish curl_easy_upkeep's state
843
    // precondition. These are the two pre-attachment failure results.
844
7.92k
    const bool probe_upkeep =
845
7.92k
        retains_internal_multi && transfer_result != CURLE_OUT_OF_MEMORY && transfer_result != CURLE_FAILED_INIT;
846
7.92k
    api_lifecycle->ProbeTransferResults(probe_upkeep);
847
7.92k
    api_lifecycle->ProbeEasyDuplication();
848
7.92k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
849
7.92k
    const volatile std::size_t api_observation[] = {
850
7.92k
        api_lifecycle->response_pause_returned(), api_lifecycle->response_bytes_received(),
851
7.92k
        api_lifecycle->reentrant_probes_run(), api_lifecycle->reentrant_recursive_rejections()};
852
7.92k
    (void)api_observation;
853
7.92k
#endif
854
299k
  } else if (mode != ScenarioRunMode::kFastProtocol) {
855
266k
    ProbeTransferResults(easy);
856
266k
  }
857
858
307k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
859
  // TransferSession still owns these completed callback states. Local volatile
860
  // stores retain their getter calls during profiling without adding work to
861
  // ordinary fuzz builds or changing the transfer's callback configuration.
862
307k
  const RequestBuildStats& stats = request_data->stats();
863
307k
  const UploadScriptState& upload = request_data->upload_state();
864
307k
  const volatile std::size_t request_observation[] = {stats.request_headers,
865
307k
                                                      stats.resolve_entries,
866
307k
                                                      stats.telnet_options,
867
307k
                                                      stats.mime_parts,
868
307k
                                                      stats.mime_callback_parts,
869
307k
                                                      stats.mime_headers,
870
307k
                                                      stats.generated_mime_bytes,
871
307k
                                                      request_data->upload_callbacks_installed(),
872
307k
                                                      upload.data_size(),
873
307k
                                                      upload.read_step_count(),
874
307k
                                                      upload.offset(),
875
307k
                                                      upload.seek_calls(),
876
307k
                                                      upload.scripted()};
877
307k
  (void)request_observation;
878
307k
  mock->ObserveState();
879
307k
#endif
880
307k
  return 0;
881
307k
}
proto_fuzzer::RunScenario(curl::fuzzer::proto::Scenario const&, proto_fuzzer::ScenarioRunMode)
Line
Count
Source
673
332k
int RunScenario(const curl::fuzzer::proto::Scenario& scenario, ScenarioRunMode mode) {
674
  // Every input starts from a known off trace baseline (and returns to it when
675
  // the scope exits), so one input's trace selection cannot leak into another.
676
332k
  proto_fuzzer::ScopedTraceReset trace_reset;
677
332k
  if (mode == ScenarioRunMode::kMultiTransfer) {
678
18.2k
    (void)proto_fuzzer::RunMultiTransferScenario(scenario);
679
18.2k
    return 0;
680
18.2k
  }
681
682
314k
  const char* prefix = SchemePrefix(scenario.scheme());
683
314k
  if (prefix == nullptr) {
684
400
    return 0;
685
400
  }
686
687
  // FILE is the one peerless lane. Gate it on the dedicated run mode so the
688
  // compatibility target cannot reinterpret the append-only enum value as a
689
  // local filesystem read from an accumulated corpus entry.
690
313k
  const bool file_lane =
691
313k
      mode == ScenarioRunMode::kFileCoverage && scenario.scheme() == curl::fuzzer::proto::SCHEME_FILE;
692
313k
  if (scenario.scheme() == curl::fuzzer::proto::SCHEME_FILE && !file_lane) {
693
2
    return 0;
694
2
  }
695
313k
  if (!file_lane && scenario.host_path().empty()) {
696
1.62k
    return 0;
697
1.62k
  }
698
699
312k
  std::unique_ptr<MockServerBase> mock = MakeMockServerForScenario(scenario, mode);
700
312k
  if (!mock && !file_lane) {
701
37
    return 0;
702
37
  }
703
704
  // The peer survives every easy option/callback, including cleanup of a
705
  // bounded incomplete transfer. TransferSession owns the rest of that graph.
706
312k
  TransferSession transfer;
707
312k
  if (!transfer.PrepareInputFiles(scenario, mode)) {
708
0
    return 0;
709
0
  }
710
312k
  if (!transfer.Initialize()) {
711
777
    return 0;
712
777
  }
713
311k
  CURL* easy = transfer.easy();
714
311k
  std::string url;
715
311k
  if (file_lane) {
716
4.08k
    const char* source_path = transfer.FileSourcePath();
717
4.08k
    if (source_path == nullptr) {
718
0
      return 0;
719
0
    }
720
4.08k
    url = std::string("file://") + source_path;
721
307k
  } else {
722
307k
    url = ScenarioUrl(scenario, prefix);
723
307k
  }
724
311k
  const auto configure_easy = [&] {
725
311k
    transfer.ApplyBaseline(scenario);
726
311k
    (void)curl_easy_setopt(easy, CURLOPT_URL, url.c_str());
727
311k
    if (mock) {
728
311k
      mock->Install(easy);
729
311k
    }
730
311k
    transfer.ApplyInputFiles();
731
    // Repeat the option-count bound for compatibility and direct callers.
732
311k
    (void)ApplyScenarioOptions(easy, scenario);
733
311k
    if (auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get())) {
734
311k
      rtsp->ConfigureClient(easy, scenario.rtsp_probe_mode());
735
311k
    }
736
311k
    if (auto* smtp = dynamic_cast<SmtpMockServer*>(mock.get())) {
737
311k
      smtp->ConfigureClient(easy);
738
311k
    }
739
311k
    if (mode == ScenarioRunMode::kGopherCoverage &&
740
311k
        scenario.gopher_probe_mode() == curl::fuzzer::proto::GOPHER_PROBE_TLS_SESSION_KEY_LIMIT) {
741
311k
      (void)curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
742
311k
      (void)curl_easy_setopt(easy, CURLOPT_SSL_OPTIONS, static_cast<long>(CURLSSLOPT_ALLOW_BEAST));
743
311k
    }
744
311k
    if (ScenarioRequestsStartTls(scenario, mode)) {
745
311k
      (void)curl_easy_setopt(easy, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
746
311k
    }
747
311k
    if (mode == ScenarioRunMode::kResolverCoverage && !ApplyResolverMode(easy, scenario.resolver_mode())) {
748
311k
      return false;
749
311k
    }
750
311k
    if (mode == ScenarioRunMode::kTlsCoverage) {
751
311k
      (void)ApplyTlsClientCredentialProfile(easy, scenario.tls_client_credential_profile());
752
311k
    }
753
311k
    return true;
754
311k
  };
755
311k
  if (!configure_easy()) {
756
2
    return 0;
757
2
  }
758
311k
  transfer.ConfigureAltSvcRouting(scenario.host_path());
759
760
311k
  if (file_lane) {
761
    // The file:// handler reads or writes only the session-owned anonymous
762
    // descriptor and never opens a socket. Install the bounded request-data
763
    // owner before perform so retained upload options consume Scenario.upload
764
    // through curl's read callback instead of falling back to stdin. The
765
    // baseline's short timeout still bounds the transfer.
766
4.08k
    ScenarioRequestData* request_data = transfer.InstallRequestData(scenario);
767
4.08k
    assert(request_data != nullptr);
768
4.08k
    (void)request_data;
769
4.08k
    const CURLcode transfer_result = curl_easy_perform(easy);
770
4.08k
    (void)transfer_result;
771
4.08k
    ProbeTransferResults(easy);
772
4.08k
    return 0;
773
4.08k
  }
774
775
307k
  if (mode == ScenarioRunMode::kResolverCoverage) {
776
    // The normal CONNECT_TO baseline deliberately bypasses DNS. This lane
777
    // removes only that override; OPENSOCKET still returns the in-process
778
    // socketpair, so no resolved address can receive network traffic.
779
10.4k
    (void)curl_easy_setopt(easy, CURLOPT_CONNECT_TO, nullptr);
780
10.4k
  }
781
782
307k
  const curl::fuzzer::proto::ApiPlan* api_plan =
783
307k
      mode == ScenarioRunMode::kApiLifecycle && scenario.has_api_plan() ? &scenario.api_plan() : nullptr;
784
307k
  if (api_plan != nullptr && api_plan->reset_easy() && transfer.ResetConfiguration()) {
785
2.42k
    configure_easy();
786
2.42k
  }
787
788
307k
  ApiLifecycle* api_lifecycle = nullptr;
789
307k
  if (api_plan != nullptr) {
790
7.92k
    api_lifecycle = transfer.InstallApiLifecycle(*api_plan, url);
791
7.92k
    assert(api_lifecycle != nullptr);
792
7.92k
    mock->SetMultiObserver([api_lifecycle](CURLM* multi) { api_lifecycle->SetActiveMulti(multi); });
793
7.92k
  }
794
795
  // The DoH lane lets the loopback responder answer every lookup, including
796
  // the address query, so it must not install the resolver lane's fixed
797
  // CURLOPT_RESOLVE cache entries.
798
307k
  const bool apply_resolve_entries = mode == ScenarioRunMode::kResolverCoverage && !scenario.has_doh() &&
799
10.4k
                                     scenario.resolver_mode() == curl::fuzzer::proto::RESOLVER_MODE_DEFAULT;
800
307k
  ScenarioRequestData* request_data = transfer.InstallRequestData(scenario, apply_resolve_entries);
801
307k
  assert(request_data != nullptr);
802
307k
  if (mode == ScenarioRunMode::kResolverCoverage && !request_data->resolve_entries_ready()) {
803
16
    return 0;
804
16
  }
805
307k
  mock->ConfigureRequestData(request_data);
806
307k
  const auto drive_mode = api_plan == nullptr ? curl::fuzzer::proto::API_DRIVE_MULTI_PERFORM : api_plan->drive_mode();
807
307k
  CURLcode transfer_result = CURLE_FAILED_INIT;
808
307k
  if (mode == ScenarioRunMode::kResolverCoverage &&
809
10.4k
      scenario.resolver_mode() == curl::fuzzer::proto::RESOLVER_MODE_THREADED_QUEUED_CANCEL) {
810
261
    auto* resolver_mock = dynamic_cast<MockServer*>(mock.get());
811
261
    if (resolver_mock != nullptr) {
812
261
      transfer_result = RunQueuedResolverCancellation(easy, resolver_mock, scenario);
813
261
    }
814
307k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_PERFORM) {
815
1.34k
    transfer_result = mock->DriveEasyScenario(easy, scenario);
816
305k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_EVENTS) {
817
287
    transfer_result = mock->DriveEasyScenario(easy, scenario, true);
818
305k
  } else if (drive_mode == curl::fuzzer::proto::API_DRIVE_CONNECT_ONLY) {
819
286
    transfer_result = mock->DriveConnectOnlyScenario(easy, scenario).connect_result;
820
305k
  } else if (mode == ScenarioRunMode::kRtspCoverage &&
821
7.82k
             scenario.rtsp_probe_mode() == curl::fuzzer::proto::RTSP_PROBE_CROSS_ORIGIN_REUSE) {
822
1
    auto* rtsp = dynamic_cast<RtspMockServer*>(mock.get());
823
1
    if (rtsp != nullptr) {
824
1
      transfer_result = rtsp->DriveCrossOriginReuseScenario(easy, scenario);
825
1
    }
826
305k
  } else {
827
305k
    transfer_result = mock->DriveScenario(easy, scenario);
828
305k
  }
829
307k
  if (api_plan != nullptr && api_plan->repeat_mime_post() && transfer_result == CURLE_OK) {
830
    // Re-enter the public transfer setup with the same easy handle, MIME root,
831
    // and callback cursor. A completed first body leaves a non-rewindable
832
    // callback at EOF; curl's second MIME reader setup must then report its
833
    // normal public rewind failure and clean up the unattached reader before
834
    // it can send another request.
835
846
    transfer_result = mock->DriveScenario(easy, scenario);
836
846
  }
837
307k
  if (api_lifecycle != nullptr) {
838
7.92k
    const bool retains_internal_multi = drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_PERFORM ||
839
6.58k
                                        drive_mode == curl::fuzzer::proto::API_DRIVE_EASY_EVENTS ||
840
6.29k
                                        drive_mode == curl::fuzzer::proto::API_DRIVE_CONNECT_ONLY;
841
    // An easy entrypoint can fail before attaching its private multi. The
842
    // drive mode alone therefore cannot establish curl_easy_upkeep's state
843
    // precondition. These are the two pre-attachment failure results.
844
7.92k
    const bool probe_upkeep =
845
7.92k
        retains_internal_multi && transfer_result != CURLE_OUT_OF_MEMORY && transfer_result != CURLE_FAILED_INIT;
846
7.92k
    api_lifecycle->ProbeTransferResults(probe_upkeep);
847
7.92k
    api_lifecycle->ProbeEasyDuplication();
848
7.92k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
849
7.92k
    const volatile std::size_t api_observation[] = {
850
7.92k
        api_lifecycle->response_pause_returned(), api_lifecycle->response_bytes_received(),
851
7.92k
        api_lifecycle->reentrant_probes_run(), api_lifecycle->reentrant_recursive_rejections()};
852
7.92k
    (void)api_observation;
853
7.92k
#endif
854
299k
  } else if (mode != ScenarioRunMode::kFastProtocol) {
855
266k
    ProbeTransferResults(easy);
856
266k
  }
857
858
307k
#if defined(PROTO_FUZZER_OBSERVE_STATE)
859
  // TransferSession still owns these completed callback states. Local volatile
860
  // stores retain their getter calls during profiling without adding work to
861
  // ordinary fuzz builds or changing the transfer's callback configuration.
862
307k
  const RequestBuildStats& stats = request_data->stats();
863
307k
  const UploadScriptState& upload = request_data->upload_state();
864
307k
  const volatile std::size_t request_observation[] = {stats.request_headers,
865
307k
                                                      stats.resolve_entries,
866
307k
                                                      stats.telnet_options,
867
307k
                                                      stats.mime_parts,
868
307k
                                                      stats.mime_callback_parts,
869
307k
                                                      stats.mime_headers,
870
307k
                                                      stats.generated_mime_bytes,
871
307k
                                                      request_data->upload_callbacks_installed(),
872
307k
                                                      upload.data_size(),
873
307k
                                                      upload.read_step_count(),
874
307k
                                                      upload.offset(),
875
307k
                                                      upload.seek_calls(),
876
307k
                                                      upload.scripted()};
877
307k
  (void)request_observation;
878
307k
  mock->ObserveState();
879
307k
#endif
880
307k
  return 0;
881
307k
}
882
883
}  // namespace proto_fuzzer