|
3 | 3 | #include <cppgc/heap.h> |
4 | 4 | #include <node.h> |
5 | 5 | #include <v8-cppgc.h> |
| 6 | +#include <v8-external-memory-accounter.h> |
6 | 7 | #include <v8-sandbox.h> |
7 | 8 | #include <v8.h> |
| 9 | +#include "cppgc_helpers-inl.h" |
| 10 | +#include "node_realm-inl.h" |
8 | 11 | #include "node_test_fixture.h" |
9 | 12 |
|
10 | 13 | // This tests that Node.js can work with an existing CppHeap. |
@@ -106,3 +109,114 @@ TEST_F(NodeZeroIsolateTestFixture, ExistingCppHeapTest) { |
106 | 109 | // heap can be reclaimed. So just check at least some of them are traced. |
107 | 110 | EXPECT_GT(CppGCed::kTraceCount, 0); |
108 | 111 | } |
| 112 | + |
| 113 | +class CppgcTest : public EnvironmentTestFixture { |
| 114 | + protected: |
| 115 | + // Above the external memory hard limit, so V8 runs a full GC synchronously |
| 116 | + // and leaves sweeping (and cppgc destructors) for later. |
| 117 | + static constexpr size_t kExternalMemoryPressure = size_t{8} << 30; |
| 118 | + |
| 119 | + void CollectGarbageLeavingSweepingPending() { |
| 120 | + v8::ExternalMemoryAccounter pressure; |
| 121 | + pressure.Increase(isolate_, kExternalMemoryPressure); |
| 122 | + pressure.Decrease(isolate_, kExternalMemoryPressure); |
| 123 | + } |
| 124 | + |
| 125 | + void FinishSweeping() { |
| 126 | + isolate_->LowMemoryNotification(); |
| 127 | + platform->DrainTasks(isolate_); |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +using node::CppgcMixin; |
| 132 | + |
| 133 | +class RealmBoundWrap final : CPPGC_MIXIN(RealmBoundWrap) { |
| 134 | + public: |
| 135 | + SET_CPPGC_NAME(RealmBoundWrap) |
| 136 | + DEFAULT_CPPGC_TRACE() |
| 137 | + SET_NO_MEMORY_INFO() |
| 138 | + |
| 139 | + static node::Realm* live_realm; |
| 140 | + static int clean_count; |
| 141 | + static int clean_with_dead_realm_count; |
| 142 | + static int destructor_count; |
| 143 | + |
| 144 | + RealmBoundWrap(node::Environment* env, v8::Local<v8::Object> object) { |
| 145 | + CppgcMixin::Wrap(this, env, object); |
| 146 | + } |
| 147 | + ~RealmBoundWrap() { |
| 148 | + Finalize(); |
| 149 | + destructor_count++; |
| 150 | + } |
| 151 | + void Clean(node::Realm* realm) override { |
| 152 | + clean_count++; |
| 153 | + if (realm != live_realm) clean_with_dead_realm_count++; |
| 154 | + } |
| 155 | +}; |
| 156 | + |
| 157 | +node::Realm* RealmBoundWrap::live_realm = nullptr; |
| 158 | +int RealmBoundWrap::clean_count = 0; |
| 159 | +int RealmBoundWrap::clean_with_dead_realm_count = 0; |
| 160 | +int RealmBoundWrap::destructor_count = 0; |
| 161 | + |
| 162 | +TEST_F(CppgcTest, CleanIsNotCalledWithFreedRealm) { |
| 163 | + constexpr int kCount = 32; |
| 164 | + { |
| 165 | + const v8::HandleScope handle_scope(isolate_); |
| 166 | + Env env{handle_scope, Argv()}; |
| 167 | + RealmBoundWrap::live_realm = (*env)->principal_realm(); |
| 168 | + |
| 169 | + v8::Local<v8::FunctionTemplate> ctor = v8::FunctionTemplate::New(isolate_); |
| 170 | + ctor->InstanceTemplate()->SetInternalFieldCount( |
| 171 | + node::CppgcMixin::kInternalFieldCount); |
| 172 | + v8::Local<v8::Function> fn = |
| 173 | + ctor->GetFunction(env.context()).ToLocalChecked(); |
| 174 | + { |
| 175 | + v8::HandleScope inner_scope(isolate_); |
| 176 | + for (int i = 0; i <= kCount; i++) { |
| 177 | + v8::Local<v8::Object> obj = |
| 178 | + fn->NewInstance(env.context()).ToLocalChecked(); |
| 179 | + cppgc::MakeGarbageCollected<RealmBoundWrap>( |
| 180 | + (*env)->cppgc_allocation_handle(), *env, obj); |
| 181 | + if (i < kCount) continue; |
| 182 | + env.context() |
| 183 | + ->Global() |
| 184 | + ->Set(env.context(), |
| 185 | + v8::String::NewFromUtf8Literal(isolate_, "kept"), |
| 186 | + obj) |
| 187 | + .Check(); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + CollectGarbageLeavingSweepingPending(); |
| 192 | + EXPECT_LT(RealmBoundWrap::destructor_count, kCount); |
| 193 | + } |
| 194 | + RealmBoundWrap::live_realm = nullptr; |
| 195 | + FinishSweeping(); |
| 196 | + |
| 197 | + EXPECT_GE(RealmBoundWrap::clean_count, 1); |
| 198 | + EXPECT_EQ(RealmBoundWrap::clean_with_dead_realm_count, 0); |
| 199 | +} |
| 200 | + |
| 201 | +TEST_F(CppgcTest, WrappersAliveAtFreeEnvironmentDoNotLeak) { |
| 202 | + const v8::HandleScope handle_scope(isolate_); |
| 203 | + Env env{handle_scope, Argv()}; |
| 204 | + node::LoadEnvironment(*env, |
| 205 | + "globalThis.script = new (require('vm').Script)('1');" |
| 206 | + "globalThis.context = require('vm').createContext();") |
| 207 | + .ToLocalChecked(); |
| 208 | +} |
| 209 | + |
| 210 | +TEST_F(CppgcTest, VmScriptCollectedBeforeFreeEnvironmentSweptAfter) { |
| 211 | + { |
| 212 | + const v8::HandleScope handle_scope(isolate_); |
| 213 | + Env env{handle_scope, Argv()}; |
| 214 | + node::LoadEnvironment(*env, |
| 215 | + "const { Script } = require('vm');" |
| 216 | + "for (let i = 0; i < 64; i++) new Script('1');" |
| 217 | + "undefined;") |
| 218 | + .ToLocalChecked(); |
| 219 | + CollectGarbageLeavingSweepingPending(); |
| 220 | + } |
| 221 | + FinishSweeping(); |
| 222 | +} |
0 commit comments