Skip to content

Commit a20e8e1

Browse files
committed
src: track cppgc wrappers with a list in Realm
This allows us to perform cleanups of cppgc wrappers that rely on a living Realm during Realm shutdown. Otherwise the cleanup may happen during object destruction, which can be triggered by GC after Realm shutdown, leading to invalid access to Realm. The general pattern for this type of non-trivial destruction is designed to be: ``` class MyWrap final : CPPGC_MIXIN(MyWrap) { public: ~MyWrap() { this->Finalize(); } void Clean(Realm* realm) override { // Do cleanup that relies on a living Realm. This would be // called by CppgcMixin::Finalize() first during Realm // shutdown, while the Realm is still alive. If the destructor // calls Finalize() again later during garbage collection that // happens after Realm shutdown, Clean() would be skipped, // preventing invalid access to the Realm. } } ``` In addition, this allows us to trace external memory held by the wrappers in the heap snapshots if we add synthethic edges between the wrappers and other nodes in the embdder graph callback, or to perform snapshot serialization for them.
1 parent eab0fe2 commit a20e8e1

11 files changed

Lines changed: 330 additions & 49 deletions

node.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
'src/connect_wrap.h',
207207
'src/connection_wrap.h',
208208
'src/cppgc_helpers.h',
209+
'src/cppgc_helpers.cc',
209210
'src/dataqueue/queue.h',
210211
'src/debug_utils.h',
211212
'src/debug_utils-inl.h',

src/README.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,17 @@ class MyWrap final : CPPGC_MIXIN(MyWrap) {
11121112
}
11131113
```
11141114

1115+
If the wrapper needs to perform cleanups when it's destroyed and that
1116+
cleanup relies on a living Node.js `Realm`, it should implement a
1117+
pattern like this:
1118+
1119+
```cpp
1120+
~MyWrap() { this->Finalize(); }
1121+
void Clean(Realm* env) override {
1122+
// Do cleanup that relies on a living Realm.
1123+
}
1124+
```
1125+
11151126
`cppgc::GarbageCollected` types are expected to implement a
11161127
`void Trace(cppgc::Visitor* visitor) const` method. When they are the
11171128
final class in the hierarchy, this method must be marked `final`. For
@@ -1266,16 +1277,76 @@ referrer->Set(
12661277
).ToLocalChecked();
12671278
```
12681279

1280+
#### Creating references between cppgc-managed objects and `BaseObject`s
1281+
1282+
This is currently unsupported with the existing helpers. If this has
1283+
to be done, new helpers must be implemented first. Consult the cppgc
1284+
headers when trying to implement it.
1285+
1286+
Another way to work around it is to always do the migration bottom-to-top.
1287+
If a cppgc-managed object needs to reference a `BaseObject`, convert
1288+
that `BaseObject` to be cppgc-managed first, and then use `cppgc::Member`
1289+
to create the references.
1290+
1291+
#### Lifetime and cleanups of cppgc-managed objects
1292+
12691293
Typically, a newly created cppgc-managed wrapper object should be held alive
12701294
by the JavaScript land (for example, by being returned by a method and
12711295
staying alive in a closure). Long-lived cppgc objects can also
12721296
be held alive from C++ using persistent handles (see
12731297
`deps/v8/include/cppgc/persistent.h`) or as members of other living
12741298
cppgc-managed objects (see `deps/v8/include/cppgc/member.h`) if necessary.
1275-
Its destructor will be called when no other objects from the V8 heap reference
1276-
it, this can happen at any time after the garbage collector notices that
1277-
it's no longer reachable and before the V8 isolate is torn down.
1278-
See the [Oilpan documentation in Chromium][] for more details.
1299+
1300+
When a cppgc-managed object is no longer reachable in the heap, its destructor
1301+
will be invoked by the garbage collection, which can happen after the `Realm`
1302+
is already gone, or after any object it references is gone. It is therefore
1303+
unsafe to invoke V8 APIs directly in the destructors. To ensure safety,
1304+
the cleanups of a cppgc-managed object should adhere to different patterns,
1305+
depending on what it needs to do:
1306+
1307+
1. If it does not need to do any non-trivial cleanup, nor does its members, just use
1308+
the default destructor. Cleanup of `v8::TracedReference` and
1309+
`cppgc::Member` are already handled automatically by V8 so if they are all the
1310+
non-trivial members the class has, this case applies.
1311+
2. If the cleanup relies on a living `Realm`, but does not need to access V8
1312+
APIs, the class should use this pattern in its class body:
1313+
1314+
```cpp
1315+
~MyWrap() { this->Finalize(); }
1316+
void Clean(Realm* env) override {
1317+
// Do cleanup that relies on a living Realm. This would be
1318+
// called by CppgcMixin::Finalize() first during Realm shutdown,
1319+
// while the Realm is still alive. If the destructor calls
1320+
// Finalize() again later during garbage collection that happens after
1321+
// Realm shutdown, Clean() would be skipped, preventing
1322+
// invalid access to the Realm.
1323+
}
1324+
```
1325+
1326+
If implementers want to call `Finalize()` from `Clean()` again, they
1327+
need to make sure that calling `Clean()` recursively is safe.
1328+
3. If the cleanup relies on access to the V8 heap, including using any V8
1329+
handles, in addition to 2, it should use the `CPPGC_USING_PRE_FINALIZER`
1330+
macro (from the [`cppgc/prefinalizer.h` header][]) in the private
1331+
section of its class body:
1332+
1333+
```cpp
1334+
private:
1335+
CPPGC_USING_PRE_FINALIZER(MyWrap, Finalize);
1336+
```
1337+
1338+
Both the destructor and the pre-finalizer are always called on the thread
1339+
in which the object is created.
1340+
1341+
It's worth noting that the use of pre-finalizers would have a negative impact
1342+
on the garbage collection performance as V8 needs to scan all of them during
1343+
each sweeping. If the object is expected to be created frequently in large
1344+
amounts in the application, it's better to avoid access to the V8 heap in its
1345+
cleanup to avoid having to use a pre-finalizer.
1346+
1347+
For more information about the cleanup of cppgc-managed objects and
1348+
what can be done in a pre-finalizer, see the [cppgc documentation][] and
1349+
the [`cppgc/prefinalizer.h` header][].
12791350

