GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_main_instance.cc Lines: 88 89 98.9 %
Date: 2022-08-14 04:19:53 Branches: 31 40 77.5 %

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_builtins.h"
8
#include "node_external_reference.h"
9
#include "node_internals.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
7
NodeMainInstance::NodeMainInstance(Isolate* isolate,
32
                                   uv_loop_t* event_loop,
33
                                   MultiIsolatePlatform* platform,
34
                                   const std::vector<std::string>& args,
35
7
                                   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
7
      snapshot_data_(nullptr) {
43
  isolate_data_ =
44
7
      std::make_unique<IsolateData>(isolate_, event_loop, platform, nullptr);
45
46
7
  SetIsolateMiscHandlers(isolate_, {});
47
7
}
48
49
7
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
7
      new NodeMainInstance(isolate, event_loop, platform, args, exec_args));
57
}
58
59
5254
NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
60
                                   uv_loop_t* event_loop,
61
                                   MultiIsolatePlatform* platform,
62
                                   const std::vector<std::string>& args,
63
5254
                                   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
5254
      snapshot_data_(snapshot_data) {
72
5254
  isolate_params_->array_buffer_allocator = array_buffer_allocator_.get();
73
5254
  if (snapshot_data != nullptr) {
74
5252
    SnapshotBuilder::InitializeIsolateParams(snapshot_data,
75
                                             isolate_params_.get());
76
  }
77
78
5254
  isolate_ = Isolate::Allocate();
79
5254
  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
5254
  platform->RegisterIsolate(isolate_, event_loop);
83
5254
  SetIsolateCreateParamsForNode(isolate_params_.get());
84
5254
  Isolate::Initialize(isolate_, *isolate_params_);
85
86
  // If the indexes are not nullptr, we are not deserializing
87
5254
  isolate_data_ = std::make_unique<IsolateData>(
88
5254
      isolate_,
89
      event_loop,
90
      platform,
91
5254
      array_buffer_allocator_.get(),
92
10508
      snapshot_data == nullptr ? nullptr : &(snapshot_data->isolate_data_info));
93
5254
  IsolateSettings s;
94
5254
  SetIsolateMiscHandlers(isolate_, s);
95
5254
  if (snapshot_data == nullptr) {
96
    // If in deserialize mode, delay until after the deserialization is
97
    // complete.
98
2
    SetIsolateErrorHandlers(isolate_, s);
99
  }
100
5254
  isolate_data_->max_young_gen_size =
101
5254
      isolate_params_->constraints.max_young_generation_size_in_bytes();
102
5254
}
103
104
7
void NodeMainInstance::Dispose() {
105
  // This should only be called on a main instance that does not own its
106
  // isolate.
107
7
  CHECK_NULL(isolate_params_);
108
7
  platform_->DrainTasks(isolate_);
109
7
}
110
111


4643
NodeMainInstance::~NodeMainInstance() {
112
4615
  if (isolate_params_ == nullptr) {
113
7
    return;
114
  }
115
  // This should only be done on a main instance that owns its isolate.
116
4608
  platform_->UnregisterIsolate(isolate_);
117
4608
  isolate_->Dispose();
118
4615
}
119
120
5254
int NodeMainInstance::Run() {
121
9862
  Locker locker(isolate_);
122
9862
  Isolate::Scope isolate_scope(isolate_);
123
9862
  HandleScope handle_scope(isolate_);
124
125
5254
  int exit_code = 0;
126
  DeleteFnPtr<Environment, FreeEnvironment> env =
127
9862
      CreateMainEnvironment(&exit_code);
128
5254
  CHECK_NOT_NULL(env);
129
130
5254
  Context::Scope context_scope(env->context());
131
5254
  Run(&exit_code, env.get());
132
4608
  return exit_code;
133
}
134
135
5254
void NodeMainInstance::Run(int* exit_code, Environment* env) {
136
5254
  if (*exit_code == 0) {
137
5254
    LoadEnvironment(env, StartExecutionCallback{});
138
139
9553
    *exit_code = SpinEventLoop(env).FromMaybe(1);
140
  }
141
142
#if defined(LEAK_SANITIZER)
143
  __lsan_do_leak_check();
144
#endif
145
4608
}
146
147
DeleteFnPtr<Environment, FreeEnvironment>
148
5254
NodeMainInstance::CreateMainEnvironment(int* exit_code) {
149
5254
  *exit_code = 0;  // Reset the exit code to 0
150
151
10508
  HandleScope handle_scope(isolate_);
152
153
  // TODO(addaleax): This should load a real per-Isolate option, currently
154
  // this is still effectively per-process.
155
5254
  if (isolate_data_->options()->track_heap_objects) {
156
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
157
  }
158
159
  Local<Context> context;
160
5254
  DeleteFnPtr<Environment, FreeEnvironment> env;
161
162
5254
  if (snapshot_data_ != nullptr) {
163
10504
    env.reset(new Environment(isolate_data_.get(),
164
                              isolate_,
165
5252
                              args_,
166
5252
                              exec_args_,
167
5252
                              &(snapshot_data_->env_info),
168
                              EnvironmentFlags::kDefaultFlags,
169
5252
                              {}));
170
5252
    context = Context::FromSnapshot(isolate_,
171
                                    SnapshotData::kNodeMainContextIndex,
172
5252
                                    {DeserializeNodeInternalFields, env.get()})
173
5252
                  .ToLocalChecked();
174
175
5252
    CHECK(!context.IsEmpty());
176
5252
    Context::Scope context_scope(context);
177
178
10504
    CHECK(InitializeContextRuntime(context).IsJust());
179
5252
    SetIsolateErrorHandlers(isolate_, {});
180
5252
    env->InitializeMainContext(context, &(snapshot_data_->env_info));
181
#if HAVE_INSPECTOR
182
5252
    env->InitializeInspector({});
183
#endif
184
5252
    env->DoneBootstrapping();
185
186
#if HAVE_OPENSSL
187
5252
    crypto::InitCryptoOnce(isolate_);
188
#endif  // HAVE_OPENSSL
189
  } else {
190
2
    context = NewContext(isolate_);
191
2
    CHECK(!context.IsEmpty());
192
2
    Context::Scope context_scope(context);
193
4
    env.reset(new Environment(isolate_data_.get(),
194
                              context,
195
2
                              args_,
196
2
                              exec_args_,
197
                              nullptr,
198
                              EnvironmentFlags::kDefaultFlags,
199
2
                              {}));
200
#if HAVE_INSPECTOR
201
2
    env->InitializeInspector({});
202
#endif
203
4
    if (env->RunBootstrapping().IsEmpty()) {
204
      return nullptr;
205
    }
206
  }
207
208
5254
  return env;
209
}
210
211
}  // namespace node