Debian Codegraph @

test/core/end2end/fuzzers/server_fuzzer.cc

raw
1 /*
2 *
3 * Copyright 2016 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 <stddef.h>
20 #include <stdint.h>
21
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <grpc/support/log.h>
25 #include <grpc/support/time.h>
26
27 #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/channel/channel_args_preconditioning.h"
30 #include "src/core/lib/channel/channelz.h"
31 #include "src/core/lib/config/core_configuration.h"
32 #include "src/core/lib/gprpp/time.h"
33 #include "src/core/lib/iomgr/endpoint.h"
34 #include "src/core/lib/iomgr/error.h"
35 #include "src/core/lib/iomgr/exec_ctx.h"
36 #include "src/core/lib/iomgr/executor.h"
37 #include "src/core/lib/resource_quota/api.h"
38 #include "src/core/lib/surface/server.h"
39 #include "src/core/lib/transport/transport_fwd.h"
40 #include "test/core/util/mock_endpoint.h"
41
42 bool squelch = true;
43 bool leak_check = true;
44
45 static void discard_write(grpc_slice /*slice*/) {}
46
47 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
48
49 static void dont_log(gpr_log_func_args* /*args*/) {}
50
51 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
52 if (squelch) gpr_set_log_function(dont_log);
53 grpc_init();
54 {
55 grpc_core::ExecCtx exec_ctx;
56 grpc_core::Executor::SetThreadingAll(false);
57 grpc_resource_quota* resource_quota =
58 grpc_resource_quota_create("context_list_test");
59 grpc_endpoint* mock_endpoint = grpc_mock_endpoint_create(discard_write);
60 grpc_mock_endpoint_put_read(
61 mock_endpoint, grpc_slice_from_copied_buffer((const char*)data, size));
62 grpc_server* server = grpc_server_create(nullptr, nullptr);
63 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
64 grpc_server_register_completion_queue(server, cq, nullptr);
65 // TODO(ctiller): add more registered methods (one for POST, one for PUT)
66 grpc_server_register_method(server, "/reg", nullptr, {}, 0);
67 grpc_server_start(server);
68 grpc_core::ChannelArgs channel_args = grpc_core::CoreConfiguration::Get()
69 .channel_args_preconditioning()
70 .PreconditionChannelArgs(nullptr);
71 grpc_transport* transport =
72 grpc_create_chttp2_transport(channel_args, mock_endpoint, false);
73 grpc_resource_quota_unref(resource_quota);
74 GPR_ASSERT(GRPC_LOG_IF_ERROR(
75 "SetupTransport", grpc_core::Server::FromC(server)->SetupTransport(
76 transport, nullptr, channel_args, nullptr)));
77 grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
78
79 grpc_call* call1 = nullptr;
80 grpc_call_details call_details1;
81 grpc_metadata_array request_metadata1;
82 grpc_call_details_init(&call_details1);
83 grpc_metadata_array_init(&request_metadata1);
84 int requested_calls = 0;
85
86 GPR_ASSERT(GRPC_CALL_OK ==
87 grpc_server_request_call(server, &call1, &call_details1,
88 &request_metadata1, cq, cq, tag(1)));
89 requested_calls++;
90
91 grpc_event ev;
92 while (true) {
93 grpc_core::ExecCtx::Get()->Flush();
94 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
95 nullptr);
96 switch (ev.type) {
97 case GRPC_QUEUE_TIMEOUT:
98 goto done;
99 case GRPC_QUEUE_SHUTDOWN:
100 break;
101 case GRPC_OP_COMPLETE:
102 if (ev.tag == tag(1)) {
103 requested_calls--;
104 // TODO(ctiller): keep reading that call!
105 }
106 break;
107 }
108 }
109
110 done:
111 if (call1 != nullptr) grpc_call_unref(call1);
112 grpc_call_details_destroy(&call_details1);
113 grpc_metadata_array_destroy(&request_metadata1);
114 grpc_server_shutdown_and_notify(server, cq, tag(0xdead));
115 grpc_server_cancel_all_calls(server);
116 grpc_core::Timestamp deadline =
117 grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(5);
118 for (int i = 0; i <= requested_calls; i++) {
119 // A single grpc_completion_queue_next might not be sufficient for getting
120 // the tag from shutdown, because we might potentially get blocked by
121 // an operation happening on the timer thread.
122 // For example, the deadline timer might expire, leading to the timer
123 // thread trying to cancel the RPC and thereby acquiring a few references
124 // to the call. This will prevent the shutdown to complete till the timer
125 // thread releases those references.
126 // As a solution, we are going to keep performing a cq_next for a
127 // liberal period of 5 seconds for the timer thread to complete its work.
128 do {
129 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
130 nullptr);
131 grpc_core::ExecCtx::Get()->InvalidateNow();
132 } while (ev.type != GRPC_OP_COMPLETE &&
133 grpc_core::Timestamp::Now() < deadline);
134 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
135 }
136 grpc_completion_queue_shutdown(cq);
137 for (int i = 0; i <= requested_calls; i++) {
138 do {
139 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
140 nullptr);
141 grpc_core::ExecCtx::Get()->InvalidateNow();
142 } while (ev.type != GRPC_QUEUE_SHUTDOWN &&
143 grpc_core::Timestamp::Now() < deadline);
144 GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN);
145 }
146 grpc_server_destroy(server);
147 grpc_completion_queue_destroy(cq);
148 }
149 grpc_shutdown();
150 return 0;
151 }