1 |
|
|
#include "node_main_instance.h" |
2 |
|
|
#include <memory> |
3 |
|
|
#if HAVE_OPENSSL |
4 |
|
|
#include "crypto/crypto_util.h" |
5 |
|
|
#endif // HAVE_OPENSSL |
6 |
|
|
#include "debug_utils-inl.h" |
7 |
|
|
#include "node_external_reference.h" |
8 |
|
|
#include "node_internals.h" |
9 |
|
|
#include "node_options-inl.h" |
10 |
|
|
#include "node_snapshotable.h" |
11 |
|
|
#include "node_v8_platform-inl.h" |
12 |
|
|
#include "util-inl.h" |
13 |
|
|
#if defined(LEAK_SANITIZER) |
14 |
|
|
#include <sanitizer/lsan_interface.h> |
15 |
|
|
#endif |
16 |
|
|
|
17 |
|
|
#if HAVE_INSPECTOR |
18 |
|
|
#include "inspector/worker_inspector.h" // ParentInspectorHandle |
19 |
|
|
#endif |
20 |
|
|
|
21 |
|
|
namespace node { |
22 |
|
|
|
23 |
|
|
using v8::Context; |
24 |
|
|
using v8::HandleScope; |
25 |
|
|
using v8::Isolate; |
26 |
|
|
using v8::Local; |
27 |
|
|
using v8::Locker; |
28 |
|
|
|
29 |
|
|
std::unique_ptr<ExternalReferenceRegistry> NodeMainInstance::registry_ = |
30 |
|
|
nullptr; |
31 |
|
6 |
NodeMainInstance::NodeMainInstance(Isolate* isolate, |
32 |
|
|
uv_loop_t* event_loop, |
33 |
|
|
MultiIsolatePlatform* platform, |
34 |
|
|
const std::vector<std::string>& args, |
35 |
|
6 |
const std::vector<std::string>& exec_args) |
36 |
|
|
: args_(args), |
37 |
|
|
exec_args_(exec_args), |
38 |
|
|
array_buffer_allocator_(nullptr), |
39 |
|
|
isolate_(isolate), |
40 |
|
|
platform_(platform), |
41 |
|
|
isolate_data_(nullptr), |
42 |
|
|
owns_isolate_(false), |
43 |
|
6 |
deserialize_mode_(false) { |
44 |
|
|
isolate_data_ = |
45 |
|
6 |
std::make_unique<IsolateData>(isolate_, event_loop, platform, nullptr); |
46 |
|
|
|
47 |
|
6 |
SetIsolateMiscHandlers(isolate_, {}); |
48 |
|
6 |
} |
49 |
|
|
|
50 |
|
4961 |
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() { |
51 |
|
|
// Cannot be called more than once. |
52 |
✗✓ |
4961 |
CHECK_NULL(registry_); |
53 |
|
4961 |
registry_.reset(new ExternalReferenceRegistry()); |
54 |
|
4961 |
return registry_->external_references(); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
6 |
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create( |
58 |
|
|
Isolate* isolate, |
59 |
|
|
uv_loop_t* event_loop, |
60 |
|
|
MultiIsolatePlatform* platform, |
61 |
|
|
const std::vector<std::string>& args, |
62 |
|
|
const std::vector<std::string>& exec_args) { |
63 |
|
|
return std::unique_ptr<NodeMainInstance>( |
64 |
|
6 |
new NodeMainInstance(isolate, event_loop, platform, args, exec_args)); |
65 |
|
|
} |
66 |
|
|
|
67 |
|
4956 |
NodeMainInstance::NodeMainInstance( |
68 |
|
|
Isolate::CreateParams* params, |
69 |
|
|
uv_loop_t* event_loop, |
70 |
|
|
MultiIsolatePlatform* platform, |
71 |
|
|
const std::vector<std::string>& args, |
72 |
|
|
const std::vector<std::string>& exec_args, |
73 |
|
4956 |
const std::vector<size_t>* per_isolate_data_indexes) |
74 |
|
|
: args_(args), |
75 |
|
|
exec_args_(exec_args), |
76 |
|
|
array_buffer_allocator_(ArrayBufferAllocator::Create()), |
77 |
|
|
isolate_(nullptr), |
78 |
|
|
platform_(platform), |
79 |
|
|
isolate_data_(nullptr), |
80 |
|
4956 |
owns_isolate_(true) { |
81 |
|
4956 |
params->array_buffer_allocator = array_buffer_allocator_.get(); |
82 |
|
4956 |
deserialize_mode_ = per_isolate_data_indexes != nullptr; |
83 |
✓✓ |
4956 |
if (deserialize_mode_) { |
84 |
|
|
// TODO(joyeecheung): collect external references and set it in |
85 |
|
|
// params.external_references. |
86 |
|
|
const std::vector<intptr_t>& external_references = |
87 |
|
4955 |
CollectExternalReferences(); |
88 |
|
4955 |
params->external_references = external_references.data(); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
4956 |
isolate_ = Isolate::Allocate(); |
92 |
✗✓ |
4956 |
CHECK_NOT_NULL(isolate_); |
93 |
|
|
// Register the isolate on the platform before the isolate gets initialized, |
94 |
|
|
// so that the isolate can access the platform during initialization. |
95 |
|
4956 |
platform->RegisterIsolate(isolate_, event_loop); |
96 |
|
4956 |
SetIsolateCreateParamsForNode(params); |
97 |
|
4956 |
Isolate::Initialize(isolate_, *params); |
98 |
|
|
|
99 |
|
|
// If the indexes are not nullptr, we are not deserializing |
100 |
✓✓✗✓
|
4956 |
CHECK_IMPLIES(deserialize_mode_, params->external_references != nullptr); |
101 |
|
4956 |
isolate_data_ = std::make_unique<IsolateData>(isolate_, |
102 |
|
|
event_loop, |
103 |
|
|
platform, |
104 |
|
4956 |
array_buffer_allocator_.get(), |
105 |
|
4956 |
per_isolate_data_indexes); |
106 |
|
4956 |
IsolateSettings s; |
107 |
|
4956 |
SetIsolateMiscHandlers(isolate_, s); |
108 |
✓✓ |
4956 |
if (!deserialize_mode_) { |
109 |
|
|
// If in deserialize mode, delay until after the deserialization is |
110 |
|
|
// complete. |
111 |
|
1 |
SetIsolateErrorHandlers(isolate_, s); |
112 |
|
|
} |
113 |
|
4956 |
isolate_data_->max_young_gen_size = |
114 |
|
4956 |
params->constraints.max_young_generation_size_in_bytes(); |
115 |
|
4956 |
} |
116 |
|
|
|
117 |
|
6 |
void NodeMainInstance::Dispose() { |
118 |
✗✓ |
6 |
CHECK(!owns_isolate_); |
119 |
|
6 |
platform_->DrainTasks(isolate_); |
120 |
|
6 |
} |
121 |
|
|
|
122 |
✓✓✓✓ ✓✓✓✓
|
4492 |
NodeMainInstance::~NodeMainInstance() { |
123 |
✓✓ |
4474 |
if (!owns_isolate_) { |
124 |
|
6 |
return; |
125 |
|
|
} |
126 |
|
4468 |
platform_->UnregisterIsolate(isolate_); |
127 |
|
4468 |
isolate_->Dispose(); |
128 |
|
4474 |
} |
129 |
|
|
|
130 |
|
4956 |
int NodeMainInstance::Run(const EnvSerializeInfo* env_info) { |
131 |
|
9424 |
Locker locker(isolate_); |
132 |
|
9424 |
Isolate::Scope isolate_scope(isolate_); |
133 |
|
9424 |
HandleScope handle_scope(isolate_); |
134 |
|
|
|
135 |
|
4956 |
int exit_code = 0; |
136 |
|
|
DeleteFnPtr<Environment, FreeEnvironment> env = |
137 |
|
9424 |
CreateMainEnvironment(&exit_code, env_info); |
138 |
✗✓ |
4956 |
CHECK_NOT_NULL(env); |
139 |
|
|
|
140 |
|
4956 |
Context::Scope context_scope(env->context()); |
141 |
|
4956 |
Run(&exit_code, env.get()); |
142 |
|
4468 |
return exit_code; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
4956 |
void NodeMainInstance::Run(int* exit_code, Environment* env) { |
146 |
✓✗ |
4956 |
if (*exit_code == 0) { |
147 |
|
4956 |
LoadEnvironment(env, StartExecutionCallback{}); |
148 |
|
|
|
149 |
✓✗ |
9138 |
*exit_code = SpinEventLoop(env).FromMaybe(1); |
150 |
|
|
} |
151 |
|
|
|
152 |
|
4468 |
ResetStdio(); |
153 |
|
|
|
154 |
|
|
// TODO(addaleax): Neither NODE_SHARED_MODE nor HAVE_INSPECTOR really |
155 |
|
|
// make sense here. |
156 |
|
|
#if HAVE_INSPECTOR && defined(__POSIX__) && !defined(NODE_SHARED_MODE) |
157 |
|
|
struct sigaction act; |
158 |
|
4468 |
memset(&act, 0, sizeof(act)); |
159 |
✓✓ |
142976 |
for (unsigned nr = 1; nr < kMaxSignal; nr += 1) { |
160 |
✓✓✓✓ ✓✓ |
138508 |
if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF) |
161 |
|
13404 |
continue; |
162 |
✓✓ |
125104 |
act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL; |
163 |
✗✓ |
125104 |
CHECK_EQ(0, sigaction(nr, &act, nullptr)); |
164 |
|
|
} |
165 |
|
|
#endif |
166 |
|
|
|
167 |
|
|
#if defined(LEAK_SANITIZER) |
168 |
|
|
__lsan_do_leak_check(); |
169 |
|
|
#endif |
170 |
|
4468 |
} |
171 |
|
|
|
172 |
|
|
DeleteFnPtr<Environment, FreeEnvironment> |
173 |
|
4956 |
NodeMainInstance::CreateMainEnvironment(int* exit_code, |
174 |
|
|
const EnvSerializeInfo* env_info) { |
175 |
|
4956 |
*exit_code = 0; // Reset the exit code to 0 |
176 |
|
|
|
177 |
|
9912 |
HandleScope handle_scope(isolate_); |
178 |
|
|
|
179 |
|
|
// TODO(addaleax): This should load a real per-Isolate option, currently |
180 |
|
|
// this is still effectively per-process. |
181 |
✓✓ |
4956 |
if (isolate_data_->options()->track_heap_objects) { |
182 |
|
1 |
isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true); |
183 |
|
|
} |
184 |
|
|
|
185 |
✓✓✗✓
|
4956 |
CHECK_IMPLIES(deserialize_mode_, env_info != nullptr); |
186 |
|
|
Local<Context> context; |
187 |
|
4956 |
DeleteFnPtr<Environment, FreeEnvironment> env; |
188 |
|
|
|
189 |
✓✓ |
4956 |
if (deserialize_mode_) { |
190 |
|
9910 |
env.reset(new Environment(isolate_data_.get(), |
191 |
|
|
isolate_, |
192 |
|
4955 |
args_, |
193 |
|
4955 |
exec_args_, |
194 |
|
|
env_info, |
195 |
|
|
EnvironmentFlags::kDefaultFlags, |
196 |
|
4955 |
{})); |
197 |
|
4955 |
context = Context::FromSnapshot(isolate_, |
198 |
|
|
kNodeContextIndex, |
199 |
|
4955 |
{DeserializeNodeInternalFields, env.get()}) |
200 |
|
4955 |
.ToLocalChecked(); |
201 |
|
|
|
202 |
✗✓ |
4955 |
CHECK(!context.IsEmpty()); |
203 |
|
4955 |
Context::Scope context_scope(context); |
204 |
✗✓ |
9910 |
CHECK(InitializeContextRuntime(context).IsJust()); |
205 |
|
4955 |
SetIsolateErrorHandlers(isolate_, {}); |
206 |
|
4955 |
env->InitializeMainContext(context, env_info); |
207 |
|
|
#if HAVE_INSPECTOR |
208 |
|
4955 |
env->InitializeInspector({}); |
209 |
|
|
#endif |
210 |
|
4955 |
env->DoneBootstrapping(); |
211 |
|
|
|
212 |
|
|
#if HAVE_OPENSSL |
213 |
|
4955 |
crypto::InitCryptoOnce(isolate_); |
214 |
|
|
#endif // HAVE_OPENSSL |
215 |
|
|
} else { |
216 |
|
1 |
context = NewContext(isolate_); |
217 |
✗✓ |
1 |
CHECK(!context.IsEmpty()); |
218 |
|
1 |
Context::Scope context_scope(context); |
219 |
|
2 |
env.reset(new Environment(isolate_data_.get(), |
220 |
|
|
context, |
221 |
|
1 |
args_, |
222 |
|
1 |
exec_args_, |
223 |
|
|
nullptr, |
224 |
|
|
EnvironmentFlags::kDefaultFlags, |
225 |
|
1 |
{})); |
226 |
|
|
#if HAVE_INSPECTOR |
227 |
|
1 |
env->InitializeInspector({}); |
228 |
|
|
#endif |
229 |
✗✓ |
2 |
if (env->RunBootstrapping().IsEmpty()) { |
230 |
|
|
return nullptr; |
231 |
|
|
} |
232 |
|
|
} |
233 |
|
|
|
234 |
|
4956 |
return env; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
} // namespace node |