Skip to content

Commit

Permalink
Fix segfault by ensuring that we have created the context in the VM.
Browse files Browse the repository at this point in the history
envoyproxy#180

Signed-off-by: John Plevyak <jplevyak@gmail.com>
  • Loading branch information
jplevyak committed Sep 13, 2019
1 parent e16c692 commit f9354c7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions source/extensions/common/wasm/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,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 @@ -1819,6 +1820,14 @@ Http::FilterMetadataStatus Context::onRequestMetadata() {
}

Http::FilterHeadersStatus Context::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 befor ethe
// onResponseHeaders() call.
onCreate(root_context_id_);
in_vm_context_created_ = true;
}
if (!wasm_->onResponseHeaders_) {
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 @@ -399,6 +399,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 f9354c7

Please sign in to comment.