GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env.cc Lines: 920 1005 91.5 %
Date: 2022-09-02 04:23:42 Branches: 851 1462 58.2 %

Line Branch Exec Source
1
#include "env.h"
2
#include "async_wrap.h"
3
#include "base_object-inl.h"
4
#include "debug_utils-inl.h"
5
#include "diagnosticfilename-inl.h"
6
#include "memory_tracker-inl.h"
7
#include "node_buffer.h"
8
#include "node_context_data.h"
9
#include "node_contextify.h"
10
#include "node_errors.h"
11
#include "node_internals.h"
12
#include "node_options-inl.h"
13
#include "node_process-inl.h"
14
#include "node_v8_platform-inl.h"
15
#include "node_worker.h"
16
#include "req_wrap-inl.h"
17
#include "stream_base.h"
18
#include "tracing/agent.h"
19
#include "tracing/traced_value.h"
20
#include "util-inl.h"
21
#include "v8-profiler.h"
22
23
#include <algorithm>
24
#include <atomic>
25
#include <cinttypes>
26
#include <cstdio>
27
#include <iostream>
28
#include <limits>
29
#include <memory>
30
31
namespace node {
32
33
using errors::TryCatchScope;
34
using v8::Array;
35
using v8::Boolean;
36
using v8::Context;
37
using v8::EmbedderGraph;
38
using v8::EscapableHandleScope;
39
using v8::Function;
40
using v8::FunctionCallbackInfo;
41
using v8::FunctionTemplate;
42
using v8::HandleScope;
43
using v8::HeapSpaceStatistics;
44
using v8::Integer;
45
using v8::Isolate;
46
using v8::Local;
47
using v8::MaybeLocal;
48
using v8::NewStringType;
49
using v8::Number;
50
using v8::Object;
51
using v8::Private;
52
using v8::Script;
53
using v8::SnapshotCreator;
54
using v8::StackTrace;
55
using v8::String;
56
using v8::Symbol;
57
using v8::TracingController;
58
using v8::TryCatch;
59
using v8::Undefined;
60
using v8::Value;
61
using v8::WeakCallbackInfo;
62
using v8::WeakCallbackType;
63
using worker::Worker;
64
65
int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64;
66
void* const ContextEmbedderTag::kNodeContextTagPtr = const_cast<void*>(
67
    static_cast<const void*>(&ContextEmbedderTag::kNodeContextTag));
68
69
15606
void AsyncHooks::SetJSPromiseHooks(Local<Function> init,
70
                                   Local<Function> before,
71
                                   Local<Function> after,
72
                                   Local<Function> resolve) {
73
15606
  js_promise_hooks_[0].Reset(env()->isolate(), init);
74
15606
  js_promise_hooks_[1].Reset(env()->isolate(), before);
75
15606
  js_promise_hooks_[2].Reset(env()->isolate(), after);
76
15606
  js_promise_hooks_[3].Reset(env()->isolate(), resolve);
77
31557
  for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
78
15951
    if (it->IsEmpty()) {
79
      contexts_.erase(it--);
80
      continue;
81
    }
82
31902
    PersistentToLocal::Weak(env()->isolate(), *it)
83
15951
        ->SetPromiseHooks(init, before, after, resolve);
84
  }
85
15606
}
86
87
// Remember to keep this code aligned with pushAsyncContext() in JS.
88
810294
void AsyncHooks::push_async_context(double async_id,
89
                                    double trigger_async_id,
90
                                    Local<Object> resource) {
91
  // Since async_hooks is experimental, do only perform the check
92
  // when async_hooks is enabled.
93
810294
  if (fields_[kCheck] > 0) {
94
810290
    CHECK_GE(async_id, -1);
95
810290
    CHECK_GE(trigger_async_id, -1);
96
  }
97
98
810294
  uint32_t offset = fields_[kStackLength];
99
810294
  if (offset * 2 >= async_ids_stack_.Length()) grow_async_ids_stack();
100
810294
  async_ids_stack_[2 * offset] = async_id_fields_[kExecutionAsyncId];
101
810294
  async_ids_stack_[2 * offset + 1] = async_id_fields_[kTriggerAsyncId];
102
810294
  fields_[kStackLength] += 1;
103
810294
  async_id_fields_[kExecutionAsyncId] = async_id;
104
810294
  async_id_fields_[kTriggerAsyncId] = trigger_async_id;
105
106
#ifdef DEBUG
107
  for (uint32_t i = offset; i < native_execution_async_resources_.size(); i++)
108
    CHECK(native_execution_async_resources_[i].IsEmpty());
109
#endif
110
111
  // When this call comes from JS (as a way of increasing the stack size),
112
  // `resource` will be empty, because JS caches these values anyway.
113
810294
  if (!resource.IsEmpty()) {
114
810290
    native_execution_async_resources_.resize(offset + 1);
115
    // Caveat: This is a v8::Local<> assignment, we do not keep a v8::Global<>!
116
810290
    native_execution_async_resources_[offset] = resource;
117
  }
118
810294
}
119
120
// Remember to keep this code aligned with popAsyncContext() in JS.
121
809881
bool AsyncHooks::pop_async_context(double async_id) {
122
  // In case of an exception then this may have already been reset, if the
123
  // stack was multiple MakeCallback()'s deep.
124
809881
  if (UNLIKELY(fields_[kStackLength] == 0)) return false;
125
126
  // Ask for the async_id to be restored as a check that the stack
127
  // hasn't been corrupted.
128
1617648
  if (UNLIKELY(fields_[kCheck] > 0 &&
129

1617648
               async_id_fields_[kExecutionAsyncId] != async_id)) {
130
4
    FailWithCorruptedAsyncStack(async_id);
131
  }
132
133
808822
  uint32_t offset = fields_[kStackLength] - 1;
134
808822
  async_id_fields_[kExecutionAsyncId] = async_ids_stack_[2 * offset];
135
808822
  async_id_fields_[kTriggerAsyncId] = async_ids_stack_[2 * offset + 1];
136
808822
  fields_[kStackLength] = offset;
137
138
1617644
  if (LIKELY(offset < native_execution_async_resources_.size() &&
139

1617644
             !native_execution_async_resources_[offset].IsEmpty())) {
140
#ifdef DEBUG
141
    for (uint32_t i = offset + 1; i < native_execution_async_resources_.size();
142
         i++) {
143
      CHECK(native_execution_async_resources_[i].IsEmpty());
144
    }
145
#endif
146
808822
    native_execution_async_resources_.resize(offset);
147
808822
    if (native_execution_async_resources_.size() <
148

1070341
            native_execution_async_resources_.capacity() / 2 &&
149
261519
        native_execution_async_resources_.size() > 16) {
150
      native_execution_async_resources_.shrink_to_fit();
151
    }
152
  }
153
154
1617644
  if (UNLIKELY(js_execution_async_resources()->Length() > offset)) {
155
28342
    HandleScope handle_scope(env()->isolate());
156
56684
    USE(js_execution_async_resources()->Set(
157
        env()->context(),
158
        env()->length_string(),
159
113368
        Integer::NewFromUnsigned(env()->isolate(), offset)));
160
  }
161
162
808822
  return fields_[kStackLength] > 0;
163
}
164
165
2324
void AsyncHooks::clear_async_id_stack() {
166
2324
  Isolate* isolate = env()->isolate();
167
2324
  HandleScope handle_scope(isolate);
168
2324
  if (!js_execution_async_resources_.IsEmpty()) {
169
3100
    USE(PersistentToLocal::Strong(js_execution_async_resources_)
170
3100
            ->Set(env()->context(),
171
                  env()->length_string(),
172
6200
                  Integer::NewFromUnsigned(isolate, 0)));
173
  }
174
2324
  native_execution_async_resources_.clear();
175
2324
  native_execution_async_resources_.shrink_to_fit();
176
177
2324
  async_id_fields_[kExecutionAsyncId] = 0;
178
2324
  async_id_fields_[kTriggerAsyncId] = 0;
179
2324
  fields_[kStackLength] = 0;
180
2324
}
181
182
6752
void AsyncHooks::AddContext(Local<Context> ctx) {
183
20256
  ctx->SetPromiseHooks(js_promise_hooks_[0].IsEmpty()
184
6752
                           ? Local<Function>()
185
205
                           : PersistentToLocal::Strong(js_promise_hooks_[0]),
186
6752
                       js_promise_hooks_[1].IsEmpty()
187
6752
                           ? Local<Function>()
188
205
                           : PersistentToLocal::Strong(js_promise_hooks_[1]),
189
6752
                       js_promise_hooks_[2].IsEmpty()
190
6752
                           ? Local<Function>()
191
205
                           : PersistentToLocal::Strong(js_promise_hooks_[2]),
192
6752
                       js_promise_hooks_[3].IsEmpty()
193
6752
                           ? Local<Function>()
194
                           : PersistentToLocal::Strong(js_promise_hooks_[3]));
195
196
6752
  size_t id = contexts_.size();
197
6752
  contexts_.resize(id + 1);
198
6752
  contexts_[id].Reset(env()->isolate(), ctx);
199
6752
  contexts_[id].SetWeak();
200
6752
}
201
202
507
void AsyncHooks::RemoveContext(Local<Context> ctx) {
203
507
  Isolate* isolate = env()->isolate();
204
1014
  HandleScope handle_scope(isolate);
205
507
  contexts_.erase(std::remove_if(contexts_.begin(),
206
                                 contexts_.end(),
207
3727
                                 [&](auto&& el) { return el.IsEmpty(); }),
208
1014
                  contexts_.end());
209
3664
  for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
210
3157
    Local<Context> saved_context = PersistentToLocal::Weak(isolate, *it);
211
3157
    if (saved_context == ctx) {
212
      it->Reset();
213
      contexts_.erase(it);
214
      break;
215
    }
216
  }
217
507
}
218
219
237880
AsyncHooks::DefaultTriggerAsyncIdScope::DefaultTriggerAsyncIdScope(
220
237880
    Environment* env, double default_trigger_async_id)
221
237880
    : async_hooks_(env->async_hooks()) {
222
237880
  if (env->async_hooks()->fields()[AsyncHooks::kCheck] > 0) {
223
237880
    CHECK_GE(default_trigger_async_id, 0);
224
  }
225
226
237880
  old_default_trigger_async_id_ =
227
237880
      async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
228
237880
  async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] =
229
237880
      default_trigger_async_id;
230
237880
}
231
232
475758
AsyncHooks::DefaultTriggerAsyncIdScope::~DefaultTriggerAsyncIdScope() {
233
237879
  async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] =
234
237879
      old_default_trigger_async_id_;
235
237879
}
236
237
237880
AsyncHooks::DefaultTriggerAsyncIdScope::DefaultTriggerAsyncIdScope(
238
237880
    AsyncWrap* async_wrap)
239
    : DefaultTriggerAsyncIdScope(async_wrap->env(),
240
237880
                                 async_wrap->get_async_id()) {}
