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