GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_main_instance.cc Lines: 100 101 99.0 %
Date: 2021-12-25 04:14:02 Branches: 45 58 77.6 %

Line Branch Exec Source
1
#include "node_main_instance.h"
2
#include <memory>
3
#include "debug_utils-inl.h"
4
#include "node_external_reference.h"
5
#include "node_internals.h"
6
#include "node_options-inl.h"
7
#include "node_snapshotable.h"
8
#include "node_v8_platform-inl.h"
9
#include "util-inl.h"
10
#if defined(LEAK_SANITIZER)
11
#include <sanitizer/lsan_interface.h>
12
#endif
13
14
#if HAVE_INSPECTOR
15
#include "inspector/worker_inspector.h"  // ParentInspectorHandle
16
#endif
17
18
namespace node {
19
20
using v8::Context;
21
using v8::HandleScope;
22
using v8::Isolate;
23
using v8::Local;
24
using v8::Locker;
25
26
std::unique_ptr<ExternalReferenceRegistry> NodeMainInstance::registry_ =
27
    nullptr;
28
6
NodeMainInstance::NodeMainInstance(Isolate* isolate,
29
                                   uv_loop_t* event_loop,
30
                                   MultiIsolatePlatform* platform,
31
                                   const std::vector<std::string>& args,
32
6
                                   const std::vector<std::string>& exec_args)
33
    : args_(args),
34
      exec_args_(exec_args),
35
      array_buffer_allocator_(nullptr),
36
      isolate_(isolate),
37
      platform_(platform),
38
      isolate_data_(nullptr),
39
      owns_isolate_(false),
40
6
      deserialize_mode_(false) {
41
  isolate_data_ =
42
6
      std::make_unique<IsolateData>(isolate_, event_loop, platform, nullptr);
43
44
6
  SetIsolateMiscHandlers(isolate_, {});
45
6
}
46
47
4900
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() {
48
  // Cannot be called more than once.
49
4900
  CHECK_NULL(registry_);
50
4900
  registry_.reset(new ExternalReferenceRegistry());
51
4900
  return registry_->external_references();
52
}
53
54
6
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
55
    Isolate* isolate,
56
    uv_loop_t* event_loop,
57
    MultiIsolatePlatform* platform,
58
    const std::vector<std::string>& args,
59
    const std::vector<std::string>& exec_args) {
60
  return std::unique_ptr<NodeMainInstance>(
61
6
      new NodeMainInstance(isolate, event_loop, platform, args, exec_args));
62
}
63
64
4895
NodeMainInstance::NodeMainInstance(
65
    Isolate::CreateParams* params,
66
    uv_loop_t* event_loop,
67
    MultiIsolatePlatform* platform,
68
    const std::vector<std::string>& args,
69
    const std::vector<std::string>& exec_args,
70
4895
    const std::vector<size_t>* per_isolate_data_indexes)
71
    : args_(args),
72
      exec_args_(exec_args),
73
      array_buffer_allocator_(ArrayBufferAllocator::Create()),
74
      isolate_(nullptr),
75
      platform_(platform),
76
      isolate_data_(nullptr),
77
4895
      owns_isolate_(true) {
78
4895
  params->array_buffer_allocator = array_buffer_allocator_.get();
79
4895
  deserialize_mode_ = per_isolate_data_indexes != nullptr;
80
4895
  if (deserialize_mode_) {
81
    // TODO(joyeecheung): collect external references and set it in
82
    // params.external_references.
83
    const std::vector<intptr_t>& external_references =
84
4894
        CollectExternalReferences();
85
4894
    params->external_references = external_references.data();
86
  }
87
88
4895
  isolate_ = Isolate::Allocate();
89
4895
  CHECK_NOT_NULL(isolate_);
90
  // Register the isolate on the platform before the isolate gets initialized,
91
  // so that the isolate can access the platform during initialization.
92
4895
  platform->RegisterIsolate(isolate_, event_loop);
93
4895
  SetIsolateCreateParamsForNode(params);
94
4895
  Isolate::Initialize(isolate_, *params);
95
96
  // If the indexes are not nullptr, we are not deserializing
97

4895
  CHECK_IMPLIES(deserialize_mode_, params->external_references != nullptr);
98
4895
  isolate_data_ = std::make_unique<IsolateData>(isolate_,
99
                                                event_loop,
100
                                                platform,
101
4895
                                                array_buffer_allocator_.get(),
102
4895
                                                per_isolate_data_indexes);
103
4895
  IsolateSettings s;
104
4895
  SetIsolateMiscHandlers(isolate_, s);
105
4895
  if (!deserialize_mode_) {
106
    // If in deserialize mode, delay until after the deserialization is
107
    // complete.
108
1
    SetIsolateErrorHandlers(isolate_, s);
109
  }
110
4895
  isolate_data_->max_young_gen_size =
111
4895
      params->constraints.max_young_generation_size_in_bytes();
112
4895
}
113
114
6
void NodeMainInstance::Dispose() {
115
6
  CHECK(!owns_isolate_);
116
6
  platform_->DrainTasks(isolate_);
117
6
}
118
119