12801351
### Callback scopes
12811352

@@ -1402,6 +1473,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
14021473
[`async_hooks` module]: https://nodejs.org/api/async_hooks.html
14031474
[`async_wrap.h`]: async_wrap.h
14041475
[`base_object.h`]: base_object.h
1476+
[`cppgc/prefinalizer.h` header]: ../deps/v8/include/cppgc/prefinalizer.h
14051477
[`handle_wrap.h`]: handle_wrap.h
14061478
[`memory_tracker.h`]: memory_tracker.h
14071479
[`req_wrap.h`]: req_wrap.h
@@ -1412,6 +1484,7 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
14121484
[`vm` module]: https://nodejs.org/api/vm.html
14131485
[binding function]: #binding-functions
14141486
[cleanup hooks]: #cleanup-hooks
1487+
[cppgc documentation]: ../deps/v8/include/cppgc/README.md
14151488
[event loop]: #event-loop
14161489
[exception handling]: #exception-handling
14171490
[fast API calls]: ../doc/contributing/adding-v8-fast-api.md

src/cppgc_helpers-inl.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef SRC_CPPGC_HELPERS_INL_H_
2+
#define SRC_CPPGC_HELPERS_INL_H_
3+
4+
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5+
6+
#include "cppgc_helpers.h"
7+
#include "env-inl.h"
8+
9+
namespace node {
10+
11+
template <typename T>
12+
void CppgcMixin::Wrap(T* ptr, Realm* realm, v8::Local<v8::Object> obj) {
13+
CHECK_GE(obj->InternalFieldCount(), T::kInternalFieldCount);
14+
ptr->realm_ = realm;
15+
v8::Isolate* isolate = realm->isolate();
16+
ptr->traced_reference_ = v8::TracedReference<v8::Object>(isolate, obj);
17+
// Note that ptr must be of concrete type T in Wrap.
18+
v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>(isolate, obj, ptr);
19+
// Keep the layout consistent with BaseObjects.
20+
obj->SetAlignedPointerInInternalField(
21+
kEmbedderType, realm->isolate_data()->embedder_id_for_cppgc());
22+
obj->SetAlignedPointerInInternalField(kSlot, ptr);
23+
realm->TrackCppgcWrapper(ptr);
24+
}
25+
26+
template <typename T>
27+
void CppgcMixin::Wrap(T* ptr, Environment* env, v8::Local<v8::Object> obj) {
28+
Wrap(ptr, env->principal_realm(), obj);
29+
}
30+
31+
template <typename T>
32+
T* CppgcMixin::Unwrap(v8::Local<v8::Object> obj) {
33+
// We are not using v8::Object::Unwrap currently because that requires
34+
// access to isolate which the ASSIGN_OR_RETURN_UNWRAP macro that we'll shim
35+
// with ASSIGN_OR_RETURN_UNWRAP_GC doesn't take, and we also want a
36+
// signature consistent with BaseObject::Unwrap() to avoid churn. Since
37+
// cppgc-managed objects share the same layout as BaseObjects, just unwrap
38+
// from the pointer in the internal field, which should be valid as long as
39+
// the object is still alive.
40+
if (obj->InternalFieldCount() != T::kInternalFieldCount) {
41+
return nullptr;
42+
}
43+
T* ptr = static_cast<T*>(obj->GetAlignedPointerFromInternalField(T::kSlot));
44+
return ptr;
45+
}
46+
47+
v8::Local<v8::Object> CppgcMixin::object() const {
48+
return traced_reference_.Get(realm_->isolate());
49+
}
50+
51+
Environment* CppgcMixin::env() const {
52+
return realm_->env();
53+
}
54+
55+
} // namespace node
56+
57+
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
58+
59+
#endif // SRC_CPPGC_HELPERS_INL_H_