241
242
12
std::ostream& operator<<(std::ostream& output,
243
                         const std::vector<SnapshotIndex>& v) {
244
12
  output << "{ ";
245
2142
  for (const SnapshotIndex i : v) {
246
2130
    output << i << ", ";
247
  }
248
12
  output << " }";
249
12
  return output;
250
}
251
252
6
std::ostream& operator<<(std::ostream& output,
253
                         const IsolateDataSerializeInfo& i) {
254
  output << "{\n"
255
6
         << "// -- primitive begins --\n"
256
6
         << i.primitive_values << ",\n"
257
         << "// -- primitive ends --\n"
258
6
         << "// -- template_values begins --\n"
259
6
         << i.template_values << ",\n"
260
         << "// -- template_values ends --\n"
261
6
         << "}";
262
6
  return output;
263
}
264
265
6
std::ostream& operator<<(std::ostream& output, const SnapshotMetadata& i) {
266
  output << "{\n"
267
         << "  "
268
6
         << (i.type == SnapshotMetadata::Type::kDefault
269
                 ? "SnapshotMetadata::Type::kDefault"
270
                 : "SnapshotMetadata::Type::kFullyCustomized")
271
         << ", // type\n"
272
6
         << "  \"" << i.node_version << "\", // node_version\n"
273
6
         << "  \"" << i.node_arch << "\", // node_arch\n"
274
6
         << "  \"" << i.node_platform << "\", // node_platform\n"
275
6
         << "  " << i.v8_cache_version_tag << ", // v8_cache_version_tag\n"
276
6
         << "}";
277
6
  return output;
278
}
279
280
6
IsolateDataSerializeInfo IsolateData::Serialize(SnapshotCreator* creator) {
281
6
  Isolate* isolate = creator->GetIsolate();
282
6
  IsolateDataSerializeInfo info;
283
12
  HandleScope handle_scope(isolate);
284
  // XXX(joyeecheung): technically speaking, the indexes here should be
285
  // consecutive and we could just return a range instead of an array,
286
  // but that's not part of the V8 API contract so we use an array
287
  // just to be safe.
288
289
#define VP(PropertyName, StringValue) V(Private, PropertyName)
290
#define VY(PropertyName, StringValue) V(Symbol, PropertyName)
291
#define VS(PropertyName, StringValue) V(String, PropertyName)
292
#define V(TypeName, PropertyName)                                              \
293
  info.primitive_values.push_back(                                             \
294
      creator->AddData(PropertyName##_.Get(isolate)));
295
60
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
296
78
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
297
1662
  PER_ISOLATE_STRING_PROPERTIES(VS)
298
#undef V
299
#undef VY
300
#undef VS
301
#undef VP
302
303
354
  for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++)
304
696
    info.primitive_values.push_back(creator->AddData(async_wrap_provider(i)));
305
306
6
  uint32_t id = 0;
307
#define V(PropertyName, TypeName)                                              \
308
  do {                                                                         \
309
    Local<TypeName> field = PropertyName();                                    \
310
    if (!field.IsEmpty()) {                                                    \
311
      size_t index = creator->AddData(field);                                  \
312
      info.template_values.push_back({#PropertyName, id, index});              \
313
    }                                                                          \
314
    id++;                                                                      \
315
  } while (0);
316


















348
  PER_ISOLATE_TEMPLATE_PROPERTIES(V)
317
#undef V
318
319
6
  return info;
320
}
321
322
5358
void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
323
5358
  size_t i = 0;
324
5358
  HandleScope handle_scope(isolate_);
325
326
#define VP(PropertyName, StringValue) V(Private, PropertyName)
327
#define VY(PropertyName, StringValue) V(Symbol, PropertyName)
328
#define VS(PropertyName, StringValue) V(String, PropertyName)
329
#define V(TypeName, PropertyName)                                              \
330
  do {                                                                         \
331
    MaybeLocal<TypeName> maybe_field =                                         \
332
        isolate_->GetDataFromSnapshotOnce<TypeName>(                           \
333
            info->primitive_values[i++]);                                      \
334
    Local<TypeName> field;                                                     \
335
    if (!maybe_field.ToLocal(&field)) {                                        \
336
      fprintf(stderr, "Failed to deserialize " #PropertyName "\n");            \
337
    }                                                                          \
338
    PropertyName##_.Set(isolate_, field);                                      \
339
  } while (0);
340




101802
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
341






133950
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
342










































































































































2962974
  PER_ISOLATE_STRING_PROPERTIES(VS)
343
#undef V
344
#undef VY
345
#undef VS
346
#undef VP
347
348
316122
  for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
349
    MaybeLocal<String> maybe_field =
350
621528
        isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
351
    Local<String> field;
352
310764
    if (!maybe_field.ToLocal(&field)) {
353
      fprintf(stderr, "Failed to deserialize AsyncWrap provider %zu\n", j);
354
    }
355
310764
    async_wrap_providers_[j].Set(isolate_, field);
356
  }
357
358
5358
  const std::vector<PropInfo>& values = info->template_values;
359
5358
  i = 0;  // index to the array
360
5358
  uint32_t id = 0;
361
#define V(PropertyName, TypeName)                                              \
362
  do {                                                                         \
363
    if (values.size() > i && id == values[i].id) {                             \
364
      const PropInfo& d = values[i];                                           \
365
      DCHECK_EQ(d.name, #PropertyName);                                        \
366
      MaybeLocal<TypeName> maybe_field =                                       \
367
          isolate_->GetDataFromSnapshotOnce<TypeName>(d.index);                \
368
      Local<TypeName> field;                                                   \
369
      if (!maybe_field.ToLocal(&field)) {                                      \
370
        fprintf(stderr,                                                        \
371
                "Failed to deserialize isolate data template " #PropertyName   \
372
                "\n");                                                         \
373
      }                                                                        \
374
      set_##PropertyName(field);                                               \
375
      i++;                                                                     \
376
    }                                                                          \
377
    id++;                                                                      \
378
  } while (0);
379
380










































































219678
  PER_ISOLATE_TEMPLATE_PROPERTIES(V);
381
#undef V
382
5358
}
383
384
785
void IsolateData::CreateProperties() {
385
  // Create string and private symbol properties as internalized one byte
386
  // strings after the platform is properly initialized.
387
  //
388
  // Internalized because it makes property lookups a little faster and
389
  // because the string is created in the old space straight away.  It's going
390
  // to end up in the old space sooner or later anyway but now it doesn't go
391
  // through v8::Eternal's new space handling first.
392
  //
393
  // One byte because our strings are ASCII and we can safely skip V8's UTF-8
394
  // decoding step.
395
396
1570
  HandleScope handle_scope(isolate_);
397
398
#define V(PropertyName, StringValue)                                           \
399
  PropertyName##_.Set(                                                         \
400
      isolate_,                                                                \
401
      Private::New(isolate_,                                                   \
402
                   String::NewFromOneByte(                                     \
403
                       isolate_,                                               \
404
                       reinterpret_cast<const uint8_t*>(StringValue),          \
405
                       NewStringType::kInternalized,                           \
406
                       sizeof(StringValue) - 1)                                \
407
                       .ToLocalChecked()));
408
7850
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
409
#undef V
410
#define V(PropertyName, StringValue)                                           \
411
  PropertyName##_.Set(                                                         \
412
      isolate_,                                                                \
413
      Symbol::New(isolate_,                                                    \
414
                  String::NewFromOneByte(                                      \
415
                      isolate_,                                                \
416
                      reinterpret_cast<const uint8_t*>(StringValue),           \
417
                      NewStringType::kInternalized,                            \
418
                      sizeof(StringValue) - 1)                                 \
419
                      .ToLocalChecked()));
420
10205
  PER_ISOLATE_SYMBOL_PROPERTIES(V)
421
#undef V
422
#define V(PropertyName, StringValue)                                           \
423
  PropertyName##_.Set(                                                         \
424
      isolate_,                                                                \
425
      String::NewFromOneByte(isolate_,                                         \
426
                             reinterpret_cast<const uint8_t*>(StringValue),    \
427
                             NewStringType::kInternalized,                     \
428
                             sizeof(StringValue) - 1)                          \
429
          .ToLocalChecked());
430
217445
  PER_ISOLATE_STRING_PROPERTIES(V)
431
#undef V
432
433
  // Create all the provider strings that will be passed to JS. Place them in
434
  // an array so the array index matches the PROVIDER id offset. This way the
435
  // strings can be retrieved quickly.
436
#define V(Provider)                                                           \
437
  async_wrap_providers_[AsyncWrap::PROVIDER_ ## Provider].Set(                \
438
      isolate_,                                                               \
439
      String::NewFromOneByte(                                                 \
440
        isolate_,                                                             \
441
        reinterpret_cast<const uint8_t*>(#Provider),                          \
442
        NewStringType::kInternalized,                                         \
443
        sizeof(#Provider) - 1).ToLocalChecked());
444
46315
  NODE_ASYNC_PROVIDER_TYPES(V)
445
#undef V
446
447
  // TODO(legendecas): eagerly create per isolate templates.
448
785
  Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
449
1570
  templ->InstanceTemplate()->SetInternalFieldCount(
450
      BaseObject::kInternalFieldCount);
451
785
  templ->Inherit(BaseObject::GetConstructorTemplate(this));
452
785
  set_binding_data_ctor_template(templ);
453
454
785
  set_contextify_global_template(
455
785
      contextify::ContextifyContext::CreateGlobalTemplate(isolate_));
456
785
}
457
458
6143
IsolateData::IsolateData(Isolate* isolate,
459
                         uv_loop_t* event_loop,
460
                         MultiIsolatePlatform* platform,
461
                         ArrayBufferAllocator* node_allocator,
462
6143
                         const IsolateDataSerializeInfo* isolate_data_info)
463
    : isolate_(isolate),
464
      event_loop_(event_loop),
465
51
      node_allocator_(node_allocator == nullptr ? nullptr
466
6092
                                                : node_allocator->GetImpl()),
467
12286
      platform_(platform) {
468
6143
  options_.reset(
469
6143
      new PerIsolateOptions(*(per_process::cli_options->per_isolate)));
470
471
6143
  if (isolate_data_info == nullptr) {
472
785
    CreateProperties();
473
  } else {
474
5358
    DeserializeProperties(isolate_data_info);
475
  }
476
6143
}
477
478
24
void IsolateData::MemoryInfo(MemoryTracker* tracker) const {
479
#define V(PropertyName, StringValue)                                           \
480
  tracker->TrackField(#PropertyName, PropertyName());
481
24
  PER_ISOLATE_SYMBOL_PROPERTIES(V)
482
483
24
  PER_ISOLATE_STRING_PROPERTIES(V)
484
#undef V
485
486
24
  tracker->TrackField("async_wrap_providers", async_wrap_providers_);
487
488
24
  if (node_allocator_ != nullptr) {
489
24
    tracker->TrackFieldWithSize(
490
        "node_allocator", sizeof(*node_allocator_), "NodeArrayBufferAllocator");
491
  }
492
24
  tracker->TrackFieldWithSize(
493
      "platform", sizeof(*platform_), "MultiIsolatePlatform");
494
  // TODO(joyeecheung): implement MemoryRetainer in the option classes.
495
24
}
496
497
154
void TrackingTraceStateObserver::UpdateTraceCategoryState() {
498

154
  if (!env_->owns_process_state() || !env_->can_call_into_js()) {
499
    // Ideally, we’d have a consistent story that treats all threads/Environment
500
    // instances equally here. However, tracing is essentially global, and this
501
    // callback is called from whichever thread calls `StartTracing()` or
502
    // `StopTracing()`. The only way to do this in a threadsafe fashion
503
    // seems to be only tracking this from the main thread, and only allowing
504
    // these state modifications from the main thread.
505
96
    return;
506
  }
507
508
143
  if (env_->principal_realm() == nullptr) {
509
85
    return;
510
  }
511
512
58
  bool async_hooks_enabled = (*(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
513
58
                                 TRACING_CATEGORY_NODE1(async_hooks)))) != 0;
514
515
58
  Isolate* isolate = env_->isolate();
516
58
  HandleScope handle_scope(isolate);
517
58
  Local<Function> cb = env_->trace_category_state_function();
518
58
  if (cb.IsEmpty())
519
    return;
520
58
  TryCatchScope try_catch(env_);
521
58
  try_catch.SetVerbose(true);
522
116
  Local<Value> args[] = {Boolean::New(isolate, async_hooks_enabled)};
523
116
  USE(cb->Call(env_->context(), Undefined(isolate), arraysize(args), args));
524
}
525
526
6752
void Environment::AssignToContext(Local<v8::Context> context,
527
                                  Realm* realm,
528
                                  const ContextInfo& info) {
529
6752
  context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
530
                                           this);
531
6752
  context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kRealm, realm);
532
  // Used to retrieve bindings
533
13504
  context->SetAlignedPointerInEmbedderData(
534
6752
      ContextEmbedderIndex::kBindingListIndex, &(this->bindings_));
535
536
  // ContextifyContexts will update this to a pointer to the native object.
537
6752
  context->SetAlignedPointerInEmbedderData(
538
      ContextEmbedderIndex::kContextifyContext, nullptr);
539
540
  // This must not be done before other context fields are initialized.
541
6752
  ContextEmbedderTag::TagNodeContext(context);
542
543
#if HAVE_INSPECTOR
544
6752
  inspector_agent()->ContextCreated(context, info);
545
#endif  // HAVE_INSPECTOR
546
547
6752
  this->async_hooks()->AddContext(context);
548
6752
}
549
550
179
void Environment::TryLoadAddon(
551
    const char* filename,
552
    int flags,
553
    const std::function<bool(binding::DLib*)>& was_loaded) {
554
179
  loaded_addons_.emplace_back(filename, flags);
555
179
  if (!was_loaded(&loaded_addons_.back())) {
556
10
    loaded_addons_.pop_back();
557
  }
558
179
}
559
560
11
std::string Environment::GetCwd() {
561
  char cwd[PATH_MAX_BYTES];
562
11
  size_t size = PATH_MAX_BYTES;
563
11
  const int err = uv_cwd(cwd, &size);
564
565
11
  if (err == 0) {
566
11
    CHECK_GT(size, 0);
567
11
    return cwd;
568
  }
569
570
  // This can fail if the cwd is deleted. In that case, fall back to
571
  // exec_path.
572
  const std::string& exec_path = exec_path_;
573
  return exec_path.substr(0, exec_path.find_last_of(kPathSeparator));
574
}
575
576
1892
void Environment::add_refs(int64_t diff) {
577
1892
  task_queues_async_refs_ += diff;
578
1892
  CHECK_GE(task_queues_async_refs_, 0);
579
1892
  if (task_queues_async_refs_ == 0)
580
415
    uv_unref(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
581
  else
582
1477
    uv_ref(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
583
1892
}
584
585
65807
uv_buf_t Environment::allocate_managed_buffer(const size_t suggested_size) {
586
131614
  NoArrayBufferZeroFillScope no_zero_fill_scope(isolate_data());
587
  std::unique_ptr<v8::BackingStore> bs =
588
65807
      v8::ArrayBuffer::NewBackingStore(isolate(), suggested_size);
589
65807
  uv_buf_t buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
590
65807
  released_allocated_buffers_.emplace(buf.base, std::move(bs));
591
65807
  return buf;
592
}
593
594
80683
std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
595
    const uv_buf_t& buf) {
596
80683
  std::unique_ptr<v8::BackingStore> bs;
597
80683
  if (buf.base != nullptr) {
598
65807
    auto it = released_allocated_buffers_.find(buf.base);
599
65807
    CHECK_NE(it, released_allocated_buffers_.end());
600
65807
    bs = std::move(it->second);
601
65807
    released_allocated_buffers_.erase(it);
602
  }
603
80683
  return bs;
604
}
605
606
6132
std::string GetExecPath(const std::vector<std::string>& argv) {
607
  char exec_path_buf[2 * PATH_MAX];
608
6132
  size_t exec_path_len = sizeof(exec_path_buf);
609
6132
  std::string exec_path;
610
6132
  if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
611
6132
    exec_path = std::string(exec_path_buf, exec_path_len);
612
  } else {
613
    exec_path = argv[0];
614
  }
615
616
  // On OpenBSD process.execPath will be relative unless we
617
  // get the full path before process.execPath is used.
618
#if defined(__OpenBSD__)
619
  uv_fs_t req;
620
  req.ptr = nullptr;
621
  if (0 ==
622
      uv_fs_realpath(nullptr, &req, exec_path.c_str(), nullptr)) {
623
    CHECK_NOT_NULL(req.ptr);
624
    exec_path = std::string(static_cast<char*>(req.ptr));
625
  }
626
  uv_fs_req_cleanup(&req);
627
#endif
628
629
6132
  return exec_path;
630
}
631
632
6132
Environment::Environment(IsolateData* isolate_data,
633
                         Isolate* isolate,
634
                         const std::vector<std::string>& args,
635
                         const std::vector<std::string>& exec_args,
636
                         const EnvSerializeInfo* env_info,
637
                         EnvironmentFlags::Flags flags,
638
6132
                         ThreadId thread_id)
639
    : isolate_(isolate),
640
      isolate_data_(isolate_data),
641
      async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)),
