Coverage Report

Created: 2026-10-31 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl_fuzzer/proto_fuzzer/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
137k
void AppendUint32(std::string* output, std::uint32_t value) {
49
137k
  output->push_back(static_cast<char>((value >> 24U) & 0xffU));
50
137k
  output->push_back(static_cast<char>((value >> 16U) & 0xffU));
51
137k
  output->push_back(static_cast<char>((value >> 8U) & 0xffU));
52
137k
  output->push_back(static_cast<char>(value & 0xffU));
53
137k
}
54
55
296k
std::uint32_t ReadUint32(const char* input) {
56
296k
  const auto* bytes = reinterpret_cast<const unsigned char*>(input);
57
296k
  return (static_cast<std::uint32_t>(bytes[0]) << 24U) | (static_cast<std::uint32_t>(bytes[1]) << 16U) |
58
296k
         (static_cast<std::uint32_t>(bytes[2]) << 8U) | static_cast<std::uint32_t>(bytes[3]);
59
296k
}
60
61
void AppendFrame(std::string* output, std::uint32_t declared_length, std::uint8_t type, std::uint8_t flags,
62
100k
                 std::uint32_t stream_id, std::string_view payload) {
63
100k
  const std::uint32_t length = declared_length & 0x00ffffffU;
64
100k
  output->push_back(static_cast<char>((length >> 16U) & 0xffU));
65
100k
  output->push_back(static_cast<char>((length >> 8U) & 0xffU));
66
100k
  output->push_back(static_cast<char>(length & 0xffU));
67
100k
  output->push_back(static_cast<char>(type));
68
100k
  output->push_back(static_cast<char>(flags));
69
100k
  AppendUint32(output, stream_id & 0x7fffffffU);
70
100k
  output->append(payload.data(), payload.size());
71
100k
}
72
73
26.5k
std::uint32_t CanonicalStatus(std::uint32_t status) {
74
26.5k
  if (status >= 100U && status <= 599U) {
75
26.5k
    return status;
76
26.5k
  }
77
0
  return status == 0U ? 200U : 100U + (status % 500U);
78
26.5k
}
79
80
120k
std::string BoundedString(const std::string& input, std::size_t limit) { return input.substr(0, limit); }
81
82
54.0k
std::string CanonicalHeaderName(const std::string& input) {
83
54.0k
  std::string name = BoundedString(input, scenario_limits::kMaxHttp3HeaderNameBytes);
84
54.0k
  if (name.empty()) {
85
0
    return "x-fuzz";
86
0
  }
87
475k
  for (char& byte : name) {
88
475k
    const unsigned char value = static_cast<unsigned char>(byte);
89
475k
    const bool alpha = (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z');
90
475k
    const bool digit = value >= '0' && value <= '9';
91
475k
    const bool punctuation = value == '!' || value == '#' || value == '$' || value == '%' || value == '&' ||
92
464k
                             value == '\'' || value == '*' || value == '+' || value == '-' || value == '.' ||
93
277k
                             value == '^' || value == '_' || value == '`' || value == '|' || value == '~';
94
475k
    if (value >= 'A' && value <= 'Z') {
95
0
      byte = static_cast<char>(value - 'A' + 'a');
96
475k
    } else if (!alpha && !digit && !punctuation) {
97
0
      byte = '-';
98
0
    }
99
475k
  }
100
54.0k
  return name;
101
54.0k
}
102
103
54.0k
std::string CanonicalHeaderValue(const std::string& input) {
104
54.0k
  std::string value = BoundedString(input, scenario_limits::kMaxMetadataBytes);
105
4.70M
  for (char& byte : value) {
106
4.70M
    const unsigned char character = static_cast<unsigned char>(byte);
107
4.70M
    if ((character < 0x20U && character != '\t') || character == 0x7fU) {
108
0
      byte = ' ';
109
0
    }
110
4.70M
  }
111
54.0k
  return value;
112
54.0k
}
113
114
}  // namespace
115
116
H2PlanDriver::H2PlanDriver()
117
31.7k
    : plan_(nullptr),
118
31.7k
      deflater_(nullptr),
