1 |
|
|
#include "node.h" |
2 |
|
|
#include "node_context_data.h" |
3 |
|
|
#include "node_errors.h" |
4 |
|
|
#include "node_internals.h" |
5 |
|
|
#include "node_native_module.h" |
6 |
|
|
#include "node_options-inl.h" |
7 |
|
|
#include "node_platform.h" |
8 |
|
|
#include "node_shadow_realm.h" |
9 |
|
|
#include "node_v8_platform-inl.h" |
10 |
|
|
#include "node_wasm_web_api.h" |
11 |
|
|
#include "uv.h" |
12 |
|
|
|
13 |
|
|
#if HAVE_INSPECTOR |
14 |
|
|
#include "inspector/worker_inspector.h" // ParentInspectorHandle |
15 |
|
|
#endif |
16 |
|
|
|
17 |
|
|
namespace node { |
18 |
|
|
using errors::TryCatchScope; |
19 |
|
|
using v8::Array; |
20 |
|
|
using v8::Context; |
21 |
|
|
using v8::EscapableHandleScope; |
22 |
|
|
using v8::Function; |
23 |
|
|
using v8::FunctionCallbackInfo; |
24 |
|
|
using v8::HandleScope; |
25 |
|
|
using v8::Isolate; |
26 |
|
|
using v8::Just; |
27 |
|
|
using v8::Local; |
28 |
|
|
using v8::Maybe; |
29 |
|
|
using v8::MaybeLocal; |
30 |
|
|
using v8::Nothing; |
31 |
|
|
using v8::Null; |
32 |
|
|
using v8::Object; |
33 |
|
|
using v8::ObjectTemplate; |
34 |
|
|
using v8::Private; |
35 |
|
|
using v8::PropertyDescriptor; |
36 |
|
|
using v8::SealHandleScope; |
37 |
|
|
using v8::String; |
38 |
|
|
using v8::Value; |
39 |
|
|
|
40 |
|
371 |
bool AllowWasmCodeGenerationCallback(Local<Context> context, |
41 |
|
|
Local<String>) { |
42 |
|
|
Local<Value> wasm_code_gen = |
43 |
|
742 |
context->GetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration); |
44 |
✓✗✓✓
|
1113 |
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue(); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
33 |
bool ShouldAbortOnUncaughtException(Isolate* isolate) { |
48 |
|
33 |
DebugSealHandleScope scope(isolate); |
49 |
|
33 |
Environment* env = Environment::GetCurrent(isolate); |
50 |
✓✓ |
33 |
return env != nullptr && |
51 |
✓✗✓✓
|
68 |
(env->is_main_thread() || !env->is_stopping()) && |
52 |
✓✓ |
63 |
env->abort_on_uncaught_exception() && |
53 |
✓✗ |
96 |
env->should_abort_on_uncaught_toggle()[0] && |
54 |
✗✓ |
34 |
!env->inside_should_not_abort_on_uncaught_scope(); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
69110 |
MaybeLocal<Value> PrepareStackTraceCallback(Local<Context> context, |
58 |
|
|
Local<Value> exception, |
59 |
|
|
Local<Array> trace) { |
60 |
|
69110 |
Environment* env = Environment::GetCurrent(context); |
61 |
✗✓ |
69110 |
if (env == nullptr) { |
62 |
|
|
return exception->ToString(context).FromMaybe(Local<Value>()); |
63 |
|
|
} |
64 |
|
69110 |
Local<Function> prepare = env->prepare_stack_trace_callback(); |
65 |
✗✓ |
69110 |
if (prepare.IsEmpty()) { |
66 |
|
|
return exception->ToString(context).FromMaybe(Local<Value>()); |
67 |
|
|
} |
68 |
|
|
Local<Value> args[] = { |
69 |
|
|
context->Global(), |
70 |
|
|
exception, |
71 |
|
|
trace, |
72 |
|
138220 |
}; |
73 |
|
|
// This TryCatch + Rethrow is required by V8 due to details around exception |
74 |
|
|
// handling there. For C++ callbacks, V8 expects a scheduled exception (which |
75 |
|
|
// is what ReThrow gives us). Just returning the empty MaybeLocal would leave |
76 |
|
|
// us with a pending exception. |
77 |
|
69110 |
TryCatchScope try_catch(env); |
78 |
|
|
MaybeLocal<Value> result = prepare->Call( |
79 |
|
138220 |
context, Undefined(env->isolate()), arraysize(args), args); |
80 |
✓✓✓✗ ✓✓ |
69110 |
if (try_catch.HasCaught() && !try_catch.HasTerminated()) { |
81 |
|
2 |
try_catch.ReThrow(); |
82 |
|
|
} |
83 |
|
69110 |
return result; |
84 |
|
|
} |
85 |
|
|
|
86 |
|
397619 |
void* NodeArrayBufferAllocator::Allocate(size_t size) { |
87 |
|
|
void* ret; |
88 |
✓✓✓✓ ✓✓ |
397619 |
if (zero_fill_field_ || per_process::cli_options->zero_fill_all_buffers) |
89 |
|
70384 |
ret = allocator_->Allocate(size); |
90 |
|
|
else |
91 |
|
327235 |
ret = allocator_->AllocateUninitialized(size); |
92 |
✓✗ |
397619 |
if (LIKELY(ret != nullptr)) |
93 |
|
397619 |
total_mem_usage_.fetch_add(size, std::memory_order_relaxed); |
94 |
|
397619 |
return ret; |
95 |
|
|
} |
96 |
|
|
|
97 |
|
162547 |
void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) { |
98 |
|
162547 |
void* ret = allocator_->AllocateUninitialized(size); |
99 |
✓✗ |
162547 |
if (LIKELY(ret != nullptr)) |
100 |
|
162547 |
total_mem_usage_.fetch_add(size, std::memory_order_relaxed); |
101 |
|
162547 |
return ret; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
64824 |
void* NodeArrayBufferAllocator::Reallocate( |
105 |
|
|
void* data, size_t old_size, size_t size) { |
106 |
|
64824 |
void* ret = allocator_->Reallocate(data, old_size, size); |
107 |
✗✓✗✗
|
64824 |
if (LIKELY(ret != nullptr) || UNLIKELY(size == 0)) |
108 |
|
64824 |
total_mem_usage_.fetch_add(size - old_size, std::memory_order_relaxed); |
109 |
|
64824 |
return ret; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
542093 |
void NodeArrayBufferAllocator::Free(void* data, size_t size) { |
113 |
|
542093 |
total_mem_usage_.fetch_sub(size, std::memory_order_relaxed); |
114 |
|
542093 |
allocator_->Free(data, size); |
115 |
|
542093 |
} |
116 |
|
|
|
117 |
|
12 |
DebuggingArrayBufferAllocator::~DebuggingArrayBufferAllocator() { |
118 |
✗✓ |
6 |
CHECK(allocations_.empty()); |
119 |
|
12 |
} |
120 |
|
|
|
121 |
|
78 |
void* DebuggingArrayBufferAllocator::Allocate(size_t size) { |
122 |
|
78 |
Mutex::ScopedLock lock(mutex_); |
123 |
|
78 |
void* data = NodeArrayBufferAllocator::Allocate(size); |
124 |
|
78 |
RegisterPointerInternal(data, size); |
125 |
|
78 |
return data; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
41 |
void* DebuggingArrayBufferAllocator::AllocateUninitialized(size_t size) { |
129 |
|
41 |
Mutex::ScopedLock lock(mutex_); |
130 |
|
41 |
void* data = NodeArrayBufferAllocator::AllocateUninitialized(size); |
131 |
|
41 |
RegisterPointerInternal(data, size); |
132 |
|
41 |
return data; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
119 |
void DebuggingArrayBufferAllocator::Free(void* data, size_t size) { |
136 |
|
238 |
Mutex::ScopedLock lock(mutex_); |
137 |
|
119 |
UnregisterPointerInternal(data, size); |
138 |
|
119 |
NodeArrayBufferAllocator::Free(data, size); |
139 |
|
119 |
} |
140 |
|
|
|
141 |
|
|
void* DebuggingArrayBufferAllocator::Reallocate(void* data, |
142 |
|
|
size_t old_size, |
143 |
|
|
size_t size) { |
144 |
|
|
Mutex::ScopedLock lock(mutex_); |
145 |
|
|
void* ret = NodeArrayBufferAllocator::Reallocate(data, old_size, size); |
146 |
|
|
if (ret == nullptr) { |
147 |
|
|
if (size == 0) { // i.e. equivalent to free(). |
148 |
|
|
// suppress coverity warning as data is used as key versus as pointer |
149 |
|
|
// in UnregisterPointerInternal |
150 |
|
|
// coverity[pass_freed_arg] |
151 |
|
|
UnregisterPointerInternal(data, old_size); |
152 |
|
|
} |
153 |
|
|
return nullptr; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
if (data != nullptr) { |
157 |
|
|
auto it = allocations_.find(data); |
158 |
|
|
CHECK_NE(it, allocations_.end()); |
159 |
|
|
allocations_.erase(it); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
RegisterPointerInternal(ret, size); |
163 |
|
|
return ret; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
void DebuggingArrayBufferAllocator::RegisterPointer(void* data, size_t size) { |
167 |
|
|
Mutex::ScopedLock lock(mutex_); |
168 |
|
|
NodeArrayBufferAllocator::RegisterPointer(data, size); |
169 |
|
|
RegisterPointerInternal(data, size); |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
void DebuggingArrayBufferAllocator::UnregisterPointer(void* data, size_t size) { |
173 |
|
|
Mutex::ScopedLock lock(mutex_); |
174 |
|
|
NodeArrayBufferAllocator::UnregisterPointer(data, size); |
175 |
|
|
UnregisterPointerInternal(data, size); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
119 |
void DebuggingArrayBufferAllocator::UnregisterPointerInternal(void* data, |
179 |
|
|
size_t size) { |
180 |
✗✓ |
119 |
if (data == nullptr) return; |
181 |
|
119 |
auto it = allocations_.find(data); |
182 |
✗✓ |
119 |
CHECK_NE(it, allocations_.end()); |
183 |
✓✗ |
119 |
if (size > 0) { |
184 |
|
|
// We allow allocations with size 1 for 0-length buffers to avoid having |
185 |
|
|
// to deal with nullptr values. |
186 |
✗✓ |
119 |
CHECK_EQ(it->second, size); |
187 |
|
|
} |
188 |
|
119 |
allocations_.erase(it); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
119 |
void DebuggingArrayBufferAllocator::RegisterPointerInternal(void* data, |
192 |
|
|
size_t size) { |
193 |
✗✓ |
119 |
if (data == nullptr) return; |
194 |
✗✓ |
119 |
CHECK_EQ(allocations_.count(data), 0); |
195 |
|
119 |
allocations_[data] = size; |
196 |
|
|
} |
197 |
|
|
|
198 |
|
6603 |
std::unique_ptr<ArrayBufferAllocator> ArrayBufferAllocator::Create(bool debug) { |
199 |
✓✗✓✓ ✓✓ |
6603 |
if (debug || per_process::cli_options->debug_arraybuffer_allocations) |
200 |
|
3 |
return std::make_unique<DebuggingArrayBufferAllocator>(); |
201 |
|
|
else |
202 |
|
6600 |
return std::make_unique<NodeArrayBufferAllocator>(); |
203 |
|
|
} |
204 |
|
|
|
205 |
|
57 |
ArrayBufferAllocator* CreateArrayBufferAllocator() { |
206 |
|
57 |
return ArrayBufferAllocator::Create().release(); |
207 |
|
|
} |
208 |
|
|
|
209 |
|
57 |
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) { |
210 |
✓✗ |
57 |
delete allocator; |
211 |
|
57 |
} |
212 |
|
|
|
213 |
|
6601 |
void SetIsolateCreateParamsForNode(Isolate::CreateParams* params) { |
214 |
|
6601 |
const uint64_t constrained_memory = uv_get_constrained_memory(); |
215 |
✓✓ |
13198 |
const uint64_t total_memory = constrained_memory > 0 ? |
216 |
|
6597 |
std::min(uv_get_total_memory(), constrained_memory) : |
217 |
|
4 |
uv_get_total_memory(); |
218 |
✓✗ |
6601 |
if (total_memory > 0) { |
219 |
|
|
// V8 defaults to 700MB or 1.4GB on 32 and 64 bit platforms respectively. |
220 |
|
|
// This default is based on browser use-cases. Tell V8 to configure the |
221 |
|
|
// heap based on the actual physical memory. |
222 |
|
6601 |
params->constraints.ConfigureDefaults(total_memory, 0); |
223 |
|
|
} |
224 |
|
6601 |
params->embedder_wrapper_object_index = BaseObject::InternalFields::kSlot; |
225 |
|
6601 |
params->embedder_wrapper_type_index = std::numeric_limits<int>::max(); |
226 |
|
6601 |
} |
227 |
|
|
|
228 |
|
6601 |
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s) { |
229 |
✓✗ |
6601 |
if (s.flags & MESSAGE_LISTENER_WITH_ERROR_LEVEL) |
230 |
|
6601 |
isolate->AddMessageListenerWithErrorLevel( |
231 |
|
|
errors::PerIsolateMessageListener, |
232 |
|
|
Isolate::MessageErrorLevel::kMessageError | |
233 |
|
|
Isolate::MessageErrorLevel::kMessageWarning); |
234 |
|
|
|
235 |
✗✓ |
6601 |
auto* abort_callback = s.should_abort_on_uncaught_exception_callback ? |
236 |
|
|
s.should_abort_on_uncaught_exception_callback : |
237 |
|
|
ShouldAbortOnUncaughtException; |
238 |
|
6601 |
isolate->SetAbortOnUncaughtExceptionCallback(abort_callback); |
239 |
|
|
|
240 |
✗✓ |
6601 |
auto* fatal_error_cb = s.fatal_error_callback ? |
241 |
|
|
s.fatal_error_callback : OnFatalError; |
242 |
|
6601 |
isolate->SetFatalErrorHandler(fatal_error_cb); |
243 |
|
|
|
244 |
✓✗ |
6601 |
if ((s.flags & SHOULD_NOT_SET_PREPARE_STACK_TRACE_CALLBACK) == 0) { |
245 |
✗✓ |
6601 |
auto* prepare_stack_trace_cb = s.prepare_stack_trace_callback ? |
246 |
|
|
s.prepare_stack_trace_callback : PrepareStackTraceCallback; |
247 |
|
6601 |
isolate->SetPrepareStackTraceCallback(prepare_stack_trace_cb); |
248 |
|
|
} |
249 |
|
6601 |
} |
250 |
|
|
|
251 |
|
6607 |
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s) { |
252 |
|
6607 |
isolate->SetMicrotasksPolicy(s.policy); |
253 |
|
|
|
254 |
✗✓ |
6607 |
auto* allow_wasm_codegen_cb = s.allow_wasm_code_generation_callback ? |
255 |
|
|
s.allow_wasm_code_generation_callback : AllowWasmCodeGenerationCallback; |
256 |
|
6607 |
isolate->SetAllowWasmCodeGenerationCallback(allow_wasm_codegen_cb); |
257 |
|
6607 |
isolate->SetModifyCodeGenerationFromStringsCallback( |
258 |
|
|
ModifyCodeGenerationFromStrings); |
259 |
|
|
|
260 |
|
13214 |
Mutex::ScopedLock lock(node::per_process::cli_options_mutex); |
261 |
|
6607 |
if (per_process::cli_options->get_per_isolate_options() |
262 |
|
6607 |
->get_per_env_options() |
263 |
✓✓ |
6607 |
->experimental_fetch) { |
264 |
|
6606 |
isolate->SetWasmStreamingCallback(wasm_web_api::StartStreamingCompilation); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
6607 |
if (per_process::cli_options->get_per_isolate_options() |
268 |
✓✓ |
6607 |
->experimental_shadow_realm) { |
269 |
|
1 |
isolate->SetHostCreateShadowRealmContextCallback( |
270 |
|
|
shadow_realm::HostCreateShadowRealmContextCallback); |
271 |
|
|
} |
272 |
|
|
|
273 |
✓✗ |
6607 |
if ((s.flags & SHOULD_NOT_SET_PROMISE_REJECTION_CALLBACK) == 0) { |
274 |
✗✓ |
6607 |
auto* promise_reject_cb = s.promise_reject_callback ? |
275 |
|
|
s.promise_reject_callback : PromiseRejectCallback; |
276 |
|
6607 |
isolate->SetPromiseRejectCallback(promise_reject_cb); |
277 |
|
|
} |
278 |
|
|
|
279 |
✓✗ |
6607 |
if (s.flags & DETAILED_SOURCE_POSITIONS_FOR_PROFILING) |
280 |
|
6607 |
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate); |
281 |
|
6607 |
} |
282 |
|
|
|
283 |
|
1318 |
void SetIsolateUpForNode(v8::Isolate* isolate, |
284 |
|
|
const IsolateSettings& settings) { |
285 |
|
1318 |
SetIsolateErrorHandlers(isolate, settings); |
286 |
|
1318 |
SetIsolateMiscHandlers(isolate, settings); |
287 |
|
1318 |
} |
288 |
|
|
|
289 |
|
1318 |
void SetIsolateUpForNode(v8::Isolate* isolate) { |
290 |
|
1318 |
IsolateSettings settings; |
291 |
|
1318 |
SetIsolateUpForNode(isolate, settings); |
292 |
|
1318 |
} |
293 |
|
|
|
294 |
|
|
// TODO(joyeecheung): we may want to expose this, but then we need to be |
295 |
|
|
// careful about what we override in the params. |
296 |
|
63 |
Isolate* NewIsolate(Isolate::CreateParams* params, |
297 |
|
|
uv_loop_t* event_loop, |
298 |
|
|
MultiIsolatePlatform* platform) { |
299 |
|
63 |
Isolate* isolate = Isolate::Allocate(); |
300 |
✗✓ |
63 |
if (isolate == nullptr) return nullptr; |
301 |
|
|
|
302 |
|
|
// Register the isolate on the platform before the isolate gets initialized, |
303 |
|
|
// so that the isolate can access the platform during initialization. |
304 |
|
63 |
platform->RegisterIsolate(isolate, event_loop); |
305 |
|
|
|
306 |
|
63 |
SetIsolateCreateParamsForNode(params); |
307 |
|
63 |
Isolate::Initialize(isolate, *params); |
308 |
|
63 |
SetIsolateUpForNode(isolate); |
309 |
|
|
|
310 |
|
63 |
return isolate; |
311 |
|
|
} |
312 |
|
|
|
313 |
|
55 |
Isolate* NewIsolate(ArrayBufferAllocator* allocator, |
314 |
|
|
uv_loop_t* event_loop, |
315 |
|
|
MultiIsolatePlatform* platform) { |
316 |
|
110 |
Isolate::CreateParams params; |
317 |
✓✗ |
55 |
if (allocator != nullptr) params.array_buffer_allocator = allocator; |
318 |
|
55 |
return NewIsolate(¶ms, event_loop, platform); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
8 |
Isolate* NewIsolate(std::shared_ptr<ArrayBufferAllocator> allocator, |
322 |
|
|
uv_loop_t* event_loop, |
323 |
|
|
MultiIsolatePlatform* platform) { |
324 |
|
16 |
Isolate::CreateParams params; |
325 |
✓✗ |
8 |
if (allocator) params.array_buffer_allocator_shared = allocator; |
326 |
|
8 |
return NewIsolate(¶ms, event_loop, platform); |
327 |
|
|
} |
328 |
|
|
|
329 |
|
1303 |
IsolateData* CreateIsolateData(Isolate* isolate, |
330 |
|
|
uv_loop_t* loop, |
331 |
|
|
MultiIsolatePlatform* platform, |
332 |
|
|
ArrayBufferAllocator* allocator) { |
333 |
|
1303 |
return new IsolateData(isolate, loop, platform, allocator); |
334 |
|
|
} |
335 |
|
|
|
336 |
|
1301 |
void FreeIsolateData(IsolateData* isolate_data) { |
337 |
✓✗ |
1301 |
delete isolate_data; |
338 |
|
1301 |
} |
339 |
|
|
|
340 |
|
2970 |
InspectorParentHandle::~InspectorParentHandle() {} |
341 |
|
|
|
342 |
|
|
// Hide the internal handle class from the public API. |
343 |
|
|
#if HAVE_INSPECTOR |
344 |
|
|
struct InspectorParentHandleImpl : public InspectorParentHandle { |
345 |
|
|
std::unique_ptr<inspector::ParentInspectorHandle> impl; |
346 |
|
|
|
347 |
|
1485 |
explicit InspectorParentHandleImpl( |
348 |
|
|
std::unique_ptr<inspector::ParentInspectorHandle>&& impl) |
349 |
|
1485 |
: impl(std::move(impl)) {} |
350 |
|
|
}; |
351 |
|
|
#endif |
352 |
|
|
|
353 |
|
1295 |
Environment* CreateEnvironment( |
354 |
|
|
IsolateData* isolate_data, |
355 |
|
|
Local<Context> context, |
356 |
|
|
const std::vector<std::string>& args, |
357 |
|
|
const std::vector<std::string>& exec_args, |
358 |
|
|
EnvironmentFlags::Flags flags, |
359 |
|
|
ThreadId thread_id, |
360 |
|
|
std::unique_ptr<InspectorParentHandle> inspector_parent_handle) { |
361 |
|
1295 |
Isolate* isolate = context->GetIsolate(); |
362 |
|
2590 |
HandleScope handle_scope(isolate); |
363 |
|
1295 |
Context::Scope context_scope(context); |
364 |
|
|
// TODO(addaleax): This is a much better place for parsing per-Environment |
365 |
|
|
// options than the global parse call. |
366 |
|
|
Environment* env = new Environment( |
367 |
|
1295 |
isolate_data, context, args, exec_args, nullptr, flags, thread_id); |
368 |
|
|
#if HAVE_INSPECTOR |
369 |
✓✗ |
1295 |
if (env->should_create_inspector()) { |
370 |
✓✓ |
1295 |
if (inspector_parent_handle) { |
371 |
|
1248 |
env->InitializeInspector( |
372 |
|
|
std::move(static_cast<InspectorParentHandleImpl*>( |
373 |
|
1248 |
inspector_parent_handle.get())->impl)); |
374 |
|
|
} else { |
375 |
|
47 |
env->InitializeInspector({}); |
376 |
|
|
} |
377 |
|
|
} |
378 |
|
|
#endif |
379 |
|
|
|
380 |
✗✓ |
2590 |
if (env->RunBootstrapping().IsEmpty()) { |
381 |
|
|
FreeEnvironment(env); |
382 |
|
|
return nullptr; |
383 |
|
|
} |
384 |
|
|
|
385 |
|
1295 |
return env; |
386 |
|
|
} |
387 |
|
|
|
388 |
|
6061 |
void FreeEnvironment(Environment* env) { |
389 |
|
6061 |
Isolate* isolate = env->isolate(); |
390 |
|
|
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate, |
391 |
|
12122 |
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); |
392 |
|
|
{ |
393 |
|
12122 |
HandleScope handle_scope(isolate); // For env->context(). |
394 |
|
6061 |
Context::Scope context_scope(env->context()); |
395 |
|
12122 |
SealHandleScope seal_handle_scope(isolate); |
396 |
|
|
|
397 |
|
6061 |
env->set_stopping(true); |
398 |
|
6061 |
env->stop_sub_worker_contexts(); |
399 |
|
6061 |
env->RunCleanup(); |
400 |
|
6061 |
RunAtExit(env); |
401 |
|
|
} |
402 |
|
|
|
403 |
|
|
// This call needs to be made while the `Environment` is still alive |
404 |
|
|
// because we assume that it is available for async tracking in the |
405 |
|
|
// NodePlatform implementation. |
406 |
|
6061 |
MultiIsolatePlatform* platform = env->isolate_data()->platform(); |
407 |
✓✗ |
6061 |
if (platform != nullptr) |
408 |
|
6061 |
platform->DrainTasks(isolate); |
409 |
|
|
|
410 |
✓✗ |
6061 |
delete env; |
411 |
|
6061 |
} |
412 |
|
|
|
413 |
|
1485 |
NODE_EXTERN std::unique_ptr<InspectorParentHandle> GetInspectorParentHandle( |
414 |
|
|
Environment* env, |
415 |
|
|
ThreadId thread_id, |
416 |
|
|
const char* url) { |
417 |
✗✓ |
1485 |
CHECK_NOT_NULL(env); |
418 |
✗✓ |
1485 |
CHECK_NE(thread_id.id, static_cast<uint64_t>(-1)); |
419 |
|
|
#if HAVE_INSPECTOR |
420 |
|
2970 |
return std::make_unique<InspectorParentHandleImpl>( |
421 |
|
4455 |
env->inspector_agent()->GetParentHandle(thread_id.id, url)); |
422 |
|
|
#else |
423 |
|
|
return {}; |
424 |
|
|
#endif |
425 |
|
|
} |
426 |
|
|
|
427 |
|
6548 |
MaybeLocal<Value> LoadEnvironment( |
428 |
|
|
Environment* env, |
429 |
|
|
StartExecutionCallback cb) { |
430 |
|
6548 |
env->InitializeLibuv(); |
431 |
|
6548 |
env->InitializeDiagnostics(); |
432 |
|
|
|
433 |
|
6548 |
return StartExecution(env, cb); |
434 |
|
|
} |
435 |
|
|
|
436 |
|
17 |
MaybeLocal<Value> LoadEnvironment( |
437 |
|
|
Environment* env, |
438 |
|
|
const char* main_script_source_utf8) { |
439 |
✗✓ |
17 |
CHECK_NOT_NULL(main_script_source_utf8); |
440 |
|
17 |
Isolate* isolate = env->isolate(); |
441 |
|
|
return LoadEnvironment( |
442 |
|
|
env, |
443 |
|
17 |
[&](const StartExecutionCallbackInfo& info) -> MaybeLocal<Value> { |
444 |
|
|
// This is a slightly hacky way to convert UTF-8 to UTF-16. |
445 |
|
|
Local<String> str = |
446 |
|
34 |
String::NewFromUtf8(isolate, |
447 |
|
17 |
main_script_source_utf8).ToLocalChecked(); |
448 |
|
32 |
auto main_utf16 = std::make_unique<String::Value>(isolate, str); |
449 |
|
|
|
450 |
|
|
// TODO(addaleax): Avoid having a global table for all scripts. |
451 |
|
32 |
std::string name = "embedder_main_" + std::to_string(env->thread_id()); |
452 |
|
17 |
native_module::NativeModuleLoader::Add( |
453 |
|
17 |
name.c_str(), UnionBytes(**main_utf16, main_utf16->length())); |
454 |
|
17 |
env->set_main_utf16(std::move(main_utf16)); |
455 |
|
|
std::vector<Local<String>> params = { |
456 |
|
17 |
env->process_string(), |
457 |
|
32 |
env->require_string()}; |
458 |
|
|
std::vector<Local<Value>> args = { |
459 |
|
|
env->process_object(), |
460 |
|
66 |
env->native_module_require()}; |
461 |
|
17 |
return ExecuteBootstrapper(env, name.c_str(), ¶ms, &args); |
462 |
|
17 |
}); |
463 |
|
|
} |
464 |
|
|
|
465 |
|
5 |
Environment* GetCurrentEnvironment(Local<Context> context) { |
466 |
|
5 |
return Environment::GetCurrent(context); |
467 |
|
|
} |
468 |
|
|
|
469 |
|
1 |
IsolateData* GetEnvironmentIsolateData(Environment* env) { |
470 |
|
1 |
return env->isolate_data(); |
471 |
|
|
} |
472 |
|
|
|
473 |
|
1 |
ArrayBufferAllocator* GetArrayBufferAllocator(IsolateData* isolate_data) { |
474 |
|
1 |
return isolate_data->node_allocator(); |
475 |
|
|
} |
476 |
|
|
|
477 |
|
6243 |
MultiIsolatePlatform* GetMultiIsolatePlatform(Environment* env) { |
478 |
|
6243 |
return GetMultiIsolatePlatform(env->isolate_data()); |
479 |
|
|
} |
480 |
|
|
|
481 |
|
6243 |
MultiIsolatePlatform* GetMultiIsolatePlatform(IsolateData* env) { |
482 |
|
6243 |
return env->platform(); |
483 |
|
|
} |
484 |
|
|
|
485 |
|
|
MultiIsolatePlatform* CreatePlatform( |
486 |
|
|
int thread_pool_size, |
487 |
|
|
node::tracing::TracingController* tracing_controller) { |
488 |
|
|
return CreatePlatform( |
489 |
|
|
thread_pool_size, |
490 |
|
|
static_cast<v8::TracingController*>(tracing_controller)); |
491 |
|
|
} |
492 |
|
|
|
493 |
|
|
MultiIsolatePlatform* CreatePlatform( |
494 |
|
|
int thread_pool_size, |
495 |
|
|
v8::TracingController* tracing_controller) { |
496 |
|
|
return MultiIsolatePlatform::Create(thread_pool_size, |
497 |
|
|
tracing_controller) |
498 |
|
|
.release(); |
499 |
|
|
} |
500 |
|
|
|
501 |
|
|
void FreePlatform(MultiIsolatePlatform* platform) { |
502 |
|
|
delete platform; |
503 |
|
|
} |
504 |
|
|
|
505 |
|
7 |
std::unique_ptr<MultiIsolatePlatform> MultiIsolatePlatform::Create( |
506 |
|
|
int thread_pool_size, |
507 |
|
|
v8::TracingController* tracing_controller, |
508 |
|
|
v8::PageAllocator* page_allocator) { |
509 |
|
14 |
return std::make_unique<NodePlatform>(thread_pool_size, |
510 |
|
|
tracing_controller, |
511 |
|
7 |
page_allocator); |
512 |
|
|
} |
513 |
|
|
|
514 |
|
40071 |
MaybeLocal<Object> GetPerContextExports(Local<Context> context) { |
515 |
|
40071 |
Isolate* isolate = context->GetIsolate(); |
516 |
|
40071 |
EscapableHandleScope handle_scope(isolate); |
517 |
|
|
|
518 |
|
40071 |
Local<Object> global = context->Global(); |
519 |
|
|
Local<Private> key = Private::ForApi(isolate, |
520 |
|
40071 |
FIXED_ONE_BYTE_STRING(isolate, "node:per_context_binding_exports")); |
521 |
|
|
|
522 |
|
|
Local<Value> existing_value; |
523 |
✗✓ |
80142 |
if (!global->GetPrivate(context, key).ToLocal(&existing_value)) |
524 |
|
|
return MaybeLocal<Object>(); |
525 |
✓✓ |
40071 |
if (existing_value->IsObject()) |
526 |
|
40003 |
return handle_scope.Escape(existing_value.As<Object>()); |
527 |
|
|
|
528 |
|
68 |
Local<Object> exports = Object::New(isolate); |
529 |
✓✗ |
272 |
if (context->Global()->SetPrivate(context, key, exports).IsNothing() || |
530 |
✗✓✗✓
|
204 |
InitializePrimordials(context).IsNothing()) |
531 |
|
|
return MaybeLocal<Object>(); |
532 |
|
68 |
return handle_scope.Escape(exports); |
533 |
|
|
} |
534 |
|
|
|
535 |
|
|
// Any initialization logic should be performed in |
536 |
|
|
// InitializeContext, because embedders don't necessarily |
537 |
|
|
// call NewContext and so they will experience breakages. |
538 |
|
62 |
Local<Context> NewContext(Isolate* isolate, |
539 |
|
|
Local<ObjectTemplate> object_template) { |
540 |
|
124 |
auto context = Context::New(isolate, nullptr, object_template); |
541 |
✗✓ |
62 |
if (context.IsEmpty()) return context; |
542 |
|
|
|
543 |
✗✓ |
124 |
if (InitializeContext(context).IsNothing()) { |
544 |
|
|
return Local<Context>(); |
545 |
|
|
} |
546 |
|
|
|
547 |
|
62 |
return context; |
548 |
|
|
} |
549 |
|
|
|
550 |
|
8 |
void ProtoThrower(const FunctionCallbackInfo<Value>& info) { |
551 |
|
8 |
THROW_ERR_PROTO_ACCESS(info.GetIsolate()); |
552 |
|
8 |
} |
553 |
|
|
|
554 |
|
|
// This runs at runtime, regardless of whether the context |
555 |
|
|
// is created from a snapshot. |
556 |
|
7219 |
Maybe<bool> InitializeContextRuntime(Local<Context> context) { |
557 |
|
7219 |
Isolate* isolate = context->GetIsolate(); |
558 |
|
14438 |
HandleScope handle_scope(isolate); |
559 |
|
|
|
560 |
|
|
// Delete `Intl.v8BreakIterator` |
561 |
|
|
// https://github.com/nodejs/node/issues/14909 |
562 |
|
|
{ |
563 |
|
|
Local<String> intl_string = |
564 |
|
7219 |
FIXED_ONE_BYTE_STRING(isolate, "Intl"); |
565 |
|
|
Local<String> break_iter_string = |
566 |
|
7219 |
FIXED_ONE_BYTE_STRING(isolate, "v8BreakIterator"); |
567 |
|
|
|
568 |
|
|
Local<Value> intl_v; |
569 |
|
7219 |
if (!context->Global() |
570 |
|
7219 |
->Get(context, intl_string) |
571 |
✗✓ |
7219 |
.ToLocal(&intl_v)) { |
572 |
|
|
return Nothing<bool>(); |
573 |
|
|
} |
574 |
|
|
|
575 |
✓✗ |
14438 |
if (intl_v->IsObject() && |
576 |
|
7219 |
intl_v.As<Object>() |
577 |
✗✓ |
14438 |
->Delete(context, break_iter_string) |
578 |
✗✓ |
7219 |
.IsNothing()) { |
579 |
|
|
return Nothing<bool>(); |
580 |
|
|
} |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
// Delete `Atomics.wake` |
584 |
|
|
// https://github.com/nodejs/node/issues/21219 |
585 |
|
|
{ |
586 |
|
|
Local<String> atomics_string = |
587 |
|
7219 |
FIXED_ONE_BYTE_STRING(isolate, "Atomics"); |
588 |
|
|
Local<String> wake_string = |
589 |
|
7219 |
FIXED_ONE_BYTE_STRING(isolate, "wake"); |
590 |
|
|
|
591 |
|
|
Local<Value> atomics_v; |
592 |
|
7219 |
if (!context->Global() |
593 |
|
7219 |
->Get(context, atomics_string) |
594 |
✗✓ |
7219 |
.ToLocal(&atomics_v)) { |
595 |
|
|
return Nothing<bool>(); |
596 |
|
|
} |
597 |
|
|
|
598 |
✓✓ |
14426 |
if (atomics_v->IsObject() && |
599 |
|
7207 |
atomics_v.As<Object>() |
600 |
✗✓ |
14426 |
->Delete(context, wake_string) |
601 |
✗✓ |
7207 |
.IsNothing()) { |
602 |
|
|
return Nothing<bool>(); |
603 |
|
|
} |
604 |
|
|
} |
605 |
|
|
|
606 |
|
|
// Remove __proto__ |
607 |
|
|
// https://github.com/nodejs/node/issues/31951 |
608 |
|
|
Local<Object> prototype; |
609 |
|
|
{ |
610 |
|
|
Local<String> object_string = |
611 |
|
7219 |
FIXED_ONE_BYTE_STRING(isolate, "Object"); |
612 |
|
|
Local<String> prototype_string = |
613 |
|
7219 |
FIXED_ONE_BYTE_STRING(isolate, "prototype"); |
614 |
|
|
|
615 |
|
|
Local<Value> object_v; |
616 |
|
7219 |
if (!context->Global() |
617 |
|
7219 |
->Get(context, object_string) |
618 |
✗✓ |
7219 |
.ToLocal(&object_v)) { |
619 |
|
|
return Nothing<bool>(); |
620 |
|
|
} |
621 |
|
|
|
622 |
|
|
Local<Value> prototype_v; |
623 |
|
7219 |
if (!object_v.As<Object>() |
624 |
|
7219 |
->Get(context, prototype_string) |
625 |
✗✓ |
7219 |
.ToLocal(&prototype_v)) { |
626 |
|
|
return Nothing<bool>(); |
627 |
|
|
} |
628 |
|
|
|
629 |
|
7219 |
prototype = prototype_v.As<Object>(); |
630 |
|
|
} |
631 |
|
|
|
632 |
|
|
Local<String> proto_string = |
633 |
|
7219 |
FIXED_ONE_BYTE_STRING(isolate, "__proto__"); |
634 |
|
|
|
635 |
✓✓ |
7219 |
if (per_process::cli_options->disable_proto == "delete") { |
636 |
|
4 |
if (prototype |
637 |
|
4 |
->Delete(context, proto_string) |
638 |
✗✓ |
4 |
.IsNothing()) { |
639 |
|
|
return Nothing<bool>(); |
640 |
|
|
} |
641 |
✓✓ |
7215 |
} else if (per_process::cli_options->disable_proto == "throw") { |
642 |
|
|
Local<Value> thrower; |
643 |
|
4 |
if (!Function::New(context, ProtoThrower) |
644 |
✗✓ |
4 |
.ToLocal(&thrower)) { |
645 |
|
|
return Nothing<bool>(); |
646 |
|
|
} |
647 |
|
|
|
648 |
|
4 |
PropertyDescriptor descriptor(thrower, thrower); |
649 |
|
4 |
descriptor.set_enumerable(false); |
650 |
|
4 |
descriptor.set_configurable(true); |
651 |
|
4 |
if (prototype |
652 |
|
4 |
->DefineProperty(context, proto_string, descriptor) |
653 |
✗✓ |
4 |
.IsNothing()) { |
654 |
|
|
return Nothing<bool>(); |
655 |
|
|
} |
656 |
✗✓ |
7211 |
} else if (per_process::cli_options->disable_proto != "") { |
657 |
|
|
// Validated in ProcessGlobalArgs |
658 |
|
|
FatalError("InitializeContextRuntime()", |
659 |
|
|
"invalid --disable-proto mode"); |
660 |
|
|
} |
661 |
|
|
|
662 |
|
7219 |
return Just(true); |
663 |
|
|
} |
664 |
|
|
|
665 |
|
63 |
Maybe<bool> InitializeContextForSnapshot(Local<Context> context) { |
666 |
|
63 |
Isolate* isolate = context->GetIsolate(); |
667 |
|
126 |
HandleScope handle_scope(isolate); |
668 |
|
|
|
669 |
|
63 |
context->AllowCodeGenerationFromStrings(false); |
670 |
|
126 |
context->SetEmbedderData( |
671 |
|
|
ContextEmbedderIndex::kAllowCodeGenerationFromStrings, True(isolate)); |
672 |
|
126 |
context->SetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration, |
673 |
|
|
True(isolate)); |
674 |
|
|
|
675 |
|
63 |
return InitializePrimordials(context); |
676 |
|
|
} |
677 |
|
|
|
678 |
|
131 |
Maybe<bool> InitializePrimordials(Local<Context> context) { |
679 |
|
|
// Run per-context JS files. |
680 |
|
131 |
Isolate* isolate = context->GetIsolate(); |
681 |
|
131 |
Context::Scope context_scope(context); |
682 |
|
|
Local<Object> exports; |
683 |
|
|
|
684 |
|
|
Local<String> primordials_string = |
685 |
|
131 |
FIXED_ONE_BYTE_STRING(isolate, "primordials"); |
686 |
|
131 |
Local<String> global_string = FIXED_ONE_BYTE_STRING(isolate, "global"); |
687 |
|
131 |
Local<String> exports_string = FIXED_ONE_BYTE_STRING(isolate, "exports"); |
688 |
|
|
|
689 |
|
|
// Create primordials first and make it available to per-context scripts. |
690 |
|
131 |
Local<Object> primordials = Object::New(isolate); |
691 |
|
262 |
if (primordials->SetPrototype(context, Null(isolate)).IsNothing() || |
692 |
✓✗✓✗
|
393 |
!GetPerContextExports(context).ToLocal(&exports) || |
693 |
✗✓✗✓
|
393 |
exports->Set(context, primordials_string, primordials).IsNothing()) { |
694 |
|
|
return Nothing<bool>(); |
695 |
|
|
} |
696 |
|
|
|
697 |
|
|
static const char* context_files[] = {"internal/per_context/primordials", |
698 |
|
|
"internal/per_context/domexception", |
699 |
|
|
"internal/per_context/messageport", |
700 |
|
|
nullptr}; |
701 |
|
|
|
702 |
✓✓ |
524 |
for (const char** module = context_files; *module != nullptr; module++) { |
703 |
|
|
std::vector<Local<String>> parameters = { |
704 |
|
393 |
global_string, exports_string, primordials_string}; |
705 |
|
786 |
Local<Value> arguments[] = {context->Global(), exports, primordials}; |
706 |
|
|
MaybeLocal<Function> maybe_fn = |
707 |
|
|
native_module::NativeModuleLoader::LookupAndCompile( |
708 |
|
393 |
context, *module, ¶meters, nullptr); |
709 |
|
|
Local<Function> fn; |
710 |
✗✓ |
393 |
if (!maybe_fn.ToLocal(&fn)) { |
711 |
|
|
return Nothing<bool>(); |
712 |
|
|
} |
713 |
|
|
MaybeLocal<Value> result = |
714 |
|
786 |
fn->Call(context, Undefined(isolate), arraysize(arguments), arguments); |
715 |
|
|
// Execution failed during context creation. |
716 |
✗✓ |
393 |
if (result.IsEmpty()) { |
717 |
|
|
return Nothing<bool>(); |
718 |
|
|
} |
719 |
|
|
} |
720 |
|
|
|
721 |
|
131 |
return Just(true); |
722 |
|
|
} |
723 |
|
|
|
724 |
|
63 |
Maybe<bool> InitializeContext(Local<Context> context) { |
725 |
✗✓ |
126 |
if (InitializeContextForSnapshot(context).IsNothing()) { |
726 |
|
|
return Nothing<bool>(); |
727 |
|
|
} |
728 |
|
|
|
729 |
|
63 |
return InitializeContextRuntime(context); |
730 |
|
|
} |
731 |
|
|
|
732 |
|
10 |
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) { |
733 |
|
20 |
HandleScope handle_scope(isolate); |
734 |
|
10 |
Local<Context> context = isolate->GetCurrentContext(); |
735 |
✗✓ |
10 |
if (context.IsEmpty()) return nullptr; |
736 |
|
10 |
Environment* env = Environment::GetCurrent(context); |
737 |
✗✓ |
10 |
if (env == nullptr) return nullptr; |
738 |
|
10 |
return env->event_loop(); |
739 |
|
|
} |
740 |
|
|
|
741 |
|
9 |
void AddLinkedBinding(Environment* env, const node_module& mod) { |
742 |
✗✓ |
9 |
CHECK_NOT_NULL(env); |
743 |
|
18 |
Mutex::ScopedLock lock(env->extra_linked_bindings_mutex()); |
744 |
|
|
|
745 |
|
9 |
node_module* prev_tail = env->extra_linked_bindings_tail(); |
746 |
|
9 |
env->extra_linked_bindings()->push_back(mod); |
747 |
✓✓ |
9 |
if (prev_tail != nullptr) |
748 |
|
5 |
prev_tail->nm_link = &env->extra_linked_bindings()->back(); |
749 |
|
9 |
} |
750 |
|
|
|
751 |
|
3 |
void AddLinkedBinding(Environment* env, const napi_module& mod) { |
752 |
|
3 |
AddLinkedBinding(env, napi_module_to_node_module(&mod)); |
753 |
|
3 |
} |
754 |
|
|
|
755 |
|
6 |
void AddLinkedBinding(Environment* env, |
756 |
|
|
const char* name, |
757 |
|
|
addon_context_register_func fn, |
758 |
|
|
void* priv) { |
759 |
|
6 |
node_module mod = { |
760 |
|
|
NODE_MODULE_VERSION, |
761 |
|
|
NM_F_LINKED, |
762 |
|
|
nullptr, // nm_dso_handle |
763 |
|
|
nullptr, // nm_filename |
764 |
|
|
nullptr, // nm_register_func |
765 |
|
|
fn, |
766 |
|
|
name, |
767 |
|
|
priv, |
768 |
|
|
nullptr // nm_link |
769 |
|
6 |
}; |
770 |
|
6 |
AddLinkedBinding(env, mod); |
771 |
|
6 |
} |
772 |
|
|
|
773 |
|
|
static std::atomic<uint64_t> next_thread_id{0}; |
774 |
|
|
|
775 |
|
6821 |
ThreadId AllocateEnvironmentThreadId() { |
776 |
|
6821 |
return ThreadId { next_thread_id++ }; |
777 |
|
|
} |
778 |
|
|
|
779 |
|
518 |
void DefaultProcessExitHandler(Environment* env, int exit_code) { |
780 |
|
518 |
env->set_can_call_into_js(false); |
781 |
|
518 |
env->stop_sub_worker_contexts(); |
782 |
|
518 |
DisposePlatform(); |
783 |
|
518 |
uv_library_shutdown(); |
784 |
|
518 |
exit(exit_code); |
785 |
|
|
} |
786 |
|
|
|
787 |
|
|
|
788 |
|
1246 |
void SetProcessExitHandler(Environment* env, |
789 |
|
|
std::function<void(Environment*, int)>&& handler) { |
790 |
|
1246 |
env->set_process_exit_handler(std::move(handler)); |
791 |
|
1246 |
} |
792 |
|
|
|
793 |
|
|
} // namespace node |