James M Snell | 98e54b0 | 2017-01-03 21:16:48 | [diff] [blame] | 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 | |
Refael Ackermann | f2064df | 2019-02-25 22:27:19 | [diff] [blame] | 22 | #include "node.h" |
| 23 | |
| 24 | // ========== local headers ========== |
| 25 | |
Anna Henningsen | 9cc747b | 2020-01-21 17:30:20 | [diff] [blame] | 26 | #include "debug_utils-inl.h" |
Sam Roberts | b6bfc19 | 2019-05-09 19:52:38 | [diff] [blame] | 27 | #include "env-inl.h" |
Sam Roberts | 2ecef9b | 2019-05-17 20:11:02 | [diff] [blame] | 28 | #include "memory_tracker-inl.h" |
Joyee Cheung | 3d66826 | 2018-11-28 16:46:45 | [diff] [blame] | 29 | #include "node_binding.h" |
Gus Caplan | 43fb6ff | 2020-01-11 22:03:59 | [diff] [blame] | 30 | #include "node_errors.h" |
Joyee Cheung | 5850220 | 2018-11-03 14:45:40 | [diff] [blame] | 31 | #include "node_internals.h" |
Joyee Cheung | cab1dc5 | 2019-04-14 06:41:04 | [diff] [blame] | 32 | #include "node_main_instance.h" |
Joyee Cheung | d17d7bd | 2018-12-01 20:16:09 | [diff] [blame] | 33 | #include "node_metadata.h" |
Joyee Cheung | dfd7e99 | 2019-04-09 21:08:48 | [diff] [blame] | 34 | #include "node_native_module_env.h" |
Joyee Cheung | 61a8963 | 2018-12-01 17:30:30 | [diff] [blame] | 35 | #include "node_options-inl.h" |
Joyee Cheung | bd765d6 | 2018-11-03 06:26:32 | [diff] [blame] | 36 | #include "node_perf.h" |
Joyee Cheung | 7824280 | 2018-12-31 15:18:08 | [diff] [blame] | 37 | #include "node_process.h" |
cjihrig | a6bd2c2 | 2020-03-13 01:32:07 | [diff] [blame] | 38 | #include "node_report.h" |
James M Snell | d387591 | 2016-02-04 01:16:10 | [diff] [blame] | 39 | #include "node_revert.h" |
Joyee Cheung | 99c3243 | 2019-01-16 18:34:43 | [diff] [blame] | 40 | #include "node_v8_platform-inl.h" |
Joyee Cheung | 5850220 | 2018-11-03 14:45:40 | [diff] [blame] | 41 | #include "node_version.h" |
Ben Noordhuis | 02cab97 | 2013-07-31 21:16:08 | [diff] [blame] | 42 | |
Ben Noordhuis | 02cab97 | 2013-07-31 21:16:08 | [diff] [blame] | 43 | #if HAVE_OPENSSL |
| 44 | #include "node_crypto.h" |
| 45 | #endif |
| 46 | |
Steven R. Loomis | ac2857b | 2014-09-05 05:03:24 | [diff] [blame] | 47 | #if defined(NODE_HAVE_I18N_SUPPORT) |
| 48 | #include "node_i18n.h" |
| 49 | #endif |
| 50 | |
Ben Noordhuis | 399cb25 | 2017-05-27 11:31:00 | [diff] [blame] | 51 | #if HAVE_INSPECTOR |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 52 | #include "inspector_agent.h" |
Ben Noordhuis | 399cb25 | 2017-05-27 11:31:00 | [diff] [blame] | 53 | #include "inspector_io.h" |
| 54 | #endif |
| 55 | |
Ben Noordhuis | c4def50 | 2013-10-28 19:18:59 | [diff] [blame] | 56 | #if defined HAVE_DTRACE || defined HAVE_ETW |
Ben Noordhuis | 02cab97 | 2013-07-31 21:16:08 | [diff] [blame] | 57 | #include "node_dtrace.h" |
| 58 | #endif |
| 59 | |
Stefan Budeanu | 410296c | 2016-03-27 00:17:55 | [diff] [blame] | 60 | #if NODE_USE_V8_PLATFORM |
Ben Noordhuis | 4a801c2 | 2015-04-02 21:51:01 | [diff] [blame] | 61 | #include "libplatform/libplatform.h" |
Stefan Budeanu | 410296c | 2016-03-27 00:17:55 | [diff] [blame] | 62 | #endif // NODE_USE_V8_PLATFORM |
Ben Noordhuis | 57231d5 | 2013-10-02 04:37:44 | [diff] [blame] | 63 | #include "v8-profiler.h" |
Ryan Dahl | 4635ed7 | 2010-03-11 20:40:19 | [diff] [blame] | 64 | |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 65 | #if HAVE_INSPECTOR |
| 66 | #include "inspector/worker_inspector.h" // ParentInspectorHandle |
| 67 | #endif |
| 68 | |
Suresh Srinivas | bf7ed80 | 2018-03-23 19:16:48 | [diff] [blame] | 69 | #include "large_pages/node_large_page.h" |
Suresh Srinivas | bf7ed80 | 2018-03-23 19:16:48 | [diff] [blame] | 70 | |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 71 | #if defined(__APPLE__) || defined(__linux__) |
| 72 | #define NODE_USE_V8_WASM_TRAP_HANDLER 1 |
| 73 | #else |
| 74 | #define NODE_USE_V8_WASM_TRAP_HANDLER 0 |
| 75 | #endif |
| 76 | |
| 77 | #if NODE_USE_V8_WASM_TRAP_HANDLER |
| 78 | #include <atomic> |
| 79 | #include "v8-wasm-trap-handler-posix.h" |
| 80 | #endif // NODE_USE_V8_WASM_TRAP_HANDLER |
| 81 | |
Refael Ackermann | f2064df | 2019-02-25 22:27:19 | [diff] [blame] | 82 | // ========== global C headers ========== |
| 83 | |
| 84 | #include <fcntl.h> // _O_RDWR |
| 85 | #include <sys/types.h> |
Ben Noordhuis | 02cab97 | 2013-07-31 21:16:08 | [diff] [blame] | 86 | |
Evan Lucas | 30b8bb0 | 2015-09-27 15:59:02 | [diff] [blame] | 87 | #if defined(NODE_HAVE_I18N_SUPPORT) |
| 88 | #include <unicode/uvernum.h> |
Stephen Gallagher | d502b83 | 2019-12-06 21:40:25 | [diff] [blame] | 89 | #include <unicode/utypes.h> |
Evan Lucas | 30b8bb0 | 2015-09-27 15:59:02 | [diff] [blame] | 90 | #endif |
| 91 | |
Gireesh Punathil | 4f67973 | 2018-09-05 14:06:59 | [diff] [blame] | 92 | |
Karl Skomski | 6ed0603 | 2015-08-14 08:07:18 | [diff] [blame] | 93 | #if defined(LEAK_SANITIZER) |
| 94 | #include <sanitizer/lsan_interface.h> |
| 95 | #endif |
| 96 | |
Ben Noordhuis | 02cab97 | 2013-07-31 21:16:08 | [diff] [blame] | 97 | #if defined(_MSC_VER) |
Peter Bright | 13d6a1f | 2011-08-06 04:23:25 | [diff] [blame] | 98 | #include <direct.h> |
Peter Bright | b9d7777 | 2011-08-11 01:45:56 | [diff] [blame] | 99 | #include <io.h> |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 100 | #define STDIN_FILENO 0 |
Ben Noordhuis | 02cab97 | 2013-07-31 21:16:08 | [diff] [blame] | 101 | #else |
Ben Noordhuis | 844f0a9 | 2016-03-25 16:59:07 | [diff] [blame] | 102 | #include <pthread.h> |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 103 | #ifndef __Fuchsia__ |
Ben Noordhuis | 6820054 | 2013-10-02 10:17:57 | [diff] [blame] | 104 | #include <sys/resource.h> // getrlimit, setrlimit |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 105 | #endif |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 106 | #include <termios.h> // tcgetattr, tcsetattr |
Joyee Cheung | 321e296 | 2018-12-15 19:13:12 | [diff] [blame] | 107 | #include <unistd.h> // STDIN_FILENO, STDERR_FILENO |
Bert Belder | a177d60 | 2010-11-25 00:02:55 | [diff] [blame] | 108 | #endif |
| 109 | |
Refael Ackermann | f2064df | 2019-02-25 22:27:19 | [diff] [blame] | 110 | // ========== global C++ headers ========== |
| 111 | |
| 112 | #include <cerrno> |
| 113 | #include <climits> // PATH_MAX |
| 114 | #include <csignal> |
| 115 | #include <cstdio> |
| 116 | #include <cstdlib> |
| 117 | #include <cstring> |
| 118 | |
| 119 | #include <string> |
| 120 | #include <vector> |
| 121 | |
Ryan | d6c9d31 | 2009-09-11 14:02:29 | [diff] [blame] | 122 | namespace node { |
| 123 | |
Joyee Cheung | dfd7e99 | 2019-04-09 21:08:48 | [diff] [blame] | 124 | using native_module::NativeModuleEnv; |
Joyee Cheung | dfd7e99 | 2019-04-09 21:08:48 | [diff] [blame] | 125 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 126 | using v8::EscapableHandleScope; |
Ben Noordhuis | 110a9cd | 2013-07-03 02:23:44 | [diff] [blame] | 127 | using v8::Function; |
| 128 | using v8::FunctionCallbackInfo; |
Ben Noordhuis | 110a9cd | 2013-07-03 02:23:44 | [diff] [blame] | 129 | using v8::HandleScope; |
Ben Noordhuis | 110a9cd | 2013-07-03 02:23:44 | [diff] [blame] | 130 | using v8::Isolate; |
Ben Noordhuis | 511af4d | 2013-07-30 19:28:43 | [diff] [blame] | 131 | using v8::Local; |
Anna Henningsen | f3cd537 | 2017-12-01 23:04:56 | [diff] [blame] | 132 | using v8::Maybe; |
Michaƫl Zasso | 023c317 | 2016-02-08 21:34:05 | [diff] [blame] | 133 | using v8::MaybeLocal; |
Ben Noordhuis | 110a9cd | 2013-07-03 02:23:44 | [diff] [blame] | 134 | using v8::Object; |
Ben Noordhuis | 511af4d | 2013-07-30 19:28:43 | [diff] [blame] | 135 | using v8::String; |
Anna Henningsen | 64616bb | 2017-08-08 18:02:55 | [diff] [blame] | 136 | using v8::Undefined; |
Ben Noordhuis | 110a9cd | 2013-07-03 02:23:44 | [diff] [blame] | 137 | using v8::V8; |
| 138 | using v8::Value; |
Ben Noordhuis | 110a9cd | 2013-07-03 02:23:44 | [diff] [blame] | 139 | |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 140 | namespace per_process { |
James M Snell | 35f6e59 | 2017-08-16 16:34:37 | [diff] [blame] | 141 | |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 142 | // node_revert.h |
| 143 | // Bit flag used to track security reverts. |
| 144 | unsigned int reverted_cve = 0; |
| 145 | |
| 146 | // util.h |
| 147 | // Tells whether the per-process V8::Initialize() is called and |
| 148 | // if it is safe to call v8::Isolate::GetCurrent(). |
Anna Henningsen | 72c60e8 | 2016-09-10 16:21:20 | [diff] [blame] | 149 | bool v8_initialized = false; |
| 150 | |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 151 | // node_internals.h |
Joyee Cheung | fd0a861 | 2019-02-09 15:48:14 | [diff] [blame] | 152 | // process-relative uptime base in nanoseconds, initialized in node::Start() |
| 153 | uint64_t node_start_time; |
Joyee Cheung | ca9e24e | 2019-01-16 19:00:55 | [diff] [blame] | 154 | // Tells whether --prof is passed. |
| 155 | bool v8_is_profiling = false; |
Joyee Cheung | 99c3243 | 2019-01-16 18:34:43 | [diff] [blame] | 156 | |
| 157 | // node_v8_platform-inl.h |
| 158 | struct V8Platform v8_platform; |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 159 | } // namespace per_process |
Stefan Budeanu | 410296c | 2016-03-27 00:17:55 | [diff] [blame] | 160 | |
Ben Noordhuis | 844f0a9 | 2016-03-25 16:59:07 | [diff] [blame] | 161 | #ifdef __POSIX__ |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 162 | void SignalExit(int signo, siginfo_t* info, void* ucontext) { |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 163 | ResetStdio(); |
Geir Hauge | c61b0e9 | 2014-03-31 07:52:03 | [diff] [blame] | 164 | raise(signo); |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 165 | } |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 166 | #endif // __POSIX__ |
Dean McNamee | f67e8f2 | 2011-03-15 22:39:16 | [diff] [blame] | 167 | |
Joyee Cheung | 1944265 | 2019-03-19 16:12:23 | [diff] [blame] | 168 | MaybeLocal<Value> ExecuteBootstrapper(Environment* env, |
| 169 | const char* id, |
| 170 | std::vector<Local<String>>* parameters, |
| 171 | std::vector<Local<Value>>* arguments) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 172 | EscapableHandleScope scope(env->isolate()); |
| 173 | MaybeLocal<Function> maybe_fn = |
Joyee Cheung | dfd7e99 | 2019-04-09 21:08:48 | [diff] [blame] | 174 | NativeModuleEnv::LookupAndCompile(env->context(), id, parameters, env); |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 175 | |
| 176 | if (maybe_fn.IsEmpty()) { |
| 177 | return MaybeLocal<Value>(); |
| 178 | } |
| 179 | |
| 180 | Local<Function> fn = maybe_fn.ToLocalChecked(); |
| 181 | MaybeLocal<Value> result = fn->Call(env->context(), |
| 182 | Undefined(env->isolate()), |
| 183 | arguments->size(), |
| 184 | arguments->data()); |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 185 | |
Joyee Cheung | a33c3c6 | 2019-06-15 00:07:15 | [diff] [blame] | 186 | // If there was an error during bootstrap, it must be unrecoverable |
| 187 | // (e.g. max call stack exceeded). Clear the stack so that the |
| 188 | // AsyncCallbackScope destructor doesn't fail on the id check. |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 189 | // There are only two ways to have a stack size > 1: 1) the user manually |
| 190 | // called MakeCallback or 2) user awaited during bootstrap, which triggered |
| 191 | // _tickCallback(). |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 192 | if (result.IsEmpty()) { |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 193 | env->async_hooks()->clear_async_id_stack(); |
| 194 | } |
| 195 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 196 | return scope.EscapeMaybe(result); |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 197 | } |
| 198 | |
Shelley Vohr | 71b342f | 2019-10-18 17:56:24 | [diff] [blame] | 199 | #if HAVE_INSPECTOR |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 200 | int Environment::InitializeInspector( |
Anna Henningsen | 86cdfc6 | 2019-11-02 20:04:57 | [diff] [blame] | 201 | std::unique_ptr<inspector::ParentInspectorHandle> parent_handle) { |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 202 | std::string inspector_path; |
Anna Henningsen | a9fb51f | 2019-11-11 14:24:13 | [diff] [blame] | 203 | bool is_main = !parent_handle; |
Anna Henningsen | 86cdfc6 | 2019-11-02 20:04:57 | [diff] [blame] | 204 | if (parent_handle) { |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 205 | inspector_path = parent_handle->url(); |
Anna Henningsen | 86cdfc6 | 2019-11-02 20:04:57 | [diff] [blame] | 206 | inspector_agent_->SetParentHandle(std::move(parent_handle)); |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 207 | } else { |
| 208 | inspector_path = argv_.size() > 1 ? argv_[1].c_str() : ""; |
| 209 | } |
| 210 | |
| 211 | CHECK(!inspector_agent_->IsListening()); |
| 212 | // Inspector agent can't fail to start, but if it was configured to listen |
| 213 | // right away on the websocket port and fails to bind/etc, this will return |
| 214 | // false. |
| 215 | inspector_agent_->Start(inspector_path, |
| 216 | options_->debug_options(), |
| 217 | inspector_host_port(), |
Anna Henningsen | a9fb51f | 2019-11-11 14:24:13 | [diff] [blame] | 218 | is_main); |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 219 | if (options_->debug_options().inspector_enabled && |
| 220 | !inspector_agent_->IsListening()) { |
| 221 | return 12; // Signal internal error |
| 222 | } |
| 223 | |
| 224 | profiler::StartProfilers(this); |
| 225 | |
Samuel Attard | 4208158 | 2019-06-19 18:44:49 | [diff] [blame] | 226 | if (inspector_agent_->options().break_node_first_line) { |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 227 | inspector_agent_->PauseOnNextJavascriptStatement("Break at bootstrap"); |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | #endif // HAVE_INSPECTOR && NODE_USE_V8_PLATFORM |
| 233 | |
| 234 | void Environment::InitializeDiagnostics() { |
| 235 | isolate_->GetHeapProfiler()->AddBuildEmbedderGraphCallback( |
| 236 | Environment::BuildEmbedderGraph, this); |
Anna Henningsen | 31217a8 | 2019-10-18 15:05:35 | [diff] [blame] | 237 | if (options_->trace_uncaught) |
| 238 | isolate_->SetCaptureStackTraceForUncaughtExceptions(true); |
Joyee Cheung | a0d86be | 2019-05-20 11:39:21 | [diff] [blame] | 239 | |
| 240 | #if defined HAVE_DTRACE || defined HAVE_ETW |
| 241 | InitDTrace(this); |
| 242 | #endif |
| 243 | } |
| 244 | |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 245 | MaybeLocal<Value> Environment::BootstrapInternalLoaders() { |
| 246 | EscapableHandleScope scope(isolate_); |
Joyee Cheung | 39d9221 | 2019-01-30 14:21:07 | [diff] [blame] | 247 | |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 248 | // Create binding loaders |
Joyee Cheung | edcb950 | 2018-11-26 17:50:41 | [diff] [blame] | 249 | std::vector<Local<String>> loaders_params = { |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 250 | process_string(), |
| 251 | FIXED_ONE_BYTE_STRING(isolate_, "getLinkedBinding"), |
| 252 | FIXED_ONE_BYTE_STRING(isolate_, "getInternalBinding"), |
| 253 | primordials_string()}; |
Joyee Cheung | edcb950 | 2018-11-26 17:50:41 | [diff] [blame] | 254 | std::vector<Local<Value>> loaders_args = { |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 255 | process_object(), |
| 256 | NewFunctionTemplate(binding::GetLinkedBinding) |
| 257 | ->GetFunction(context()) |
Joyee Cheung | edcb950 | 2018-11-26 17:50:41 | [diff] [blame] | 258 | .ToLocalChecked(), |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 259 | NewFunctionTemplate(binding::GetInternalBinding) |
| 260 | ->GetFunction(context()) |
Joyee Cheung | edcb950 | 2018-11-26 17:50:41 | [diff] [blame] | 261 | .ToLocalChecked(), |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 262 | primordials()}; |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 263 | |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 264 | // Bootstrap internal loaders |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 265 | Local<Value> loader_exports; |
| 266 | if (!ExecuteBootstrapper( |
| 267 | this, "internal/bootstrap/loaders", &loaders_params, &loaders_args) |
| 268 | .ToLocal(&loader_exports)) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 269 | return MaybeLocal<Value>(); |
Joyee Cheung | 2a9eb31 | 2018-03-03 20:55:45 | [diff] [blame] | 270 | } |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 271 | CHECK(loader_exports->IsObject()); |
| 272 | Local<Object> loader_exports_obj = loader_exports.As<Object>(); |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 273 | Local<Value> internal_binding_loader = |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 274 | loader_exports_obj->Get(context(), internal_binding_string()) |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 275 | .ToLocalChecked(); |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 276 | CHECK(internal_binding_loader->IsFunction()); |
| 277 | set_internal_binding_loader(internal_binding_loader.As<Function>()); |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 278 | Local<Value> require = |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 279 | loader_exports_obj->Get(context(), require_string()).ToLocalChecked(); |
| 280 | CHECK(require->IsFunction()); |
| 281 | set_native_module_require(require.As<Function>()); |
| 282 | |
| 283 | return scope.Escape(loader_exports); |
| 284 | } |
| 285 | |
| 286 | MaybeLocal<Value> Environment::BootstrapNode() { |
| 287 | EscapableHandleScope scope(isolate_); |
| 288 | |
| 289 | Local<Object> global = context()->Global(); |
| 290 | // TODO(joyeecheung): this can be done in JS land now. |
| 291 | global->Set(context(), FIXED_ONE_BYTE_STRING(isolate_, "global"), global) |
| 292 | .Check(); |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 293 | |
Joyee Cheung | c63d511 | 2019-12-09 02:53:02 | [diff] [blame] | 294 | // process, require, internalBinding, primordials |
Joyee Cheung | edcb950 | 2018-11-26 17:50:41 | [diff] [blame] | 295 | std::vector<Local<String>> node_params = { |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 296 | process_string(), |
| 297 | require_string(), |
| 298 | internal_binding_string(), |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 299 | primordials_string()}; |
Joyee Cheung | edcb950 | 2018-11-26 17:50:41 | [diff] [blame] | 300 | std::vector<Local<Value>> node_args = { |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 301 | process_object(), |
| 302 | native_module_require(), |
| 303 | internal_binding_loader(), |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 304 | primordials()}; |
Joyee Cheung | edcb950 | 2018-11-26 17:50:41 | [diff] [blame] | 305 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 306 | MaybeLocal<Value> result = ExecuteBootstrapper( |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 307 | this, "internal/bootstrap/node", &node_params, &node_args); |
Anna Henningsen | f6a1d88 | 2019-01-02 21:14:23 | [diff] [blame] | 308 | |
Joyee Cheung | c63d511 | 2019-12-09 02:53:02 | [diff] [blame] | 309 | if (result.IsEmpty()) { |
| 310 | return scope.EscapeMaybe(result); |
| 311 | } |
| 312 | |
Denys Otrishko | 08f5726 | 2020-01-22 19:21:22 | [diff] [blame] | 313 | auto thread_switch_id = |
| 314 | is_main_thread() ? "internal/bootstrap/switches/is_main_thread" |
| 315 | : "internal/bootstrap/switches/is_not_main_thread"; |
| 316 | result = |
| 317 | ExecuteBootstrapper(this, thread_switch_id, &node_params, &node_args); |
Joyee Cheung | c63d511 | 2019-12-09 02:53:02 | [diff] [blame] | 318 | |
| 319 | if (result.IsEmpty()) { |
| 320 | return scope.EscapeMaybe(result); |
| 321 | } |
| 322 | |
Denys Otrishko | 08f5726 | 2020-01-22 19:21:22 | [diff] [blame] | 323 | auto process_state_switch_id = |
| 324 | owns_process_state() |
| 325 | ? "internal/bootstrap/switches/does_own_process_state" |
| 326 | : "internal/bootstrap/switches/does_not_own_process_state"; |
| 327 | result = ExecuteBootstrapper( |
| 328 | this, process_state_switch_id, &node_params, &node_args); |
Joyee Cheung | c63d511 | 2019-12-09 02:53:02 | [diff] [blame] | 329 | |
| 330 | if (result.IsEmpty()) { |
| 331 | return scope.EscapeMaybe(result); |
| 332 | } |
| 333 | |
Denys Otrishko | 08f5726 | 2020-01-22 19:21:22 | [diff] [blame] | 334 | Local<String> env_string = FIXED_ONE_BYTE_STRING(isolate_, "env"); |
Joyee Cheung | ee2d263 | 2019-03-06 14:25:18 | [diff] [blame] | 335 | Local<Object> env_var_proxy; |
Anna Henningsen | 6576b9b | 2019-03-05 00:28:45 | [diff] [blame] | 336 | if (!CreateEnvVarProxy(context(), isolate_, current_callback_data()) |
Joyee Cheung | ee2d263 | 2019-03-06 14:25:18 | [diff] [blame] | 337 | .ToLocal(&env_var_proxy) || |
Denys Otrishko | 08f5726 | 2020-01-22 19:21:22 | [diff] [blame] | 338 | process_object()->Set(context(), env_string, env_var_proxy).IsNothing()) { |
Joyee Cheung | ee2d263 | 2019-03-06 14:25:18 | [diff] [blame] | 339 | return MaybeLocal<Value>(); |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | return scope.EscapeMaybe(result); |
| 343 | } |
| 344 | |
| 345 | MaybeLocal<Value> Environment::RunBootstrapping() { |
| 346 | EscapableHandleScope scope(isolate_); |
| 347 | |
| 348 | CHECK(!has_run_bootstrapping_code()); |
| 349 | |
| 350 | if (BootstrapInternalLoaders().IsEmpty()) { |
| 351 | return MaybeLocal<Value>(); |
| 352 | } |
| 353 | |
| 354 | Local<Value> result; |
| 355 | if (!BootstrapNode().ToLocal(&result)) { |
| 356 | return MaybeLocal<Value>(); |
| 357 | } |
Joyee Cheung | ee2d263 | 2019-03-06 14:25:18 | [diff] [blame] | 358 | |
Joyee Cheung | 197dbf6 | 2019-03-11 15:52:47 | [diff] [blame] | 359 | // Make sure that no request or handle is created during bootstrap - |
gengjiawen | 3b48ce6 | 2019-05-06 13:58:16 | [diff] [blame] | 360 | // if necessary those should be done in pre-execution. |
Anna Henningsen | 850f381 | 2019-11-12 18:33:37 | [diff] [blame] | 361 | // Usually, doing so would trigger the checks present in the ReqWrap and |
| 362 | // HandleWrap classes, so this is only a consistency check. |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 363 | CHECK(req_wrap_queue()->IsEmpty()); |
| 364 | CHECK(handle_wrap_queue()->IsEmpty()); |
Joyee Cheung | 197dbf6 | 2019-03-11 15:52:47 | [diff] [blame] | 365 | |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 366 | set_has_run_bootstrapping_code(true); |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 367 | |
Joyee Cheung | 0778599 | 2019-05-20 11:50:13 | [diff] [blame] | 368 | return scope.Escape(result); |
Anna Henningsen | f6a1d88 | 2019-01-02 21:14:23 | [diff] [blame] | 369 | } |
| 370 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 371 | void MarkBootstrapComplete(const FunctionCallbackInfo<Value>& args) { |
| 372 | Environment* env = Environment::GetCurrent(args); |
| 373 | env->performance_state()->Mark( |
| 374 | performance::NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE); |
| 375 | } |
Anna Henningsen | f6a1d88 | 2019-01-02 21:14:23 | [diff] [blame] | 376 | |
Anna Henningsen | c44edec | 2019-11-13 15:54:57 | [diff] [blame] | 377 | static |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 378 | MaybeLocal<Value> StartExecution(Environment* env, const char* main_script_id) { |
| 379 | EscapableHandleScope scope(env->isolate()); |
ZYSzys | 93e0c6a | 2019-02-04 07:38:51 | [diff] [blame] | 380 | CHECK_NOT_NULL(main_script_id); |
Joyee Cheung | 2c7f4f4 | 2019-01-13 15:44:09 | [diff] [blame] | 381 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 382 | std::vector<Local<String>> parameters = { |
| 383 | env->process_string(), |
| 384 | env->require_string(), |
| 385 | env->internal_binding_string(), |
Michaƫl Zasso | 908292c | 2019-04-09 07:55:53 | [diff] [blame] | 386 | env->primordials_string(), |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 387 | FIXED_ONE_BYTE_STRING(env->isolate(), "markBootstrapComplete")}; |
| 388 | |
| 389 | std::vector<Local<Value>> arguments = { |
| 390 | env->process_object(), |
| 391 | env->native_module_require(), |
| 392 | env->internal_binding_loader(), |
Michaƫl Zasso | 908292c | 2019-04-09 07:55:53 | [diff] [blame] | 393 | env->primordials(), |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 394 | env->NewFunctionTemplate(MarkBootstrapComplete) |
| 395 | ->GetFunction(env->context()) |
| 396 | .ToLocalChecked()}; |
| 397 | |
Anna Henningsen | d80e49d | 2019-11-03 12:00:24 | [diff] [blame] | 398 | return scope.EscapeMaybe( |
| 399 | ExecuteBootstrapper(env, main_script_id, ¶meters, &arguments)); |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 400 | } |
| 401 | |
Anna Henningsen | c44edec | 2019-11-13 15:54:57 | [diff] [blame] | 402 | MaybeLocal<Value> StartExecution(Environment* env, StartExecutionCallback cb) { |
| 403 | InternalCallbackScope callback_scope( |
| 404 | env, |
| 405 | Object::New(env->isolate()), |
| 406 | { 1, 0 }, |
| 407 | InternalCallbackScope::kSkipAsyncHooks); |
| 408 | |
| 409 | if (cb != nullptr) { |
| 410 | EscapableHandleScope scope(env->isolate()); |
| 411 | |
| 412 | if (StartExecution(env, "internal/bootstrap/environment").IsEmpty()) |
| 413 | return {}; |
| 414 | |
| 415 | StartExecutionCallbackInfo info = { |
| 416 | env->process_object(), |
| 417 | env->native_module_require(), |
| 418 | }; |
| 419 | |
| 420 | return scope.EscapeMaybe(cb(info)); |
| 421 | } |
| 422 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 423 | // To allow people to extend Node in different ways, this hook allows |
| 424 | // one to drop a file lib/_third_party_main.js into the build |
| 425 | // directory which will be executed instead of Node's normal loading. |
Joyee Cheung | dfd7e99 | 2019-04-09 21:08:48 | [diff] [blame] | 426 | if (NativeModuleEnv::Exists("_third_party_main")) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 427 | return StartExecution(env, "internal/main/run_third_party_main"); |
Joyee Cheung | 2c7f4f4 | 2019-01-13 15:44:09 | [diff] [blame] | 428 | } |
| 429 | |
Anna Henningsen | a9fb51f | 2019-11-11 14:24:13 | [diff] [blame] | 430 | if (env->worker_context() != nullptr) { |
| 431 | return StartExecution(env, "internal/main/worker_thread"); |
| 432 | } |
| 433 | |
Joyee Cheung | d005f38 | 2019-03-19 23:55:59 | [diff] [blame] | 434 | std::string first_argv; |
| 435 | if (env->argv().size() > 1) { |
| 436 | first_argv = env->argv()[1]; |
| 437 | } |
| 438 | |
| 439 | if (first_argv == "inspect" || first_argv == "debug") { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 440 | return StartExecution(env, "internal/main/inspect"); |
| 441 | } |
| 442 | |
| 443 | if (per_process::cli_options->print_help) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 444 | return StartExecution(env, "internal/main/print_help"); |
| 445 | } |
| 446 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 447 | |
| 448 | if (env->options()->prof_process) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 449 | return StartExecution(env, "internal/main/prof_process"); |
| 450 | } |
| 451 | |
| 452 | // -e/--eval without -i/--interactive |
| 453 | if (env->options()->has_eval_string && !env->options()->force_repl) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 454 | return StartExecution(env, "internal/main/eval_string"); |
| 455 | } |
| 456 | |
| 457 | if (env->options()->syntax_check_only) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 458 | return StartExecution(env, "internal/main/check_syntax"); |
| 459 | } |
| 460 | |
Joyee Cheung | d005f38 | 2019-03-19 23:55:59 | [diff] [blame] | 461 | if (!first_argv.empty() && first_argv != "-") { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 462 | return StartExecution(env, "internal/main/run_main_module"); |
| 463 | } |
| 464 | |
| 465 | if (env->options()->force_repl || uv_guess_handle(STDIN_FILENO) == UV_TTY) { |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 466 | return StartExecution(env, "internal/main/repl"); |
| 467 | } |
| 468 | |
Joyee Cheung | 6967f91 | 2019-01-15 15:12:21 | [diff] [blame] | 469 | return StartExecution(env, "internal/main/eval_stdin"); |
| 470 | } |
| 471 | |
Anna Henningsen | 89b3237 | 2019-05-20 00:03:48 | [diff] [blame] | 472 | #ifdef __POSIX__ |
| 473 | typedef void (*sigaction_cb)(int signo, siginfo_t* info, void* ucontext); |
| 474 | #endif |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 475 | #if NODE_USE_V8_WASM_TRAP_HANDLER |
Anna Henningsen | 89b3237 | 2019-05-20 00:03:48 | [diff] [blame] | 476 | static std::atomic<sigaction_cb> previous_sigsegv_action; |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 477 | |
| 478 | void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) { |
| 479 | if (!v8::TryHandleWebAssemblyTrapPosix(signo, info, ucontext)) { |
Anna Henningsen | 89b3237 | 2019-05-20 00:03:48 | [diff] [blame] | 480 | sigaction_cb prev = previous_sigsegv_action.load(); |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 481 | if (prev != nullptr) { |
| 482 | prev(signo, info, ucontext); |
| 483 | } else { |
Anna Henningsen | e256204 | 2019-05-19 23:57:17 | [diff] [blame] | 484 | // Reset to the default signal handler, i.e. cause a hard crash. |
| 485 | struct sigaction sa; |
| 486 | memset(&sa, 0, sizeof(sa)); |
| 487 | sa.sa_handler = SIG_DFL; |
| 488 | CHECK_EQ(sigaction(signo, &sa, nullptr), 0); |
| 489 | |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 490 | ResetStdio(); |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 491 | raise(signo); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | #endif // NODE_USE_V8_WASM_TRAP_HANDLER |
Ryan Dahl | 2a7e7b1 | 2010-12-18 19:17:29 | [diff] [blame] | 496 | |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 497 | #if defined(POSIX) && !defined(__Fuchsia__) |
Anna Henningsen | 0815b94 | 2016-05-08 01:28:47 | [diff] [blame] | 498 | void RegisterSignalHandler(int signal, |
Anna Henningsen | 89b3237 | 2019-05-20 00:03:48 | [diff] [blame] | 499 | sigaction_cb handler, |
Anna Henningsen | 0815b94 | 2016-05-08 01:28:47 | [diff] [blame] | 500 | bool reset_handler) { |
Anna Henningsen | 89b3237 | 2019-05-20 00:03:48 | [diff] [blame] | 501 | CHECK_NOT_NULL(handler); |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 502 | #if NODE_USE_V8_WASM_TRAP_HANDLER |
| 503 | if (signal == SIGSEGV) { |
| 504 | CHECK(previous_sigsegv_action.is_lock_free()); |
Anna Henningsen | 89b3237 | 2019-05-20 00:03:48 | [diff] [blame] | 505 | CHECK(!reset_handler); |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 506 | previous_sigsegv_action.store(handler); |
| 507 | return; |
| 508 | } |
| 509 | #endif // NODE_USE_V8_WASM_TRAP_HANDLER |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 510 | struct sigaction sa; |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 511 | memset(&sa, 0, sizeof(sa)); |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 512 | sa.sa_sigaction = handler; |
Geir Hauge | c61b0e9 | 2014-03-31 07:52:03 | [diff] [blame] | 513 | sa.sa_flags = reset_handler ? SA_RESETHAND : 0; |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 514 | sigfillset(&sa.sa_mask); |
Ben Noordhuis | 2d82cdf | 2014-10-22 01:29:32 | [diff] [blame] | 515 | CHECK_EQ(sigaction(signal, &sa, nullptr), 0); |
Bert Belder | 829735e | 2011-11-04 15:23:02 | [diff] [blame] | 516 | } |
| 517 | |
Fedor Indutny | 8e29ce9 | 2013-07-31 18:07:29 | [diff] [blame] | 518 | #endif // __POSIX__ |
Tom Hughes | f61b110 | 2010-10-12 21:01:58 | [diff] [blame] | 519 | |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 520 | #ifdef __POSIX__ |
| 521 | static struct { |
| 522 | int flags; |
| 523 | bool isatty; |
| 524 | struct stat stat; |
| 525 | struct termios termios; |
| 526 | } stdio[1 + STDERR_FILENO]; |
| 527 | #endif // __POSIX__ |
| 528 | |
| 529 | |
Ben Noordhuis | 5756f92 | 2015-01-26 22:15:20 | [diff] [blame] | 530 | inline void PlatformInit() { |
| 531 | #ifdef __POSIX__ |
Daniel Bevenius | 65a6e05 | 2017-04-07 06:48:32 | [diff] [blame] | 532 | #if HAVE_INSPECTOR |
Ben Noordhuis | 63ae1d2 | 2015-01-26 22:26:33 | [diff] [blame] | 533 | sigset_t sigmask; |
| 534 | sigemptyset(&sigmask); |
| 535 | sigaddset(&sigmask, SIGUSR1); |
Ben Noordhuis | b5f25a9 | 2015-02-18 02:43:29 | [diff] [blame] | 536 | const int err = pthread_sigmask(SIG_SETMASK, &sigmask, nullptr); |
Daniel Bevenius | 65a6e05 | 2017-04-07 06:48:32 | [diff] [blame] | 537 | #endif // HAVE_INSPECTOR |
Ben Noordhuis | b5f25a9 | 2015-02-18 02:43:29 | [diff] [blame] | 538 | |
| 539 | // Make sure file descriptors 0-2 are valid before we start logging anything. |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 540 | for (auto& s : stdio) { |
| 541 | const int fd = &s - stdio; |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 542 | #ifdef __Fuchsia__ |
| 543 | // In fuchsia stdin is not readily available. |
| 544 | if (fd == 0) continue; |
| 545 | #endif |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 546 | if (fstat(fd, &s.stat) == 0) |
Ben Noordhuis | b5f25a9 | 2015-02-18 02:43:29 | [diff] [blame] | 547 | continue; |
| 548 | // Anything but EBADF means something is seriously wrong. We don't |
| 549 | // have to special-case EINTR, fstat() is not interruptible. |
| 550 | if (errno != EBADF) |
Evan Lucas | 870229e | 2015-09-16 15:12:41 | [diff] [blame] | 551 | ABORT(); |
Ben Noordhuis | b5f25a9 | 2015-02-18 02:43:29 | [diff] [blame] | 552 | if (fd != open("/dev/null", O_RDWR)) |
Evan Lucas | 870229e | 2015-09-16 15:12:41 | [diff] [blame] | 553 | ABORT(); |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 554 | if (fstat(fd, &s.stat) != 0) |
| 555 | ABORT(); |
Ben Noordhuis | b5f25a9 | 2015-02-18 02:43:29 | [diff] [blame] | 556 | } |
| 557 | |
Daniel Bevenius | 65a6e05 | 2017-04-07 06:48:32 | [diff] [blame] | 558 | #if HAVE_INSPECTOR |
Ben Noordhuis | b5f25a9 | 2015-02-18 02:43:29 | [diff] [blame] | 559 | CHECK_EQ(err, 0); |
Daniel Bevenius | 65a6e05 | 2017-04-07 06:48:32 | [diff] [blame] | 560 | #endif // HAVE_INSPECTOR |
Ben Noordhuis | dd47a8c | 2015-01-26 23:07:34 | [diff] [blame] | 561 | |
Anna Henningsen | 7e2a182 | 2019-11-02 18:06:59 | [diff] [blame] | 562 | // TODO(addaleax): NODE_SHARED_MODE does not really make sense here. |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 563 | #if !defined(NODE_SHARED_MODE) && !defined(__Fuchsia__) |
Ben Noordhuis | dd47a8c | 2015-01-26 23:07:34 | [diff] [blame] | 564 | // Restore signal dispositions, the parent process may have changed them. |
| 565 | struct sigaction act; |
| 566 | memset(&act, 0, sizeof(act)); |
| 567 | |
| 568 | // The hard-coded upper limit is because NSIG is not very reliable; on Linux, |
| 569 | // it evaluates to 32, 34 or 64, depending on whether RT signals are enabled. |
| 570 | // Counting up to SIGRTMIN doesn't work for the same reason. |
Eugene Ostroukhov | 6626919 | 2016-06-08 21:09:28 | [diff] [blame] | 571 | for (unsigned nr = 1; nr < kMaxSignal; nr += 1) { |
Ben Noordhuis | dd47a8c | 2015-01-26 23:07:34 | [diff] [blame] | 572 | if (nr == SIGKILL || nr == SIGSTOP) |
| 573 | continue; |
Ben Noordhuis | 58fe440 | 2019-05-23 09:27:53 | [diff] [blame] | 574 | act.sa_handler = (nr == SIGPIPE || nr == SIGXFSZ) ? SIG_IGN : SIG_DFL; |
Ben Noordhuis | dd47a8c | 2015-01-26 23:07:34 | [diff] [blame] | 575 | CHECK_EQ(0, sigaction(nr, &act, nullptr)); |
| 576 | } |
Stewart X Addison | 0f0f3d3 | 2016-12-30 12:44:46 | [diff] [blame] | 577 | #endif // !NODE_SHARED_MODE |
Ben Noordhuis | dd47a8c | 2015-01-26 23:07:34 | [diff] [blame] | 578 | |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 579 | // Record the state of the stdio file descriptors so we can restore it |
| 580 | // on exit. Needs to happen before installing signal handlers because |
| 581 | // they make use of that information. |
| 582 | for (auto& s : stdio) { |
| 583 | const int fd = &s - stdio; |
| 584 | int err; |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 585 | #ifdef __Fuchsia__ |
| 586 | // In fuchsia stdin is not readily available. |
| 587 | if (fd == 0) continue; |
| 588 | #endif |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 589 | |
| 590 | do |
| 591 | s.flags = fcntl(fd, F_GETFL); |
| 592 | while (s.flags == -1 && errno == EINTR); // NOLINT |
| 593 | CHECK_NE(s.flags, -1); |
| 594 | |
cjihrig | 38c0784 | 2020-01-12 23:52:41 | [diff] [blame] | 595 | if (uv_guess_handle(fd) != UV_TTY) continue; |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 596 | s.isatty = true; |
| 597 | |
| 598 | do |
| 599 | err = tcgetattr(fd, &s.termios); |
| 600 | while (err == -1 && errno == EINTR); // NOLINT |
| 601 | CHECK_EQ(err, 0); |
| 602 | } |
| 603 | |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 604 | #ifndef __Fuchsia__ |
Ben Noordhuis | 63ae1d2 | 2015-01-26 22:26:33 | [diff] [blame] | 605 | RegisterSignalHandler(SIGINT, SignalExit, true); |
| 606 | RegisterSignalHandler(SIGTERM, SignalExit, true); |
Ben Noordhuis | dd47a8c | 2015-01-26 23:07:34 | [diff] [blame] | 607 | |
Gus Caplan | f206193 | 2019-04-15 21:09:51 | [diff] [blame] | 608 | #if NODE_USE_V8_WASM_TRAP_HANDLER |
| 609 | // Tell V8 to disable emitting WebAssembly |
| 610 | // memory bounds checks. This means that we have |
| 611 | // to catch the SIGSEGV in TrapWebAssemblyOrContinue |
| 612 | // and pass the signal context to V8. |
| 613 | { |
| 614 | struct sigaction sa; |
| 615 | memset(&sa, 0, sizeof(sa)); |
| 616 | sa.sa_sigaction = TrapWebAssemblyOrContinue; |
| 617 | CHECK_EQ(sigaction(SIGSEGV, &sa, nullptr), 0); |
| 618 | } |
| 619 | V8::EnableWebAssemblyTrapHandler(false); |
| 620 | #endif // NODE_USE_V8_WASM_TRAP_HANDLER |
| 621 | |
Ben Noordhuis | 5756f92 | 2015-01-26 22:15:20 | [diff] [blame] | 622 | // Raise the open file descriptor limit. |
| 623 | struct rlimit lim; |
| 624 | if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) { |
| 625 | // Do a binary search for the limit. |
| 626 | rlim_t min = lim.rlim_cur; |
| 627 | rlim_t max = 1 << 20; |
| 628 | // But if there's a defined upper bound, don't search, just set it. |
| 629 | if (lim.rlim_max != RLIM_INFINITY) { |
| 630 | min = lim.rlim_max; |
| 631 | max = lim.rlim_max; |
| 632 | } |
| 633 | do { |
| 634 | lim.rlim_cur = min + (max - min) / 2; |
| 635 | if (setrlimit(RLIMIT_NOFILE, &lim)) { |
| 636 | max = lim.rlim_cur; |
| 637 | } else { |
| 638 | min = lim.rlim_cur; |
| 639 | } |
| 640 | } while (min + 1 < max); |
| 641 | } |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 642 | #endif // __Fuchsia__ |
Ben Noordhuis | 5756f92 | 2015-01-26 22:15:20 | [diff] [blame] | 643 | #endif // __POSIX__ |
Bartosz Sosnowski | bd496e0 | 2017-03-14 17:22:53 | [diff] [blame] | 644 | #ifdef _WIN32 |
| 645 | for (int fd = 0; fd <= 2; ++fd) { |
| 646 | auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd)); |
| 647 | if (handle == INVALID_HANDLE_VALUE || |
| 648 | GetFileType(handle) == FILE_TYPE_UNKNOWN) { |
| 649 | // Ignore _close result. If it fails or not depends on used Windows |
| 650 | // version. We will just check _open result. |
| 651 | _close(fd); |
| 652 | if (fd != _open("nul", _O_RDWR)) |
| 653 | ABORT(); |
| 654 | } |
| 655 | } |
| 656 | #endif // _WIN32 |
Ben Noordhuis | 5756f92 | 2015-01-26 22:15:20 | [diff] [blame] | 657 | } |
| 658 | |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 659 | |
| 660 | // Safe to call more than once and from signal handlers. |
| 661 | void ResetStdio() { |
| 662 | uv_tty_reset_mode(); |
| 663 | #ifdef __POSIX__ |
| 664 | for (auto& s : stdio) { |
| 665 | const int fd = &s - stdio; |
| 666 | |
Victor Gomes | 64159cb | 2020-04-24 15:28:20 | [diff] [blame] | 667 | #ifdef __Fuchsia__ |
| 668 | // In fuchsia stdin is not readily available. |
| 669 | if (fd == 0) continue; |
| 670 | #endif |
| 671 | |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 672 | struct stat tmp; |
| 673 | if (-1 == fstat(fd, &tmp)) { |
| 674 | CHECK_EQ(errno, EBADF); // Program closed file descriptor. |
| 675 | continue; |
| 676 | } |
| 677 | |
| 678 | bool is_same_file = |
| 679 | (s.stat.st_dev == tmp.st_dev && s.stat.st_ino == tmp.st_ino); |
| 680 | if (!is_same_file) continue; // Program reopened file descriptor. |
| 681 | |
| 682 | int flags; |
| 683 | do |
| 684 | flags = fcntl(fd, F_GETFL); |
| 685 | while (flags == -1 && errno == EINTR); // NOLINT |
| 686 | CHECK_NE(flags, -1); |
| 687 | |
| 688 | // Restore the O_NONBLOCK flag if it changed. |
| 689 | if (O_NONBLOCK & (flags ^ s.flags)) { |
| 690 | flags &= ~O_NONBLOCK; |
| 691 | flags |= s.flags & O_NONBLOCK; |
| 692 | |
| 693 | int err; |
| 694 | do |
| 695 | err = fcntl(fd, F_SETFL, flags); |
| 696 | while (err == -1 && errno == EINTR); // NOLINT |
| 697 | CHECK_NE(err, -1); |
| 698 | } |
| 699 | |
| 700 | if (s.isatty) { |
Ben Noordhuis | b06ce0b | 2019-07-04 10:11:00 | [diff] [blame] | 701 | sigset_t sa; |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 702 | int err; |
Ben Noordhuis | b06ce0b | 2019-07-04 10:11:00 | [diff] [blame] | 703 | |
| 704 | // We might be a background job that doesn't own the TTY so block SIGTTOU |
| 705 | // before making the tcsetattr() call, otherwise that signal suspends us. |
| 706 | sigemptyset(&sa); |
| 707 | sigaddset(&sa, SIGTTOU); |
| 708 | |
| 709 | CHECK_EQ(0, pthread_sigmask(SIG_BLOCK, &sa, nullptr)); |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 710 | do |
| 711 | err = tcsetattr(fd, TCSANOW, &s.termios); |
| 712 | while (err == -1 && errno == EINTR); // NOLINT |
Ben Noordhuis | b06ce0b | 2019-07-04 10:11:00 | [diff] [blame] | 713 | CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &sa, nullptr)); |
| 714 | CHECK_EQ(0, err); |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | #endif // __POSIX__ |
| 718 | } |
| 719 | |
| 720 | |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 721 | int ProcessGlobalArgs(std::vector<std::string>* args, |
| 722 | std::vector<std::string>* exec_args, |
| 723 | std::vector<std::string>* errors, |
Shelley Vohr | 0f58bfd | 2019-11-13 15:53:19 | [diff] [blame] | 724 | OptionEnvvarSettings settings) { |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 725 | // Parse a few arguments which are specific to Node. |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 726 | std::vector<std::string> v8_args; |
Anna Henningsen | e812be4 | 2018-08-23 14:41:05 | [diff] [blame] | 727 | |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 728 | Mutex::ScopedLock lock(per_process::cli_options_mutex); |
Refael Ackermann | af8b92c | 2019-02-23 22:21:56 | [diff] [blame] | 729 | options_parser::Parse( |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 730 | args, |
| 731 | exec_args, |
| 732 | &v8_args, |
| 733 | per_process::cli_options.get(), |
Shelley Vohr | 0f58bfd | 2019-11-13 15:53:19 | [diff] [blame] | 734 | settings, |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 735 | errors); |
Anna Henningsen | e812be4 | 2018-08-23 14:41:05 | [diff] [blame] | 736 | |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 737 | if (!errors->empty()) return 9; |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 738 | |
Anna Henningsen | 38ab1e9 | 2019-01-12 19:55:58 | [diff] [blame] | 739 | std::string revert_error; |
| 740 | for (const std::string& cve : per_process::cli_options->security_reverts) { |
| 741 | Revert(cve.c_str(), &revert_error); |
| 742 | if (!revert_error.empty()) { |
| 743 | errors->emplace_back(std::move(revert_error)); |
| 744 | return 12; |
| 745 | } |
| 746 | } |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 747 | |
Gus Caplan | 7a742ec | 2020-03-15 00:54:15 | [diff] [blame] | 748 | if (per_process::cli_options->disable_proto != "delete" && |
| 749 | per_process::cli_options->disable_proto != "throw" && |
| 750 | per_process::cli_options->disable_proto != "") { |
| 751 | errors->emplace_back("invalid mode passed to --disable-proto"); |
| 752 | return 12; |
| 753 | } |
| 754 | |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 755 | auto env_opts = per_process::cli_options->per_isolate->per_env; |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 756 | if (std::find(v8_args.begin(), v8_args.end(), |
| 757 | "--abort-on-uncaught-exception") != v8_args.end() || |
| 758 | std::find(v8_args.begin(), v8_args.end(), |
| 759 | "--abort_on_uncaught_exception") != v8_args.end()) { |
Daniel Bevenius | 6a9e776 | 2018-09-06 08:39:38 | [diff] [blame] | 760 | env_opts->abort_on_uncaught_exception = true; |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 761 | } |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 762 | |
| 763 | // TODO(bnoordhuis) Intercept --prof arguments and start the CPU profiler |
| 764 | // manually? That would give us a little more control over its runtime |
| 765 | // behavior but it could also interfere with the user's intentions in ways |
| 766 | // we fail to anticipate. Dillema. |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 767 | if (std::find(v8_args.begin(), v8_args.end(), "--prof") != v8_args.end()) { |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 768 | per_process::v8_is_profiling = true; |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | #ifdef __POSIX__ |
| 772 | // Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the |
| 773 | // performance penalty of frequent EINTR wakeups when the profiler is running. |
| 774 | // Only do this for v8.log profiling, as it breaks v8::CpuProfiler users. |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 775 | if (per_process::v8_is_profiling) { |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 776 | uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF); |
| 777 | } |
| 778 | #endif |
| 779 | |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 780 | std::vector<char*> v8_args_as_char_ptr(v8_args.size()); |
| 781 | if (v8_args.size() > 0) { |
| 782 | for (size_t i = 0; i < v8_args.size(); ++i) |
| 783 | v8_args_as_char_ptr[i] = &v8_args[i][0]; |
| 784 | int argc = v8_args.size(); |
| 785 | V8::SetFlagsFromCommandLine(&argc, &v8_args_as_char_ptr[0], true); |
| 786 | v8_args_as_char_ptr.resize(argc); |
| 787 | } |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 788 | |
| 789 | // Anything that's still in v8_argv is not a V8 or a node option. |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 790 | for (size_t i = 1; i < v8_args_as_char_ptr.size(); i++) |
| 791 | errors->push_back("bad option: " + std::string(v8_args_as_char_ptr[i])); |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 792 | |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 793 | if (v8_args_as_char_ptr.size() > 1) return 9; |
| 794 | |
| 795 | return 0; |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 796 | } |
| 797 | |
Anna Henningsen | 97ce5e0 | 2019-03-05 19:16:33 | [diff] [blame] | 798 | static std::atomic_bool init_called{false}; |
| 799 | |
Joyee Cheung | 963ee0b | 2019-03-07 13:52:31 | [diff] [blame] | 800 | int InitializeNodeWithArgs(std::vector<std::string>* argv, |
| 801 | std::vector<std::string>* exec_argv, |
| 802 | std::vector<std::string>* errors) { |
| 803 | // Make sure InitializeNodeWithArgs() is called only once. |
Anna Henningsen | 97ce5e0 | 2019-03-05 19:16:33 | [diff] [blame] | 804 | CHECK(!init_called.exchange(true)); |
| 805 | |
Micha Hanselmann | 35cb955 | 2019-07-24 22:34:33 | [diff] [blame] | 806 | // Initialize node_start_time to get relative uptime. |
| 807 | per_process::node_start_time = uv_hrtime(); |
| 808 | |
Yihong Wang | 8680bb9 | 2017-10-22 06:16:50 | [diff] [blame] | 809 | // Register built-in modules |
Joyee Cheung | 3d66826 | 2018-11-28 16:46:45 | [diff] [blame] | 810 | binding::RegisterBuiltinModules(); |
Yihong Wang | 8680bb9 | 2017-10-22 06:16:50 | [diff] [blame] | 811 | |
Bert Belder | 09be360 | 2012-06-13 23:28:51 | [diff] [blame] | 812 | // Make inherited handles noninheritable. |
| 813 | uv_disable_stdio_inheritance(); |
| 814 | |
Gireesh Punathil | 4f67973 | 2018-09-05 14:06:59 | [diff] [blame] | 815 | // Cache the original command line to be |
| 816 | // used in diagnostic reports. |
| 817 | per_process::cli_options->cmdline = *argv; |
Gireesh Punathil | 4f67973 | 2018-09-05 14:06:59 | [diff] [blame] | 818 | |
Ben Noordhuis | 490d5ab | 2014-03-31 12:22:49 | [diff] [blame] | 819 | #if defined(NODE_V8_OPTIONS) |
| 820 | // Should come before the call to V8::SetFlagsFromCommandLine() |
| 821 | // so the user can disable a flag --foo at run-time by passing |
| 822 | // --no_foo from the command line. |
| 823 | V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1); |
| 824 | #endif |
Fedor Indutny | b55c9d6 | 2014-03-26 20:30:49 | [diff] [blame] | 825 | |
Denys Otrishko | 84b8857 | 2020-02-09 22:17:33 | [diff] [blame] | 826 | HandleEnvOptions(per_process::cli_options->per_isolate->per_env); |
James M Snell | 03e89b3 | 2016-12-04 18:38:35 | [diff] [blame] | 827 | |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 828 | #if !defined(NODE_WITHOUT_NODE_OPTIONS) |
| 829 | std::string node_options; |
Maƫl Nison | 3ef1512 | 2018-11-03 17:52:44 | [diff] [blame] | 830 | |
Joyee Cheung | 321e296 | 2018-12-15 19:13:12 | [diff] [blame] | 831 | if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) { |
Denys Otrishko | 84b8857 | 2020-02-09 22:17:33 | [diff] [blame] | 832 | std::vector<std::string> env_argv = |
| 833 | ParseNodeOptionsEnvVar(node_options, errors); |
| 834 | |
| 835 | if (!errors->empty()) return 9; |
| 836 | |
Maƫl Nison | 3ef1512 | 2018-11-03 17:52:44 | [diff] [blame] | 837 | // [0] is expected to be the program name, fill it in from the real argv. |
Denys Otrishko | 84b8857 | 2020-02-09 22:17:33 | [diff] [blame] | 838 | env_argv.insert(env_argv.begin(), argv->at(0)); |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 839 | |
Shelley Vohr | 0f58bfd | 2019-11-13 15:53:19 | [diff] [blame] | 840 | const int exit_code = ProcessGlobalArgs(&env_argv, |
| 841 | nullptr, |
| 842 | errors, |
| 843 | kAllowedInEnvironment); |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 844 | if (exit_code != 0) return exit_code; |
Oleksandr Chekhovskyi | 8e14135 | 2015-08-07 13:03:04 | [diff] [blame] | 845 | } |
| 846 | #endif |
| 847 | |
Shelley Vohr | 0f58bfd | 2019-11-13 15:53:19 | [diff] [blame] | 848 | const int exit_code = ProcessGlobalArgs(argv, |
| 849 | exec_argv, |
| 850 | errors, |
| 851 | kDisallowedInEnvironment); |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 852 | if (exit_code != 0) return exit_code; |
Sam Roberts | f2282bb | 2017-02-20 14:18:43 | [diff] [blame] | 853 | |
James M Snell | 9d71619 | 2018-06-22 23:16:03 | [diff] [blame] | 854 | // Set the process.title immediately after processing argv if --title is set. |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 855 | if (!per_process::cli_options->title.empty()) |
| 856 | uv_set_process_title(per_process::cli_options->title.c_str()); |
James M Snell | 9d71619 | 2018-06-22 23:16:03 | [diff] [blame] | 857 | |
Steven R. Loomis | ac2857b | 2014-09-05 05:03:24 | [diff] [blame] | 858 | #if defined(NODE_HAVE_I18N_SUPPORT) |
Ben Noordhuis | a8734af | 2017-01-28 12:27:02 | [diff] [blame] | 859 | // If the parameter isn't given, use the env variable. |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 860 | if (per_process::cli_options->icu_data_dir.empty()) |
| 861 | credentials::SafeGetenv("NODE_ICU_DATA", |
| 862 | &per_process::cli_options->icu_data_dir); |
Stephen Gallagher | d502b83 | 2019-12-06 21:40:25 | [diff] [blame] | 863 | |
| 864 | #ifdef NODE_ICU_DEFAULT_DATA_DIR |
| 865 | // If neither the CLI option nor the environment variable was specified, |
| 866 | // fall back to the configured default |
| 867 | if (per_process::cli_options->icu_data_dir.empty()) { |
| 868 | // Check whether the NODE_ICU_DEFAULT_DATA_DIR contains the right data |
| 869 | // file and can be read. |
| 870 | static const char full_path[] = |
| 871 | NODE_ICU_DEFAULT_DATA_DIR "/" U_ICUDATA_NAME ".dat"; |
| 872 | |
| 873 | FILE* f = fopen(full_path, "rb"); |
| 874 | |
| 875 | if (f != nullptr) { |
| 876 | fclose(f); |
| 877 | per_process::cli_options->icu_data_dir = NODE_ICU_DEFAULT_DATA_DIR; |
| 878 | } |
| 879 | } |
| 880 | #endif // NODE_ICU_DEFAULT_DATA_DIR |
| 881 | |
Steven R. Loomis | ac2857b | 2014-09-05 05:03:24 | [diff] [blame] | 882 | // Initialize ICU. |
Ben Noordhuis | a8734af | 2017-01-28 12:27:02 | [diff] [blame] | 883 | // If icu_data_dir is empty here, it will load the 'minimal' data. |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 884 | if (!i18n::InitializeICUDirectory(per_process::cli_options->icu_data_dir)) { |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 885 | errors->push_back("could not initialize ICU " |
| 886 | "(check NODE_ICU_DATA or --icu-data-dir parameters)\n"); |
| 887 | return 9; |
Steven R. Loomis | ac2857b | 2014-09-05 05:03:24 | [diff] [blame] | 888 | } |
Joyee Cheung | 263d137 | 2018-12-18 21:23:47 | [diff] [blame] | 889 | per_process::metadata.versions.InitializeIntlVersions(); |
Steven R. Loomis | ac2857b | 2014-09-05 05:03:24 | [diff] [blame] | 890 | #endif |
Tom Hughes | 78da9cb | 2010-10-18 22:50:56 | [diff] [blame] | 891 | |
Joyee Cheung | dfd7e99 | 2019-04-09 21:08:48 | [diff] [blame] | 892 | NativeModuleEnv::InitializeCodeCache(); |
| 893 | |
Cheng Zhao | 22e1aea | 2015-01-12 21:31:25 | [diff] [blame] | 894 | // We should set node_is_initialized here instead of in node::Start, |
| 895 | // otherwise embedders using node::Init to initialize everything will not be |
| 896 | // able to set it and native modules will not load for them. |
| 897 | node_is_initialized = true; |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 898 | return 0; |
Ben Noordhuis | 5866f1a | 2011-12-09 18:02:33 | [diff] [blame] | 899 | } |
Ben Noordhuis | 356992f | 2011-11-22 16:10:09 | [diff] [blame] | 900 | |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 901 | // TODO(addaleax): Deprecate and eventually remove this. |
| 902 | void Init(int* argc, |
| 903 | const char** argv, |
| 904 | int* exec_argc, |
| 905 | const char*** exec_argv) { |
| 906 | std::vector<std::string> argv_(argv, argv + *argc); // NOLINT |
| 907 | std::vector<std::string> exec_argv_; |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 908 | std::vector<std::string> errors; |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 909 | |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 910 | // This (approximately) duplicates some logic that has been moved to |
| 911 | // node::Start(), with the difference that here we explicitly call `exit()`. |
Joyee Cheung | 963ee0b | 2019-03-07 13:52:31 | [diff] [blame] | 912 | int exit_code = InitializeNodeWithArgs(&argv_, &exec_argv_, &errors); |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 913 | |
| 914 | for (const std::string& error : errors) |
| 915 | fprintf(stderr, "%s: %s\n", argv_.at(0).c_str(), error.c_str()); |
| 916 | if (exit_code != 0) exit(exit_code); |
| 917 | |
| 918 | if (per_process::cli_options->print_version) { |
| 919 | printf("%s\n", NODE_VERSION); |
| 920 | exit(0); |
| 921 | } |
| 922 | |
Joyee Cheung | 403c84a | 2019-02-03 03:47:26 | [diff] [blame] | 923 | if (per_process::cli_options->print_bash_completion) { |
| 924 | std::string completion = options_parser::GetBashCompletion(); |
| 925 | printf("%s\n", completion.c_str()); |
| 926 | exit(0); |
| 927 | } |
| 928 | |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 929 | if (per_process::cli_options->print_v8_help) { |
Michaƫl Zasso | 94e980c | 2019-07-20 11:35:59 | [diff] [blame] | 930 | // Doesn't return. |
| 931 | V8::SetFlagsFromString("--help", static_cast<size_t>(6)); |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 932 | UNREACHABLE(); |
| 933 | } |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 934 | |
| 935 | *argc = argv_.size(); |
| 936 | *exec_argc = exec_argv_.size(); |
| 937 | // These leak memory, because, in the original code of this function, no |
| 938 | // extra allocations were visible. This should be okay because this function |
| 939 | // is only supposed to be called once per process, though. |
| 940 | *exec_argv = Malloc<const char*>(*exec_argc); |
| 941 | for (int i = 0; i < *exec_argc; ++i) |
| 942 | (*exec_argv)[i] = strdup(exec_argv_[i].c_str()); |
| 943 | for (int i = 0; i < *argc; ++i) |
| 944 | argv[i] = strdup(argv_[i].c_str()); |
| 945 | } |
Ben Noordhuis | 5866f1a | 2011-12-09 18:02:33 | [diff] [blame] | 946 | |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 947 | InitializationResult InitializeOncePerProcess(int argc, char** argv) { |
Joyee Cheung | 51bc55f | 2020-02-20 09:22:48 | [diff] [blame] | 948 | // Initialized the enabled list for Debug() calls with system |
| 949 | // environment variables. |
| 950 | per_process::enabled_debug_list.Parse(nullptr); |
| 951 | |
Ben Noordhuis | 5872705 | 2019-05-24 14:11:19 | [diff] [blame] | 952 | atexit(ResetStdio); |
Ben Noordhuis | 5756f92 | 2015-01-26 22:15:20 | [diff] [blame] | 953 | PlatformInit(); |
| 954 | |
Ben Noordhuis | 5fdff38 | 2014-10-11 14:52:07 | [diff] [blame] | 955 | CHECK_GT(argc, 0); |
Micheil Smith | 19fd530 | 2012-03-05 17:53:15 | [diff] [blame] | 956 | |
Ben Noordhuis | 185c515 | 2013-09-02 14:42:01 | [diff] [blame] | 957 | // Hack around with the argv pointer. Used for process.title = "blah". |
Ben Noordhuis | 1a97998 | 2012-03-15 22:10:32 | [diff] [blame] | 958 | argv = uv_setup_args(argc, argv); |
| 959 | |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 960 | InitializationResult result; |
| 961 | result.args = std::vector<std::string>(argv, argv + argc); |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 962 | std::vector<std::string> errors; |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 963 | |
Anna Henningsen | 29a71ba | 2018-08-10 00:45:28 | [diff] [blame] | 964 | // This needs to run *before* V8::Initialize(). |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 965 | { |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 966 | result.exit_code = |
| 967 | InitializeNodeWithArgs(&(result.args), &(result.exec_args), &errors); |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 968 | for (const std::string& error : errors) |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 969 | fprintf(stderr, "%s: %s\n", result.args.at(0).c_str(), error.c_str()); |
| 970 | if (result.exit_code != 0) { |
| 971 | result.early_return = true; |
| 972 | return result; |
| 973 | } |
Anna Henningsen | eb664c3 | 2019-01-06 21:55:09 | [diff] [blame] | 974 | } |
Ben Noordhuis | 5866f1a | 2011-12-09 18:02:33 | [diff] [blame] | 975 | |
Gabriel Schulhof | 8952105 | 2019-12-13 19:59:37 | [diff] [blame] | 976 | if (per_process::cli_options->use_largepages == "on" || |
| 977 | per_process::cli_options->use_largepages == "silent") { |
Gabriel Schulhof | 0dff851 | 2020-02-21 16:26:29 | [diff] [blame] | 978 | int result = node::MapStaticCodeToLargePages(); |
| 979 | if (per_process::cli_options->use_largepages == "on" && result != 0) { |
| 980 | fprintf(stderr, "%s\n", node::LargePagesError(result)); |
Gabriel Schulhof | 8952105 | 2019-12-13 19:59:37 | [diff] [blame] | 981 | } |
| 982 | } |
Gabriel Schulhof | 8952105 | 2019-12-13 19:59:37 | [diff] [blame] | 983 | |
Joyee Cheung | 963ee0b | 2019-03-07 13:52:31 | [diff] [blame] | 984 | if (per_process::cli_options->print_version) { |
| 985 | printf("%s\n", NODE_VERSION); |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 986 | result.exit_code = 0; |
| 987 | result.early_return = true; |
| 988 | return result; |
Joyee Cheung | 963ee0b | 2019-03-07 13:52:31 | [diff] [blame] | 989 | } |
| 990 | |
Joyee Cheung | 403c84a | 2019-02-03 03:47:26 | [diff] [blame] | 991 | if (per_process::cli_options->print_bash_completion) { |
| 992 | std::string completion = options_parser::GetBashCompletion(); |
| 993 | printf("%s\n", completion.c_str()); |
| 994 | exit(0); |
| 995 | } |
| 996 | |
Joyee Cheung | 963ee0b | 2019-03-07 13:52:31 | [diff] [blame] | 997 | if (per_process::cli_options->print_v8_help) { |
Michaƫl Zasso | 94e980c | 2019-07-20 11:35:59 | [diff] [blame] | 998 | // Doesn't return. |
| 999 | V8::SetFlagsFromString("--help", static_cast<size_t>(6)); |
Joyee Cheung | 963ee0b | 2019-03-07 13:52:31 | [diff] [blame] | 1000 | UNREACHABLE(); |
| 1001 | } |
| 1002 | |
Ben Noordhuis | 7ac2391 | 2013-09-20 20:01:49 | [diff] [blame] | 1003 | #if HAVE_OPENSSL |
Ben Noordhuis | a8734af | 2017-01-28 12:27:02 | [diff] [blame] | 1004 | { |
| 1005 | std::string extra_ca_certs; |
Joyee Cheung | 321e296 | 2018-12-15 19:13:12 | [diff] [blame] | 1006 | if (credentials::SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs)) |
Ben Noordhuis | a8734af | 2017-01-28 12:27:02 | [diff] [blame] | 1007 | crypto::UseExtraCaCerts(extra_ca_certs); |
| 1008 | } |
Stefan Budeanu | 7c48cb5 | 2016-01-22 23:10:09 | [diff] [blame] | 1009 | #ifdef NODE_FIPS_MODE |
| 1010 | // In the case of FIPS builds we should make sure |
| 1011 | // the random source is properly initialized first. |
| 1012 | OPENSSL_init(); |
| 1013 | #endif // NODE_FIPS_MODE |
Ben Noordhuis | 7ac2391 | 2013-09-20 20:01:49 | [diff] [blame] | 1014 | // V8 on Windows doesn't have a good source of entropy. Seed it from |
| 1015 | // OpenSSL's pool. |
| 1016 | V8::SetEntropySource(crypto::EntropySource); |
Ben Noordhuis | a8734af | 2017-01-28 12:27:02 | [diff] [blame] | 1017 | #endif // HAVE_OPENSSL |
Ben Noordhuis | 7ac2391 | 2013-09-20 20:01:49 | [diff] [blame] | 1018 | |
Ben Noordhuis | 93b0e39 | 2020-01-07 13:18:56 | [diff] [blame] | 1019 | per_process::v8_platform.Initialize( |
| 1020 | per_process::cli_options->v8_thread_pool_size); |
Ben Noordhuis | 75ea566 | 2013-09-23 12:27:26 | [diff] [blame] | 1021 | V8::Initialize(); |
Daniel Bevenius | cae4182 | 2018-02-23 14:42:20 | [diff] [blame] | 1022 | performance::performance_v8_start = PERFORMANCE_NOW(); |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 1023 | per_process::v8_initialized = true; |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 1024 | return result; |
| 1025 | } |
Joyee Cheung | cab1dc5 | 2019-04-14 06:41:04 | [diff] [blame] | 1026 | |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 1027 | void TearDownOncePerProcess() { |
Joyee Cheung | 9db9e7e | 2019-01-01 05:56:53 | [diff] [blame] | 1028 | per_process::v8_initialized = false; |
Ryan | 27b268b | 2009-06-17 13:05:44 | [diff] [blame] | 1029 | V8::Dispose(); |
Igor Zinkovsky | a58b643 | 2011-07-07 20:54:30 | [diff] [blame] | 1030 | |
Matt Loring | 9e08695 | 2017-03-13 22:17:57 | [diff] [blame] | 1031 | // uv_run cannot be called from the time before the beforeExit callback |
| 1032 | // runs until the program exits unless the event loop has any referenced |
| 1033 | // handles after beforeExit terminates. This prevents unrefed timers |
| 1034 | // that happen to terminate during shutdown from being run unsafely. |
| 1035 | // Since uv_run cannot be called, uv_async handles held by the platform |
| 1036 | // will never be fully cleaned up. |
Joyee Cheung | 99c3243 | 2019-01-16 18:34:43 | [diff] [blame] | 1037 | per_process::v8_platform.Dispose(); |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 1038 | } |
Ben Noordhuis | 4a801c2 | 2015-04-02 21:51:01 | [diff] [blame] | 1039 | |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 1040 | int Start(int argc, char** argv) { |
| 1041 | InitializationResult result = InitializeOncePerProcess(argc, argv); |
| 1042 | if (result.early_return) { |
| 1043 | return result.exit_code; |
| 1044 | } |
| 1045 | |
| 1046 | { |
Joyee Cheung | 70d887e | 2019-04-19 04:50:38 | [diff] [blame] | 1047 | Isolate::CreateParams params; |
Joyee Cheung | 581bddc | 2019-07-05 18:43:27 | [diff] [blame] | 1048 | const std::vector<size_t>* indexes = nullptr; |
| 1049 | std::vector<intptr_t> external_references; |
| 1050 | |
| 1051 | bool force_no_snapshot = |
| 1052 | per_process::cli_options->per_isolate->no_node_snapshot; |
| 1053 | if (!force_no_snapshot) { |
| 1054 | v8::StartupData* blob = NodeMainInstance::GetEmbeddedSnapshotBlob(); |
| 1055 | if (blob != nullptr) { |
| 1056 | // TODO(joyeecheung): collect external references and set it in |
| 1057 | // params.external_references. |
| 1058 | external_references.push_back(reinterpret_cast<intptr_t>(nullptr)); |
| 1059 | params.external_references = external_references.data(); |
| 1060 | params.snapshot_blob = blob; |
| 1061 | indexes = NodeMainInstance::GetIsolateDataIndexes(); |
| 1062 | } |
Joyee Cheung | bf5dade | 2019-04-18 14:09:28 | [diff] [blame] | 1063 | } |
| 1064 | |
Joyee Cheung | 70d887e | 2019-04-19 04:50:38 | [diff] [blame] | 1065 | NodeMainInstance main_instance(¶ms, |
| 1066 | uv_default_loop(), |
| 1067 | per_process::v8_platform.Platform(), |
| 1068 | result.args, |
Joyee Cheung | bf5dade | 2019-04-18 14:09:28 | [diff] [blame] | 1069 | result.exec_args, |
| 1070 | indexes); |
Joyee Cheung | 5356b4a | 2019-04-14 09:46:52 | [diff] [blame] | 1071 | result.exit_code = main_instance.Run(); |
| 1072 | } |
| 1073 | |
| 1074 | TearDownOncePerProcess(); |
| 1075 | return result.exit_code; |
Ryan | 19478ed | 2009-03-03 00:56:15 | [diff] [blame] | 1076 | } |
Ryan Dahl | 124fbed | 2010-09-19 20:13:57 | [diff] [blame] | 1077 | |
Gireesh Punathil | d35af56 | 2018-06-12 13:01:46 | [diff] [blame] | 1078 | int Stop(Environment* env) { |
Anna Henningsen | f621536 | 2020-04-02 21:40:25 | [diff] [blame] | 1079 | env->ExitEnv(); |
Gireesh Punathil | d35af56 | 2018-06-12 13:01:46 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | |
Ryan Dahl | 124fbed | 2010-09-19 20:13:57 | [diff] [blame] | 1083 | } // namespace node |
Eugene Ostroukhov | 3f48ab3 | 2017-04-25 21:55:55 | [diff] [blame] | 1084 | |
| 1085 | #if !HAVE_INSPECTOR |
Daniel Bevenius | ebbf393 | 2018-03-23 08:38:16 | [diff] [blame] | 1086 | void Initialize() {} |
Eugene Ostroukhov | 3f48ab3 | 2017-04-25 21:55:55 | [diff] [blame] | 1087 | |
Beni von Cheni | 0500237 | 2018-12-10 03:12:39 | [diff] [blame] | 1088 | NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector, Initialize) |
Eugene Ostroukhov | 3f48ab3 | 2017-04-25 21:55:55 | [diff] [blame] | 1089 | #endif // !HAVE_INSPECTOR |