119
31.7k
      next_action_(0),
120
31.7k
      remaining_yields_(0),
121
31.7k
      initial_settings_sent_(false),
122
31.7k
      client_settings_ack_sent_(false),
123
31.7k
      expect_client_preface_(true),
124
31.7k
      observed_client_settings_(0),
125
31.7k
      observed_client_priority_(0) {
126
31.7k
  (void)nghttp2_hd_deflate_new(&deflater_, 4096);
127
31.7k
}
128
129
31.7k
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
18.0k
void H2PlanDriver::Reset(const curl::fuzzer::proto::Http2Plan& plan, std::string scheme, std::string authority) {
136
18.0k
  plan_ = &plan;
137
18.0k
  next_action_ = 0;
138
18.0k
  remaining_yields_ = 0;
139
18.0k
  initial_settings_sent_ = false;
140
18.0k
  client_settings_ack_sent_ = false;
141
18.0k
  expect_client_preface_ = true;
142
18.0k
  observed_client_settings_ = 0;
143
18.0k
  observed_client_priority_ = 0;
144
18.0k
  client_bytes_.clear();
145
18.0k
  request_streams_.clear();
146
18.0k
  pushed_streams_.clear();
147
18.0k
  client_streams_.clear();
148
18.0k
  scheme_ = std::move(scheme);
149
18.0k
  authority_ = std::move(authority);
150
18.0k
  nghttp2_hd_deflate_del(deflater_);
151
18.0k
  deflater_ = nullptr;
152
18.0k
  (void)nghttp2_hd_deflate_new(&deflater_, 4096);
153
18.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
157k
void H2PlanDriver::ObserveIncomingData(const unsigned char* data, std::size_t size) {
159
157k
  if (data == nullptr || size == 0) {
160
0
    return;
161
0
  }
162
157k
  if (size > scenario_limits::kMaxHttp2ObservedBytes ||
163
157k
      client_bytes_.size() > scenario_limits::kMaxHttp2ObservedBytes - size) {
164
0
    client_bytes_.clear();
165
0
    expect_client_preface_ = false;
166
0
    return;
167
0
  }
168
157k
  client_bytes_.append(reinterpret_cast<const char*>(data), size);
169
157k
  ParseClientFrames();
170
157k
}
171
172
157k
void H2PlanDriver::ParseClientFrames() {
173
157k
  if (expect_client_preface_) {
174
29.3k
    if (client_bytes_.size() < kClientPreface.size()) {
175
0
      return;
176
0
    }
177
29.3k
    if (std::string_view(client_bytes_).substr(0, kClientPreface.size()) == kClientPreface) {
178
29.3k
      client_bytes_.erase(0, kClientPreface.size());
179
29.3k
    }
180
29.3k
    expect_client_preface_ = false;
181
29.3k
  }
182
183
157k
  std::size_t consumed = 0;
184
454k
  while (client_bytes_.size() - consumed >= kFrameHeaderSize) {
185
320k
    const auto* header = reinterpret_cast<const unsigned char*>(client_bytes_.data() + consumed);
186
320k
    const std::size_t payload_size = (static_cast<std::size_t>(header[0]) << 16U) |
187
320k
                                     (static_cast<std::size_t>(header[1]) << 8U) | static_cast<std::size_t>(header[2]);
188
320k
    if (payload_size > scenario_limits::kMaxHttp2ObservedBytes ||
189
320k
        client_bytes_.size() - consumed < kFrameHeaderSize + payload_size) {
190
24.0k
      break;
191
24.0k
    }
192
296k
    const std::uint8_t type = header[3];
193
296k
    const std::uint8_t flags = header[4];
194
296k
    const std::uint32_t stream_id = ReadUint32(client_bytes_.data() + consumed + 5) & 0x7fffffffU;
195
296k
    RecordClientFrame(type, flags, stream_id);
196
296k
    consumed += kFrameHeaderSize + payload_size;
197
296k
  }
198
157k
  client_bytes_.erase(0, consumed);
199
157k
}
200
201
296k
void H2PlanDriver::RecordClientFrame(std::uint8_t type, std::uint8_t flags, std::uint32_t stream_id) {
202
296k
  if (type == kSettingsFrame && stream_id == 0 && (flags & kFlagAck) == 0) {
203
29.3k
    ++observed_client_settings_;
204
29.3k
    return;
205
29.3k
  }
206
267k
  if (stream_id == 0) {
207
62.1k
    return;
208
62.1k
  }
209
210
204k
  if (type == kPriorityFrame) {
211
0
    ++observed_client_priority_;
212
0
  }
213
214
204k
  auto& stream = client_streams_[stream_id];
215
204k
  if (type == kHeadersFrame) {
216
45.0k
    ++stream.headers;
217
45.0k
    if ((stream_id & 1U) != 0 &&
218
45.0k
        std::find(request_streams_.begin(), request_streams_.end(), stream_id) == request_streams_.end()) {
219
45.0k
      request_streams_.push_back(stream_id);
220
45.0k
    }
221
159k
  } else if (type == kDataFrame) {
222
52.0k
    ++stream.data;
223
52.0k
  }
224
204k
  if ((type == kHeadersFrame || type == kDataFrame) && (flags & kFlagEndStream) != 0) {
225
43.3k
    ++stream.end_stream;
226
43.3k
  }
227
204k
}
228
229
std::uint32_t H2PlanDriver::ResolveStream(const curl::fuzzer::proto::Http2StreamRef& stream,
230
54.5k
                                          bool allow_connection) const {
231
54.5k
  using StreamRef = curl::fuzzer::proto::Http2StreamRef;
232
54.5k
  switch (stream.target_case()) {
233
1.86k
    case StreamRef::kConnection:
234
1.86k
      return allow_connection ? 0U : 1U;
235
2.67k
    case StreamRef::kRequestIndex: {
236
2.67k
      const std::size_t index = static_cast<std::size_t>(stream.request_index());
237
2.67k
      if (index < request_streams_.size()) {
238
1.84k
        return request_streams_[index];
239
1.84k
      }
240
829
      return 1U + 2U * static_cast<std::uint32_t>(index % 0x3fffffffU);
241
2.67k
    }
242
3.05k
    case StreamRef::kPushIndex: {
243
3.05k
      const std::size_t index = static_cast<std::size_t>(stream.push_index());
244
3.05k
      if (index < pushed_streams_.size()) {
245
1.77k
        return pushed_streams_[index];
246
1.77k
      }
247
1.27k
      return 2U + 2U * static_cast<std::uint32_t>(index % 0x3fffffffU);
248
3.05k
    }
249
3.22k
    case StreamRef::kExplicitId:
250
3.22k
      return stream.explicit_id() & 0x7fffffffU;
251
1.16k
    case StreamRef::kLatestRequest:
252
43.6k
    case StreamRef::TARGET_NOT_SET:
253
43.6k
    default:
254
43.6k
      return request_streams_.empty() ? 1U : request_streams_.back();
255
54.5k
  }
256
54.5k
}
257
258
10.1k
std::uint32_t H2PlanDriver::ResolvePromisedStream(const curl::fuzzer::proto::Http2StreamRef& stream) {
259
10.1k
  std::uint32_t stream_id = 0;
260
10.1k
  if (stream.target_case() == curl::fuzzer::proto::Http2StreamRef::TARGET_NOT_SET ||
261
1.88k
      stream.target_case() == curl::fuzzer::proto::Http2StreamRef::kLatestRequest ||
262
8.80k
      stream.target_case() == curl::fuzzer::proto::Http2StreamRef::kConnection) {
263
8.80k
    stream_id = 2U + 2U * static_cast<std::uint32_t>(pushed_streams_.size());
264
8.80k
  } else {
265
1.39k
    stream_id = ResolveStream(stream, false);
266
1.39k
  }
267
10.1k
  if (std::find(pushed_streams_.begin(), pushed_streams_.end(), stream_id) == pushed_streams_.end()) {
268
10.0k
    pushed_streams_.push_back(stream_id);
269
10.0k
  }
270
10.1k
  return stream_id;
271
10.1k
}
272
273
7.75k
bool H2PlanDriver::WaitSatisfied(const curl::fuzzer::proto::Http2Wait& wait) const {
274
7.75k
  const std::size_t required = std::max<std::size_t>(1, std::min<std::size_t>(wait.count(), 16));
275
7.75k
  if (wait.event() == curl::fuzzer::proto::HTTP2_CLIENT_EVENT_SETTINGS) {
276
2.42k
    return observed_client_settings_ >= required;
277
2.42k
  }
278
5.32k
  const std::uint32_t stream_id = ResolveStream(wait.stream(), false);
279
5.32k
  const auto found = client_streams_.find(stream_id);
280
5.32k
  if (found == client_streams_.end()) {
281
1.94k
    return false;
282
1.94k
  }
283
3.38k
  switch (wait.event()) {
284
1.22k
    case curl::fuzzer::proto::HTTP2_CLIENT_EVENT_HEADERS:
285
1.22k
      return found->second.headers >= required;
286
509
    case curl::fuzzer::proto::HTTP2_CLIENT_EVENT_DATA:
287
509
      return found->second.data >= required;
288
1.64k
    case curl::fuzzer::proto::HTTP2_CLIENT_EVENT_END_STREAM:
289
1.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
3.38k
  }
294
3.38k
}
295
296
37.4k
bool H2PlanDriver::EncodeHeaders(const std::vector<std::pair<std::string, std::string>>& headers, std::string* output) {
297
37.4k
  if (deflater_ == nullptr || output == nullptr) {
298
0
    return false;
299
0
  }
300
37.4k
  std::vector<nghttp2_nv> name_values;
301
37.4k
  name_values.reserve(headers.size());
302
121k
  for (const auto& header : headers) {
303
121k
    nghttp2_nv nv{};
304
121k
    nv.name = reinterpret_cast<std::uint8_t*>(const_cast<char*>(header.first.data()));
305
121k
    nv.value = reinterpret_cast<std::uint8_t*>(const_cast<char*>(header.second.data()));
306
121k
    nv.namelen = header.first.size();
307
121k
    nv.valuelen = header.second.size();
308
121k
    nv.flags = NGHTTP2_NV_FLAG_NONE;
309
121k
    name_values.push_back(nv);
310
121k
  }
311
37.4k
  const std::size_t bound = nghttp2_hd_deflate_bound(deflater_, name_values.data(), name_values.size());
312
37.4k
  if (bound > scenario_limits::kMaxHttp2HeaderBytes * 2U) {
313
0
    return false;
314
0
  }
315
37.4k
  output->resize(bound);
316
37.4k
  const nghttp2_ssize encoded = nghttp2_hd_deflate_hd2(deflater_, reinterpret_cast<std::uint8_t*>(output->data()),
317
37.4k
                                                       output->size(), name_values.data(), name_values.size());
318
37.4k
  if (encoded < 0) {
319
0
    output->clear();
320
0
    return false;
321
0
  }
322
37.4k
  output->resize(static_cast<std::size_t>(encoded));
323
37.4k
  return true;
324
37.4k
}
325
326
84.6k
bool H2PlanDriver::SerializeAction(const curl::fuzzer::proto::Http2Action& action, std::string* output) {
327
84.6k
  using Action = curl::fuzzer::proto::Http2Action;
328
84.6k
  output->clear();
329
84.6k
  switch (action.action_case()) {
330
24.7k
    case Action::kSettings: {
331
24.7k
      std::string payload;
332
24.7k
      const auto& settings = action.settings();
333
24.7k
      if (!settings.ack()) {
334
24.5k
        const std::size_t count = std::min<std::size_t>(scenario_limits::kMaxHttp2Settings,
335
24.5k
                                                        static_cast<std::size_t>(settings.entries_size()));
336
24.5k
        payload.reserve(count * 6U);
337
39.2k
        for (std::size_t index = 0; index < count; ++index) {
338
14.7k
          const auto& entry = settings.entries(static_cast<int>(index));
339
14.7k
          const std::uint16_t identifier = static_cast<std::uint16_t>(entry.identifier());
340
14.7k
          payload.push_back(static_cast<char>((identifier >> 8U) & 0xffU));
341
14.7k
          payload.push_back(static_cast<char>(identifier & 0xffU));
342
14.7k
          AppendUint32(&payload, entry.value());
343
14.7k
        }
344
24.5k
      }
345
24.7k
      AppendFrame(output, payload.size(), kSettingsFrame, settings.ack() ? kFlagAck : 0, 0, payload);
346
24.7k
      return true;
347
0
    }
348
27.2k
    case Action::kHeaders: {
349
27.2k
      const auto& headers = action.headers();
350
27.2k
      std::vector<std::pair<std::string, std::string>> fields;
351
27.2k
      const std::size_t count =
352
27.2k
          std::min<std::size_t>(scenario_limits::kMaxHttp2Headers, static_cast<std::size_t>(headers.fields_size()));
353
27.2k
      fields.reserve(count + 1U);
354
27.2k
      if (!headers.trailers()) {
355
26.5k
        fields.emplace_back(":status", std::to_string(CanonicalStatus(headers.status_code())));
356
26.5k
      }
357
27.2k
      std::size_t bytes = 0;
358
65.3k
      for (std::size_t index = 0; index < count && bytes < scenario_limits::kMaxHttp2HeaderBytes; ++index) {
359
38.0k
        const auto& field = headers.fields(static_cast<int>(index));
360
38.0k
        std::string name = CanonicalHeaderName(field.name());
361
38.0k
        std::string value = CanonicalHeaderValue(field.value());
362
38.0k
        const std::size_t remaining = scenario_limits::kMaxHttp2HeaderBytes - bytes;
363
38.0k
        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
38.0k
        bytes += name.size() + value.size();
368
38.0k
        fields.emplace_back(std::move(name), std::move(value));
369
38.0k
      }
370
27.2k
      std::string block;
371
27.2k
      if (!EncodeHeaders(fields, &block)) {
372
0
        return false;
373
0
      }
374
27.2k
      const std::uint32_t stream_id = ResolveStream(headers.stream(), false);
375
27.2k
      std::size_t offset = 0;
376
27.2k
      do {
377
27.2k
        const std::size_t chunk_size = std::min(kMaxFramePayload, block.size() - offset);
378
27.2k
        const bool last = offset + chunk_size == block.size();
379
27.2k
        std::uint8_t flags = last ? kFlagEndHeaders : 0;
380
27.2k
        if (offset == 0 && headers.end_stream()) {
381
20.3k
          flags |= kFlagEndStream;
382
20.3k
        }
383
27.2k
        AppendFrame(output, chunk_size, offset == 0 ? kHeadersFrame : kContinuationFrame, flags, stream_id,
384
27.2k
                    std::string_view(block).substr(offset, chunk_size));
385
27.2k
        offset += chunk_size;
386
27.2k
      } while (offset < block.size());
387
27.2k
      return true;
388
27.2k
    }
389
1.78k
    case Action::kData: {
390
1.78k
      const auto& data = action.data();
391
1.78k
      const std::string payload = BoundedString(data.data(), scenario_limits::kMaxHttp2DataBytes);
392
1.78k
      const std::uint32_t stream_id = ResolveStream(data.stream(), false);
393
1.78k
      std::size_t offset = 0;
394
1.79k
      do {
395
1.79k
        const std::size_t chunk_size = std::min(kMaxFramePayload, payload.size() - offset);
396
1.79k
        const bool last = offset + chunk_size == payload.size();
397
1.79k
        AppendFrame(output, chunk_size, kDataFrame, last && data.end_stream() ? kFlagEndStream : 0, stream_id,
398
1.79k
                    std::string_view(payload).substr(offset, chunk_size));
399
1.79k
        offset += chunk_size;
400
1.79k
      } while (offset < payload.size());
401
1.78k
      return true;
402
27.2k
    }
403
3.01k
    case Action::kWindowUpdate: {
404
3.01k
      const auto& update = action.window_update();
405
3.01k
      std::string payload;
406
3.01k
      AppendUint32(&payload, std::max(1U, update.increment() & 0x7fffffffU));
407
3.01k
      AppendFrame(output, payload.size(), kWindowUpdateFrame, 0, ResolveStream(update.stream(), true), payload);
408
3.01k
      return true;
409
27.2k
    }
410
2.15k
    case Action::kRstStream: {
411
2.15k
      const auto& reset = action.rst_stream();
412
2.15k
      std::string payload;
413
2.15k
      AppendUint32(&payload, reset.error_code());
414
2.15k
      AppendFrame(output, payload.size(), kRstStreamFrame, 0, ResolveStream(reset.stream(), false), payload);
415
2.15k
      return true;
416
27.2k
    }
417
4.60k
    case Action::kPing: {
418
4.60k
      std::array<char, 8> opaque{};
419
4.60k
      const std::string& source = action.ping().opaque_data();
420
4.60k
      std::copy_n(source.begin(), std::min(source.size(), opaque.size()), opaque.begin());
421
4.60k
      AppendFrame(output, opaque.size(), kPingFrame, action.ping().ack() ? kFlagAck : 0, 0,
422
4.60k
                  std::string_view(opaque.data(), opaque.size()));
423
4.60k
      return true;
424
27.2k
    }
425
3.37k
    case Action::kGoaway: {
426
3.37k
      const auto& goaway = action.goaway();
427
3.37k
      std::string payload;
428
3.37k
      AppendUint32(&payload, ResolveStream(goaway.last_stream(), true));
429
3.37k
      AppendUint32(&payload, goaway.error_code());
430
3.37k
      payload.append(BoundedString(goaway.debug_data(), scenario_limits::kMaxHttp2RawFrameBytes));
431
3.37k
      AppendFrame(output, payload.size(), kGoawayFrame, 0, 0, payload);
432
3.37k
      return true;
433
27.2k
    }
434
10.1k
    case Action::kPushPromise: {
435
10.1k
      const auto& promise = action.push_promise();
436
10.1k
      std::vector<std::pair<std::string, std::string>> fields = {
437
10.1k
          {":method", "GET"}, {":scheme", scheme_}, {":authority", authority_}, {":path", "/push"}};
438
10.1k
      const std::size_t count = std::min<std::size_t>(scenario_limits::kMaxHttp2Headers,
439
10.1k
                                                      static_cast<std::size_t>(promise.request_headers_size()));
440
26.1k
      for (std::size_t index = 0; index < count; ++index) {
441
15.9k
        const auto& field = promise.request_headers(static_cast<int>(index));
442
15.9k
        fields.emplace_back(CanonicalHeaderName(field.name()), CanonicalHeaderValue(field.value()));
443
15.9k
      }
444
10.1k
      std::string block;
445
10.1k
      if (!EncodeHeaders(fields, &block)) {
446
0
        return false;
447
0
      }
448
10.1k
      const std::uint32_t parent = ResolveStream(promise.parent_stream(), false);
449
10.1k
      const std::uint32_t promised = ResolvePromisedStream(promise.promised_stream());
450
10.1k
      std::string first_payload;
451
10.1k
      AppendUint32(&first_payload, promised);
452
10.1k
      const std::size_t first_size = std::min(kMaxFramePayload - 4U, block.size());
453
10.1k
      first_payload.append(block.data(), first_size);
454
10.1k
      AppendFrame(output, first_payload.size(), kPushPromiseFrame, first_size == block.size() ? kFlagEndHeaders : 0,
455
10.1k
                  parent, first_payload);
456
10.1k
      std::size_t offset = first_size;
457
10.2k
      while (offset < block.size()) {
458
21
        const std::size_t chunk_size = std::min(kMaxFramePayload, block.size() - offset);
459
21
        offset += chunk_size;
460
21
        AppendFrame(output, chunk_size, kContinuationFrame, offset == block.size() ? kFlagEndHeaders : 0, parent,
461
21
                    std::string_view(block).substr(offset - chunk_size, chunk_size));
462
21
      }
463
10.1k
      return true;
464
10.1k
    }
465
5.39k
    case Action::kWireFrame: {
466
5.39k
      const auto& frame = action.wire_frame();
467
5.39k
      const std::string payload = BoundedString(frame.payload(), scenario_limits::kMaxHttp2RawFrameBytes);
468
5.39k
      const std::uint32_t declared = frame.use_declared_length() ? frame.declared_length() : payload.size();
469
5.39k
      AppendFrame(output, declared, static_cast<std::uint8_t>(frame.type()), static_cast<std::uint8_t>(frame.flags()),
470
5.39k
                  frame.stream_id(), payload);
471
5.39k
      return true;
472
10.1k
    }
473
2.13k
    case Action::kRawBytes:
474
2.13k
      *output = BoundedString(action.raw_bytes(), scenario_limits::kMaxHttp2RawFrameBytes);
475
2.13k
      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
84.6k
  }
482
84.6k
}
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
289k
bool H2PlanDriver::NextChunk(std::string* output) {
490
289k
  if (output == nullptr || plan_ == nullptr) {
491
0
    return false;
492
0
  }
493
289k
  output->clear();
494
289k
  if (!initial_settings_sent_) {
495
17.8k
    curl::fuzzer::proto::Http2Action initial;
496
17.8k
    *initial.mutable_settings() = plan_->initial_settings();
497
17.8k
    initial.mutable_settings()->set_ack(false);
498
17.8k
    (void)SerializeAction(initial, output);
499
17.8k
    initial_settings_sent_ = true;
500
17.8k
    return true;
501
17.8k
  }
502
271k
  if (!client_settings_ack_sent_) {
503
17.6k
    if (observed_client_settings_ == 0) {
504
1
      return false;
505
1
    }
506
17.6k
    AppendFrame(output, 0, kSettingsFrame, kFlagAck, 0, {});
507
17.6k
    client_settings_ack_sent_ = true;
508
17.6k
    return true;
509
17.6k
  }
510
511
254k
  const std::size_t action_count =
512
254k
      std::min<std::size_t>(scenario_limits::kMaxHttp2Actions, static_cast<std::size_t>(plan_->actions_size()));
513
254k
  if (next_action_ >= action_count) {
514
86.0k
    return false;
515
86.0k
  }
516
168k
  const auto& action = plan_->actions(static_cast<int>(next_action_));
517
168k
  if (action.action_case() == curl::fuzzer::proto::Http2Action::kWait) {
518
7.75k
    if (!WaitSatisfied(action.wait())) {
519
5.81k
      return false;
520
5.81k
    }
521
1.93k
    ++next_action_;
522
1.93k
    return true;
523
7.75k
  }
524
160k
  if (action.action_case() == curl::fuzzer::proto::Http2Action::kYieldTurns) {
525
93.4k
    if (remaining_yields_ == 0) {
526
7.79k
      remaining_yields_ = std::min<std::size_t>(scenario_limits::kMaxHttp2YieldTurns, action.yield_turns());
527
7.79k
      if (remaining_yields_ == 0) {
528
959
        ++next_action_;
529
959
        return true;
530
959
      }
531
7.79k
    }
532
92.5k
    --remaining_yields_;
533
92.5k
    if (remaining_yields_ == 0) {
534
6.70k
      ++next_action_;
535
6.70k
    }
536
92.5k
    return true;
537
93.4k
  }
538
539
66.8k
  (void)SerializeAction(action, output);
540
66.8k
  ++next_action_;
541
66.8k
  return true;
542
160k
}
543
544
/// @return true while startup or a bounded action remains to be processed.
545
31.5k
bool H2PlanDriver::has_pending_work() const {
546
31.5k
  if (plan_ == nullptr || !initial_settings_sent_ || !client_settings_ack_sent_) {
547
13.9k
    return plan_ != nullptr;
548
13.9k
  }
549
17.6k
  return next_action_ <
550
17.6k
         std::min<std::size_t>(scenario_limits::kMaxHttp2Actions, static_cast<std::size_t>(plan_->actions_size()));
551
31.5k
}
552
553
/// @return number of distinct client request streams observed.
554
37.3k
std::size_t H2PlanDriver::observed_request_count() const { return request_streams_.size(); }
555
556
/// @return number of non-ACK client SETTINGS frames observed.
557
31.5k
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