GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_main_instance.cc Lines: 96 97 99.0 %
Date: 2021-04-18 04:12:15 Branches: 47 62 75.8 %

Line Branch Exec Source
1
#include "node_main_instance.h"
2
#include <iostream>
3
#include <memory>
4
#include "debug_utils-inl.h"
5
#include "node_external_reference.h"
6
#include "node_internals.h"
7
#include "node_options-inl.h"
8
#include "node_snapshotable.h"
9
#include "node_v8_platform-inl.h"
10
#include "util-inl.h"
11
#if defined(LEAK_SANITIZER)
12
#include <sanitizer/lsan_interface.h>
13
#endif
14
15
#if HAVE_INSPECTOR
16
#include "inspector/worker_inspector.h"  // ParentInspectorHandle
17
#endif
18
19
namespace node {
20
21
using v8::Context;
22
using v8::HandleScope;
23
using v8::Isolate;
24
using v8::Local;
25
using v8::Locker;
26
27
4770
std::unique_ptr<ExternalReferenceRegistry> NodeMainInstance::registry_ =
28
    nullptr;
29
8
NodeMainInstance::NodeMainInstance(Isolate* isolate,
30
                                   uv_loop_t* event_loop,
31
                                   MultiIsolatePlatform* platform,
32
                                   const std::vector<std::string>& args,
33
8
                                   const std::vector<std::string>& exec_args)
34
    : args_(args),
35
      exec_args_(exec_args),
36
      array_buffer_allocator_(nullptr),
37
      isolate_(isolate),
38
      platform_(platform),
39
      isolate_data_(nullptr),
40
      owns_isolate_(false),
41
8
      deserialize_mode_(false) {
42
  isolate_data_ =
43
8
      std::make_unique<IsolateData>(isolate_, event_loop, platform, nullptr);
44
45
8
  SetIsolateMiscHandlers(isolate_, {});
46
8
}
47
48
4703
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() {
49
  // Cannot be called more than once.
50
4703
  CHECK_NULL(registry_);
51
4703
  registry_.reset(new ExternalReferenceRegistry());
52
4703
  return registry_->external_references();
53
}
54
55
8
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
56
    Isolate* isolate,
57
    uv_loop_t* event_loop,
58
    MultiIsolatePlatform* platform,
59
    const std::vector<std::string>& args,
60
    const std::vector<std::string>& exec_args) {
61
  return std::unique_ptr<NodeMainInstance>(
62
8
      new NodeMainInstance(isolate, event_loop, platform, args, exec_args));
63
}
64
65
4696
NodeMainInstance::NodeMainInstance(
66
    Isolate::CreateParams* params,
67
    uv_loop_t* event_loop,
68
    MultiIsolatePlatform* platform,
69
    const std::vector<std::string>& args,
70
    const std::vector<std::string>& exec_args,
71
4696
    const std::vector<size_t>* per_isolate_data_indexes)
72
    : args_(args),
73
      exec_args_(exec_args),
74
      array_buffer_allocator_(ArrayBufferAllocator::Create()),
75
      isolate_(nullptr),
76
      platform_(platform),
77
      isolate_data_(nullptr),
