GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_main_instance.cc Lines: 100 101 99.0 %
Date: 2021-09-08 04:14:13 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
4844
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() {
48
  // Cannot be called more than once.
49
4844
  CHECK_NULL(registry_);
50
4844
  registry_.reset(new ExternalReferenceRegistry());
51
4844
  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
4839
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
4839
    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
4839
      owns_isolate_(true) {
78
4839
  params->array_buffer_allocator = array_buffer_allocator_.get();
79
4839
  deserialize_mode_ = per_isolate_data_indexes != nullptr;
80
4839
  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
4838
        CollectExternalReferences();
85
4838
    params->external_references = external_references.data();
86
  }
87
88
4839
  isolate_ = Isolate::Allocate();
89
4839
  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
4839
  platform->RegisterIsolate(isolate_, event_loop);
93
4839
  SetIsolateCreateParamsForNode(params);
94
4839
  Isolate::Initialize(isolate_, *params);
95
96
  // If the indexes are not nullptr, we are not deserializing
97

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


4382
NodeMainInstance::~NodeMainInstance() {
120
4364
  if (!owns_isolate_) {
121
6
    return;
122
  }
123
4358
  platform_->UnregisterIsolate(isolate_);
124
4358
  isolate_->Dispose();
125
4364
}
126
127
4839
int NodeMainInstance::Run(const EnvSerializeInfo* env_info) {
128
9197
  Locker locker(isolate_);
129
9197
  Isolate::Scope isolate_scope(isolate_);
130
9197
  HandleScope handle_scope(isolate_);
131
132
4839
  int exit_code = 0;
133
  DeleteFnPtr<Environment, FreeEnvironment> env =
134
9197
      CreateMainEnvironment(&exit_code, env_info);
135
4839
  CHECK_NOT_NULL(env);
136
137
4839
  Context::Scope context_scope(env->context());
138
4839
  Run(&exit_code, env.get());
139
4358
  return exit_code;
140
}
141
142
4839
void NodeMainInstance::Run(int* exit_code, Environment* env) {
143
4839
  if (*exit_code == 0) {
144
4839
    LoadEnvironment(env, StartExecutionCallback{});
145
146
8915
    *exit_code = SpinEventLoop(env).FromMaybe(1);
147
  }
148
149
4358
  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
4358
  memset(&act, 0, sizeof(act));
156
139456
  for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
157

135098
    if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF)
158
13074
      continue;
159
122024
    act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
160
122024
    CHECK_EQ(0, sigaction(nr, &act, nullptr));
161
  }
162
#endif
163
164
#if defined(LEAK_SANITIZER)
165
  __lsan_do_leak_check();
166
#endif
167
4358
}
168
169
DeleteFnPtr<Environment, FreeEnvironment>
170
4839
NodeMainInstance::CreateMainEnvironment(int* exit_code,
171
                                        const EnvSerializeInfo* env_info) {
172
4839
  *exit_code = 0;  // Reset the exit code to 0
173
174
9678
  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
4839
  if (isolate_data_->options()->track_heap_objects) {
179
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
180
  }
181
182

4839
  CHECK_IMPLIES(deserialize_mode_, env_info != nullptr);
183
  Local<Context> context;
184
4839
  DeleteFnPtr<Environment, FreeEnvironment> env;
185
186
4839
  if (deserialize_mode_) {
187
9676
    env.reset(new Environment(isolate_data_.get(),
188
                              isolate_,
189
4838
                              args_,
190
4838
                              exec_args_,
191
                              env_info,
192
                              EnvironmentFlags::kDefaultFlags,
193
4838
                              {}));
194
4838
    context = Context::FromSnapshot(isolate_,
195
                                    kNodeContextIndex,
196
4838
                                    {DeserializeNodeInternalFields, env.get()})
197
4838
                  .ToLocalChecked();
198
199
4838
    CHECK(!context.IsEmpty());
200
4838
    Context::Scope context_scope(context);
201
9676
    CHECK(InitializeContextRuntime(context).IsJust());
202
4838
    SetIsolateErrorHandlers(isolate_, {});
203
4838
    env->InitializeMainContext(context, env_info);
204
#if HAVE_INSPECTOR
205
4838
    env->InitializeInspector({});
206
#endif
207
4838
    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
4839
  return env;
228
}
229
230
}  // namespace node