Coverage Report

Created: 2026-12-01 06:33

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