src/cppgc_helpers.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "cppgc_helpers.h"
2+
#include "env-inl.h"
3+
4+
namespace node {
5+
6+
void CppgcWrapperList::Cleanup() {
7+
for (auto handle : *this) {
8+
handle->Finalize();
9+
}
10+
}
11+
12+
void CppgcWrapperList::MemoryInfo(MemoryTracker* tracker) const {
13+
for (auto handle : *this) {
14+
tracker->Track(handle);
15+
}
16+
}
17+
18+
} // namespace node

src/cppgc_helpers.h

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
#include <type_traits> // std::remove_reference
77
#include "cppgc/garbage-collected.h"
88
#include "cppgc/name-provider.h"
9-
#include "env.h"
109
#include "memory_tracker.h"
10+
#include "util.h"
1111
#include "v8-cppgc.h"
1212
#include "v8-sandbox.h"
1313
#include "v8.h"
1414

1515
namespace node {
1616

17+
class Environment;
18+
class Realm;
19+
class CppgcWrapperList;
20+
1721
/**
1822
* This is a helper mixin with a BaseObject-like interface to help
1923
* implementing wrapper objects managed by V8's cppgc (Oilpan) library.
@@ -25,20 +29,29 @@ namespace node {
2529
* with V8's GC scheduling.
2630
*
2731
* A cppgc-managed native wrapper should look something like this, note
28-
* that per cppgc rules, CPPGC_MIXIN(Klass) must be at the left-most
32+
* that per cppgc rules, CPPGC_MIXIN(MyWrap) must be at the left-most
2933
* position in the hierarchy (which ensures cppgc::GarbageCollected
3034
* is at the left-most position).
3135
*
32-
* class Klass final : CPPGC_MIXIN(Klass) {
36+
* class MyWrap final : CPPGC_MIXIN(MyWrap) {
3337
* public:
34-
* SET_CPPGC_NAME(Klass) // Sets the heap snapshot name to "Node / Klass"
38+
* SET_CPPGC_NAME(MyWrap) // Sets the heap snapshot name to "Node / MyWrap"
3539
* void Trace(cppgc::Visitor* visitor) const final {
3640
* CppgcMixin::Trace(visitor);
3741
* visitor->Trace(...); // Trace any additional owned traceable data
3842
* }
3943
* }
44+
*
45+
* If the wrapper needs to perform cleanups when it's destroyed and that
46+
* cleanup relies on a living Node.js `Realm`, it should implement a
47+
* pattern like this:
48+
*
49+
* ~MyWrap() { this->Destroy(); }
50+
* void Clean(Realm* env) override {
51+
* // Do cleanup that relies on a living Environemnt.
52+
* }
4053
*/
41-
class CppgcMixin : public cppgc::GarbageCollectedMixin {
54+
class CppgcMixin : public cppgc::GarbageCollectedMixin, public MemoryRetainer {
4255
public:
4356
// To help various callbacks access wrapper objects with different memory
4457
// management, cppgc-managed objects share the same layout as BaseObjects.
@@ -48,49 +61,56 @@ class CppgcMixin : public cppgc::GarbageCollectedMixin {
4861
// invoked from the child class constructor, per cppgc::GarbageCollectedMixin
4962
// rules.
5063
template <typename T>
51-
static void Wrap(T* ptr, Environment* env, v8::Local<v8::Object> obj) {
52-
CHECK_GE(obj->InternalFieldCount(), T::kInternalFieldCount);
53-
ptr->env_ = env;
54-
v8::Isolate* isolate = env->isolate();
55-
ptr->traced_reference_ = v8::TracedReference<v8::Object>(isolate, obj);
56-
v8::Object::Wrap<v8::CppHeapPointerTag::kDefaultTag>(isolate, obj, ptr);
57-
// Keep the layout consistent with BaseObjects.
58-
obj->SetAlignedPointerInInternalField(
59-
kEmbedderType, env->isolate_data()->embedder_id_for_cppgc());
60-
obj->SetAlignedPointerInInternalField(kSlot, ptr);
61-
}
64+
static inline void Wrap(T* ptr, Realm* realm, v8::Local<v8::Object> obj);
65+
template <typename T>
66+
static inline void Wrap(T* ptr, Environment* env, v8::Local<v8::Object> obj);
6267

63-
v8::Local<v8::Object> object() const {
64-
return traced_reference_.Get(env_->isolate());
68+
inline v8::Local<v8::Object> object() const;
69+
inline Environment* env() const;
70+
inline Realm* realm() const { return realm_; }
71+
inline v8::Local<v8::Object> object(v8::Isolate* isolate) const {
72+
return traced_reference_.Get(isolate);
6573
}
6674

67-
Environment* env() const { return env_; }
68-
6975
template <typename T>
70-
static T* Unwrap(v8::Local<v8::Object> obj) {
71-
// We are not using v8::Object::Unwrap currently because that requires
72-
// access to isolate which the ASSIGN_OR_RETURN_UNWRAP macro that we'll shim
73-
// with ASSIGN_OR_RETURN_UNWRAP_GC doesn't take, and we also want a
74-
// signature consistent with BaseObject::Unwrap() to avoid churn. Since
75-
// cppgc-managed objects share the same layout as BaseObjects, just unwrap
76-
// from the pointer in the internal field, which should be valid as long as
77-
// the object is still alive.
78-
if (obj->InternalFieldCount() != T::kInternalFieldCount) {
79-
return nullptr;
80-
}
81-
T* ptr = static_cast<T*>(obj->GetAlignedPointerFromInternalField(T::kSlot));
82-
return ptr;
83-
}
76+
static inline T* Unwrap(v8::Local<v8::Object> obj);
8477

8578
// Subclasses are expected to invoke CppgcMixin::Trace() in their own Trace()
8679
// methods.
8780
void Trace(cppgc::Visitor* visitor) const override {
8881
visitor->Trace(traced_reference_);
8982
}
9083

84+
// TODO(joyeecheung): use ObjectSizeTrait;
85+
inline size_t SelfSize() const override { return sizeof(*this); }
86+
inline bool IsCppgcWrapper() const override { return true; }
87+
88+
// This is run for all the remaining Cppgc wrappers tracked in the Realm
89+
// during Realm shutdown. The destruction of the wrappers would happen later,
90+
// when the final garbage collection is triggered when CppHeap is torn down as
91+
// part of the Isolate teardown. If subclasses of CppgcMixin wish to perform
92+
// cleanups that depend on the Realm during destruction, they should implment
93+
// it in a Clean() override, and then call this->Finalize() from their
94+
// destructor. Outside of Finalize(), subclasses should avoid calling
95+
// into JavaScript or perform any operation that can trigger garbage
96+
// collection during the destruction.
97+
void Finalize() {
98+
if (realm_ == nullptr) return;
99+
this->Clean(realm_);
100+
realm_ = nullptr;
101+
}
102+
103+
// The default implementation of Clean() is a no-op. Subclasses
104+
// should override it to perform cleanup that require a living Realm,
105+
// instead of doing these cleanups directly in the destructor.
106+
virtual void Clean(Realm* realm) {}
107+
108+
friend class CppgcWrapperList;
109+
91110
private:
92-
Environment* env_;
111+
Realm* realm_ = nullptr;
93112
v8::TracedReference<v8::Object> traced_reference_;
113+
ListNode<CppgcMixin> wrapper_list_node_;
94114
};
95115

96116
// If the class doesn't have additional owned traceable data, use this macro to
@@ -105,7 +125,8 @@ class CppgcMixin : public cppgc::GarbageCollectedMixin {
105125
#define SET_CPPGC_NAME(Klass) \
106126
inline const char* GetHumanReadableName() const final { \
107127
return "Node / " #Klass; \
108-
}
128+
} \
129+
inline const char* MemoryInfoName() const override { return #Klass; }
109130

110131
/**
111132
* Similar to ASSIGN_OR_RETURN_UNWRAP() but works on cppgc-managed types

0 commit comments

Comments
 (0)