Skip to content

Commit

Permalink
env: fix use-after-free in keep-alive allocators
Browse files Browse the repository at this point in the history
As per comment in the change it is possible that
`Environment:AddArrayBufferAllocatorToKeepAliveUntilIsolateDispose`
is called from `node::FreeEnvironment(env)` after `node::Stop(env)`
was already called.

In this case platform's `per_isolate_` map won't have an entry for our
isolate and the `AddIsolateFinishedCallback` will invoke the callback
immediately leaving us with `keep_alive_allocators_` set to freed pointer.

This commit also changes the assertion in the
`AddIsolateFinishedCallback` that is triggered incorrectly.
  • Loading branch information
indutny committed Mar 31, 2021
1 parent 7d488fe commit d248ed3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,17 @@ char* Environment::Reallocate(char* data, size_t old_size, size_t size) {

void Environment::AddArrayBufferAllocatorToKeepAliveUntilIsolateDispose(
std::shared_ptr<v8::ArrayBuffer::Allocator> allocator) {
// It is possible that we can arrive here from `node::FreeEnvironment` when it
// is called after `node::Stop(env)`.
//
// In this case platform's `per_isolate_` map won't have an entry for our
// isolate and the `AddIsolateFinishedCallback` will invoke the callback
// immediately leaving us with `keep_alive_allocators_` set to freed pointer.
if (is_stopping() && started_cleanup_) {
allocator.reset();
return;
}

if (keep_alive_allocators_ == nullptr) {
MultiIsolatePlatform* platform = isolate_data()->platform();
CHECK_NOT_NULL(platform);
Expand Down
2 changes: 1 addition & 1 deletion src/node_platform.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,10 @@ void NodePlatform::AddIsolateFinishedCallback(Isolate* isolate,
Mutex::ScopedLock lock(per_isolate_mutex_);
auto it = per_isolate_.find(isolate);
if (it == per_isolate_.end()) {
CHECK(it->second);
cb(data);
return;
}
CHECK(it->second);
it->second->AddShutdownCallback(cb, data);
}

Expand Down

0 comments on commit d248ed3

Please sign in to comment.