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-02-11 04:11: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
4699
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
4621
const std::vector<intptr_t>& NodeMainInstance::CollectExternalReferences() {
49
  // Cannot be called more than once.
50
4621
  CHECK_NULL(registry_);
51
4621
  registry_.reset(new ExternalReferenceRegistry());
52
4621
  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
4614
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
4614
    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
4614
      owns_isolate_(true) {
79
4614
  params->array_buffer_allocator = array_buffer_allocator_.get();
80
4614
  deserialize_mode_ = per_isolate_data_indexes != nullptr;
81
4614
  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
4613
        CollectExternalReferences();
86
4613
    params->external_references = external_references.data();
87
  }
88
89
4614
  isolate_ = Isolate::Allocate();
90
4614
  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
4614
  platform->RegisterIsolate(isolate_, event_loop);
94
4614
  SetIsolateCreateParamsForNode(params);
95
4614
  Isolate::Initialize(isolate_, *params);
96
97
  // If the indexes are not nullptr, we are not deserializing
98

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


8390
NodeMainInstance::~NodeMainInstance() {
121
4195
  if (!owns_isolate_) {
122
8
    return;
123
  }
124
4187
  platform_->UnregisterIsolate(isolate_);
125
4187
  isolate_->Dispose();
126
4195
}
127
128
4614
int NodeMainInstance::Run(const EnvSerializeInfo* env_info) {
129
8801
  Locker locker(isolate_);
130
8801
  Isolate::Scope isolate_scope(isolate_);
131
8801
  HandleScope handle_scope(isolate_);
132
133
4614
  int exit_code = 0;
134
  DeleteFnPtr<Environment, FreeEnvironment> env =
135
8801
      CreateMainEnvironment(&exit_code, env_info);
136
137
4614
  CHECK_NOT_NULL(env);
138
  {
139
4614
    Context::Scope context_scope(env->context());
140
141
4614
    if (exit_code == 0) {
142
4614
      LoadEnvironment(env.get(), StartExecutionCallback{});
143
144
8536
      exit_code = SpinEventLoop(env.get()).FromMaybe(1);
145
    }
146
147
4187
    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
4187
  memset(&act, 0, sizeof(act));
154
133984
  for (unsigned nr = 1; nr < kMaxSignal; nr += 1) {
155

129797
    if (nr == SIGKILL || nr == SIGSTOP || nr == SIGPROF)
156
12561
      continue;
157
117236
    act.sa_handler = (nr == SIGPIPE) ? SIG_IGN : SIG_DFL;
158
117236
    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
8374
  return exit_code;
168
}
169
170
DeleteFnPtr<Environment, FreeEnvironment>
171
4614
NodeMainInstance::CreateMainEnvironment(int* exit_code,
172
                                        const EnvSerializeInfo* env_info) {
173
4614
  *exit_code = 0;  // Reset the exit code to 0
174
175
9228
  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
4614
  if (isolate_data_->options()->track_heap_objects) {
180
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
181
  }
182
183

4614
  CHECK_IMPLIES(deserialize_mode_, env_info != nullptr);
184
  Local<Context> context;
185
9228
  DeleteFnPtr<Environment, FreeEnvironment> env;
186
187
4614
  if (deserialize_mode_) {
188
9226
    env.reset(new Environment(isolate_data_.get(),
189
                              isolate_,
190
                              args_,
191
                              exec_args_,
192
                              env_info,
193
                              EnvironmentFlags::kDefaultFlags,
194
4613
                              {}));
195
9226
    context = Context::FromSnapshot(isolate_,
196
                                    kNodeContextIndex,
197
9226
                                    {DeserializeNodeInternalFields, env.get()})
198
4613
                  .ToLocalChecked();
199
200
4613
    CHECK(!context.IsEmpty());
201
    Context::Scope context_scope(context);
202
4613
    InitializeContextRuntime(context);
203
4613
    SetIsolateErrorHandlers(isolate_, {});
204
4613
    env->InitializeMainContext(context, env_info);
205
#if HAVE_INSPECTOR
206
4613
    env->InitializeInspector({});
207
#endif
208
4613
    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
4614
  CHECK(env->req_wrap_queue()->IsEmpty());
229
4614
  CHECK(env->handle_wrap_queue()->IsEmpty());
230
4614
  return env;
231
}
232
233

14097
}  // namespace node