GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_main_instance.cc Lines: 95 96 99.0 %
Date: 2022-07-25 04:16:17 Branches: 42 52 80.8 %

Line Branch Exec Source
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_native_module.h"
10
#include "node_options-inl.h"
11
#include "node_snapshot_builder.h"
12
#include "node_snapshotable.h"
13
#include "node_v8_platform-inl.h"
14
#include "util-inl.h"
15
#if defined(LEAK_SANITIZER)
16
#include <sanitizer/lsan_interface.h>
17
#endif
18
19
#if HAVE_INSPECTOR
20
#include "inspector/worker_inspector.h"  // ParentInspectorHandle
21
#endif
22
23
namespace node {
24
25
using v8::Context;
26
using v8::HandleScope;
27
using v8::Isolate;
28
using v8::Local;
29
using v8::Locker;
30
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
6
      snapshot_data_(nullptr) {
43
  isolate_data_ =
44
6
      std::make_unique<IsolateData>(isolate_, event_loop, platform, nullptr);
45
46
6
  SetIsolateMiscHandlers(isolate_, {});
47
6
}
48
49
6
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
50
    Isolate* isolate,
51
    uv_loop_t* event_loop,
52
    MultiIsolatePlatform* platform,
53
    const std::vector<std::string>& args,
54
    const std::vector<std::string>& exec_args) {
55
  return std::unique_ptr<NodeMainInstance>(
56
6
      new NodeMainInstance(isolate, event_loop, platform, args, exec_args));
57
}
58
59
5280
NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
60
                                   uv_loop_t* event_loop,
61
                                   MultiIsolatePlatform* platform,
62
                                   const std::vector<std::string>& args,
63
5280
                                   const std::vector<std::string>& exec_args)
64
    : args_(args),
65
      exec_args_(exec_args),
66
      array_buffer_allocator_(ArrayBufferAllocator::Create()),
67
      isolate_(nullptr),
68
      platform_(platform),
69
      isolate_data_(),
70
      isolate_params_(std::make_unique<Isolate::CreateParams>()),
71
5280
      snapshot_data_(snapshot_data) {
72
5280
  isolate_params_->array_buffer_allocator = array_buffer_allocator_.get();
73
5280
  if (snapshot_data != nullptr) {
74
5278
    SnapshotBuilder::InitializeIsolateParams(snapshot_data,
75
                                             isolate_params_.get());
76
  }
77
78
5280
  isolate_ = Isolate::Allocate();
79
5280
  CHECK_NOT_NULL(isolate_);
80
  // Register the isolate on the platform before the isolate gets initialized,
81
  // so that the isolate can access the platform during initialization.
82
5280
  platform->RegisterIsolate(isolate_, event_loop);
83
5280
  SetIsolateCreateParamsForNode(isolate_params_.get());
84
5280
  Isolate::Initialize(isolate_, *isolate_params_);
85
86
  // If the indexes are not nullptr, we are not deserializing
87
5280
  isolate_data_ = std::make_unique<IsolateData>(
88
5280
      isolate_,
89
      event_loop,
90
      platform,
91
5280
      array_buffer_allocator_.get(),
92
10560
      snapshot_data == nullptr ? nullptr : &(snapshot_data->isolate_data_info));
93
5280
  IsolateSettings s;
94
5280
  SetIsolateMiscHandlers(isolate_, s);
95
5280
  if (snapshot_data == nullptr) {
96
    // If in deserialize mode, delay until after the deserialization is
97
    // complete.
98
2
    SetIsolateErrorHandlers(isolate_, s);
99
  }
100
5280
  isolate_data_->max_young_gen_size =
101
5280
      isolate_params_->constraints.max_young_generation_size_in_bytes();
102
5280
}
103
104
6
void NodeMainInstance::Dispose() {
105
  // This should only be called on a main instance that does not own its
106
  // isolate.
107
6
  CHECK_NULL(isolate_params_);
108
6
  platform_->DrainTasks(isolate_);
109
6
}
110
111


