Debian Codegraph @

test/core/transport/chttp2/hpack_parser_fuzzer_test.cc

raw
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 <algorithm>
20 #include <memory>
21 #include <string>
22
23 #include <grpc/grpc.h>
24 #include <grpc/slice.h>
25 #include <grpc/support/log.h>
26
27 #include "src/core/ext/transport/chttp2/transport/hpack_parser.h"
28 #include "src/core/lib/gprpp/ref_counted_ptr.h"
29 #include "src/core/lib/iomgr/exec_ctx.h"
30 #include "src/core/lib/resource_quota/arena.h"
31 #include "src/core/lib/resource_quota/memory_quota.h"
32 #include "src/core/lib/resource_quota/resource_quota.h"
33 #include "src/core/lib/transport/metadata_batch.h"
34 #include "src/libfuzzer/libfuzzer_macro.h"
35 #include "test/core/transport/chttp2/hpack_parser_fuzzer.pb.h"
36
37 // IWYU pragma: no_include <google/protobuf/repeated_ptr_field.h>
38
39 bool squelch = true;
40 bool leak_check = true;
41
42 static void dont_log(gpr_log_func_args* /*args*/) {}
43
44 DEFINE_PROTO_FUZZER(const hpack_parser_fuzzer::Msg& msg) {
45 if (squelch) gpr_set_log_function(dont_log);
46 grpc_init();
47 auto memory_allocator = grpc_core::ResourceQuota::Default()
48 ->memory_quota()
49 ->CreateMemoryAllocator("test-allocator");
50 {
51 std::unique_ptr<grpc_core::HPackParser> parser(new grpc_core::HPackParser);
52 for (int i = 0; i < msg.frames_size(); i++) {
53 auto arena = grpc_core::MakeScopedArena(1024, &memory_allocator);
54 grpc_core::ExecCtx exec_ctx;
55 grpc_metadata_batch b(arena.get());
56
57 const auto& frame = msg.frames(i);
58 grpc_core::HPackParser::Boundary boundary =
59 grpc_core::HPackParser::Boundary::None;
60 if (frame.end_of_headers()) {
61 boundary = grpc_core::HPackParser::Boundary::EndOfHeaders;
62 }
63 if (frame.end_of_stream()) {
64 boundary = grpc_core::HPackParser::Boundary::EndOfStream;
65 }
66 grpc_core::HPackParser::Priority priority =
67 grpc_core::HPackParser::Priority::None;
68 if (frame.priority()) {
69 priority = grpc_core::HPackParser::Priority::Included;
70 }
71 int max_length = 1024;
72 if (frame.max_metadata_length() != 0) {
73 max_length = frame.max_metadata_length();
74 }
75
76 parser->BeginFrame(
77 &b, max_length, boundary, priority,
78 grpc_core::HPackParser::LogInfo{
79 1, grpc_core::HPackParser::LogInfo::kHeaders, false});
80 int stop_buffering_ctr =
81 std::max(-1, frame.stop_buffering_after_segments());
82 for (const auto& parse : frame.parse()) {
83 grpc_slice buffer =
84 grpc_slice_from_copied_buffer(parse.data(), parse.size());
85 (void)parser->Parse(buffer, i == msg.frames_size() - 1);
86 grpc_slice_unref(buffer);
87 stop_buffering_ctr--;
88 if (0 == stop_buffering_ctr) parser->StopBufferingFrame();
89 }
90 parser->FinishFrame();
91 }
92 }
93 grpc_shutdown();
94 }