From 2bee3d552e085ee1388c709df83b355c40c5dbf6 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Wed, 17 Jan 2024 09:58:16 +0200 Subject: [PATCH] Fix use-after-free Fixes #443 --- src/external_copy/string.cc | 6 ++++-- src/isolate/executor.cc | 5 +++++ src/isolate/executor.h | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/external_copy/string.cc b/src/external_copy/string.cc index 8c6c4400..a6f65269 100644 --- a/src/external_copy/string.cc +++ b/src/external_copy/string.cc @@ -20,7 +20,8 @@ class ExternalString final : public v8::String::ExternalStringResource { ExternalString(const ExternalString&) = delete; ~ExternalString() final { - IsolateEnvironment::GetCurrent()->AdjustExtraAllocatedMemory(-static_cast(this->value->size())); + if (auto *env = IsolateEnvironment::GetCurrent()) + env->AdjustExtraAllocatedMemory(-static_cast(this->value->size())); } auto operator= (const ExternalString&) = delete; @@ -46,7 +47,8 @@ class ExternalStringOneByte final : public v8::String::ExternalOneByteStringReso ExternalStringOneByte(const ExternalStringOneByte&) = delete; ~ExternalStringOneByte() final { - IsolateEnvironment::GetCurrent()->AdjustExtraAllocatedMemory(-static_cast(this->value->size())); + if (auto *env = IsolateEnvironment::GetCurrent()) + env->AdjustExtraAllocatedMemory(-static_cast(this->value->size())); } auto operator= (const ExternalStringOneByte&) = delete; diff --git a/src/isolate/executor.cc b/src/isolate/executor.cc index 9c1e3e41..57cb8555 100644 --- a/src/isolate/executor.cc +++ b/src/isolate/executor.cc @@ -17,6 +17,11 @@ Executor::Executor(IsolateEnvironment& env) : default_executor{*(current_executor == nullptr ? (current_executor = this) : ¤t_executor->default_executor)}, default_thread{&default_executor == this ? std::this_thread::get_id() : default_executor.default_thread} {} +Executor::~Executor() { + if (current_executor == this) + current_executor = nullptr; +} + auto Executor::MayRunInlineTasks(IsolateEnvironment& env) -> bool { if (current_executor == &env.executor) { if (env.nodejs_isolate) { diff --git a/src/isolate/executor.h b/src/isolate/executor.h index b4a149e6..529c5f3b 100644 --- a/src/isolate/executor.h +++ b/src/isolate/executor.h @@ -21,7 +21,7 @@ class Executor { // "En taro adun" public: explicit Executor(IsolateEnvironment& env); Executor(const Executor&) = delete; - ~Executor() = default; + ~Executor(); auto operator= (const Executor&) = delete; static auto GetCurrentEnvironment() -> IsolateEnvironment*;