| 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 <stdint.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include "absl/status/statusor.h" |
| 23 |
|
| 24 |
#include <grpc/byte_buffer.h> |
| 25 |
#include <grpc/grpc.h> |
| 26 |
#include <grpc/slice.h> |
| 27 |
#include <grpc/status.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/time.h> |
| 30 |
|
| 31 |
#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h" |
| 32 |
#include "src/core/lib/channel/channel_args.h" |
| 33 |
#include "src/core/lib/channel/channel_args_preconditioning.h" |
| 34 |
#include "src/core/lib/config/core_configuration.h" |
| 35 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 36 |
#include "src/core/lib/iomgr/endpoint.h" |
| 37 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 38 |
#include "src/core/lib/iomgr/executor.h" |
| 39 |
#include "src/core/lib/resource_quota/api.h" |
| 40 |
#include "src/core/lib/surface/channel.h" |
| 41 |
#include "src/core/lib/surface/channel_stack_type.h" |
| 42 |
#include "src/core/lib/transport/transport_fwd.h" |
| 43 |
#include "test/core/util/mock_endpoint.h" |
| 44 |
|
| 45 |
bool squelch = true; |
| 46 |
bool leak_check = true; |
| 47 |
|
| 48 |
static void discard_write(grpc_slice /*slice*/) {} |
| 49 |
|
| 50 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 51 |
|
| 52 |
static void dont_log(gpr_log_func_args* /*args*/) {} |
| 53 |
|
| 54 |
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 55 |
if (squelch) gpr_set_log_function(dont_log); |
| 56 |
grpc_init(); |
| 57 |
{ |
| 58 |
grpc_core::ExecCtx exec_ctx; |
| 59 |
grpc_core::Executor::SetThreadingAll(false); |
| 60 |
|
| 61 |
grpc_resource_quota* resource_quota = |
| 62 |
grpc_resource_quota_create("context_list_test"); |
| 63 |
grpc_endpoint* mock_endpoint = grpc_mock_endpoint_create(discard_write); |
| 64 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 65 |
grpc_core::ChannelArgs args = grpc_core::CoreConfiguration::Get() |
| 66 |
.channel_args_preconditioning() |
| 67 |
.PreconditionChannelArgs(nullptr); |
| 68 |
grpc_transport* transport = |
| 69 |
grpc_create_chttp2_transport(args, mock_endpoint, true); |
| 70 |
grpc_resource_quota_unref(resource_quota); |
| 71 |
grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr); |
| 72 |
auto channel_args = |
| 73 |
grpc_core::CoreConfiguration::Get() |
| 74 |
.channel_args_preconditioning() |
| 75 |
.PreconditionChannelArgs(nullptr) |
| 76 |
.SetIfUnset(GRPC_ARG_DEFAULT_AUTHORITY, "test-authority"); |
| 77 |
auto channel = grpc_core::Channel::Create( |
| 78 |
"test-target", channel_args, GRPC_CLIENT_DIRECT_CHANNEL, transport); |
| 79 |
grpc_slice host = grpc_slice_from_static_string("localhost"); |
| 80 |
grpc_call* call = |
| 81 |
grpc_channel_create_call(channel->get()->c_ptr(), nullptr, 0, cq, |
| 82 |
grpc_slice_from_static_string("/foo"), &host, |
| 83 |
gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 84 |
|
| 85 |
grpc_metadata_array initial_metadata_recv; |
| 86 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 87 |
grpc_byte_buffer* response_payload_recv = nullptr; |
| 88 |
grpc_metadata_array trailing_metadata_recv; |
| 89 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 90 |
grpc_status_code status; |
| 91 |
grpc_slice details = grpc_empty_slice(); |
| 92 |
|
| 93 |
grpc_op ops[6]; |
| 94 |
memset(ops, 0, sizeof(ops)); |
| 95 |
grpc_op* op = ops; |
| 96 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 97 |
op->data.send_initial_metadata.count = 0; |
| 98 |
op->flags = 0; |
| 99 |
op->reserved = nullptr; |
| 100 |
op++; |
| 101 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 102 |
op->flags = 0; |
| 103 |
op->reserved = nullptr; |
| 104 |
op++; |
| 105 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 106 |
op->data.recv_initial_metadata.recv_initial_metadata = |
| 107 |
&initial_metadata_recv; |
| 108 |
op->flags = 0; |
| 109 |
op->reserved = nullptr; |
| 110 |
op++; |
| 111 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 112 |
op->data.recv_message.recv_message = &response_payload_recv; |
| 113 |
op->flags = 0; |
| 114 |
op->reserved = nullptr; |
| 115 |
op++; |
| 116 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 117 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 118 |
op->data.recv_status_on_client.status = &status; |
| 119 |
op->data.recv_status_on_client.status_details = &details; |
| 120 |
op->flags = 0; |
| 121 |
op->reserved = nullptr; |
| 122 |
op++; |
| 123 |
grpc_call_error error = |
| 124 |
grpc_call_start_batch(call, ops, (size_t)(op - ops), tag(1), nullptr); |
| 125 |
int requested_calls = 1; |
| 126 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 127 |
|
| 128 |
grpc_mock_endpoint_put_read( |
| 129 |
mock_endpoint, grpc_slice_from_copied_buffer((const char*)data, size)); |
| 130 |
|
| 131 |
grpc_event ev; |
| 132 |
while (true) { |
| 133 |
grpc_core::ExecCtx::Get()->Flush(); |
| 134 |
ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), |
| 135 |
nullptr); |
| 136 |
switch (ev.type) { |
| 137 |
case GRPC_QUEUE_TIMEOUT: |
| 138 |
goto done; |
| 139 |
case GRPC_QUEUE_SHUTDOWN: |
| 140 |
break; |
| 141 |
case GRPC_OP_COMPLETE: |
| 142 |
requested_calls--; |
| 143 |
break; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
done: |
| 148 |
if (requested_calls) { |
| 149 |
grpc_call_cancel(call, nullptr); |
| 150 |
} |
| 151 |
for (int i = 0; i < requested_calls; i++) { |
| 152 |
ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), |
| 153 |
nullptr); |
| 154 |
GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); |
| 155 |
} |
| 156 |
grpc_completion_queue_shutdown(cq); |
| 157 |
for (int i = 0; i < requested_calls; i++) { |
| 158 |
ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), |
| 159 |
nullptr); |
| 160 |
GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN); |
| 161 |
} |
| 162 |
grpc_call_unref(call); |
| 163 |
grpc_completion_queue_destroy(cq); |
| 164 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 165 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 166 |
grpc_slice_unref(details); |
| 167 |
if (response_payload_recv != nullptr) { |
| 168 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 169 |
} |
| 170 |
} |
| 171 |
grpc_shutdown(); |
| 172 |
return 0; |
| 173 |
} |