diff --git a/api/wasm/cpp/proxy_wasm_api.h b/api/wasm/cpp/proxy_wasm_api.h index fcfd0282eb..4462a3fa05 100644 --- a/api/wasm/cpp/proxy_wasm_api.h +++ b/api/wasm/cpp/proxy_wasm_api.h @@ -303,7 +303,7 @@ class RootContext : public ContextBase { // Low level HTTP/gRPC interface. virtual void onHttpCallResponse(uint32_t token, uint32_t headers, size_t body_size, uint32_t trailers); - virtual void onGrpcCreateInitialMetadata(uint32_t token, uint32_t headers); + virtual void onGrpcCreateInitialMetadata(uint32_t headers); virtual void onGrpcReceiveInitialMetadata(uint32_t token, uint32_t headers); virtual void onGrpcReceiveTrailingMetadata(uint32_t token, uint32_t trailers); virtual void onGrpcReceive(uint32_t token, size_t body_size); @@ -354,6 +354,11 @@ class RootContext : public ContextBase { std::unordered_map simple_grpc_calls_; std::unordered_map> grpc_calls_; std::unordered_map> grpc_streams_; + + // Handles initial metadata creation callback, which is triggered inline when initializing the call, by which time token is not yet available in module and thus the handler cannot be added into grpc_calls_ and grpc_streams_; + // These two pointers should only be used in onCreateInitialMetadata. + std::unique_ptr cur_grpc_call_handler_; + std::unique_ptr cur_grpc_stream_handler_; }; RootContext* getRoot(StringView root_id); @@ -1247,18 +1252,16 @@ inline void GrpcStreamHandlerBase::send(StringView message, bool end_of_stream) } } -inline void RootContext::onGrpcCreateInitialMetadata(uint32_t token, uint32_t headers) { +inline void RootContext::onGrpcCreateInitialMetadata(uint32_t headers) { { - auto it = grpc_calls_.find(token); - if (it != grpc_calls_.end()) { - it->second->onCreateInitialMetadata(headers); + if (cur_grpc_call_handler_ != nullptr) { + cur_grpc_call_handler_->onCreateInitialMetadata(headers); return; } } { - auto it = grpc_streams_.find(token); - if (it != grpc_streams_.end()) { - it->second->onCreateInitialMetadata(headers); + if (cur_grpc_stream_handler_ != nullptr) { + cur_grpc_stream_handler_->onCreateInitialMetadata(headers); return; } } @@ -1357,12 +1360,14 @@ inline WasmResult RootContext::grpcCallHandler(StringView service, StringView se uint32_t timeout_milliseconds, std::unique_ptr handler) { uint32_t token = 0; + cur_grpc_call_handler_ = std::move(handler); auto result = grpcCall(service, service_name, method_name, request, timeout_milliseconds, &token); if (result == WasmResult::Ok) { - handler->token_ = token; - handler->context_ = this; - grpc_calls_[token] = std::move(handler); + cur_grpc_call_handler_->token_ = token; + cur_grpc_call_handler_->context_ = this; + grpc_calls_[token] = std::move(cur_grpc_call_handler_); } + cur_grpc_call_handler_ = nullptr; return result; } @@ -1370,12 +1375,14 @@ inline WasmResult RootContext::grpcStreamHandler(StringView service, StringView StringView method_name, std::unique_ptr handler) { uint32_t token = 0; + cur_grpc_stream_handler_ = std::move(handler); auto result = grpcStream(service, service_name, method_name, &token); if (result == WasmResult::Ok) { - handler->token_ = token; - handler->context_ = this; - grpc_streams_[token] = std::move(handler); + cur_grpc_stream_handler_->token_ = token; + cur_grpc_stream_handler_->context_ = this; + grpc_streams_[token] = std::move(cur_grpc_stream_handler_); } + cur_grpc_stream_handler_ = nullptr; return result; } diff --git a/api/wasm/cpp/proxy_wasm_externs.h b/api/wasm/cpp/proxy_wasm_externs.h index dec6dfade8..68039aa5c5 100644 --- a/api/wasm/cpp/proxy_wasm_externs.h +++ b/api/wasm/cpp/proxy_wasm_externs.h @@ -155,8 +155,7 @@ extern "C" FilterMetadataStatus proxy_on_response_metadata(uint32_t context_id, // HTTP/gRPC. extern "C" void proxy_on_http_call_response(uint32_t context_id, uint32_t token, uint32_t headers, uint32_t body_size, uint32_t trailers); -extern "C" void proxy_on_grpc_create_initial_metadata(uint32_t context_id, uint32_t token, - uint32_t headers); +extern "C" void proxy_on_grpc_create_initial_metadata(uint32_t context_id, uint32_t headers); extern "C" void proxy_on_grpc_receive_initial_metadata(uint32_t context_id, uint32_t token, uint32_t headers); extern "C" void proxy_on_grpc_trailing_metadata(uint32_t context_id, uint32_t token, diff --git a/api/wasm/cpp/proxy_wasm_intrinsics.cc b/api/wasm/cpp/proxy_wasm_intrinsics.cc index df809bc850..a14b0d0d88 100644 --- a/api/wasm/cpp/proxy_wasm_intrinsics.cc +++ b/api/wasm/cpp/proxy_wasm_intrinsics.cc @@ -226,8 +226,8 @@ extern "C" PROXY_WASM_KEEPALIVE void proxy_on_http_call_response(uint32_t contex } extern "C" PROXY_WASM_KEEPALIVE void -proxy_on_grpc_create_initial_metadata(uint32_t context_id, uint32_t token, uint32_t headers) { - getRootContext(context_id)->onGrpcCreateInitialMetadata(token, headers); +proxy_on_grpc_create_initial_metadata(uint32_t context_id, uint32_t headers) { + getRootContext(context_id)->onGrpcCreateInitialMetadata(headers); } extern "C" PROXY_WASM_KEEPALIVE void diff --git a/examples/wasm/envoy_filter_http_wasm_example.wasm b/examples/wasm/envoy_filter_http_wasm_example.wasm index 588bc8fbd2..782a0dcc57 100644 Binary files a/examples/wasm/envoy_filter_http_wasm_example.wasm and b/examples/wasm/envoy_filter_http_wasm_example.wasm differ diff --git a/source/extensions/common/wasm/context.cc b/source/extensions/common/wasm/context.cc index fb4933d39f..97dfd42a9f 100644 --- a/source/extensions/common/wasm/context.cc +++ b/source/extensions/common/wasm/context.cc @@ -1278,13 +1278,12 @@ void Context::onQueueReady(uint32_t token) { } } -void Context::onGrpcCreateInitialMetadata(uint32_t token, Http::HeaderMap& metadata) { +void Context::onGrpcCreateInitialMetadata(Http::HeaderMap& metadata) { if (!wasm_->on_grpc_create_initial_metadata_) { return; } grpc_create_initial_metadata_ = &metadata; - wasm_->on_grpc_create_initial_metadata_(this, id_, token, - headerSize(grpc_create_initial_metadata_)); + wasm_->on_grpc_create_initial_metadata_(this, id_, headerSize(grpc_create_initial_metadata_)); grpc_create_initial_metadata_ = nullptr; } diff --git a/source/extensions/common/wasm/context.h b/source/extensions/common/wasm/context.h index 07082b9fba..055d7b9eef 100644 --- a/source/extensions/common/wasm/context.h +++ b/source/extensions/common/wasm/context.h @@ -348,7 +348,7 @@ class Context : public Logger::Loggable, struct GrpcCallClientHandler : public Grpc::RawAsyncRequestCallbacks { // Grpc::AsyncRequestCallbacks void onCreateInitialMetadata(Http::HeaderMap& metadata) override { - context_->onGrpcCreateInitialMetadata(token_, metadata); + context_->onGrpcCreateInitialMetadata(metadata); } void onSuccessRaw(Buffer::InstancePtr&& response, Tracing::Span& /* span */) override { context_->onGrpcReceive(token_, std::move(response)); @@ -367,7 +367,7 @@ class Context : public Logger::Loggable, struct GrpcStreamClientHandler : public Grpc::RawAsyncStreamCallbacks { // Grpc::AsyncStreamCallbacks void onCreateInitialMetadata(Http::HeaderMap& metadata) override { - context_->onGrpcCreateInitialMetadata(token_, metadata); + context_->onGrpcCreateInitialMetadata(metadata); } void onReceiveInitialMetadata(Http::HeaderMapPtr&& metadata) override { context_->onGrpcReceiveInitialMetadata(token_, std::move(metadata)); @@ -395,8 +395,7 @@ class Context : public Logger::Loggable, void onHttpCallSuccess(uint32_t token, Envoy::Http::MessagePtr& response); void onHttpCallFailure(uint32_t token, Http::AsyncClient::FailureReason reason); - virtual void onGrpcCreateInitialMetadata(uint32_t token, - Http::HeaderMap& metadata); // For both Call and Stream. + virtual void onGrpcCreateInitialMetadata(Http::HeaderMap& metadata); // For both Call and Stream. virtual void onGrpcReceive(uint32_t token, Buffer::InstancePtr response); // Call (implies OK close) and Stream. virtual void onGrpcClose(uint32_t token, const Grpc::Status::GrpcStatus& status, diff --git a/source/extensions/common/wasm/null/null_plugin.cc b/source/extensions/common/wasm/null/null_plugin.cc index d8cfd1104d..b43c1ccbf5 100644 --- a/source/extensions/common/wasm/null/null_plugin.cc +++ b/source/extensions/common/wasm/null/null_plugin.cc @@ -83,6 +83,11 @@ void NullPlugin::getFunction(absl::string_view function_name, WasmCallVoid<2>* f SaveRestoreContext saved_context(context); plugin->onQueueReady(context_id.u64_, token.u64_); }; + } else if (function_name == "proxy_on_grpc_create_initial_metadata") { + *f = [plugin](Common::Wasm::Context* context, Word context_id, Word headers) { + SaveRestoreContext saved_context(context); + plugin->onGrpcCreateInitialMetadata(context_id.u64_, headers.u64_); + }; } else { throw WasmVmException(fmt::format("Missing getFunction for: {}", function_name)); } @@ -100,11 +105,6 @@ void NullPlugin::getFunction(absl::string_view function_name, WasmCallVoid<3>* f SaveRestoreContext saved_context(context); plugin->onGrpcReceive(context_id.u64_, token.u64_, body_size.u64_); }; - } else if (function_name == "proxy_on_grpc_create_initial_metadata") { - *f = [plugin](Common::Wasm::Context* context, Word context_id, Word token, Word headers) { - SaveRestoreContext saved_context(context); - plugin->onGrpcCreateInitialMetadata(context_id.u64_, token.u64_, headers.u64_); - }; } else if (function_name == "proxy_on_grpc_receive_initial_metadata") { *f = [plugin](Common::Wasm::Context* context, Word context_id, Word token, Word headers) { SaveRestoreContext saved_context(context); @@ -429,9 +429,8 @@ void NullPlugin::onGrpcClose(uint64_t context_id, uint64_t token, uint64_t statu getRootContext(context_id)->onGrpcClose(token, static_cast(status_code)); } -void NullPlugin::onGrpcCreateInitialMetadata(uint64_t context_id, uint64_t token, - uint64_t headers) { - getRootContext(context_id)->onGrpcCreateInitialMetadata(token, headers); +void NullPlugin::onGrpcCreateInitialMetadata(uint64_t context_id, uint64_t headers) { + getRootContext(context_id)->onGrpcCreateInitialMetadata(headers); } void NullPlugin::onGrpcReceiveInitialMetadata(uint64_t context_id, uint64_t token, diff --git a/source/extensions/common/wasm/null/null_plugin.h b/source/extensions/common/wasm/null/null_plugin.h index f063b7431e..97d823df13 100644 --- a/source/extensions/common/wasm/null/null_plugin.h +++ b/source/extensions/common/wasm/null/null_plugin.h @@ -103,7 +103,7 @@ class NullPlugin : public NullVmPlugin { void onGrpcReceive(uint64_t context_id, uint64_t token, size_t body_size); void onGrpcClose(uint64_t context_id, uint64_t token, uint64_t status_code); - void onGrpcCreateInitialMetadata(uint64_t context_id, uint64_t token, uint64_t headers); + void onGrpcCreateInitialMetadata(uint64_t context_id, uint64_t headers); void onGrpcReceiveInitialMetadata(uint64_t context_id, uint64_t token, uint64_t headers); void onGrpcReceiveTrailingMetadata(uint64_t context_id, uint64_t token, uint64_t trailers); diff --git a/source/extensions/common/wasm/wasm.h b/source/extensions/common/wasm/wasm.h index 319a1dd568..a95564cff2 100644 --- a/source/extensions/common/wasm/wasm.h +++ b/source/extensions/common/wasm/wasm.h @@ -230,7 +230,7 @@ class Wasm : public Logger::Loggable, public std::enable_share WasmCallVoid<3> on_grpc_receive_; WasmCallVoid<3> on_grpc_close_; - WasmCallVoid<3> on_grpc_create_initial_metadata_; + WasmCallVoid<2> on_grpc_create_initial_metadata_; WasmCallVoid<3> on_grpc_receive_initial_metadata_; WasmCallVoid<3> on_grpc_receive_trailing_metadata_; diff --git a/test/extensions/access_loggers/wasm/test_data/logging.wasm b/test/extensions/access_loggers/wasm/test_data/logging.wasm index 9b23d9c4ec..ef6276591d 100644 Binary files a/test/extensions/access_loggers/wasm/test_data/logging.wasm and b/test/extensions/access_loggers/wasm/test_data/logging.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/Makefile.docker_cpp_builder b/test/extensions/filters/http/wasm/test_data/Makefile.docker_cpp_builder index c6dbea3473..8758ad43b2 100644 --- a/test/extensions/filters/http/wasm/test_data/Makefile.docker_cpp_builder +++ b/test/extensions/filters/http/wasm/test_data/Makefile.docker_cpp_builder @@ -1,6 +1,6 @@ DOCKER_SDK=/external_sdk -all: headers_cpp.wasm async_call_cpp.wasm metadata_cpp.wasm grpc_call_cpp.wasm shared_cpp.wasm queue_cpp.wasm http_callout_cpp.wasm grpc_callout_cpp.wasm +all: headers_cpp.wasm async_call_cpp.wasm metadata_cpp.wasm grpc_call_cpp.wasm shared_cpp.wasm queue_cpp.wasm http_callout_cpp.wasm grpc_callout_cpp.wasm root_id_cpp.wasm chown ${uid}.${gid} *.wasm include ${DOCKER_SDK}/Makefile.base_lite diff --git a/test/extensions/filters/http/wasm/test_data/async_call_cpp.wasm b/test/extensions/filters/http/wasm/test_data/async_call_cpp.wasm index 13315693b9..bf06510bd2 100644 Binary files a/test/extensions/filters/http/wasm/test_data/async_call_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/async_call_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/grpc_call_cpp.wasm b/test/extensions/filters/http/wasm/test_data/grpc_call_cpp.wasm index 9accf17bd7..067f9022af 100644 Binary files a/test/extensions/filters/http/wasm/test_data/grpc_call_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/grpc_call_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/grpc_callout_cpp.wasm b/test/extensions/filters/http/wasm/test_data/grpc_callout_cpp.wasm index 2e2da2f65f..3773a76390 100644 Binary files a/test/extensions/filters/http/wasm/test_data/grpc_callout_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/grpc_callout_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/headers_cpp.wasm b/test/extensions/filters/http/wasm/test_data/headers_cpp.wasm index 9b23d9c4ec..ef6276591d 100644 Binary files a/test/extensions/filters/http/wasm/test_data/headers_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/headers_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/http_callout_cpp.wasm b/test/extensions/filters/http/wasm/test_data/http_callout_cpp.wasm index 3b5fc78e17..ffb59c7941 100644 Binary files a/test/extensions/filters/http/wasm/test_data/http_callout_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/http_callout_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/metadata_cpp.wasm b/test/extensions/filters/http/wasm/test_data/metadata_cpp.wasm index 53f5167ae2..53986a661f 100644 Binary files a/test/extensions/filters/http/wasm/test_data/metadata_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/metadata_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/queue_cpp.wasm b/test/extensions/filters/http/wasm/test_data/queue_cpp.wasm index b443e37ae3..e5db4ee6df 100644 Binary files a/test/extensions/filters/http/wasm/test_data/queue_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/queue_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/root_id_cpp.wasm b/test/extensions/filters/http/wasm/test_data/root_id_cpp.wasm index 0e2aa4b8d4..f56a511750 100644 Binary files a/test/extensions/filters/http/wasm/test_data/root_id_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/root_id_cpp.wasm differ diff --git a/test/extensions/filters/http/wasm/test_data/shared_cpp.wasm b/test/extensions/filters/http/wasm/test_data/shared_cpp.wasm index 1e7e4f5a8a..78eda412f6 100644 Binary files a/test/extensions/filters/http/wasm/test_data/shared_cpp.wasm and b/test/extensions/filters/http/wasm/test_data/shared_cpp.wasm differ diff --git a/test/extensions/filters/network/wasm/test_data/logging_cpp.wasm b/test/extensions/filters/network/wasm/test_data/logging_cpp.wasm index 5d6650e2ac..5f45c1b694 100644 Binary files a/test/extensions/filters/network/wasm/test_data/logging_cpp.wasm and b/test/extensions/filters/network/wasm/test_data/logging_cpp.wasm differ diff --git a/test/extensions/wasm/test_data/asm2wasm_cpp.wasm b/test/extensions/wasm/test_data/asm2wasm_cpp.wasm index 2091ab6fb4..4aacb1037e 100644 Binary files a/test/extensions/wasm/test_data/asm2wasm_cpp.wasm and b/test/extensions/wasm/test_data/asm2wasm_cpp.wasm differ diff --git a/test/extensions/wasm/test_data/bad_signature_cpp.wasm b/test/extensions/wasm/test_data/bad_signature_cpp.wasm index ecb7868071..0952c8d276 100644 Binary files a/test/extensions/wasm/test_data/bad_signature_cpp.wasm and b/test/extensions/wasm/test_data/bad_signature_cpp.wasm differ diff --git a/test/extensions/wasm/test_data/emscripten_cpp.wasm b/test/extensions/wasm/test_data/emscripten_cpp.wasm index 89c75ca4e9..fe997db084 100644 Binary files a/test/extensions/wasm/test_data/emscripten_cpp.wasm and b/test/extensions/wasm/test_data/emscripten_cpp.wasm differ diff --git a/test/extensions/wasm/test_data/logging_cpp.wasm b/test/extensions/wasm/test_data/logging_cpp.wasm index e5f540fa55..89d1b81219 100644 Binary files a/test/extensions/wasm/test_data/logging_cpp.wasm and b/test/extensions/wasm/test_data/logging_cpp.wasm differ diff --git a/test/extensions/wasm/test_data/segv_cpp.wasm b/test/extensions/wasm/test_data/segv_cpp.wasm index 7f3d89cf62..43b654eb7a 100644 Binary files a/test/extensions/wasm/test_data/segv_cpp.wasm and b/test/extensions/wasm/test_data/segv_cpp.wasm differ diff --git a/test/extensions/wasm/test_data/speed_cpp.wasm b/test/extensions/wasm/test_data/speed_cpp.wasm index 49d40f03e8..9ac2422cd0 100644 Binary files a/test/extensions/wasm/test_data/speed_cpp.wasm and b/test/extensions/wasm/test_data/speed_cpp.wasm differ diff --git a/test/extensions/wasm/test_data/stats_cpp.wasm b/test/extensions/wasm/test_data/stats_cpp.wasm index 0805ac1261..79ded19ea7 100644 Binary files a/test/extensions/wasm/test_data/stats_cpp.wasm and b/test/extensions/wasm/test_data/stats_cpp.wasm differ