Skip to content

Commit

Permalink
Fix segfault by ensuring that we have created the context in the VM. (i…
Browse files Browse the repository at this point in the history
…stio#189)

* Fix segfault by ensuring that we have created the context in the VM.
envoyproxy/envoy-wasm#180

Signed-off-by: John Plevyak <jplevyak@gmail.com>
  • Loading branch information
jplevyak committed Sep 17, 2019
1 parent 81a490a commit fdf6778
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
12 changes: 11 additions & 1 deletion source/extensions/common/wasm/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1943,6 +1943,7 @@ void Context::onCreate(uint32_t root_context_id) {

Http::FilterHeadersStatus Context::onRequestHeaders() {
onCreate(root_context_id_);
in_vm_context_created_ = true;
// Store the stream id so that we can use it in log().
auto& stream_info = decoder_callbacks_->streamInfo();
auto& metadata = (*stream_info.dynamicMetadata()
Expand Down Expand Up @@ -1992,8 +1993,17 @@ Http::FilterMetadataStatus Context::onRequestMetadata() {
}

Http::FilterHeadersStatus Context::onResponseHeaders() {
if (!wasm_->onResponseHeaders_)
if (!in_vm_context_created_) {
// If the request is invalid then onRequestHeaders() will not be called and neither will
// onCreate() then sendLocalReply be called which will call this function. In this case we
// need to call onCreate() so that the Context inside the VM is created before the
// onResponseHeaders() call.
onCreate(root_context_id_);
in_vm_context_created_ = true;
}
if (!wasm_->onResponseHeaders_) {
return Http::FilterHeadersStatus::Continue;
}
if (wasm_->onResponseHeaders_(this, id_) == 0) {
return Http::FilterHeadersStatus::Continue;
}
Expand Down
1 change: 1 addition & 0 deletions source/extensions/common/wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ class Context : public Http::StreamFilter,
Context* root_context_{nullptr}; // set in all contexts.
const std::string root_id_; // set only in roots.
std::string log_prefix_;
bool in_vm_context_created_ = false;
bool destroyed_ = false;

uint32_t next_http_call_token_ = 1;
Expand Down
2 changes: 1 addition & 1 deletion source/extensions/filters/http/wasm/wasm_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FilterConfig : Logger::Loggable<Logger::Id::wasm> {
if (!root_context_id_) {
root_context_id_ = wasm.getRootContext(root_id_)->id();
}
return std::make_shared<Context>(&tls_slot_->getTyped<Wasm>(), root_context_id_);
return std::make_shared<Context>(&wasm, root_context_id_);
}

private:
Expand Down

0 comments on commit fdf6778

Please sign in to comment.