end2end/fuzzers/server_fuzzer.cc
Go to the documentation of this file.
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 <grpc/grpc.h>
20 
28 
29 bool squelch = true;
30 bool leak_check = true;
31 
32 static void discard_write(grpc_slice /*slice*/) {}
33 
34 static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
35 
36 static void dont_log(gpr_log_func_args* /*args*/) {}
37 
38 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
41  grpc_init();
42  {
46  grpc_resource_quota_create("context_list_test");
50  grpc_server* server = grpc_server_create(nullptr, nullptr);
53  // TODO(ctiller): add more registered methods (one for POST, one for PUT)
54  grpc_server_register_method(server, "/reg", nullptr, {}, 0);
56  const grpc_channel_args* channel_args =
59  .PreconditionChannelArgs(nullptr)
60  .ToC();
62  grpc_create_chttp2_transport(channel_args, mock_endpoint, false);
65  "SetupTransport", grpc_core::Server::FromC(server)->SetupTransport(
66  transport, nullptr, channel_args, nullptr)));
67  grpc_channel_args_destroy(channel_args);
68  grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
69 
70  grpc_call* call1 = nullptr;
71  grpc_call_details call_details1;
72  grpc_metadata_array request_metadata1;
73  grpc_call_details_init(&call_details1);
74  grpc_metadata_array_init(&request_metadata1);
75  int requested_calls = 0;
76 
78  grpc_server_request_call(server, &call1, &call_details1,
79  &request_metadata1, cq, cq, tag(1)));
80  requested_calls++;
81 
82  grpc_event ev;
83  while (true) {
86  nullptr);
87  switch (ev.type) {
88  case GRPC_QUEUE_TIMEOUT:
89  goto done;
91  break;
92  case GRPC_OP_COMPLETE:
93  if (ev.tag == tag(1)) {
94  requested_calls--;
95  // TODO(ctiller): keep reading that call!
96  }
97  break;
98  }
99  }
100 
101  done:
102  if (call1 != nullptr) grpc_call_unref(call1);
103  grpc_call_details_destroy(&call_details1);
104  grpc_metadata_array_destroy(&request_metadata1);
107  grpc_core::Timestamp deadline =
109  for (int i = 0; i <= requested_calls; i++) {
110  // A single grpc_completion_queue_next might not be sufficient for getting
111  // the tag from shutdown, because we might potentially get blocked by
112  // an operation happening on the timer thread.
113  // For example, the deadline timer might expire, leading to the timer
114  // thread trying to cancel the RPC and thereby acquiring a few references
115  // to the call. This will prevent the shutdown to complete till the timer
116  // thread releases those references.
117  // As a solution, we are going to keep performing a cq_next for a
118  // liberal period of 5 seconds for the timer thread to complete its work.
119  do {
121  nullptr);
123  } while (ev.type != GRPC_OP_COMPLETE &&
124  grpc_core::ExecCtx::Get()->Now() < deadline);
126  }
128  for (int i = 0; i <= requested_calls; i++) {
129  do {
131  nullptr);
133  } while (ev.type != GRPC_QUEUE_SHUTDOWN &&
134  grpc_core::ExecCtx::Get()->Now() < deadline);
136  }
139  }
140  grpc_shutdown();
141  return 0;
142 }
grpc_mock_endpoint_put_read
void grpc_mock_endpoint_put_read(grpc_endpoint *ep, grpc_slice slice)
Definition: mock_endpoint.cc:128
squelch
bool squelch
Definition: end2end/fuzzers/server_fuzzer.cc:29
mock_endpoint
Definition: mock_endpoint.cc:32
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
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition: end2end/fuzzers/server_fuzzer.cc:38
grpc_resource_quota
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:729
grpc_create_chttp2_transport
grpc_transport * grpc_create_chttp2_transport(const grpc_channel_args *channel_args, grpc_endpoint *ep, bool is_client)
Definition: chttp2_transport.cc:3122
grpc_metadata_array
Definition: grpc_types.h:579
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
tag
static void * tag(intptr_t t)
Definition: end2end/fuzzers/server_fuzzer.cc:34
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_resource_quota_create
GRPCAPI grpc_resource_quota * grpc_resource_quota_create(const char *trace_name)
Definition: api.cc:66
GRPC_LOG_IF_ERROR
#define GRPC_LOG_IF_ERROR(what, error)
Definition: error.h:398
grpc_core::Executor::SetThreadingAll
static void SetThreadingAll(bool enable)
Definition: executor.cc:446
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
grpc_channel_args
Definition: grpc_types.h:132
resource_quota
ResourceQuotaRefPtr resource_quota
Definition: filter_fuzzer.cc:145
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
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
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
transport
grpc_transport transport
Definition: filter_fuzzer.cc:146
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
time.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc_server
struct grpc_server grpc_server
Definition: grpc_types.h:65
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
grpc_server_destroy
GRPCAPI void grpc_server_destroy(grpc_server *server)
Definition: src/core/lib/surface/server.cc:1519
slice_internal.h
leak_check
bool leak_check
Definition: end2end/fuzzers/server_fuzzer.cc:30
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
grpc_slice_from_copied_buffer
GPRAPI grpc_slice grpc_slice_from_copied_buffer(const char *source, size_t len)
Definition: slice/slice.cc:170
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_resource_quota_unref
GRPCAPI void grpc_resource_quota_unref(grpc_resource_quota *resource_quota)
Definition: api.cc:79
mock_endpoint.h
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
chttp2_transport.h
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
grpc_mock_endpoint_create
grpc_endpoint * grpc_mock_endpoint_create(void(*on_write)(grpc_slice slice))
Definition: mock_endpoint.cc:118
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
discard_write
static void discard_write(grpc_slice)
Definition: end2end/fuzzers/server_fuzzer.cc:32
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
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
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_endpoint
Definition: endpoint.h:105
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_chttp2_transport_start_reading
void grpc_chttp2_transport_start_reading(grpc_transport *transport, grpc_slice_buffer *read_buffer, grpc_closure *notify_on_receive_settings, grpc_closure *notify_on_close)
Definition: chttp2_transport.cc:3128
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
dont_log
static void dont_log(gpr_log_func_args *)
Definition: end2end/fuzzers/server_fuzzer.cc:36
grpc_core::ExecCtx::InvalidateNow
void InvalidateNow()
Definition: exec_ctx.h:188
api.h


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