642
      immediate_info_(isolate, MAYBE_FIELD_PTR(env_info, immediate_info)),
643
      tick_info_(isolate, MAYBE_FIELD_PTR(env_info, tick_info)),
644
6132
      timer_base_(uv_now(isolate_data->event_loop())),
645
      exec_argv_(exec_args),
646
      argv_(args),
647
      exec_path_(GetExecPath(args)),
648
6132
      exiting_(isolate_, 1, MAYBE_FIELD_PTR(env_info, exiting)),
649
      should_abort_on_uncaught_toggle_(
650
6132
          isolate_,
651
          1,
652
          MAYBE_FIELD_PTR(env_info, should_abort_on_uncaught_toggle)),
653
6132
      stream_base_state_(isolate_,
654
                         StreamBase::kNumStreamBaseStateFields,
655
                         MAYBE_FIELD_PTR(env_info, stream_base_state)),
656
6132
      time_origin_(PERFORMANCE_NOW()),
657
6132
      time_origin_timestamp_(GetCurrentTimeInMicroseconds()),
658
      flags_(flags),
659
6132
      thread_id_(thread_id.id == static_cast<uint64_t>(-1)
660
6132
                     ? AllocateEnvironmentThreadId().id
661



24528
                     : thread_id.id) {
662
  // We'll be creating new objects so make sure we've entered the context.
663
12264
  HandleScope handle_scope(isolate);
664
665
  // Set some flags if only kDefaultFlags was passed. This can make API version
666
  // transitions easier for embedders.
667
6132
  if (flags_ & EnvironmentFlags::kDefaultFlags) {
668
10830
    flags_ = flags_ |
669
5415
        EnvironmentFlags::kOwnsProcessState |
670
        EnvironmentFlags::kOwnsInspector;
671
  }
672
673
6132
  set_env_vars(per_process::system_environment);
674
6132
  enabled_debug_list_.Parse(env_vars(), isolate);
675
676
  // We create new copies of the per-Environment option sets, so that it is
677
  // easier to modify them after Environment creation. The defaults are
678
  // part of the per-Isolate option set, for which in turn the defaults are
679
  // part of the per-process option set.
680
12264
  options_ = std::make_shared<EnvironmentOptions>(
681
18396
      *isolate_data->options()->per_env);
682
6132
  inspector_host_port_ = std::make_shared<ExclusiveAccess<HostPort>>(
683
6132
      options_->debug_options().host_port);
684
685
6132
  if (!(flags_ & EnvironmentFlags::kOwnsProcessState)) {
686
717
    set_abort_on_uncaught_exception(false);
687
  }
688
689
#if HAVE_INSPECTOR
690
  // We can only create the inspector agent after having cloned the options.
691
6132
  inspector_agent_ = std::make_unique<inspector::Agent>(this);
692
#endif
693
694
6132
  if (tracing::AgentWriterHandle* writer = GetTracingAgentWriter()) {
695
6132
    trace_state_observer_ = std::make_unique<TrackingTraceStateObserver>(this);
696
6132
    if (TracingController* tracing_controller = writer->GetTracingController())
697
6081
      tracing_controller->AddTraceStateObserver(trace_state_observer_.get());
698
  }
699
700
6132
  destroy_async_id_list_.reserve(512);
701
702
6132
  performance_state_ = std::make_unique<performance::PerformanceState>(
703
6132
      isolate, MAYBE_FIELD_PTR(env_info, performance_state));
704
705
6132
  if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
706
6132
          TRACING_CATEGORY_NODE1(environment)) != 0) {
707
16
    auto traced_value = tracing::TracedValue::Create();
708
8
    traced_value->BeginArray("args");
709
18
    for (const std::string& arg : args) traced_value->AppendString(arg);
710
8
    traced_value->EndArray();
711
8
    traced_value->BeginArray("exec_args");
712
33
    for (const std::string& arg : exec_args) traced_value->AppendString(arg);
713
8
    traced_value->EndArray();
714

15
    TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
715
                                      "Environment",
716
                                      this,
717
                                      "args",
718
                                      std::move(traced_value));
719
  }
720
6132
}
721
722
774
Environment::Environment(IsolateData* isolate_data,
723
                         Local<Context> context,
724
                         const std::vector<std::string>& args,
725
                         const std::vector<std::string>& exec_args,
726
                         const EnvSerializeInfo* env_info,
727
                         EnvironmentFlags::Flags flags,
728
774
                         ThreadId thread_id)
729
    : Environment(isolate_data,
730
                  context->GetIsolate(),
731
                  args,
732
                  exec_args,
733
                  env_info,
734
                  flags,
735
774
                  thread_id) {
736
774
  InitializeMainContext(context, env_info);
737
774
}
738
739
6132
void Environment::InitializeMainContext(Local<Context> context,
740
                                        const EnvSerializeInfo* env_info) {
741
6132
  principal_realm_ = std::make_unique<Realm>(
742
6132
      this, context, MAYBE_FIELD_PTR(env_info, principal_realm));
743
6132
  AssignToContext(context, principal_realm_.get(), ContextInfo(""));
744
6132
  if (env_info != nullptr) {
745
5358
    DeserializeProperties(env_info);
746
  }
747
748
6132
  if (!options_->force_async_hooks_checks) {
749
1
    async_hooks_.no_force_checks();
750
  }
751
752
  // By default, always abort when --abort-on-uncaught-exception was passed.
753
6132
  should_abort_on_uncaught_toggle_[0] = 1;
754
755
  // The process is not exiting by default.
756
6132
  set_exiting(false);
757
758
6132
  performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT,
759
                           time_origin_);
760
6132
  performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
761
                           per_process::node_start_time);
762
763
6132
  if (per_process::v8_initialized) {
764
6088
    performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_V8_START,
765
                            performance::performance_v8_start);
766
  }
767
6132
}
768
769
22212
Environment::~Environment() {
770
  HandleScope handle_scope(isolate());
771
5553
  Local<Context> ctx = context();
772
773
5553
  if (Environment** interrupt_data = interrupt_data_.load()) {
774
    // There are pending RequestInterrupt() callbacks. Tell them not to run,
775
    // then force V8 to run interrupts by compiling and running an empty script
776
    // so as not to leak memory.
777
11
    *interrupt_data = nullptr;
778
779
22
    Isolate::AllowJavascriptExecutionScope allow_js_here(isolate());
780
22
    TryCatch try_catch(isolate());
781
11
    Context::Scope context_scope(ctx);
782
783
#ifdef DEBUG
784
    bool consistency_check = false;
785
    isolate()->RequestInterrupt([](Isolate*, void* data) {
786
      *static_cast<bool*>(data) = true;
787
    }, &consistency_check);
788
#endif
789
790
    Local<Script> script;
791
33
    if (Script::Compile(ctx, String::Empty(isolate())).ToLocal(&script))
792
11
      USE(script->Run(ctx));
793
794
    DCHECK(consistency_check);
795
  }
796
797
  // FreeEnvironment() should have set this.
798
5553
  CHECK(is_stopping());
799
800
5553
  if (options_->heap_snapshot_near_heap_limit > heap_limit_snapshot_taken_) {
801
    isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback,
802
                                          0);
803
  }
