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.
19 #include <grpc/grpc.h>
21 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
22 #include "src/core/lib/iomgr/executor.h"
23 #include "src/core/lib/slice/slice_internal.h"
24 #include "src/core/lib/surface/server.h"
25 #include "test/core/util/mock_endpoint.h"
28 bool leak_check = true;
30 static void discard_write(grpc_slice /*slice*/) {}
32 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
34 static void dont_log(gpr_log_func_args* /*args*/) {}
36 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
37 grpc_test_only_set_slice_hash_seed(0);
38 if (squelch) gpr_set_log_function(dont_log);
41 grpc_core::ExecCtx exec_ctx;
42 grpc_core::Executor::SetThreadingAll(false);
44 grpc_resource_quota* resource_quota =
45 grpc_resource_quota_create("server_fuzzer");
46 grpc_endpoint* mock_endpoint =
47 grpc_mock_endpoint_create(discard_write, resource_quota);
48 grpc_resource_quota_unref_internal(resource_quota);
49 grpc_mock_endpoint_put_read(
50 mock_endpoint, grpc_slice_from_copied_buffer((const char*)data, size));
52 grpc_server* server = grpc_server_create(nullptr, nullptr);
53 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
54 grpc_server_register_completion_queue(server, cq, nullptr);
55 // TODO(ctiller): add more registered methods (one for POST, one for PUT)
56 grpc_server_register_method(server, "/reg", nullptr, {}, 0);
57 grpc_server_start(server);
58 grpc_transport* transport =
59 grpc_create_chttp2_transport(nullptr, mock_endpoint, false);
60 server->core_server->SetupTransport(transport, nullptr, nullptr, nullptr);
61 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
63 grpc_call* call1 = nullptr;
64 grpc_call_details call_details1;
65 grpc_metadata_array request_metadata1;
66 grpc_call_details_init(&call_details1);
67 grpc_metadata_array_init(&request_metadata1);
68 int requested_calls = 0;
70 GPR_ASSERT(GRPC_CALL_OK ==
71 grpc_server_request_call(server, &call1, &call_details1,
72 &request_metadata1, cq, cq, tag(1)));
77 grpc_core::ExecCtx::Get()->Flush();
78 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
81 case GRPC_QUEUE_TIMEOUT:
83 case GRPC_QUEUE_SHUTDOWN:
85 case GRPC_OP_COMPLETE:
86 if (ev.tag == tag(1)) {
88 // TODO(ctiller): keep reading that call!
95 if (call1 != nullptr) grpc_call_unref(call1);
96 grpc_call_details_destroy(&call_details1);
97 grpc_metadata_array_destroy(&request_metadata1);
98 grpc_server_shutdown_and_notify(server, cq, tag(0xdead));
99 grpc_server_cancel_all_calls(server);
100 grpc_millis deadline = grpc_core::ExecCtx::Get()->Now() + 5000;
101 for (int i = 0; i <= requested_calls; i++) {
102 // A single grpc_completion_queue_next might not be sufficient for getting
103 // the tag from shutdown, because we might potentially get blocked by
104 // an operation happening on the timer thread.
105 // For example, the deadline timer might expire, leading to the timer
106 // thread trying to cancel the RPC and thereby acquiring a few references
107 // to the call. This will prevent the shutdown to complete till the timer
108 // thread releases those references.
109 // As a solution, we are going to keep performing a cq_next for a
110 // liberal period of 5 seconds for the timer thread to complete its work.
112 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
114 grpc_core::ExecCtx::Get()->InvalidateNow();
115 } while (ev.type != GRPC_OP_COMPLETE &&
116 grpc_core::ExecCtx::Get()->Now() < deadline);
117 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
119 grpc_completion_queue_shutdown(cq);
120 for (int i = 0; i <= requested_calls; i++) {
122 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
124 grpc_core::ExecCtx::Get()->InvalidateNow();
125 } while (ev.type != GRPC_QUEUE_SHUTDOWN &&
126 grpc_core::ExecCtx::Get()->Now() < deadline);
127 GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN);
129 grpc_server_destroy(server);
130 grpc_completion_queue_destroy(cq);