4701
NodeMainInstance::~NodeMainInstance() {
112
4677
  if (isolate_params_ == nullptr) {
113
6
    return;
114
  }
115
  // This should only be done on a main instance that owns its isolate.
116
4671
  platform_->UnregisterIsolate(isolate_);
117
4671
  isolate_->Dispose();
118
4677
}
119
120
5280
int NodeMainInstance::Run() {
121
9951
  Locker locker(isolate_);
122
9951
  Isolate::Scope isolate_scope(isolate_);
123
9951
  HandleScope handle_scope(isolate_);
124
125
5280
  int exit_code = 0;
126
  DeleteFnPtr<Environment, FreeEnvironment> env =
127
9951
      CreateMainEnvironment(&exit_code);
128
5280
  CHECK_NOT_NULL(env);
129
130
5280
  Context::Scope context_scope(env->context());
131
5280
  Run(&exit_code, env.get());
132
4671
  return exit_code;
133
}
134
135
5280
void NodeMainInstance::Run(int* exit_code, Environment* env) {
136
5280
  if (*exit_code == 0) {
137
5280
    LoadEnvironment(env, StartExecutionCallback{});
138
139
9659
    *exit_code = SpinEventLoop(env).FromMaybe(1);
140
  }
141
142
4671
  ResetStdio();
143
144
  // TODO(addaleax): Neither NODE_SHARED_MODE nor HAVE_INSPECTOR really
145
  // make sense here.
146
#if HAVE_INSPECTOR && defined(__POSIX__) && !defined(NODE_SHARED_MODE)
147
  struct sigaction act;
148
4671
  memset(&act, 0, sizeof(act));
149
149472
  for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
150

144801
    if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF)
151
14013
      continue;
152
130788
    act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
153
130788
    CHECK_EQ(0, sigaction(nr, &act, nullptr));
154
  }
155
#endif
156
157
#if defined(LEAK_SANITIZER)
158
  __lsan_do_leak_check();
159
#endif
160
4671
}
161
162
DeleteFnPtr<Environment, FreeEnvironment>
163
5280
NodeMainInstance::CreateMainEnvironment(int* exit_code) {
164
5280
  *exit_code = 0;  // Reset the exit code to 0
165
166
10560
  HandleScope handle_scope(isolate_);
167
168
  // TODO(addaleax): This should load a real per-Isolate option, currently
169
  // this is still effectively per-process.
170
5280
  if (isolate_data_->options()->track_heap_objects) {
171
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
172
  }
173
174
  Local<Context> context;
175
5280
  DeleteFnPtr<Environment, FreeEnvironment> env;
176
177
5280
  if (snapshot_data_ != nullptr) {
178
10556
    env.reset(new Environment(isolate_data_.get(),
179
                              isolate_,
180
5278
                              args_,
181
5278
                              exec_args_,
182
5278
                              &(snapshot_data_->env_info),
183
                              EnvironmentFlags::kDefaultFlags,
184
5278
                              {}));
185
5278
    context = Context::FromSnapshot(isolate_,
186
                                    SnapshotData::kNodeMainContextIndex,
187
5278
                                    {DeserializeNodeInternalFields, env.get()})
188
5278
                  .ToLocalChecked();
189
190
5278
    CHECK(!context.IsEmpty());
191
5278
    Context::Scope context_scope(context);
192
193
10556
    CHECK(InitializeContextRuntime(context).IsJust());
194
5278
    SetIsolateErrorHandlers(isolate_, {});
195
5278
    env->InitializeMainContext(context, &(snapshot_data_->env_info));
196
#if HAVE_INSPECTOR
197
5278
    env->InitializeInspector({});
198
#endif
199
5278
    env->DoneBootstrapping();
200
201
#if HAVE_OPENSSL
202
5278
    crypto::InitCryptoOnce(isolate_);
203
#endif  // HAVE_OPENSSL
204
  } else {
205
2
    context = NewContext(isolate_);
206
2
    CHECK(!context.IsEmpty());
207
2
    Context::Scope context_scope(context);
208
4
    env.reset(new Environment(isolate_data_.get(),
209
                              context,
210
2
                              args_,
211
2
                              exec_args_,
212
                              nullptr,
213
                              EnvironmentFlags::kDefaultFlags,
214
2
                              {}));
215
#if HAVE_INSPECTOR
216
2
    env->InitializeInspector({});
217
#endif
218
4
    if (env->RunBootstrapping().IsEmpty()) {
219
      return nullptr;
220
    }
221
  }
222
223
5280
  return env;
224
}
225
226
}  // namespace node