804
805
5553
  isolate()->GetHeapProfiler()->RemoveBuildEmbedderGraphCallback(
806
      BuildEmbedderGraph, this);
807
808
#if HAVE_INSPECTOR
809
  // Destroy inspector agent before erasing the context. The inspector
810
  // destructor depends on the context still being accessible.
811
5553
  inspector_agent_.reset();
812
#endif
813
814
5553
  ctx->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
815
                                       nullptr);
816
5553
  ctx->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kRealm, nullptr);
817
818
5553
  if (trace_state_observer_) {
819
5553
    tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
820
5553
    CHECK_NOT_NULL(writer);
821
5553
    if (TracingController* tracing_controller = writer->GetTracingController())
822
5504
      tracing_controller->RemoveTraceStateObserver(trace_state_observer_.get());
823
  }
824
825

10359
  TRACE_EVENT_NESTABLE_ASYNC_END0(
826
    TRACING_CATEGORY_NODE1(environment), "Environment", this);
827
828
  // Do not unload addons on the main thread. Some addons need to retain memory
829
  // beyond the Environment's lifetime, and unloading them early would break
830
  // them; with Worker threads, we have the opportunity to be stricter.
831
  // Also, since the main thread usually stops just before the process exits,
832
  // this is far less relevant here.
833
5553
  if (!is_main_thread()) {
834
    // Dereference all addons that were loaded into this environment.
835
725
    for (binding::DLib& addon : loaded_addons_) {
836
11
      addon.Close();
837
    }
838
  }
839
840
5553
  CHECK_EQ(base_object_count_, 0);
841
5553
}
842
843
6098
void Environment::InitializeLibuv() {
844
12196
  HandleScope handle_scope(isolate());
845
6098
  Context::Scope context_scope(context());
846
847
6098
  CHECK_EQ(0, uv_timer_init(event_loop(), timer_handle()));
848
6098
  uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle()));
849
850
6098
  CHECK_EQ(0, uv_check_init(event_loop(), immediate_check_handle()));
851
6098
  uv_unref(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
852
853
6098
  CHECK_EQ(0, uv_idle_init(event_loop(), immediate_idle_handle()));
854
855
6098
  CHECK_EQ(0, uv_check_start(immediate_check_handle(), CheckImmediate));
856
857
  // Inform V8's CPU profiler when we're idle.  The profiler is sampling-based
858
  // but not all samples are created equal; mark the wall clock time spent in
859
  // epoll_wait() and friends so profiling tools can filter it out.  The samples
860
  // still end up in v8.log but with state=IDLE rather than state=EXTERNAL.
861
6098
  CHECK_EQ(0, uv_prepare_init(event_loop(), &idle_prepare_handle_));
862
6098
  CHECK_EQ(0, uv_check_init(event_loop(), &idle_check_handle_));
863
864
25186
  CHECK_EQ(0, uv_async_init(
865
      event_loop(),
866
      &task_queues_async_,
867
      [](uv_async_t* async) {
868
        Environment* env = ContainerOf(
869
            &Environment::task_queues_async_, async);
870
        HandleScope handle_scope(env->isolate());
871
        Context::Scope context_scope(env->context());
872
        env->RunAndClearNativeImmediates();
873
      }));
874
6098
  uv_unref(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
875
6098
  uv_unref(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
876
6098
  uv_unref(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
877
878
  {
879
12196
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
880
6098
    task_queues_async_initialized_ = true;
881

12196
    if (native_immediates_threadsafe_.size() > 0 ||
882
6098
        native_immediates_interrupts_.size() > 0) {
883
5355
      uv_async_send(&task_queues_async_);
884
    }
885
  }
886
887
  // Register clean-up cb to be called to clean up the handles
888
  // when the environment is freed, note that they are not cleaned in
889
  // the one environment per process setup, but will be called in
890
  // FreeEnvironment.
891
6098
  RegisterHandleCleanups();
892
893
6098
  StartProfilerIdleNotifier();
894
6098
}
895
896
380
void Environment::ExitEnv() {
897
380
  set_can_call_into_js(false);
898
380
  set_stopping(true);
899
380
  isolate_->TerminateExecution();
900
760
  SetImmediateThreadsafe([](Environment* env) { uv_stop(env->event_loop()); });
901
380
}
902
903
6098
void Environment::RegisterHandleCleanups() {
904
6098
  HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
905
33114
                                        void* arg) {
906
33114
    handle->data = env;
907
908
33114
    env->CloseHandle(handle, [](uv_handle_t* handle) {
909
#ifdef DEBUG
910
      memset(handle, 0xab, uv_handle_size(handle->type));
911
#endif
912
33114
    });
913
33114
  };
914
915
36588
  auto register_handle = [&](uv_handle_t* handle) {
916
36588
    RegisterHandleCleanup(handle, close_and_finish, nullptr);
917
42686
  };
918
6098
  register_handle(reinterpret_cast<uv_handle_t*>(timer_handle()));
919
6098
  register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
920
6098
  register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
921
6098
  register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
922
6098
  register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
923
6098
  register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
924
6098
}
925
926
11100
void Environment::CleanupHandles() {
927
  {
928
11100
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
929
11100
    task_queues_async_initialized_ = false;
930
  }
931
932
  Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(),
933
22200
      Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
934
935
11100
  RunAndClearNativeImmediates(true /* skip unrefed SetImmediate()s */);
936
937
11261
  for (ReqWrapBase* request : req_wrap_queue_)
938
161
    request->Cancel();
939
940
15318
  for (HandleWrap* handle : handle_wrap_queue_)
941
8436
    handle->Close();
942
943
44214
  for (HandleCleanup& hc : handle_cleanup_queue_)
944
33114
    hc.cb_(this, hc.handle_, hc.arg_);
945
11100
  handle_cleanup_queue_.clear();
946
947
10418
  while (handle_cleanup_waiting_ != 0 ||
948

32620
         request_waiting_ != 0 ||
949
11102
         !handle_wrap_queue_.IsEmpty()) {
950
10418
    uv_run(event_loop(), UV_RUN_ONCE);
951
  }
952
11100
}
953
954
6098
void Environment::StartProfilerIdleNotifier() {
955
6098
  uv_prepare_start(&idle_prepare_handle_, [](uv_prepare_t* handle) {
956
199397
    Environment* env = ContainerOf(&Environment::idle_prepare_handle_, handle);
957
199397
    env->isolate()->SetIdle(true);
958
199397
  });
959
6098
  uv_check_start(&idle_check_handle_, [](uv_check_t* handle) {
960
199187
    Environment* env = ContainerOf(&Environment::idle_check_handle_, handle);
961
199187
    env->isolate()->SetIdle(false);
962
199187
  });
963
6098
}
964
965
715555
void Environment::PrintSyncTrace() const {
966
715555
  if (!trace_sync_io_) return;
967
968
2
  HandleScope handle_scope(isolate());
969
970
1
  fprintf(
971
      stderr, "(node:%d) WARNING: Detected use of sync API\n", uv_os_getpid());
972
1
  PrintStackTrace(isolate(),
973
                  StackTrace::CurrentStackTrace(
974
                      isolate(), stack_trace_limit(), StackTrace::kDetailed));
975
}
976
977
5195
MaybeLocal<Value> Environment::RunSnapshotSerializeCallback() const {
978
5195
  EscapableHandleScope handle_scope(isolate());
979
10390
  if (!snapshot_serialize_callback().IsEmpty()) {
980
    Context::Scope context_scope(context());
981
    return handle_scope.EscapeMaybe(snapshot_serialize_callback()->Call(
982
        context(), v8::Undefined(isolate()), 0, nullptr));
983
  }
984
10390
  return handle_scope.Escape(Undefined(isolate()));
985
}
986
987
MaybeLocal<Value> Environment::RunSnapshotDeserializeMain() const {
988
  EscapableHandleScope handle_scope(isolate());
989
  if (!snapshot_deserialize_main().IsEmpty()) {
990
    Context::Scope context_scope(context());
991
    return handle_scope.EscapeMaybe(snapshot_deserialize_main()->Call(
992
        context(), v8::Undefined(isolate()), 0, nullptr));
993
  }
994
  return handle_scope.Escape(Undefined(isolate()));
995
}
996
997
5553
void Environment::RunCleanup() {
998
5553
  started_cleanup_ = true;
999

15911
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
1000
5553
  bindings_.clear();
1001
5553
  CleanupHandles();
1002
1003
16655
  while (!cleanup_hooks_.empty() ||
1004
11108
         native_immediates_.size() > 0 ||
1005

22208
         native_immediates_threadsafe_.size() > 0 ||
1006
5553
         native_immediates_interrupts_.size() > 0) {
1007
    // Copy into a vector, since we can't sort an unordered_set in-place.
1008
    std::vector<CleanupHookCallback> callbacks(
1009
11094
        cleanup_hooks_.begin(), cleanup_hooks_.end());
1010
    // We can't erase the copied elements from `cleanup_hooks_` yet, because we
1011
    // need to be able to check whether they were un-scheduled by another hook.
1012
1013
5547
    std::sort(callbacks.begin(), callbacks.end(),
1014
1269593
              [](const CleanupHookCallback& a, const CleanupHookCallback& b) {
1015
      // Sort in descending order so that the most recently inserted callbacks
1016
      // are run first.
1017
1269593
      return a.insertion_order_counter_ > b.insertion_order_counter_;
1018
    });
1019
1020
190627
    for (const CleanupHookCallback& cb : callbacks) {
1021
185080
      if (cleanup_hooks_.count(cb) == 0) {
1022
        // This hook was removed from the `cleanup_hooks_` set during another
1023
        // hook that was run earlier. Nothing to do here.
1024
1021
        continue;
1025
      }
1026
1027
184059
      cb.fn_(cb.arg_);
1028
184059
      cleanup_hooks_.erase(cb);
1029
    }
1030
5547
    CleanupHandles();
1031
  }
1032
1033
5556
  for (const int fd : unmanaged_fds_) {
1034
    uv_fs_t close_req;
1035
3
    uv_fs_close(nullptr, &close_req, fd, nullptr);
1036
3
    uv_fs_req_cleanup(&close_req);
1037
  }
1038
5553
}
1039
1040
6206
void Environment::RunAtExitCallbacks() {
1041

17773
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "AtExit");
1042
18432
  for (ExitCallback at_exit : at_exit_functions_) {
1043
12226
    at_exit.cb_(at_exit.arg_);
1044
  }
1045
6206
  at_exit_functions_.clear();
1046
6206
}
1047
1048
12254
void Environment::AtExit(void (*cb)(void* arg), void* arg) {
1049
12254
  at_exit_functions_.push_front(ExitCallback{cb, arg});
1050
12254
}
1051
1052
237482
void Environment::RunAndClearInterrupts() {
1053
237482
  while (native_immediates_interrupts_.size() > 0) {
1054
10555
    NativeImmediateQueue queue;
1055
    {
1056
21114
      Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
1057
10557
      queue.ConcatMove(std::move(native_immediates_interrupts_));
1058
    }
1059
10557
    DebugSealHandleScope seal_handle_scope(isolate());
1060
1061
21127
    while (auto head = queue.Shift())
1062
21142
      head->Call(this);
1063
  }
1064
226925
}
1065
1066
216652
void Environment::RunAndClearNativeImmediates(bool only_refed) {
1067

438333
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment),
1068
               "RunAndClearNativeImmediates");
