| 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 <algorithm> |
| 23 |
#include <atomic> |
| 24 |
#include <functional> |
| 25 |
#include <initializer_list> |
| 26 |
#include <memory> |
| 27 |
#include <new> |
| 28 |
#include <string> |
| 29 |
#include <utility> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#include "absl/status/status.h" |
| 33 |
#include "absl/status/statusor.h" |
| 34 |
#include "absl/strings/string_view.h" |
| 35 |
#include "absl/types/optional.h" |
| 36 |
|
| 37 |
#include <grpc/byte_buffer.h> |
| 38 |
#include <grpc/event_engine/endpoint_config.h> |
| 39 |
#include <grpc/event_engine/event_engine.h> |
| 40 |
#include <grpc/grpc.h> |
| 41 |
#include <grpc/grpc_security.h> |
| 42 |
#include <grpc/slice.h> |
| 43 |
#include <grpc/status.h> |
| 44 |
#include <grpc/support/alloc.h> |
| 45 |
#include <grpc/support/log.h> |
| 46 |
#include <grpc/support/string_util.h> |
| 47 |
#include <grpc/support/time.h> |
| 48 |
|
| 49 |
#include "src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.h" |
| 50 |
#include "src/core/ext/transport/chttp2/transport/chttp2_transport.h" |
| 51 |
#include "src/core/lib/address_utils/parse_address.h" |
| 52 |
#include "src/core/lib/channel/channel_args.h" |
| 53 |
#include "src/core/lib/channel/channelz.h" |
| 54 |
#include "src/core/lib/event_engine/default_event_engine.h" |
| 55 |
#include "src/core/lib/gprpp/debug_location.h" |
| 56 |
#include "src/core/lib/gprpp/env.h" |
| 57 |
#include "src/core/lib/gprpp/time.h" |
| 58 |
#include "src/core/lib/iomgr/closure.h" |
| 59 |
#include "src/core/lib/iomgr/endpoint.h" |
| 60 |
#include "src/core/lib/iomgr/error.h" |
| 61 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 62 |
#include "src/core/lib/iomgr/executor.h" |
| 63 |
#include "src/core/lib/iomgr/iomgr_fwd.h" |
| 64 |
#include "src/core/lib/iomgr/resolve_address.h" |
| 65 |
#include "src/core/lib/iomgr/resolved_address.h" |
| 66 |
#include "src/core/lib/iomgr/tcp_client.h" |
| 67 |
#include "src/core/lib/iomgr/timer.h" |
| 68 |
#include "src/core/lib/iomgr/timer_manager.h" |
| 69 |
#include "src/core/lib/resolver/server_address.h" |
| 70 |
#include "src/core/lib/slice/slice_internal.h" |
| 71 |
#include "src/core/lib/surface/channel.h" |
| 72 |
#include "src/core/lib/surface/server.h" |
| 73 |
#include "src/core/lib/transport/transport_fwd.h" |
| 74 |
#include "src/libfuzzer/libfuzzer_macro.h" |
| 75 |
#include "test/core/end2end/data/ssl_test_data.h" |
| 76 |
#include "test/core/end2end/fuzzers/api_fuzzer.pb.h" |
| 77 |
#include "test/core/event_engine/fuzzing_event_engine/fuzzing_event_engine.h" |
| 78 |
#include "test/core/event_engine/fuzzing_event_engine/fuzzing_event_engine.pb.h" |
| 79 |
#include "test/core/util/passthru_endpoint.h" |
| 80 |
|
| 81 |
// IWYU pragma: no_include <google/protobuf/repeated_ptr_field.h> |
| 82 |
|
| 83 |
// Applicable when simulating channel actions. Prevents overflows. |
| 84 |
static constexpr uint64_t kMaxWaitMs = grpc_core::Duration::Hours(1).millis(); |
| 85 |
// Applicable when simulating channel actions. Prevents overflows. |
| 86 |
static constexpr uint64_t kMaxAddNReadableBytes = (2 * 1024 * 1024); // 2GB |
| 87 |
// Applicable when simulating channel actions. Prevents overflows. |
| 88 |
static constexpr uint64_t kMaxAddNWritableBytes = (2 * 1024 * 1024); // 2GB |
| 89 |
|
| 90 |
//////////////////////////////////////////////////////////////////////////////// |
| 91 |
// logging |
| 92 |
|
| 93 |
bool squelch = true; |
| 94 |
bool leak_check = true; |
| 95 |
|
| 96 |
static void dont_log(gpr_log_func_args* /*args*/) {} |
| 97 |
|
| 98 |
//////////////////////////////////////////////////////////////////////////////// |
| 99 |
// global state |
| 100 |
|
| 101 |
static grpc_server* g_server; |
| 102 |
static grpc_channel* g_channel; |
| 103 |
static grpc_resource_quota* g_resource_quota; |
| 104 |
static std::vector<grpc_passthru_endpoint_channel_action> g_channel_actions; |
| 105 |
static std::atomic<bool> g_channel_force_delete{false}; |
| 106 |
|
| 107 |
//////////////////////////////////////////////////////////////////////////////// |
| 108 |
// dns resolution |
| 109 |
|
| 110 |
typedef struct addr_req { |
| 111 |
grpc_timer timer; |
| 112 |
char* addr; |
| 113 |
grpc_closure* on_done; |
| 114 |
std::unique_ptr<grpc_core::ServerAddressList>* addresses; |
| 115 |
} addr_req; |
| 116 |
|
| 117 |
static void finish_resolve(void* arg, grpc_error_handle error) { |
| 118 |
addr_req* r = static_cast<addr_req*>(arg); |
| 119 |
|
| 120 |
if (error.ok() && 0 == strcmp(r->addr, "server")) { |
| 121 |
*r->addresses = std::make_unique<grpc_core::ServerAddressList>(); |
| 122 |
grpc_resolved_address fake_resolved_address; |
| 123 |
GPR_ASSERT( |
| 124 |
grpc_parse_ipv4_hostport("1.2.3.4:5", &fake_resolved_address, false)); |
| 125 |
(*r->addresses) |
| 126 |
->emplace_back(fake_resolved_address, grpc_core::ChannelArgs()); |
| 127 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, r->on_done, absl::OkStatus()); |
| 128 |
} else { |
| 129 |
grpc_core::ExecCtx::Run( |
| 130 |
DEBUG_LOCATION, r->on_done, |
| 131 |
GRPC_ERROR_CREATE_REFERENCING("Resolution failed", &error, 1)); |
| 132 |
} |
| 133 |
|
| 134 |
gpr_free(r->addr); |
| 135 |
delete r; |
| 136 |
} |
| 137 |
|
| 138 |
namespace { |
| 139 |
|
| 140 |
using ::grpc_event_engine::experimental::FuzzingEventEngine; |
| 141 |
using ::grpc_event_engine::experimental::GetDefaultEventEngine; |
| 142 |
|
| 143 |
class FuzzerDNSResolver : public grpc_core::DNSResolver { |
| 144 |
public: |
| 145 |
class FuzzerDNSRequest { |
| 146 |
public: |
| 147 |
FuzzerDNSRequest( |
| 148 |
absl::string_view name, |
| 149 |
std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)> |
| 150 |
on_done) |
| 151 |
: name_(std::string(name)), on_done_(std::move(on_done)) { |
| 152 |
grpc_timer_init( |
| 153 |
&timer_, |
| 154 |
grpc_core::Duration::Seconds(1) + grpc_core::Timestamp::Now(), |
| 155 |
GRPC_CLOSURE_CREATE(FinishResolve, this, grpc_schedule_on_exec_ctx)); |
| 156 |
} |
| 157 |
|
| 158 |
private: |
| 159 |
static void FinishResolve(void* arg, grpc_error_handle error) { |
| 160 |
FuzzerDNSRequest* self = static_cast<FuzzerDNSRequest*>(arg); |
| 161 |
if (error.ok() && self->name_ == "server") { |
| 162 |
std::vector<grpc_resolved_address> addrs; |
| 163 |
grpc_resolved_address addr; |
| 164 |
addr.len = 0; |
| 165 |
addrs.push_back(addr); |
| 166 |
self->on_done_(std::move(addrs)); |
| 167 |
} else { |
| 168 |
self->on_done_(absl::UnknownError("Resolution failed")); |
| 169 |
} |
| 170 |
delete self; |
| 171 |
} |
| 172 |
|
| 173 |
const std::string name_; |
| 174 |
const std::function<void( |
| 175 |
absl::StatusOr<std::vector<grpc_resolved_address>>)> |
| 176 |
on_done_; |
| 177 |
grpc_timer timer_; |
| 178 |
}; |
| 179 |
|
| 180 |
explicit FuzzerDNSResolver(FuzzingEventEngine* engine) : engine_(engine) {} |
| 181 |
|
| 182 |
TaskHandle LookupHostname( |
| 183 |
std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)> |
| 184 |
on_resolved, |
| 185 |
absl::string_view name, absl::string_view /* default_port */, |
| 186 |
grpc_core::Duration /* timeout */, |
| 187 |
grpc_pollset_set* /* interested_parties */, |
| 188 |
absl::string_view /* name_server */) override { |
| 189 |
new FuzzerDNSRequest(name, std::move(on_resolved)); |
| 190 |
return kNullHandle; |
| 191 |
} |
| 192 |
|
| 193 |
absl::StatusOr<std::vector<grpc_resolved_address>> LookupHostnameBlocking( |
| 194 |
absl::string_view /* name */, |
| 195 |
absl::string_view /* default_port */) override { |
| 196 |
GPR_ASSERT(0); |
| 197 |
} |
| 198 |
|
| 199 |
TaskHandle LookupSRV( |
| 200 |
std::function<void(absl::StatusOr<std::vector<grpc_resolved_address>>)> |
| 201 |
on_resolved, |
| 202 |
absl::string_view /* name */, grpc_core::Duration /* timeout */, |
| 203 |
grpc_pollset_set* /* interested_parties */, |
| 204 |
absl::string_view /* name_server */) override { |
| 205 |
engine_->Run([on_resolved] { |
| 206 |
grpc_core::ApplicationCallbackExecCtx app_exec_ctx; |
| 207 |
grpc_core::ExecCtx exec_ctx; |
| 208 |
on_resolved(absl::UnimplementedError( |
| 209 |
"The Fuzzing DNS resolver does not support looking up SRV records")); |
| 210 |
}); |
| 211 |
return {-1, -1}; |
| 212 |
}; |
| 213 |
|
| 214 |
TaskHandle LookupTXT( |
| 215 |
std::function<void(absl::StatusOr<std::string>)> on_resolved, |
| 216 |
absl::string_view /* name */, grpc_core::Duration /* timeout */, |
| 217 |
grpc_pollset_set* /* interested_parties */, |
| 218 |
absl::string_view /* name_server */) override { |
| 219 |
// Not supported |
| 220 |
engine_->Run([on_resolved] { |
| 221 |
grpc_core::ApplicationCallbackExecCtx app_exec_ctx; |
| 222 |
grpc_core::ExecCtx exec_ctx; |
| 223 |
on_resolved(absl::UnimplementedError( |
| 224 |
"The Fuzing DNS resolver does not support looking up TXT records")); |
| 225 |
}); |
| 226 |
return {-1, -1}; |
| 227 |
}; |
| 228 |
|
| 229 |
// FuzzerDNSResolver does not support cancellation. |
| 230 |
bool Cancel(TaskHandle /*handle*/) override { return false; } |
| 231 |
|
| 232 |
private: |
| 233 |
FuzzingEventEngine* engine_; |
| 234 |
}; |
| 235 |
|
| 236 |
} // namespace |
| 237 |
|
| 238 |
grpc_ares_request* my_dns_lookup_ares( |
| 239 |
const char* /*dns_server*/, const char* addr, const char* /*default_port*/, |
| 240 |
grpc_pollset_set* /*interested_parties*/, grpc_closure* on_done, |
| 241 |
std::unique_ptr<grpc_core::ServerAddressList>* addresses, |
| 242 |
int /*query_timeout*/) { |
| 243 |
addr_req* r = new addr_req(); |
| 244 |
r->addr = gpr_strdup(addr); |
| 245 |
r->on_done = on_done; |
| 246 |
r->addresses = addresses; |
| 247 |
grpc_timer_init( |
| 248 |
&r->timer, grpc_core::Duration::Seconds(1) + grpc_core::Timestamp::Now(), |
| 249 |
GRPC_CLOSURE_CREATE(finish_resolve, r, grpc_schedule_on_exec_ctx)); |
| 250 |
return nullptr; |
| 251 |
} |
| 252 |
|
| 253 |
static void my_cancel_ares_request(grpc_ares_request* request) { |
| 254 |
GPR_ASSERT(request == nullptr); |
| 255 |
} |
| 256 |
//////////////////////////////////////////////////////////////////////////////// |
| 257 |
// client connection |
| 258 |
|
| 259 |
static void sched_connect(grpc_closure* closure, grpc_endpoint** ep, |
| 260 |
gpr_timespec deadline); |
| 261 |
|
| 262 |
typedef struct { |
| 263 |
grpc_timer timer; |
| 264 |
grpc_closure* closure; |
| 265 |
grpc_endpoint** ep; |
| 266 |
gpr_timespec deadline; |
| 267 |
} future_connect; |
| 268 |
|
| 269 |
static void do_connect(void* arg, grpc_error_handle error) { |
| 270 |
future_connect* fc = static_cast<future_connect*>(arg); |
| 271 |
if (!error.ok()) { |
| 272 |
*fc->ep = nullptr; |
| 273 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, fc->closure, error); |
| 274 |
} else if (g_server != nullptr) { |
| 275 |
grpc_endpoint* client; |
| 276 |
grpc_endpoint* server; |
| 277 |
grpc_passthru_endpoint_create(&client, &server, nullptr, true); |
| 278 |
*fc->ep = client; |
| 279 |
start_scheduling_grpc_passthru_endpoint_channel_effects(client, |
| 280 |
g_channel_actions); |
| 281 |
|
| 282 |
grpc_core::Server* core_server = grpc_core::Server::FromC(g_server); |
| 283 |
grpc_transport* transport = grpc_create_chttp2_transport( |
| 284 |
core_server->channel_args(), server, false); |
| 285 |
GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 286 |
"SetupTransport", |
| 287 |
core_server->SetupTransport(transport, nullptr, |
| 288 |
core_server->channel_args(), nullptr))); |
| 289 |
grpc_chttp2_transport_start_reading(transport, nullptr, nullptr, nullptr); |
| 290 |
|
| 291 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, fc->closure, absl::OkStatus()); |
| 292 |
} else { |
| 293 |
sched_connect(fc->closure, fc->ep, fc->deadline); |
| 294 |
} |
| 295 |
gpr_free(fc); |
| 296 |
} |
| 297 |
|
| 298 |
static void sched_connect(grpc_closure* closure, grpc_endpoint** ep, |
| 299 |
gpr_timespec deadline) { |
| 300 |
if (gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) < 0) { |
| 301 |
*ep = nullptr; |
| 302 |
grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure, |
| 303 |
GRPC_ERROR_CREATE("Connect deadline exceeded")); |
| 304 |
return; |
| 305 |
} |
| 306 |
|
| 307 |
future_connect* fc = static_cast<future_connect*>(gpr_malloc(sizeof(*fc))); |
| 308 |
fc->closure = closure; |
| 309 |
fc->ep = ep; |
| 310 |
fc->deadline = deadline; |
| 311 |
grpc_timer_init( |
| 312 |
&fc->timer, grpc_core::Duration::Seconds(1) + grpc_core::Timestamp::Now(), |
| 313 |
GRPC_CLOSURE_CREATE(do_connect, fc, grpc_schedule_on_exec_ctx)); |
| 314 |
} |
| 315 |
|
| 316 |
static int64_t my_tcp_client_connect( |
| 317 |
grpc_closure* closure, grpc_endpoint** ep, |
| 318 |
grpc_pollset_set* /*interested_parties*/, |
| 319 |
const grpc_event_engine::experimental::EndpointConfig& /*config*/, |
| 320 |
const grpc_resolved_address* /*addr*/, grpc_core::Timestamp deadline) { |
| 321 |
sched_connect(closure, ep, deadline.as_timespec(GPR_CLOCK_MONOTONIC)); |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static bool my_tcp_cancel_connect(int64_t /*connection_handle*/) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
grpc_tcp_client_vtable fuzz_tcp_client_vtable = {my_tcp_client_connect, |
| 330 |
my_tcp_cancel_connect}; |
| 331 |
|
| 332 |
//////////////////////////////////////////////////////////////////////////////// |
| 333 |
// test driver |
| 334 |
|
| 335 |
class Validator { |
| 336 |
public: |
| 337 |
explicit Validator(std::function<void(bool)> impl) : impl_(std::move(impl)) {} |
| 338 |
|
| 339 |
virtual ~Validator() {} |
| 340 |
void Run(bool success) { |
| 341 |
impl_(success); |
| 342 |
delete this; |
| 343 |
} |
| 344 |
|
| 345 |
private: |
| 346 |
std::function<void(bool)> impl_; |
| 347 |
}; |
| 348 |
|
| 349 |
Validator* MakeValidator(std::function<void(bool)> impl) { |
| 350 |
return new Validator(std::move(impl)); |
| 351 |
} |
| 352 |
|
| 353 |
static Validator* AssertSuccessAndDecrement(int* counter) { |
| 354 |
return MakeValidator([counter](bool success) { |
| 355 |
GPR_ASSERT(success); |
| 356 |
--*counter; |
| 357 |
}); |
| 358 |
} |
| 359 |
|
| 360 |
static Validator* Decrement(int* counter) { |
| 361 |
return MakeValidator([counter](bool) { --*counter; }); |
| 362 |
} |
| 363 |
|
| 364 |
static Validator* ValidateConnectivityWatch(gpr_timespec deadline, |
| 365 |
int* counter) { |
| 366 |
return MakeValidator([deadline, counter](bool success) { |
| 367 |
if (!success) { |
| 368 |
auto now = gpr_now(deadline.clock_type); |
| 369 |
GPR_ASSERT(gpr_time_cmp(now, deadline) >= 0); |
| 370 |
} |
| 371 |
--*counter; |
| 372 |
}); |
| 373 |
} |
| 374 |
|
| 375 |
static void free_non_null(void* p) { |
| 376 |
GPR_ASSERT(p != nullptr); |
| 377 |
gpr_free(p); |
| 378 |
} |
| 379 |
|
| 380 |
enum class CallType { CLIENT, SERVER, PENDING_SERVER, TOMBSTONED }; |
| 381 |
|
| 382 |
class Call : public std::enable_shared_from_this<Call> { |
| 383 |
public: |
| 384 |
explicit Call(CallType type) : type_(type) { |
| 385 |
grpc_metadata_array_init(&recv_initial_metadata_); |
| 386 |
grpc_metadata_array_init(&recv_trailing_metadata_); |
| 387 |
grpc_call_details_init(&call_details_); |
| 388 |
} |
| 389 |
|
| 390 |
~Call(); |
| 391 |
|
| 392 |
CallType type() const { return type_; } |
| 393 |
|
| 394 |
bool done() const { |
| 395 |
if ((type_ == CallType::TOMBSTONED || call_closed_) && pending_ops_ == 0) { |
| 396 |
return true; |
| 397 |
} |
| 398 |
if (call_ == nullptr && type() != CallType::PENDING_SERVER) return true; |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
void Shutdown() { |
| 403 |
if (call_ != nullptr) { |
| 404 |
grpc_call_cancel(call_, nullptr); |
| 405 |
type_ = CallType::TOMBSTONED; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
void SetCall(grpc_call* call) { |
| 410 |
GPR_ASSERT(call_ == nullptr); |
| 411 |
call_ = call; |
| 412 |
} |
| 413 |
|
| 414 |
grpc_call* call() const { return call_; } |
| 415 |
|
| 416 |
void RequestCall(grpc_server* server, grpc_completion_queue* cq) { |
| 417 |
auto* v = FinishedRequestCall(); |
| 418 |
grpc_call_error error = grpc_server_request_call( |
| 419 |
server, &call_, &call_details_, &recv_initial_metadata_, cq, cq, v); |
| 420 |
if (error != GRPC_CALL_OK) { |
| 421 |
v->Run(false); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
void* Allocate(size_t size) { |
| 426 |
void* p = gpr_malloc(size); |
| 427 |
free_pointers_.push_back(p); |
| 428 |
return p; |
| 429 |
} |
| 430 |
|
| 431 |
template <typename T> |
| 432 |
T* AllocArray(size_t elems) { |
| 433 |
return static_cast<T*>(Allocate(sizeof(T) * elems)); |
| 434 |
} |
| 435 |
|
| 436 |
template <typename T> |
| 437 |
T* NewCopy(T value) { |
| 438 |
T* p = AllocArray<T>(1); |
| 439 |
new (p) T(value); |
| 440 |
return p; |
| 441 |
} |
| 442 |
|
| 443 |
template <typename T> |
| 444 |
grpc_slice ReadSlice(const T& s) { |
| 445 |
grpc_slice slice = grpc_slice_from_cpp_string(s.value()); |
| 446 |
unref_slices_.push_back(slice); |
| 447 |
return slice; |
| 448 |
} |
| 449 |
|
| 450 |
template <typename M> |
| 451 |
grpc_metadata_array ReadMetadata(const M& metadata) { |
| 452 |
grpc_metadata* m = AllocArray<grpc_metadata>(metadata.size()); |
| 453 |
for (int i = 0; i < metadata.size(); ++i) { |
| 454 |
m[i].key = ReadSlice(metadata[i].key()); |
| 455 |
m[i].value = ReadSlice(metadata[i].value()); |
| 456 |
} |
| 457 |
return grpc_metadata_array{static_cast<size_t>(metadata.size()), |
| 458 |
static_cast<size_t>(metadata.size()), m}; |
| 459 |
} |
| 460 |
|
| 461 |
absl::optional<grpc_op> ReadOp( |
| 462 |
const api_fuzzer::BatchOp& batch_op, bool* batch_is_ok, |
| 463 |
uint8_t* batch_ops, std::vector<std::function<void()>>* unwinders) { |
| 464 |
grpc_op op; |
| 465 |
memset(&op, 0, sizeof(op)); |
| 466 |
switch (batch_op.op_case()) { |
| 467 |
case api_fuzzer::BatchOp::OP_NOT_SET: |
| 468 |
/* invalid value */ |
| 469 |
return {}; |
| 470 |
case api_fuzzer::BatchOp::kSendInitialMetadata: |
| 471 |
if (sent_initial_metadata_) { |
| 472 |
*batch_is_ok = false; |
| 473 |
} else { |
| 474 |
sent_initial_metadata_ = true; |
| 475 |
op.op = GRPC_OP_SEND_INITIAL_METADATA; |
| 476 |
*batch_ops |= 1 << GRPC_OP_SEND_INITIAL_METADATA; |
| 477 |
auto ary = ReadMetadata(batch_op.send_initial_metadata().metadata()); |
| 478 |
op.data.send_initial_metadata.count = ary.count; |
| 479 |
op.data.send_initial_metadata.metadata = ary.metadata; |
| 480 |
} |
| 481 |
break; |
| 482 |
case api_fuzzer::BatchOp::kSendMessage: |
| 483 |
op.op = GRPC_OP_SEND_MESSAGE; |
| 484 |
if (send_message_ != nullptr) { |
| 485 |
*batch_is_ok = false; |
| 486 |
} else { |
| 487 |
*batch_ops |= 1 << GRPC_OP_SEND_MESSAGE; |
| 488 |
std::vector<grpc_slice> slices; |
| 489 |
for (const auto& m : batch_op.send_message().message()) { |
| 490 |
slices.push_back(ReadSlice(m)); |
| 491 |
} |
| 492 |
send_message_ = op.data.send_message.send_message = |
| 493 |
grpc_raw_byte_buffer_create(slices.data(), slices.size()); |
| 494 |
unwinders->push_back([this]() { |
| 495 |
grpc_byte_buffer_destroy(send_message_); |
| 496 |
send_message_ = nullptr; |
| 497 |
}); |
| 498 |
} |
| 499 |
break; |
| 500 |
case api_fuzzer::BatchOp::kSendCloseFromClient: |
| 501 |
op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 502 |
*batch_ops |= 1 << GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
| 503 |
break; |
| 504 |
case api_fuzzer::BatchOp::kSendStatusFromServer: { |
| 505 |
op.op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 506 |
*batch_ops |= 1 << GRPC_OP_SEND_STATUS_FROM_SERVER; |
| 507 |
auto ary = ReadMetadata(batch_op.send_status_from_server().metadata()); |
| 508 |
op.data.send_status_from_server.trailing_metadata_count = ary.count; |
| 509 |
op.data.send_status_from_server.trailing_metadata = ary.metadata; |
| 510 |
op.data.send_status_from_server.status = static_cast<grpc_status_code>( |
| 511 |
batch_op.send_status_from_server().status_code()); |
| 512 |
op.data.send_status_from_server.status_details = |
| 513 |
batch_op.send_status_from_server().has_status_details() |
| 514 |
? NewCopy(ReadSlice( |
| 515 |
batch_op.send_status_from_server().status_details())) |
| 516 |
: nullptr; |
| 517 |
} break; |
| 518 |
case api_fuzzer::BatchOp::kReceiveInitialMetadata: |
| 519 |
if (enqueued_recv_initial_metadata_) { |
| 520 |
*batch_is_ok = false; |
| 521 |
} else { |
| 522 |
enqueued_recv_initial_metadata_ = true; |
| 523 |
op.op = GRPC_OP_RECV_INITIAL_METADATA; |
| 524 |
*batch_ops |= 1 << GRPC_OP_RECV_INITIAL_METADATA; |
| 525 |
op.data.recv_initial_metadata.recv_initial_metadata = |
| 526 |
&recv_initial_metadata_; |
| 527 |
} |
| 528 |
break; |
| 529 |
case api_fuzzer::BatchOp::kReceiveMessage: |
| 530 |
// Allow only one active pending_recv_message_op to exist. Otherwise if |
| 531 |
// the previous enqueued recv_message_op is not complete by the time |
| 532 |
// we get here, then under certain conditions, enqueing this op will |
| 533 |
// over-write the internal call->receiving_buffer maintained by grpc |
| 534 |
// leading to a memory leak. |
| 535 |
if (call_closed_ || pending_recv_message_op_) { |
| 536 |
*batch_is_ok = false; |
| 537 |
} else { |
| 538 |
op.op = GRPC_OP_RECV_MESSAGE; |
| 539 |
*batch_ops |= 1 << GRPC_OP_RECV_MESSAGE; |
| 540 |
pending_recv_message_op_ = true; |
| 541 |
op.data.recv_message.recv_message = &recv_message_; |
| 542 |
unwinders->push_back([this]() { pending_recv_message_op_ = false; }); |
| 543 |
} |
| 544 |
break; |
| 545 |
case api_fuzzer::BatchOp::kReceiveStatusOnClient: |
| 546 |
op.op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 547 |
op.data.recv_status_on_client.status = &status_; |
| 548 |
op.data.recv_status_on_client.trailing_metadata = |
| 549 |
&recv_trailing_metadata_; |
| 550 |
op.data.recv_status_on_client.status_details = &recv_status_details_; |
| 551 |
*batch_ops |= 1 << GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 552 |
break; |
| 553 |
case api_fuzzer::BatchOp::kReceiveCloseOnServer: |
| 554 |
op.op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 555 |
*batch_ops |= 1 << GRPC_OP_RECV_CLOSE_ON_SERVER; |
| 556 |
op.data.recv_close_on_server.cancelled = &cancelled_; |
| 557 |
break; |
| 558 |
} |
| 559 |
op.reserved = nullptr; |
| 560 |
op.flags = batch_op.flags(); |
| 561 |
return op; |
| 562 |
} |
| 563 |
|
| 564 |
Validator* FinishedBatchValidator(uint8_t has_ops) { |
| 565 |
++pending_ops_; |
| 566 |
auto self = shared_from_this(); |
| 567 |
return MakeValidator([self, has_ops](bool /*success*/) { |
| 568 |
--self->pending_ops_; |
| 569 |
if (has_ops & (1u << GRPC_OP_RECV_MESSAGE)) { |
| 570 |
self->pending_recv_message_op_ = false; |
| 571 |
if (self->recv_message_ != nullptr) { |
| 572 |
grpc_byte_buffer_destroy(self->recv_message_); |
| 573 |
self->recv_message_ = nullptr; |
| 574 |
} |
| 575 |
} |
| 576 |
if ((has_ops & (1u << GRPC_OP_SEND_MESSAGE))) { |
| 577 |
grpc_byte_buffer_destroy(self->send_message_); |
| 578 |
self->send_message_ = nullptr; |
| 579 |
} |
| 580 |
if ((has_ops & (1u << GRPC_OP_RECV_STATUS_ON_CLIENT)) || |
| 581 |
(has_ops & (1u << GRPC_OP_RECV_CLOSE_ON_SERVER))) { |
| 582 |
self->call_closed_ = true; |
| 583 |
} |
| 584 |
}); |
| 585 |
} |
| 586 |
|
| 587 |
Validator* FinishedRequestCall() { |
| 588 |
++pending_ops_; |
| 589 |
auto self = shared_from_this(); |
| 590 |
return MakeValidator([self](bool success) { |
| 591 |
GPR_ASSERT(self->pending_ops_ > 0); |
| 592 |
--self->pending_ops_; |
| 593 |
if (success) { |
| 594 |
GPR_ASSERT(self->call_ != nullptr); |
| 595 |
self->type_ = CallType::SERVER; |
| 596 |
} else { |
| 597 |
self->type_ = CallType::TOMBSTONED; |
| 598 |
} |
| 599 |
}); |
| 600 |
} |
| 601 |
|
| 602 |
private: |
| 603 |
CallType type_; |
| 604 |
grpc_call* call_ = nullptr; |
| 605 |
grpc_byte_buffer* recv_message_ = nullptr; |
| 606 |
grpc_status_code status_; |
| 607 |
grpc_metadata_array recv_initial_metadata_{0, 0, nullptr}; |
| 608 |
grpc_metadata_array recv_trailing_metadata_{0, 0, nullptr}; |
| 609 |
grpc_slice recv_status_details_ = grpc_empty_slice(); |
| 610 |
// set by receive close on server, unset here to trigger |
| 611 |
// msan if misused |
| 612 |
int cancelled_; |
| 613 |
int pending_ops_ = 0; |
| 614 |
bool sent_initial_metadata_ = false; |
| 615 |
bool enqueued_recv_initial_metadata_ = false; |
| 616 |
grpc_call_details call_details_{}; |
| 617 |
grpc_byte_buffer* send_message_ = nullptr; |
| 618 |
bool call_closed_ = false; |
| 619 |
bool pending_recv_message_op_ = false; |
| 620 |
|
| 621 |
std::vector<void*> free_pointers_; |
| 622 |
std::vector<grpc_slice> unref_slices_; |
| 623 |
}; |
| 624 |
|
| 625 |
static std::vector<std::shared_ptr<Call>> g_calls; |
| 626 |
static size_t g_active_call = 0; |
| 627 |
|
| 628 |
static Call* ActiveCall() { |
| 629 |
while (!g_calls.empty()) { |
| 630 |
if (g_active_call >= g_calls.size()) { |
| 631 |
g_active_call = 0; |
| 632 |
} |
| 633 |
if (g_calls[g_active_call] != nullptr && !g_calls[g_active_call]->done()) { |
| 634 |
return g_calls[g_active_call].get(); |
| 635 |
} |
| 636 |
g_calls.erase(g_calls.begin() + g_active_call); |
| 637 |
} |
| 638 |
return nullptr; |
| 639 |
} |
| 640 |
|
| 641 |
Call::~Call() { |
| 642 |
if (call_ != nullptr) { |
| 643 |
grpc_call_unref(call_); |
| 644 |
} |
| 645 |
grpc_slice_unref(recv_status_details_); |
| 646 |
grpc_call_details_destroy(&call_details_); |
| 647 |
|
| 648 |
for (auto* p : free_pointers_) { |
| 649 |
gpr_free(p); |
| 650 |
} |
| 651 |
for (auto s : unref_slices_) { |
| 652 |
grpc_slice_unref(s); |
| 653 |
} |
| 654 |
|
| 655 |
if (recv_message_ != nullptr) { |
| 656 |
grpc_byte_buffer_destroy(recv_message_); |
| 657 |
recv_message_ = nullptr; |
| 658 |
} |
| 659 |
|
| 660 |
grpc_metadata_array_destroy(&recv_initial_metadata_); |
| 661 |
grpc_metadata_array_destroy(&recv_trailing_metadata_); |
| 662 |
} |
| 663 |
|
| 664 |
template <typename ChannelArgContainer> |
| 665 |
grpc_channel_args* ReadArgs(const ChannelArgContainer& args) { |
| 666 |
grpc_channel_args* res = |
| 667 |
static_cast<grpc_channel_args*>(gpr_malloc(sizeof(grpc_channel_args))); |
| 668 |
res->args = |
| 669 |
static_cast<grpc_arg*>(gpr_malloc(sizeof(grpc_arg) * args.size())); |
| 670 |
int j = 0; |
| 671 |
for (int i = 0; i < args.size(); i++) { |
| 672 |
switch (args[i].value_case()) { |
| 673 |
case api_fuzzer::ChannelArg::kStr: |
| 674 |
res->args[j].type = GRPC_ARG_STRING; |
| 675 |
res->args[j].value.string = gpr_strdup(args[i].str().c_str()); |
| 676 |
break; |
| 677 |
case api_fuzzer::ChannelArg::kI: |
| 678 |
res->args[j].type = GRPC_ARG_INTEGER; |
| 679 |
res->args[j].value.integer = args[i].i(); |
| 680 |
break; |
| 681 |
case api_fuzzer::ChannelArg::kResourceQuota: |
| 682 |
if (args[i].key() != GRPC_ARG_RESOURCE_QUOTA) continue; |
| 683 |
grpc_resource_quota_ref(g_resource_quota); |
| 684 |
res->args[j].type = GRPC_ARG_POINTER; |
| 685 |
res->args[j].value.pointer.p = g_resource_quota; |
| 686 |
res->args[j].value.pointer.vtable = grpc_resource_quota_arg_vtable(); |
| 687 |
break; |
| 688 |
case api_fuzzer::ChannelArg::VALUE_NOT_SET: |
| 689 |
res->args[j].type = GRPC_ARG_INTEGER; |
| 690 |
res->args[j].value.integer = 0; |
| 691 |
break; |
| 692 |
} |
| 693 |
res->args[j].key = gpr_strdup(args[i].key().c_str()); |
| 694 |
++j; |
| 695 |
} |
| 696 |
res->num_args = j; |
| 697 |
return res; |
| 698 |
} |
| 699 |
|
| 700 |
static const char* ReadCredArtifact( |
| 701 |
const api_fuzzer::CredArtifact& artifact, |
| 702 |
std::initializer_list<const char*> builtins) { |
| 703 |
switch (artifact.type_case()) { |
| 704 |
case api_fuzzer::CredArtifact::kCustom: |
| 705 |
return artifact.custom().c_str(); |
| 706 |
case api_fuzzer::CredArtifact::kBuiltin: |
| 707 |
if (artifact.builtin() < 0) return nullptr; |
| 708 |
if (artifact.builtin() < static_cast<int>(builtins.size())) { |
| 709 |
return *(builtins.begin() + artifact.builtin()); |
| 710 |
} |
| 711 |
return nullptr; |
| 712 |
case api_fuzzer::CredArtifact::TYPE_NOT_SET: |
| 713 |
return nullptr; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
static grpc_channel_credentials* ReadSslChannelCreds( |
| 718 |
const api_fuzzer::SslChannelCreds& creds) { |
| 719 |
const char* root_certs = |
| 720 |
creds.has_root_certs() |
| 721 |
? ReadCredArtifact(creds.root_certs(), {test_root_cert}) |
| 722 |
: nullptr; |
| 723 |
const char* private_key = |
| 724 |
creds.has_private_key() |
| 725 |
? ReadCredArtifact(creds.private_key(), |
| 726 |
{test_server1_key, test_self_signed_client_key, |
| 727 |
test_signed_client_key}) |
| 728 |
: nullptr; |
| 729 |
const char* certs = |
| 730 |
creds.has_certs() |
| 731 |
? ReadCredArtifact(creds.certs(), |
| 732 |
{test_server1_cert, test_self_signed_client_cert, |
| 733 |
test_signed_client_cert}) |
| 734 |
: nullptr; |
| 735 |
grpc_ssl_pem_key_cert_pair key_cert_pair = {private_key, certs}; |
| 736 |
return grpc_ssl_credentials_create( |
| 737 |
root_certs, |
| 738 |
private_key != nullptr && certs != nullptr ? &key_cert_pair : nullptr, |
| 739 |
nullptr, nullptr); |
| 740 |
} |
| 741 |
|
| 742 |
static grpc_call_credentials* ReadCallCreds( |
| 743 |
const api_fuzzer::CallCreds& creds) { |
| 744 |
switch (creds.type_case()) { |
| 745 |
case api_fuzzer::CallCreds::TYPE_NOT_SET: |
| 746 |
return nullptr; |
| 747 |
case api_fuzzer::CallCreds::kNull: |
| 748 |
return nullptr; |
| 749 |
case api_fuzzer::CallCreds::kCompositeCallCreds: { |
| 750 |
grpc_call_credentials* out = nullptr; |
| 751 |
for (const auto& child_creds : |
| 752 |
creds.composite_call_creds().call_creds()) { |
| 753 |
grpc_call_credentials* child = ReadCallCreds(child_creds); |
| 754 |
if (child != nullptr) { |
| 755 |
if (out == nullptr) { |
| 756 |
out = child; |
| 757 |
} else { |
| 758 |
auto* composed = |
| 759 |
grpc_composite_call_credentials_create(out, child, nullptr); |
| 760 |
grpc_call_credentials_release(child); |
| 761 |
grpc_call_credentials_release(out); |
| 762 |
out = composed; |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
return out; |
| 767 |
} |
| 768 |
case api_fuzzer::CallCreds::kAccessToken: |
| 769 |
return grpc_access_token_credentials_create(creds.access_token().c_str(), |
| 770 |
nullptr); |
| 771 |
case api_fuzzer::CallCreds::kIam: |
| 772 |
return grpc_google_iam_credentials_create( |
| 773 |
creds.iam().auth_token().c_str(), creds.iam().auth_selector().c_str(), |
| 774 |
nullptr); |
| 775 |
/* TODO(ctiller): more cred types here */ |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
static grpc_channel_credentials* ReadChannelCreds( |
| 780 |
const api_fuzzer::ChannelCreds& creds) { |
| 781 |
switch (creds.type_case()) { |
| 782 |
case api_fuzzer::ChannelCreds::TYPE_NOT_SET: |
| 783 |
return nullptr; |
| 784 |
case api_fuzzer::ChannelCreds::kSslChannelCreds: |
| 785 |
return ReadSslChannelCreds(creds.ssl_channel_creds()); |
| 786 |
case api_fuzzer::ChannelCreds::kCompositeChannelCreds: { |
| 787 |
const auto& comp = creds.composite_channel_creds(); |
| 788 |
grpc_channel_credentials* c1 = |
| 789 |
comp.has_channel_creds() ? ReadChannelCreds(comp.channel_creds()) |
| 790 |
: nullptr; |
| 791 |
grpc_call_credentials* c2 = |
| 792 |
comp.has_call_creds() ? ReadCallCreds(comp.call_creds()) : nullptr; |
| 793 |
if (c1 != nullptr && c2 != nullptr) { |
| 794 |
grpc_channel_credentials* out = |
| 795 |
grpc_composite_channel_credentials_create(c1, c2, nullptr); |
| 796 |
grpc_channel_credentials_release(c1); |
| 797 |
grpc_call_credentials_release(c2); |
| 798 |
return out; |
| 799 |
} else if (c1 != nullptr) { |
| 800 |
return c1; |
| 801 |
} else if (c2 != nullptr) { |
| 802 |
grpc_call_credentials_release(c2); |
| 803 |
return nullptr; |
| 804 |
} else { |
| 805 |
return nullptr; |
| 806 |
} |
| 807 |
GPR_UNREACHABLE_CODE(return nullptr); |
| 808 |
} |
| 809 |
case api_fuzzer::ChannelCreds::kNull: |
| 810 |
return nullptr; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
DEFINE_PROTO_FUZZER(const api_fuzzer::Msg& msg) { |
| 815 |
if (squelch && !grpc_core::GetEnv("GRPC_TRACE_FUZZER").has_value()) { |
| 816 |
gpr_set_log_function(dont_log); |
| 817 |
} |
| 818 |
grpc_set_tcp_client_impl(&fuzz_tcp_client_vtable); |
| 819 |
grpc_event_engine::experimental::SetEventEngineFactory( |
| 820 |
[actions = msg.event_engine_actions()]() { |
| 821 |
return std::make_unique<FuzzingEventEngine>( |
| 822 |
FuzzingEventEngine::Options(), actions); |
| 823 |
}); |
| 824 |
auto engine = |
| 825 |
std::dynamic_pointer_cast<FuzzingEventEngine>(GetDefaultEventEngine()); |
| 826 |
FuzzingEventEngine::SetGlobalNowImplEngine(engine.get()); |
| 827 |
grpc_init(); |
| 828 |
grpc_timer_manager_set_threading(false); |
| 829 |
{ |
| 830 |
grpc_core::ExecCtx exec_ctx; |
| 831 |
grpc_core::Executor::SetThreadingAll(false); |
| 832 |
} |
| 833 |
grpc_core::ResetDNSResolver( |
| 834 |
std::make_unique<FuzzerDNSResolver>(engine.get())); |
| 835 |
grpc_dns_lookup_hostname_ares = my_dns_lookup_ares; |
| 836 |
grpc_cancel_ares_request = my_cancel_ares_request; |
| 837 |
|
| 838 |
GPR_ASSERT(g_channel == nullptr); |
| 839 |
GPR_ASSERT(g_server == nullptr); |
| 840 |
|
| 841 |
bool server_shutdown = false; |
| 842 |
int pending_server_shutdowns = 0; |
| 843 |
int pending_channel_watches = 0; |
| 844 |
int pending_pings = 0; |
| 845 |
|
| 846 |
g_resource_quota = grpc_resource_quota_create("api_fuzzer"); |
| 847 |
|
| 848 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 849 |
|
| 850 |
int action_index = 0; |
| 851 |
auto no_more_actions = [&]() { action_index = msg.actions_size(); }; |
| 852 |
auto poll_cq = [&]() -> bool { |
| 853 |
grpc_event ev = grpc_completion_queue_next( |
| 854 |
cq, gpr_inf_past(GPR_CLOCK_REALTIME), nullptr); |
| 855 |
switch (ev.type) { |
| 856 |
case GRPC_OP_COMPLETE: { |
| 857 |
static_cast<Validator*>(ev.tag)->Run(ev.success); |
| 858 |
break; |
| 859 |
} |
| 860 |
case GRPC_QUEUE_TIMEOUT: |
| 861 |
break; |
| 862 |
case GRPC_QUEUE_SHUTDOWN: |
| 863 |
return true; |
| 864 |
} |
| 865 |
return false; |
| 866 |
}; |
| 867 |
|
| 868 |
while (action_index < msg.actions_size() || g_channel != nullptr || |
| 869 |
g_server != nullptr || pending_channel_watches > 0 || |
| 870 |
pending_pings > 0 || ActiveCall() != nullptr) { |
| 871 |
engine->Tick(); |
| 872 |
|
| 873 |
if (action_index == msg.actions_size()) { |
| 874 |
engine->FuzzingDone(); |
| 875 |
if (g_channel != nullptr) { |
| 876 |
grpc_channel_destroy(g_channel); |
| 877 |
g_channel = nullptr; |
| 878 |
} |
| 879 |
if (g_server != nullptr) { |
| 880 |
if (!server_shutdown) { |
| 881 |
grpc_server_shutdown_and_notify( |
| 882 |
g_server, cq, |
| 883 |
AssertSuccessAndDecrement(&pending_server_shutdowns)); |
| 884 |
server_shutdown = true; |
| 885 |
pending_server_shutdowns++; |
| 886 |
} else if (pending_server_shutdowns == 0) { |
| 887 |
grpc_server_destroy(g_server); |
| 888 |
g_server = nullptr; |
| 889 |
} |
| 890 |
} |
| 891 |
for (auto& call : g_calls) { |
| 892 |
if (call == nullptr) continue; |
| 893 |
if (call->type() == CallType::PENDING_SERVER) continue; |
| 894 |
call->Shutdown(); |
| 895 |
} |
| 896 |
|
| 897 |
grpc_timer_manager_tick(); |
| 898 |
GPR_ASSERT(!poll_cq()); |
| 899 |
continue; |
| 900 |
} |
| 901 |
|
| 902 |
grpc_timer_manager_tick(); |
| 903 |
|
| 904 |
if (g_channel_force_delete.exchange(false) && g_channel) { |
| 905 |
grpc_channel_destroy(g_channel); |
| 906 |
g_channel = nullptr; |
| 907 |
g_channel_actions.clear(); |
| 908 |
} |
| 909 |
|
| 910 |
const api_fuzzer::Action& action = msg.actions(action_index); |
| 911 |
action_index++; |
| 912 |
switch (action.type_case()) { |
| 913 |
case api_fuzzer::Action::TYPE_NOT_SET: |
| 914 |
no_more_actions(); |
| 915 |
break; |
| 916 |
// tickle completion queue |
| 917 |
case api_fuzzer::Action::kPollCq: { |
| 918 |
GPR_ASSERT(!poll_cq()); |
| 919 |
break; |
| 920 |
} |
| 921 |
// create an insecure channel |
| 922 |
case api_fuzzer::Action::kCreateChannel: { |
| 923 |
if (!action.create_channel().channel_actions_size() || |
| 924 |
g_channel != nullptr) { |
| 925 |
no_more_actions(); |
| 926 |
} else { |
| 927 |
grpc_channel_args* args = |
| 928 |
ReadArgs(action.create_channel().channel_args()); |
| 929 |
grpc_channel_credentials* creds = |
| 930 |
action.create_channel().has_channel_creds() |
| 931 |
? ReadChannelCreds(action.create_channel().channel_creds()) |
| 932 |
: grpc_insecure_credentials_create(); |
| 933 |
g_channel = grpc_channel_create( |
| 934 |
action.create_channel().target().c_str(), creds, args); |
| 935 |
grpc_channel_credentials_release(creds); |
| 936 |
g_channel_actions.clear(); |
| 937 |
for (int i = 0; i < action.create_channel().channel_actions_size(); |
| 938 |
i++) { |
| 939 |
const api_fuzzer::ChannelAction& channel_action = |
| 940 |
action.create_channel().channel_actions(i); |
| 941 |
g_channel_actions.push_back({ |
| 942 |
std::min(channel_action.wait_ms(), kMaxWaitMs), |
| 943 |
std::min(channel_action.add_n_bytes_writable(), |
| 944 |
kMaxAddNWritableBytes), |
| 945 |
std::min(channel_action.add_n_bytes_readable(), |
| 946 |
kMaxAddNReadableBytes), |
| 947 |
}); |
| 948 |
} |
| 949 |
GPR_ASSERT(g_channel != nullptr); |
| 950 |
g_channel_force_delete = false; |
| 951 |
{ |
| 952 |
grpc_core::ExecCtx exec_ctx; |
| 953 |
grpc_channel_args_destroy(args); |
| 954 |
} |
| 955 |
} |
| 956 |
break; |
| 957 |
} |
| 958 |
// destroy a channel |
| 959 |
case api_fuzzer::Action::kCloseChannel: { |
| 960 |
if (g_channel != nullptr) { |
| 961 |
grpc_channel_destroy(g_channel); |
| 962 |
g_channel = nullptr; |
| 963 |
} else { |
| 964 |
no_more_actions(); |
| 965 |
} |
| 966 |
break; |
| 967 |
} |
| 968 |
// bring up a server |
| 969 |
case api_fuzzer::Action::kCreateServer: { |
| 970 |
if (g_server == nullptr) { |
| 971 |
grpc_channel_args* args = |
| 972 |
ReadArgs(action.create_server().channel_args()); |
| 973 |
g_server = grpc_server_create(args, nullptr); |
| 974 |
GPR_ASSERT(g_server != nullptr); |
| 975 |
{ |
| 976 |
grpc_core::ExecCtx exec_ctx; |
| 977 |
grpc_channel_args_destroy(args); |
| 978 |
} |
| 979 |
grpc_server_register_completion_queue(g_server, cq, nullptr); |
| 980 |
grpc_server_start(g_server); |
| 981 |
server_shutdown = false; |
| 982 |
GPR_ASSERT(pending_server_shutdowns == 0); |
| 983 |
} else { |
| 984 |
no_more_actions(); |
| 985 |
} |
| 986 |
break; |
| 987 |
} |
| 988 |
// begin server shutdown |
| 989 |
case api_fuzzer::Action::kShutdownServer: { |
| 990 |
if (g_server != nullptr) { |
| 991 |
grpc_server_shutdown_and_notify( |
| 992 |
g_server, cq, |
| 993 |
AssertSuccessAndDecrement(&pending_server_shutdowns)); |
| 994 |
pending_server_shutdowns++; |
| 995 |
server_shutdown = true; |
| 996 |
} else { |
| 997 |
no_more_actions(); |
| 998 |
} |
| 999 |
break; |
| 1000 |
} |
| 1001 |
// cancel all calls if shutdown |
| 1002 |
case api_fuzzer::Action::kCancelAllCallsIfShutdown: { |
| 1003 |
if (g_server != nullptr && server_shutdown) { |
| 1004 |
grpc_server_cancel_all_calls(g_server); |
| 1005 |
} else { |
| 1006 |
no_more_actions(); |
| 1007 |
} |
| 1008 |
break; |
| 1009 |
} |
| 1010 |
// destroy server |
| 1011 |
case api_fuzzer::Action::kDestroyServerIfReady: { |
| 1012 |
if (g_server != nullptr && server_shutdown && |
| 1013 |
pending_server_shutdowns == 0) { |
| 1014 |
grpc_server_destroy(g_server); |
| 1015 |
g_server = nullptr; |
| 1016 |
} else { |
| 1017 |
no_more_actions(); |
| 1018 |
} |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
// check connectivity |
| 1022 |
case api_fuzzer::Action::kCheckConnectivity: { |
| 1023 |
if (g_channel != nullptr) { |
| 1024 |
grpc_channel_check_connectivity_state(g_channel, |
| 1025 |
action.check_connectivity()); |
| 1026 |
} else { |
| 1027 |
no_more_actions(); |
| 1028 |
} |
| 1029 |
break; |
| 1030 |
} |
| 1031 |
// watch connectivity |
| 1032 |
case api_fuzzer::Action::kWatchConnectivity: { |
| 1033 |
if (g_channel != nullptr) { |
| 1034 |
grpc_connectivity_state st = |
| 1035 |
grpc_channel_check_connectivity_state(g_channel, 0); |
| 1036 |
if (st != GRPC_CHANNEL_SHUTDOWN) { |
| 1037 |
gpr_timespec deadline = |
| 1038 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 1039 |
gpr_time_from_micros(action.watch_connectivity(), |
| 1040 |
GPR_TIMESPAN)); |
| 1041 |
grpc_channel_watch_connectivity_state( |
| 1042 |
g_channel, st, deadline, cq, |
| 1043 |
ValidateConnectivityWatch(deadline, &pending_channel_watches)); |
| 1044 |
pending_channel_watches++; |
| 1045 |
} |
| 1046 |
} else { |
| 1047 |
no_more_actions(); |
| 1048 |
} |
| 1049 |
break; |
| 1050 |
} |
| 1051 |
// create a call |
| 1052 |
case api_fuzzer::Action::kCreateCall: { |
| 1053 |
bool ok = true; |
| 1054 |
if (g_channel == nullptr) ok = false; |
| 1055 |
// If the active call is a server call, then use it as the parent call |
| 1056 |
// to exercise the propagation logic. |
| 1057 |
Call* parent_call = ActiveCall(); |
| 1058 |
if (parent_call != nullptr && parent_call->type() != CallType::SERVER) { |
| 1059 |
parent_call = nullptr; |
| 1060 |
} |
| 1061 |
g_calls.emplace_back(new Call(CallType::CLIENT)); |
| 1062 |
grpc_slice method = |
| 1063 |
g_calls.back()->ReadSlice(action.create_call().method()); |
| 1064 |
if (GRPC_SLICE_LENGTH(method) == 0) { |
| 1065 |
ok = false; |
| 1066 |
} |
| 1067 |
grpc_slice host = |
| 1068 |
g_calls.back()->ReadSlice(action.create_call().host()); |
| 1069 |
gpr_timespec deadline = gpr_time_add( |
| 1070 |
gpr_now(GPR_CLOCK_REALTIME), |
| 1071 |
gpr_time_from_micros(action.create_call().timeout(), GPR_TIMESPAN)); |
| 1072 |
|
| 1073 |
if (ok) { |
| 1074 |
g_calls.back()->SetCall(grpc_channel_create_call( |
| 1075 |
g_channel, parent_call == nullptr ? nullptr : parent_call->call(), |
| 1076 |
action.create_call().propagation_mask(), cq, method, &host, |
| 1077 |
deadline, nullptr)); |
| 1078 |
} else { |
| 1079 |
g_calls.pop_back(); |
| 1080 |
no_more_actions(); |
| 1081 |
} |
| 1082 |
break; |
| 1083 |
} |
| 1084 |
// switch the 'current' call |
| 1085 |
case api_fuzzer::Action::kChangeActiveCall: { |
| 1086 |
g_active_call++; |
| 1087 |
ActiveCall(); |
| 1088 |
break; |
| 1089 |
} |
| 1090 |
// queue some ops on a call |
| 1091 |
case api_fuzzer::Action::kQueueBatch: { |
| 1092 |
auto* active_call = ActiveCall(); |
| 1093 |
if (active_call == nullptr || |
| 1094 |
active_call->type() == CallType::PENDING_SERVER || |
| 1095 |
active_call->call() == nullptr) { |
| 1096 |
no_more_actions(); |
| 1097 |
break; |
| 1098 |
} |
| 1099 |
const auto& batch = action.queue_batch().operations(); |
| 1100 |
if (batch.size() > 6) { |
| 1101 |
no_more_actions(); |
| 1102 |
break; |
| 1103 |
} |
| 1104 |
std::vector<grpc_op> ops; |
| 1105 |
bool ok = true; |
| 1106 |
uint8_t has_ops = 0; |
| 1107 |
std::vector<std::function<void()>> unwinders; |
| 1108 |
for (const auto& batch_op : batch) { |
| 1109 |
auto op = active_call->ReadOp(batch_op, &ok, &has_ops, &unwinders); |
| 1110 |
if (!op.has_value()) continue; |
| 1111 |
ops.push_back(*op); |
| 1112 |
} |
| 1113 |
|
| 1114 |
if (g_channel == nullptr) ok = false; |
| 1115 |
if (ok) { |
| 1116 |
auto* v = active_call->FinishedBatchValidator(has_ops); |
| 1117 |
grpc_call_error error = grpc_call_start_batch( |
| 1118 |
active_call->call(), ops.data(), ops.size(), v, nullptr); |
| 1119 |
if (error != GRPC_CALL_OK) { |
| 1120 |
v->Run(false); |
| 1121 |
} |
| 1122 |
} else { |
| 1123 |
no_more_actions(); |
| 1124 |
for (auto& unwind : unwinders) { |
| 1125 |
unwind(); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
break; |
| 1129 |
} |
| 1130 |
// cancel current call |
| 1131 |
case api_fuzzer::Action::kCancelCall: { |
| 1132 |
auto* active_call = ActiveCall(); |
| 1133 |
if (active_call != nullptr && active_call->call() != nullptr) { |
| 1134 |
grpc_call_cancel(active_call->call(), nullptr); |
| 1135 |
} else { |
| 1136 |
no_more_actions(); |
| 1137 |
} |
| 1138 |
break; |
| 1139 |
} |
| 1140 |
// get a calls peer |
| 1141 |
case api_fuzzer::Action::kGetPeer: { |
| 1142 |
auto* active_call = ActiveCall(); |
| 1143 |
if (active_call != nullptr && active_call->call() != nullptr) { |
| 1144 |
free_non_null(grpc_call_get_peer(active_call->call())); |
| 1145 |
} else { |
| 1146 |
no_more_actions(); |
| 1147 |
} |
| 1148 |
break; |
| 1149 |
} |
| 1150 |
// get a channels target |
| 1151 |
case api_fuzzer::Action::kGetTarget: { |
| 1152 |
if (g_channel != nullptr) { |
| 1153 |
free_non_null(grpc_channel_get_target(g_channel)); |
| 1154 |
} else { |
| 1155 |
no_more_actions(); |
| 1156 |
} |
| 1157 |
break; |
| 1158 |
} |
| 1159 |
// send a ping on a channel |
| 1160 |
case api_fuzzer::Action::kPing: { |
| 1161 |
if (g_channel != nullptr) { |
| 1162 |
pending_pings++; |
| 1163 |
grpc_channel_ping(g_channel, cq, Decrement(&pending_pings), nullptr); |
| 1164 |
} else { |
| 1165 |
no_more_actions(); |
| 1166 |
} |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
// enable a tracer |
| 1170 |
case api_fuzzer::Action::kEnableTracer: { |
| 1171 |
grpc_tracer_set_enabled(action.enable_tracer().c_str(), 1); |
| 1172 |
break; |
| 1173 |
} |
| 1174 |
// disable a tracer |
| 1175 |
case api_fuzzer::Action::kDisableTracer: { |
| 1176 |
grpc_tracer_set_enabled(action.disable_tracer().c_str(), 0); |
| 1177 |
break; |
| 1178 |
} |
| 1179 |
// request a server call |
| 1180 |
case api_fuzzer::Action::kRequestCall: { |
| 1181 |
if (g_server == nullptr) { |
| 1182 |
no_more_actions(); |
| 1183 |
break; |
| 1184 |
} |
| 1185 |
g_calls.emplace_back(new Call(CallType::PENDING_SERVER)); |
| 1186 |
g_calls.back()->RequestCall(g_server, cq); |
| 1187 |
break; |
| 1188 |
} |
| 1189 |
// destroy a call |
| 1190 |
case api_fuzzer::Action::kDestroyCall: { |
| 1191 |
auto* active_call = ActiveCall(); |
| 1192 |
if (active_call != nullptr && |
| 1193 |
active_call->type() != CallType::PENDING_SERVER && |
| 1194 |
active_call->call() != nullptr) { |
| 1195 |
g_calls[g_active_call]->Shutdown(); |
| 1196 |
} else { |
| 1197 |
no_more_actions(); |
| 1198 |
} |
| 1199 |
break; |
| 1200 |
} |
| 1201 |
// resize the buffer pool |
| 1202 |
case api_fuzzer::Action::kResizeResourceQuota: { |
| 1203 |
grpc_resource_quota_resize(g_resource_quota, |
| 1204 |
action.resize_resource_quota()); |
| 1205 |
break; |
| 1206 |
} |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
GPR_ASSERT(g_channel == nullptr); |
| 1211 |
GPR_ASSERT(g_server == nullptr); |
| 1212 |
GPR_ASSERT(ActiveCall() == nullptr); |
| 1213 |
GPR_ASSERT(g_calls.empty()); |
| 1214 |
|
| 1215 |
grpc_completion_queue_shutdown(cq); |
| 1216 |
GPR_ASSERT(poll_cq()); |
| 1217 |
grpc_completion_queue_destroy(cq); |
| 1218 |
|
| 1219 |
grpc_resource_quota_unref(g_resource_quota); |
| 1220 |
grpc_shutdown_blocking(); |
| 1221 |
FuzzingEventEngine::UnsetGlobalNowImplEngine(engine.get()); |
| 1222 |
} |