4433
NodeMainInstance::~NodeMainInstance() {
120
4415
  if (!owns_isolate_) {
121
6
    return;
122
  }
123
4409
  platform_->UnregisterIsolate(isolate_);
124
4409
  isolate_->Dispose();
125
4415
}
126
127
4895
int NodeMainInstance::Run(const EnvSerializeInfo* env_info) {
128
9304
  Locker locker(isolate_);
129
9304
  Isolate::Scope isolate_scope(isolate_);
130
9304
  HandleScope handle_scope(isolate_);
131
132
4895
  int exit_code = 0;
133
  DeleteFnPtr<Environment, FreeEnvironment> env =
134
9304
      CreateMainEnvironment(&exit_code, env_info);
135
4895
  CHECK_NOT_NULL(env);
136
137
4895
  Context::Scope context_scope(env->context());
138
4895
  Run(&exit_code, env.get());
139
4409
  return exit_code;
140
}
141
142
4895
void NodeMainInstance::Run(int* exit_code, Environment* env) {
143
4895
  if (*exit_code == 0) {
144
4895
    LoadEnvironment(env, StartExecutionCallback{});
145
146
9021
    *exit_code = SpinEventLoop(env).FromMaybe(1);
147
  }
148
149
4409
  ResetStdio();
150
151
  // TODO(addaleax): Neither NODE_SHARED_MODE nor HAVE_INSPECTOR really
152
  // make sense here.
153
#if HAVE_INSPECTOR && defined(__POSIX__) && !defined(NODE_SHARED_MODE)
154
  struct sigaction act;
155
4409
  memset(&act, 0, sizeof(act));
156
141088
  for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
157

136679
    if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF)
158
13227
      continue;
159
123452
    act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
160
123452
    CHECK_EQ(0, sigaction(nr, &act, nullptr));
161
  }
162
#endif
163
164
#if defined(LEAK_SANITIZER)
165
  __lsan_do_leak_check();
166
#endif
167
4409
}
168
169
DeleteFnPtr<Environment, FreeEnvironment>
170
4895
NodeMainInstance::CreateMainEnvironment(int* exit_code,
171
                                        const EnvSerializeInfo* env_info) {
172
4895
  *exit_code = 0;  // Reset the exit code to 0
173
174
9790
  HandleScope handle_scope(isolate_);
175
176
  // TODO(addaleax): This should load a real per-Isolate option, currently
177
  // this is still effectively per-process.
178
4895
  if (isolate_data_->options()->track_heap_objects) {
179
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
180
  }
181
182

4895
  CHECK_IMPLIES(deserialize_mode_, env_info != nullptr);
183
  Local<Context> context;
184
4895
  DeleteFnPtr<Environment, FreeEnvironment> env;
185
186
4895
  if (deserialize_mode_) {
187
9788
    env.reset(new Environment(isolate_data_.get(),
188
                              isolate_,
189
4894
                              args_,
190
4894
                              exec_args_,
191
                              env_info,
192
                              EnvironmentFlags::kDefaultFlags,
193
4894
                              {}));
194
4894
    context = Context::FromSnapshot(isolate_,
195
                                    kNodeContextIndex,
196
4894
                                    {DeserializeNodeInternalFields, env.get()})
197
4894
                  .ToLocalChecked();
198
199
4894
    CHECK(!context.IsEmpty());
200
4894
    Context::Scope context_scope(context);
201
9788
    CHECK(InitializeContextRuntime(context).IsJust());
202
4894
    SetIsolateErrorHandlers(isolate_, {});
203
4894
    env->InitializeMainContext(context, env_info);
204
#if HAVE_INSPECTOR
205
4894
    env->InitializeInspector({});
206
#endif
207
4894
    env->DoneBootstrapping();
208
  } else {
209
1
    context = NewContext(isolate_);
210
1
    CHECK(!context.IsEmpty());
211
1
    Context::Scope context_scope(context);
212
2
    env.reset(new Environment(isolate_data_.get(),
213
                              context,
214
1
                              args_,
215
1
                              exec_args_,
216
                              nullptr,
217
                              EnvironmentFlags::kDefaultFlags,
218
1
                              {}));
219
#if HAVE_INSPECTOR
220
1
    env->InitializeInspector({});
221
#endif
222
2
    if (env->RunBootstrapping().IsEmpty()) {
223
      return nullptr;
224
    }
225
  }
226
227
4895
  return env;
228
}
229
230
}  // namespace node