Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault by ensuring that we have created the context in the VM. … #107

Merged
merged 1 commit into from
Sep 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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