Skip to content

Commit 670a077

Browse files
committed
src: assert that an IsolateData outlives its Environments
FreeIsolateData() while an Environment created from it is still alive left that Environment with a dangling pointer and failed later in unrelated code. Count the Environments using an IsolateData and CHECK in its destructor that none are left. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com> PR-URL: #66239 Refs: #65977 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent ae70f7e commit 670a077

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎src/env.cc‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,10 @@ IsolateData::IsolateData(Isolate* isolate,
651651
}
652652
}
653653

654-
IsolateData::~IsolateData() {}
654+
IsolateData::~IsolateData() {
655+
// FreeIsolateData() before FreeEnvironment() of an Environment using it.
656+
CHECK_EQ(environment_count_, 0);
657+
}
655658

656659
// Deprecated API, embedders should use v8::Object::Wrap() directly instead.
657660
void SetCppgcReference(Isolate* isolate,
@@ -983,6 +986,7 @@ Environment::Environment(IsolateData* isolate_data,
983986
? AllocateEnvironmentThreadId().id
984987
: thread_id.id),
985988
thread_name_(thread_name) {
989+
isolate_data->AddEnvironment();
986990
#if HAVE_OPENSSL && NCRYPTO_USE_OPENSSL3_PROVIDER
987991
provider_digest_cache = std::make_unique<ncrypto::DigestCache>();
988992
provider_cipher_cache = std::make_unique<ncrypto::CipherCache>();
@@ -1304,6 +1308,7 @@ Environment::~Environment() {
13041308
cpu_profiler_->Dispose();
13051309
cpu_profiler_ = nullptr;
13061310
}
1311+
isolate_data_->RemoveEnvironment();
13071312
}
13081313

13091314
void Environment::InitializeLibuv() {

‎src/env.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
182182
inline worker::Worker* worker_context() const;
183183
inline void set_worker_context(worker::Worker* context);
184184

185+
void AddEnvironment() { environment_count_++; }
186+
void RemoveEnvironment() { environment_count_--; }
187+
185188
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
186189
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName)
187190
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
@@ -283,6 +286,7 @@ class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer {
283286

284287
std::shared_ptr<PerIsolateOptions> options_;
285288
worker::Worker* worker_context_ = nullptr;
289+
size_t environment_count_ = 0;
286290
PerIsolateWrapperData* wrapper_data_;
287291

288292
static Mutex isolate_data_mutex_;

0 commit comments

Comments
 (0)