server_fuzzer.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include <grpc/grpc.h>
  19. #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h"
  20. #include "src/core/lib/gprpp/time.h"
  21. #include "src/core/lib/iomgr/executor.h"
  22. #include "src/core/lib/resource_quota/api.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"
  26. bool squelch = true;
  27. bool leak_check = true;
  28. static void discard_write(grpc_slice /*slice*/) {}
  29. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  30. static void dont_log(gpr_log_func_args* /*args*/) {}
  31. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  32. grpc_test_only_set_slice_hash_seed(0);
  33. if (squelch) gpr_set_log_function(dont_log);
  34. grpc_init();
  35. {
  36. grpc_core::ExecCtx exec_ctx;
  37. grpc_core::Executor::SetThreadingAll(false);
  38. grpc_resource_quota* resource_quota =
  39. grpc_resource_quota_create("context_list_test");
  40. grpc_endpoint* mock_endpoint = grpc_mock_endpoint_create(discard_write);
  41. grpc_mock_endpoint_put_read(
  42. mock_endpoint, grpc_slice_from_copied_buffer((const char*)data, size));
  43. grpc_server* server = grpc_server_create(nullptr, nullptr);
  44. grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr);
  45. grpc_server_register_completion_queue(server, cq, nullptr);
  46. // TODO(ctiller): add more registered methods (one for POST, one for PUT)
  47. grpc_server_register_method(server, "/reg", nullptr, {}, 0);
  48. grpc_server_start(server);
  49. const grpc_channel_args* channel_args =
  50. grpc_core::CoreConfiguration::Get()
  51. .channel_args_preconditioning()
  52. .PreconditionChannelArgs(nullptr);
  53. grpc_transport* transport =
  54. grpc_create_chttp2_transport(channel_args, mock_endpoint, false);
  55. grpc_resource_quota_unref(resource_quota);
  56. GPR_ASSERT(GRPC_LOG_IF_ERROR(
  57. "SetupTransport", grpc_core::Server::FromC(server)->SetupTransport(
  58. transport, nullptr, channel_args, nullptr)));
  59. grpc_channel_args_destroy(channel_args);
  60. grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr);
  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. GPR_ASSERT(GRPC_CALL_OK ==
  68. grpc_server_request_call(server, &call1, &call_details1,
  69. &request_metadata1, cq, cq, tag(1)));
  70. requested_calls++;
  71. grpc_event ev;
  72. while (true) {
  73. grpc_core::ExecCtx::Get()->Flush();
  74. ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME),
  75. nullptr);
  76. switch (ev.type) {
  77. case GRPC_QUEUE_TIMEOUT:
  78. goto done;
  79. case GRPC_QUEUE_SHUTDOWN:
  80. break;
  81. case GRPC_OP_COMPLETE:
  82. if (ev.tag == tag(1)) {
  83. requested_calls--;
  84. // TODO(ctiller): keep reading that call!
  85. }
  86. break;
  87. }
  88. }
  89. done:
  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::ExecCtx::Get()->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::ExecCtx::Get()->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::ExecCtx::Get()->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. return 0;
  130. }