GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_main_instance.cc Lines: 88 89 98.9 %
Date: 2022-08-31 04:20:31 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
5417
NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
60
                                   uv_loop_t* event_loop,
61
                                   MultiIsolatePlatform* platform,
62
                                   const std::vector<std::string>& args,
63
5417
                                   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
5417
      snapshot_data_(snapshot_data) {
72
5417
  isolate_params_->array_buffer_allocator = array_buffer_allocator_.get();
73
5417
  if (snapshot_data != nullptr) {
74
5415
    SnapshotBuilder::InitializeIsolateParams(snapshot_data,
75
                                             isolate_params_.get());
76
  }
77
78
5417
  isolate_ = Isolate::Allocate();
79
5417
  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
5417
  platform->RegisterIsolate(isolate_, event_loop);
83
5417
  SetIsolateCreateParamsForNode(isolate_params_.get());
84
5417
  Isolate::Initialize(isolate_, *isolate_params_);
85
86
  // If the indexes are not nullptr, we are not deserializing
87
5417
  isolate_data_ = std::make_unique<IsolateData>(
88
5417
      isolate_,
89
      event_loop,
90
      platform,
91
5417
      array_buffer_allocator_.get(),
92
10834
      snapshot_data == nullptr ? nullptr : &(snapshot_data->isolate_data_info));
93
5417
  IsolateSettings s;
94
5417
  SetIsolateMiscHandlers(isolate_, s);
95
5417
  if (snapshot_data == nullptr) {
96
    // If in deserialize mode, delay until after the deserialization is
97
    // complete.
98
2
    SetIsolateErrorHandlers(isolate_, s);
99
  }
100
5417
  isolate_data_->max_young_gen_size =
101
5417
      isolate_params_->constraints.max_young_generation_size_in_bytes();
102
5417
}
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


4900
NodeMainInstance::~NodeMainInstance() {
112
4872
  if (isolate_params_ == nullptr) {
113
7
    return;
114
  }
115
  // This should only be done on a main instance that owns its isolate.
116
4865
  platform_->UnregisterIsolate(isolate_);
117
4865
  isolate_->Dispose();
118
4872
}
119
120
5417
int NodeMainInstance::Run() {
121
10282
  Locker locker(isolate_);
122
10282
  Isolate::Scope isolate_scope(isolate_);
123
10282
  HandleScope handle_scope(isolate_);
124
125
5417
  int exit_code = 0;
126
  DeleteFnPtr<Environment, FreeEnvironment> env =
127
10282
      CreateMainEnvironment(&exit_code);
128
5417
  CHECK_NOT_NULL(env);
129
130
5417
  Context::Scope context_scope(env->context());
131
5417
  Run(&exit_code, env.get());
132
4865
  return exit_code;
133
}
134
135
5417
void NodeMainInstance::Run(int* exit_code, Environment* env) {
136
5417
  if (*exit_code == 0) {
137
5417
    LoadEnvironment(env, StartExecutionCallback{});
138
139
9972
    *exit_code = SpinEventLoop(env).FromMaybe(1);
140
  }
141
142
#if defined(LEAK_SANITIZER)
143
  __lsan_do_leak_check();
144
#endif
145
4865
}
146
147
DeleteFnPtr<Environment, FreeEnvironment>
148
5417
NodeMainInstance::CreateMainEnvironment(int* exit_code) {
149
5417
  *exit_code = 0;  // Reset the exit code to 0
150
151
10834
  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
5417
  if (isolate_data_->options()->track_heap_objects) {
156
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
157
  }
158
159
  Local<Context> context;
160
5417
  DeleteFnPtr<Environment, FreeEnvironment> env;
161
162
5417
  if (snapshot_data_ != nullptr) {
163
10830
    env.reset(new Environment(isolate_data_.get(),
164
                              isolate_,
165
5415
                              args_,
166
5415
                              exec_args_,
167
5415
                              &(snapshot_data_->env_info),
168
                              EnvironmentFlags::kDefaultFlags,
169
5415
                              {}));
170
5415
    context = Context::FromSnapshot(isolate_,
171
                                    SnapshotData::kNodeMainContextIndex,
172
5415
                                    {DeserializeNodeInternalFields, env.get()})
173
5415
                  .ToLocalChecked();
174
175
5415
    CHECK(!context.IsEmpty());
176
5415
    Context::Scope context_scope(context);
177
178
10830
    CHECK(InitializeContextRuntime(context).IsJust());
179
5415
    SetIsolateErrorHandlers(isolate_, {});
180
5415
    env->InitializeMainContext(context, &(snapshot_data_->env_info));
181
#if HAVE_INSPECTOR
182
5415
    env->InitializeInspector({});
183
#endif
184
5415
    env->DoneBootstrapping();
185
186
#if HAVE_OPENSSL
187
5415
    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
5417
  return env;
209
}
210
211
}  // namespace node