3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/string_util.h>
25 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
26 #include "src/core/lib/iomgr/executor.h"
27 #include "src/core/lib/slice/slice_internal.h"
28 #include "src/core/lib/surface/channel.h"
29 #include "test/core/util/memory_counters.h"
30 #include "test/core/util/mock_endpoint.h"
33 bool leak_check = true;
35 static void discard_write(grpc_slice slice) {}
37 static void* tag(int n) { return (void*)static_cast<uintptr_t>(n); }
39 static void dont_log(gpr_log_func_args* args) {}
41 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
42 grpc_test_only_set_slice_hash_seed(0);
43 if (squelch) gpr_set_log_function(dont_log);
44 grpc_core::testing::LeakDetector leak_detector(leak_check);
47 grpc_core::ExecCtx exec_ctx;
48 grpc_core::Executor::SetThreadingAll(false);
50 grpc_resource_quota* resource_quota =
51 grpc_resource_quota_create("client_fuzzer");
52 grpc_endpoint* mock_endpoint =
53 grpc_mock_endpoint_create(discard_write, resource_quota);
54 grpc_resource_quota_unref_internal(resource_quota);
56 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
57 grpc_transport* transport =
58 grpc_create_chttp2_transport(nullptr, mock_endpoint, true);
59 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr);
61 grpc_arg authority_arg = grpc_channel_arg_string_create(
62 const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY),
63 const_cast<char*>("test-authority"));
64 grpc_channel_args* args =
65 grpc_channel_args_copy_and_add(nullptr, &authority_arg, 1);
66 grpc_channel* channel = grpc_channel_create(
67 "test-target", args, GRPC_CLIENT_DIRECT_CHANNEL, transport);
68 grpc_channel_args_destroy(args);
69 grpc_slice host = grpc_slice_from_static_string("localhost");
70 grpc_call* call = grpc_channel_create_call(
71 channel, nullptr, 0, cq, grpc_slice_from_static_string("/foo"), &host,
72 gpr_inf_future(GPR_CLOCK_REALTIME), nullptr);
74 grpc_metadata_array initial_metadata_recv;
75 grpc_metadata_array_init(&initial_metadata_recv);
76 grpc_byte_buffer* response_payload_recv = nullptr;
77 grpc_metadata_array trailing_metadata_recv;
78 grpc_metadata_array_init(&trailing_metadata_recv);
79 grpc_status_code status;
80 grpc_slice details = grpc_empty_slice();
83 memset(ops, 0, sizeof(ops));
85 op->op = GRPC_OP_SEND_INITIAL_METADATA;
86 op->data.send_initial_metadata.count = 0;
88 op->reserved = nullptr;
90 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
92 op->reserved = nullptr;
94 op->op = GRPC_OP_RECV_INITIAL_METADATA;
95 op->data.recv_initial_metadata.recv_initial_metadata =
96 &initial_metadata_recv;
98 op->reserved = nullptr;
100 op->op = GRPC_OP_RECV_MESSAGE;
101 op->data.recv_message.recv_message = &response_payload_recv;
103 op->reserved = nullptr;
105 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
106 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
107 op->data.recv_status_on_client.status = &status;
108 op->data.recv_status_on_client.status_details = &details;
110 op->reserved = nullptr;
112 grpc_call_error error =
113 grpc_call_start_batch(call, ops, (size_t)(op - ops), tag(1), nullptr);
114 int requested_calls = 1;
115 GPR_ASSERT(GRPC_CALL_OK == error);
117 grpc_mock_endpoint_put_read(
118 mock_endpoint, grpc_slice_from_copied_buffer((const char*)data, size));
122 grpc_core::ExecCtx::Get()->Flush();
123 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
126 case GRPC_QUEUE_TIMEOUT:
128 case GRPC_QUEUE_SHUTDOWN:
130 case GRPC_OP_COMPLETE:
137 if (requested_calls) {
138 grpc_call_cancel(call, nullptr);
140 for (int i = 0; i < requested_calls; i++) {
141 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
143 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
145 grpc_completion_queue_shutdown(cq);
146 for (int i = 0; i < requested_calls; i++) {
147 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
149 GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN);
151 grpc_call_unref(call);
152 grpc_completion_queue_destroy(cq);
153 grpc_metadata_array_destroy(&initial_metadata_recv);
154 grpc_metadata_array_destroy(&trailing_metadata_recv);
155 grpc_slice_unref(details);
156 grpc_channel_destroy(channel);
157 if (response_payload_recv != nullptr) {
158 grpc_byte_buffer_destroy(response_payload_recv);
161 grpc_shutdown_blocking();