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


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