Debian Codegraph @

test/core/transport/binder/end2end/fuzzers/server_fuzzer.cc

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/grpc.h>
16
17 #include "src/core/ext/transport/binder/transport/binder_transport.h"
18 #include "src/core/lib/config/core_configuration.h"
19 #include "src/core/lib/iomgr/executor.h"
20 #include "src/core/lib/slice/slice_internal.h"
21 #include "src/core/lib/surface/server.h"
22 #include "src/libfuzzer/libfuzzer_macro.h"
23 #include "test/core/transport/binder/end2end/fuzzers/binder_transport_fuzzer.pb.h"
24 #include "test/core/transport/binder/end2end/fuzzers/fuzzer_utils.h"
25
26 bool squelch = true;
27 bool leak_check = true;
28
29 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
30
31 static void dont_log(gpr_log_func_args* /*args*/) {}
32
33 DEFINE_PROTO_FUZZER(const binder_transport_fuzzer::Input& input) {
34 if (squelch) gpr_set_log_function(dont_log);
35 grpc_init();
36 {
37 // Copied and modified from grpc/test/core/end2end/fuzzers/server_fuzzer.cc
38 grpc_core::ExecCtx exec_ctx;
39 grpc_core::Executor::SetThreadingAll(false);
40
41 grpc_server* server = grpc_server_create(nullptr, nullptr);
42 grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
43 grpc_server_register_completion_queue(server, cq, nullptr);
44 // TODO(ctiller): add more registered methods (one for POST, one for PUT)
45 grpc_server_register_method(server, "/reg", nullptr, {}, 0);
46 grpc_server_start(server);
47 grpc_transport* server_transport = grpc_create_binder_transport_server(
48 std::make_unique<grpc_binder::fuzzing::BinderForFuzzing>(
49 input.incoming_parcels()),
50 std::make_shared<
51 grpc::experimental::binder::UntrustedSecurityPolicy>());
52 grpc_core::ChannelArgs channel_args = grpc_core::CoreConfiguration::Get()
53 .channel_args_preconditioning()
54 .PreconditionChannelArgs(nullptr);
55 (void)grpc_core::Server::FromC(server)->SetupTransport(
56 server_transport, nullptr, channel_args, nullptr);
57 grpc_call* call1 = nullptr;
58 grpc_call_details call_details1;
59 grpc_metadata_array request_metadata1;
60 grpc_call_details_init(&call_details1);
61 grpc_metadata_array_init(&request_metadata1);
62 int requested_calls = 0;
63
64 GPR_ASSERT(GRPC_CALL_OK ==
65 grpc_server_request_call(server, &call1, &call_details1,
66 &request_metadata1, cq, cq, tag(1)));
67 requested_calls++;
68
69 grpc_event ev;
70 while (true) {
71 grpc_core::ExecCtx::Get()->Flush();
72 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
73 nullptr);
74 switch (ev.type) {
75 case GRPC_QUEUE_TIMEOUT:
76 goto done;
77 case GRPC_QUEUE_SHUTDOWN:
78 break;
79 case GRPC_OP_COMPLETE:
80 if (ev.tag == tag(1)) {
81 requested_calls--;
82 // TODO(ctiller): keep reading that call!
83 }
84 break;
85 }
86 }
87
88 done:
89 grpc_binder::fuzzing::JoinFuzzingThread();
90 if (call1 != nullptr) grpc_call_unref(call1);
91 grpc_call_details_destroy(&call_details1);
92 grpc_metadata_array_destroy(&request_metadata1);
93 grpc_server_shutdown_and_notify(server, cq, tag(0xdead));
94 grpc_server_cancel_all_calls(server);
95 grpc_core::Timestamp deadline =
96 grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(5);
97 for (int i = 0; i <= requested_calls; i++) {
98 // A single grpc_completion_queue_next might not be sufficient for getting
99 // the tag from shutdown, because we might potentially get blocked by
100 // an operation happening on the timer thread.
101 // For example, the deadline timer might expire, leading to the timer
102 // thread trying to cancel the RPC and thereby acquiring a few references
103 // to the call. This will prevent the shutdown to complete till the timer
104 // thread releases those references.
105 // As a solution, we are going to keep performing a cq_next for a
106 // liberal period of 5 seconds for the timer thread to complete its work.
107 do {
108 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
109 nullptr);
110 grpc_core::ExecCtx::Get()->InvalidateNow();
111 } while (ev.type != GRPC_OP_COMPLETE &&
112 grpc_core::Timestamp::Now() < deadline);
113 GPR_ASSERT(ev.type == GRPC_OP_COMPLETE);
114 }
115 grpc_completion_queue_shutdown(cq);
116 for (int i = 0; i <= requested_calls; i++) {
117 do {
118 ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
119 nullptr);
120 grpc_core::ExecCtx::Get()->InvalidateNow();
121 } while (ev.type != GRPC_QUEUE_SHUTDOWN &&
122 grpc_core::Timestamp::Now() < deadline);
123 GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN);
124 }
125 grpc_server_destroy(server);
126 grpc_completion_queue_destroy(cq);
127 }
128 grpc_shutdown();
129 }