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: 91 92 98.9 %
Date: 2019-09-26 22:31:05 Branches: 37 50 74.0 %

Line Branch Exec Source
1
#include "node_main_instance.h"
2
#include "node_internals.h"
3
#include "node_options-inl.h"
4
#include "node_v8_platform-inl.h"
5
#include "util-inl.h"
6
#if defined(LEAK_SANITIZER)
7
#include <sanitizer/lsan_interface.h>
8
#endif
9
10
namespace node {
11
12
using v8::Context;
13
using v8::HandleScope;
14
using v8::Isolate;
15
using v8::Local;
16
using v8::Locker;
17
using v8::SealHandleScope;
18
19
1
NodeMainInstance::NodeMainInstance(Isolate* isolate,
20
                                   uv_loop_t* event_loop,
21
                                   MultiIsolatePlatform* platform,
22
                                   const std::vector<std::string>& args,
23
                                   const std::vector<std::string>& exec_args)
24
    : args_(args),
25
      exec_args_(exec_args),
26
      array_buffer_allocator_(nullptr),
27
      isolate_(isolate),
28
      platform_(platform),
29
      isolate_data_(nullptr),
30
      owns_isolate_(false),
31
1
      deserialize_mode_(false) {
32
1
  isolate_data_.reset(new IsolateData(isolate_, event_loop, platform, nullptr));
33
1
  SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
34
1
}
35
36
1
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create(
37
    Isolate* isolate,
38
    uv_loop_t* event_loop,
39
    MultiIsolatePlatform* platform,
40
    const std::vector<std::string>& args,
41
    const std::vector<std::string>& exec_args) {
42
  return std::unique_ptr<NodeMainInstance>(
43
1
      new NodeMainInstance(isolate, event_loop, platform, args, exec_args));
44
}
45
46
4894
NodeMainInstance::NodeMainInstance(
47
    Isolate::CreateParams* params,
48
    uv_loop_t* event_loop,
49
    MultiIsolatePlatform* platform,
50
    const std::vector<std::string>& args,
51
    const std::vector<std::string>& exec_args,
52
    const std::vector<size_t>* per_isolate_data_indexes)
53
    : args_(args),
54
      exec_args_(exec_args),
55
      array_buffer_allocator_(ArrayBufferAllocator::Create()),
56
      isolate_(nullptr),
57
      platform_(platform),
58
      isolate_data_(nullptr),
59
4894
      owns_isolate_(true) {
60
4894
  params->array_buffer_allocator = array_buffer_allocator_.get();
61
4894
  isolate_ = Isolate::Allocate();
62
4894
  CHECK_NOT_NULL(isolate_);
63
  // Register the isolate on the platform before the isolate gets initialized,
64
  // so that the isolate can access the platform during initialization.
65
4894
  platform->RegisterIsolate(isolate_, event_loop);
66
4894
  SetIsolateCreateParamsForNode(params);
67
4894
  Isolate::Initialize(isolate_, *params);
68
69
4894
  deserialize_mode_ = per_isolate_data_indexes != nullptr;
70
  // If the indexes are not nullptr, we are not deserializing
71

4894
  CHECK_IMPLIES(deserialize_mode_, params->external_references != nullptr);
72
  isolate_data_.reset(new IsolateData(isolate_,
73
                                      event_loop,
74
                                      platform,
75
4894
                                      array_buffer_allocator_.get(),
76
4894
                                      per_isolate_data_indexes));
77
4894
  SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc);
78
4894
  if (!deserialize_mode_) {
79
    // If in deserialize mode, delay until after the deserialization is
80
    // complete.
81
1
    SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
82
  }
83
4894
}
84
85
1
void NodeMainInstance::Dispose() {
86
1
  CHECK(!owns_isolate_);
87
1
  platform_->DrainTasks(isolate_);
88
1
}
89
90