1069
433297
  HandleScope handle_scope(isolate_);
1070
433297
  InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 });
1071
1072
216652
  size_t ref_count = 0;
1073
1074
  // Handle interrupts first. These functions are not allowed to throw
1075
  // exceptions, so we do not need to handle that.
1076
216652
  RunAndClearInterrupts();
1077
1078
433300
  auto drain_list = [&](NativeImmediateQueue* queue) {
1079
866594
    TryCatchScope try_catch(this);
1080
433300
    DebugSealHandleScope seal_handle_scope(isolate());
1081
493092
    while (auto head = queue->Shift()) {
1082
59799
      bool is_refed = head->flags() & CallbackFlags::kRefed;
1083
59799
      if (is_refed)
1084
34973
        ref_count++;
1085
1086

59799
      if (is_refed || !only_refed)
1087
59559
        head->Call(this);
1088
1089
59794
      head.reset();  // Destroy now so that this is also observed by try_catch.
1090
1091
59794
      if (UNLIKELY(try_catch.HasCaught())) {
1092

2
        if (!try_catch.HasTerminated() && can_call_into_js())
1093
2
          errors::TriggerUncaughtException(isolate(), try_catch);
1094
1095
1
        return true;
1096
      }
1097
59792
    }
1098
433293
    return false;
1099
216651
  };
1100
216651
  while (drain_list(&native_immediates_)) {}
1101
1102
216648
  immediate_info()->ref_count_dec(ref_count);
1103
1104
216648
  if (immediate_info()->ref_count() == 0)
1105
174554
    ToggleImmediateRef(false);
1106
1107
  // It is safe to check .size() first, because there is a causal relationship
1108
  // between pushes to the threadsafe immediate list and this function being
1109
  // called. For the common case, it's worth checking the size first before
1110
  // establishing a mutex lock.
1111
  // This is intentionally placed after the `ref_count` handling, because when
1112
  // refed threadsafe immediates are created, they are not counted towards the
1113
  // count in immediate_info() either.
1114
216645
  NativeImmediateQueue threadsafe_immediates;
1115
216648
  if (native_immediates_threadsafe_.size() > 0) {
1116
2176
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
1117
1088
    threadsafe_immediates.ConcatMove(std::move(native_immediates_threadsafe_));
1118
  }
1119
216649
  while (drain_list(&threadsafe_immediates)) {}
1120
216645
}
1121
1122
10578
void Environment::RequestInterruptFromV8() {
1123
  // The Isolate may outlive the Environment, so some logic to handle the
1124
  // situation in which the Environment is destroyed before the handler runs
1125
  // is required.
1126
1127
  // We allocate a new pointer to a pointer to this Environment instance, and
1128
  // try to set it as interrupt_data_. If interrupt_data_ was already set, then
1129
  // callbacks are already scheduled to run and we can delete our own pointer
1130
  // and just return. If it was nullptr previously, the Environment** is stored;
1131
  // ~Environment sets the Environment* contained in it to nullptr, so that
1132
  // the callback can check whether ~Environment has already run and it is thus
1133
  // not safe to access the Environment instance itself.
1134
10578
  Environment** interrupt_data = new Environment*(this);
1135
10578
  Environment** dummy = nullptr;
1136
10578
  if (!interrupt_data_.compare_exchange_strong(dummy, interrupt_data)) {
1137
398
    delete interrupt_data;
1138
398
    return;  // Already scheduled.
1139
  }
1140
1141
10180
  isolate()->RequestInterrupt([](Isolate* isolate, void* data) {
1142
10169
    std::unique_ptr<Environment*> env_ptr { static_cast<Environment**>(data) };
1143
10169
    Environment* env = *env_ptr;
1144
10169
    if (env == nullptr) {
1145
      // The Environment has already been destroyed. That should be okay; any
1146
      // callback added before the Environment shuts down would have been
1147
      // handled during cleanup.
1148
11
      return;
1149
    }
1150
10158
    env->interrupt_data_.store(nullptr);
1151
10158
    env->RunAndClearInterrupts();
1152
  }, interrupt_data);
1153
}
1154
1155
9518
void Environment::ScheduleTimer(int64_t duration_ms) {
1156
9518
  if (started_cleanup_) return;
1157
9518
  uv_timer_start(timer_handle(), RunTimers, duration_ms, 0);
1158
}
1159
1160
3740
void Environment::ToggleTimerRef(bool ref) {
1161
3740
  if (started_cleanup_) return;
1162
1163
3740
  if (ref) {
1164
2471
    uv_ref(reinterpret_cast<uv_handle_t*>(timer_handle()));
1165
  } else {
1166
1269
    uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle()));
1167
  }
1168
}
1169
1170
7510
void Environment::RunTimers(uv_timer_t* handle) {
1171
7510
  Environment* env = Environment::from_timer_handle(handle);
1172

8021
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunTimers");
1173
1174
7510
  if (!env->can_call_into_js())
1175
    return;
1176
1177
7510
  HandleScope handle_scope(env->isolate());
1178
7510
  Context::Scope context_scope(env->context());
1179
1180
7510
  Local<Object> process = env->process_object();
1181
7510
  InternalCallbackScope scope(env, process, {0, 0});
1182
1183
7510
  Local<Function> cb = env->timers_callback_function();
1184
  MaybeLocal<Value> ret;
1185
7510
  Local<Value> arg = env->GetNow();
1186
  // This code will loop until all currently due timers will process. It is
1187
  // impossible for us to end up in an infinite loop due to how the JS-side
1188
  // is structured.
1189
33
  do {
1190
7543
    TryCatchScope try_catch(env);
1191
7543
    try_catch.SetVerbose(true);
1192
7543
    ret = cb->Call(env->context(), process, 1, &arg);
1193

7534
  } while (ret.IsEmpty() && env->can_call_into_js());
1194
1195
  // NOTE(apapirovski): If it ever becomes possible that `call_into_js` above
1196
  // is reset back to `true` after being previously set to `false` then this
1197
  // code becomes invalid and needs to be rewritten. Otherwise catastrophic
1198
  // timers corruption will occur and all timers behaviour will become
1199
  // entirely unpredictable.
1200
7501
  if (ret.IsEmpty())
1201
8
    return;
1202
1203
  // To allow for less JS-C++ boundary crossing, the value returned from JS
1204
  // serves a few purposes:
1205
  // 1. If it's 0, no more timers exist and the handle should be unrefed
1206
  // 2. If it's > 0, the value represents the next timer's expiry and there
1207
  //    is at least one timer remaining that is refed.
1208
  // 3. If it's < 0, the absolute value represents the next timer's expiry
1209
  //    and there are no timers that are refed.
1210
  int64_t expiry_ms =
1211
7493
      ret.ToLocalChecked()->IntegerValue(env->context()).FromJust();
1212
1213
7493
  uv_handle_t* h = reinterpret_cast<uv_handle_t*>(handle);
1214
1215
7493
  if (expiry_ms != 0) {
1216
    int64_t duration_ms =
1217
6477
        llabs(expiry_ms) - (uv_now(env->event_loop()) - env->timer_base());
1218
1219
6477
    env->ScheduleTimer(duration_ms > 0 ? duration_ms : 1);
1220
1221
6477
    if (expiry_ms > 0)
1222
5814
      uv_ref(h);
1223
    else
1224
663
      uv_unref(h);
1225
  } else {
1226
1016
    uv_unref(h);
1227
  }
1228
}
1229
1230
1231
199187
void Environment::CheckImmediate(uv_check_t* handle) {
1232
199187
  Environment* env = Environment::from_immediate_check_handle(handle);
1233

202254
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "CheckImmediate");
1234
1235
199187
  HandleScope scope(env->isolate());
1236
199187
  Context::Scope context_scope(env->context());
1237
1238
199187
  env->RunAndClearNativeImmediates();
1239
1240

199187
  if (env->immediate_info()->count() == 0 || !env->can_call_into_js())
1241
157660
    return;
1242
1243
951
  do {
1244
42474
    MakeCallback(env->isolate(),
1245
                 env->process_object(),
1246
                 env->immediate_callback_function(),
1247
                 0,
1248
                 nullptr,
1249
42478
                 {0, 0}).ToLocalChecked();
1250

42474
  } while (env->immediate_info()->has_outstanding() && env->can_call_into_js());
1251
1252
41523
  if (env->immediate_info()->ref_count() == 0)
1253
4734
    env->ToggleImmediateRef(false);
1254
}
1255
1256
244150
void Environment::ToggleImmediateRef(bool ref) {
1257
244150
  if (started_cleanup_) return;
1258
1259
233127
  if (ref) {
1260
    // Idle handle is needed only to stop the event loop from blocking in poll.
1261
64838
    uv_idle_start(immediate_idle_handle(), [](uv_idle_t*){ });
1262
  } else {
1263
168289
    uv_idle_stop(immediate_idle_handle());
1264
  }
1265
}
1266
1267
1268
46420
Local<Value> Environment::GetNow() {
1269
46420
  uv_update_time(event_loop());
1270
46420
  uint64_t now = uv_now(event_loop());
1271
46420
  CHECK_GE(now, timer_base());
1272
46420
  now -= timer_base();
1273
46420
  if (now <= 0xffffffff)
1274
92840
    return Integer::NewFromUnsigned(isolate(), static_cast<uint32_t>(now));
1275
  else
1276
    return Number::New(isolate(), static_cast<double>(now));
1277
}
1278
1279
28
void CollectExceptionInfo(Environment* env,
1280
                          Local<Object> obj,
1281
                          int errorno,
1282
                          const char* err_string,
1283
                          const char* syscall,
1284
                          const char* message,
1285
                          const char* path,
1286
                          const char* dest) {
1287
28
  obj->Set(env->context(),
1288
           env->errno_string(),
1289
112
           Integer::New(env->isolate(), errorno)).Check();
1290
1291
28
  obj->Set(env->context(), env->code_string(),
1292
84
           OneByteString(env->isolate(), err_string)).Check();
1293
1294
28
  if (message != nullptr) {
1295
28
    obj->Set(env->context(), env->message_string(),
1296
112
             OneByteString(env->isolate(), message)).Check();
1297
  }
1298
1299
  Local<Value> path_buffer;
1300
28
  if (path != nullptr) {
1301
    path_buffer =
1302
      Buffer::Copy(env->isolate(), path, strlen(path)).ToLocalChecked();
1303
    obj->Set(env->context(), env->path_string(), path_buffer).Check();
1304
  }
1305
1306
  Local<Value> dest_buffer;
1307
28
  if (dest != nullptr) {
1308
    dest_buffer =
1309
      Buffer::Copy(env->isolate(), dest, strlen(dest)).ToLocalChecked();
1310
    obj->Set(env->context(), env->dest_string(), dest_buffer).Check();
1311
  }
1312
1313
28
  if (syscall != nullptr) {
1314
28
    obj->Set(env->context(), env->syscall_string(),
1315
112
             OneByteString(env->isolate(), syscall)).Check();
1316
  }
1317
28
}
1318
1319
28
void Environment::CollectUVExceptionInfo(Local<Value> object,
1320
                                         int errorno,
1321
                                         const char* syscall,
1322
                                         const char* message,
1323
                                         const char* path,
1324
                                         const char* dest) {
1325

28
  if (!object->IsObject() || errorno == 0)
1326
    return;
1327
1328
28
  Local<Object> obj = object.As<Object>();
1329
28
  const char* err_string = uv_err_name(errorno);
1330
1331

28
  if (message == nullptr || message[0] == '\0') {
1332
28
    message = uv_strerror(errorno);
1333
  }
1334
1335
28
  node::CollectExceptionInfo(this, obj, errorno, err_string,
1336
                             syscall, message, path, dest);
1337
}
1338
1339
6132
ImmediateInfo::ImmediateInfo(Isolate* isolate, const SerializeInfo* info)
1340
6132
    : fields_(isolate, kFieldsCount, MAYBE_FIELD_PTR(info, fields)) {}
