mirror of
https://github.com/nodejs/node.git
synced 2026-12-05 11:48:49 -05:00
The warnings in question are:
../src/node.cc:844:13: warning:
unused function 'DebugProcess' [-Wunused-function]
static void DebugProcess(const FunctionCallbackInfo<Value>& args);
^
../src/node.cc:845:13: warning:
unused function 'DebugEnd' [-Wunused-function]
static void DebugEnd(const FunctionCallbackInfo<Value>& args);
1953 lines
58 KiB
C++
1953 lines
58 KiB
C++
// Copyright Joyent, Inc. and other Node contributors.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
// copy of this software and associated documentation files (the
|
|
// "Software"), to deal in the Software without restriction, including
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
// following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included
|
|
// in all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
#include "node_binding.h"
|
|
#include "node_buffer.h"
|
|
#include "node_constants.h"
|
|
#include "node_context_data.h"
|
|
#include "node_errors.h"
|
|
#include "node_internals.h"
|
|
#include "node_metadata.h"
|
|
#include "node_native_module.h"
|
|
#include "node_options-inl.h"
|
|
#include "node_perf.h"
|
|
#include "node_platform.h"
|
|
#include "node_revert.h"
|
|
#include "node_version.h"
|
|
#include "tracing/traced_value.h"
|
|
|
|
#if HAVE_OPENSSL
|
|
#include "node_crypto.h"
|
|
#endif
|
|
|
|
#if defined(NODE_HAVE_I18N_SUPPORT)
|
|
#include "node_i18n.h"
|
|
#endif
|
|
|
|
#if HAVE_INSPECTOR
|
|
#include "inspector_io.h"
|
|
#endif
|
|
|
|
#if defined HAVE_DTRACE || defined HAVE_ETW
|
|
#include "node_dtrace.h"
|
|
#endif
|
|
|
|
#include "async_wrap-inl.h"
|
|
#include "env-inl.h"
|
|
#include "handle_wrap.h"
|
|
#include "req_wrap-inl.h"
|
|
#include "string_bytes.h"
|
|
#include "tracing/agent.h"
|
|
#include "tracing/node_trace_writer.h"
|
|
#include "util.h"
|
|
#include "uv.h"
|
|
#if NODE_USE_V8_PLATFORM
|
|
#include "libplatform/libplatform.h"
|
|
#endif // NODE_USE_V8_PLATFORM
|
|
#include "v8-profiler.h"
|
|
|
|
#ifdef NODE_ENABLE_VTUNE_PROFILING
|
|
#include "../deps/v8/src/third_party/vtune/v8-vtune.h"
|
|
#endif
|
|
|
|
#ifdef NODE_ENABLE_LARGE_CODE_PAGES
|
|
#include "large_pages/node_large_page.h"
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h> // _O_RDWR
|
|
#include <limits.h> // PATH_MAX
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#if defined(NODE_HAVE_I18N_SUPPORT)
|
|
#include <unicode/uvernum.h>
|
|
#endif
|
|
|
|
#if defined(LEAK_SANITIZER)
|
|
#include <sanitizer/lsan_interface.h>
|
|
#endif
|
|
|
|
#if defined(_MSC_VER)
|
|
#include <direct.h>
|
|
#include <io.h>
|
|
#else
|
|
#include <pthread.h>
|
|
#include <sys/resource.h> // getrlimit, setrlimit
|
|
#include <unistd.h> // STDIN_FILENO, STDERR_FILENO
|
|
#endif
|
|
|
|
namespace node {
|
|
|
|
using options_parser::kAllowedInEnvironment;
|
|
using options_parser::kDisallowedInEnvironment;
|
|
using v8::Array;
|
|
using v8::Boolean;
|
|
using v8::Context;
|
|
using v8::DEFAULT;
|
|
using v8::EscapableHandleScope;
|
|
using v8::Exception;
|
|
using v8::Function;
|
|
using v8::FunctionCallbackInfo;
|
|
using v8::HandleScope;
|
|
using v8::Int32;
|
|
using v8::Integer;
|
|
using v8::Isolate;
|
|
using v8::Just;
|
|
using v8::Local;
|
|
using v8::Locker;
|
|
using v8::Maybe;
|
|
using v8::MaybeLocal;
|
|
using v8::Message;
|
|
using v8::MicrotasksPolicy;
|
|
using v8::NewStringType;
|
|
using v8::None;
|
|
using v8::Nothing;
|
|
using v8::Object;
|
|
using v8::ObjectTemplate;
|
|
using v8::Script;
|
|
using v8::ScriptOrigin;
|
|
using v8::SealHandleScope;
|
|
using v8::SideEffectType;
|
|
using v8::String;
|
|
using v8::TracingController;
|
|
using v8::Undefined;
|
|
using v8::V8;
|
|
using v8::Value;
|
|
|
|
static bool v8_is_profiling = false;
|
|
|
|
// Bit flag used to track security reverts (see node_revert.h)
|
|
unsigned int reverted = 0;
|
|
|
|
bool v8_initialized = false;
|
|
|
|
// process-relative uptime base, initialized at start-up
|
|
double prog_start_time;
|
|
|
|
Mutex per_process_opts_mutex;
|
|
std::shared_ptr<PerProcessOptions> per_process_opts {
|
|
new PerProcessOptions() };
|
|
static Mutex node_isolate_mutex;
|
|
static Isolate* node_isolate;
|
|
|
|
// Ensures that __metadata trace events are only emitted
|
|
// when tracing is enabled.
|
|
class NodeTraceStateObserver :
|
|
public TracingController::TraceStateObserver {
|
|
public:
|
|
void OnTraceEnabled() override {
|
|
char name_buffer[512];
|
|
if (uv_get_process_title(name_buffer, sizeof(name_buffer)) == 0) {
|
|
// Only emit the metadata event if the title can be retrieved
|
|
// successfully. Ignore it otherwise.
|
|
TRACE_EVENT_METADATA1("__metadata", "process_name",
|
|
"name", TRACE_STR_COPY(name_buffer));
|
|
}
|
|
TRACE_EVENT_METADATA1("__metadata", "version",
|
|
"node", NODE_VERSION_STRING);
|
|
TRACE_EVENT_METADATA1("__metadata", "thread_name",
|
|
"name", "JavaScriptMainThread");
|
|
|
|
auto trace_process = tracing::TracedValue::Create();
|
|
trace_process->BeginDictionary("versions");
|
|
|
|
#define V(key) \
|
|
trace_process->SetString(#key, per_process::metadata.versions.key.c_str());
|
|
|
|
NODE_VERSIONS_KEYS(V)
|
|
#undef V
|
|
|
|
trace_process->EndDictionary();
|
|
|
|
trace_process->SetString("arch", NODE_ARCH);
|
|
trace_process->SetString("platform", NODE_PLATFORM);
|
|
|
|
trace_process->BeginDictionary("release");
|
|
trace_process->SetString("name", NODE_RELEASE);
|
|
#if NODE_VERSION_IS_LTS
|
|
trace_process->SetString("lts", NODE_VERSION_LTS_CODENAME);
|
|
#endif
|
|
trace_process->EndDictionary();
|
|
TRACE_EVENT_METADATA1("__metadata", "node",
|
|
"process", std::move(trace_process));
|
|
|
|
// This only runs the first time tracing is enabled
|
|
controller_->RemoveTraceStateObserver(this);
|
|
delete this;
|
|
}
|
|
|
|
void OnTraceDisabled() override {
|
|
// Do nothing here. This should never be called because the
|
|
// observer removes itself when OnTraceEnabled() is called.
|
|
UNREACHABLE();
|
|
}
|
|
|
|
explicit NodeTraceStateObserver(TracingController* controller) :
|
|
controller_(controller) {}
|
|
~NodeTraceStateObserver() override {}
|
|
|
|
private:
|
|
TracingController* controller_;
|
|
};
|
|
|
|
static struct {
|
|
#if NODE_USE_V8_PLATFORM
|
|
void Initialize(int thread_pool_size) {
|
|
tracing_agent_.reset(new tracing::Agent());
|
|
node::tracing::TraceEventHelper::SetAgent(tracing_agent_.get());
|
|
auto controller = tracing_agent_->GetTracingController();
|
|
controller->AddTraceStateObserver(new NodeTraceStateObserver(controller));
|
|
StartTracingAgent();
|
|
// Tracing must be initialized before platform threads are created.
|
|
platform_ = new NodePlatform(thread_pool_size, controller);
|
|
V8::InitializePlatform(platform_);
|
|
}
|
|
|
|
void Dispose() {
|
|
platform_->Shutdown();
|
|
delete platform_;
|
|
platform_ = nullptr;
|
|
// Destroy tracing after the platform (and platform threads) have been
|
|
// stopped.
|
|
tracing_agent_.reset(nullptr);
|
|
}
|
|
|
|
void DrainVMTasks(Isolate* isolate) {
|
|
platform_->DrainTasks(isolate);
|
|
}
|
|
|
|
void CancelVMTasks(Isolate* isolate) {
|
|
platform_->CancelPendingDelayedTasks(isolate);
|
|
}
|
|
|
|
#if HAVE_INSPECTOR
|
|
bool StartInspector(Environment* env, const char* script_path) {
|
|
// Inspector agent can't fail to start, but if it was configured to listen
|
|
// right away on the websocket port and fails to bind/etc, this will return
|
|
// false.
|
|
return env->inspector_agent()->Start(
|
|
script_path == nullptr ? "" : script_path,
|
|
env->options()->debug_options(),
|
|
env->inspector_host_port(),
|
|
true);
|
|
}
|
|
|
|
bool InspectorStarted(Environment* env) {
|
|
return env->inspector_agent()->IsListening();
|
|
}
|
|
#endif // HAVE_INSPECTOR
|
|
|
|
void StartTracingAgent() {
|
|
if (per_process_opts->trace_event_categories.empty()) {
|
|
tracing_file_writer_ = tracing_agent_->DefaultHandle();
|
|
} else {
|
|
tracing_file_writer_ = tracing_agent_->AddClient(
|
|
ParseCommaSeparatedSet(per_process_opts->trace_event_categories),
|
|
std::unique_ptr<tracing::AsyncTraceWriter>(
|
|
new tracing::NodeTraceWriter(
|
|
per_process_opts->trace_event_file_pattern)),
|
|
tracing::Agent::kUseDefaultCategories);
|
|
}
|
|
}
|
|
|
|
void StopTracingAgent() {
|
|
tracing_file_writer_.reset();
|
|
}
|
|
|
|
tracing::AgentWriterHandle* GetTracingAgentWriter() {
|
|
return &tracing_file_writer_;
|
|
}
|
|
|
|
NodePlatform* Platform() {
|
|
return platform_;
|
|
}
|
|
|
|
std::unique_ptr<tracing::Agent> tracing_agent_;
|
|
tracing::AgentWriterHandle tracing_file_writer_;
|
|
NodePlatform* platform_;
|
|
#else // !NODE_USE_V8_PLATFORM
|
|
void Initialize(int thread_pool_size) {}
|
|
void Dispose() {}
|
|
void DrainVMTasks(Isolate* isolate) {}
|
|
void CancelVMTasks(Isolate* isolate) {}
|
|
bool StartInspector(Environment* env, const char* script_path) {
|
|
env->ThrowError("Node compiled with NODE_USE_V8_PLATFORM=0");
|
|
return true;
|
|
}
|
|
|
|
void StartTracingAgent() {
|
|
if (!trace_enabled_categories.empty()) {
|
|
fprintf(stderr, "Node compiled with NODE_USE_V8_PLATFORM=0, "
|
|
"so event tracing is not available.\n");
|
|
}
|
|
}
|
|
void StopTracingAgent() {}
|
|
|
|
tracing::AgentWriterHandle* GetTracingAgentWriter() {
|
|
return nullptr;
|
|
}
|
|
|
|
NodePlatform* Platform() {
|
|
return nullptr;
|
|
}
|
|
#endif // !NODE_USE_V8_PLATFORM
|
|
|
|
#if !NODE_USE_V8_PLATFORM || !HAVE_INSPECTOR
|
|
bool InspectorStarted(Environment* env) {
|
|
return false;
|
|
}
|
|
#endif // !NODE_USE_V8_PLATFORM || !HAVE_INSPECTOR
|
|
} v8_platform;
|
|
|
|
tracing::AgentWriterHandle* GetTracingAgentWriter() {
|
|
return v8_platform.GetTracingAgentWriter();
|
|
}
|
|
|
|
#ifdef __POSIX__
|
|
static const unsigned kMaxSignal = 32;
|
|
#endif
|
|
|
|
const char* signo_string(int signo) {
|
|
#define SIGNO_CASE(e) case e: return #e;
|
|
switch (signo) {
|
|
#ifdef SIGHUP
|
|
SIGNO_CASE(SIGHUP);
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
SIGNO_CASE(SIGINT);
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
|
SIGNO_CASE(SIGQUIT);
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
|
SIGNO_CASE(SIGILL);
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
|
SIGNO_CASE(SIGTRAP);
|
|
#endif
|
|
|
|
#ifdef SIGABRT
|
|
SIGNO_CASE(SIGABRT);
|
|
#endif
|
|
|
|
#ifdef SIGIOT
|
|
# if SIGABRT != SIGIOT
|
|
SIGNO_CASE(SIGIOT);
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef SIGBUS
|
|
SIGNO_CASE(SIGBUS);
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
|
SIGNO_CASE(SIGFPE);
|
|
#endif
|
|
|
|
#ifdef SIGKILL
|
|
SIGNO_CASE(SIGKILL);
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
SIGNO_CASE(SIGUSR1);
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
|
SIGNO_CASE(SIGSEGV);
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
SIGNO_CASE(SIGUSR2);
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
SIGNO_CASE(SIGPIPE);
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
SIGNO_CASE(SIGALRM);
|
|
#endif
|
|
|
|
SIGNO_CASE(SIGTERM);
|
|
|
|
#ifdef SIGCHLD
|
|
SIGNO_CASE(SIGCHLD);
|
|
#endif
|
|
|
|
#ifdef SIGSTKFLT
|
|
SIGNO_CASE(SIGSTKFLT);
|
|
#endif
|
|
|
|
|
|
#ifdef SIGCONT
|
|
SIGNO_CASE(SIGCONT);
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
|
SIGNO_CASE(SIGSTOP);
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
|
SIGNO_CASE(SIGTSTP);
|
|
#endif
|
|
|
|
#ifdef SIGBREAK
|
|
SIGNO_CASE(SIGBREAK);
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
|
SIGNO_CASE(SIGTTIN);
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
|
SIGNO_CASE(SIGTTOU);
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
|
SIGNO_CASE(SIGURG);
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
SIGNO_CASE(SIGXCPU);
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
SIGNO_CASE(SIGXFSZ);
|
|
#endif
|
|
|
|
#ifdef SIGVTALRM
|
|
SIGNO_CASE(SIGVTALRM);
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
|
SIGNO_CASE(SIGPROF);
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
|
SIGNO_CASE(SIGWINCH);
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
|
SIGNO_CASE(SIGIO);
|
|
#endif
|
|
|
|
#ifdef SIGPOLL
|
|
# if SIGPOLL != SIGIO
|
|
SIGNO_CASE(SIGPOLL);
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef SIGLOST
|
|
# if SIGLOST != SIGABRT
|
|
SIGNO_CASE(SIGLOST);
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
# if SIGPWR != SIGLOST
|
|
SIGNO_CASE(SIGPWR);
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef SIGINFO
|
|
# if !defined(SIGPWR) || SIGINFO != SIGPWR
|
|
SIGNO_CASE(SIGINFO);
|
|
# endif
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
|
SIGNO_CASE(SIGSYS);
|
|
#endif
|
|
|
|
default: return "";
|
|
}
|
|
}
|
|
|
|
void* ArrayBufferAllocator::Allocate(size_t size) {
|
|
if (zero_fill_field_ || per_process_opts->zero_fill_all_buffers)
|
|
return UncheckedCalloc(size);
|
|
else
|
|
return UncheckedMalloc(size);
|
|
}
|
|
|
|
namespace {
|
|
|
|
bool ShouldAbortOnUncaughtException(Isolate* isolate) {
|
|
HandleScope scope(isolate);
|
|
Environment* env = Environment::GetCurrent(isolate);
|
|
return env != nullptr &&
|
|
env->should_abort_on_uncaught_toggle()[0] &&
|
|
!env->inside_should_not_abort_on_uncaught_scope();
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
void AddPromiseHook(Isolate* isolate, promise_hook_func fn, void* arg) {
|
|
Environment* env = Environment::GetCurrent(isolate);
|
|
CHECK_NOT_NULL(env);
|
|
env->AddPromiseHook(fn, arg);
|
|
}
|
|
|
|
void AddEnvironmentCleanupHook(Isolate* isolate,
|
|
void (*fun)(void* arg),
|
|
void* arg) {
|
|
Environment* env = Environment::GetCurrent(isolate);
|
|
CHECK_NOT_NULL(env);
|
|
env->AddCleanupHook(fun, arg);
|
|
}
|
|
|
|
|
|
void RemoveEnvironmentCleanupHook(Isolate* isolate,
|
|
void (*fun)(void* arg),
|
|
void* arg) {
|
|
Environment* env = Environment::GetCurrent(isolate);
|
|
CHECK_NOT_NULL(env);
|
|
env->RemoveCleanupHook(fun, arg);
|
|
}
|
|
|
|
MaybeLocal<Value> InternalMakeCallback(Environment* env,
|
|
Local<Object> recv,
|
|
const Local<Function> callback,
|
|
int argc,
|
|
Local<Value> argv[],
|
|
async_context asyncContext) {
|
|
CHECK(!recv.IsEmpty());
|
|
InternalCallbackScope scope(env, recv, asyncContext);
|
|
if (scope.Failed()) {
|
|
return MaybeLocal<Value>();
|
|
}
|
|
|
|
Local<Function> domain_cb = env->domain_callback();
|
|
MaybeLocal<Value> ret;
|
|
if (asyncContext.async_id != 0 || domain_cb.IsEmpty() || recv.IsEmpty()) {
|
|
ret = callback->Call(env->context(), recv, argc, argv);
|
|
} else {
|
|
std::vector<Local<Value>> args(1 + argc);
|
|
args[0] = callback;
|
|
std::copy(&argv[0], &argv[argc], args.begin() + 1);
|
|
ret = domain_cb->Call(env->context(), recv, args.size(), &args[0]);
|
|
}
|
|
|
|
if (ret.IsEmpty()) {
|
|
scope.MarkAsFailed();
|
|
return MaybeLocal<Value>();
|
|
}
|
|
|
|
scope.Close();
|
|
if (scope.Failed()) {
|
|
return MaybeLocal<Value>();
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
// Public MakeCallback()s
|
|
|
|
|
|
MaybeLocal<Value> MakeCallback(Isolate* isolate,
|
|
Local<Object> recv,
|
|
const char* method,
|
|
int argc,
|
|
Local<Value> argv[],
|
|
async_context asyncContext) {
|
|
Local<String> method_string =
|
|
String::NewFromUtf8(isolate, method, NewStringType::kNormal)
|
|
.ToLocalChecked();
|
|
return MakeCallback(isolate, recv, method_string, argc, argv, asyncContext);
|
|
}
|
|
|
|
|
|
MaybeLocal<Value> MakeCallback(Isolate* isolate,
|
|
Local<Object> recv,
|
|
Local<String> symbol,
|
|
int argc,
|
|
Local<Value> argv[],
|
|
async_context asyncContext) {
|
|
Local<Value> callback_v = recv->Get(isolate->GetCurrentContext(),
|
|
symbol).ToLocalChecked();
|
|
if (callback_v.IsEmpty()) return Local<Value>();
|
|
if (!callback_v->IsFunction()) return Local<Value>();
|
|
Local<Function> callback = callback_v.As<Function>();
|
|
return MakeCallback(isolate, recv, callback, argc, argv, asyncContext);
|
|
}
|
|
|
|
|
|
MaybeLocal<Value> MakeCallback(Isolate* isolate,
|
|
Local<Object> recv,
|
|
Local<Function> callback,
|
|
int argc,
|
|
Local<Value> argv[],
|
|
async_context asyncContext) {
|
|
// Observe the following two subtleties:
|
|
//
|
|
// 1. The environment is retrieved from the callback function's context.
|
|
// 2. The context to enter is retrieved from the environment.
|
|
//
|
|
// Because of the AssignToContext() call in src/node_contextify.cc,
|
|
// the two contexts need not be the same.
|
|
Environment* env = Environment::GetCurrent(callback->CreationContext());
|
|
CHECK_NOT_NULL(env);
|
|
Context::Scope context_scope(env->context());
|
|
MaybeLocal<Value> ret = InternalMakeCallback(env, recv, callback,
|
|
argc, argv, asyncContext);
|
|
if (ret.IsEmpty() && env->makecallback_depth() == 0) {
|
|
// This is only for legacy compatiblity and we may want to look into
|
|
// removing/adjusting it.
|
|
return Undefined(env->isolate());
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
|
|
// Legacy MakeCallback()s
|
|
|
|
Local<Value> MakeCallback(Isolate* isolate,
|
|
Local<Object> recv,
|
|
const char* method,
|
|
int argc,
|
|
Local<Value>* argv) {
|
|
EscapableHandleScope handle_scope(isolate);
|
|
return handle_scope.Escape(
|
|
MakeCallback(isolate, recv, method, argc, argv, {0, 0})
|
|
.FromMaybe(Local<Value>()));
|
|
}
|
|
|
|
|
|
Local<Value> MakeCallback(Isolate* isolate,
|
|
Local<Object> recv,
|
|
Local<String> symbol,
|
|
int argc,
|
|
Local<Value>* argv) {
|
|
EscapableHandleScope handle_scope(isolate);
|
|
return handle_scope.Escape(
|
|
MakeCallback(isolate, recv, symbol, argc, argv, {0, 0})
|
|
.FromMaybe(Local<Value>()));
|
|
}
|
|
|
|
|
|
Local<Value> MakeCallback(Isolate* isolate,
|
|
Local<Object> recv,
|
|
Local<Function> callback,
|
|
int argc,
|
|
Local<Value>* argv) {
|
|
EscapableHandleScope handle_scope(isolate);
|
|
return handle_scope.Escape(
|
|
MakeCallback(isolate, recv, callback, argc, argv, {0, 0})
|
|
.FromMaybe(Local<Value>()));
|
|
}
|
|
|
|
static void WaitForInspectorDisconnect(Environment* env) {
|
|
#if HAVE_INSPECTOR
|
|
if (env->inspector_agent()->IsActive()) {
|
|
// Restore signal dispositions, the app is done and is no longer
|
|
// capable of handling signals.
|
|
#if defined(__POSIX__) && !defined(NODE_SHARED_MODE)
|
|
struct sigaction act;
|
|
memset(&act, 0, sizeof(act));
|
|
for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
|
|
if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF)
|
|
continue;
|
|
act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
|
|
CHECK_EQ(0, sigaction(nr, &act, nullptr));
|
|
}
|
|
#endif
|
|
env->inspector_agent()->WaitForDisconnect();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void Exit(const FunctionCallbackInfo<Value>& args) {
|
|
Environment* env = Environment::GetCurrent(args);
|
|
WaitForInspectorDisconnect(env);
|
|
v8_platform.StopTracingAgent();
|
|
int code = args[0]->Int32Value(env->context()).FromMaybe(0);
|
|
env->Exit(code);
|
|
}
|
|
|
|
static Maybe<bool> ProcessEmitWarningGeneric(Environment* env,
|
|
const char* warning,
|
|
const char* type = nullptr,
|
|
const char* code = nullptr) {
|
|
HandleScope handle_scope(env->isolate());
|
|
Context::Scope context_scope(env->context());
|
|
|
|
Local<Object> process = env->process_object();
|
|
Local<Value> emit_warning;
|
|
if (!process->Get(env->context(),
|
|
env->emit_warning_string()).ToLocal(&emit_warning)) {
|
|
return Nothing<bool>();
|
|
}
|
|
|
|
if (!emit_warning->IsFunction()) return Just(false);
|
|
|
|
int argc = 0;
|
|
Local<Value> args[3]; // warning, type, code
|
|
|
|
// The caller has to be able to handle a failure anyway, so we might as well
|
|
// do proper error checking for string creation.
|
|
if (!String::NewFromUtf8(env->isolate(),
|
|
warning,
|
|
NewStringType::kNormal).ToLocal(&args[argc++])) {
|
|
return Nothing<bool>();
|
|
}
|
|
if (type != nullptr) {
|
|
if (!String::NewFromOneByte(env->isolate(),
|
|
reinterpret_cast<const uint8_t*>(type),
|
|
NewStringType::kNormal)
|
|
.ToLocal(&args[argc++])) {
|
|
return Nothing<bool>();
|
|
}
|
|
if (code != nullptr &&
|
|
!String::NewFromOneByte(env->isolate(),
|
|
reinterpret_cast<const uint8_t*>(code),
|
|
NewStringType::kNormal)
|
|
.ToLocal(&args[argc++])) {
|
|
return Nothing<bool>();
|
|
}
|
|
}
|
|
|
|
// MakeCallback() unneeded because emitWarning is internal code, it calls
|
|
// process.emit('warning', ...), but does so on the nextTick.
|
|
if (emit_warning.As<Function>()->Call(env->context(),
|
|
process,
|
|
argc,
|
|
args).IsEmpty()) {
|
|
return Nothing<bool>();
|
|
}
|
|
return Just(true);
|
|
}
|
|
|
|
|
|
// Call process.emitWarning(str), fmt is a snprintf() format string
|
|
Maybe<bool> ProcessEmitWarning(Environment* env, const char* fmt, ...) {
|
|
char warning[1024];
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
vsnprintf(warning, sizeof(warning), fmt, ap);
|
|
va_end(ap);
|
|
|
|
return ProcessEmitWarningGeneric(env, warning);
|
|
}
|
|
|
|
|
|
Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
|
|
const char* warning,
|
|
const char* deprecation_code) {
|
|
return ProcessEmitWarningGeneric(env,
|
|
warning,
|
|
"DeprecationWarning",
|
|
deprecation_code);
|
|
}
|
|
|
|
static void OnMessage(Local<Message> message, Local<Value> error) {
|
|
Isolate* isolate = message->GetIsolate();
|
|
switch (message->ErrorLevel()) {
|
|
case Isolate::MessageErrorLevel::kMessageWarning: {
|
|
Environment* env = Environment::GetCurrent(isolate);
|
|
if (!env) {
|
|
break;
|
|
}
|
|
Utf8Value filename(isolate,
|
|
message->GetScriptOrigin().ResourceName());
|
|
// (filename):(line) (message)
|
|
std::stringstream warning;
|
|
warning << *filename;
|
|
warning << ":";
|
|
warning << message->GetLineNumber(env->context()).FromMaybe(-1);
|
|
warning << " ";
|
|
v8::String::Utf8Value msg(isolate, message->Get());
|
|
warning << *msg;
|
|
USE(ProcessEmitWarningGeneric(env, warning.str().c_str(), "V8"));
|
|
break;
|
|
}
|
|
case Isolate::MessageErrorLevel::kMessageError:
|
|
FatalException(isolate, error, message);
|
|
break;
|
|
}
|
|
}
|
|
|
|
static Local<Object> GetFeatures(Environment* env) {
|
|
EscapableHandleScope scope(env->isolate());
|
|
|
|
Local<Object> obj = Object::New(env->isolate());
|
|
#if defined(DEBUG) && DEBUG
|
|
Local<Value> debug = True(env->isolate());
|
|
#else
|
|
Local<Value> debug = False(env->isolate());
|
|
#endif // defined(DEBUG) && DEBUG
|
|
|
|
obj->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "debug"),
|
|
debug).FromJust();
|
|
obj->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "uv"),
|
|
True(env->isolate())).FromJust();
|
|
// TODO(bnoordhuis) ping libuv
|
|
obj->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "ipv6"),
|
|
True(env->isolate())).FromJust();
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
Local<Boolean> have_openssl = True(env->isolate());
|
|
#else
|
|
Local<Boolean> have_openssl = False(env->isolate());
|
|
#endif
|
|
|
|
obj->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "tls_alpn"),
|
|
have_openssl).FromJust();
|
|
obj->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "tls_sni"),
|
|
have_openssl).FromJust();
|
|
obj->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "tls_ocsp"),
|
|
have_openssl).FromJust();
|
|
obj->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "tls"),
|
|
have_openssl).FromJust();
|
|
|
|
return scope.Escape(obj);
|
|
}
|
|
|
|
void SetupProcessObject(Environment* env,
|
|
const std::vector<std::string>& args,
|
|
const std::vector<std::string>& exec_args) {
|
|
Isolate* isolate = env->isolate();
|
|
HandleScope scope(isolate);
|
|
Local<Context> context = env->context();
|
|
|
|
Local<Object> process = env->process_object();
|
|
|
|
auto title_string = FIXED_ONE_BYTE_STRING(env->isolate(), "title");
|
|
CHECK(process->SetAccessor(
|
|
env->context(),
|
|
title_string,
|
|
ProcessTitleGetter,
|
|
env->is_main_thread() ? ProcessTitleSetter : nullptr,
|
|
env->as_external(),
|
|
DEFAULT,
|
|
None,
|
|
SideEffectType::kHasNoSideEffect).FromJust());
|
|
|
|
// process.version
|
|
READONLY_PROPERTY(process,
|
|
"version",
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION));
|
|
|
|
// process.versions
|
|
Local<Object> versions = Object::New(env->isolate());
|
|
READONLY_PROPERTY(process, "versions", versions);
|
|
|
|
#define V(key) \
|
|
if (!per_process::metadata.versions.key.empty()) { \
|
|
READONLY_STRING_PROPERTY( \
|
|
versions, #key, per_process::metadata.versions.key); \
|
|
}
|
|
NODE_VERSIONS_KEYS(V)
|
|
#undef V
|
|
|
|
// process.arch
|
|
READONLY_PROPERTY(process, "arch", OneByteString(env->isolate(), NODE_ARCH));
|
|
|
|
// process.platform
|
|
READONLY_PROPERTY(process,
|
|
"platform",
|
|
OneByteString(env->isolate(), NODE_PLATFORM));
|
|
|
|
// process.release
|
|
Local<Object> release = Object::New(env->isolate());
|
|
READONLY_PROPERTY(process, "release", release);
|
|
READONLY_PROPERTY(release, "name",
|
|
OneByteString(env->isolate(), NODE_RELEASE));
|
|
|
|
#if NODE_VERSION_IS_LTS
|
|
READONLY_PROPERTY(release, "lts",
|
|
OneByteString(env->isolate(), NODE_VERSION_LTS_CODENAME));
|
|
#endif
|
|
|
|
// if this is a release build and no explicit base has been set
|
|
// substitute the standard release download URL
|
|
#ifndef NODE_RELEASE_URLBASE
|
|
# if NODE_VERSION_IS_RELEASE
|
|
# define NODE_RELEASE_URLBASE "https://nodejs.org/download/release/"
|
|
# endif
|
|
#endif
|
|
|
|
#if defined(NODE_RELEASE_URLBASE)
|
|
# define NODE_RELEASE_URLPFX NODE_RELEASE_URLBASE "v" NODE_VERSION_STRING "/"
|
|
# define NODE_RELEASE_URLFPFX NODE_RELEASE_URLPFX "node-v" NODE_VERSION_STRING
|
|
|
|
READONLY_PROPERTY(release,
|
|
"sourceUrl",
|
|
OneByteString(env->isolate(),
|
|
NODE_RELEASE_URLFPFX ".tar.gz"));
|
|
READONLY_PROPERTY(release,
|
|
"headersUrl",
|
|
OneByteString(env->isolate(),
|
|
NODE_RELEASE_URLFPFX "-headers.tar.gz"));
|
|
# ifdef _WIN32
|
|
READONLY_PROPERTY(release,
|
|
"libUrl",
|
|
OneByteString(env->isolate(),
|
|
strcmp(NODE_ARCH, "ia32") ? NODE_RELEASE_URLPFX "win-"
|
|
NODE_ARCH "/node.lib"
|
|
: NODE_RELEASE_URLPFX
|
|
"win-x86/node.lib"));
|
|
# endif
|
|
#endif
|
|
|
|
// process.argv
|
|
Local<Array> arguments = Array::New(env->isolate(), args.size());
|
|
for (size_t i = 0; i < args.size(); ++i) {
|
|
arguments->Set(env->context(), i,
|
|
String::NewFromUtf8(env->isolate(), args[i].c_str(),
|
|
NewStringType::kNormal).ToLocalChecked())
|
|
.FromJust();
|
|
}
|
|
process->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "argv"),
|
|
arguments).FromJust();
|
|
|
|
// process.execArgv
|
|
Local<Array> exec_arguments = Array::New(env->isolate(), exec_args.size());
|
|
for (size_t i = 0; i < exec_args.size(); ++i) {
|
|
exec_arguments->Set(env->context(), i,
|
|
String::NewFromUtf8(env->isolate(), exec_args[i].c_str(),
|
|
NewStringType::kNormal).ToLocalChecked())
|
|
.FromJust();
|
|
}
|
|
process->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "execArgv"),
|
|
exec_arguments).FromJust();
|
|
|
|
// create process.env
|
|
process
|
|
->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "env"),
|
|
CreateEnvVarProxy(context, isolate, env->as_external()))
|
|
.FromJust();
|
|
|
|
READONLY_PROPERTY(process, "pid",
|
|
Integer::New(env->isolate(), uv_os_getpid()));
|
|
READONLY_PROPERTY(process, "features", GetFeatures(env));
|
|
|
|
CHECK(process->SetAccessor(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "ppid"),
|
|
GetParentProcessId).FromJust());
|
|
|
|
// -e, --eval
|
|
// TODO(addaleax): Remove this.
|
|
if (env->options()->has_eval_string) {
|
|
READONLY_PROPERTY(process,
|
|
"_eval",
|
|
String::NewFromUtf8(
|
|
env->isolate(),
|
|
env->options()->eval_string.c_str(),
|
|
NewStringType::kNormal).ToLocalChecked());
|
|
}
|
|
|
|
// -p, --print
|
|
// TODO(addaleax): Remove this.
|
|
if (env->options()->print_eval) {
|
|
READONLY_PROPERTY(process, "_print_eval", True(env->isolate()));
|
|
}
|
|
|
|
// -c, --check
|
|
// TODO(addaleax): Remove this.
|
|
if (env->options()->syntax_check_only) {
|
|
READONLY_PROPERTY(process, "_syntax_check_only", True(env->isolate()));
|
|
}
|
|
|
|
// -i, --interactive
|
|
// TODO(addaleax): Remove this.
|
|
if (env->options()->force_repl) {
|
|
READONLY_PROPERTY(process, "_forceRepl", True(env->isolate()));
|
|
}
|
|
|
|
// -r, --require
|
|
// TODO(addaleax): Remove this.
|
|
const std::vector<std::string>& preload_modules =
|
|
env->options()->preload_modules;
|
|
if (!preload_modules.empty()) {
|
|
Local<Array> array = Array::New(env->isolate());
|
|
for (unsigned int i = 0; i < preload_modules.size(); ++i) {
|
|
Local<String> module = String::NewFromUtf8(env->isolate(),
|
|
preload_modules[i].c_str(),
|
|
NewStringType::kNormal)
|
|
.ToLocalChecked();
|
|
array->Set(env->context(), i, module).FromJust();
|
|
}
|
|
READONLY_PROPERTY(process,
|
|
"_preload_modules",
|
|
array);
|
|
}
|
|
|
|
// --no-deprecation
|
|
if (env->options()->no_deprecation) {
|
|
READONLY_PROPERTY(process, "noDeprecation", True(env->isolate()));
|
|
}
|
|
|
|
// --no-warnings
|
|
if (env->options()->no_warnings) {
|
|
READONLY_PROPERTY(process, "noProcessWarnings", True(env->isolate()));
|
|
}
|
|
|
|
// --trace-warnings
|
|
if (env->options()->trace_warnings) {
|
|
READONLY_PROPERTY(process, "traceProcessWarnings", True(env->isolate()));
|
|
}
|
|
|
|
// --throw-deprecation
|
|
if (env->options()->throw_deprecation) {
|
|
READONLY_PROPERTY(process, "throwDeprecation", True(env->isolate()));
|
|
}
|
|
|
|
#ifdef NODE_NO_BROWSER_GLOBALS
|
|
// configure --no-browser-globals
|
|
READONLY_PROPERTY(process, "_noBrowserGlobals", True(env->isolate()));
|
|
#endif // NODE_NO_BROWSER_GLOBALS
|
|
|
|
// --prof-process
|
|
// TODO(addaleax): Remove this.
|
|
if (env->options()->prof_process) {
|
|
READONLY_PROPERTY(process, "profProcess", True(env->isolate()));
|
|
}
|
|
|
|
// --trace-deprecation
|
|
if (env->options()->trace_deprecation) {
|
|
READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate()));
|
|
}
|
|
|
|
// TODO(refack): move the following 4 to `node_config`
|
|
// --inspect-brk
|
|
if (env->options()->debug_options().wait_for_connect()) {
|
|
READONLY_DONT_ENUM_PROPERTY(process,
|
|
"_breakFirstLine", True(env->isolate()));
|
|
}
|
|
|
|
if (env->options()->debug_options().break_node_first_line) {
|
|
READONLY_DONT_ENUM_PROPERTY(process,
|
|
"_breakNodeFirstLine", True(env->isolate()));
|
|
}
|
|
|
|
// --inspect --debug-brk
|
|
if (env->options()->debug_options().deprecated_invocation()) {
|
|
READONLY_DONT_ENUM_PROPERTY(process,
|
|
"_deprecatedDebugBrk", True(env->isolate()));
|
|
}
|
|
|
|
// --debug or, --debug-brk without --inspect
|
|
if (env->options()->debug_options().invalid_invocation()) {
|
|
READONLY_DONT_ENUM_PROPERTY(process,
|
|
"_invalidDebug", True(env->isolate()));
|
|
}
|
|
|
|
// --security-revert flags
|
|
#define V(code, _, __) \
|
|
do { \
|
|
if (IsReverted(SECURITY_REVERT_ ## code)) { \
|
|
READONLY_PROPERTY(process, "REVERT_" #code, True(env->isolate())); \
|
|
} \
|
|
} while (0);
|
|
SECURITY_REVERSIONS(V)
|
|
#undef V
|
|
|
|
{
|
|
size_t exec_path_len = 2 * PATH_MAX;
|
|
std::vector<char> exec_path(exec_path_len);
|
|
Local<String> exec_path_value;
|
|
if (uv_exepath(exec_path.data(), &exec_path_len) == 0) {
|
|
exec_path_value = String::NewFromUtf8(env->isolate(),
|
|
exec_path.data(),
|
|
NewStringType::kInternalized,
|
|
exec_path_len).ToLocalChecked();
|
|
} else {
|
|
exec_path_value = String::NewFromUtf8(env->isolate(), args[0].c_str(),
|
|
NewStringType::kInternalized).ToLocalChecked();
|
|
}
|
|
process->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "execPath"),
|
|
exec_path_value).FromJust();
|
|
}
|
|
|
|
auto debug_port_string = FIXED_ONE_BYTE_STRING(env->isolate(), "debugPort");
|
|
CHECK(process->SetAccessor(env->context(),
|
|
debug_port_string,
|
|
DebugPortGetter,
|
|
env->is_main_thread() ? DebugPortSetter : nullptr,
|
|
env->as_external()).FromJust());
|
|
}
|
|
|
|
|
|
void SignalExit(int signo) {
|
|
uv_tty_reset_mode();
|
|
#ifdef __FreeBSD__
|
|
// FreeBSD has a nasty bug, see RegisterSignalHandler for details
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = SIG_DFL;
|
|
CHECK_EQ(sigaction(signo, &sa, nullptr), 0);
|
|
#endif
|
|
raise(signo);
|
|
}
|
|
|
|
static MaybeLocal<Value> ExecuteBootstrapper(
|
|
Environment* env,
|
|
const char* id,
|
|
std::vector<Local<String>>* parameters,
|
|
std::vector<Local<Value>>* arguments) {
|
|
MaybeLocal<Value> ret = per_process::native_module_loader.CompileAndCall(
|
|
env->context(), id, parameters, arguments, env);
|
|
|
|
// If there was an error during bootstrap then it was either handled by the
|
|
// FatalException handler or it's unrecoverable (e.g. max call stack
|
|
// exceeded). Either way, clear the stack so that the AsyncCallbackScope
|
|
// destructor doesn't fail on the id check.
|
|
// There are only two ways to have a stack size > 1: 1) the user manually
|
|
// called MakeCallback or 2) user awaited during bootstrap, which triggered
|
|
// _tickCallback().
|
|
if (ret.IsEmpty()) {
|
|
env->async_hooks()->clear_async_id_stack();
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
void LoadEnvironment(Environment* env) {
|
|
HandleScope handle_scope(env->isolate());
|
|
Isolate* isolate = env->isolate();
|
|
Local<Context> context = env->context();
|
|
|
|
// Add a reference to the global object
|
|
Local<Object> global = context->Global();
|
|
|
|
#if defined HAVE_DTRACE || defined HAVE_ETW
|
|
InitDTrace(env, global);
|
|
#endif
|
|
|
|
Local<Object> process = env->process_object();
|
|
|
|
// Setting global properties for the bootstrappers to use:
|
|
// - global
|
|
// - process._rawDebug
|
|
// Expose the global object as a property on itself
|
|
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
|
|
global->Set(context, FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global)
|
|
.FromJust();
|
|
env->SetMethod(process, "_rawDebug", RawDebug);
|
|
|
|
// Create binding loaders
|
|
std::vector<Local<String>> loaders_params = {
|
|
env->process_string(),
|
|
FIXED_ONE_BYTE_STRING(isolate, "getBinding"),
|
|
FIXED_ONE_BYTE_STRING(isolate, "getLinkedBinding"),
|
|
FIXED_ONE_BYTE_STRING(isolate, "getInternalBinding"),
|
|
FIXED_ONE_BYTE_STRING(isolate, "debugBreak")};
|
|
std::vector<Local<Value>> loaders_args = {
|
|
process,
|
|
env->NewFunctionTemplate(binding::GetBinding)
|
|
->GetFunction(context)
|
|
.ToLocalChecked(),
|
|
env->NewFunctionTemplate(binding::GetLinkedBinding)
|
|
->GetFunction(context)
|
|
.ToLocalChecked(),
|
|
env->NewFunctionTemplate(binding::GetInternalBinding)
|
|
->GetFunction(context)
|
|
.ToLocalChecked(),
|
|
Boolean::New(isolate,
|
|
env->options()->debug_options().break_node_first_line)};
|
|
|
|
MaybeLocal<Value> loader_exports;
|
|
// Bootstrap internal loaders
|
|
loader_exports = ExecuteBootstrapper(
|
|
env, "internal/bootstrap/loaders", &loaders_params, &loaders_args);
|
|
if (loader_exports.IsEmpty()) {
|
|
return;
|
|
}
|
|
|
|
// process, bootstrappers, loaderExports, triggerFatalException
|
|
std::vector<Local<String>> node_params = {
|
|
env->process_string(),
|
|
FIXED_ONE_BYTE_STRING(isolate, "loaderExports"),
|
|
FIXED_ONE_BYTE_STRING(isolate, "triggerFatalException"),
|
|
FIXED_ONE_BYTE_STRING(isolate, "isMainThread")};
|
|
std::vector<Local<Value>> node_args = {
|
|
process,
|
|
loader_exports.ToLocalChecked(),
|
|
env->NewFunctionTemplate(FatalException)
|
|
->GetFunction(context)
|
|
.ToLocalChecked(),
|
|
Boolean::New(isolate, env->is_main_thread())};
|
|
|
|
if (ExecuteBootstrapper(
|
|
env, "internal/bootstrap/node", &node_params, &node_args)
|
|
.IsEmpty()) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void StartInspector(Environment* env, const char* path) {
|
|
#if HAVE_INSPECTOR
|
|
CHECK(!env->inspector_agent()->IsListening());
|
|
v8_platform.StartInspector(env, path);
|
|
#endif // HAVE_INSPECTOR
|
|
}
|
|
|
|
|
|
#ifdef __POSIX__
|
|
void RegisterSignalHandler(int signal,
|
|
void (*handler)(int signal),
|
|
bool reset_handler) {
|
|
struct sigaction sa;
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = handler;
|
|
#ifndef __FreeBSD__
|
|
// FreeBSD has a nasty bug with SA_RESETHAND reseting the SA_SIGINFO, that is
|
|
// in turn set for a libthr wrapper. This leads to a crash.
|
|
// Work around the issue by manually setting SIG_DFL in the signal handler
|
|
sa.sa_flags = reset_handler ? SA_RESETHAND : 0;
|
|
#endif
|
|
sigfillset(&sa.sa_mask);
|
|
CHECK_EQ(sigaction(signal, &sa, nullptr), 0);
|
|
}
|
|
|
|
#endif // __POSIX__
|
|
|
|
inline void PlatformInit() {
|
|
#ifdef __POSIX__
|
|
#if HAVE_INSPECTOR
|
|
sigset_t sigmask;
|
|
sigemptyset(&sigmask);
|
|
sigaddset(&sigmask, SIGUSR1);
|
|
const int err = pthread_sigmask(SIG_SETMASK, &sigmask, nullptr);
|
|
#endif // HAVE_INSPECTOR
|
|
|
|
// Make sure file descriptors 0-2 are valid before we start logging anything.
|
|
for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd += 1) {
|
|
struct stat ignored;
|
|
if (fstat(fd, &ignored) == 0)
|
|
continue;
|
|
// Anything but EBADF means something is seriously wrong. We don't
|
|
// have to special-case EINTR, fstat() is not interruptible.
|
|
if (errno != EBADF)
|
|
ABORT();
|
|
if (fd != open("/dev/null", O_RDWR))
|
|
ABORT();
|
|
}
|
|
|
|
#if HAVE_INSPECTOR
|
|
CHECK_EQ(err, 0);
|
|
#endif // HAVE_INSPECTOR
|
|
|
|
#ifndef NODE_SHARED_MODE
|
|
// Restore signal dispositions, the parent process may have changed them.
|
|
struct sigaction act;
|
|
memset(&act, 0, sizeof(act));
|
|
|
|
// The hard-coded upper limit is because NSIG is not very reliable; on Linux,
|
|
// it evaluates to 32, 34 or 64, depending on whether RT signals are enabled.
|
|
// Counting up to SIGRTMIN doesn't work for the same reason.
|
|
for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
|
|
if (nr == SIGKILL || nr == SIGSTOP)
|
|
continue;
|
|
act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
|
|
CHECK_EQ(0, sigaction(nr, &act, nullptr));
|
|
}
|
|
#endif // !NODE_SHARED_MODE
|
|
|
|
RegisterSignalHandler(SIGINT, SignalExit, true);
|
|
RegisterSignalHandler(SIGTERM, SignalExit, true);
|
|
|
|
// Raise the open file descriptor limit.
|
|
struct rlimit lim;
|
|
if (getrlimit(RLIMIT_NOFILE, &lim) == 0 && lim.rlim_cur != lim.rlim_max) {
|
|
// Do a binary search for the limit.
|
|
rlim_t min = lim.rlim_cur;
|
|
rlim_t max = 1 << 20;
|
|
// But if there's a defined upper bound, don't search, just set it.
|
|
if (lim.rlim_max != RLIM_INFINITY) {
|
|
min = lim.rlim_max;
|
|
max = lim.rlim_max;
|
|
}
|
|
do {
|
|
lim.rlim_cur = min + (max - min) / 2;
|
|
if (setrlimit(RLIMIT_NOFILE, &lim)) {
|
|
max = lim.rlim_cur;
|
|
} else {
|
|
min = lim.rlim_cur;
|
|
}
|
|
} while (min + 1 < max);
|
|
}
|
|
#endif // __POSIX__
|
|
#ifdef _WIN32
|
|
for (int fd = 0; fd <= 2; ++fd) {
|
|
auto handle = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
|
|
if (handle == INVALID_HANDLE_VALUE ||
|
|
GetFileType(handle) == FILE_TYPE_UNKNOWN) {
|
|
// Ignore _close result. If it fails or not depends on used Windows
|
|
// version. We will just check _open result.
|
|
_close(fd);
|
|
if (fd != _open("nul", _O_RDWR))
|
|
ABORT();
|
|
}
|
|
}
|
|
#endif // _WIN32
|
|
}
|
|
|
|
void ProcessArgv(std::vector<std::string>* args,
|
|
std::vector<std::string>* exec_args,
|
|
bool is_env) {
|
|
// Parse a few arguments which are specific to Node.
|
|
std::vector<std::string> v8_args;
|
|
std::vector<std::string> errors{};
|
|
|
|
{
|
|
// TODO(addaleax): The mutex here should ideally be held during the
|
|
// entire function, but that doesn't play well with the exit() calls below.
|
|
Mutex::ScopedLock lock(per_process_opts_mutex);
|
|
options_parser::PerProcessOptionsParser::instance.Parse(
|
|
args,
|
|
exec_args,
|
|
&v8_args,
|
|
per_process_opts.get(),
|
|
is_env ? kAllowedInEnvironment : kDisallowedInEnvironment,
|
|
&errors);
|
|
}
|
|
|
|
if (!errors.empty()) {
|
|
for (auto const& error : errors) {
|
|
fprintf(stderr, "%s: %s\n", args->at(0).c_str(), error.c_str());
|
|
}
|
|
exit(9);
|
|
}
|
|
|
|
if (per_process_opts->print_version) {
|
|
printf("%s\n", NODE_VERSION);
|
|
exit(0);
|
|
}
|
|
|
|
if (per_process_opts->print_v8_help) {
|
|
V8::SetFlagsFromString("--help", 6);
|
|
exit(0);
|
|
}
|
|
|
|
for (const std::string& cve : per_process_opts->security_reverts)
|
|
Revert(cve.c_str());
|
|
|
|
auto env_opts = per_process_opts->per_isolate->per_env;
|
|
if (std::find(v8_args.begin(), v8_args.end(),
|
|
"--abort-on-uncaught-exception") != v8_args.end() ||
|
|
std::find(v8_args.begin(), v8_args.end(),
|
|
"--abort_on_uncaught_exception") != v8_args.end()) {
|
|
env_opts->abort_on_uncaught_exception = true;
|
|
}
|
|
|
|
// TODO(bnoordhuis) Intercept --prof arguments and start the CPU profiler
|
|
// manually? That would give us a little more control over its runtime
|
|
// behavior but it could also interfere with the user's intentions in ways
|
|
// we fail to anticipate. Dillema.
|
|
if (std::find(v8_args.begin(), v8_args.end(), "--prof") != v8_args.end()) {
|
|
v8_is_profiling = true;
|
|
}
|
|
|
|
#ifdef __POSIX__
|
|
// Block SIGPROF signals when sleeping in epoll_wait/kevent/etc. Avoids the
|
|
// performance penalty of frequent EINTR wakeups when the profiler is running.
|
|
// Only do this for v8.log profiling, as it breaks v8::CpuProfiler users.
|
|
if (v8_is_profiling) {
|
|
uv_loop_configure(uv_default_loop(), UV_LOOP_BLOCK_SIGNAL, SIGPROF);
|
|
}
|
|
#endif
|
|
|
|
std::vector<char*> v8_args_as_char_ptr(v8_args.size());
|
|
if (v8_args.size() > 0) {
|
|
for (size_t i = 0; i < v8_args.size(); ++i)
|
|
v8_args_as_char_ptr[i] = &v8_args[i][0];
|
|
int argc = v8_args.size();
|
|
V8::SetFlagsFromCommandLine(&argc, &v8_args_as_char_ptr[0], true);
|
|
v8_args_as_char_ptr.resize(argc);
|
|
}
|
|
|
|
// Anything that's still in v8_argv is not a V8 or a node option.
|
|
for (size_t i = 1; i < v8_args_as_char_ptr.size(); i++) {
|
|
fprintf(stderr, "%s: bad option: %s\n",
|
|
args->at(0).c_str(), v8_args_as_char_ptr[i]);
|
|
}
|
|
|
|
if (v8_args_as_char_ptr.size() > 1) {
|
|
exit(9);
|
|
}
|
|
}
|
|
|
|
|
|
void Init(std::vector<std::string>* argv,
|
|
std::vector<std::string>* exec_argv) {
|
|
// Initialize prog_start_time to get relative uptime.
|
|
prog_start_time = static_cast<double>(uv_now(uv_default_loop()));
|
|
|
|
// Register built-in modules
|
|
binding::RegisterBuiltinModules();
|
|
|
|
// Make inherited handles noninheritable.
|
|
uv_disable_stdio_inheritance();
|
|
|
|
#if defined(NODE_V8_OPTIONS)
|
|
// Should come before the call to V8::SetFlagsFromCommandLine()
|
|
// so the user can disable a flag --foo at run-time by passing
|
|
// --no_foo from the command line.
|
|
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1);
|
|
#endif
|
|
|
|
std::shared_ptr<EnvironmentOptions> default_env_options =
|
|
per_process_opts->per_isolate->per_env;
|
|
{
|
|
std::string text;
|
|
default_env_options->pending_deprecation =
|
|
credentials::SafeGetenv("NODE_PENDING_DEPRECATION", &text) &&
|
|
text[0] == '1';
|
|
}
|
|
|
|
// Allow for environment set preserving symlinks.
|
|
{
|
|
std::string text;
|
|
default_env_options->preserve_symlinks =
|
|
credentials::SafeGetenv("NODE_PRESERVE_SYMLINKS", &text) &&
|
|
text[0] == '1';
|
|
}
|
|
|
|
{
|
|
std::string text;
|
|
default_env_options->preserve_symlinks_main =
|
|
credentials::SafeGetenv("NODE_PRESERVE_SYMLINKS_MAIN", &text) &&
|
|
text[0] == '1';
|
|
}
|
|
|
|
if (default_env_options->redirect_warnings.empty()) {
|
|
credentials::SafeGetenv("NODE_REDIRECT_WARNINGS",
|
|
&default_env_options->redirect_warnings);
|
|
}
|
|
|
|
#if HAVE_OPENSSL
|
|
std::string* openssl_config = &per_process_opts->openssl_config;
|
|
if (openssl_config->empty()) {
|
|
credentials::SafeGetenv("OPENSSL_CONF", openssl_config);
|
|
}
|
|
#endif
|
|
|
|
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
|
|
std::string node_options;
|
|
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) {
|
|
std::vector<std::string> env_argv;
|
|
// [0] is expected to be the program name, fill it in from the real argv.
|
|
env_argv.push_back(argv->at(0));
|
|
|
|
// Split NODE_OPTIONS at each ' ' character.
|
|
std::string::size_type index = std::string::npos;
|
|
do {
|
|
std::string::size_type prev_index = index;
|
|
index = node_options.find(' ', index + 1);
|
|
if (index - prev_index == 1) continue;
|
|
|
|
const std::string option = node_options.substr(
|
|
prev_index + 1, index - prev_index - 1);
|
|
if (!option.empty())
|
|
env_argv.emplace_back(std::move(option));
|
|
} while (index != std::string::npos);
|
|
|
|
|
|
ProcessArgv(&env_argv, nullptr, true);
|
|
}
|
|
#endif
|
|
|
|
ProcessArgv(argv, exec_argv, false);
|
|
|
|
// Set the process.title immediately after processing argv if --title is set.
|
|
if (!per_process_opts->title.empty())
|
|
uv_set_process_title(per_process_opts->title.c_str());
|
|
|
|
#if defined(NODE_HAVE_I18N_SUPPORT)
|
|
// If the parameter isn't given, use the env variable.
|
|
if (per_process_opts->icu_data_dir.empty())
|
|
credentials::SafeGetenv("NODE_ICU_DATA", &per_process_opts->icu_data_dir);
|
|
// Initialize ICU.
|
|
// If icu_data_dir is empty here, it will load the 'minimal' data.
|
|
if (!i18n::InitializeICUDirectory(per_process_opts->icu_data_dir)) {
|
|
fprintf(stderr,
|
|
"%s: could not initialize ICU "
|
|
"(check NODE_ICU_DATA or --icu-data-dir parameters)\n",
|
|
argv->at(0).c_str());
|
|
exit(9);
|
|
}
|
|
per_process::metadata.versions.InitializeIntlVersions();
|
|
#endif
|
|
|
|
// We should set node_is_initialized here instead of in node::Start,
|
|
// otherwise embedders using node::Init to initialize everything will not be
|
|
// able to set it and native modules will not load for them.
|
|
node_is_initialized = true;
|
|
}
|
|
|
|
// TODO(addaleax): Deprecate and eventually remove this.
|
|
void Init(int* argc,
|
|
const char** argv,
|
|
int* exec_argc,
|
|
const char*** exec_argv) {
|
|
std::vector<std::string> argv_(argv, argv + *argc); // NOLINT
|
|
std::vector<std::string> exec_argv_;
|
|
|
|
Init(&argv_, &exec_argv_);
|
|
|
|
*argc = argv_.size();
|
|
*exec_argc = exec_argv_.size();
|
|
// These leak memory, because, in the original code of this function, no
|
|
// extra allocations were visible. This should be okay because this function
|
|
// is only supposed to be called once per process, though.
|
|
*exec_argv = Malloc<const char*>(*exec_argc);
|
|
for (int i = 0; i < *exec_argc; ++i)
|
|
(*exec_argv)[i] = strdup(exec_argv_[i].c_str());
|
|
for (int i = 0; i < *argc; ++i)
|
|
argv[i] = strdup(argv_[i].c_str());
|
|
}
|
|
|
|
void RunAtExit(Environment* env) {
|
|
env->RunAtExitCallbacks();
|
|
}
|
|
|
|
|
|
uv_loop_t* GetCurrentEventLoop(Isolate* isolate) {
|
|
HandleScope handle_scope(isolate);
|
|
Local<Context> context = isolate->GetCurrentContext();
|
|
if (context.IsEmpty())
|
|
return nullptr;
|
|
Environment* env = Environment::GetCurrent(context);
|
|
if (env == nullptr)
|
|
return nullptr;
|
|
return env->event_loop();
|
|
}
|
|
|
|
|
|
void AtExit(void (*cb)(void* arg), void* arg) {
|
|
auto env = Environment::GetThreadLocalEnv();
|
|
AtExit(env, cb, arg);
|
|
}
|
|
|
|
|
|
void AtExit(Environment* env, void (*cb)(void* arg), void* arg) {
|
|
CHECK_NOT_NULL(env);
|
|
env->AtExit(cb, arg);
|
|
}
|
|
|
|
|
|
void RunBeforeExit(Environment* env) {
|
|
env->RunBeforeExitCallbacks();
|
|
|
|
if (!uv_loop_alive(env->event_loop()))
|
|
EmitBeforeExit(env);
|
|
}
|
|
|
|
|
|
void EmitBeforeExit(Environment* env) {
|
|
HandleScope handle_scope(env->isolate());
|
|
Context::Scope context_scope(env->context());
|
|
Local<Object> process_object = env->process_object();
|
|
Local<String> exit_code = env->exit_code_string();
|
|
Local<Value> args[] = {
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "beforeExit"),
|
|
process_object->Get(env->context(), exit_code).ToLocalChecked()
|
|
->ToInteger(env->context()).ToLocalChecked()
|
|
};
|
|
MakeCallback(env->isolate(),
|
|
process_object, "emit", arraysize(args), args,
|
|
{0, 0}).ToLocalChecked();
|
|
}
|
|
|
|
|
|
int EmitExit(Environment* env) {
|
|
// process.emit('exit')
|
|
HandleScope handle_scope(env->isolate());
|
|
Context::Scope context_scope(env->context());
|
|
Local<Object> process_object = env->process_object();
|
|
process_object->Set(env->context(),
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "_exiting"),
|
|
True(env->isolate())).FromJust();
|
|
|
|
Local<String> exit_code = env->exit_code_string();
|
|
int code = process_object->Get(env->context(), exit_code).ToLocalChecked()
|
|
->Int32Value(env->context()).ToChecked();
|
|
|
|
Local<Value> args[] = {
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "exit"),
|
|
Integer::New(env->isolate(), code)
|
|
};
|
|
|
|
MakeCallback(env->isolate(),
|
|
process_object, "emit", arraysize(args), args,
|
|
{0, 0}).ToLocalChecked();
|
|
|
|
// Reload exit code, it may be changed by `emit('exit')`
|
|
return process_object->Get(env->context(), exit_code).ToLocalChecked()
|
|
->Int32Value(env->context()).ToChecked();
|
|
}
|
|
|
|
|
|
ArrayBufferAllocator* CreateArrayBufferAllocator() {
|
|
return new ArrayBufferAllocator();
|
|
}
|
|
|
|
|
|
void FreeArrayBufferAllocator(ArrayBufferAllocator* allocator) {
|
|
delete allocator;
|
|
}
|
|
|
|
|
|
IsolateData* CreateIsolateData(
|
|
Isolate* isolate,
|
|
uv_loop_t* loop,
|
|
MultiIsolatePlatform* platform,
|
|
ArrayBufferAllocator* allocator) {
|
|
return new IsolateData(
|
|
isolate,
|
|
loop,
|
|
platform,
|
|
allocator != nullptr ? allocator->zero_fill_field() : nullptr);
|
|
}
|
|
|
|
|
|
void FreeIsolateData(IsolateData* isolate_data) {
|
|
delete isolate_data;
|
|
}
|
|
|
|
|
|
Environment* CreateEnvironment(IsolateData* isolate_data,
|
|
Local<Context> context,
|
|
int argc,
|
|
const char* const* argv,
|
|
int exec_argc,
|
|
const char* const* exec_argv) {
|
|
Isolate* isolate = context->GetIsolate();
|
|
HandleScope handle_scope(isolate);
|
|
Context::Scope context_scope(context);
|
|
// TODO(addaleax): This is a much better place for parsing per-Environment
|
|
// options than the global parse call.
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
std::vector<std::string> exec_args(exec_argv, exec_argv + exec_argc);
|
|
Environment* env = new Environment(isolate_data, context);
|
|
env->Start(args, exec_args, v8_is_profiling);
|
|
return env;
|
|
}
|
|
|
|
|
|
void FreeEnvironment(Environment* env) {
|
|
env->RunCleanup();
|
|
delete env;
|
|
}
|
|
|
|
|
|
Environment* GetCurrentEnvironment(Local<Context> context) {
|
|
return Environment::GetCurrent(context);
|
|
}
|
|
|
|
|
|
MultiIsolatePlatform* GetMainThreadMultiIsolatePlatform() {
|
|
return v8_platform.Platform();
|
|
}
|
|
|
|
|
|
MultiIsolatePlatform* CreatePlatform(
|
|
int thread_pool_size,
|
|
node::tracing::TracingController* tracing_controller) {
|
|
return new NodePlatform(thread_pool_size, tracing_controller);
|
|
}
|
|
|
|
|
|
MultiIsolatePlatform* InitializeV8Platform(int thread_pool_size) {
|
|
v8_platform.Initialize(thread_pool_size);
|
|
return v8_platform.Platform();
|
|
}
|
|
|
|
|
|
void FreePlatform(MultiIsolatePlatform* platform) {
|
|
delete platform;
|
|
}
|
|
|
|
Local<Context> NewContext(Isolate* isolate,
|
|
Local<ObjectTemplate> object_template) {
|
|
auto context = Context::New(isolate, nullptr, object_template);
|
|
if (context.IsEmpty()) return context;
|
|
HandleScope handle_scope(isolate);
|
|
|
|
context->SetEmbedderData(
|
|
ContextEmbedderIndex::kAllowWasmCodeGeneration, True(isolate));
|
|
|
|
{
|
|
// Run lib/internal/per_context.js
|
|
Context::Scope context_scope(context);
|
|
|
|
std::vector<Local<String>> parameters = {
|
|
FIXED_ONE_BYTE_STRING(isolate, "global")};
|
|
std::vector<Local<Value>> arguments = {context->Global()};
|
|
MaybeLocal<Value> result = per_process::native_module_loader.CompileAndCall(
|
|
context, "internal/per_context", ¶meters, &arguments, nullptr);
|
|
if (result.IsEmpty()) {
|
|
// Execution failed during context creation.
|
|
// TODO(joyeecheung): deprecate this signature and return a MaybeLocal.
|
|
return Local<Context>();
|
|
}
|
|
}
|
|
|
|
return context;
|
|
}
|
|
|
|
|
|
inline int Start(Isolate* isolate, IsolateData* isolate_data,
|
|
const std::vector<std::string>& args,
|
|
const std::vector<std::string>& exec_args) {
|
|
HandleScope handle_scope(isolate);
|
|
Local<Context> context = NewContext(isolate);
|
|
Context::Scope context_scope(context);
|
|
Environment env(isolate_data, context);
|
|
env.Start(args, exec_args, v8_is_profiling);
|
|
|
|
const char* path = args.size() > 1 ? args[1].c_str() : nullptr;
|
|
StartInspector(&env, path);
|
|
|
|
if (env.options()->debug_options().inspector_enabled &&
|
|
!v8_platform.InspectorStarted(&env)) {
|
|
return 12; // Signal internal error.
|
|
}
|
|
|
|
{
|
|
Environment::AsyncCallbackScope callback_scope(&env);
|
|
env.async_hooks()->push_async_ids(1, 0);
|
|
LoadEnvironment(&env);
|
|
env.async_hooks()->pop_async_id(1);
|
|
}
|
|
|
|
{
|
|
SealHandleScope seal(isolate);
|
|
bool more;
|
|
env.performance_state()->Mark(
|
|
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
|
|
do {
|
|
uv_run(env.event_loop(), UV_RUN_DEFAULT);
|
|
|
|
v8_platform.DrainVMTasks(isolate);
|
|
|
|
more = uv_loop_alive(env.event_loop());
|
|
if (more)
|
|
continue;
|
|
|
|
RunBeforeExit(&env);
|
|
|
|
// Emit `beforeExit` if the loop became alive either after emitting
|
|
// event, or after running some callbacks.
|
|
more = uv_loop_alive(env.event_loop());
|
|
} while (more == true);
|
|
env.performance_state()->Mark(
|
|
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
|
|
}
|
|
|
|
env.set_trace_sync_io(false);
|
|
|
|
const int exit_code = EmitExit(&env);
|
|
|
|
WaitForInspectorDisconnect(&env);
|
|
|
|
env.set_can_call_into_js(false);
|
|
env.stop_sub_worker_contexts();
|
|
uv_tty_reset_mode();
|
|
env.RunCleanup();
|
|
RunAtExit(&env);
|
|
|
|
v8_platform.DrainVMTasks(isolate);
|
|
v8_platform.CancelVMTasks(isolate);
|
|
#if defined(LEAK_SANITIZER)
|
|
__lsan_do_leak_check();
|
|
#endif
|
|
|
|
return exit_code;
|
|
}
|
|
|
|
bool AllowWasmCodeGenerationCallback(
|
|
Local<Context> context, Local<String>) {
|
|
Local<Value> wasm_code_gen =
|
|
context->GetEmbedderData(ContextEmbedderIndex::kAllowWasmCodeGeneration);
|
|
return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
|
|
}
|
|
|
|
Isolate* NewIsolate(ArrayBufferAllocator* allocator, uv_loop_t* event_loop) {
|
|
Isolate::CreateParams params;
|
|
params.array_buffer_allocator = allocator;
|
|
#ifdef NODE_ENABLE_VTUNE_PROFILING
|
|
params.code_event_handler = vTune::GetVtuneCodeEventHandler();
|
|
#endif
|
|
|
|
Isolate* isolate = Isolate::Allocate();
|
|
if (isolate == nullptr)
|
|
return nullptr;
|
|
|
|
// Register the isolate on the platform before the isolate gets initialized,
|
|
// so that the isolate can access the platform during initialization.
|
|
v8_platform.Platform()->RegisterIsolate(isolate, event_loop);
|
|
Isolate::Initialize(isolate, params);
|
|
|
|
isolate->AddMessageListenerWithErrorLevel(OnMessage,
|
|
Isolate::MessageErrorLevel::kMessageError |
|
|
Isolate::MessageErrorLevel::kMessageWarning);
|
|
isolate->SetAbortOnUncaughtExceptionCallback(ShouldAbortOnUncaughtException);
|
|
isolate->SetMicrotasksPolicy(MicrotasksPolicy::kExplicit);
|
|
isolate->SetFatalErrorHandler(OnFatalError);
|
|
isolate->SetAllowWasmCodeGenerationCallback(AllowWasmCodeGenerationCallback);
|
|
v8::CpuProfiler::UseDetailedSourcePositionsForProfiling(isolate);
|
|
|
|
return isolate;
|
|
}
|
|
|
|
inline int Start(uv_loop_t* event_loop,
|
|
const std::vector<std::string>& args,
|
|
const std::vector<std::string>& exec_args) {
|
|
std::unique_ptr<ArrayBufferAllocator, decltype(&FreeArrayBufferAllocator)>
|
|
allocator(CreateArrayBufferAllocator(), &FreeArrayBufferAllocator);
|
|
Isolate* const isolate = NewIsolate(allocator.get(), event_loop);
|
|
if (isolate == nullptr)
|
|
return 12; // Signal internal error.
|
|
|
|
{
|
|
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
|
|
CHECK_NULL(node_isolate);
|
|
node_isolate = isolate;
|
|
}
|
|
|
|
int exit_code;
|
|
{
|
|
Locker locker(isolate);
|
|
Isolate::Scope isolate_scope(isolate);
|
|
HandleScope handle_scope(isolate);
|
|
std::unique_ptr<IsolateData, decltype(&FreeIsolateData)> isolate_data(
|
|
CreateIsolateData(
|
|
isolate,
|
|
event_loop,
|
|
v8_platform.Platform(),
|
|
allocator.get()),
|
|
&FreeIsolateData);
|
|
// TODO(addaleax): This should load a real per-Isolate option, currently
|
|
// this is still effectively per-process.
|
|
if (isolate_data->options()->track_heap_objects) {
|
|
isolate->GetHeapProfiler()->StartTrackingHeapObjects(true);
|
|
}
|
|
exit_code =
|
|
Start(isolate, isolate_data.get(), args, exec_args);
|
|
}
|
|
|
|
{
|
|
Mutex::ScopedLock scoped_lock(node_isolate_mutex);
|
|
CHECK_EQ(node_isolate, isolate);
|
|
node_isolate = nullptr;
|
|
}
|
|
|
|
isolate->Dispose();
|
|
v8_platform.Platform()->UnregisterIsolate(isolate);
|
|
|
|
return exit_code;
|
|
}
|
|
|
|
int Start(int argc, char** argv) {
|
|
atexit([] () { uv_tty_reset_mode(); });
|
|
PlatformInit();
|
|
performance::performance_node_start = PERFORMANCE_NOW();
|
|
|
|
CHECK_GT(argc, 0);
|
|
|
|
#ifdef NODE_ENABLE_LARGE_CODE_PAGES
|
|
if (node::IsLargePagesEnabled()) {
|
|
if (node::MapStaticCodeToLargePages() != 0) {
|
|
fprintf(stderr, "Reverting to default page size\n");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// Hack around with the argv pointer. Used for process.title = "blah".
|
|
argv = uv_setup_args(argc, argv);
|
|
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
std::vector<std::string> exec_args;
|
|
// This needs to run *before* V8::Initialize().
|
|
Init(&args, &exec_args);
|
|
|
|
#if HAVE_OPENSSL
|
|
{
|
|
std::string extra_ca_certs;
|
|
if (credentials::SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs))
|
|
crypto::UseExtraCaCerts(extra_ca_certs);
|
|
}
|
|
#ifdef NODE_FIPS_MODE
|
|
// In the case of FIPS builds we should make sure
|
|
// the random source is properly initialized first.
|
|
OPENSSL_init();
|
|
#endif // NODE_FIPS_MODE
|
|
// V8 on Windows doesn't have a good source of entropy. Seed it from
|
|
// OpenSSL's pool.
|
|
V8::SetEntropySource(crypto::EntropySource);
|
|
#endif // HAVE_OPENSSL
|
|
|
|
InitializeV8Platform(per_process_opts->v8_thread_pool_size);
|
|
V8::Initialize();
|
|
performance::performance_v8_start = PERFORMANCE_NOW();
|
|
v8_initialized = true;
|
|
const int exit_code =
|
|
Start(uv_default_loop(), args, exec_args);
|
|
v8_platform.StopTracingAgent();
|
|
v8_initialized = false;
|
|
V8::Dispose();
|
|
|
|
// uv_run cannot be called from the time before the beforeExit callback
|
|
// runs until the program exits unless the event loop has any referenced
|
|
// handles after beforeExit terminates. This prevents unrefed timers
|
|
// that happen to terminate during shutdown from being run unsafely.
|
|
// Since uv_run cannot be called, uv_async handles held by the platform
|
|
// will never be fully cleaned up.
|
|
v8_platform.Dispose();
|
|
|
|
return exit_code;
|
|
}
|
|
|
|
} // namespace node
|
|
|
|
#if !HAVE_INSPECTOR
|
|
void Initialize() {}
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_INTERNAL(inspector, Initialize)
|
|
#endif // !HAVE_INSPECTOR
|