78
4696
      owns_isolate_(true) {
79
4696
  params->array_buffer_allocator = array_buffer_allocator_.get();
80
4696
  deserialize_mode_ = per_isolate_data_indexes != nullptr;
81
4696
  if (deserialize_mode_) {
82
    // TODO(joyeecheung): collect external references and set it in
83
    // params.external_references.
84
    const std::vector<intptr_t>& external_references =
85
4695
        CollectExternalReferences();
86
4695
    params->external_references = external_references.data();
87
  }
88
89
4696
  isolate_ = Isolate::Allocate();
90
4696
  CHECK_NOT_NULL(isolate_);
91
  // Register the isolate on the platform before the isolate gets initialized,
92
  // so that the isolate can access the platform during initialization.
93
4696
  platform->RegisterIsolate(isolate_, event_loop);
94
4696
  SetIsolateCreateParamsForNode(params);
95
4696
  Isolate::Initialize(isolate_, *params);
96
97
  // If the indexes are not nullptr, we are not deserializing
98

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


8500
NodeMainInstance::~NodeMainInstance() {
121
4250
  if (!owns_isolate_) {
122
8
    return;
123
  }
124
4242
  platform_->UnregisterIsolate(isolate_);
125
4242
  isolate_->Dispose();
126
4250
}
127
128
4696
int NodeMainInstance::Run(const EnvSerializeInfo* env_info) {
129
8938
  Locker locker(isolate_);
130
8938
  Isolate::Scope isolate_scope(isolate_);
131
8938
  HandleScope handle_scope(isolate_);
132
133
4696
  int exit_code = 0;
134
  DeleteFnPtr<Environment, FreeEnvironment> env =
135
8938
      CreateMainEnvironment(&exit_code, env_info);
136
137
4696
  CHECK_NOT_NULL(env);
138
  {
139
4696
    Context::Scope context_scope(env->context());
140
141
4696
    if (exit_code == 0) {
142
4696
      LoadEnvironment(env.get(), StartExecutionCallback{});
143
144
8650
      exit_code = SpinEventLoop(env.get()).FromMaybe(1);
145
    }
146
147
4242
    ResetStdio();
148
149
    // TODO(addaleax): Neither NODE_SHARED_MODE nor HAVE_INSPECTOR really
150
    // make sense here.
151
#if HAVE_INSPECTOR && defined(__POSIX__) && !defined(NODE_SHARED_MODE)
152
  struct sigaction act;
153
4242
  memset(&act, 0, sizeof(act));
154
135744
  for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
155

131502
    if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF)
156
12726
      continue;
157
118776
    act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
158
118776
    CHECK_EQ(0, sigaction(nr, &act, nullptr));
159
  }
160
#endif
161
162
#if defined(LEAK_SANITIZER)
163
  __lsan_do_leak_check();
164
#endif
165
  }
166
167
8484
  return exit_code;
168
}
169
170
DeleteFnPtr<Environment, FreeEnvironment>
171
4696
NodeMainInstance::CreateMainEnvironment(int* exit_code,
172
                                        const EnvSerializeInfo* env_info) {
173
4696
  *exit_code = 0;  // Reset the exit code to 0
174
175
9392
  HandleScope handle_scope(isolate_);
176
177
  // TODO(addaleax): This should load a real per-Isolate option, currently
178
  // this is still effectively per-process.
179
4696
  if (isolate_data_->options()->track_heap_objects) {
180
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
181
  }
182
183

4696
  CHECK_IMPLIES(deserialize_mode_, env_info != nullptr);
184
  Local<Context> context;
185
9392
  DeleteFnPtr<Environment, FreeEnvironment> env;
186
187
4696
  if (deserialize_mode_) {
188
9390
    env.reset(new Environment(isolate_data_.get(),
189
                              isolate_,
190
                              args_,
191
                              exec_args_,
192
                              env_info,
193
                              EnvironmentFlags::kDefaultFlags,
194
4695
                              {}));
195
9390
    context = Context::FromSnapshot(isolate_,
196
                                    kNodeContextIndex,
197
9390
                                    {DeserializeNodeInternalFields, env.get()})
198
4695
                  .ToLocalChecked();
199
200
4695
    CHECK(!context.IsEmpty());
201
    Context::Scope context_scope(context);
202
4695
    InitializeContextRuntime(context);
203
4695
    SetIsolateErrorHandlers(isolate_, {});
204
4695
    env->InitializeMainContext(context, env_info);
205
#if HAVE_INSPECTOR
206
4695
    env->InitializeInspector({});
207
#endif
208
4695
    env->DoneBootstrapping();
209
  } else {
210
1
    context = NewContext(isolate_);
211
1
    CHECK(!context.IsEmpty());
212
1
    Context::Scope context_scope(context);
213
2
    env.reset(new Environment(isolate_data_.get(),
214
                              context,
215
                              args_,
216
                              exec_args_,
217
                              nullptr,
218
                              EnvironmentFlags::kDefaultFlags,
219
1
                              {}));
220
#if HAVE_INSPECTOR
221
1
    env->InitializeInspector({});
222
#endif
223
2
    if (env->RunBootstrapping().IsEmpty()) {
224
      return nullptr;
225
    }
226
  }
227
228
4696
  CHECK(env->req_wrap_queue()->IsEmpty());
229
4696
  CHECK(env->handle_wrap_queue()->IsEmpty());
230
4696
  return env;
231
}
232
233

14310
}  // namespace node