8972
NodeMainInstance::~NodeMainInstance() {
91
4486
  if (!owns_isolate_) {
92
1
    return;
93
  }
94
4485
  isolate_->Dispose();
95
4485
  platform_->UnregisterIsolate(isolate_);
96
4485
}
97
98
4894
int NodeMainInstance::Run() {
99
4894
  Locker locker(isolate_);
100
9379
  Isolate::Scope isolate_scope(isolate_);
101
9379
  HandleScope handle_scope(isolate_);
102
103
4894
  int exit_code = 0;
104
9379
  std::unique_ptr<Environment> env = CreateMainEnvironment(&exit_code);
105
106
4894
  CHECK_NOT_NULL(env);
107
4894
  Context::Scope context_scope(env->context());
108
109
4894
  if (exit_code == 0) {
110
    {
111
4893
      AsyncCallbackScope callback_scope(env.get());
112
4893
      env->async_hooks()->push_async_ids(1, 0);
113
4893
      LoadEnvironment(env.get());
114
4636
      env->async_hooks()->pop_async_id(1);
115
    }
116
117
4635
    env->set_trace_sync_io(env->options()->trace_sync_io);
118
119
    {
120
4635
      SealHandleScope seal(isolate_);
121
      bool more;
122
      env->performance_state()->Mark(
123
4635
          node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
124
4503
      do {
125
4649
        uv_run(env->event_loop(), UV_RUN_DEFAULT);
126
127
4504
        per_process::v8_platform.DrainVMTasks(isolate_);
128
129
4504
        more = uv_loop_alive(env->event_loop());
130

4504
        if (more && !env->is_stopping()) continue;
131
132
4504
        env->RunBeforeExitCallbacks();
133
134
4504
        if (!uv_loop_alive(env->event_loop())) {
135
4504
          EmitBeforeExit(env.get());
136
        }
137
138
        // Emit `beforeExit` if the loop became alive either after emitting
139
        // event, or after running some callbacks.
140
4503
        more = uv_loop_alive(env->event_loop());
141

4503
      } while (more == true && !env->is_stopping());
142
      env->performance_state()->Mark(
143
4489
          node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
144
    }
145
146
4489
    env->set_trace_sync_io(false);
147
4489
    exit_code = EmitExit(env.get());
148
4484
    WaitForInspectorDisconnect(env.get());
149
  }
150
151
4485
  env->set_can_call_into_js(false);
152
4485
  env->stop_sub_worker_contexts();
153
4485
  ResetStdio();
154
4485
  env->RunCleanup();
155
4485
  RunAtExit(env.get());
156
157
4485
  per_process::v8_platform.DrainVMTasks(isolate_);
158
4485
  per_process::v8_platform.CancelVMTasks(isolate_);
159
160
#if defined(LEAK_SANITIZER)
161
  __lsan_do_leak_check();
162
#endif
163
164
8970
  return exit_code;
165
}
166
167
// TODO(joyeecheung): align this with the CreateEnvironment exposed in node.h
168
// and the environment creation routine in workers somehow.
169
4894
std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment(
170
    int* exit_code) {
171
4894
  *exit_code = 0;  // Reset the exit code to 0
172
173
4894
  HandleScope handle_scope(isolate_);
174
175
  // TODO(addaleax): This should load a real per-Isolate option, currently
176
  // this is still effectively per-process.
177
4894
  if (isolate_data_->options()->track_heap_objects) {
178
1
    isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true);
179
  }
180
181
  Local<Context> context;
182
4894
  if (deserialize_mode_) {
183
    context =
184
9786
        Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked();
185
4893
    InitializeContextRuntime(context);
186
4893
    SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers);
187
  } else {
188
1
    context = NewContext(isolate_);
189
  }
190
191
4894
  CHECK(!context.IsEmpty());
192
  Context::Scope context_scope(context);
193
194
  std::unique_ptr<Environment> env = std::make_unique<Environment>(
195
4894
      isolate_data_.get(),
196
      context,
197
      args_,
198
      exec_args_,
199
      static_cast<Environment::Flags>(Environment::kIsMainThread |
200
                                      Environment::kOwnsProcessState |
201
9788
                                      Environment::kOwnsInspector));
202
4894
  env->InitializeLibuv(per_process::v8_is_profiling);
203
4894
  env->InitializeDiagnostics();
204
205
  // TODO(joyeecheung): when we snapshot the bootstrapped context,
206
  // the inspector and diagnostics setup should after after deserialization.
207
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM
208
4894
  *exit_code = env->InitializeInspector(nullptr);
209
#endif
210
4894
  if (*exit_code != 0) {
211
1
    return env;
212
  }
213
214
9786
  if (env->RunBootstrapping().IsEmpty()) {
215
    *exit_code = 1;
216
  }
217
218
9787
  return env;
219
}
220
221
}  // namespace node