Commit a86a2e1
module: use symbol in WeakMap to manage host defined options
Previously when managing the importModuleDynamically callback of
vm.compileFunction(), we use an ID number as the host defined option
and maintain a per-Environment ID -> CompiledFnEntry map to retain
the top-level referrer function returned by vm.compileFunction() in
order to pass it back to the callback, but it would leak because with
how we used v8::Persistent to maintain this reference, V8 would not
be able to understand the cycle and would just think that the
CompiledFnEntry was supposed to live forever. We made an attempt
to make that reference known to V8 by making the CompiledFnEntry weak
and using a private symbol to make CompiledFnEntry strongly
references the top-level referrer function in
#46785, but that turned out to be
unsound, because the there's no guarantee that the top-level function
must be alive while import() can still be initiated from that
function, since V8 could discard the top-level function and only keep
inner functions alive, so relying on the top-level function to keep
the CompiledFnEntry alive could result in use-after-free which caused
a revert of that fix.
With this patch we use a symbol in the host defined options instead of
a number, because with the stage-3 symbol-as-weakmap-keys proposal
we could directly use that symbol to keep the referrer alive using a
WeakMap. As a bonus this also keeps the other kinds of referrers
alive as long as import() can still be initiated from that
Script/Module, so this also fixes the long-standing crash caused by
vm.Script being GC'ed too early when its importModuleDynamically
callback still needs it.
PR-URL: #48510
Backport-PR-URL: #51004
Refs: #44211
Refs: #42080
Refs: #47096
Refs: #43205
Refs: #38695
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>1 parent 2c2892b commit a86a2e1Copy full SHA for a86a2e1
File tree
Expand file treeCollapse file tree
16 files changed
+185
-191
lines changedOpen diff view settings
Filter options
- lib
- internal
- modules/esm
- vm
- src
- test/es-module
Expand file treeCollapse file tree
16 files changed
+185
-191
lines changedOpen diff view settings
Collapse file
lib/internal/modules/esm/create_dynamic_module.js
Copy file name to clipboardExpand all lines: lib/internal/modules/esm/create_dynamic_module.js+3-2Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
69 | 69 | |
70 | 70 | |
71 | 71 | |
72 | | - |
73 | | - |
| 72 | + |
| 73 | + |
| 74 | + |
74 | 75 | |
75 | 76 | |
76 | 77 | |
|
Collapse file
lib/internal/modules/esm/loader.js
Copy file name to clipboardExpand all lines: lib/internal/modules/esm/loader.js+3-2Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
210 | 210 | |
211 | 211 | |
212 | 212 | |
213 | | - |
| 213 | + |
214 | 214 | |
215 | | - |
| 215 | + |
| 216 | + |
216 | 217 | |
217 | 218 | |
218 | 219 | |
|
Collapse file
lib/internal/modules/esm/translators.js
Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js+3-2Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
150 | 150 | |
151 | 151 | |
152 | 152 | |
153 | | - |
154 | | - |
| 153 | + |
| 154 | + |
| 155 | + |
155 | 156 | |
156 | 157 | |
157 | 158 | |
|
Collapse file
lib/internal/modules/esm/utils.js
Copy file name to clipboardExpand all lines: lib/internal/modules/esm/utils.js+69-20Lines changed: 69 additions & 20 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
7 | 7 | |
8 | 8 | |
9 | 9 | |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | + |
10 | 15 | |
11 | 16 | |
12 | 17 | |
| ||
21 | 26 | |
22 | 27 | |
23 | 28 | |
24 | | - |
25 | | - |
26 | | - |
27 | 29 | |
28 | 30 | |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | 31 | |
35 | 32 | |
36 | 33 | |
| ||
83 | 80 | |
84 | 81 | |
85 | 82 | |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
86 | 136 | |
87 | 137 | |
88 | | - |
| 138 | + |
89 | 139 | |
90 | 140 | |
91 | | - |
92 | | - |
93 | | - |
| 141 | + |
| 142 | + |
| 143 | + |
94 | 144 | |
95 | | - |
| 145 | + |
96 | 146 | |
97 | 147 | |
98 | 148 | |
99 | 149 | |
100 | 150 | |
101 | 151 | |
102 | | - |
| 152 | + |
103 | 153 | |
104 | 154 | |
105 | 155 | |
106 | 156 | |
107 | 157 | |
108 | | - |
109 | | - |
110 | | - |
| 158 | + |
| 159 | + |
| 160 | + |
111 | 161 | |
112 | | - |
113 | | - |
| 162 | + |
114 | 163 | |
115 | 164 | |
116 | 165 | |
| ||
176 | 225 | |
177 | 226 | |
178 | 227 | |
179 | | - |
| 228 | + |
180 | 229 | |
181 | 230 | |
182 | 231 | |
|
Collapse file
+4-3Lines changed: 4 additions & 3 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
100 | 100 | |
101 | 101 | |
102 | 102 | |
103 | | - |
104 | | - |
105 | | - |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + |
106 | 107 | |
107 | 108 | |
108 | 109 | |
|
Collapse file
+9-7Lines changed: 9 additions & 7 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
12 | 12 | |
13 | 13 | |
14 | 14 | |
15 | | - |
16 | 15 | |
17 | 16 | |
18 | 17 | |
| ||
70 | 69 | |
71 | 70 | |
72 | 71 | |
73 | | - |
74 | 72 | |
75 | 73 | |
76 | 74 | |
| ||
121 | 119 | |
122 | 120 | |
123 | 121 | |
| 122 | + |
124 | 123 | |
125 | 124 | |
126 | 125 | |
127 | 126 | |
128 | | - |
129 | | - |
| 127 | + |
| 128 | + |
130 | 129 | |
131 | 130 | |
132 | 131 | |
133 | 132 | |
134 | | - |
| 133 | + |
135 | 134 | |
136 | 135 | |
137 | 136 | |
138 | 137 | |
139 | 138 | |
140 | 139 | |
141 | 140 | |
142 | | - |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
143 | 146 | |
144 | 147 | |
145 | 148 | |
| ||
446 | 449 | |
447 | 450 | |
448 | 451 | |
449 | | - |
450 | 452 | |
Collapse file
+3-2Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
106 | 106 | |
107 | 107 | |
108 | 108 | |
109 | | - |
110 | | - |
| 109 | + |
| 110 | + |
| 111 | + |
111 | 112 | |
112 | 113 | |
113 | 114 | |
|
Collapse file
-10Lines changed: 0 additions & 10 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
345 | 345 | |
346 | 346 | |
347 | 347 | |
348 | | - |
349 | | - |
350 | | - |
351 | | - |
352 | | - |
353 | | - |
354 | | - |
355 | | - |
356 | | - |
357 | | - |
358 | 348 | |
359 | 349 | |
360 | 350 | |
|
Collapse file
-8Lines changed: 0 additions & 8 deletions
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
698 | 698 | |
699 | 699 | |
700 | 700 | |
701 | | - |
702 | | - |
703 | | - |
704 | | - |
705 | | - |
706 | | - |
707 | | - |
708 | | - |
709 | 701 | |
710 | 702 | |
711 | 703 | |
|
Collapse file
+1-1Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
21 | 21 | |
22 | 22 | |
23 | 23 | |
| 24 | + |
24 | 25 | |
25 | 26 | |
26 | 27 | |
| ||
337 | 338 | |
338 | 339 | |
339 | 340 | |
340 | | - |
341 | 341 | |
342 | 342 | |
343 | 343 | |
|
0 commit comments