1341
1342
6
ImmediateInfo::SerializeInfo ImmediateInfo::Serialize(
1343
    Local<Context> context, SnapshotCreator* creator) {
1344
6
  return {fields_.Serialize(context, creator)};
1345
}
1346
1347
5358
void ImmediateInfo::Deserialize(Local<Context> context) {
1348
5358
  fields_.Deserialize(context);
1349
5358
}
1350
1351
6
std::ostream& operator<<(std::ostream& output,
1352
                         const ImmediateInfo::SerializeInfo& i) {
1353
6
  output << "{ " << i.fields << " }";
1354
6
  return output;
1355
}
1356
1357
24
void ImmediateInfo::MemoryInfo(MemoryTracker* tracker) const {
1358
24
  tracker->TrackField("fields", fields_);
1359
24
}
1360
1361
6
TickInfo::SerializeInfo TickInfo::Serialize(Local<Context> context,
1362
                                            SnapshotCreator* creator) {
1363
6
  return {fields_.Serialize(context, creator)};
1364
}
1365
1366
5358
void TickInfo::Deserialize(Local<Context> context) {
1367
5358
  fields_.Deserialize(context);
1368
5358
}
1369
1370
6
std::ostream& operator<<(std::ostream& output,
1371
                         const TickInfo::SerializeInfo& i) {
1372
6
  output << "{ " << i.fields << " }";
1373
6
  return output;
1374
}
1375
1376
24
void TickInfo::MemoryInfo(MemoryTracker* tracker) const {
1377
24
  tracker->TrackField("fields", fields_);
1378
24
}
1379
1380
6132
TickInfo::TickInfo(Isolate* isolate, const SerializeInfo* info)
1381
    : fields_(
1382
6132
          isolate, kFieldsCount, info == nullptr ? nullptr : &(info->fields)) {}
1383
1384
6132
AsyncHooks::AsyncHooks(Isolate* isolate, const SerializeInfo* info)
1385
    : async_ids_stack_(isolate, 16 * 2, MAYBE_FIELD_PTR(info, async_ids_stack)),
1386
      fields_(isolate, kFieldsCount, MAYBE_FIELD_PTR(info, fields)),
1387
      async_id_fields_(
1388
          isolate, kUidFieldsCount, MAYBE_FIELD_PTR(info, async_id_fields)),
1389

6132
      info_(info) {
1390
12264
  HandleScope handle_scope(isolate);
1391
6132
  if (info == nullptr) {
1392
774
    clear_async_id_stack();
1393
1394
    // Always perform async_hooks checks, not just when async_hooks is enabled.
1395
    // TODO(AndreasMadsen): Consider removing this for LTS releases.
1396
    // See discussion in https://github.com/nodejs/node/pull/15454
1397
    // When removing this, do it by reverting the commit. Otherwise the test
1398
    // and flag changes won't be included.
1399
774
    fields_[kCheck] = 1;
1400
1401
    // kDefaultTriggerAsyncId should be -1, this indicates that there is no
1402
    // specified default value and it should fallback to the executionAsyncId.
1403
    // 0 is not used as the magic value, because that indicates a missing
1404
    // context which is different from a default context.
1405
774
    async_id_fields_[AsyncHooks::kDefaultTriggerAsyncId] = -1;
1406
1407
    // kAsyncIdCounter should start at 1 because that'll be the id the execution
1408
    // context during bootstrap (code that runs before entering uv_run()).
1409
774
    async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1;
1410
  }
1411
6132
}
1412
1413
5358
void AsyncHooks::Deserialize(Local<Context> context) {
1414
5358
  async_ids_stack_.Deserialize(context);
1415
5358
  fields_.Deserialize(context);
1416
5358
  async_id_fields_.Deserialize(context);
1417
1418
  Local<Array> js_execution_async_resources;
1419
5358
  if (info_->js_execution_async_resources != 0) {
1420
    js_execution_async_resources =
1421
5358
        context->GetDataFromSnapshotOnce<Array>(
1422
16074
            info_->js_execution_async_resources).ToLocalChecked();
1423
  } else {
1424
    js_execution_async_resources = Array::New(context->GetIsolate());
1425
  }
1426
5358
  js_execution_async_resources_.Reset(
1427
      context->GetIsolate(), js_execution_async_resources);
1428
1429
  // The native_execution_async_resources_ field requires v8::Local<> instances
1430
  // for async calls whose resources were on the stack as JS objects when they
1431
  // were entered. We cannot recreate this here; however, storing these values
1432
  // on the JS equivalent gives the same result, so we do that instead.
1433
5358
  for (size_t i = 0; i < info_->native_execution_async_resources.size(); ++i) {
1434
    if (info_->native_execution_async_resources[i] == SIZE_MAX)
1435
      continue;
1436
    Local<Object> obj = context->GetDataFromSnapshotOnce<Object>(
1437
                                   info_->native_execution_async_resources[i])
1438
                               .ToLocalChecked();
1439
    js_execution_async_resources->Set(context, i, obj).Check();
1440
  }
1441
5358
  info_ = nullptr;
1442
5358
}
1443
1444
6
std::ostream& operator<<(std::ostream& output,
1445
                         const AsyncHooks::SerializeInfo& i) {
1446
  output << "{\n"
1447
6
         << "  " << i.async_ids_stack << ",  // async_ids_stack\n"
1448
6
         << "  " << i.fields << ",  // fields\n"
1449
6
         << "  " << i.async_id_fields << ",  // async_id_fields\n"
1450
6
         << "  " << i.js_execution_async_resources
1451
         << ",  // js_execution_async_resources\n"
1452
6
         << "  " << i.native_execution_async_resources
1453
         << ",  // native_execution_async_resources\n"
1454
6
         << "}";
1455
6
  return output;
1456
}
1457
1458
6
AsyncHooks::SerializeInfo AsyncHooks::Serialize(Local<Context> context,
1459
                                                SnapshotCreator* creator) {
1460
6
  SerializeInfo info;
1461
  // TODO(joyeecheung): some of these probably don't need to be serialized.
1462
6
  info.async_ids_stack = async_ids_stack_.Serialize(context, creator);
1463
6
  info.fields = fields_.Serialize(context, creator);
1464
6
  info.async_id_fields = async_id_fields_.Serialize(context, creator);
1465
6
  if (!js_execution_async_resources_.IsEmpty()) {
1466
6
    info.js_execution_async_resources = creator->AddData(
1467
        context, js_execution_async_resources_.Get(context->GetIsolate()));
1468
6
    CHECK_NE(info.js_execution_async_resources, 0);
1469
  } else {
1470
    info.js_execution_async_resources = 0;
1471
  }
1472
1473
6
  info.native_execution_async_resources.resize(
1474
      native_execution_async_resources_.size());
1475
6
  for (size_t i = 0; i < native_execution_async_resources_.size(); i++) {
1476
    info.native_execution_async_resources[i] =
1477
        native_execution_async_resources_[i].IsEmpty() ? SIZE_MAX :
1478
            creator->AddData(
1479
                context,
1480
                native_execution_async_resources_[i]);
1481
  }
1482
6
  CHECK_EQ(contexts_.size(), 1);
1483

12
  CHECK_EQ(contexts_[0], env()->context());
1484
6
  CHECK(js_promise_hooks_[0].IsEmpty());
1485
6
  CHECK(js_promise_hooks_[1].IsEmpty());
1486
6
  CHECK(js_promise_hooks_[2].IsEmpty());
1487
6
  CHECK(js_promise_hooks_[3].IsEmpty());
1488
1489
6
  return info;
1490
}
1491
1492
24
void AsyncHooks::MemoryInfo(MemoryTracker* tracker) const {
1493
24
  tracker->TrackField("async_ids_stack", async_ids_stack_);
1494
24
  tracker->TrackField("fields", fields_);
1495
24
  tracker->TrackField("async_id_fields", async_id_fields_);
1496
24
  tracker->TrackField("js_promise_hooks", js_promise_hooks_);
1497
24
}
1498
1499
4
void AsyncHooks::grow_async_ids_stack() {
1500
4
  async_ids_stack_.reserve(async_ids_stack_.Length() * 3);
1501
1502
4
  env()->async_hooks_binding()->Set(
1503
      env()->context(),
1504
      env()->async_ids_stack_string(),
1505
12
      async_ids_stack_.GetJSArray()).Check();
1506
4
}
1507
1508
4
void AsyncHooks::FailWithCorruptedAsyncStack(double expected_async_id) {
1509
4
  fprintf(stderr,
1510
          "Error: async hook stack has become corrupted ("
1511
          "actual: %.f, expected: %.f)\n",
1512
          async_id_fields_.GetValue(kExecutionAsyncId),
1513
          expected_async_id);
1514
4
  DumpBacktrace(stderr);
1515
4
  fflush(stderr);
1516
4
  if (!env()->abort_on_uncaught_exception())
1517
4
    exit(1);
1518
  fprintf(stderr, "\n");
1519
  fflush(stderr);
1520
  ABORT_NO_BACKTRACE();
1521
}
1522
1523
636
void Environment::Exit(int exit_code) {
1524
636
  if (options()->trace_exit) {
1525
4
    HandleScope handle_scope(isolate());
1526
    Isolate::DisallowJavascriptExecutionScope disallow_js(
1527
4
        isolate(), Isolate::DisallowJavascriptExecutionScope::CRASH_ON_FAILURE);
1528
1529
2
    if (is_main_thread()) {
1530
1
      fprintf(stderr, "(node:%d) ", uv_os_getpid());
1531
    } else {
1532
1
      fprintf(stderr, "(node:%d, thread:%" PRIu64 ") ",
1533
              uv_os_getpid(), thread_id());
1534
    }
1535
1536
2
    fprintf(
1537
        stderr, "WARNING: Exited the environment with code %d\n", exit_code);
1538
2
    PrintStackTrace(isolate(),
1539
                    StackTrace::CurrentStackTrace(
1540
                        isolate(), stack_trace_limit(), StackTrace::kDetailed));
1541
  }
1542
636
  process_exit_handler_(this, exit_code);
1543
64
}
1544
1545
6153
void Environment::stop_sub_worker_contexts() {
1546
  DCHECK_EQ(Isolate::GetCurrent(), isolate());
1547
1548
6153
  while (!sub_worker_contexts_.empty()) {
1549
28
    Worker* w = *sub_worker_contexts_.begin();
1550
28
    remove_sub_worker_context(w);
1551
28
    w->Exit(1);
1552
28
    w->JoinThread();
1553
  }
1554
6125
}
1555
1556
10
Environment* Environment::worker_parent_env() const {
1557
10
  if (worker_context() == nullptr) return nullptr;
1558
  return worker_context()->env();
1559
}
1560
1561
63129
void Environment::AddUnmanagedFd(int fd) {
1562
63129
  if (!tracks_unmanaged_fds()) return;
1563
2722
  auto result = unmanaged_fds_.insert(fd);
1564
2722
  if (!result.second) {
1565
    ProcessEmitWarning(
1566
1
        this, "File descriptor %d opened in unmanaged mode twice", fd);
1567
  }
1568
}
1569
1570
62739
void Environment::RemoveUnmanagedFd(int fd) {
1571
62739
  if (!tracks_unmanaged_fds()) return;
1572
2719
  size_t removed_count = unmanaged_fds_.erase(fd);
1573
2719
  if (removed_count == 0) {
1574
    ProcessEmitWarning(
1575
1
        this, "File descriptor %d closed but not opened in unmanaged mode", fd);
1576
  }
1577
}
1578
1579
5163
void Environment::PrintInfoForSnapshotIfDebug() {
1580
10326
  if (enabled_debug_list()->enabled(DebugCategory::MKSNAPSHOT)) {
1581
    fprintf(stderr, "BaseObjects at the exit of the Environment:\n");
1582
    PrintAllBaseObjects();
1583
    fprintf(stderr, "\nNative modules without cache:\n");
1584
    for (const auto& s : builtins_without_cache) {
1585
      fprintf(stderr, "%s\n", s.c_str());
1586
    }
1587
    fprintf(stderr, "\nNative modules with cache:\n");
1588
    for (const auto& s : builtins_with_cache) {
1589
      fprintf(stderr, "%s\n", s.c_str());
1590
    }
1591
    fprintf(stderr, "\nStatic bindings (need to be registered):\n");
1592
    for (const auto mod : internal_bindings) {
1593
      fprintf(stderr, "%s:%s\n", mod->nm_filename, mod->nm_modname);
1594
    }
1595
  }
1596
5163
}
1597
1598
void Environment::PrintAllBaseObjects() {
1599
  size_t i = 0;
1600
  std::cout << "BaseObjects\n";
1601
  ForEachBaseObject([&](BaseObject* obj) {
1602
    std::cout << "#" << i++ << " " << obj << ": " <<
1603
      obj->MemoryInfoName() << "\n";
1604
  });
1605
}
1606
1607
5163
void Environment::VerifyNoStrongBaseObjects() {
1608
  // When a process exits cleanly, i.e. because the event loop ends up without
1609
  // things to wait for, the Node.js objects that are left on the heap should
1610
  // be:
1611
  //
1612
  //   1. weak, i.e. ready for garbage collection once no longer referenced, or
1613
  //   2. detached, i.e. scheduled for destruction once no longer referenced, or
1614
  //   3. an unrefed libuv handle, i.e. does not keep the event loop alive, or
1615
  //   4. an inactive libuv handle (essentially the same here)
1616
  //
1617
  // There are a few exceptions to this rule, but generally, if there are
1618
  // C++-backed Node.js objects on the heap that do not fall into the above
1619
  // categories, we may be looking at a potential memory leak. Most likely,
1620
  // the cause is a missing MakeWeak() call on the corresponding object.
1621
  //
1622
  // In order to avoid this kind of problem, we check the list of BaseObjects
1623
  // for these criteria. Currently, we only do so when explicitly instructed to
1624
  // or when in debug mode (where --verify-base-objects is always-on).
1625
1626
5163
  if (!options()->verify_base_objects) return;
1627
1628
  ForEachBaseObject([](BaseObject* obj) {
1629
    if (obj->IsNotIndicativeOfMemoryLeakAtExit()) return;
1630
    fprintf(stderr, "Found bad BaseObject during clean exit: %s\n",
1631
            obj->MemoryInfoName().c_str());
1632
    fflush(stderr);
1633
    ABORT();
1634
  });
1635
}
1636
1637
6
EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) {
1638
6
  EnvSerializeInfo info;
1639
6
  Local<Context> ctx = context();
1640
1641
  // Currently all modules are compiled without cache in builtin snapshot
1642
  // builder.
1643
12
  info.builtins = std::vector<std::string>(builtins_without_cache.begin(),
1644
6
                                           builtins_without_cache.end());
1645
1646
6
  info.async_hooks = async_hooks_.Serialize(ctx, creator);
1647
6
  info.immediate_info = immediate_info_.Serialize(ctx, creator);
1648
6
  info.tick_info = tick_info_.Serialize(ctx, creator);
1649
6
  info.performance_state = performance_state_->Serialize(ctx, creator);
1650
6
  info.exiting = exiting_.Serialize(ctx, creator);
1651
6
  info.stream_base_state = stream_base_state_.Serialize(ctx, creator);
1652
6
  info.should_abort_on_uncaught_toggle =
1653
6
      should_abort_on_uncaught_toggle_.Serialize(ctx, creator);
1654
1655
  // Do this after other creator->AddData() calls so that Snapshotable objects
1656
  // can use 0 to indicate that a SnapshotIndex is invalid.
1657
6
  SerializeSnapshotableObjects(this, creator, &info);
1658
1659
6
  info.principal_realm = principal_realm_->Serialize(creator);
1660
6
  return info;
1661
}
1662
1663
21432
void Environment::EnqueueDeserializeRequest(DeserializeRequestCallback cb,
1664
                                            Local<Object> holder,
1665
                                            int index,
1666
                                            InternalFieldInfoBase* info) {
1667
  DCHECK_EQ(index, BaseObject::kEmbedderType);
1668
42864
  DeserializeRequest request{cb, {isolate(), holder}, index, info};
1669
21432
  deserialize_requests_.push_back(std::move(request));
1670
21432
}
1671
1672
5358
void Environment::RunDeserializeRequests() {
1673
10716
  HandleScope scope(isolate());
1674
5358
  Local<Context> ctx = context();
1675
5358
  Isolate* is = isolate();
1676
26790
  while (!deserialize_requests_.empty()) {
1677
42864
    DeserializeRequest request(std::move(deserialize_requests_.front()));
1678
21432
    deserialize_requests_.pop_front();
1679
21432
    Local<Object> holder = request.holder.Get(is);
1680
21432
    request.cb(ctx, holder, request.index, request.info);
1681
    request.holder.Reset();
1682
21432
    request.info->Delete();
1683
  }
1684
5358
}
1685
1686
5358
void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
1687
5358
  Local<Context> ctx = context();
