| 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 <thread> |
| 16 |
#include <utility> |
| 17 |
|
| 18 |
#include "absl/memory/memory.h" |
| 19 |
|
| 20 |
#include <grpc/grpc.h> |
| 21 |
#include <grpcpp/security/binder_security_policy.h> |
| 22 |
|
| 23 |
#include "src/core/ext/transport/binder/transport/binder_transport.h" |
| 24 |
#include "src/core/lib/config/core_configuration.h" |
| 25 |
#include "src/core/lib/iomgr/executor.h" |
| 26 |
#include "src/core/lib/surface/channel.h" |
| 27 |
#include "src/libfuzzer/libfuzzer_macro.h" |
| 28 |
#include "test/core/transport/binder/end2end/fuzzers/binder_transport_fuzzer.pb.h" |
| 29 |
#include "test/core/transport/binder/end2end/fuzzers/fuzzer_utils.h" |
| 30 |
|
| 31 |
bool squelch = true; |
| 32 |
bool leak_check = true; |
| 33 |
|
| 34 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 35 |
|
| 36 |
static void dont_log(gpr_log_func_args*) {} |
| 37 |
|
| 38 |
DEFINE_PROTO_FUZZER(const binder_transport_fuzzer::Input& input) { |
| 39 |
if (squelch) gpr_set_log_function(dont_log); |
| 40 |
grpc_init(); |
| 41 |
{ |
| 42 |
// Copied and modified from grpc/test/core/end2end/fuzzers/client_fuzzer.cc |
| 43 |
grpc_core::ExecCtx exec_ctx; |
| 44 |
grpc_core::Executor::SetThreadingAll(false); |
| 45 |
|
| 46 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 47 |
grpc_transport* client_transport = grpc_create_binder_transport_client( |
| 48 |
std::make_unique<grpc_binder::fuzzing::BinderForFuzzing>( |
| 49 |
input.incoming_parcels()), |
| 50 |
std::make_shared< |
| 51 |
grpc::experimental::binder::UntrustedSecurityPolicy>()); |
| 52 |
grpc_arg authority_arg = grpc_channel_arg_string_create( |
| 53 |
const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY), |
| 54 |
const_cast<char*>("test-authority")); |
| 55 |
grpc_channel_args* args = |
| 56 |
grpc_channel_args_copy_and_add(nullptr, &authority_arg, 1); |
| 57 |
auto channel_args = grpc_core::CoreConfiguration::Get() |
| 58 |
.channel_args_preconditioning() |
| 59 |
.PreconditionChannelArgs(args); |
| 60 |
auto channel = |
| 61 |
grpc_core::Channel::Create("test-target", channel_args, |
| 62 |
GRPC_CLIENT_DIRECT_CHANNEL, client_transport) |
| 63 |
->release() |
| 64 |
->c_ptr(); |
| 65 |
grpc_channel_args_destroy(args); |
| 66 |
grpc_slice host = grpc_slice_from_static_string("localhost"); |
| 67 |
grpc_call* call = grpc_channel_create_call( |
| 68 |
channel, nullptr, 0, cq, grpc_slice_from_static_string("/foo"), &host, |
| 69 |
gpr_inf_future(GPR_CLOCK_REALTIME), nullptr); |
| 70 |
grpc_metadata_array initial_metadata_recv; |
| 71 |
grpc_metadata_array_init(&initial_metadata_recv); |
| 72 |
grpc_byte_buffer* response_payload_recv = nullptr; |
| 73 |
grpc_metadata_array trailing_metadata_recv; |
| 74 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 75 |
grpc_status_code status; |
| 76 |
grpc_slice details = grpc_empty_slice(); |
| 77 |
|
| 78 |
grpc_op ops[6]; |
| 79 |
memset(ops, 0, sizeof(ops)); |
| 80 |
grpc_op* op = ops; |
| 81 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 82 |
op->data.send_initial_metadata.count = 0; |
| 83 |
op->flags = 0; |
| 84 |
op->reserved = nullptr; |
| 85 |
op++; |
| 86 |
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 87 |
op->flags = 0; |
| 88 |
op->reserved = nullptr; |
| 89 |
op++; |
| 90 |
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
| 91 |
op->data.recv_initial_metadata.recv_initial_metadata = |
| 92 |
&initial_metadata_recv; |
| 93 |
op->flags = 0; |
| 94 |
op->reserved = nullptr; |
| 95 |
op++; |
| 96 |
op->op = GRPC_OP_RECV_MESSAGE; |
| 97 |
op->data.recv_message.recv_message = &response_payload_recv; |
| 98 |
op->flags = 0; |
| 99 |
op->reserved = nullptr; |
| 100 |
op++; |
| 101 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 102 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 103 |
op->data.recv_status_on_client.status = &status; |
| 104 |
op->data.recv_status_on_client.status_details = &details; |
| 105 |
op->flags = 0; |
| 106 |
op->reserved = nullptr; |
| 107 |
op++; |
| 108 |
grpc_call_error error = grpc_call_start_batch( |
| 109 |
call, ops, static_cast<size_t>(op - ops), tag(1), nullptr); |
| 110 |
int requested_calls = 1; |
| 111 |
GPR_ASSERT(GRPC_CALL_OK == error); |
| 112 |
grpc_event ev; |
| 113 |
while (true) { |
| 114 |
grpc_core::ExecCtx::Get()->Flush(); |
| 115 |
ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), |
| 116 |
nullptr); |
| 117 |
switch (ev.type) { |
| 118 |
case GRPC_QUEUE_TIMEOUT: |
| 119 |
goto done; |
| 120 |
case GRPC_QUEUE_SHUTDOWN: |
| 121 |
break; |
| 122 |
case GRPC_OP_COMPLETE: |
| 123 |
requested_calls--; |
| 124 |
break; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
done: |
| 129 |
if (requested_calls) { |
| 130 |
grpc_call_cancel(call, nullptr); |
| 131 |
} |
| 132 |
grpc_binder::fuzzing::JoinFuzzingThread(); |
| 133 |
for (int i = 0; i < requested_calls; i++) { |
| 134 |
ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), |
| 135 |
nullptr); |
| 136 |
GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); |
| 137 |
} |
| 138 |
grpc_completion_queue_shutdown(cq); |
| 139 |
for (int i = 0; i < requested_calls; i++) { |
| 140 |
ev = grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), |
| 141 |
nullptr); |
| 142 |
GPR_ASSERT(ev.type == GRPC_QUEUE_SHUTDOWN); |
| 143 |
} |
| 144 |
grpc_call_unref(call); |
| 145 |
grpc_completion_queue_destroy(cq); |
| 146 |
grpc_metadata_array_destroy(&initial_metadata_recv); |
| 147 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 148 |
grpc_slice_unref(details); |
| 149 |
grpc_channel_destroy(channel); |
| 150 |
if (response_payload_recv != nullptr) { |
| 151 |
grpc_byte_buffer_destroy(response_payload_recv); |
| 152 |
} |
| 153 |
} |
| 154 |
grpc_shutdown(); |
| 155 |
} |