transport/binder/end2end/fuzzers/server_fuzzer.cc
Go to the documentation of this file.
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 
22 #include "src/libfuzzer/libfuzzer_macro.h"
23 #include "test/core/transport/binder/end2end/fuzzers/binder_transport_fuzzer.pb.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) {
36  grpc_init();
37  {
38  // Copied and modified from grpc/test/core/end2end/fuzzers/server_fuzzer.cc
41 
42  grpc_server* server = grpc_server_create(nullptr, nullptr);
45  // TODO(ctiller): add more registered methods (one for POST, one for PUT)
46  grpc_server_register_method(server, "/reg", nullptr, {}, 0);
49  absl::make_unique<grpc_binder::fuzzing::BinderForFuzzing>(
50  input.incoming_parcels()),
51  std::make_shared<
53  const grpc_channel_args* channel_args =
56  .PreconditionChannelArgs(nullptr)
57  .ToC();
59  server_transport, nullptr, channel_args, nullptr);
60  grpc_channel_args_destroy(channel_args);
61  grpc_call* call1 = nullptr;
62  grpc_call_details call_details1;
63  grpc_metadata_array request_metadata1;
64  grpc_call_details_init(&call_details1);
65  grpc_metadata_array_init(&request_metadata1);
66  int requested_calls = 0;
67 
69  grpc_server_request_call(server, &call1, &call_details1,
70  &request_metadata1, cq, cq, tag(1)));
71  requested_calls++;
72 
73  grpc_event ev;
74  while (true) {
77  nullptr);
78  switch (ev.type) {
79  case GRPC_QUEUE_TIMEOUT:
80  goto done;
82  break;
83  case GRPC_OP_COMPLETE:
84  if (ev.tag == tag(1)) {
85  requested_calls--;
86  // TODO(ctiller): keep reading that call!
87  }
88  break;
89  }
90  }
91 
92  done:
94  if (call1 != nullptr) grpc_call_unref(call1);
95  grpc_call_details_destroy(&call_details1);
96  grpc_metadata_array_destroy(&request_metadata1);
99  grpc_core::Timestamp deadline =
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.
111  do {
113  nullptr);
115  } while (ev.type != GRPC_OP_COMPLETE &&
116  grpc_core::ExecCtx::Get()->Now() < deadline);
118  }
120  for (int i = 0; i <= requested_calls; i++) {
121  do {
123  nullptr);
125  } while (ev.type != GRPC_QUEUE_SHUTDOWN &&
126  grpc_core::ExecCtx::Get()->Now() < deadline);
128  }
131  }
132  grpc_shutdown();
133 }
grpc_core::Server::SetupTransport
grpc_error_handle SetupTransport(grpc_transport *transport, grpc_pollset *accepting_pollset, const grpc_channel_args *args, const RefCountedPtr< channelz::SocketNode > &socket_node)
Definition: src/core/lib/surface/server.cc:605
leak_check
bool leak_check
Definition: transport/binder/end2end/fuzzers/server_fuzzer.cc:27
grpc_call_details_destroy
GRPCAPI void grpc_call_details_destroy(grpc_call_details *details)
Definition: call_details.cc:36
grpc_call_details_init
GRPCAPI void grpc_call_details_init(grpc_call_details *details)
Definition: call_details.cc:30
core_configuration.h
grpc_binder::fuzzing::JoinFuzzingThread
void JoinFuzzingThread()
Definition: fuzzer_utils.cc:32
grpc_metadata_array
Definition: grpc_types.h:579
grpc::experimental::binder::UntrustedSecurityPolicy
Definition: binder_security_policy.h:44
grpc_call_details
Definition: grpc_types.h:585
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
GRPC_QUEUE_SHUTDOWN
@ GRPC_QUEUE_SHUTDOWN
Definition: grpc_types.h:554
GRPC_OP_COMPLETE
@ GRPC_OP_COMPLETE
Definition: grpc_types.h:558
grpc_server_create
GRPCAPI grpc_server * grpc_server_create(const grpc_channel_args *args, void *reserved)
Definition: src/core/lib/surface/server.cc:1456
GRPC_CALL_OK
@ GRPC_CALL_OK
Definition: grpc_types.h:466
grpc_server_register_completion_queue
GRPCAPI void grpc_server_register_completion_queue(grpc_server *server, grpc_completion_queue *cq, void *reserved)
Definition: src/core/lib/surface/server.cc:1466
grpc_core::Executor::SetThreadingAll
static void SetThreadingAll(bool enable)
Definition: executor.cc:446
tag
static void * tag(intptr_t t)
Definition: transport/binder/end2end/fuzzers/server_fuzzer.cc:29
grpc_channel_args
Definition: grpc_types.h:132
gpr_log_func_args
Definition: include/grpc/impl/codegen/log.h:77
grpc_metadata_array_destroy
GRPCAPI void grpc_metadata_array_destroy(grpc_metadata_array *array)
Definition: metadata_array.cc:35
grpc_server_request_call
GRPCAPI grpc_call_error grpc_server_request_call(grpc_server *server, grpc_call **call, grpc_call_details *details, grpc_metadata_array *request_metadata, grpc_completion_queue *cq_bound_to_call, grpc_completion_queue *cq_for_notification, void *tag_new)
Definition: src/core/lib/surface/server.cc:1526
squelch
bool squelch
Definition: transport/binder/end2end/fuzzers/server_fuzzer.cc:26
DEFINE_PROTO_FUZZER
DEFINE_PROTO_FUZZER(const binder_transport_fuzzer::Input &input)
Definition: transport/binder/end2end/fuzzers/server_fuzzer.cc:33
grpc_test_only_set_slice_hash_seed
void grpc_test_only_set_slice_hash_seed(uint32_t seed)
Definition: slice_refcount.cc:33
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::CoreConfiguration::Get
static const CoreConfiguration & Get()
Definition: core_configuration.h:82
dont_log
static void dont_log(gpr_log_func_args *)
Definition: transport/binder/end2end/fuzzers/server_fuzzer.cc:31
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
grpc_call_unref
GRPCAPI void grpc_call_unref(grpc_call *call)
Definition: call.cc:1770
grpc_event
Definition: grpc_types.h:564
grpc_completion_queue
Definition: completion_queue.cc:347
grpc.h
grpc_call
struct grpc_call grpc_call
Definition: grpc_types.h:70
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
grpc_core::ChannelArgs::ToC
const grpc_channel_args * ToC() const
Definition: channel_args.cc:94
grpc_channel_args_destroy
void grpc_channel_args_destroy(grpc_channel_args *a)
Definition: channel_args.cc:360
grpc_server
struct grpc_server grpc_server
Definition: grpc_types.h:65
fuzzer_utils.h
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc_server_destroy
GRPCAPI void grpc_server_destroy(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1519
slice_internal.h
grpc_create_binder_transport_server
grpc_transport * grpc_create_binder_transport_server(std::unique_ptr< grpc_binder::Binder > client_binder, std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy)
Definition: binder_transport.cc:752
grpc_core::CoreConfiguration::channel_args_preconditioning
const ChannelArgsPreconditioning & channel_args_preconditioning() const
Definition: core_configuration.h:139
grpc_core::ExecCtx
Definition: exec_ctx.h:97
executor.h
grpc_server_cancel_all_calls
GRPCAPI void grpc_server_cancel_all_calls(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1512
gpr_inf_past
GPRAPI gpr_timespec gpr_inf_past(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:63
server
Definition: examples/python/async_streaming/server.py:1
grpc_completion_queue_destroy
GRPCAPI void grpc_completion_queue_destroy(grpc_completion_queue *cq)
Definition: completion_queue.cc:1424
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
grpc_server_shutdown_and_notify
GRPCAPI void grpc_server_shutdown_and_notify(grpc_server *server, grpc_completion_queue *cq, void *tag)
Definition: src/core/lib/surface/server.cc:1503
grpc_completion_queue_next
GRPCAPI grpc_event grpc_completion_queue_next(grpc_completion_queue *cq, gpr_timespec deadline, void *reserved)
Definition: completion_queue.cc:1133
grpc_completion_queue_shutdown
GRPCAPI void grpc_completion_queue_shutdown(grpc_completion_queue *cq)
Definition: completion_queue.cc:1416
server.h
grpc_transport
Definition: transport_impl.h:89
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
grpc_completion_queue_create_for_next
GRPCAPI grpc_completion_queue * grpc_completion_queue_create_for_next(void *reserved)
Definition: completion_queue_factory.cc:62
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_event::type
grpc_completion_type type
Definition: grpc_types.h:566
gpr_set_log_function
GPRAPI void gpr_set_log_function(gpr_log_func func)
Definition: log.cc:143
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_server_start
GRPCAPI void grpc_server_start(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1497
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
grpc_core::CppImplOf< Server, grpc_server >::FromC
static Server * FromC(grpc_server *c_type)
Definition: cpp_impl_of.h:30
GRPC_QUEUE_TIMEOUT
@ GRPC_QUEUE_TIMEOUT
Definition: grpc_types.h:556
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_core::ChannelArgsPreconditioning::PreconditionChannelArgs
ChannelArgs PreconditionChannelArgs(const grpc_channel_args *args) const
Definition: channel_args_preconditioning.cc:34
grpc_event::tag
void * tag
Definition: grpc_types.h:576
cq
static grpc_completion_queue * cq
Definition: test/core/fling/client.cc:37
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
grpc_metadata_array_init
GRPCAPI void grpc_metadata_array_init(grpc_metadata_array *array)
Definition: metadata_array.cc:30
grpc_server_register_method
GRPCAPI void * grpc_server_register_method(grpc_server *server, const char *method, const char *host, grpc_server_register_method_payload_handling payload_handling, uint32_t flags)
Definition: src/core/lib/surface/server.cc:1485
grpc_core::ExecCtx::InvalidateNow
void InvalidateNow()
Definition: exec_ctx.h:188
binder_transport.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:11