1688
1689
5358
  RunDeserializeRequests();
1690
1691
5358
  builtins_in_snapshot = info->builtins;
1692
5358
  async_hooks_.Deserialize(ctx);
1693
5358
  immediate_info_.Deserialize(ctx);
1694
5358
  tick_info_.Deserialize(ctx);
1695
5358
  performance_state_->Deserialize(ctx);
1696
5358
  exiting_.Deserialize(ctx);
1697
5358
  stream_base_state_.Deserialize(ctx);
1698
5358
  should_abort_on_uncaught_toggle_.Deserialize(ctx);
1699
1700
5358
  principal_realm_->DeserializeProperties(&info->principal_realm);
1701
1702
5358
  if (enabled_debug_list_.enabled(DebugCategory::MKSNAPSHOT)) {
1703
    fprintf(stderr, "deserializing...\n");
1704
    std::cerr << *info << "\n";
1705
  }
1706
5358
}
1707
1708
1
uint64_t GuessMemoryAvailableToTheProcess() {
1709
1
  uint64_t free_in_system = uv_get_free_memory();
1710
1
  size_t allowed = uv_get_constrained_memory();
1711
1
  if (allowed == 0) {
1712
    return free_in_system;
1713
  }
1714
  size_t rss;
1715
1
  int err = uv_resident_set_memory(&rss);
1716
1
  if (err) {
1717
    return free_in_system;
1718
  }
1719
1
  if (allowed < rss) {
1720
    // Something is probably wrong. Fallback to the free memory.
1721
    return free_in_system;
1722
  }
1723
  // There may still be room for swap, but we will just leave it here.
1724
1
  return allowed - rss;
1725
}
1726
1727
24
void Environment::BuildEmbedderGraph(Isolate* isolate,
1728
                                     EmbedderGraph* graph,
1729
                                     void* data) {
1730
24
  MemoryTracker tracker(isolate, graph);
1731
24
  Environment* env = static_cast<Environment*>(data);
1732
24
  tracker.Track(env);
1733
24
  env->ForEachBaseObject([&](BaseObject* obj) {
1734
633
    if (obj->IsDoneInitializing())
1735
632
      tracker.Track(obj);
1736
633
  });
1737
24
}
1738
1739
1
size_t Environment::NearHeapLimitCallback(void* data,
1740
                                          size_t current_heap_limit,
1741
                                          size_t initial_heap_limit) {
1742
1
  Environment* env = static_cast<Environment*>(data);
1743
1744
  Debug(env,
1745
        DebugCategory::DIAGNOSTICS,
1746
        "Invoked NearHeapLimitCallback, processing=%d, "
1747
        "current_limit=%" PRIu64 ", "
1748
        "initial_limit=%" PRIu64 "\n",
1749
1
        env->is_processing_heap_limit_callback_,
1750
2
        static_cast<uint64_t>(current_heap_limit),
1751
1
        static_cast<uint64_t>(initial_heap_limit));
1752
1753
1
  size_t max_young_gen_size = env->isolate_data()->max_young_gen_size;
1754
1
  size_t young_gen_size = 0;
1755
1
  size_t old_gen_size = 0;
1756
1757
1
  HeapSpaceStatistics stats;
1758
1
  size_t num_heap_spaces = env->isolate()->NumberOfHeapSpaces();
1759
9
  for (size_t i = 0; i < num_heap_spaces; ++i) {
1760
8
    env->isolate()->GetHeapSpaceStatistics(&stats, i);
1761

15
    if (strcmp(stats.space_name(), "new_space") == 0 ||
1762
7
        strcmp(stats.space_name(), "new_large_object_space") == 0) {
1763
2
      young_gen_size += stats.space_used_size();
1764
    } else {
1765
6
      old_gen_size += stats.space_used_size();
1766
    }
1767
  }
1768
1769
  Debug(env,
1770
        DebugCategory::DIAGNOSTICS,
1771
        "max_young_gen_size=%" PRIu64 ", "
1772
        "young_gen_size=%" PRIu64 ", "
1773
        "old_gen_size=%" PRIu64 ", "
1774
        "total_size=%" PRIu64 "\n",
1775
2
        static_cast<uint64_t>(max_young_gen_size),
1776
2
        static_cast<uint64_t>(young_gen_size),
1777
2
        static_cast<uint64_t>(old_gen_size),
1778
1
        static_cast<uint64_t>(young_gen_size + old_gen_size));
1779
1780
1
  uint64_t available = GuessMemoryAvailableToTheProcess();
1781
  // TODO(joyeecheung): get a better estimate about the native memory
1782
  // usage into the overhead, e.g. based on the count of objects.
1783
1
  uint64_t estimated_overhead = max_young_gen_size;
1784
  Debug(env,
1785
        DebugCategory::DIAGNOSTICS,
1786
        "Estimated available memory=%" PRIu64 ", "
1787
        "estimated overhead=%" PRIu64 "\n",
1788
2
        static_cast<uint64_t>(available),
1789
1
        static_cast<uint64_t>(estimated_overhead));
1790
1791
  // This might be hit when the snapshot is being taken in another
1792
  // NearHeapLimitCallback invocation.
1793
  // When taking the snapshot, objects in the young generation may be
1794
  // promoted to the old generation, result in increased heap usage,
1795
  // but it should be no more than the young generation size.
1796
  // Ideally, this should be as small as possible - the heap limit
1797
  // can only be restored when the heap usage falls down below the
1798
  // new limit, so in a heap with unbounded growth the isolate
1799
  // may eventually crash with this new limit - effectively raising
1800
  // the heap limit to the new one.
1801
1
  if (env->is_processing_heap_limit_callback_) {
1802
    size_t new_limit = current_heap_limit + max_young_gen_size;
1803
    Debug(env,
1804
          DebugCategory::DIAGNOSTICS,
1805
          "Not generating snapshots in nested callback. "
1806
          "new_limit=%" PRIu64 "\n",
1807
          static_cast<uint64_t>(new_limit));
1808
    return new_limit;
1809
  }
1810
1811
  // Estimate whether the snapshot is going to use up all the memory
1812
  // available to the process. If so, just give up to prevent the system
1813
  // from killing the process for a system OOM.
1814
1
  if (estimated_overhead > available) {
1815
    Debug(env,
1816
          DebugCategory::DIAGNOSTICS,
1817
          "Not generating snapshots because it's too risky.\n");
1818
    env->isolate()->RemoveNearHeapLimitCallback(NearHeapLimitCallback,
1819
                                                initial_heap_limit);
1820
    // The new limit must be higher than current_heap_limit or V8 might
1821
    // crash.
1822
    return current_heap_limit + 1;
1823
  }
1824
1825
  // Take the snapshot synchronously.
1826
1
  env->is_processing_heap_limit_callback_ = true;
1827
1828
2
  std::string dir = env->options()->diagnostic_dir;
1829
1
  if (dir.empty()) {
1830
1
    dir = env->GetCwd();
1831
  }
1832
2
  DiagnosticFilename name(env, "Heap", "heapsnapshot");
1833
1
  std::string filename = dir + kPathSeparator + (*name);
1834
1835
1
  Debug(env, DebugCategory::DIAGNOSTICS, "Start generating %s...\n", *name);
1836
1837
  // Remove the callback first in case it's triggered when generating
1838
  // the snapshot.
1839
1
  env->isolate()->RemoveNearHeapLimitCallback(NearHeapLimitCallback,
1840
                                              initial_heap_limit);
1841
1842
1
  heap::WriteSnapshot(env, filename.c_str());
1843
1
  env->heap_limit_snapshot_taken_ += 1;
1844
1845
  // Don't take more snapshots than the number specified by
1846
  // --heapsnapshot-near-heap-limit.
1847
2
  if (env->heap_limit_snapshot_taken_ <
1848
1
      env->options_->heap_snapshot_near_heap_limit) {
1849
    env->isolate()->AddNearHeapLimitCallback(NearHeapLimitCallback, env);
1850
  }
1851
1852
1
  FPrintF(stderr, "Wrote snapshot to %s\n", filename.c_str());
1853
  // Tell V8 to reset the heap limit once the heap usage falls down to
1854
  // 95% of the initial limit.
1855
1
  env->isolate()->AutomaticallyRestoreInitialHeapLimit(0.95);
1856
1857
1
  env->is_processing_heap_limit_callback_ = false;
1858
1859
  // The new limit must be higher than current_heap_limit or V8 might
1860
  // crash.
1861
1
  return current_heap_limit + 1;
1862
}
1863
1864
24
inline size_t Environment::SelfSize() const {
1865
24
  size_t size = sizeof(*this);
1866
  // Remove non pointer fields that will be tracked in MemoryInfo()
1867
  // TODO(joyeecheung): refactor the MemoryTracker interface so
1868
  // this can be done for common types within the Track* calls automatically
1869
  // if a certain scope is entered.
1870
24
  size -= sizeof(async_hooks_);
1871
24
  size -= sizeof(tick_info_);
1872
24
  size -= sizeof(immediate_info_);
1873
24
  return size;
1874
}
1875
1876
24
void Environment::MemoryInfo(MemoryTracker* tracker) const {
1877
  // Iteratable STLs have their own sizes subtracted from the parent
1878
  // by default.
1879
24
  tracker->TrackField("isolate_data", isolate_data_);
1880
24
  tracker->TrackField("builtins_with_cache", builtins_with_cache);
1881
24
  tracker->TrackField("builtins_without_cache", builtins_without_cache);
1882
24
  tracker->TrackField("destroy_async_id_list", destroy_async_id_list_);
1883
24
  tracker->TrackField("exec_argv", exec_argv_);
1884
24
  tracker->TrackField("exiting", exiting_);
1885
24
  tracker->TrackField("should_abort_on_uncaught_toggle",
1886
24
                      should_abort_on_uncaught_toggle_);
1887
24
  tracker->TrackField("stream_base_state", stream_base_state_);
1888
24
  tracker->TrackFieldWithSize(
1889
24
      "cleanup_hooks", cleanup_hooks_.size() * sizeof(CleanupHookCallback));
1890
24
  tracker->TrackField("async_hooks", async_hooks_);
1891
24
  tracker->TrackField("immediate_info", immediate_info_);
1892
24
  tracker->TrackField("tick_info", tick_info_);
1893
24
  tracker->TrackField("principal_realm", principal_realm_);
1894
1895
  // FIXME(joyeecheung): track other fields in Environment.
1896
  // Currently MemoryTracker is unable to track these
1897
  // correctly:
1898
  // - Internal types that do not implement MemoryRetainer yet
1899
  // - STL containers with MemoryRetainer* inside
1900
  // - STL containers with numeric types inside that should not have their
1901
  //   nodes elided e.g. numeric keys in maps.
1902
  // We also need to make sure that when we add a non-pointer field as its own
1903
  // node, we shift its sizeof() size out of the Environment node.
1904
24
}
1905
1906
736422
void Environment::RunWeakRefCleanup() {
1907
736422
  isolate()->ClearKeptObjects();
1908
736422
}
1909
1910
// Not really any better place than env.cc at this moment.
1911
1452717
BaseObject::BaseObject(Environment* env, Local<Object> object)
1912
2905434
    : persistent_handle_(env->isolate(), object), env_(env) {
1913
1452717
  CHECK_EQ(false, object.IsEmpty());
1914
1452717
  CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
1915
1452717
  object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
1916
                                           &kNodeEmbedderId);
