hpack_parser_fuzzer_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <stdint.h>
20 #include <string.h>
21 
22 #include <grpc/grpc.h>
23 #include <grpc/support/alloc.h>
24 #include <grpc/support/log.h>
25 
30 #include "src/libfuzzer/libfuzzer_macro.h"
31 #include "test/core/transport/chttp2/hpack_parser_fuzzer.pb.h"
32 
33 bool squelch = true;
34 bool leak_check = true;
35 
36 static void dont_log(gpr_log_func_args* /*args*/) {}
37 
38 DEFINE_PROTO_FUZZER(const hpack_parser_fuzzer::Msg& msg) {
41  grpc_init();
42  auto memory_allocator = grpc_core::ResourceQuota::Default()
43  ->memory_quota()
44  ->CreateMemoryAllocator("test-allocator");
45  {
46  std::unique_ptr<grpc_core::HPackParser> parser(new grpc_core::HPackParser);
47  for (int i = 0; i < msg.frames_size(); i++) {
48  auto arena = grpc_core::MakeScopedArena(1024, &memory_allocator);
51 
52  const auto& frame = msg.frames(i);
55  if (frame.end_of_headers()) {
57  }
58  if (frame.end_of_stream()) {
60  }
63  if (frame.priority()) {
65  }
66  int max_length = 1024;
67  if (frame.max_metadata_length() != 0) {
68  max_length = frame.max_metadata_length();
69  }
70 
71  parser->BeginFrame(
72  &b, max_length, boundary, priority,
75  int stop_buffering_ctr =
76  std::max(-1, frame.stop_buffering_after_segments());
77  for (const auto& parse : frame.parse()) {
80  GRPC_ERROR_UNREF(parser->Parse(buffer, i == msg.frames_size() - 1));
82  stop_buffering_ctr--;
83  if (0 == stop_buffering_ctr) parser->StopBufferingFrame();
84  }
85  parser->FinishFrame();
86  }
87  }
88  grpc_shutdown();
89 }
grpc_core::HPackParser::LogInfo
Definition: hpack_parser.h:59
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
grpc_core::MakeScopedArena
ScopedArenaPtr MakeScopedArena(size_t initial_size, MemoryAllocator *memory_allocator)
Definition: src/core/lib/resource_quota/arena.h:130
log.h
grpc_core::HPackParser::Priority
Priority
Definition: hpack_parser.h:52
priority
int priority
Definition: abseil-cpp/absl/synchronization/internal/graphcycles.cc:286
string.h
dont_log
static void dont_log(gpr_log_func_args *)
Definition: hpack_parser_fuzzer_test.cc:36
grpc_core::HPackParser
Definition: hpack_parser.h:40
grpc_core::HPackParser::Boundary::None
@ None
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
gpr_log_func_args
Definition: include/grpc/impl/codegen/log.h:77
grpc_core::HPackParser::Boundary::EndOfHeaders
@ EndOfHeaders
grpc_core::HPackParser::Boundary
Boundary
Definition: hpack_parser.h:43
leak_check
bool leak_check
Definition: hpack_parser_fuzzer_test.cc:34
hpack_parser.h
asyncio_get_stats.parser
parser
Definition: asyncio_get_stats.py:34
grpc_test_only_set_slice_hash_seed
void grpc_test_only_set_slice_hash_seed(uint32_t seed)
Definition: slice_refcount.cc:33
grpc_core::HPackParser::Boundary::EndOfStream
@ EndOfStream
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc.h
grpc_core::ResourceQuota::memory_quota
MemoryQuotaRefPtr memory_quota()
Definition: src/core/lib/resource_quota/resource_quota.h:51
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition: resource_quota.cc:27
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
slice_internal.h
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
resource_quota.h
grpc_core::ExecCtx
Definition: exec_ctx.h:97
stdint.h
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
squelch
bool squelch
Definition: hpack_parser_fuzzer_test.cc:33
grpc_core::HPackParser::Priority::Included
@ Included
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
grpc_core::HPackParser::Priority::None
@ None
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
alloc.h
parse
static void parse(const char *s)
Definition: debug/trace.cc:121
frame
static void frame(frame_handler *handler, unsigned char *payload, size_t payload_length, size_t write_length)
Definition: frame_handler_test.cc:65
exec_ctx.h
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
DEFINE_PROTO_FUZZER
DEFINE_PROTO_FUZZER(const hpack_parser_fuzzer::Msg &msg)
Definition: hpack_parser_fuzzer_test.cc:38
gpr_set_log_function
GPRAPI void gpr_set_log_function(gpr_log_func func)
Definition: log.cc:143
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_core::HPackParser::LogInfo::kHeaders
@ kHeaders
Definition: hpack_parser.h:64
grpc_metadata_batch
Definition: metadata_batch.h:1259
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:13