Skip to content

Commit 1e9c10b

Browse files
codebytereaduh95
authored andcommitted
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 6f610a8 commit 1e9c10b

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
@@ -650,7 +650,10 @@ IsolateData::IsolateData(Isolate* isolate,
650650
}
651651
}
652652

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

655658
// Deprecated API, embedders should use v8::Object::Wrap() directly instead.
656659
void SetCppgcReference(Isolate* isolate,
@@ -982,6 +985,7 @@ Environment::Environment(IsolateData* isolate_data,
982985
? AllocateEnvironmentThreadId().id
983986
: thread_id.id),
984987
thread_name_(thread_name) {
988+
isolate_data->AddEnvironment();
985989
#if HAVE_OPENSSL && NCRYPTO_USE_OPENSSL3_PROVIDER
986990
provider_digest_cache = std::make_unique<ncrypto::DigestCache>();
987991
provider_cipher_cache = std::make_unique<ncrypto::CipherCache>();
@@ -1289,6 +1293,7 @@ Environment::~Environment() {
12891293
cpu_profiler_->Dispose();
12901294
cpu_profiler_ = nullptr;
12911295
}
1296+
isolate_data_->RemoveEnvironment();
12921297
}
12931298

12941299
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)