1917
1452717
  object->SetAlignedPointerInInternalField(BaseObject::kSlot,
1918
                                           static_cast<void*>(this));
1919
1452717
  env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
1920
1452717
  env->modify_base_object_count(1);
1921
1452717
}
1922
1923

7901926
BaseObject::~BaseObject() {
1924
2877496
  env()->modify_base_object_count(-1);
1925
2877496
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
1926
1927
2877496
  if (UNLIKELY(has_pointer_data())) {
1928
363844
    PointerData* metadata = pointer_data();
1929
363844
    CHECK_EQ(metadata->strong_ptr_count, 0);
1930
363844
    metadata->self = nullptr;
1931
363844
    if (metadata->weak_ptr_count == 0) delete metadata;
1932
  }
1933
1934
2877496
  if (persistent_handle_.IsEmpty()) {
1935
    // This most likely happened because the weak callback below cleared it.
1936
2146934
    return;
1937
  }
1938
1939
  {
1940
730562
    HandleScope handle_scope(env()->isolate());
1941
1461124
    object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
1942
  }
1943
}
1944
1945
1241624
void BaseObject::MakeWeak() {
1946
1241624
  if (has_pointer_data()) {
1947
42509
    pointer_data()->wants_weak_jsobj = true;
1948
42509
    if (pointer_data()->strong_ptr_count > 0) return;
1949
  }
1950
1951
2483246
  persistent_handle_.SetWeak(
1952
      this,
1953
1073466
      [](const WeakCallbackInfo<BaseObject>& data) {
1954
1073466
        BaseObject* obj = data.GetParameter();
1955
        // Clear the persistent handle so that ~BaseObject() doesn't attempt
1956
        // to mess with internal fields, since the JS object may have
1957
        // transitioned into an invalid state.
1958
        // Refs: https://github.com/nodejs/node/issues/18897
1959
1073466
        obj->persistent_handle_.Reset();
1960

1073466
        CHECK_IMPLIES(obj->has_pointer_data(),
1961
                      obj->pointer_data()->strong_ptr_count == 0);
1962
1073466
        obj->OnGCCollect();
1963
1073466
      },
1964
      WeakCallbackType::kParameter);
1965
}
1966
1967
// This just has to be different from the Chromium ones:
1968
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a
1969
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will
1970
// misinterpret the data stored in the embedder fields and try to garbage
1971
// collect them.
1972
uint16_t kNodeEmbedderId = 0x90de;
1973
1974
23512
void BaseObject::LazilyInitializedJSTemplateConstructor(
1975
    const FunctionCallbackInfo<Value>& args) {
1976
  DCHECK(args.IsConstructCall());
1977
23512
  CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount);
1978
23512
  args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
1979
                                                &kNodeEmbedderId);
1980
23512
  args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
1981
23512
}
1982
1983
21396
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
1984
    Environment* env) {
1985
  Local<FunctionTemplate> t = NewFunctionTemplate(
1986
21396
      env->isolate(), LazilyInitializedJSTemplateConstructor);
1987
21396
  t->Inherit(BaseObject::GetConstructorTemplate(env));
1988
42792
  t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount);
1989
21396
  return t;
1990
}
1991
1992
2882283
BaseObject::PointerData* BaseObject::pointer_data() {
1993
2882283
  if (!has_pointer_data()) {
1994
184359
    PointerData* metadata = new PointerData();
1995
184359
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
1996
184359
    metadata->self = this;
1997
184359
    pointer_data_ = metadata;
1998
  }
1999
2882283
  CHECK(has_pointer_data());
2000
2882283
  return pointer_data_;
2001
}
2002
2003
740741
void BaseObject::decrease_refcount() {
2004
740741
  CHECK(has_pointer_data());
2005
740741
  PointerData* metadata = pointer_data();
2006
740741
  CHECK_GT(metadata->strong_ptr_count, 0);
2007
740741
  unsigned int new_refcount = --metadata->strong_ptr_count;
2008
740741
  if (new_refcount == 0) {
2009
248694
    if (metadata->is_detached) {
2010
175963
      OnGCCollect();
2011

72731
    } else if (metadata->wants_weak_jsobj && !persistent_handle_.IsEmpty()) {
2012
42508
      MakeWeak();
2013
    }
2014
  }
2015
740741
}
2016
2017
743281
void BaseObject::increase_refcount() {
2018
743281
  unsigned int prev_refcount = pointer_data()->strong_ptr_count++;
2019

743281
  if (prev_refcount == 0 && !persistent_handle_.IsEmpty())
2020
251114
    persistent_handle_.ClearWeak();
2021
743281
}
2022
2023
168219
void BaseObject::DeleteMe(void* data) {
2024
168219
  BaseObject* self = static_cast<BaseObject*>(data);
2025

173207
  if (self->has_pointer_data() &&
2026
4988
      self->pointer_data()->strong_ptr_count > 0) {
2027
827
    return self->Detach();
2028
  }
2029
167392
  delete self;
2030
}
2031
2032
576
bool BaseObject::IsDoneInitializing() const { return true; }
2033
2034
632
Local<Object> BaseObject::WrappedObject() const {
2035
632
  return object();
2036
}
2037
2038
1264
bool BaseObject::IsRootNode() const {
2039
2528
  return !persistent_handle_.IsWeak();
2040
}
2041
2042
55226
Local<FunctionTemplate> BaseObject::GetConstructorTemplate(
2043
    IsolateData* isolate_data) {
2044
55226
  Local<FunctionTemplate> tmpl = isolate_data->base_object_ctor_template();
2045
55226
  if (tmpl.IsEmpty()) {
2046
785
    tmpl = NewFunctionTemplate(isolate_data->isolate(), nullptr);
2047
785
    tmpl->SetClassName(
2048
        FIXED_ONE_BYTE_STRING(isolate_data->isolate(), "BaseObject"));
2049
785
    isolate_data->set_base_object_ctor_template(tmpl);
2050
  }
2051
55226
  return tmpl;
2052
}
2053
2054
bool BaseObject::IsNotIndicativeOfMemoryLeakAtExit() const {
2055
  return IsWeakOrDetached();
2056
}
2057
2058
}  // namespace node