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/h2_plan.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 HTTP/2 structured-action serialization and client frame tracking.
9
10
#include "proto_fuzzer/h2_plan.h"
11
12
#include <nghttp2/nghttp2.h>
13
14
#include <algorithm>
15
#include <array>
16
#include <cstddef>
17
#include <cstdint>
18
#include <string>
19
#include <string_view>
20
#include <utility>
21
#include <vector>
22
23
#include "proto_fuzzer/scenario_limits.h"
24
25
namespace proto_fuzzer {
26
27
namespace {
28
29
constexpr std::string_view kClientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
30
constexpr std::size_t kFrameHeaderSize = 9;
31
constexpr std::size_t kMaxFramePayload = 16384;
32
33
constexpr std::uint8_t kDataFrame = 0;
34
constexpr std::uint8_t kHeadersFrame = 1;
35
constexpr std::uint8_t kPriorityFrame = 2;
36
constexpr std::uint8_t kRstStreamFrame = 3;
37
constexpr std::uint8_t kSettingsFrame = 4;
38
constexpr std::uint8_t kPushPromiseFrame = 5;
39
constexpr std::uint8_t kPingFrame = 6;
40
constexpr std::uint8_t kGoawayFrame = 7;
41
constexpr std::uint8_t kWindowUpdateFrame = 8;
42
constexpr std::uint8_t kContinuationFrame = 9;
43
44
constexpr std::uint8_t kFlagAck = 0x1;
45
constexpr std::uint8_t kFlagEndStream = 0x1;
46
constexpr std::uint8_t kFlagEndHeaders = 0x4;
47
48
156k
void AppendUint32(std::string* output, std::uint32_t value) {
49
156k
  output->push_back(static_cast<char>((value >> 24U) & 0xffU));
50
156k
  output->push_back(static_cast<char>((value >> 16U) & 0xffU));
51
156k
  output->push_back(static_cast<char>((value >> 8U) & 0xffU));
52
156k
  output->push_back(static_cast<char>(value & 0xffU));
53
156k
}
54
55
366k
std::uint32_t ReadUint32(const char* input) {
56
366k
  const auto* bytes = reinterpret_cast<const unsigned char*>(input);
57
366k
  return (static_cast<std::uint32_t>(bytes[0]) << 24U) | (static_cast<std::uint32_t>(bytes[1]) << 16U) |
58
366k
         (static_cast<std::uint32_t>(bytes[2]) << 8U) | static_cast<std::uint32_t>(bytes[3]);
59
366k
}
60
61
void AppendFrame(std::string* output, std::uint32_t declared_length, std::uint8_t type, std::uint8_t flags,
62
120k
                 std::uint32_t stream_id, std::string_view payload) {
63
120k
  const std::uint32_t length = declared_length & 0x00ffffffU;
64
120k
  output->push_back(static_cast<char>((length >> 16U) & 0xffU));
65
120k
  output->push_back(static_cast<char>((length >> 8U) & 0xffU));
66
120k
  output->push_back(static_cast<char>(length & 0xffU));
67
120k
  output->push_back(static_cast<char>(type));
68
120k
  output->push_back(static_cast<char>(flags));
69
120k
  AppendUint32(output, stream_id & 0x7fffffffU);
70
120k
  output->append(payload.data(), payload.size());
71
120k
}
72
73
36.2k
std::uint32_t CanonicalStatus(std::uint32_t status) {
74
36.2k
  if (status >= 100U && status <= 599U) {
75
36.2k
    return status;
76
36.2k
  }
77
0
  return status == 0U ? 200U : 100U + (status % 500U);
78
36.2k
}
79
80
141k
std::string BoundedString(const std::string& input, std::size_t limit) { return input.substr(0, limit); }
81
82
63.6k
std::string CanonicalHeaderName(const std::string& input) {
83
63.6k
  std::string name = BoundedString(input, scenario_limits::kMaxHttp3HeaderNameBytes);
84
63.6k
  if (name.empty()) {
85
0
    return "x-fuzz";
86
0
  }
87
511k
  for (char& byte : name) {
88
511k
    const unsigned char value = static_cast<unsigned char>(byte);
89
511k
    const bool alpha = (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z');
90
511k
    const bool digit = value >= '0' && value <= '9';
91
511k
    const bool punctuation = value == '!' || value == '#' || value == '$' || value == '%' || value == '&' ||
92
500k
                             value == '\'' || value == '*' || value == '+' || value == '-' || value == '.' ||
93
359k
                             value == '^' || value == '_' || value == '`' || value == '|' || value == '~';
94
511k
    if (value >= 'A' && value <= 'Z') {
95
0
      byte = static_cast<char>(value - 'A' + 'a');
96
511k
    } else if (!alpha && !digit && !punctuation) {
97
0
      byte = '-';
98
0
    }
99
511k
  }
100
63.6k
  return name;
101
63.6k
}
102
103
63.6k
std::string CanonicalHeaderValue(const std::string& input) {
104
63.6k
  std::string value = BoundedString(input, scenario_limits::kMaxMetadataBytes);
105
4.51M
  for (char& byte : value) {
106
4.51M
    const unsigned char character = static_cast<unsigned char>(byte);
107
4.51M
    if ((character < 0x20U && character != '\t') || character == 0x7fU) {
108
0
      byte = ' ';
109
0
    }
110
4.51M
  }
111
63.6k
  return value;
112
63.6k
}
113
114
}  // namespace
115
116
H2PlanDriver::H2PlanDriver()
117
37.8k
    : plan_(nullptr),
118
37.8k
      deflater_(nullptr),
119
37.8k
      next_action_(0),
120
37.8k
      remaining_yields_(0),
121
37.8k
      initial_settings_sent_(false),
122
37.8k
      client_settings_ack_sent_(false),
123
37.8k
      expect_client_preface_(true),
124
37.8k
      observed_client_settings_(0),
125
37.8k
      observed_client_priority_(0) {
126
37.8k
  (void)nghttp2_hd_deflate_new(&deflater_, 4096);
127
37.8k
}
128
129
37.8k
H2PlanDriver::~H2PlanDriver() { nghttp2_hd_deflate_del(deflater_); }
130
131
/// Reset HPACK and stream state for one connection.
132
/// @param plan Ordered H2 actions and initial peer settings to execute.
133
/// @param scheme Scheme used in generated push-request pseudo-headers.
134
/// @param authority Authority used in generated push-request pseudo-headers.
135
21.0k
void H2PlanDriver::Reset(const curl::fuzzer::proto::Http2Plan& plan, std::string scheme, std::string authority) {
136
21.0k
  plan_ = &plan;
137
21.0k
  next_action_ = 0;
138
21.0k
  remaining_yields_ = 0;
139
21.0k
  initial_settings_sent_ = false;
140
21.0k
  client_settings_ack_sent_ = false;
141
21.0k
  expect_client_preface_ = true;
142
21.0k
  observed_client_settings_ = 0;
143
21.0k
  observed_client_priority_ = 0;
144
21.0k
  client_bytes_.clear();
145
21.0k
  request_streams_.clear();
146
21.0k
  pushed_streams_.clear();
147
21.0k
  client_streams_.clear();
148
21.0k
  scheme_ = std::move(scheme);
149
21.0k
  authority_ = std::move(authority);
150
21.0k
  nghttp2_hd_deflate_del(deflater_);
151
21.0k
  deflater_ = nullptr;
152
21.0k
  (void)nghttp2_hd_deflate_new(&deflater_, 4096);
153
21.0k
}
154
155
/// Parse client frame headers after transport decoding.
156
/// @param data Contiguous bytes received from curl.
157
/// @param size Number of bytes available at `data`.
158
185k
void H2PlanDriver::ObserveIncomingData(const unsigned char* data, std::size_t size) {
159
185k
  if (data == nullptr || size == 0) {
160
0
    return;
161
0
  }
162
185k
  if (size > scenario_limits::kMaxHttp2ObservedBytes ||
163
185k
      client_bytes_.size() > scenario_limits::kMaxHttp2ObservedBytes - size) {
164
0
    client_bytes_.clear();
165
0
    expect_client_preface_ = false;
166
0
    return;
167
0
  }
168
185k
  client_bytes_.append(reinterpret_cast<const char*>(data), size);
169
185k
  ParseClientFrames();
170
185k
}
171
172
185k
void H2PlanDriver::ParseClientFrames() {
173
185k
  if (expect_client_preface_) {
174
34.7k
    if (client_bytes_.size() < kClientPreface.size()) {
175
0
      return;
176
0
    }
177
34.7k
    if (std::string_view(client_bytes_).substr(0, kClientPreface.size()) == kClientPreface) {
178
34.7k
      client_bytes_.erase(0, kClientPreface.size());
179
34.7k
    }
180
34.7k
    expect_client_preface_ = false;
181
34.7k
  }
182
183
185k
  std::size_t consumed = 0;
184
551k
  while (client_bytes_.size() - consumed >= kFrameHeaderSize) {
185
391k
    const auto* header = reinterpret_cast<const unsigned char*>(client_bytes_.data() + consumed);
186
391k
    const std::size_t payload_size = (static_cast<std::size_t>(header[0]) << 16U) |
187
391k
                                     (static_cast<std::size_t>(header[1]) << 8U) | static_cast<std::size_t>(header[2]);
188
391k
    if (payload_size > scenario_limits::kMaxHttp2ObservedBytes ||
189
391k
        client_bytes_.size() - consumed < kFrameHeaderSize + payload_size) {
190
24.2k
      break;
191
24.2k
    }
192
366k
    const std::uint8_t type = header[3];
193
366k
    const std::uint8_t flags = header[4];
194
366k
    const std::uint32_t stream_id = ReadUint32(client_bytes_.data() + consumed + 5) & 0x7fffffffU;
195
366k
    RecordClientFrame(type, flags, stream_id);
196
366k
    consumed += kFrameHeaderSize + payload_size;
197
366k
  }
198
185k
  client_bytes_.erase(0, consumed);
199
185k
}
200
201
366k
void H2PlanDriver::RecordClientFrame(std::uint8_t type, std::uint8_t flags, std::uint32_t stream_id) {
202
366k
  if (type == kSettingsFrame && stream_id == 0 && (flags & kFlagAck) == 0) {
203
34.7k
    ++observed_client_settings_;
204
34.7k
    return;
205
34.7k
  }
206
332k
  if (stream_id == 0) {
207
73.5k
    return;
208
73.5k
  }
209
210
258k
  if (type == kPriorityFrame) {
211
0
    ++observed_client_priority_;
212
0
  }
213
214
258k
  auto& stream = client_streams_[stream_id];
215
258k
  if (type == kHeadersFrame) {
216
60.0k
    ++stream.headers;
217
60.0k
    if ((stream_id & 1U) != 0 &&
218
60.0k
        std::find(request_streams_.begin(), request_streams_.end(), stream_id) == request_streams_.end()) {
219
60.0k
      request_streams_.push_back(stream_id);
220
60.0k
    }
221
198k
  } else if (type == kDataFrame) {
222
53.6k
    ++stream.data;
223
53.6k
  }
224
258k
  if ((type == kHeadersFrame || type == kDataFrame) && (flags & kFlagEndStream) != 0) {
225
58.0k
    ++stream.end_stream;
226
58.0k
  }
227
258k
}
228
229
std::uint32_t H2PlanDriver::ResolveStream(const curl::fuzzer::proto::Http2StreamRef& stream,
230
66.3k
                                          bool allow_connection) const {
231
66.3k
  using StreamRef = curl::fuzzer::proto::Http2StreamRef;
232
66.3k
  switch (stream.target_case()) {
233
1.55k
    case StreamRef::kConnection:
234
1.55k
      return allow_connection ? 0U : 1U;
235
2.14k
    case StreamRef::kRequestIndex: {
236
2.14k
      const std::size_t index = static_cast<std::size_t>(stream.request_index());
237
2.14k
      if (index < request_streams_.size()) {
238
1.54k
        return request_streams_[index];
239
1.54k
      }
240
596
      return 1U + 2U * static_cast<std::uint32_t>(index % 0x3fffffffU);
241
2.14k
    }
242
2.35k
    case StreamRef::kPushIndex: {
243
2.35k
      const std::size_t index = static_cast<std::size_t>(stream.push_index());
244
2.35k
      if (index < pushed_streams_.size()) {
245
1.19k
        return pushed_streams_[index];
246
1.19k
      }
247
1.16k
      return 2U + 2U * static_cast<std::uint32_t>(index % 0x3fffffffU);
248
2.35k
    }
249
3.54k
    case StreamRef::kExplicitId:
250
3.54k
      return stream.explicit_id() & 0x7fffffffU;
251
629
    case StreamRef::kLatestRequest:
252
56.7k
    case StreamRef::TARGET_NOT_SET:
253
56.7k
    default:
254
56.7k
      return request_streams_.empty() ? 1U : request_streams_.back();
255
66.3k
  }
256
66.3k
}
257
258
10.0k
std::uint32_t H2PlanDriver::ResolvePromisedStream(const curl::fuzzer::proto::Http2StreamRef& stream) {
259
10.0k
  std::uint32_t stream_id = 0;
260
10.0k
  if (stream.target_case() == curl::fuzzer::proto::Http2StreamRef::TARGET_NOT_SET ||
261
1.90k
      stream.target_case() == curl::fuzzer::proto::Http2StreamRef::kLatestRequest ||
262
8.54k
      stream.target_case() == curl::fuzzer::proto::Http2StreamRef::kConnection) {
263
8.54k
    stream_id = 2U + 2U * static_cast<std::uint32_t>(pushed_streams_.size());
264
8.54k
  } else {
265
1.51k
    stream_id = ResolveStream(stream, false);
266
1.51k
  }
267
10.0k
  if (std::find(pushed_streams_.begin(), pushed_streams_.end(), stream_id) == pushed_streams_.end()) {
268
9.91k
    pushed_streams_.push_back(stream_id);
269
9.91k
  }
270
10.0k
  return stream_id;
271
10.0k
}
272
273
9.04k
bool H2PlanDriver::WaitSatisfied(const curl::fuzzer::proto::Http2Wait& wait) const {
274
9.04k
  const std::size_t required = std::max<std::size_t>(1, std::min<std::size_t>(wait.count(), 16));
275
9.04k
  if (wait.event() == curl::fuzzer::proto::HTTP2_CLIENT_EVENT_SETTINGS) {
276
2.17k
    return observed_client_settings_ >= required;
277
2.17k
  }
278
6.87k
  const std::uint32_t stream_id = ResolveStream(wait.stream(), false);
279
6.87k
  const auto found = client_streams_.find(stream_id);
280
6.87k
  if (found == client_streams_.end()) {
281
1.67k
    return false;
282
1.67k
  }
283
5.19k
  switch (wait.event()) {
284
1.08k
    case curl::fuzzer::proto::HTTP2_CLIENT_EVENT_HEADERS:
285
1.08k
      return found->second.headers >= required;
286
464
    case curl::fuzzer::proto::HTTP2_CLIENT_EVENT_DATA:
287
464
      return found->second.data >= required;
288
3.64k
    case curl::fuzzer::proto::HTTP2_CLIENT_EVENT_END_STREAM:
289
3.64k
      return found->second.end_stream >= required;
290
0
    case curl::fuzzer::proto::HTTP2_CLIENT_EVENT_SETTINGS:
291
0
    default:
292
0
      return false;
293
5.19k
  }
294
5.19k
}
295
296
46.9k
bool H2PlanDriver::EncodeHeaders(const std::vector<std::pair<std::string, std::string>>& headers, std::string* output) {
297
46.9k
  if (deflater_ == nullptr || output == nullptr) {
298
0
    return false;
299
0
  }
300
46.9k
  std::vector<nghttp2_nv> name_values;
301
46.9k
  name_values.reserve(headers.size());
302
140k
  for (const auto& header : headers) {
303
140k
    nghttp2_nv nv{};
304
140k
    nv.name = reinterpret_cast<std::uint8_t*>(const_cast<char*>(header.first.data()));
305
140k
    nv.value = reinterpret_cast<std::uint8_t*>(const_cast<char*>(header.second.data()));
306
140k
    nv.namelen = header.first.size();
307
140k
    nv.valuelen = header.second.size();
308
140k
    nv.flags = NGHTTP2_NV_FLAG_NONE;
309
140k
    name_values.push_back(nv);
310
140k
  }
311
46.9k
  const std::size_t bound = nghttp2_hd_deflate_bound(deflater_, name_values.data(), name_values.size());
312
46.9k
  if (bound > scenario_limits::kMaxHttp2HeaderBytes * 2U) {
313
0
    return false;
314
0
  }
315
46.9k
  output->resize(bound);
316
46.9k
  const nghttp2_ssize encoded = nghttp2_hd_deflate_hd2(deflater_, reinterpret_cast<std::uint8_t*>(output->data()),
317
46.9k
                                                       output->size(), name_values.data(), name_values.size());
318
46.9k
  if (encoded < 0) {
319
0
    output->clear();
320
0
    return false;
321
0
  }
322
46.9k
  output->resize(static_cast<std::size_t>(encoded));
323
46.9k
  return true;
324
46.9k
}
325
326
102k
bool H2PlanDriver::SerializeAction(const curl::fuzzer::proto::Http2Action& action, std::string* output) {
327
102k
  using Action = curl::fuzzer::proto::Http2Action;
328
102k
  output->clear();
329
102k
  switch (action.action_case()) {
330
29.6k
    case Action::kSettings: {
331
29.6k
      std::string payload;
332
29.6k
      const auto& settings = action.settings();
333
29.6k
      if (!settings.ack()) {
334
29.4k
        const std::size_t count = std::min<std::size_t>(scenario_limits::kMaxHttp2Settings,
335
29.4k
                                                        static_cast<std::size_t>(settings.entries_size()));
336
29.4k
        payload.reserve(count * 6U);
337
42.8k
        for (std::size_t index = 0; index < count; ++index) {
338
13.4k
          const auto& entry = settings.entries(static_cast<int>(index));
339
13.4k
          const std::uint16_t identifier = static_cast<std::uint16_t>(entry.identifier());
340
13.4k
          payload.push_back(static_cast<char>((identifier >> 8U) & 0xffU));
341
13.4k
          payload.push_back(static_cast<char>(identifier & 0xffU));
342
13.4k
          AppendUint32(&payload, entry.value());
343
13.4k
        }
344
29.4k
      }
345
29.6k
      AppendFrame(output, payload.size(), kSettingsFrame, settings.ack() ? kFlagAck : 0, 0, payload);
346
29.6k
      return true;
347
0
    }
348
36.8k
    case Action::kHeaders: {
349
36.8k
      const auto& headers = action.headers();
350
36.8k
      std::vector<std::pair<std::string, std::string>> fields;
351
36.8k
      const std::size_t count =
352
36.8k
          std::min<std::size_t>(scenario_limits::kMaxHttp2Headers, static_cast<std::size_t>(headers.fields_size()));
353
36.8k
      fields.reserve(count + 1U);
354
36.8k
      if (!headers.trailers()) {
355
36.2k
        fields.emplace_back(":status", std::to_string(CanonicalStatus(headers.status_code())));
356
36.2k
      }
357
36.8k
      std::size_t bytes = 0;
358
82.3k
      for (std::size_t index = 0; index < count && bytes < scenario_limits::kMaxHttp2HeaderBytes; ++index) {
359
45.4k
        const auto& field = headers.fields(static_cast<int>(index));
360
45.4k
        std::string name = CanonicalHeaderName(field.name());
361
45.4k
        std::string value = CanonicalHeaderValue(field.value());
362
45.4k
        const std::size_t remaining = scenario_limits::kMaxHttp2HeaderBytes - bytes;
363
45.4k
        if (name.size() + value.size() > remaining) {
364
0
          value.resize(remaining > name.size() ? remaining - name.size() : 0);
365
0
          name.resize(std::min(name.size(), remaining));
366
0
        }
367
45.4k
        bytes += name.size() + value.size();
368
45.4k
        fields.emplace_back(std::move(name), std::move(value));
369
45.4k
      }
370
36.8k
      std::string block;
371
36.8k
      if (!EncodeHeaders(fields, &block)) {
372
0
        return false;
373
0
      }
374
36.8k
      const std::uint32_t stream_id = ResolveStream(headers.stream(), false);
375
36.8k
      std::size_t offset = 0;
376
36.8k
      do {
377
36.8k
        const std::size_t chunk_size = std::min(kMaxFramePayload, block.size() - offset);
378
36.8k
        const bool last = offset + chunk_size == block.size();
379
36.8k
        std::uint8_t flags = last ? kFlagEndHeaders : 0;
380
36.8k
        if (offset == 0 && headers.end_stream()) {
381
24.1k
          flags |= kFlagEndStream;
382
24.1k
        }
383
36.8k
        AppendFrame(output, chunk_size, offset == 0 ? kHeadersFrame : kContinuationFrame, flags, stream_id,
384
36.8k
                    std::string_view(block).substr(offset, chunk_size));
385
36.8k
        offset += chunk_size;
386
36.8k
      } while (offset < block.size());
387
36.8k
      return true;
388
36.8k
    }
389
2.14k
    case Action::kData: {
390
2.14k
      const auto& data = action.data();
391
2.14k
      const std::string payload = BoundedString(data.data(), scenario_limits::kMaxHttp2DataBytes);
392
2.14k
      const std::uint32_t stream_id = ResolveStream(data.stream(), false);
393
2.14k
      std::size_t offset = 0;
394
2.16k
      do {
395
2.16k
        const std::size_t chunk_size = std::min(kMaxFramePayload, payload.size() - offset);
396
2.16k
        const bool last = offset + chunk_size == payload.size();
397
2.16k
        AppendFrame(output, chunk_size, kDataFrame, last && data.end_stream() ? kFlagEndStream : 0, stream_id,
398
2.16k
                    std::string_view(payload).substr(offset, chunk_size));
399
2.16k
        offset += chunk_size;
400
2.16k
      } while (offset < payload.size());
401
2.14k
      return true;
402
36.8k
    }
403
3.30k
    case Action::kWindowUpdate: {
404
3.30k
      const auto& update = action.window_update();
405
3.30k
      std::string payload;
406
3.30k
      AppendUint32(&payload, std::max(1U, update.increment() & 0x7fffffffU));
407
3.30k
      AppendFrame(output, payload.size(), kWindowUpdateFrame, 0, ResolveStream(update.stream(), true), payload);
408
3.30k
      return true;
409
36.8k
    }
410
2.26k
    case Action::kRstStream: {
411
2.26k
      const auto& reset = action.rst_stream();
412
2.26k
      std::string payload;
413
2.26k
      AppendUint32(&payload, reset.error_code());
414
2.26k
      AppendFrame(output, payload.size(), kRstStreamFrame, 0, ResolveStream(reset.stream(), false), payload);
415
2.26k
      return true;
416
36.8k
    }
417
6.07k
    case Action::kPing: {
418
6.07k
      std::array<char, 8> opaque{};
419
6.07k
      const std::string& source = action.ping().opaque_data();
420
6.07k
      std::copy_n(source.begin(), std::min(source.size(), opaque.size()), opaque.begin());
421
6.07k
      AppendFrame(output, opaque.size(), kPingFrame, action.ping().ack() ? kFlagAck : 0, 0,
422
6.07k
                  std::string_view(opaque.data(), opaque.size()));
423
6.07k
      return true;
424
36.8k
    }
425
3.30k
    case Action::kGoaway: {
426
3.30k
      const auto& goaway = action.goaway();
427
3.30k
      std::string payload;
428
3.30k
      AppendUint32(&payload, ResolveStream(goaway.last_stream(), true));
429
3.30k
      AppendUint32(&payload, goaway.error_code());
430
3.30k
      payload.append(BoundedString(goaway.debug_data(), scenario_limits::kMaxHttp2RawFrameBytes));
431
3.30k
      AppendFrame(output, payload.size(), kGoawayFrame, 0, 0, payload);
432
3.30k
      return true;
433
36.8k
    }
434
10.0k
    case Action::kPushPromise: {
435
10.0k
      const auto& promise = action.push_promise();
436
10.0k
      std::vector<std::pair<std::string, std::string>> fields = {
437
10.0k
          {":method", "GET"}, {":scheme", scheme_}, {":authority", authority_}, {":path", "/push"}};
438
10.0k
      const std::size_t count = std::min<std::size_t>(scenario_limits::kMaxHttp2Headers,
439
10.0k
                                                      static_cast<std::size_t>(promise.request_headers_size()));
440
28.2k
      for (std::size_t index = 0; index < count; ++index) {
441
18.1k
        const auto& field = promise.request_headers(static_cast<int>(index));
442
18.1k
        fields.emplace_back(CanonicalHeaderName(field.name()), CanonicalHeaderValue(field.value()));
443
18.1k
      }
444
10.0k
      std::string block;
445
10.0k
      if (!EncodeHeaders(fields, &block)) {
446
0
        return false;
447
0
      }
448
10.0k
      const std::uint32_t parent = ResolveStream(promise.parent_stream(), false);
449
10.0k
      const std::uint32_t promised = ResolvePromisedStream(promise.promised_stream());
450
10.0k
      std::string first_payload;
451
10.0k
      AppendUint32(&first_payload, promised);
452
10.0k
      const std::size_t first_size = std::min(kMaxFramePayload - 4U, block.size());
453
10.0k
      first_payload.append(block.data(), first_size);
454
10.0k
      AppendFrame(output, first_payload.size(), kPushPromiseFrame, first_size == block.size() ? kFlagEndHeaders : 0,
455
10.0k
                  parent, first_payload);
456
10.0k
      std::size_t offset = first_size;
457
10.0k
      while (offset < block.size()) {
458
18
        const std::size_t chunk_size = std::min(kMaxFramePayload, block.size() - offset);
459
18
        offset += chunk_size;
460
18
        AppendFrame(output, chunk_size, kContinuationFrame, offset == block.size() ? kFlagEndHeaders : 0, parent,
461
18
                    std::string_view(block).substr(offset - chunk_size, chunk_size));
462
18
      }
463
10.0k
      return true;
464
10.0k
    }
465
6.07k
    case Action::kWireFrame: {
466
6.07k
      const auto& frame = action.wire_frame();
467
6.07k
      const std::string payload = BoundedString(frame.payload(), scenario_limits::kMaxHttp2RawFrameBytes);
468
6.07k
      const std::uint32_t declared = frame.use_declared_length() ? frame.declared_length() : payload.size();
469
6.07k
      AppendFrame(output, declared, static_cast<std::uint8_t>(frame.type()), static_cast<std::uint8_t>(frame.flags()),
470
6.07k
                  frame.stream_id(), payload);
471
6.07k
      return true;
472
10.0k
    }
473
2.40k
    case Action::kRawBytes:
474
2.40k
      *output = BoundedString(action.raw_bytes(), scenario_limits::kMaxHttp2RawFrameBytes);
475
2.40k
      return true;
476
0
    case Action::kWait:
477
0
    case Action::kYieldTurns:
478
0
    case Action::ACTION_NOT_SET:
479
0
    default:
480
0
      return false;
481
102k
  }
482
102k
}
483
484
/// Advance one scripted boundary. `output` receives zero or more complete
485
/// HTTP/2 frames. A true result also represents a consumed wait or yield;
486
/// false means the next barrier is not ready or the plan is complete.
487
/// @param output Destination for the serialized frame bytes.
488
/// @return true when a frame, wait, or yield boundary was consumed.
489
367k
bool H2PlanDriver::NextChunk(std::string* output) {
490
367k
  if (output == nullptr || plan_ == nullptr) {
491
0
    return false;
492
0
  }
493
367k
  output->clear();
494
367k
  if (!initial_settings_sent_) {
495
20.7k
    curl::fuzzer::proto::Http2Action initial;
496
20.7k
    *initial.mutable_settings() = plan_->initial_settings();
497
20.7k
    initial.mutable_settings()->set_ack(false);
498
20.7k
    (void)SerializeAction(initial, output);
499
20.7k
    initial_settings_sent_ = true;
500
20.7k
    return true;
501
20.7k
  }
502
346k
  if (!client_settings_ack_sent_) {
503
20.6k
    if (observed_client_settings_ == 0) {
504
1
      return false;
505
1
    }
506
20.6k
    AppendFrame(output, 0, kSettingsFrame, kFlagAck, 0, {});
507
20.6k
    client_settings_ack_sent_ = true;
508
20.6k
    return true;
509
20.6k
  }
510
511
325k
  const std::size_t action_count =
512
325k
      std::min<std::size_t>(scenario_limits::kMaxHttp2Actions, static_cast<std::size_t>(plan_->actions_size()));
513
325k
  if (next_action_ >= action_count) {
514
107k
    return false;
515
107k
  }
516
218k
  const auto& action = plan_->actions(static_cast<int>(next_action_));
517
218k
  if (action.action_case() == curl::fuzzer::proto::Http2Action::kWait) {
518
9.04k
    if (!WaitSatisfied(action.wait())) {
519
7.14k
      return false;
520
7.14k
    }
521
1.89k
    ++next_action_;
522
1.89k
    return true;
523
9.04k
  }
524
209k
  if (action.action_case() == curl::fuzzer::proto::Http2Action::kYieldTurns) {
525
128k
    if (remaining_yields_ == 0) {
526
10.6k
      remaining_yields_ = std::min<std::size_t>(scenario_limits::kMaxHttp2YieldTurns, action.yield_turns());
527
10.6k
      if (remaining_yields_ == 0) {
528
1.53k
        ++next_action_;
529
1.53k
        return true;
530
1.53k
      }
531
10.6k
    }
532
126k
    --remaining_yields_;
533
126k
    if (remaining_yields_ == 0) {
534
8.89k
      ++next_action_;
535
8.89k
    }
536
126k
    return true;
537
128k
  }
538
539
81.3k
  (void)SerializeAction(action, output);
540
81.3k
  ++next_action_;
541
81.3k
  return true;
542
209k
}
543
544
/// @return true while startup or a bounded action remains to be processed.
545
37.4k
bool H2PlanDriver::has_pending_work() const {
546
37.4k
  if (plan_ == nullptr || !initial_settings_sent_ || !client_settings_ack_sent_) {
547
16.8k
    return plan_ != nullptr;
548
16.8k
  }
549
20.6k
  return next_action_ <
550
20.6k
         std::min<std::size_t>(scenario_limits::kMaxHttp2Actions, static_cast<std::size_t>(plan_->actions_size()));
551
37.4k
}
552
553
/// @return number of distinct client request streams observed.
554
45.6k
std::size_t H2PlanDriver::observed_request_count() const { return request_streams_.size(); }
555
556
/// @return number of non-ACK client SETTINGS frames observed.
557
37.4k
std::size_t H2PlanDriver::observed_client_settings_count() const { return observed_client_settings_; }
558
559
/// @return PRIORITY frames observed from curl since the latest plan reset.
560
0
std::size_t H2PlanDriver::observed_client_priority_count() const { return observed_client_priority_; }
561
562
}  // namespace proto_fuzzer