Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7552de6

Browse files
joyeecheungrichardlau
authored andcommitted
module: fix the leak in SourceTextModule and ContextifySript
Replace the persistent handles to v8::Module andv8::UnboundScript with an internal reference that V8's GC isaware of to fix the leaks.PR-URL:#48510Backport-PR-URL:#51004Refs:#44211Refs:#42080Refs:#47096Refs:#43205Refs:#38695Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
1 parent2e05cf1 commit7552de6

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

‎src/module_wrap.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ ModuleWrap::ModuleWrap(Environment* env,
5555
Local<String> url,
5656
Local<Object> context_object,
5757
Local<Value> synthetic_evaluation_step)
58-
: BaseObject(env, object), module_(env->isolate(), module) {
58+
: BaseObject(env, object),
59+
module_(env->isolate(), module),
60+
module_hash_(module->GetIdentityHash()) {
61+
object->SetInternalFieldForNodeCore(kModuleSlot,module);
5962
object->SetInternalField(kURLSlot, url);
6063
object->SetInternalField(kSyntheticEvaluationStepsSlot,
6164
synthetic_evaluation_step);
@@ -65,12 +68,12 @@ ModuleWrap::ModuleWrap(Environment* env,
6568
synthetic_ =true;
6669
}
6770
MakeWeak();
71+
module_.SetWeak();
6872
}
6973

7074
ModuleWrap::~ModuleWrap() {
7175
HandleScopescope(env()->isolate());
72-
Local<Module>module = module_.Get(env()->isolate());
73-
auto range =env()->hash_to_module_map.equal_range(module->GetIdentityHash());
76+
auto range =env()->hash_to_module_map.equal_range(module_hash_);
7477
for (auto it = range.first; it != range.second; ++it) {
7578
if (it->second ==this) {
7679
env()->hash_to_module_map.erase(it);

‎src/module_wrap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ enum HostDefinedOptions : int {
3333
classModuleWrap :publicBaseObject {
3434
public:
3535
enum InternalFields {
36-
kModuleWrapBaseField = BaseObject::kInternalFieldCount,
36+
kModuleSlot = BaseObject::kInternalFieldCount,
3737
kURLSlot,
3838
kSyntheticEvaluationStepsSlot,
3939
kContextObjectSlot,// Object whose creation context is the target Context
@@ -106,6 +106,7 @@ class ModuleWrap : public BaseObject {
106106
contextify::ContextifyContext* contextify_context_ =nullptr;
107107
bool synthetic_ =false;
108108
bool linked_ =false;
109+
int module_hash_;
109110
};
110111

111112
}// namespace loader

‎src/node_contextify.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,11 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
871871
"ContextifyScript::New");
872872
return;
873873
}
874+
874875
contextify_script->script_.Reset(isolate, v8_script);
876+
contextify_script->script_.SetWeak();
877+
contextify_script->object()->SetInternalFieldForNodeCore(kUnboundScriptSlot,
878+
v8_script);
875879

876880
std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
877881
if (produce_cached_data) {

‎src/node_contextify.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ class ContextifyContext : public BaseObject {
149149

150150
classContextifyScript :publicBaseObject {
151151
public:
152+
enum InternalFields {
153+
kUnboundScriptSlot = BaseObject::kInternalFieldCount,
154+
kInternalFieldCount
155+
};
156+
152157
SET_NO_MEMORY_INFO()
153158
SET_MEMORY_INFO_NAME(ContextifyScript)
154159
SET_SELF_SIZE(ContextifyScript)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Flags: --max-old-space-size=16 --trace-gc
2+
'use strict';
3+
4+
// This tests that vm.Script with dynamic import callback does not leak.
5+
// See: https://github.com/nodejs/node/issues/33439
6+
require('../common');
7+
constvm=require('vm');
8+
letcount=0;
9+
10+
functionmain(){
11+
// Try to reach the maximum old space size.
12+
newvm.Script(`"${Math.random().toString().repeat(512)}";`,{
13+
asyncimportModuleDynamically(){},
14+
});
15+
if(count++<2*1024){
16+
setTimeout(main,1);
17+
}
18+
}
19+
main();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Flags: --experimental-vm-modules --max-old-space-size=16 --trace-gc
2+
'use strict';
3+
4+
// This tests that vm.SourceTextModule() does not leak.
5+
// See: https://github.com/nodejs/node/issues/33439
6+
require('../common');
7+
8+
constvm=require('vm');
9+
letcount=0;
10+
asyncfunctioncreateModule(){
11+
// Try to reach the maximum old space size.
12+
constm=newvm.SourceTextModule(`
13+
const bar = new Array(512).fill("----");
14+
export { bar };
15+
`);
16+
awaitm.link(()=>{});
17+
awaitm.evaluate();
18+
if(count++<4096){
19+
setTimeout(createModule,1);
20+
}
21+
returnm;
22+
}
23+
createModule();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp