Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include <grpc/grpc.h> |
| 37 | #include <grpc/support/alloc.h> |
| 38 | #include <grpc/support/log.h> |
| 39 | #include <grpc/support/string_util.h> |
| 40 | |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 41 | #include "src/core/ext/transport/chttp2/transport/chttp2_transport.h" |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 42 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 43 | #include "src/core/lib/iomgr/resolve_address.h" |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 44 | #include "src/core/lib/iomgr/tcp_client.h" |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 45 | #include "src/core/lib/iomgr/timer.h" |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 46 | #include "src/core/lib/surface/server.h" |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 47 | #include "src/core/lib/transport/metadata.h" |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 48 | #include "test/core/util/passthru_endpoint.h" |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 49 | |
| 50 | //////////////////////////////////////////////////////////////////////////////// |
| 51 | // logging |
| 52 | |
Craig Tiller | 42fe4cd | 2016-04-23 12:50:46 -0700 | [diff] [blame] | 53 | static const bool squelch = true; |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 54 | |
| 55 | static void dont_log(gpr_log_func_args *args) {} |
| 56 | |
| 57 | //////////////////////////////////////////////////////////////////////////////// |
| 58 | // input_stream: allows easy access to input bytes, and allows reading a little |
| 59 | // past the end (avoiding needing to check everywhere) |
| 60 | |
| 61 | typedef struct { |
| 62 | const uint8_t *cur; |
| 63 | const uint8_t *end; |
| 64 | } input_stream; |
| 65 | |
| 66 | static uint8_t next_byte(input_stream *inp) { |
| 67 | if (inp->cur == inp->end) { |
| 68 | return 0; |
| 69 | } |
| 70 | return *inp->cur++; |
| 71 | } |
| 72 | |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 73 | static void end(input_stream *inp) { inp->cur = inp->end; } |
| 74 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 75 | static char *read_string(input_stream *inp) { |
Craig Tiller | 7673f7a | 2016-04-18 22:44:57 -0700 | [diff] [blame] | 76 | char *str = NULL; |
| 77 | size_t cap = 0; |
| 78 | size_t sz = 0; |
| 79 | char c; |
| 80 | do { |
| 81 | if (cap == sz) { |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 82 | cap = GPR_MAX(3 * cap / 2, cap + 8); |
Craig Tiller | 7673f7a | 2016-04-18 22:44:57 -0700 | [diff] [blame] | 83 | str = gpr_realloc(str, cap); |
| 84 | } |
| 85 | c = (char)next_byte(inp); |
| 86 | str[sz++] = c; |
| 87 | } while (c != 0); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 88 | return str; |
| 89 | } |
| 90 | |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 91 | static void read_buffer(input_stream *inp, char **buffer, size_t *length) { |
| 92 | *length = next_byte(inp); |
| 93 | *buffer = gpr_malloc(*length); |
| 94 | for (size_t i = 0; i < *length; i++) { |
| 95 | (*buffer)[i] = (char)next_byte(inp); |
| 96 | } |
| 97 | } |
| 98 | |
Craig Tiller | f582305 | 2016-04-15 15:22:09 -0700 | [diff] [blame] | 99 | static uint32_t read_uint22(input_stream *inp) { |
| 100 | uint8_t b = next_byte(inp); |
| 101 | uint32_t x = b & 0x7f; |
| 102 | if (b & 0x80) { |
| 103 | x <<= 7; |
| 104 | b = next_byte(inp); |
| 105 | x |= b & 0x7f; |
| 106 | if (b & 0x80) { |
| 107 | x <<= 8; |
| 108 | x |= next_byte(inp); |
| 109 | } |
| 110 | } |
| 111 | return x; |
| 112 | } |
| 113 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 114 | static uint32_t read_uint32(input_stream *inp) { |
| 115 | uint8_t b = next_byte(inp); |
| 116 | uint32_t x = b & 0x7f; |
| 117 | if (b & 0x80) { |
| 118 | x <<= 7; |
| 119 | b = next_byte(inp); |
| 120 | x |= b & 0x7f; |
| 121 | if (b & 0x80) { |
| 122 | x <<= 7; |
| 123 | b = next_byte(inp); |
| 124 | x |= b & 0x7f; |
| 125 | if (b & 0x80) { |
| 126 | x <<= 7; |
| 127 | b = next_byte(inp); |
| 128 | x |= b & 0x7f; |
| 129 | if (b & 0x80) { |
| 130 | x = (x << 4) | (next_byte(inp) & 0x0f); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return x; |
| 136 | } |
| 137 | |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 138 | static grpc_byte_buffer *read_message(input_stream *inp) { |
Craig Tiller | f582305 | 2016-04-15 15:22:09 -0700 | [diff] [blame] | 139 | gpr_slice slice = gpr_slice_malloc(read_uint22(inp)); |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 140 | memset(GPR_SLICE_START_PTR(slice), 0, GPR_SLICE_LENGTH(slice)); |
Craig Tiller | d78ca88 | 2016-04-22 14:47:05 -0700 | [diff] [blame] | 141 | grpc_byte_buffer *out = grpc_raw_byte_buffer_create(&slice, 1); |
| 142 | gpr_slice_unref(slice); |
| 143 | return out; |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 146 | static void read_metadata(input_stream *inp, size_t *count, |
| 147 | grpc_metadata **metadata) { |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 148 | *count = next_byte(inp); |
| 149 | *metadata = gpr_malloc(*count * sizeof(**metadata)); |
| 150 | memset(*metadata, 0, *count * sizeof(**metadata)); |
| 151 | for (size_t i = 0; i < *count; i++) { |
| 152 | (*metadata)[i].key = read_string(inp); |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 153 | read_buffer(inp, (char **)&(*metadata)[i].value, |
| 154 | &(*metadata)[i].value_length); |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 155 | (*metadata)[i].flags = read_uint32(inp); |
| 156 | } |
| 157 | } |
| 158 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 159 | static int read_int(input_stream *inp) { return (int)read_uint32(inp); } |
| 160 | |
| 161 | static grpc_channel_args *read_args(input_stream *inp) { |
| 162 | size_t n = next_byte(inp); |
| 163 | grpc_arg *args = gpr_malloc(sizeof(*args) * n); |
| 164 | for (size_t i = 0; i < n; i++) { |
| 165 | bool is_string = next_byte(inp) & 1; |
| 166 | args[i].type = is_string ? GRPC_ARG_STRING : GRPC_ARG_INTEGER; |
| 167 | args[i].key = read_string(inp); |
| 168 | if (is_string) { |
| 169 | args[i].value.string = read_string(inp); |
| 170 | } else { |
| 171 | args[i].value.integer = read_int(inp); |
| 172 | } |
| 173 | } |
| 174 | grpc_channel_args *a = gpr_malloc(sizeof(*a)); |
| 175 | a->args = args; |
| 176 | a->num_args = n; |
| 177 | return a; |
| 178 | } |
| 179 | |
| 180 | static bool is_eof(input_stream *inp) { return inp->cur == inp->end; } |
| 181 | |
| 182 | //////////////////////////////////////////////////////////////////////////////// |
| 183 | // global state |
| 184 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 185 | static gpr_timespec g_now; |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 186 | static grpc_server *g_server; |
| 187 | static grpc_channel *g_channel; |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 188 | |
| 189 | extern gpr_timespec (*gpr_now_impl)(gpr_clock_type clock_type); |
| 190 | |
| 191 | static gpr_timespec now_impl(gpr_clock_type clock_type) { |
| 192 | GPR_ASSERT(clock_type != GPR_TIMESPAN); |
Craig Tiller | d0b2523 | 2016-04-13 19:54:34 -0700 | [diff] [blame] | 193 | return g_now; |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | //////////////////////////////////////////////////////////////////////////////// |
| 197 | // dns resolution |
| 198 | |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 199 | typedef struct addr_req { |
| 200 | grpc_timer timer; |
| 201 | char *addr; |
| 202 | grpc_resolve_cb cb; |
| 203 | void *arg; |
| 204 | } addr_req; |
| 205 | |
| 206 | static void finish_resolve(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 207 | addr_req *r = arg; |
| 208 | |
Craig Tiller | 849155d | 2016-04-13 20:21:42 -0700 | [diff] [blame] | 209 | if (success && 0 == strcmp(r->addr, "server")) { |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 210 | grpc_resolved_addresses *addrs = gpr_malloc(sizeof(*addrs)); |
| 211 | addrs->naddrs = 1; |
| 212 | addrs->addrs = gpr_malloc(sizeof(*addrs->addrs)); |
| 213 | addrs->addrs[0].len = 0; |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 214 | r->cb(exec_ctx, r->arg, addrs); |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 215 | } else { |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 216 | r->cb(exec_ctx, r->arg, NULL); |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 217 | } |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 218 | |
| 219 | gpr_free(r->addr); |
| 220 | gpr_free(r); |
| 221 | } |
| 222 | |
| 223 | void my_resolve_address(grpc_exec_ctx *exec_ctx, const char *addr, |
| 224 | const char *default_port, grpc_resolve_cb cb, |
| 225 | void *arg) { |
| 226 | addr_req *r = gpr_malloc(sizeof(*r)); |
| 227 | r->addr = gpr_strdup(addr); |
| 228 | r->cb = cb; |
| 229 | r->arg = arg; |
| 230 | grpc_timer_init(exec_ctx, &r->timer, |
| 231 | gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
| 232 | gpr_time_from_seconds(1, GPR_TIMESPAN)), |
| 233 | finish_resolve, r, gpr_now(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 236 | //////////////////////////////////////////////////////////////////////////////// |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 237 | // client connection |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 238 | |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 239 | // defined in tcp_client_posix.c |
| 240 | extern void (*grpc_tcp_client_connect_impl)( |
| 241 | grpc_exec_ctx *exec_ctx, grpc_closure *closure, grpc_endpoint **ep, |
| 242 | grpc_pollset_set *interested_parties, const struct sockaddr *addr, |
| 243 | size_t addr_len, gpr_timespec deadline); |
| 244 | |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 245 | static void sched_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 246 | grpc_endpoint **ep, gpr_timespec deadline); |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 247 | |
| 248 | typedef struct { |
| 249 | grpc_timer timer; |
| 250 | grpc_closure *closure; |
| 251 | grpc_endpoint **ep; |
| 252 | gpr_timespec deadline; |
| 253 | } future_connect; |
| 254 | |
| 255 | static void do_connect(grpc_exec_ctx *exec_ctx, void *arg, bool success) { |
| 256 | future_connect *fc = arg; |
Craig Tiller | f4cc2f8 | 2016-04-13 20:47:37 -0700 | [diff] [blame] | 257 | if (!success) { |
| 258 | *fc->ep = NULL; |
| 259 | grpc_exec_ctx_enqueue(exec_ctx, fc->closure, false, NULL); |
| 260 | } else if (g_server != NULL) { |
Craig Tiller | 62c7a5a | 2016-04-13 22:25:03 -0700 | [diff] [blame] | 261 | grpc_endpoint *client; |
| 262 | grpc_endpoint *server; |
| 263 | grpc_passthru_endpoint_create(&client, &server); |
| 264 | *fc->ep = client; |
| 265 | |
| 266 | grpc_transport *transport = |
| 267 | grpc_create_chttp2_transport(exec_ctx, NULL, server, 0); |
| 268 | grpc_server_setup_transport(exec_ctx, g_server, transport, NULL); |
| 269 | grpc_chttp2_transport_start_reading(exec_ctx, transport, NULL, 0); |
| 270 | |
| 271 | grpc_exec_ctx_enqueue(exec_ctx, fc->closure, false, NULL); |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 272 | } else { |
| 273 | sched_connect(exec_ctx, fc->closure, fc->ep, fc->deadline); |
| 274 | } |
| 275 | gpr_free(fc); |
| 276 | } |
| 277 | |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 278 | static void sched_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 279 | grpc_endpoint **ep, gpr_timespec deadline) { |
Craig Tiller | f4cc2f8 | 2016-04-13 20:47:37 -0700 | [diff] [blame] | 280 | if (gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) <= 0) { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 281 | *ep = NULL; |
| 282 | grpc_exec_ctx_enqueue(exec_ctx, closure, false, NULL); |
| 283 | return; |
| 284 | } |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 285 | |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 286 | future_connect *fc = gpr_malloc(sizeof(*fc)); |
| 287 | fc->closure = closure; |
| 288 | fc->ep = ep; |
| 289 | fc->deadline = deadline; |
| 290 | grpc_timer_init(exec_ctx, &fc->timer, |
| 291 | gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
| 292 | gpr_time_from_millis(1, GPR_TIMESPAN)), |
| 293 | do_connect, fc, gpr_now(GPR_CLOCK_MONOTONIC)); |
| 294 | } |
| 295 | |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 296 | static void my_tcp_client_connect(grpc_exec_ctx *exec_ctx, |
| 297 | grpc_closure *closure, grpc_endpoint **ep, |
| 298 | grpc_pollset_set *interested_parties, |
| 299 | const struct sockaddr *addr, size_t addr_len, |
| 300 | gpr_timespec deadline) { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 301 | sched_connect(exec_ctx, closure, ep, deadline); |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 302 | } |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 303 | |
| 304 | //////////////////////////////////////////////////////////////////////////////// |
| 305 | // test driver |
| 306 | |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 307 | typedef struct validator { |
| 308 | void (*validate)(void *arg, bool success); |
| 309 | void *arg; |
| 310 | } validator; |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 311 | |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 312 | static validator *create_validator(void (*validate)(void *arg, bool success), |
| 313 | void *arg) { |
| 314 | validator *v = gpr_malloc(sizeof(*v)); |
| 315 | v->validate = validate; |
| 316 | v->arg = arg; |
| 317 | return v; |
| 318 | } |
| 319 | |
| 320 | static void assert_success_and_decrement(void *counter, bool success) { |
| 321 | GPR_ASSERT(success); |
| 322 | --*(int *)counter; |
| 323 | } |
| 324 | |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 325 | static void decrement(void *counter, bool success) { --*(int *)counter; } |
| 326 | |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 327 | typedef struct connectivity_watch { |
| 328 | int *counter; |
| 329 | gpr_timespec deadline; |
| 330 | } connectivity_watch; |
| 331 | |
| 332 | static connectivity_watch *make_connectivity_watch(gpr_timespec s, |
| 333 | int *counter) { |
| 334 | connectivity_watch *o = gpr_malloc(sizeof(*o)); |
| 335 | o->deadline = s; |
| 336 | o->counter = counter; |
| 337 | return o; |
| 338 | } |
| 339 | |
| 340 | static void validate_connectivity_watch(void *p, bool success) { |
| 341 | connectivity_watch *w = p; |
| 342 | if (!success) { |
| 343 | GPR_ASSERT(gpr_time_cmp(gpr_now(w->deadline.clock_type), w->deadline) >= 0); |
| 344 | } |
| 345 | --*w->counter; |
| 346 | gpr_free(w); |
| 347 | } |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 348 | |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 349 | static void free_non_null(void *p) { |
| 350 | GPR_ASSERT(p != NULL); |
| 351 | gpr_free(p); |
| 352 | } |
| 353 | |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 354 | typedef enum { ROOT, CLIENT, SERVER, PENDING_SERVER } call_state_type; |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 355 | |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 356 | typedef struct call_state { |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 357 | call_state_type type; |
| 358 | grpc_call *call; |
| 359 | grpc_byte_buffer *recv_message; |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 360 | grpc_status_code status; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 361 | grpc_metadata_array recv_initial_metadata; |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 362 | grpc_metadata_array recv_trailing_metadata; |
| 363 | char *recv_status_details; |
| 364 | size_t recv_status_details_capacity; |
| 365 | int cancelled; |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 366 | int pending_ops; |
Craig Tiller | dbfc895 | 2016-04-18 22:58:10 -0700 | [diff] [blame] | 367 | grpc_call_details call_details; |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 368 | |
| 369 | struct call_state *next; |
| 370 | struct call_state *prev; |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 371 | } call_state; |
| 372 | |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 373 | static call_state *g_active_call; |
| 374 | |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 375 | static call_state *new_call(call_state *sibling, call_state_type type) { |
| 376 | call_state *c = gpr_malloc(sizeof(*c)); |
| 377 | memset(c, 0, sizeof(*c)); |
| 378 | if (sibling != NULL) { |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 379 | c->next = sibling; |
| 380 | c->prev = sibling->prev; |
| 381 | c->next->prev = c->prev->next = c; |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 382 | } else { |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 383 | c->next = c->prev = c; |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 384 | } |
| 385 | c->type = type; |
| 386 | return c; |
| 387 | } |
| 388 | |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 389 | static call_state *maybe_delete_call_state(call_state *call) { |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 390 | call_state *next = call->next; |
Craig Tiller | 2b3d33a | 2016-04-21 14:51:44 -0700 | [diff] [blame] | 391 | |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 392 | if (call->call != NULL) return next; |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 393 | if (call->pending_ops != 0) return next; |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 394 | |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 395 | if (call == g_active_call) { |
| 396 | g_active_call = call->next; |
| 397 | GPR_ASSERT(call != g_active_call); |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | call->prev->next = call->next; |
| 401 | call->next->prev = call->prev; |
| 402 | grpc_metadata_array_destroy(&call->recv_initial_metadata); |
| 403 | grpc_metadata_array_destroy(&call->recv_trailing_metadata); |
| 404 | gpr_free(call->recv_status_details); |
| 405 | grpc_call_details_destroy(&call->call_details); |
| 406 | gpr_free(call); |
| 407 | |
| 408 | return next; |
| 409 | } |
| 410 | |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 411 | static call_state *destroy_call(call_state *call) { |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 412 | grpc_call_destroy(call->call); |
| 413 | call->call = NULL; |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 414 | return maybe_delete_call_state(call); |
| 415 | } |
| 416 | |
| 417 | static void finished_request_call(void *csp, bool success) { |
| 418 | call_state *cs = csp; |
| 419 | GPR_ASSERT(cs->pending_ops > 0); |
| 420 | --cs->pending_ops; |
| 421 | if (success) { |
| 422 | GPR_ASSERT(cs->call != NULL); |
| 423 | cs->type = SERVER; |
| 424 | } else { |
| 425 | maybe_delete_call_state(cs); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static void finished_batch(void *csp, bool success) { |
| 430 | call_state *cs = csp; |
| 431 | --cs->pending_ops; |
| 432 | maybe_delete_call_state(cs); |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 435 | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 436 | grpc_test_only_set_metadata_hash_seed(0); |
| 437 | if (squelch) gpr_set_log_function(dont_log); |
| 438 | input_stream inp = {data, data + size}; |
Craig Tiller | 24d687e | 2016-04-13 19:47:27 -0700 | [diff] [blame] | 439 | grpc_resolve_address = my_resolve_address; |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 440 | grpc_tcp_client_connect_impl = my_tcp_client_connect; |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 441 | gpr_now_impl = now_impl; |
| 442 | grpc_init(); |
| 443 | |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 444 | GPR_ASSERT(g_channel == NULL); |
| 445 | GPR_ASSERT(g_server == NULL); |
| 446 | |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 447 | bool server_shutdown = false; |
| 448 | int pending_server_shutdowns = 0; |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 449 | int pending_channel_watches = 0; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 450 | int pending_pings = 0; |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 451 | |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 452 | g_active_call = new_call(NULL, ROOT); |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 453 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 454 | grpc_completion_queue *cq = grpc_completion_queue_create(NULL); |
| 455 | |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 456 | while (!is_eof(&inp) || g_channel != NULL || g_server != NULL || |
Craig Tiller | 2b3d33a | 2016-04-21 14:51:44 -0700 | [diff] [blame] | 457 | pending_channel_watches > 0 || pending_pings > 0 || |
| 458 | g_active_call->type != ROOT || g_active_call->next != g_active_call) { |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 459 | if (is_eof(&inp)) { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 460 | if (g_channel != NULL) { |
| 461 | grpc_channel_destroy(g_channel); |
| 462 | g_channel = NULL; |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 463 | } |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 464 | if (g_server != NULL) { |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 465 | if (!server_shutdown) { |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 466 | grpc_server_shutdown_and_notify( |
| 467 | g_server, cq, create_validator(assert_success_and_decrement, |
| 468 | &pending_server_shutdowns)); |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 469 | server_shutdown = true; |
Craig Tiller | 481635c | 2016-04-13 10:30:10 -0700 | [diff] [blame] | 470 | pending_server_shutdowns++; |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 471 | } else if (pending_server_shutdowns == 0) { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 472 | grpc_server_destroy(g_server); |
| 473 | g_server = NULL; |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 474 | } |
| 475 | } |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 476 | call_state *s = g_active_call; |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 477 | do { |
| 478 | if (s->type != PENDING_SERVER && s->call != NULL) { |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 479 | s = destroy_call(s); |
Craig Tiller | ffae017 | 2016-04-19 20:47:50 -0700 | [diff] [blame] | 480 | } else { |
| 481 | s = s->next; |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 482 | } |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 483 | } while (s != g_active_call); |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 484 | |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 485 | g_now = gpr_time_add(g_now, gpr_time_from_seconds(1, GPR_TIMESPAN)); |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 488 | switch (next_byte(&inp)) { |
Craig Tiller | c1e0776 | 2016-04-13 20:49:29 -0700 | [diff] [blame] | 489 | // terminate on bad bytes |
| 490 | default: |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 491 | end(&inp); |
Craig Tiller | c1e0776 | 2016-04-13 20:49:29 -0700 | [diff] [blame] | 492 | break; |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 493 | // tickle completion queue |
| 494 | case 0: { |
| 495 | grpc_event ev = grpc_completion_queue_next( |
| 496 | cq, gpr_inf_past(GPR_CLOCK_REALTIME), NULL); |
| 497 | switch (ev.type) { |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 498 | case GRPC_OP_COMPLETE: { |
| 499 | validator *v = ev.tag; |
| 500 | v->validate(v->arg, ev.success); |
| 501 | gpr_free(v); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 502 | break; |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 503 | } |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 504 | case GRPC_QUEUE_TIMEOUT: |
| 505 | break; |
| 506 | case GRPC_QUEUE_SHUTDOWN: |
| 507 | abort(); |
| 508 | break; |
| 509 | } |
| 510 | break; |
| 511 | } |
| 512 | // increment global time |
| 513 | case 1: { |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 514 | g_now = gpr_time_add( |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 515 | g_now, gpr_time_from_micros(read_uint32(&inp), GPR_TIMESPAN)); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 516 | break; |
| 517 | } |
| 518 | // create an insecure channel |
| 519 | case 2: { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 520 | if (g_channel == NULL) { |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 521 | char *target = read_string(&inp); |
| 522 | char *target_uri; |
Craig Tiller | 99f67d1 | 2016-04-13 10:52:23 -0700 | [diff] [blame] | 523 | gpr_asprintf(&target_uri, "dns:%s", target); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 524 | grpc_channel_args *args = read_args(&inp); |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 525 | g_channel = grpc_insecure_channel_create(target_uri, args, NULL); |
| 526 | GPR_ASSERT(g_channel != NULL); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 527 | grpc_channel_args_destroy(args); |
| 528 | gpr_free(target_uri); |
| 529 | gpr_free(target); |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 530 | } else { |
| 531 | end(&inp); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 532 | } |
| 533 | break; |
| 534 | } |
| 535 | // destroy a channel |
| 536 | case 3: { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 537 | if (g_channel != NULL) { |
| 538 | grpc_channel_destroy(g_channel); |
| 539 | g_channel = NULL; |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 540 | } else { |
| 541 | end(&inp); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 542 | } |
| 543 | break; |
| 544 | } |
| 545 | // bring up a server |
| 546 | case 4: { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 547 | if (g_server == NULL) { |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 548 | grpc_channel_args *args = read_args(&inp); |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 549 | g_server = grpc_server_create(args, NULL); |
| 550 | GPR_ASSERT(g_server != NULL); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 551 | grpc_channel_args_destroy(args); |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 552 | grpc_server_register_completion_queue(g_server, cq, NULL); |
| 553 | grpc_server_start(g_server); |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 554 | server_shutdown = false; |
| 555 | GPR_ASSERT(pending_server_shutdowns == 0); |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 556 | } else { |
| 557 | end(&inp); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 560 | // begin server shutdown |
| 561 | case 5: { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 562 | if (g_server != NULL) { |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 563 | grpc_server_shutdown_and_notify( |
| 564 | g_server, cq, create_validator(assert_success_and_decrement, |
| 565 | &pending_server_shutdowns)); |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 566 | pending_server_shutdowns++; |
| 567 | server_shutdown = true; |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 568 | } else { |
| 569 | end(&inp); |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 570 | } |
| 571 | break; |
| 572 | } |
| 573 | // cancel all calls if shutdown |
| 574 | case 6: { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 575 | if (g_server != NULL && server_shutdown) { |
| 576 | grpc_server_cancel_all_calls(g_server); |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 577 | } else { |
| 578 | end(&inp); |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 579 | } |
| 580 | break; |
| 581 | } |
| 582 | // destroy server |
| 583 | case 7: { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 584 | if (g_server != NULL && server_shutdown && |
Craig Tiller | 481635c | 2016-04-13 10:30:10 -0700 | [diff] [blame] | 585 | pending_server_shutdowns == 0) { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 586 | grpc_server_destroy(g_server); |
| 587 | g_server = NULL; |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 588 | } else { |
| 589 | end(&inp); |
Craig Tiller | 156a2f3 | 2016-04-13 09:03:31 -0700 | [diff] [blame] | 590 | } |
| 591 | break; |
| 592 | } |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 593 | // check connectivity |
| 594 | case 8: { |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 595 | if (g_channel != NULL) { |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 596 | uint8_t try_to_connect = next_byte(&inp); |
| 597 | if (try_to_connect == 0 || try_to_connect == 1) { |
| 598 | grpc_channel_check_connectivity_state(g_channel, try_to_connect); |
| 599 | } else { |
| 600 | end(&inp); |
| 601 | } |
| 602 | } else { |
| 603 | end(&inp); |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 604 | } |
| 605 | break; |
| 606 | } |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 607 | // watch connectivity |
| 608 | case 9: { |
| 609 | if (g_channel != NULL) { |
| 610 | grpc_connectivity_state st = |
| 611 | grpc_channel_check_connectivity_state(g_channel, 0); |
| 612 | if (st != GRPC_CHANNEL_FATAL_FAILURE) { |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 613 | gpr_timespec deadline = gpr_time_add( |
| 614 | gpr_now(GPR_CLOCK_REALTIME), |
| 615 | gpr_time_from_micros(read_uint32(&inp), GPR_TIMESPAN)); |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 616 | grpc_channel_watch_connectivity_state( |
Craig Tiller | 36750ed | 2016-04-14 08:58:35 -0700 | [diff] [blame] | 617 | g_channel, st, deadline, cq, |
| 618 | create_validator(validate_connectivity_watch, |
| 619 | make_connectivity_watch( |
| 620 | deadline, &pending_channel_watches))); |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 621 | pending_channel_watches++; |
| 622 | } |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 623 | } else { |
| 624 | end(&inp); |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 625 | } |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | // create a call |
| 629 | case 10: { |
| 630 | bool ok = true; |
| 631 | if (g_channel == NULL) ok = false; |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 632 | grpc_call *parent_call = NULL; |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 633 | if (g_active_call->type != ROOT) { |
| 634 | if (g_active_call->call == NULL || g_active_call->type == CLIENT) { |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 635 | end(&inp); |
| 636 | break; |
| 637 | } |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 638 | parent_call = g_active_call->call; |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 639 | } |
| 640 | uint32_t propagation_mask = read_uint32(&inp); |
| 641 | char *method = read_string(&inp); |
| 642 | char *host = read_string(&inp); |
| 643 | gpr_timespec deadline = |
| 644 | gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 645 | gpr_time_from_micros(read_uint32(&inp), GPR_TIMESPAN)); |
| 646 | |
| 647 | if (ok) { |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 648 | call_state *cs = new_call(g_active_call, CLIENT); |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 649 | cs->call = |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 650 | grpc_channel_create_call(g_channel, parent_call, propagation_mask, |
| 651 | cq, method, host, deadline, NULL); |
| 652 | } else { |
| 653 | end(&inp); |
| 654 | } |
Craig Tiller | 1612abc | 2016-04-19 19:43:47 -0700 | [diff] [blame] | 655 | gpr_free(method); |
| 656 | gpr_free(host); |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 657 | break; |
| 658 | } |
| 659 | // switch the 'current' call |
| 660 | case 11: { |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 661 | g_active_call = g_active_call->next; |
Craig Tiller | ac82c18 | 2016-04-14 09:29:13 -0700 | [diff] [blame] | 662 | break; |
Craig Tiller | d2fd769 | 2016-04-13 22:44:48 -0700 | [diff] [blame] | 663 | } |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 664 | // queue some ops on a call |
| 665 | case 12: { |
Craig Tiller | 2b3d33a | 2016-04-21 14:51:44 -0700 | [diff] [blame] | 666 | if (g_active_call->type == PENDING_SERVER || |
| 667 | g_active_call->type == ROOT || g_active_call->call == NULL) { |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 668 | end(&inp); |
| 669 | break; |
| 670 | } |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 671 | size_t num_ops = next_byte(&inp); |
Craig Tiller | cb6d406 | 2016-04-19 19:58:36 -0700 | [diff] [blame] | 672 | if (num_ops > 6) { |
| 673 | end(&inp); |
| 674 | break; |
| 675 | } |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 676 | grpc_op *ops = gpr_malloc(sizeof(grpc_op) * num_ops); |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 677 | bool ok = true; |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 678 | size_t i; |
| 679 | grpc_op *op; |
| 680 | for (i = 0; i < num_ops; i++) { |
| 681 | op = &ops[i]; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 682 | switch (next_byte(&inp)) { |
| 683 | default: |
Craig Tiller | cb6d406 | 2016-04-19 19:58:36 -0700 | [diff] [blame] | 684 | /* invalid value */ |
Craig Tiller | 7bbcd74 | 2016-04-19 20:00:16 -0700 | [diff] [blame] | 685 | op->op = (grpc_op_type)-1; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 686 | ok = false; |
| 687 | break; |
| 688 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 689 | op->op = GRPC_OP_SEND_INITIAL_METADATA; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 690 | read_metadata(&inp, &op->data.send_initial_metadata.count, |
| 691 | &op->data.send_initial_metadata.metadata); |
| 692 | break; |
| 693 | case GRPC_OP_SEND_MESSAGE: |
Craig Tiller | 2ca75bf | 2016-04-15 13:19:55 -0700 | [diff] [blame] | 694 | op->op = GRPC_OP_SEND_MESSAGE; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 695 | op->data.send_message = read_message(&inp); |
| 696 | break; |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 697 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 698 | op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 699 | break; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 700 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 701 | op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 702 | read_metadata( |
| 703 | &inp, |
| 704 | &op->data.send_status_from_server.trailing_metadata_count, |
| 705 | &op->data.send_status_from_server.trailing_metadata); |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 706 | op->data.send_status_from_server.status = next_byte(&inp); |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 707 | op->data.send_status_from_server.status_details = |
| 708 | read_string(&inp); |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 709 | break; |
| 710 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 711 | op->op = GRPC_OP_RECV_INITIAL_METADATA; |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 712 | op->data.recv_initial_metadata = |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 713 | &g_active_call->recv_initial_metadata; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 714 | break; |
| 715 | case GRPC_OP_RECV_MESSAGE: |
| 716 | op->op = GRPC_OP_RECV_MESSAGE; |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 717 | op->data.recv_message = &g_active_call->recv_message; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 718 | break; |
| 719 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 720 | op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 721 | op->data.recv_status_on_client.status = &g_active_call->status; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 722 | op->data.recv_status_on_client.trailing_metadata = |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 723 | &g_active_call->recv_trailing_metadata; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 724 | op->data.recv_status_on_client.status_details = |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 725 | &g_active_call->recv_status_details; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 726 | op->data.recv_status_on_client.status_details_capacity = |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 727 | &g_active_call->recv_status_details_capacity; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 728 | break; |
| 729 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 730 | op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
Craig Tiller | 2b3d33a | 2016-04-21 14:51:44 -0700 | [diff] [blame] | 731 | op->data.recv_close_on_server.cancelled = |
| 732 | &g_active_call->cancelled; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 733 | break; |
| 734 | } |
| 735 | op->reserved = NULL; |
| 736 | op->flags = read_uint32(&inp); |
Craig Tiller | 3e0d936 | 2016-04-15 13:36:25 -0700 | [diff] [blame] | 737 | } |
| 738 | if (ok) { |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 739 | validator *v = create_validator(finished_batch, g_active_call); |
| 740 | g_active_call->pending_ops++; |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 741 | grpc_call_error error = |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 742 | grpc_call_start_batch(g_active_call->call, ops, num_ops, v, NULL); |
Craig Tiller | 3e0d936 | 2016-04-15 13:36:25 -0700 | [diff] [blame] | 743 | if (error != GRPC_CALL_OK) { |
| 744 | v->validate(v->arg, false); |
| 745 | gpr_free(v); |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 746 | } |
Craig Tiller | 3e0d936 | 2016-04-15 13:36:25 -0700 | [diff] [blame] | 747 | } else { |
| 748 | end(&inp); |
| 749 | } |
| 750 | for (i = 0; i < num_ops; i++) { |
| 751 | op = &ops[i]; |
| 752 | switch (op->op) { |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 753 | case GRPC_OP_SEND_INITIAL_METADATA: |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 754 | for (size_t j = 0; j < op->data.send_initial_metadata.count; |
| 755 | j++) { |
| 756 | gpr_free( |
| 757 | (void *)op->data.send_initial_metadata.metadata[j].key); |
| 758 | gpr_free( |
| 759 | (void *)op->data.send_initial_metadata.metadata[j].value); |
Craig Tiller | 4170358 | 2016-04-15 13:27:40 -0700 | [diff] [blame] | 760 | } |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 761 | gpr_free(op->data.send_initial_metadata.metadata); |
| 762 | break; |
| 763 | case GRPC_OP_SEND_MESSAGE: |
| 764 | grpc_byte_buffer_destroy(op->data.send_message); |
| 765 | break; |
| 766 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 767 | for (size_t j = 0; |
| 768 | j < op->data.send_status_from_server.trailing_metadata_count; |
| 769 | j++) { |
| 770 | gpr_free((void *)op->data.send_status_from_server |
| 771 | .trailing_metadata[j] |
| 772 | .key); |
| 773 | gpr_free((void *)op->data.send_status_from_server |
| 774 | .trailing_metadata[j] |
| 775 | .value); |
Craig Tiller | 4170358 | 2016-04-15 13:27:40 -0700 | [diff] [blame] | 776 | } |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 777 | gpr_free(op->data.send_status_from_server.trailing_metadata); |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 778 | gpr_free((void *)op->data.send_status_from_server.status_details); |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 779 | break; |
| 780 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 781 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 782 | case GRPC_OP_RECV_MESSAGE: |
| 783 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 784 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 785 | break; |
Craig Tiller | ba0d6ac | 2016-04-15 13:07:54 -0700 | [diff] [blame] | 786 | } |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 787 | } |
Craig Tiller | 3e0d936 | 2016-04-15 13:36:25 -0700 | [diff] [blame] | 788 | gpr_free(ops); |
| 789 | |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 790 | break; |
| 791 | } |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 792 | // cancel current call |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 793 | case 13: { |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 794 | if (g_active_call->type != ROOT && g_active_call->call != NULL) { |
| 795 | grpc_call_cancel(g_active_call->call, NULL); |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 796 | } else { |
| 797 | end(&inp); |
| 798 | } |
| 799 | break; |
| 800 | } |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 801 | // get a calls peer |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 802 | case 14: { |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 803 | if (g_active_call->type != ROOT && g_active_call->call != NULL) { |
| 804 | free_non_null(grpc_call_get_peer(g_active_call->call)); |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 805 | } else { |
| 806 | end(&inp); |
| 807 | } |
| 808 | break; |
| 809 | } |
| 810 | // get a channels target |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 811 | case 15: { |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 812 | if (g_channel != NULL) { |
| 813 | free_non_null(grpc_channel_get_target(g_channel)); |
| 814 | } else { |
| 815 | end(&inp); |
| 816 | } |
| 817 | break; |
| 818 | } |
| 819 | // send a ping on a channel |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 820 | case 16: { |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 821 | if (g_channel != NULL) { |
Craig Tiller | 36d6f78 | 2016-04-15 13:48:21 -0700 | [diff] [blame] | 822 | pending_pings++; |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 823 | grpc_channel_ping(g_channel, cq, |
| 824 | create_validator(decrement, &pending_pings), NULL); |
| 825 | } else { |
| 826 | end(&inp); |
| 827 | } |
| 828 | break; |
| 829 | } |
| 830 | // enable a tracer |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 831 | case 17: { |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 832 | char *tracer = read_string(&inp); |
| 833 | grpc_tracer_set_enabled(tracer, 1); |
| 834 | gpr_free(tracer); |
| 835 | break; |
| 836 | } |
| 837 | // disable a tracer |
Craig Tiller | 07f2e5f | 2016-04-18 22:29:26 -0700 | [diff] [blame] | 838 | case 18: { |
Craig Tiller | 1395e19 | 2016-04-14 12:49:27 -0700 | [diff] [blame] | 839 | char *tracer = read_string(&inp); |
| 840 | grpc_tracer_set_enabled(tracer, 0); |
| 841 | gpr_free(tracer); |
| 842 | break; |
| 843 | } |
Craig Tiller | dbfc895 | 2016-04-18 22:58:10 -0700 | [diff] [blame] | 844 | // request a server call |
| 845 | case 19: { |
| 846 | if (g_server == NULL) { |
| 847 | end(&inp); |
| 848 | break; |
| 849 | } |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 850 | call_state *cs = new_call(g_active_call, PENDING_SERVER); |
| 851 | cs->pending_ops++; |
| 852 | validator *v = create_validator(finished_request_call, cs); |
Craig Tiller | 33fd9fa | 2016-04-19 16:13:21 -0700 | [diff] [blame] | 853 | grpc_call_error error = |
| 854 | grpc_server_request_call(g_server, &cs->call, &cs->call_details, |
| 855 | &cs->recv_initial_metadata, cq, cq, v); |
| 856 | if (error != GRPC_CALL_OK) { |
| 857 | v->validate(v->arg, false); |
| 858 | gpr_free(v); |
| 859 | } |
Craig Tiller | dbfc895 | 2016-04-18 22:58:10 -0700 | [diff] [blame] | 860 | break; |
| 861 | } |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 862 | // destroy a call |
| 863 | case 20: { |
Craig Tiller | 2b3d33a | 2016-04-21 14:51:44 -0700 | [diff] [blame] | 864 | if (g_active_call->type != ROOT && |
| 865 | g_active_call->type != PENDING_SERVER && |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 866 | g_active_call->call != NULL) { |
| 867 | destroy_call(g_active_call); |
Craig Tiller | 36cce53 | 2016-04-19 17:02:49 -0700 | [diff] [blame] | 868 | } else { |
| 869 | end(&inp); |
| 870 | } |
| 871 | break; |
| 872 | } |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 876 | GPR_ASSERT(g_channel == NULL); |
| 877 | GPR_ASSERT(g_server == NULL); |
Craig Tiller | 6321cf2 | 2016-04-21 14:37:23 -0700 | [diff] [blame] | 878 | GPR_ASSERT(g_active_call->type == ROOT); |
| 879 | GPR_ASSERT(g_active_call->next == g_active_call); |
| 880 | gpr_free(g_active_call); |
Craig Tiller | f224c0c | 2016-04-13 20:15:13 -0700 | [diff] [blame] | 881 | |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 882 | grpc_completion_queue_shutdown(cq); |
| 883 | GPR_ASSERT( |
| 884 | grpc_completion_queue_next(cq, gpr_inf_past(GPR_CLOCK_REALTIME), NULL) |
| 885 | .type == GRPC_QUEUE_SHUTDOWN); |
| 886 | grpc_completion_queue_destroy(cq); |
| 887 | |
| 888 | grpc_shutdown(); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 889 | return 0; |
| 890 | } |