GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
// Copyright Joyent, Inc. and other Node contributors. |
||
2 |
// |
||
3 |
// Permission is hereby granted, free of charge, to any person obtaining a |
||
4 |
// copy of this software and associated documentation files (the |
||
5 |
// "Software"), to deal in the Software without restriction, including |
||
6 |
// without limitation the rights to use, copy, modify, merge, publish, |
||
7 |
// distribute, sublicense, and/or sell copies of the Software, and to permit |
||
8 |
// persons to whom the Software is furnished to do so, subject to the |
||
9 |
// following conditions: |
||
10 |
// |
||
11 |
// The above copyright notice and this permission notice shall be included |
||
12 |
// in all copies or substantial portions of the Software. |
||
13 |
// |
||
14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
||
15 |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
16 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
||
17 |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
||
18 |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
||
19 |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
||
20 |
// USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
21 |
|||
22 |
#include "node.h" |
||
23 |
|||
24 |
// ========== local headers ========== |
||
25 |
|||
26 |
#include "debug_utils-inl.h" |
||
27 |
#include "env-inl.h" |
||
28 |
#include "histogram-inl.h" |
||
29 |
#include "memory_tracker-inl.h" |
||
30 |
#include "node_binding.h" |
||
31 |
#include "node_builtins.h" |
||
32 |
#include "node_errors.h" |
||
33 |
#include "node_internals.h" |
||
34 |
#include "node_main_instance.h" |
||
35 |
#include "node_metadata.h" |
||
36 |
#include "node_options-inl.h" |
||
37 |
#include "node_perf.h" |
||
38 |
#include "node_process-inl.h" |
||
39 |
#include "node_realm-inl.h" |
||
40 |
#include "node_report.h" |
||
41 |
#include "node_revert.h" |
||
42 |
#include "node_snapshot_builder.h" |
||
43 |
#include "node_v8_platform-inl.h" |
||
44 |
#include "node_version.h" |
||
45 |
|||
46 |
#if HAVE_OPENSSL |
||
47 |
#include "node_crypto.h" |
||
48 |
#endif |
||
49 |
|||
50 |
#if defined(NODE_HAVE_I18N_SUPPORT) |
||
51 |
#include "node_i18n.h" |
||
52 |
#endif |
||
53 |
|||
54 |
#if HAVE_INSPECTOR |
||
55 |
#include "inspector_agent.h" |
||
56 |
#include "inspector_io.h" |
||
57 |
#endif |
||
58 |
|||
59 |
#if NODE_USE_V8_PLATFORM |
||
60 |
#include "libplatform/libplatform.h" |
||
61 |
#endif // NODE_USE_V8_PLATFORM |
||
62 |
#include "v8-profiler.h" |
||
63 |
|||
64 |
#if HAVE_INSPECTOR |
||
65 |
#include "inspector/worker_inspector.h" // ParentInspectorHandle |
||
66 |
#endif |
||
67 |
|||
68 |
#include "large_pages/node_large_page.h" |
||
69 |
|||
70 |
#if defined(__APPLE__) || defined(__linux__) || defined(_WIN32) |
||
71 |
#define NODE_USE_V8_WASM_TRAP_HANDLER 1 |
||
72 |
#else |
||
73 |
#define NODE_USE_V8_WASM_TRAP_HANDLER 0 |
||
74 |
#endif |
||
75 |
|||
76 |
#if NODE_USE_V8_WASM_TRAP_HANDLER |
||
77 |
#if defined(_WIN32) |
||
78 |
#include "v8-wasm-trap-handler-win.h" |
||
79 |
#else |
||
80 |
#include <atomic> |
||
81 |
#include "v8-wasm-trap-handler-posix.h" |
||
82 |
#endif |
||
83 |
#endif // NODE_USE_V8_WASM_TRAP_HANDLER |
||
84 |
|||
85 |
// ========== global C headers ========== |
||
86 |
|||
87 |
#include <fcntl.h> // _O_RDWR |
||
88 |
#include <sys/types.h> |
||
89 |
|||
90 |
#if defined(NODE_HAVE_I18N_SUPPORT) |
||
91 |
#include <unicode/uvernum.h> |
||
92 |
#include <unicode/utypes.h> |
||
93 |
#endif |
||
94 |
|||
95 |
|||
96 |
#if defined(LEAK_SANITIZER) |
||
97 |
#include <sanitizer/lsan_interface.h> |
||
98 |
#endif |
||
99 |
|||
100 |
#if defined(_MSC_VER) |
||
101 |
#include <direct.h> |
||
102 |
#include <io.h> |
||
103 |
#define STDIN_FILENO 0 |
||
104 |
#else |
||
105 |
#include <pthread.h> |
||
106 |
#include <sys/resource.h> // getrlimit, setrlimit |
||
107 |
#include <termios.h> // tcgetattr, tcsetattr |
||
108 |
#include <unistd.h> // STDIN_FILENO, STDERR_FILENO |
||
109 |
#endif |
||
110 |
|||
111 |
// ========== global C++ headers ========== |
||
112 |
|||
113 |
#include <cerrno> |
||
114 |
#include <climits> // PATH_MAX |
||
115 |
#include <csignal> |
||
116 |
#include <cstdio> |
||
117 |
#include <cstdlib> |
||
118 |
#include <cstring> |
||
119 |
|||
120 |
#include <string> |
||
121 |
#include <vector> |
||
122 |
|||
123 |
namespace node { |
||
124 |
|||
125 |
using builtins::BuiltinLoader; |
||
126 |
|||
127 |
using v8::EscapableHandleScope; |
||
128 |
using v8::Isolate; |
||
129 |
using v8::Local; |
||
130 |
using v8::MaybeLocal; |
||
131 |
using v8::Object; |
||
132 |
using v8::V8; |
||
133 |
using v8::Value; |
||
134 |
|||
135 |
namespace per_process { |
||
136 |
|||
137 |
// node_revert.h |
||
138 |
// Bit flag used to track security reverts. |
||
139 |
unsigned int reverted_cve = 0; |
||
140 |
|||
141 |
// util.h |
||
142 |
// Tells whether the per-process V8::Initialize() is called and |
||
143 |
// if it is safe to call v8::Isolate::TryGetCurrent(). |
||
144 |
bool v8_initialized = false; |
||
145 |
|||
146 |
// node_internals.h |
||
147 |
// process-relative uptime base in nanoseconds, initialized in node::Start() |
||
148 |
uint64_t node_start_time; |
||
149 |
|||
150 |
#if NODE_USE_V8_WASM_TRAP_HANDLER && defined(_WIN32) |
||
151 |
PVOID old_vectored_exception_handler; |
||
152 |
#endif |
||
153 |
|||
154 |
// node_v8_platform-inl.h |
||
155 |
struct V8Platform v8_platform; |
||
156 |
} // namespace per_process |
||
157 |
|||
158 |
// The section in the OpenSSL configuration file to be loaded. |
||
159 |
const char* conf_section_name = STRINGIFY(NODE_OPENSSL_CONF_NAME); |
||
160 |
|||
161 |
#ifdef __POSIX__ |
||
162 |
void SignalExit(int signo, siginfo_t* info, void* ucontext) { |
||
163 |
ResetStdio(); |
||
164 |
raise(signo); |
||
165 |
} |
||
166 |
#endif // __POSIX__ |
||
167 |
|||
168 |
#if HAVE_INSPECTOR |
||
169 |
6298 |
int Environment::InitializeInspector( |
|
170 |
std::unique_ptr<inspector::ParentInspectorHandle> parent_handle) { |
||
171 |
12596 |
std::string inspector_path; |
|
172 |
6298 |
bool is_main = !parent_handle; |
|
173 |
✓✓ | 6298 |
if (parent_handle) { |
174 |
725 |
inspector_path = parent_handle->url(); |
|
175 |
725 |
inspector_agent_->SetParentHandle(std::move(parent_handle)); |
|
176 |
} else { |
||
177 |
✓✓ | 5573 |
inspector_path = argv_.size() > 1 ? argv_[1].c_str() : ""; |
178 |
} |
||
179 |
|||
180 |
✗✓ | 6298 |
CHECK(!inspector_agent_->IsListening()); |
181 |
// Inspector agent can't fail to start, but if it was configured to listen |
||
182 |
// right away on the websocket port and fails to bind/etc, this will return |
||
183 |
// false. |
||
184 |
12596 |
inspector_agent_->Start(inspector_path, |
|
185 |
6298 |
options_->debug_options(), |
|
186 |
12596 |
inspector_host_port(), |
|
187 |
is_main); |
||
188 |
✓✓✓✓ |
6413 |
if (options_->debug_options().inspector_enabled && |
189 |
✓✓ | 115 |
!inspector_agent_->IsListening()) { |
190 |
5 |
return 12; // Signal internal error |
|
191 |
} |
||
192 |
|||
193 |
6293 |
profiler::StartProfilers(this); |
|
194 |
|||
195 |
✓✓ | 6293 |
if (inspector_agent_->options().break_node_first_line) { |
196 |
1 |
inspector_agent_->PauseOnNextJavascriptStatement("Break at bootstrap"); |
|
197 |
} |
||
198 |
|||
199 |
6293 |
return 0; |
|
200 |
} |
||
201 |
#endif // HAVE_INSPECTOR |
||
202 |
|||
203 |
#define ATOMIC_WAIT_EVENTS(V) \ |
||
204 |
V(kStartWait, "started") \ |
||
205 |
V(kWokenUp, "was woken up by another thread") \ |
||
206 |
V(kTimedOut, "timed out") \ |
||
207 |
V(kTerminatedExecution, "was stopped by terminated execution") \ |
||
208 |
V(kAPIStopped, "was stopped through the embedder API") \ |
||
209 |
V(kNotEqual, "did not wait because the values mismatched") \ |
||
210 |
|||
211 |
8 |
static void AtomicsWaitCallback(Isolate::AtomicsWaitEvent event, |
|
212 |
Local<v8::SharedArrayBuffer> array_buffer, |
||
213 |
size_t offset_in_bytes, int64_t value, |
||
214 |
double timeout_in_ms, |
||
215 |
Isolate::AtomicsWaitWakeHandle* stop_handle, |
||
216 |
void* data) { |
||
217 |
8 |
Environment* env = static_cast<Environment*>(data); |
|
218 |
|||
219 |
8 |
const char* message = "(unknown event)"; |
|
220 |
✓✓✓✗ ✗✓✗ |
8 |
switch (event) { |
221 |
#define V(key, msg) \ |
||
222 |
case Isolate::AtomicsWaitEvent::key: \ |
||
223 |
message = msg; \ |
||
224 |
break; |
||
225 |
8 |
ATOMIC_WAIT_EVENTS(V) |
|
226 |
#undef V |
||
227 |
} |
||
228 |
|||
229 |
16 |
fprintf(stderr, |
|
230 |
"(node:%d) [Thread %" PRIu64 "] Atomics.wait(%p + %zx, %" PRId64 |
||
231 |
", %.f) %s\n", |
||
232 |
8 |
static_cast<int>(uv_os_getpid()), |
|
233 |
env->thread_id(), |
||
234 |
array_buffer->Data(), |
||
235 |
offset_in_bytes, |
||
236 |
value, |
||
237 |
timeout_in_ms, |
||
238 |
message); |
||
239 |
8 |
} |
|
240 |
|||
241 |
6270 |
void Environment::InitializeDiagnostics() { |
|
242 |
6270 |
isolate_->GetHeapProfiler()->AddBuildEmbedderGraphCallback( |
|
243 |
Environment::BuildEmbedderGraph, this); |
||
244 |
✓✓ | 6270 |
if (heap_snapshot_near_heap_limit_ > 0) { |
245 |
1 |
AddHeapSnapshotNearHeapLimitCallback(); |
|
246 |
} |
||
247 |
✓✓ | 6270 |
if (options_->trace_uncaught) |
248 |
3 |
isolate_->SetCaptureStackTraceForUncaughtExceptions(true); |
|
249 |
✓✓ | 6270 |
if (options_->trace_atomics_wait) { |
250 |
2 |
isolate_->SetAtomicsWaitCallback(AtomicsWaitCallback, this); |
|
251 |
2 |
AddCleanupHook([](void* data) { |
|
252 |
2 |
Environment* env = static_cast<Environment*>(data); |
|
253 |
2 |
env->isolate()->SetAtomicsWaitCallback(nullptr, nullptr); |
|
254 |
2 |
}, this); |
|
255 |
} |
||
256 |
6270 |
} |
|
257 |
|||
258 |
static |
||
259 |
6270 |
MaybeLocal<Value> StartExecution(Environment* env, const char* main_script_id) { |
|
260 |
6270 |
EscapableHandleScope scope(env->isolate()); |
|
261 |
✗✓ | 6270 |
CHECK_NOT_NULL(main_script_id); |
262 |
6270 |
Realm* realm = env->principal_realm(); |
|
263 |
|||
264 |
12273 |
return scope.EscapeMaybe(realm->ExecuteBootstrapper(main_script_id)); |
|
265 |
} |
||
266 |
|||
267 |
6270 |
MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { |
|
268 |
InternalCallbackScope callback_scope( |
||
269 |
env, |
||
270 |
Object::New(env->isolate()), |
||
271 |
{ 1, 0 }, |
||
272 |
12271 |
InternalCallbackScope::kSkipAsyncHooks); |
|
273 |
|||
274 |
✓✓ | 6270 |
if (cb != nullptr) { |
275 |
24 |
EscapableHandleScope scope(env->isolate()); |
|
276 |
|||
277 |
✗✓ | 48 |
if (StartExecution(env, "internal/main/environment").IsEmpty()) return {}; |
278 |
|||
279 |
StartExecutionCallbackInfo info = { |
||
280 |
24 |
env->process_object(), |
|
281 |
24 |
env->builtin_module_require(), |
|
282 |
}; |
||
283 |
|||
284 |
46 |
return scope.EscapeMaybe(cb(info)); |
|
285 |
} |
||
286 |
|||
287 |
// TODO(joyeecheung): move these conditions into JS land and let the |
||
288 |
// deserialize main function take precedence. For workers, we need to |
||
289 |
// move the pre-execution part into a different file that can be |
||
290 |
// reused when dealing with user-defined main functions. |
||
291 |
✗✓ | 12492 |
if (!env->snapshot_deserialize_main().IsEmpty()) { |
292 |
return env->RunSnapshotDeserializeMain(); |
||
293 |
} |
||
294 |
|||
295 |
✓✓ | 6246 |
if (env->worker_context() != nullptr) { |
296 |
723 |
return StartExecution(env, "internal/main/worker_thread"); |
|
297 |
} |
||
298 |
|||
299 |
10779 |
std::string first_argv; |
|
300 |
✓✓ | 5523 |
if (env->argv().size() > 1) { |
301 |
4969 |
first_argv = env->argv()[1]; |
|
302 |
} |
||
303 |
|||
304 |
✓✓ | 5523 |
if (first_argv == "inspect") { |
305 |
38 |
return StartExecution(env, "internal/main/inspect"); |
|
306 |
} |
||
307 |
|||
308 |
✓✓ | 5485 |
if (per_process::cli_options->build_snapshot) { |
309 |
1 |
return StartExecution(env, "internal/main/mksnapshot"); |
|
310 |
} |
||
311 |
|||
312 |
✓✓ | 5484 |
if (per_process::cli_options->print_help) { |
313 |
2 |
return StartExecution(env, "internal/main/print_help"); |
|
314 |
} |
||
315 |
|||
316 |
|||
317 |
✓✓ | 5482 |
if (env->options()->prof_process) { |
318 |
1 |
return StartExecution(env, "internal/main/prof_process"); |
|
319 |
} |
||
320 |
|||
321 |
// -e/--eval without -i/--interactive |
||
322 |
✓✓✓✓ ✓✓✓✗ ✓✓ |
5481 |
if (env->options()->has_eval_string && !env->options()->force_repl) { |
323 |
517 |
return StartExecution(env, "internal/main/eval_string"); |
|
324 |
} |
||
325 |
|||
326 |
✓✓ | 4964 |
if (env->options()->syntax_check_only) { |
327 |
39 |
return StartExecution(env, "internal/main/check_syntax"); |
|
328 |
} |
||
329 |
|||
330 |
✓✓ | 4925 |
if (env->options()->test_runner) { |
331 |
10 |
return StartExecution(env, "internal/main/test_runner"); |
|
332 |
} |
||
333 |
|||
334 |
✓✓✓✗ ✓✗✓✓ |
4915 |
if (env->options()->watch_mode && !first_argv.empty()) { |
335 |
11 |
return StartExecution(env, "internal/main/watch_mode"); |
|
336 |
} |
||
337 |
|||
338 |
✓✓✓✓ ✓✓ |
4904 |
if (!first_argv.empty() && first_argv != "-") { |
339 |
4858 |
return StartExecution(env, "internal/main/run_main_module"); |
|
340 |
} |
||
341 |
|||
342 |
✓✓✓✓ ✓✗✓✓ |
46 |
if (env->options()->force_repl || uv_guess_handle(STDIN_FILENO) == UV_TTY) { |
343 |
28 |
return StartExecution(env, "internal/main/repl"); |
|
344 |
} |
||
345 |
|||
346 |
18 |
return StartExecution(env, "internal/main/eval_stdin"); |
|
347 |
} |
||
348 |
|||
349 |
#ifdef __POSIX__ |
||
350 |
typedef void (*sigaction_cb)(int signo, siginfo_t* info, void* ucontext); |
||
351 |
#endif |
||
352 |
#if NODE_USE_V8_WASM_TRAP_HANDLER |
||
353 |
#if defined(_WIN32) |
||
354 |
static LONG TrapWebAssemblyOrContinue(EXCEPTION_POINTERS* exception) { |
||
355 |
if (v8::TryHandleWebAssemblyTrapWindows(exception)) { |
||
356 |
return EXCEPTION_CONTINUE_EXECUTION; |
||
357 |
} |
||
358 |
return EXCEPTION_CONTINUE_SEARCH; |
||
359 |
} |
||
360 |
#else |
||
361 |
static std::atomic<sigaction_cb> previous_sigsegv_action; |
||
362 |
|||
363 |
6 |
void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) { |
|
364 |
✓✗ | 6 |
if (!v8::TryHandleWebAssemblyTrapPosix(signo, info, ucontext)) { |
365 |
6 |
sigaction_cb prev = previous_sigsegv_action.load(); |
|
366 |
✓✗ | 6 |
if (prev != nullptr) { |
367 |
6 |
prev(signo, info, ucontext); |
|
368 |
} else { |
||
369 |
// Reset to the default signal handler, i.e. cause a hard crash. |
||
370 |
struct sigaction sa; |
||
371 |
memset(&sa, 0, sizeof(sa)); |
||
372 |
sa.sa_handler = SIG_DFL; |
||
373 |
CHECK_EQ(sigaction(signo, &sa, nullptr), 0); |
||
374 |
|||
375 |
ResetStdio(); |
||
376 |
raise(signo); |
||
377 |
} |
||
378 |
} |
||
379 |
6 |
} |
|
380 |
#endif // defined(_WIN32) |
||
381 |
#endif // NODE_USE_V8_WASM_TRAP_HANDLER |
||
382 |
|||
383 |
#ifdef __POSIX__ |
||
384 |
17649 |
void RegisterSignalHandler(int signal, |
|
385 |
sigaction_cb handler, |
||
386 |
bool reset_handler) { |
||
387 |
✗✓ | 17649 |
CHECK_NOT_NULL(handler); |
388 |
#if NODE_USE_V8_WASM_TRAP_HANDLER |
||
389 |
✓✓ | 17649 |
if (signal == SIGSEGV) { |
390 |
✗✓ | 4 |
CHECK(previous_sigsegv_action.is_lock_free()); |
391 |
✗✓ | 4 |
CHECK(!reset_handler); |
392 |
4 |
previous_sigsegv_action.store(handler); |
|
393 |
4 |
return; |
|
394 |
} |
||
395 |
#endif // NODE_USE_V8_WASM_TRAP_HANDLER |
||
396 |
struct sigaction sa; |
||
397 |
17645 |
memset(&sa, 0, sizeof(sa)); |
|
398 |
17645 |
sa.sa_sigaction = handler; |
|
399 |
✓✓ | 17645 |
sa.sa_flags = reset_handler ? SA_RESETHAND : 0; |
400 |
17645 |
sigfillset(&sa.sa_mask); |
|
401 |
✗✓ | 17645 |
CHECK_EQ(sigaction(signal, &sa, nullptr), 0); |
402 |
} |
||
403 |
#endif // __POSIX__ |
||
404 |
|||
405 |
#ifdef __POSIX__ |
||
406 |
static struct { |
||
407 |
int flags; |
||
408 |
bool isatty; |
||
409 |
struct stat stat; |
||
410 |
struct termios termios; |
||
411 |
} stdio[1 + STDERR_FILENO]; |
||
412 |
#endif // __POSIX__ |
||
413 |
|||
414 |
10475 |
void ResetSignalHandlers() { |
|
415 |
#ifdef __POSIX__ |
||
416 |
// Restore signal dispositions, the parent process may have changed them. |
||
417 |
struct sigaction act; |
||
418 |
10475 |
memset(&act, 0, sizeof(act)); |
|
419 |
|||
420 |
// The hard-coded upper limit is because NSIG is not very reliable; on Linux, |
||
421 |
// it evaluates to 32, 34 or 64, depending on whether RT signals are enabled. |
||
422 |
// Counting up to SIGRTMIN doesn't work for the same reason. |
||
423 |
✓✓ | 335200 |
for (unsigned nr = 1; nr < kMaxSignal; nr += 1) { |
424 |
✓✓✓✓ |
324725 |
if (nr == SIGKILL || nr == SIGSTOP) |
425 |
20950 |
continue; |
|
426 |
✓✓✓✓ |
303775 |
act.sa_handler = (nr == SIGPIPE || nr == SIGXFSZ) ? SIG_IGN : SIG_DFL; |
427 |
✗✓ | 303775 |
CHECK_EQ(0, sigaction(nr, &act, nullptr)); |
428 |
} |
||
429 |
#endif // __POSIX__ |
||
430 |
10475 |
} |
|
431 |
|||
432 |
static std::atomic<uint64_t> init_process_flags = 0; |
||
433 |
|||
434 |
5596 |
static void PlatformInit(ProcessInitializationFlags::Flags flags) { |
|
435 |
// init_process_flags is accessed in ResetStdio(), |
||
436 |
// which can be called from signal handlers. |
||
437 |
✗✓ | 5596 |
CHECK(init_process_flags.is_lock_free()); |
438 |
init_process_flags.store(flags); |
||
439 |
|||
440 |
✓✗ | 5596 |
if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { |
441 |
5596 |
atexit(ResetStdio); |
|
442 |
} |
||
443 |
|||
444 |
#ifdef __POSIX__ |
||
445 |
✓✗ | 5596 |
if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { |
446 |
// Disable stdio buffering, it interacts poorly with printf() |
||
447 |
// calls elsewhere in the program (e.g., any logging from V8.) |
||
448 |
5596 |
setvbuf(stdout, nullptr, _IONBF, 0); |
|
449 |
5596 |
setvbuf(stderr, nullptr, _IONBF, 0); |
|
450 |
|||
451 |
// Make sure file descriptors 0-2 are valid before we start logging |
||
452 |
// anything. |
||
453 |
✓✓ | 22384 |
for (auto& s : stdio) { |
454 |
16788 |
const int fd = &s - stdio; |
|
455 |
✓✓ | 16788 |
if (fstat(fd, &s.stat) == 0) continue; |
456 |
// Anything but EBADF means something is seriously wrong. We don't |
||
457 |
// have to special-case EINTR, fstat() is not interruptible. |
||
458 |
✗✓ | 2 |
if (errno != EBADF) ABORT(); |
459 |
✗✓ | 2 |
if (fd != open("/dev/null", O_RDWR)) ABORT(); |
460 |
✗✓ | 2 |
if (fstat(fd, &s.stat) != 0) ABORT(); |
461 |
} |
||
462 |
} |
||
463 |
|||
464 |
✓✗ | 5596 |
if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling)) { |
465 |
#if HAVE_INSPECTOR |
||
466 |
sigset_t sigmask; |
||
467 |
5596 |
sigemptyset(&sigmask); |
|
468 |
5596 |
sigaddset(&sigmask, SIGUSR1); |
|
469 |
5596 |
const int err = pthread_sigmask(SIG_SETMASK, &sigmask, nullptr); |
|
470 |
✗✓ | 5596 |
CHECK_EQ(err, 0); |
471 |
#endif // HAVE_INSPECTOR |
||
472 |
|||
473 |
5596 |
ResetSignalHandlers(); |
|
474 |
} |
||
475 |
|||
476 |
✓✗ | 5596 |
if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { |
477 |
// Record the state of the stdio file descriptors so we can restore it |
||
478 |
// on exit. Needs to happen before installing signal handlers because |
||
479 |
// they make use of that information. |
||
480 |
✓✓ | 22384 |
for (auto& s : stdio) { |
481 |
16788 |
const int fd = &s - stdio; |
|
482 |
int err; |
||
483 |
|||
484 |
do { |
||
485 |
16788 |
s.flags = fcntl(fd, F_GETFL); |
|
486 |
✗✓✗✗ |
16788 |
} while (s.flags == -1 && errno == EINTR); // NOLINT |
487 |
✗✓ | 16788 |
CHECK_NE(s.flags, -1); |
488 |
|||
489 |
✓✓ | 16788 |
if (uv_guess_handle(fd) != UV_TTY) continue; |
490 |
120 |
s.isatty = true; |
|
491 |
|||
492 |
do { |
||
493 |
120 |
err = tcgetattr(fd, &s.termios); |
|
494 |
✗✓✗✗ |
120 |
} while (err == -1 && errno == EINTR); // NOLINT |
495 |
✗✓ | 120 |
CHECK_EQ(err, 0); |
496 |
} |
||
497 |
} |
||
498 |
|||
499 |
✓✗ | 5596 |
if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling)) { |
500 |
5596 |
RegisterSignalHandler(SIGINT, SignalExit, true); |
|
501 |
5596 |
RegisterSignalHandler(SIGTERM, SignalExit, true); |
|
502 |
|||
503 |
#if NODE_USE_V8_WASM_TRAP_HANDLER |
||
504 |
#if defined(_WIN32) |
||
505 |
{ |
||
506 |
constexpr ULONG first = TRUE; |
||
507 |
per_process::old_vectored_exception_handler = |
||
508 |
AddVectoredExceptionHandler(first, TrapWebAssemblyOrContinue); |
||
509 |
} |
||
510 |
#else |
||
511 |
// Tell V8 to disable emitting WebAssembly |
||
512 |
// memory bounds checks. This means that we have |
||
513 |
// to catch the SIGSEGV in TrapWebAssemblyOrContinue |
||
514 |
// and pass the signal context to V8. |
||
515 |
{ |
||
516 |
struct sigaction sa; |
||
517 |
5596 |
memset(&sa, 0, sizeof(sa)); |
|
518 |
5596 |
sa.sa_sigaction = TrapWebAssemblyOrContinue; |
|
519 |
5596 |
sa.sa_flags = SA_SIGINFO; |
|
520 |
✗✓ | 5596 |
CHECK_EQ(sigaction(SIGSEGV, &sa, nullptr), 0); |
521 |
} |
||
522 |
#endif // defined(_WIN32) |
||
523 |
5596 |
V8::EnableWebAssemblyTrapHandler(false); |
|
524 |
#endif // NODE_USE_V8_WASM_TRAP_HANDLER |
||
525 |
} |
||
526 |
|||
527 |
✓✗ | 5596 |
if (!(flags & ProcessInitializationFlags::kNoAdjustResourceLimits)) { |
528 |
// Raise the open file descriptor limit. |
||
529 |
struct rlimit lim; |
||
530 |
✓✗✗✓ ✗✓ |
5596 |
if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) { |
531 |
// Do a binary search for the limit. |
||
532 |
rlim_t min = lim.rlim_cur; |
||
533 |
rlim_t max = 1 << 20; |
||
534 |
// But if there's a defined upper bound, don't search, just set it. |
||
535 |
if (lim.rlim_max != RLIM_INFINITY) { |
||
536 |
min = lim.rlim_max; |
||
537 |
max = lim.rlim_max; |
||
538 |
} |
||
539 |
do { |
||
540 |
lim.rlim_cur = min + (max - min) / 2; |
||
541 |
if (setrlimit(RLIMIT_NOFILE, &lim)) { |
||
542 |
max = lim.rlim_cur; |
||
543 |
} else { |
||
544 |
min = lim.rlim_cur; |
||
545 |
} |
||
546 |
} while (min + 1 < max); |
||
547 |
} |
||
548 |
} |
||
549 |
#endif // __POSIX__ |
||
550 |
#ifdef _WIN32 |
||
551 |
if (!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { |
||
552 |
for (int fd = 0; fd <= 2; ++fd) { |
||
553 |
auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); |
||
554 |
if (handle == INVALID_HANDLE_VALUE || |
||
555 |
GetFileType(handle) == FILE_TYPE_UNKNOWN) { |
||
556 |
// Ignore _close result. If it fails or not depends on used Windows |
||
557 |
// version. We will just check _open result. |
||
558 |
_close(fd); |
||
559 |
if (fd != _open("nul", _O_RDWR)) ABORT(); |
||
560 |
} |
||
561 |
} |
||
562 |
} |
||
563 |
#endif // _WIN32 |
||
564 |
5596 |
} |
|
565 |
|||
566 |
// Safe to call more than once and from signal handlers. |
||
567 |
10497 |
void ResetStdio() { |
|
568 |
20994 |
if (init_process_flags.load() & |
|
569 |
✗✓ | 10497 |
ProcessInitializationFlags::kNoStdioInitialization) { |
570 |
return; |
||
571 |
} |
||
572 |
|||
573 |
10497 |
uv_tty_reset_mode(); |
|
574 |
#ifdef __POSIX__ |
||
575 |
✓✓ | 41988 |
for (auto& s : stdio) { |
576 |
31491 |
const int fd = &s - stdio; |
|
577 |
|||
578 |
struct stat tmp; |
||
579 |
✗✓ | 31491 |
if (-1 == fstat(fd, &tmp)) { |
580 |
CHECK_EQ(errno, EBADF); // Program closed file descriptor. |
||
581 |
6 |
continue; |
|
582 |
} |
||
583 |
|||
584 |
31491 |
bool is_same_file = |
|
585 |
✓✓✓✗ |
31491 |
(s.stat.st_dev == tmp.st_dev && s.stat.st_ino == tmp.st_ino); |
586 |
✓✓ | 31491 |
if (!is_same_file) continue; // Program reopened file descriptor. |
587 |
|||
588 |
int flags; |
||
589 |
do |
||
590 |
31485 |
flags = fcntl(fd, F_GETFL); |
|
591 |
✗✓✗✗ |
31485 |
while (flags == -1 && errno == EINTR); // NOLINT |
592 |
✗✓ | 31485 |
CHECK_NE(flags, -1); |
593 |
|||
594 |
// Restore the O_NONBLOCK flag if it changed. |
||
595 |
✓✓ | 31485 |
if (O_NONBLOCK & (flags ^ s.flags)) { |
596 |
1418 |
flags &= ~O_NONBLOCK; |
|
597 |
1418 |
flags |= s.flags & O_NONBLOCK; |
|
598 |
|||
599 |
int err; |
||
600 |
do |
||
601 |
1418 |
err = fcntl(fd, F_SETFL, flags); |
|
602 |
✗✓✗✗ |
1418 |
while (err == -1 && errno == EINTR); // NOLINT |
603 |
✗✓ | 1418 |
CHECK_NE(err, -1); |
604 |
} |
||
605 |
|||
606 |
✓✓ | 31485 |
if (s.isatty) { |
607 |
sigset_t sa; |
||
608 |
int err; |
||
609 |
|||
610 |
// We might be a background job that doesn't own the TTY so block SIGTTOU |
||
611 |
// before making the tcsetattr() call, otherwise that signal suspends us. |
||
612 |
228 |
sigemptyset(&sa); |
|
613 |
228 |
sigaddset(&sa, SIGTTOU); |
|
614 |
|||
615 |
✗✓ | 228 |
CHECK_EQ(0, pthread_sigmask(SIG_BLOCK, &sa, nullptr)); |
616 |
do |
||
617 |
228 |
err = tcsetattr(fd, TCSANOW, &s.termios); |
|
618 |
✗✓✗✗ |
228 |
while (err == -1 && errno == EINTR); // NOLINT |
619 |
✗✓ | 228 |
CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sa, nullptr)); |
620 |
|||
621 |
// Normally we expect err == 0. But if macOS App Sandbox is enabled, |
||
622 |
// tcsetattr will fail with err == -1 and errno == EPERM. |
||
623 |
✗✓✗✗ ✗✗✗✗ |
228 |
CHECK_IMPLIES(err != 0, err == -1 && errno == EPERM); |
624 |
} |
||
625 |
} |
||
626 |
#endif // __POSIX__ |
||
627 |
} |
||
628 |
|||
629 |
|||
630 |
11056 |
int ProcessGlobalArgs(std::vector<std::string>* args, |
|
631 |
std::vector<std::string>* exec_args, |
||
632 |
std::vector<std::string>* errors, |
||
633 |
OptionEnvvarSettings settings) { |
||
634 |
// Parse a few arguments which are specific to Node. |
||
635 |
22112 |
std::vector<std::string> v8_args; |
|
636 |
|||
637 |
22112 |
Mutex::ScopedLock lock(per_process::cli_options_mutex); |
|
638 |
11056 |
options_parser::Parse( |
|
639 |
args, |
||
640 |
exec_args, |
||
641 |
&v8_args, |
||
642 |
per_process::cli_options.get(), |
||
643 |
settings, |
||
644 |
errors); |
||
645 |
|||
646 |
✓✓ | 11056 |
if (!errors->empty()) return 9; |
647 |
|||
648 |
22016 |
std::string revert_error; |
|
649 |
✓✓ | 11008 |
for (const std::string& cve : per_process::cli_options->security_reverts) { |
650 |
1 |
Revert(cve.c_str(), &revert_error); |
|
651 |
✓✗ | 1 |
if (!revert_error.empty()) { |
652 |
1 |
errors->emplace_back(std::move(revert_error)); |
|
653 |
1 |
return 12; |
|
654 |
} |
||
655 |
} |
||
656 |
|||
657 |
✓✓ | 22013 |
if (per_process::cli_options->disable_proto != "delete" && |
658 |
✓✓✗✓ ✗✓ |
22013 |
per_process::cli_options->disable_proto != "throw" && |
659 |
11005 |
per_process::cli_options->disable_proto != "") { |
|
660 |
errors->emplace_back("invalid mode passed to --disable-proto"); |
||
661 |
return 12; |
||
662 |
} |
||
663 |
|||
664 |
// TODO(aduh95): remove this when the harmony-import-assertions flag |
||
665 |
// is removed in V8. |
||
666 |
22014 |
if (std::find(v8_args.begin(), v8_args.end(), |
|
667 |
✓✗ | 11007 |
"--no-harmony-import-assertions") == v8_args.end()) { |
668 |
11007 |
v8_args.emplace_back("--harmony-import-assertions"); |
|
669 |
} |
||
670 |
|||
671 |
22014 |
auto env_opts = per_process::cli_options->per_isolate->per_env; |
|
672 |
22014 |
if (std::find(v8_args.begin(), v8_args.end(), |
|
673 |
✓✓✓✓ |
21996 |
"--abort-on-uncaught-exception") != v8_args.end() || |
674 |
11007 |
std::find(v8_args.begin(), v8_args.end(), |
|
675 |
✓✓ | 21996 |
"--abort_on_uncaught_exception") != v8_args.end()) { |
676 |
21 |
env_opts->abort_on_uncaught_exception = true; |
|
677 |
} |
||
678 |
|||
679 |
#ifdef __POSIX__ |
||
680 |
// Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the |
||
681 |
// performance penalty of frequent EINTR wakeups when the profiler is running. |
||
682 |
// Only do this for v8.log profiling, as it breaks v8::CpuProfiler users. |
||
683 |
✓✓ | 11007 |
if (std::find(v8_args.begin(), v8_args.end(), "--prof") != v8_args.end()) { |
684 |
2 |
uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF); |
|
685 |
} |
||
686 |
#endif |
||
687 |
|||
688 |
22014 |
std::vector<char*> v8_args_as_char_ptr(v8_args.size()); |
|
689 |
✓✗ | 11007 |
if (v8_args.size() > 0) { |
690 |
✓✓ | 33162 |
for (size_t i = 0; i < v8_args.size(); ++i) |
691 |
22155 |
v8_args_as_char_ptr[i] = &v8_args[i][0]; |
|
692 |
11007 |
int argc = v8_args.size(); |
|
693 |
11007 |
V8::SetFlagsFromCommandLine(&argc, &v8_args_as_char_ptr[0], true); |
|
694 |
11007 |
v8_args_as_char_ptr.resize(argc); |
|
695 |
} |
||
696 |
|||
697 |
// Anything that's still in v8_argv is not a V8 or a node option. |
||
698 |
✓✓ | 11008 |
for (size_t i = 1; i < v8_args_as_char_ptr.size(); i++) |
699 |
1 |
errors->push_back("bad option: " + std::string(v8_args_as_char_ptr[i])); |
|
700 |
|||
701 |
✓✓ | 11007 |
if (v8_args_as_char_ptr.size() > 1) return 9; |
702 |
|||
703 |
11006 |
return 0; |
|
704 |
} |
||
705 |
|||
706 |
static std::atomic_bool init_called{false}; |
||
707 |
|||
708 |
// TODO(addaleax): Turn this into a wrapper around InitializeOncePerProcess() |
||
709 |
// (with the corresponding additional flags set), then eventually remove this. |
||
710 |
5597 |
int InitializeNodeWithArgs(std::vector<std::string>* argv, |
|
711 |
std::vector<std::string>* exec_argv, |
||
712 |
std::vector<std::string>* errors, |
||
713 |
ProcessInitializationFlags::Flags flags) { |
||
714 |
// Make sure InitializeNodeWithArgs() is called only once. |
||
715 |
✗✓ | 5597 |
CHECK(!init_called.exchange(true)); |
716 |
|||
717 |
// Initialize node_start_time to get relative uptime. |
||
718 |
5597 |
per_process::node_start_time = uv_hrtime(); |
|
719 |
|||
720 |
// Register built-in modules |
||
721 |
5597 |
binding::RegisterBuiltinModules(); |
|
722 |
|||
723 |
// Make inherited handles noninheritable. |
||
724 |
✓✗ | 5597 |
if (!(flags & ProcessInitializationFlags::kEnableStdioInheritance) && |
725 |
✓✗ | 5597 |
!(flags & ProcessInitializationFlags::kNoStdioInitialization)) { |
726 |
5597 |
uv_disable_stdio_inheritance(); |
|
727 |
} |
||
728 |
|||
729 |
// Cache the original command line to be |
||
730 |
// used in diagnostic reports. |
||
731 |
5597 |
per_process::cli_options->cmdline = *argv; |
|
732 |
|||
733 |
#if defined(NODE_V8_OPTIONS) |
||
734 |
// Should come before the call to V8::SetFlagsFromCommandLine() |
||
735 |
// so the user can disable a flag --foo at run-time by passing |
||
736 |
// --no_foo from the command line. |
||
737 |
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1); |
||
738 |
#endif |
||
739 |
|||
740 |
5597 |
HandleEnvOptions(per_process::cli_options->per_isolate->per_env); |
|
741 |
|||
742 |
#if !defined(NODE_WITHOUT_NODE_OPTIONS) |
||
743 |
✓✗ | 5597 |
if (!(flags & ProcessInitializationFlags::kDisableNodeOptionsEnv)) { |
744 |
5597 |
std::string node_options; |
|
745 |
|||
746 |
✓✓ | 5597 |
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) { |
747 |
std::vector<std::string> env_argv = |
||
748 |
5476 |
ParseNodeOptionsEnvVar(node_options, errors); |
|
749 |
|||
750 |
✗✓ | 5476 |
if (!errors->empty()) return 9; |
751 |
|||
752 |
// [0] is expected to be the program name, fill it in from the real argv. |
||
753 |
5476 |
env_argv.insert(env_argv.begin(), argv->at(0)); |
|
754 |
|||
755 |
5476 |
const int exit_code = ProcessGlobalArgs(&env_argv, |
|
756 |
nullptr, |
||
757 |
errors, |
||
758 |
kAllowedInEnvironment); |
||
759 |
✓✓ | 5476 |
if (exit_code != 0) return exit_code; |
760 |
} |
||
761 |
} |
||
762 |
#endif |
||
763 |
|||
764 |
✓✗ | 5580 |
if (!(flags & ProcessInitializationFlags::kDisableCLIOptions)) { |
765 |
5580 |
const int exit_code = ProcessGlobalArgs(argv, |
|
766 |
exec_argv, |
||
767 |
errors, |
||
768 |
kDisallowedInEnvironment); |
||
769 |
✓✓ | 5580 |
if (exit_code != 0) return exit_code; |
770 |
} |
||
771 |
|||
772 |
// Set the process.title immediately after processing argv if --title is set. |
||
773 |
✓✓ | 5547 |
if (!per_process::cli_options->title.empty()) |
774 |
2 |
uv_set_process_title(per_process::cli_options->title.c_str()); |
|
775 |
|||
776 |
#if defined(NODE_HAVE_I18N_SUPPORT) |
||
777 |
✓✗ | 5547 |
if (!(flags & ProcessInitializationFlags::kNoICU)) { |
778 |
// If the parameter isn't given, use the env variable. |
||
779 |
✓✗ | 5547 |
if (per_process::cli_options->icu_data_dir.empty()) |
780 |
11094 |
credentials::SafeGetenv("NODE_ICU_DATA", |
|
781 |
5547 |
&per_process::cli_options->icu_data_dir); |
|
782 |
|||
783 |
#ifdef NODE_ICU_DEFAULT_DATA_DIR |
||
784 |
// If neither the CLI option nor the environment variable was specified, |
||
785 |
// fall back to the configured default |
||
786 |
if (per_process::cli_options->icu_data_dir.empty()) { |
||
787 |
// Check whether the NODE_ICU_DEFAULT_DATA_DIR contains the right data |
||
788 |
// file and can be read. |
||
789 |
static const char full_path[] = |
||
790 |
NODE_ICU_DEFAULT_DATA_DIR "/" U_ICUDATA_NAME ".dat"; |
||
791 |
|||
792 |
FILE* f = fopen(full_path, "rb"); |
||
793 |
|||
794 |
if (f != nullptr) { |
||
795 |
fclose(f); |
||
796 |
per_process::cli_options->icu_data_dir = NODE_ICU_DEFAULT_DATA_DIR; |
||
797 |
} |
||
798 |
} |
||
799 |
#endif // NODE_ICU_DEFAULT_DATA_DIR |
||
800 |
|||
801 |
// Initialize ICU. |
||
802 |
// If icu_data_dir is empty here, it will load the 'minimal' data. |
||
803 |
✗✓ | 5547 |
if (!i18n::InitializeICUDirectory(per_process::cli_options->icu_data_dir)) { |
804 |
errors->push_back("could not initialize ICU " |
||
805 |
"(check NODE_ICU_DATA or --icu-data-dir parameters)\n"); |
||
806 |
return 9; |
||
807 |
} |
||
808 |
5547 |
per_process::metadata.versions.InitializeIntlVersions(); |
|
809 |
} |
||
810 |
|||
811 |
# ifndef __POSIX__ |
||
812 |
std::string tz; |
||
813 |
if (credentials::SafeGetenv("TZ", &tz) && !tz.empty()) { |
||
814 |
i18n::SetDefaultTimeZone(tz.c_str()); |
||
815 |
} |
||
816 |
# endif |
||
817 |
|||
818 |
#endif // defined(NODE_HAVE_I18N_SUPPORT) |
||
819 |
|||
820 |
// We should set node_is_initialized here instead of in node::Start, |
||
821 |
// otherwise embedders using node::Init to initialize everything will not be |
||
822 |
// able to set it and native addons will not load for them. |
||
823 |
5547 |
node_is_initialized = true; |
|
824 |
5547 |
return 0; |
|
825 |
} |
||
826 |
|||
827 |
5596 |
std::unique_ptr<InitializationResult> InitializeOncePerProcess( |
|
828 |
const std::vector<std::string>& args, |
||
829 |
ProcessInitializationFlags::Flags flags) { |
||
830 |
11192 |
auto result = std::make_unique<InitializationResultImpl>(); |
|
831 |
5596 |
result->args_ = args; |
|
832 |
|||
833 |
✓✗ | 5596 |
if (!(flags & ProcessInitializationFlags::kNoParseGlobalDebugVariables)) { |
834 |
// Initialized the enabled list for Debug() calls with system |
||
835 |
// environment variables. |
||
836 |
5596 |
per_process::enabled_debug_list.Parse(); |
|
837 |
} |
||
838 |
|||
839 |
5596 |
PlatformInit(flags); |
|
840 |
|||
841 |
// This needs to run *before* V8::Initialize(). |
||
842 |
{ |
||
843 |
5596 |
result->exit_code_ = InitializeNodeWithArgs( |
|
844 |
5596 |
&result->args_, &result->exec_args_, &result->errors_, flags); |
|
845 |
✓✓ | 5596 |
if (result->exit_code() != 0) { |
846 |
50 |
result->early_return_ = true; |
|
847 |
50 |
return result; |
|
848 |
} |
||
849 |
} |
||
850 |
|||
851 |
✓✗✓✓ ✓✓ |
16637 |
if (!(flags & ProcessInitializationFlags::kNoUseLargePages) && |
852 |
✗✓ | 11091 |
(per_process::cli_options->use_largepages == "on" || |
853 |
5545 |
per_process::cli_options->use_largepages == "silent")) { |
|
854 |
1 |
int lp_result = node::MapStaticCodeToLargePages(); |
|
855 |
✓✗✗✓ ✗✓ |
1 |
if (per_process::cli_options->use_largepages == "on" && lp_result != 0) { |
856 |
result->errors_.emplace_back(node::LargePagesError(lp_result)); |
||
857 |
} |
||
858 |
} |
||
859 |
|||
860 |
✓✗ | 5546 |
if (!(flags & ProcessInitializationFlags::kNoPrintHelpOrVersionOutput)) { |
861 |
✓✓ | 5546 |
if (per_process::cli_options->print_version) { |
862 |
3 |
printf("%s\n", NODE_VERSION); |
|
863 |
3 |
result->exit_code_ = 0; |
|
864 |
3 |
result->early_return_ = true; |
|
865 |
3 |
return result; |
|
866 |
} |
||
867 |
|||
868 |
✓✓ | 5543 |
if (per_process::cli_options->print_bash_completion) { |
869 |
2 |
std::string completion = options_parser::GetBashCompletion(); |
|
870 |
1 |
printf("%s\n", completion.c_str()); |
|
871 |
1 |
result->exit_code_ = 0; |
|
872 |
1 |
result->early_return_ = true; |
|
873 |
1 |
return result; |
|
874 |
} |
||
875 |
|||
876 |
✗✓ | 5542 |
if (per_process::cli_options->print_v8_help) { |
877 |
V8::SetFlagsFromString("--help", static_cast<size_t>(6)); |
||
878 |
result->exit_code_ = 0; |
||
879 |
result->early_return_ = true; |
||
880 |
return result; |
||
881 |
} |
||
882 |
} |
||
883 |
|||
884 |
✓✗ | 5542 |
if (!(flags & ProcessInitializationFlags::kNoInitOpenSSL)) { |
885 |
#if HAVE_OPENSSL && !defined(OPENSSL_IS_BORINGSSL) |
||
886 |
2 |
auto GetOpenSSLErrorString = []() -> std::string { |
|
887 |
2 |
std::string ret; |
|
888 |
2 |
ERR_print_errors_cb( |
|
889 |
2 |
[](const char* str, size_t len, void* opaque) { |
|
890 |
2 |
std::string* ret = static_cast<std::string*>(opaque); |
|
891 |
2 |
ret->append(str, len); |
|
892 |
2 |
ret->append("\n"); |
|
893 |
2 |
return 0; |
|
894 |
}, |
||
895 |
static_cast<void*>(&ret)); |
||
896 |
2 |
return ret; |
|
897 |
}; |
||
898 |
|||
899 |
{ |
||
900 |
11084 |
std::string extra_ca_certs; |
|
901 |
✓✓ | 5542 |
if (credentials::SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs)) |
902 |
4 |
crypto::UseExtraCaCerts(extra_ca_certs); |
|
903 |
} |
||
904 |
// In the case of FIPS builds we should make sure |
||
905 |
// the random source is properly initialized first. |
||
906 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
907 |
// Call OPENSSL_init_crypto to initialize OPENSSL_INIT_LOAD_CONFIG to |
||
908 |
// avoid the default behavior where errors raised during the parsing of the |
||
909 |
// OpenSSL configuration file are not propagated and cannot be detected. |
||
910 |
// |
||
911 |
// If FIPS is configured the OpenSSL configuration file will have an |
||
912 |
// .include pointing to the fipsmodule.cnf file generated by the openssl |
||
913 |
// fipsinstall command. If the path to this file is incorrect no error |
||
914 |
// will be reported. |
||
915 |
// |
||
916 |
// For Node.js this will mean that EntropySource will be called by V8 as |
||
917 |
// part of its initialization process, and EntropySource will in turn call |
||
918 |
// CheckEntropy. CheckEntropy will call RAND_status which will now always |
||
919 |
// return 0, leading to an endless loop and the node process will appear to |
||
920 |
// hang/freeze. |
||
921 |
|||
922 |
// Passing NULL as the config file will allow the default openssl.cnf file |
||
923 |
// to be loaded, but the default section in that file will not be used, |
||
924 |
// instead only the section that matches the value of conf_section_name |
||
925 |
// will be read from the default configuration file. |
||
926 |
5542 |
const char* conf_file = nullptr; |
|
927 |
// To allow for using the previous default where the 'openssl_conf' appname |
||
928 |
// was used, the command line option 'openssl-shared-config' can be used to |
||
929 |
// force the old behavior. |
||
930 |
✗✓ | 5542 |
if (per_process::cli_options->openssl_shared_config) { |
931 |
conf_section_name = "openssl_conf"; |
||
932 |
} |
||
933 |
// Use OPENSSL_CONF environment variable is set. |
||
934 |
5542 |
std::string env_openssl_conf; |
|
935 |
5542 |
credentials::SafeGetenv("OPENSSL_CONF", &env_openssl_conf); |
|
936 |
✓✓ | 5542 |
if (!env_openssl_conf.empty()) { |
937 |
1 |
conf_file = env_openssl_conf.c_str(); |
|
938 |
} |
||
939 |
// Use --openssl-conf command line option if specified. |
||
940 |
✓✓ | 5542 |
if (!per_process::cli_options->openssl_config.empty()) { |
941 |
3 |
conf_file = per_process::cli_options->openssl_config.c_str(); |
|
942 |
} |
||
943 |
|||
944 |
5542 |
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new(); |
|
945 |
5542 |
OPENSSL_INIT_set_config_filename(settings, conf_file); |
|
946 |
5542 |
OPENSSL_INIT_set_config_appname(settings, conf_section_name); |
|
947 |
5542 |
OPENSSL_INIT_set_config_file_flags(settings, |
|
948 |
CONF_MFLAGS_IGNORE_MISSING_FILE); |
||
949 |
|||
950 |
5542 |
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, settings); |
|
951 |
5542 |
OPENSSL_INIT_free(settings); |
|
952 |
|||
953 |
✗✓ | 5542 |
if (ERR_peek_error() != 0) { |
954 |
// XXX: ERR_GET_REASON does not return something that is |
||
955 |
// useful as an exit code at all. |
||
956 |
result->exit_code_ = ERR_GET_REASON(ERR_peek_error()); |
||
957 |
result->early_return_ = true; |
||
958 |
result->errors_.emplace_back("OpenSSL configuration error:\n" + |
||
959 |
GetOpenSSLErrorString()); |
||
960 |
return result; |
||
961 |
} |
||
962 |
#else // OPENSSL_VERSION_MAJOR < 3 |
||
963 |
if (FIPS_mode()) { |
||
964 |
OPENSSL_init(); |
||
965 |
} |
||
966 |
#endif |
||
967 |
✓✓ | 5542 |
if (!crypto::ProcessFipsOptions()) { |
968 |
// XXX: ERR_GET_REASON does not return something that is |
||
969 |
// useful as an exit code at all. |
||
970 |
2 |
result->exit_code_ = ERR_GET_REASON(ERR_peek_error()); |
|
971 |
2 |
result->early_return_ = true; |
|
972 |
2 |
result->errors_.emplace_back( |
|
973 |
4 |
"OpenSSL error when trying to enable FIPS:\n" + |
|
974 |
6 |
GetOpenSSLErrorString()); |
|
975 |
2 |
return result; |
|
976 |
} |
||
977 |
|||
978 |
// Seed V8's PRNG from OpenSSL's pool. This is recommended by V8 and a |
||
979 |
// potentially better source of randomness than what V8 uses by default, but |
||
980 |
// it does not guarantee that pseudo-random values produced by V8 will be |
||
981 |
// cryptograhically secure. |
||
982 |
5540 |
V8::SetEntropySource(crypto::EntropySource); |
|
983 |
#endif // HAVE_OPENSSL && !defined(OPENSSL_IS_BORINGSSL) |
||
984 |
} |
||
985 |
|||
986 |
✓✓ | 5540 |
if (!(flags & ProcessInitializationFlags::kNoInitializeNodeV8Platform)) { |
987 |
5533 |
per_process::v8_platform.Initialize( |
|
988 |
5533 |
static_cast<int>(per_process::cli_options->v8_thread_pool_size)); |
|
989 |
5533 |
result->platform_ = per_process::v8_platform.Platform(); |
|
990 |
} |
||
991 |
|||
992 |
✓✓ | 5540 |
if (!(flags & ProcessInitializationFlags::kNoInitializeV8)) { |
993 |
V8::Initialize(); |
||
994 |
} |
||
995 |
|||
996 |
5540 |
performance::performance_v8_start = PERFORMANCE_NOW(); |
|
997 |
5540 |
per_process::v8_initialized = true; |
|
998 |
|||
999 |
5540 |
return result; |
|
1000 |
} |
||
1001 |
|||
1002 |
4879 |
void TearDownOncePerProcess() { |
|
1003 |
4879 |
const uint64_t flags = init_process_flags.load(); |
|
1004 |
4879 |
ResetStdio(); |
|
1005 |
✓✗ | 4879 |
if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling)) { |
1006 |
4879 |
ResetSignalHandlers(); |
|
1007 |
} |
||
1008 |
|||
1009 |
4879 |
per_process::v8_initialized = false; |
|
1010 |
✓✓ | 4879 |
if (!(flags & ProcessInitializationFlags::kNoInitializeV8)) { |
1011 |
4874 |
V8::Dispose(); |
|
1012 |
} |
||
1013 |
|||
1014 |
#if NODE_USE_V8_WASM_TRAP_HANDLER && defined(_WIN32) |
||
1015 |
if (!(flags & ProcessInitializationFlags::kNoDefaultSignalHandling)) { |
||
1016 |
RemoveVectoredExceptionHandler(per_process::old_vectored_exception_handler); |
||
1017 |
} |
||
1018 |
#endif |
||
1019 |
|||
1020 |
✓✓ | 4879 |
if (!(flags & ProcessInitializationFlags::kNoInitializeNodeV8Platform)) { |
1021 |
4874 |
V8::DisposePlatform(); |
|
1022 |
// uv_run cannot be called from the time before the beforeExit callback |
||
1023 |
// runs until the program exits unless the event loop has any referenced |
||
1024 |
// handles after beforeExit terminates. This prevents unrefed timers |
||
1025 |
// that happen to terminate during shutdown from being run unsafely. |
||
1026 |
// Since uv_run cannot be called, uv_async handles held by the platform |
||
1027 |
// will never be fully cleaned up. |
||
1028 |
4874 |
per_process::v8_platform.Dispose(); |
|
1029 |
} |
||
1030 |
4879 |
} |
|
1031 |
|||
1032 |
9870 |
InitializationResult::~InitializationResult() {} |
|
1033 |
9870 |
InitializationResultImpl::~InitializationResultImpl() {} |
|
1034 |
|||
1035 |
3 |
int GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr, |
|
1036 |
const InitializationResult* result) { |
||
1037 |
3 |
int exit_code = result->exit_code(); |
|
1038 |
// nullptr indicates there's no snapshot data. |
||
1039 |
DCHECK_NULL(*snapshot_data_ptr); |
||
1040 |
|||
1041 |
// node:embedded_snapshot_main indicates that we are using the |
||
1042 |
// embedded snapshot and we are not supposed to clean it up. |
||
1043 |
✓✓ | 3 |
if (result->args()[1] == "node:embedded_snapshot_main") { |
1044 |
2 |
*snapshot_data_ptr = SnapshotBuilder::GetEmbeddedSnapshotData(); |
|
1045 |
✗✓ | 2 |
if (*snapshot_data_ptr == nullptr) { |
1046 |
// The Node.js binary is built without embedded snapshot |
||
1047 |
fprintf(stderr, |
||
1048 |
"node:embedded_snapshot_main was specified as snapshot " |
||
1049 |
"entry point but Node.js was built without embedded " |
||
1050 |
"snapshot.\n"); |
||
1051 |
exit_code = 1; |
||
1052 |
return exit_code; |
||
1053 |
} |
||
1054 |
} else { |
||
1055 |
// Otherwise, load and run the specified main script. |
||
1056 |
std::unique_ptr<SnapshotData> generated_data = |
||
1057 |
1 |
std::make_unique<SnapshotData>(); |
|
1058 |
2 |
exit_code = node::SnapshotBuilder::Generate( |
|
1059 |
2 |
generated_data.get(), result->args(), result->exec_args()); |
|
1060 |
✗✓ | 1 |
if (exit_code == 0) { |
1061 |
*snapshot_data_ptr = generated_data.release(); |
||
1062 |
} else { |
||
1063 |
1 |
return exit_code; |
|
1064 |
} |
||
1065 |
} |
||
1066 |
|||
1067 |
// Get the path to write the snapshot blob to. |
||
1068 |
2 |
std::string snapshot_blob_path; |
|
1069 |
✓✓ | 2 |
if (!per_process::cli_options->snapshot_blob.empty()) { |
1070 |
1 |
snapshot_blob_path = per_process::cli_options->snapshot_blob; |
|
1071 |
} else { |
||
1072 |
// Defaults to snapshot.blob in the current working directory. |
||
1073 |
1 |
snapshot_blob_path = std::string("snapshot.blob"); |
|
1074 |
} |
||
1075 |
|||
1076 |
2 |
FILE* fp = fopen(snapshot_blob_path.c_str(), "wb"); |
|
1077 |
✓✗ | 2 |
if (fp != nullptr) { |
1078 |
2 |
(*snapshot_data_ptr)->ToBlob(fp); |
|
1079 |
2 |
fclose(fp); |
|
1080 |
} else { |
||
1081 |
fprintf(stderr, |
||
1082 |
"Cannot open %s for writing a snapshot.\n", |
||
1083 |
snapshot_blob_path.c_str()); |
||
1084 |
exit_code = 1; |
||
1085 |
} |
||
1086 |
2 |
return exit_code; |
|
1087 |
} |
||
1088 |
|||
1089 |
5523 |
int LoadSnapshotDataAndRun(const SnapshotData** snapshot_data_ptr, |
|
1090 |
const InitializationResult* result) { |
||
1091 |
5523 |
int exit_code = result->exit_code(); |
|
1092 |
// nullptr indicates there's no snapshot data. |
||
1093 |
DCHECK_NULL(*snapshot_data_ptr); |
||
1094 |
// --snapshot-blob indicates that we are reading a customized snapshot. |
||
1095 |
✓✓ | 5523 |
if (!per_process::cli_options->snapshot_blob.empty()) { |
1096 |
3 |
std::string filename = per_process::cli_options->snapshot_blob; |
|
1097 |
3 |
FILE* fp = fopen(filename.c_str(), "rb"); |
|
1098 |
✓✓ | 3 |
if (fp == nullptr) { |
1099 |
1 |
fprintf(stderr, "Cannot open %s", filename.c_str()); |
|
1100 |
1 |
exit_code = 1; |
|
1101 |
1 |
return exit_code; |
|
1102 |
} |
||
1103 |
2 |
std::unique_ptr<SnapshotData> read_data = std::make_unique<SnapshotData>(); |
|
1104 |
✗✓ | 2 |
if (!SnapshotData::FromBlob(read_data.get(), fp)) { |
1105 |
// If we fail to read the customized snapshot, simply exit with 1. |
||
1106 |
exit_code = 1; |
||
1107 |
return exit_code; |
||
1108 |
} |
||
1109 |
2 |
*snapshot_data_ptr = read_data.release(); |
|
1110 |
2 |
fclose(fp); |
|
1111 |
✓✓ | 5520 |
} else if (per_process::cli_options->node_snapshot) { |
1112 |
// If --snapshot-blob is not specified, we are reading the embedded |
||
1113 |
// snapshot, but we will skip it if --no-node-snapshot is specified. |
||
1114 |
const node::SnapshotData* read_data = |
||
1115 |
5518 |
SnapshotBuilder::GetEmbeddedSnapshotData(); |
|
1116 |
✓✗✓✗ ✓✗ |
5518 |
if (read_data != nullptr && read_data->Check()) { |
1117 |
// If we fail to read the embedded snapshot, treat it as if Node.js |
||
1118 |
// was built without one. |
||
1119 |
5518 |
*snapshot_data_ptr = read_data; |
|
1120 |
} |
||
1121 |
} |
||
1122 |
|||
1123 |
✓✓ | 5522 |
if ((*snapshot_data_ptr) != nullptr) { |
1124 |
5520 |
BuiltinLoader::RefreshCodeCache((*snapshot_data_ptr)->code_cache); |
|
1125 |
} |
||
1126 |
NodeMainInstance main_instance(*snapshot_data_ptr, |
||
1127 |
uv_default_loop(), |
||
1128 |
5522 |
per_process::v8_platform.Platform(), |
|
1129 |
5522 |
result->args(), |
|
1130 |
11044 |
result->exec_args()); |
|
1131 |
5522 |
exit_code = main_instance.Run(); |
|
1132 |
4863 |
return exit_code; |
|
1133 |
} |
||
1134 |
|||
1135 |
5583 |
int Start(int argc, char** argv) { |
|
1136 |
✗✓ | 5583 |
CHECK_GT(argc, 0); |
1137 |
|||
1138 |
// Hack around with the argv pointer. Used for process.title = "blah". |
||
1139 |
5583 |
argv = uv_setup_args(argc, argv); |
|
1140 |
|||
1141 |
std::unique_ptr<InitializationResult> result = |
||
1142 |
16090 |
InitializeOncePerProcess(std::vector<std::string>(argv, argv + argc)); |
|
1143 |
✓✓ | 5636 |
for (const std::string& error : result->errors()) { |
1144 |
53 |
FPrintF(stderr, "%s: %s\n", result->args().at(0), error); |
|
1145 |
} |
||
1146 |
✓✓ | 5583 |
if (result->early_return()) { |
1147 |
56 |
return result->exit_code(); |
|
1148 |
} |
||
1149 |
|||
1150 |
DCHECK_EQ(result->exit_code(), 0); |
||
1151 |
5527 |
const SnapshotData* snapshot_data = nullptr; |
|
1152 |
|||
1153 |
4868 |
auto cleanup_process = OnScopeLeave([&]() { |
|
1154 |
4868 |
TearDownOncePerProcess(); |
|
1155 |
|||
1156 |
✓✓ | 4868 |
if (snapshot_data != nullptr && |
1157 |
✓✓ | 4863 |
snapshot_data->data_ownership == SnapshotData::DataOwnership::kOwned) { |
1158 |
✓✗ | 2 |
delete snapshot_data; |
1159 |
} |
||
1160 |
10395 |
}); |
|
1161 |
|||
1162 |
5527 |
uv_loop_configure(uv_default_loop(), UV_METRICS_IDLE_TIME); |
|
1163 |
|||
1164 |
// --build-snapshot indicates that we are in snapshot building mode. |
||
1165 |
✓✓ | 5527 |
if (per_process::cli_options->build_snapshot) { |
1166 |
✓✓ | 4 |
if (result->args().size() < 2) { |
1167 |
1 |
fprintf(stderr, |
|
1168 |
"--build-snapshot must be used with an entry point script.\n" |
||
1169 |
"Usage: node --build-snapshot /path/to/entry.js\n"); |
||
1170 |
1 |
return 9; |
|
1171 |
} |
||
1172 |
3 |
return GenerateAndWriteSnapshotData(&snapshot_data, result.get()); |
|
1173 |
} |
||
1174 |
|||
1175 |
// Without --build-snapshot, we are in snapshot loading mode. |
||
1176 |
5523 |
return LoadSnapshotDataAndRun(&snapshot_data, result.get()); |
|
1177 |
} |
||
1178 |
|||
1179 |
380 |
int Stop(Environment* env) { |
|
1180 |
380 |
env->ExitEnv(); |
|
1181 |
380 |
return 0; |
|
1182 |
} |
||
1183 |
|||
1184 |
} // namespace node |
||
1185 |
|||
1186 |
#if !HAVE_INSPECTOR |
||
1187 |
void Initialize() {} |
||
1188 |
|||
1189 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector, Initialize) |
||
1190 |
#endif // !HAVE_INSPECTOR |
Generated by: GCOVR (Version 4.2) |