GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: env.cc Lines: 920 1005 91.5 %
Date: 2022-09-07 04:19:57 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
15699
void AsyncHooks::SetJSPromiseHooks(Local<Function> init,
70
                                   Local<Function> before,
71
                                   Local<Function> after,
72
                                   Local<Function> resolve) {
73
15699
  js_promise_hooks_[0].Reset(env()->isolate(), init);
74
15699
  js_promise_hooks_[1].Reset(env()->isolate(), before);
75
15699
  js_promise_hooks_[2].Reset(env()->isolate(), after);
76
15699
  js_promise_hooks_[3].Reset(env()->isolate(), resolve);
77
31743
  for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
78
16044
    if (it->IsEmpty()) {
79
      contexts_.erase(it--);
80
      continue;
81
    }
82
32088
    PersistentToLocal::Weak(env()->isolate(), *it)
83
16044
        ->SetPromiseHooks(init, before, after, resolve);
84
  }
85
15699
}
86
87
// Remember to keep this code aligned with pushAsyncContext() in JS.
88
815202
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
815202
  if (fields_[kCheck] > 0) {
94
815198
    CHECK_GE(async_id, -1);
95
815198
    CHECK_GE(trigger_async_id, -1);
96
  }
97
98
815202
  uint32_t offset = fields_[kStackLength];
99
815202
  if (offset * 2 >= async_ids_stack_.Length()) grow_async_ids_stack();
100
815202
  async_ids_stack_[2 * offset] = async_id_fields_[kExecutionAsyncId];
101
815202
  async_ids_stack_[2 * offset + 1] = async_id_fields_[kTriggerAsyncId];
102
815202
  fields_[kStackLength] += 1;
103
815202
  async_id_fields_[kExecutionAsyncId] = async_id;
104
815202
  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
815202
  if (!resource.IsEmpty()) {
114
815198
    native_execution_async_resources_.resize(offset + 1);
115
    // Caveat: This is a v8::Local<> assignment, we do not keep a v8::Global<>!
116
815198
    native_execution_async_resources_[offset] = resource;
117
  }
118
815202
}
119
120
// Remember to keep this code aligned with popAsyncContext() in JS.
121
814801
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
814801
  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
1627490
  if (UNLIKELY(fields_[kCheck] > 0 &&
129

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

1627486
             !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
813743
    native_execution_async_resources_.resize(offset);
147
813743
    if (native_execution_async_resources_.size() <
148

1075509
            native_execution_async_resources_.capacity() / 2 &&
149
261766
        native_execution_async_resources_.size() > 16) {
150
      native_execution_async_resources_.shrink_to_fit();
151
    }
152
  }
153
154
1627486
  if (UNLIKELY(js_execution_async_resources()->Length() > offset)) {
155
29499
    HandleScope handle_scope(env()->isolate());
156
58998
    USE(js_execution_async_resources()->Set(
157
        env()->context(),
158
        env()->length_string(),
159
117996
        Integer::NewFromUnsigned(env()->isolate(), offset)));
160
  }
161
162
813743
  return fields_[kStackLength] > 0;
163
}
164
165
2321
void AsyncHooks::clear_async_id_stack() {
166
2321
  Isolate* isolate = env()->isolate();
167
2321
  HandleScope handle_scope(isolate);
168
2321
  if (!js_execution_async_resources_.IsEmpty()) {
169
3082
    USE(PersistentToLocal::Strong(js_execution_async_resources_)
170
3082
            ->Set(env()->context(),
171
                  env()->length_string(),
172
6164
                  Integer::NewFromUnsigned(isolate, 0)));
173
  }
174
2321
  native_execution_async_resources_.clear();
175
2321
  native_execution_async_resources_.shrink_to_fit();
176
177
2321
  async_id_fields_[kExecutionAsyncId] = 0;
178
2321
  async_id_fields_[kTriggerAsyncId] = 0;
179
2321
  fields_[kStackLength] = 0;
180
2321
}
181
182
6876
void AsyncHooks::AddContext(Local<Context> ctx) {
183
20628
  ctx->SetPromiseHooks(js_promise_hooks_[0].IsEmpty()
184
6876
                           ? Local<Function>()
185
205
                           : PersistentToLocal::Strong(js_promise_hooks_[0]),
186
6876
                       js_promise_hooks_[1].IsEmpty()
187
6876
                           ? Local<Function>()
188
205
                           : PersistentToLocal::Strong(js_promise_hooks_[1]),
189
6876
                       js_promise_hooks_[2].IsEmpty()
190
6876
                           ? Local<Function>()
191
205
                           : PersistentToLocal::Strong(js_promise_hooks_[2]),
192
6876
                       js_promise_hooks_[3].IsEmpty()
193
6876
                           ? Local<Function>()
194
                           : PersistentToLocal::Strong(js_promise_hooks_[3]));
195
196
6876
  size_t id = contexts_.size();
197
6876
  contexts_.resize(id + 1);
198
6876
  contexts_[id].Reset(env()->isolate(), ctx);
199
6876
  contexts_[id].SetWeak();
200
6876
}
201
202
517
void AsyncHooks::RemoveContext(Local<Context> ctx) {
203
517
  Isolate* isolate = env()->isolate();
204
1034
  HandleScope handle_scope(isolate);
205
517
  contexts_.erase(std::remove_if(contexts_.begin(),
206
                                 contexts_.end(),
207
4047
                                 [&](auto&& el) { return el.IsEmpty(); }),
208
1034
                  contexts_.end());
209
3985
  for (auto it = contexts_.begin(); it != contexts_.end(); it++) {
210
3468
    Local<Context> saved_context = PersistentToLocal::Weak(isolate, *it);
211
3468
    if (saved_context == ctx) {
212
      it->Reset();
213
      contexts_.erase(it);
214
      break;
215
    }
216
  }
217
517
}
218
219
239364
AsyncHooks::DefaultTriggerAsyncIdScope::DefaultTriggerAsyncIdScope(
220
239364
    Environment* env, double default_trigger_async_id)
221
239364
    : async_hooks_(env->async_hooks()) {
222
239364
  if (env->async_hooks()->fields()[AsyncHooks::kCheck] > 0) {
223
239364
    CHECK_GE(default_trigger_async_id, 0);
224
  }
225
226
239364
  old_default_trigger_async_id_ =
227
239364
      async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId];
228
239364
  async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] =
229
239364
      default_trigger_async_id;
230
239364
}
231
232
478726
AsyncHooks::DefaultTriggerAsyncIdScope::~DefaultTriggerAsyncIdScope() {
233
239363
  async_hooks_->async_id_fields()[AsyncHooks::kDefaultTriggerAsyncId] =
234
239363
      old_default_trigger_async_id_;
235
239363
}
236
237
239364
AsyncHooks::DefaultTriggerAsyncIdScope::DefaultTriggerAsyncIdScope(
238
239364
    AsyncWrap* async_wrap)
239
    : DefaultTriggerAsyncIdScope(async_wrap->env(),
240
239364
                                 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
5466
void IsolateData::DeserializeProperties(const IsolateDataSerializeInfo* info) {
323
5466
  size_t i = 0;
324
5466
  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




103854
  PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
341






136650
  PER_ISOLATE_SYMBOL_PROPERTIES(VY)
342










































































































































3022698
  PER_ISOLATE_STRING_PROPERTIES(VS)
343
#undef V
344
#undef VY
345
#undef VS
346
#undef VP
347
348
322494
  for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) {
349
    MaybeLocal<String> maybe_field =
350
634056
        isolate_->GetDataFromSnapshotOnce<String>(info->primitive_values[i++]);
351
    Local<String> field;
352
317028
    if (!maybe_field.ToLocal(&field)) {
353
      fprintf(stderr, "Failed to deserialize AsyncWrap provider %zu\n", j);
354
    }
355
317028
    async_wrap_providers_[j].Set(isolate_, field);
356
  }
357
358
5466
  const std::vector<PropInfo>& values = info->template_values;
359
5466
  i = 0;  // index to the array
360
5466
  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










































































224106
  PER_ISOLATE_TEMPLATE_PROPERTIES(V);
381
#undef V
382
5466
}
383
384
791
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
1582
  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
7910
  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
10283
  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
219107
  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
46669
  NODE_ASYNC_PROVIDER_TYPES(V)
445
#undef V
446
447
  // TODO(legendecas): eagerly create per isolate templates.
448
791
  Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
449
1582
  templ->InstanceTemplate()->SetInternalFieldCount(
450
      BaseObject::kInternalFieldCount);
451
791
  templ->Inherit(BaseObject::GetConstructorTemplate(this));
452
791
  set_binding_data_ctor_template(templ);
453
454
791
  set_contextify_global_template(
455
791
      contextify::ContextifyContext::CreateGlobalTemplate(isolate_));
456
791
}
457
458
6257
IsolateData::IsolateData(Isolate* isolate,
459
                         uv_loop_t* event_loop,
460
                         MultiIsolatePlatform* platform,
461
                         ArrayBufferAllocator* node_allocator,
462
6257
                         const IsolateDataSerializeInfo* isolate_data_info)
463
    : isolate_(isolate),
464
      event_loop_(event_loop),
465
51
      node_allocator_(node_allocator == nullptr ? nullptr
466
6206
                                                : node_allocator->GetImpl()),
467
12514
      platform_(platform) {
468
6257
  options_.reset(
469
6257
      new PerIsolateOptions(*(per_process::cli_options->per_isolate)));
470
471
6257
  if (isolate_data_info == nullptr) {
472
791
    CreateProperties();
473
  } else {
474
5466
    DeserializeProperties(isolate_data_info);
475
  }
476
6257
}
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
6876
void Environment::AssignToContext(Local<v8::Context> context,
527
                                  Realm* realm,
528
                                  const ContextInfo& info) {
529
6876
  context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
530
                                           this);
531
6876
  context->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kRealm, realm);
532
  // Used to retrieve bindings
533
13752
  context->SetAlignedPointerInEmbedderData(
534
6876
      ContextEmbedderIndex::kBindingListIndex, &(this->bindings_));
535
536
  // ContextifyContexts will update this to a pointer to the native object.
537
6876
  context->SetAlignedPointerInEmbedderData(
538
      ContextEmbedderIndex::kContextifyContext, nullptr);
539
540
  // This must not be done before other context fields are initialized.
541
6876
  ContextEmbedderTag::TagNodeContext(context);
542
543
#if HAVE_INSPECTOR
544
6876
  inspector_agent()->ContextCreated(context, info);
545
#endif  // HAVE_INSPECTOR
546
547
6876
  this->async_hooks()->AddContext(context);
548
6876
}
549
550
185
void Environment::TryLoadAddon(
551
    const char* filename,
552
    int flags,
553
    const std::function<bool(binding::DLib*)>& was_loaded) {
554
185
  loaded_addons_.emplace_back(filename, flags);
555
185
  if (!was_loaded(&loaded_addons_.back())) {
556
10
    loaded_addons_.pop_back();
557
  }
558
185
}
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
1901
void Environment::add_refs(int64_t diff) {
577
1901
  task_queues_async_refs_ += diff;
578
1901
  CHECK_GE(task_queues_async_refs_, 0);
579
1901
  if (task_queues_async_refs_ == 0)
580
420
    uv_unref(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
581
  else
582
1481
    uv_ref(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
583
1901
}
584
585
66147
uv_buf_t Environment::allocate_managed_buffer(const size_t suggested_size) {
586
132294
  NoArrayBufferZeroFillScope no_zero_fill_scope(isolate_data());
587
  std::unique_ptr<v8::BackingStore> bs =
588
66147
      v8::ArrayBuffer::NewBackingStore(isolate(), suggested_size);
589
66147
  uv_buf_t buf = uv_buf_init(static_cast<char*>(bs->Data()), bs->ByteLength());
590
66147
  released_allocated_buffers_.emplace(buf.base, std::move(bs));
591
66147
  return buf;
592
}
593
594
81054
std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
595
    const uv_buf_t& buf) {
596
81054
  std::unique_ptr<v8::BackingStore> bs;
597
81054
  if (buf.base != nullptr) {
598
66147
    auto it = released_allocated_buffers_.find(buf.base);
599
66147
    CHECK_NE(it, released_allocated_buffers_.end());
600
66147
    bs = std::move(it->second);
601
66147
    released_allocated_buffers_.erase(it);
602
  }
603
81054
  return bs;
604
}
605
606
6246
std::string GetExecPath(const std::vector<std::string>& argv) {
607
  char exec_path_buf[2 * PATH_MAX];
608
6246
  size_t exec_path_len = sizeof(exec_path_buf);
609
6246
  std::string exec_path;
610
6246
  if (uv_exepath(exec_path_buf, &exec_path_len) == 0) {
611
6246
    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
6246
  return exec_path;
630
}
631
632
6246
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
6246
                         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
6246
      timer_base_(uv_now(isolate_data->event_loop())),
645
      exec_argv_(exec_args),
646
      argv_(args),
647
      exec_path_(GetExecPath(args)),
648
6246
      exiting_(isolate_, 1, MAYBE_FIELD_PTR(env_info, exiting)),
649
      should_abort_on_uncaught_toggle_(
650
6246
          isolate_,
651
          1,
652
          MAYBE_FIELD_PTR(env_info, should_abort_on_uncaught_toggle)),
653
6246
      stream_base_state_(isolate_,
654
                         StreamBase::kNumStreamBaseStateFields,
655
                         MAYBE_FIELD_PTR(env_info, stream_base_state)),
656
6246
      time_origin_(PERFORMANCE_NOW()),
657
6246
      time_origin_timestamp_(GetCurrentTimeInMicroseconds()),
658
      flags_(flags),
659
6246
      thread_id_(thread_id.id == static_cast<uint64_t>(-1)
660
6246
                     ? AllocateEnvironmentThreadId().id
661



24984
                     : thread_id.id) {
662
  // We'll be creating new objects so make sure we've entered the context.
663
12492
  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
6246
  if (flags_ & EnvironmentFlags::kDefaultFlags) {
668
11046
    flags_ = flags_ |
669
5523
        EnvironmentFlags::kOwnsProcessState |
670
        EnvironmentFlags::kOwnsInspector;
671
  }
672
673
6246
  set_env_vars(per_process::system_environment);
674
6246
  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
12492
  options_ = std::make_shared<EnvironmentOptions>(
681
18738
      *isolate_data->options()->per_env);
682
6246
  inspector_host_port_ = std::make_shared<ExclusiveAccess<HostPort>>(
683
6246
      options_->debug_options().host_port);
684
685
6246
  if (!(flags_ & EnvironmentFlags::kOwnsProcessState)) {
686
723
    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
6246
  inspector_agent_ = std::make_unique<inspector::Agent>(this);
692
#endif
693
694
6246
  if (tracing::AgentWriterHandle* writer = GetTracingAgentWriter()) {
695
6246
    trace_state_observer_ = std::make_unique<TrackingTraceStateObserver>(this);
696
6246
    if (TracingController* tracing_controller = writer->GetTracingController())
697
6195
      tracing_controller->AddTraceStateObserver(trace_state_observer_.get());
698
  }
699
700
6246
  destroy_async_id_list_.reserve(512);
701
702
6246
  performance_state_ = std::make_unique<performance::PerformanceState>(
703
6246
      isolate, MAYBE_FIELD_PTR(env_info, performance_state));
704
705
6246
  if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
706
6246
          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
6246
}
721
722
780
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
780
                         ThreadId thread_id)
729
    : Environment(isolate_data,
730
                  context->GetIsolate(),
731
                  args,
732
                  exec_args,
733
                  env_info,
734
                  flags,
735
780
                  thread_id) {
736
780
  InitializeMainContext(context, env_info);
737
780
}
738
739
6246
void Environment::InitializeMainContext(Local<Context> context,
740
                                        const EnvSerializeInfo* env_info) {
741
6246
  principal_realm_ = std::make_unique<Realm>(
742
6246
      this, context, MAYBE_FIELD_PTR(env_info, principal_realm));
743
6246
  AssignToContext(context, principal_realm_.get(), ContextInfo(""));
744
6246
  if (env_info != nullptr) {
745
5466
    DeserializeProperties(env_info);
746
  }
747
748
6246
  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
6246
  should_abort_on_uncaught_toggle_[0] = 1;
754
755
  // The process is not exiting by default.
756
6246
  set_exiting(false);
757
758
6246
  performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT,
759
                           time_origin_);
760
6246
  performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START,
761
                           per_process::node_start_time);
762
763
6246
  if (per_process::v8_initialized) {
764
6202
    performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_V8_START,
765
                            performance::performance_v8_start);
766
  }
767
6246
}
768
769
22712
Environment::~Environment() {
770
  HandleScope handle_scope(isolate());
771
5678
  Local<Context> ctx = context();
772
773
5678
  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
5678
  CHECK(is_stopping());
799
800
5678
  if (options_->heap_snapshot_near_heap_limit > heap_limit_snapshot_taken_) {
801
    isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback,
802
                                          0);
803
  }
804
805
5678
  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
5678
  inspector_agent_.reset();
812
#endif
813
814
5678
  ctx->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment,
815
                                       nullptr);
816
5678
  ctx->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kRealm, nullptr);
817
818
5678
  if (trace_state_observer_) {
819
5678
    tracing::AgentWriterHandle* writer = GetTracingAgentWriter();
820
5678
    CHECK_NOT_NULL(writer);
821
5678
    if (TracingController* tracing_controller = writer->GetTracingController())
822
5629
      tracing_controller->RemoveTraceStateObserver(trace_state_observer_.get());
823
  }
824
825

10601
  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
5678
  if (!is_main_thread()) {
834
    // Dereference all addons that were loaded into this environment.
835
734
    for (binding::DLib& addon : loaded_addons_) {
836
14
      addon.Close();
837
    }
838
  }
839
840
5678
  CHECK_EQ(base_object_count_, 0);
841
5678
}
842
843
6212
void Environment::InitializeLibuv() {
844
12424
  HandleScope handle_scope(isolate());
845
6212
  Context::Scope context_scope(context());
846
847
6212
  CHECK_EQ(0, uv_timer_init(event_loop(), timer_handle()));
848
6212
  uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle()));
849
850
6212
  CHECK_EQ(0, uv_check_init(event_loop(), immediate_check_handle()));
851
6212
  uv_unref(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
852
853
6212
  CHECK_EQ(0, uv_idle_init(event_loop(), immediate_idle_handle()));
854
855
6212
  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
6212
  CHECK_EQ(0, uv_prepare_init(event_loop(), &idle_prepare_handle_));
862
6212
  CHECK_EQ(0, uv_check_init(event_loop(), &idle_check_handle_));
863
864
25495
  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
6212
  uv_unref(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
875
6212
  uv_unref(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
876
6212
  uv_unref(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
877
878
  {
879
12424
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
880
6212
    task_queues_async_initialized_ = true;
881

12424
    if (native_immediates_threadsafe_.size() > 0 ||
882
6212
        native_immediates_interrupts_.size() > 0) {
883
5462
      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
6212
  RegisterHandleCleanups();
892
893
6212
  StartProfilerIdleNotifier();
894
6212
}
895
896
362
void Environment::ExitEnv() {
897
362
  set_can_call_into_js(false);
898
362
  set_stopping(true);
899
362
  isolate_->TerminateExecution();
900
724
  SetImmediateThreadsafe([](Environment* env) { uv_stop(env->event_loop()); });
901
362
}
902
903
6212
void Environment::RegisterHandleCleanups() {
904
6212
  HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle,
905
33864
                                        void* arg) {
906
33864
    handle->data = env;
907
908
33864
    env->CloseHandle(handle, [](uv_handle_t* handle) {
909
#ifdef DEBUG
910
      memset(handle, 0xab, uv_handle_size(handle->type));
911
#endif
912
33864
    });
913
33864
  };
914
915
37272
  auto register_handle = [&](uv_handle_t* handle) {
916
37272
    RegisterHandleCleanup(handle, close_and_finish, nullptr);
917
43484
  };
918
6212
  register_handle(reinterpret_cast<uv_handle_t*>(timer_handle()));
919
6212
  register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
920
6212
  register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle()));
921
6212
  register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));
922
6212
  register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));
923
6212
  register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_));
924
6212
}
925
926
11350
void Environment::CleanupHandles() {
927
  {
928
11350
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
929
11350
    task_queues_async_initialized_ = false;
930
  }
931
932
  Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(),
933
22700
      Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
934
935
11350
  RunAndClearNativeImmediates(true /* skip unrefed SetImmediate()s */);
936
937
11513
  for (ReqWrapBase* request : req_wrap_queue_)
938
163
    request->Cancel();
939
940
15884
  for (HandleWrap* handle : handle_wrap_queue_)
941
9068
    handle->Close();
942
943
45214
  for (HandleCleanup& hc : handle_cleanup_queue_)
944
33864
    hc.cb_(this, hc.handle_, hc.arg_);
945
11350
  handle_cleanup_queue_.clear();
946
947
10654
  while (handle_cleanup_waiting_ != 0 ||
948

33356
         request_waiting_ != 0 ||
949
11352
         !handle_wrap_queue_.IsEmpty()) {
950
10654
    uv_run(event_loop(), UV_RUN_ONCE);
951
  }
952
11350
}
953
954
6212
void Environment::StartProfilerIdleNotifier() {
955
6212
  uv_prepare_start(&idle_prepare_handle_, [](uv_prepare_t* handle) {
956
201361
    Environment* env = ContainerOf(&Environment::idle_prepare_handle_, handle);
957
201361
    env->isolate()->SetIdle(true);
958
201361
  });
959
6212
  uv_check_start(&idle_check_handle_, [](uv_check_t* handle) {
960
201164
    Environment* env = ContainerOf(&Environment::idle_check_handle_, handle);
961
201164
    env->isolate()->SetIdle(false);
962
201164
  });
963
6212
}
964
965
745352
void Environment::PrintSyncTrace() const {
966
745352
  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
5330
MaybeLocal<Value> Environment::RunSnapshotSerializeCallback() const {
978
5330
  EscapableHandleScope handle_scope(isolate());
979
10660
  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
10660
  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
5678
void Environment::RunCleanup() {
998
5678
  started_cleanup_ = true;
999

16280
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup");
1000
5678
  bindings_.clear();
1001
5678
  CleanupHandles();
1002
1003
17030
  while (!cleanup_hooks_.empty() ||
1004
11358
         native_immediates_.size() > 0 ||
1005

22708
         native_immediates_threadsafe_.size() > 0 ||
1006
5678
         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
11344
        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
5672
    std::sort(callbacks.begin(), callbacks.end(),
1014
1292565
              [](const CleanupHookCallback& a, const CleanupHookCallback& b) {
1015
      // Sort in descending order so that the most recently inserted callbacks
1016
      // are run first.
1017
1292565
      return a.insertion_order_counter_ > b.insertion_order_counter_;
1018
    });
1019
1020
194013
    for (const CleanupHookCallback& cb : callbacks) {
1021
188341
      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
1022
        continue;
1025
      }
1026
1027
187319
      cb.fn_(cb.arg_);
1028
187319
      cleanup_hooks_.erase(cb);
1029
    }
1030
5672
    CleanupHandles();
1031
  }
1032
1033
5681
  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
5678
}
1039
1040
6319
void Environment::RunAtExitCallbacks() {
1041

18107
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "AtExit");
1042
18773
  for (ExitCallback at_exit : at_exit_functions_) {
1043
12454
    at_exit.cb_(at_exit.arg_);
1044
  }
1045
6319
  at_exit_functions_.clear();
1046
6319
}
1047
1048
12482
void Environment::AtExit(void (*cb)(void* arg), void* arg) {
1049
12482
  at_exit_functions_.push_front(ExitCallback{cb, arg});
1050
12482
}
1051
1052
239991
void Environment::RunAndClearInterrupts() {
1053
239991
  while (native_immediates_interrupts_.size() > 0) {
1054
10666
    NativeImmediateQueue queue;
1055
    {
1056
21336
      Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
1057
10668
      queue.ConcatMove(std::move(native_immediates_interrupts_));
1058
    }
1059
10668
    DebugSealHandleScope seal_handle_scope(isolate());
1060
1061
21343
    while (auto head = queue.Shift())
1062
21352
      head->Call(this);
1063
  }
1064
229323
}
1065
1066
218944
void Environment::RunAndClearNativeImmediates(bool only_refed) {
1067

443033
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment),
1068
               "RunAndClearNativeImmediates");
1069
437881
  HandleScope handle_scope(isolate_);
1070
437881
  InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 });
1071
1072
218944
  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
218944
  RunAndClearInterrupts();
1077
1078
437884
  auto drain_list = [&](NativeImmediateQueue* queue) {
1079
875762
    TryCatchScope try_catch(this);
1080
437884
    DebugSealHandleScope seal_handle_scope(isolate());
1081
497982
    while (auto head = queue->Shift()) {
1082
60105
      bool is_refed = head->flags() & CallbackFlags::kRefed;
1083
60105
      if (is_refed)
1084
35138
        ref_count++;
1085
1086

60105
      if (is_refed || !only_refed)
1087
59853
        head->Call(this);
1088
1089
60100
      head.reset();  // Destroy now so that this is also observed by try_catch.
1090
1091
60100
      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
60098
    }
1098
437877
    return false;
1099
218943
  };
1100
218943
  while (drain_list(&native_immediates_)) {}
1101
1102
218940
  immediate_info()->ref_count_dec(ref_count);
1103
1104
218940
  if (immediate_info()->ref_count() == 0)
1105
175769
    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
218937
  NativeImmediateQueue threadsafe_immediates;
1115
218940
  if (native_immediates_threadsafe_.size() > 0) {
1116
2148
    Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_);
1117
1074
    threadsafe_immediates.ConcatMove(std::move(native_immediates_threadsafe_));
1118
  }
1119
218941
  while (drain_list(&threadsafe_immediates)) {}
1120
218937
}
1121
1122
10681
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
10681
  Environment** interrupt_data = new Environment*(this);
1135
10681
  Environment** dummy = nullptr;
1136
10681
  if (!interrupt_data_.compare_exchange_strong(dummy, interrupt_data)) {
1137
394
    delete interrupt_data;
1138
394
    return;  // Already scheduled.
1139
  }
1140
1141
10287
  isolate()->RequestInterrupt([](Isolate* isolate, void* data) {
1142
10278
    std::unique_ptr<Environment*> env_ptr { static_cast<Environment**>(data) };
1143
10278
    Environment* env = *env_ptr;
1144
10278
    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
10267
    env->interrupt_data_.store(nullptr);
1151
10267
    env->RunAndClearInterrupts();
1152
  }, interrupt_data);
1153
}
1154
1155
9625
void Environment::ScheduleTimer(int64_t duration_ms) {
1156
9625
  if (started_cleanup_) return;
1157
9625
  uv_timer_start(timer_handle(), RunTimers, duration_ms, 0);
1158
}
1159
1160
3830
void Environment::ToggleTimerRef(bool ref) {
1161
3830
  if (started_cleanup_) return;
1162
1163
3830
  if (ref) {
1164
2527
    uv_ref(reinterpret_cast<uv_handle_t*>(timer_handle()));
1165
  } else {
1166
1303
    uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle()));
1167
  }
1168
}
1169
1170
7593
void Environment::RunTimers(uv_timer_t* handle) {
1171
7593
  Environment* env = Environment::from_timer_handle(handle);
1172

8119
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunTimers");
1173
1174
7593
  if (!env->can_call_into_js())
1175
    return;
1176
1177
7593
  HandleScope handle_scope(env->isolate());
1178
7593
  Context::Scope context_scope(env->context());
1179
1180
7593
  Local<Object> process = env->process_object();
1181
7593
  InternalCallbackScope scope(env, process, {0, 0});
1182
1183
7593
  Local<Function> cb = env->timers_callback_function();
1184
  MaybeLocal<Value> ret;
1185
7593
  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
32
  do {
1190
7625
    TryCatchScope try_catch(env);
1191
7625
    try_catch.SetVerbose(true);
1192
7625
    ret = cb->Call(env->context(), process, 1, &arg);
1193

7613
  } 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
7581
  if (ret.IsEmpty())
1201
5
    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
7576
      ret.ToLocalChecked()->IntegerValue(env->context()).FromJust();
1212
1213
7576
  uv_handle_t* h = reinterpret_cast<uv_handle_t*>(handle);
1214
1215
7576
  if (expiry_ms != 0) {
1216
    int64_t duration_ms =
1217
6545
        llabs(expiry_ms) - (uv_now(env->event_loop()) - env->timer_base());
1218
1219
6545
    env->ScheduleTimer(duration_ms > 0 ? duration_ms : 1);
1220
1221
6545
    if (expiry_ms > 0)
1222
5836
      uv_ref(h);
1223
    else
1224
709
      uv_unref(h);
1225
  } else {
1226
1031
    uv_unref(h);
1227
  }
1228
}
1229
1230
1231
201164
void Environment::CheckImmediate(uv_check_t* handle) {
1232
201164
  Environment* env = Environment::from_immediate_check_handle(handle);
1233

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

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

43539
  } while (env->immediate_info()->has_outstanding() && env->can_call_into_js());
1251
1252
42588
  if (env->immediate_info()->ref_count() == 0)
1253
4700
    env->ToggleImmediateRef(false);
1254
}
1255
1256
246488
void Environment::ToggleImmediateRef(bool ref) {
1257
246488
  if (started_cleanup_) return;
1258
1259
235215
  if (ref) {
1260
    // Idle handle is needed only to stop the event loop from blocking in poll.
1261
65995
    uv_idle_start(immediate_idle_handle(), [](uv_idle_t*){ });
1262
  } else {
1263
169220
    uv_idle_stop(immediate_idle_handle());
1264
  }
1265
}
1266
1267
1268
46863
Local<Value> Environment::GetNow() {
1269
46863
  uv_update_time(event_loop());
1270
46863
  uint64_t now = uv_now(event_loop());
1271
46863
  CHECK_GE(now, timer_base());
1272
46863
  now -= timer_base();
1273
46863
  if (now <= 0xffffffff)
1274
93726
    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
6246
ImmediateInfo::ImmediateInfo(Isolate* isolate, const SerializeInfo* info)
1340
6246
    : 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
5466
void ImmediateInfo::Deserialize(Local<Context> context) {
1348
5466
  fields_.Deserialize(context);
1349
5466
}
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
5466
void TickInfo::Deserialize(Local<Context> context) {
1367
5466
  fields_.Deserialize(context);
1368
5466
}
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
6246
TickInfo::TickInfo(Isolate* isolate, const SerializeInfo* info)
1381
    : fields_(
1382
6246
          isolate, kFieldsCount, info == nullptr ? nullptr : &(info->fields)) {}
1383
1384
6246
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

6246
      info_(info) {
1390
12492
  HandleScope handle_scope(isolate);
1391
6246
  if (info == nullptr) {
1392
780
    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
780
    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
780
    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
780
    async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1;
1410
  }
1411
6246
}
1412
1413
5466
void AsyncHooks::Deserialize(Local<Context> context) {
1414
5466
  async_ids_stack_.Deserialize(context);
1415
5466
  fields_.Deserialize(context);
1416
5466
  async_id_fields_.Deserialize(context);
1417
1418
  Local<Array> js_execution_async_resources;
1419
5466
  if (info_->js_execution_async_resources != 0) {
1420
    js_execution_async_resources =
1421
5466
        context->GetDataFromSnapshotOnce<Array>(
1422
16398
            info_->js_execution_async_resources).ToLocalChecked();
1423
  } else {
1424
    js_execution_async_resources = Array::New(context->GetIsolate());
1425
  }
1426
5466
  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
5466
  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
5466
  info_ = nullptr;
1442
5466
}
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
624
void Environment::Exit(int exit_code) {
1524
624
  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
624
  process_exit_handler_(this, exit_code);
1543
63
}
1544
1545
6266
void Environment::stop_sub_worker_contexts() {
1546
  DCHECK_EQ(Isolate::GetCurrent(), isolate());
1547
1548
6266
  while (!sub_worker_contexts_.empty()) {
1549
27
    Worker* w = *sub_worker_contexts_.begin();
1550
27
    remove_sub_worker_context(w);
1551
27
    w->Exit(1);
1552
27
    w->JoinThread();
1553
  }
1554
6239
}
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
68693
void Environment::AddUnmanagedFd(int fd) {
1562
68693
  if (!tracks_unmanaged_fds()) return;
1563
2723
  auto result = unmanaged_fds_.insert(fd);
1564
2723
  if (!result.second) {
1565
    ProcessEmitWarning(
1566
1
        this, "File descriptor %d opened in unmanaged mode twice", fd);
1567
  }
1568
}
1569
1570
68304
void Environment::RemoveUnmanagedFd(int fd) {
1571
68304
  if (!tracks_unmanaged_fds()) return;
1572
2720
  size_t removed_count = unmanaged_fds_.erase(fd);
1573
2720
  if (removed_count == 0) {
1574
    ProcessEmitWarning(
1575
1
        this, "File descriptor %d closed but not opened in unmanaged mode", fd);
1576
  }
1577
}
1578
1579
5298
void Environment::PrintInfoForSnapshotIfDebug() {
1580
10596
  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
5298
}
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
5298
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
5298
  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
21864
void Environment::EnqueueDeserializeRequest(DeserializeRequestCallback cb,
1664
                                            Local<Object> holder,
1665
                                            int index,
1666
                                            InternalFieldInfoBase* info) {
1667
  DCHECK_EQ(index, BaseObject::kEmbedderType);
1668
43728
  DeserializeRequest request{cb, {isolate(), holder}, index, info};
1669
21864
  deserialize_requests_.push_back(std::move(request));
1670
21864
}
1671
1672
5466
void Environment::RunDeserializeRequests() {
1673
10932
  HandleScope scope(isolate());
1674
5466
  Local<Context> ctx = context();
1675
5466
  Isolate* is = isolate();
1676
27330
  while (!deserialize_requests_.empty()) {
1677
43728
    DeserializeRequest request(std::move(deserialize_requests_.front()));
1678
21864
    deserialize_requests_.pop_front();
1679
21864
    Local<Object> holder = request.holder.Get(is);
1680
21864
    request.cb(ctx, holder, request.index, request.info);
1681
    request.holder.Reset();
1682
21864
    request.info->Delete();
1683
  }
1684
5466
}
1685
1686
5466
void Environment::DeserializeProperties(const EnvSerializeInfo* info) {
1687
5466
  Local<Context> ctx = context();
1688
1689
5466
  RunDeserializeRequests();
1690
1691
5466
  builtins_in_snapshot = info->builtins;
1692
5466
  async_hooks_.Deserialize(ctx);
1693
5466
  immediate_info_.Deserialize(ctx);
1694
5466
  tick_info_.Deserialize(ctx);
1695
5466
  performance_state_->Deserialize(ctx);
1696
5466
  exiting_.Deserialize(ctx);
1697
5466
  stream_base_state_.Deserialize(ctx);
1698
5466
  should_abort_on_uncaught_toggle_.Deserialize(ctx);
1699
1700
5466
  principal_realm_->DeserializeProperties(&info->principal_realm);
1701
1702
5466
  if (enabled_debug_list_.enabled(DebugCategory::MKSNAPSHOT)) {
1703
    fprintf(stderr, "deserializing...\n");
1704
    std::cerr << *info << "\n";
1705
  }
1706
5466
}
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
636
    if (obj->IsDoneInitializing())
1735
635
      tracker.Track(obj);
1736
636
  });
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
741627
void Environment::RunWeakRefCleanup() {
1907
741627
  isolate()->ClearKeptObjects();
1908
741627
}
1909
1910
// Not really any better place than env.cc at this moment.
1911
1456462
BaseObject::BaseObject(Environment* env, Local<Object> object)
1912
2912924
    : persistent_handle_(env->isolate(), object), env_(env) {
1913
1456462
  CHECK_EQ(false, object.IsEmpty());
1914
1456462
  CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
1915
1456462
  object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
1916
                                           &kNodeEmbedderId);
1917
1456462
  object->SetAlignedPointerInInternalField(BaseObject::kSlot,
1918
                                           static_cast<void*>(this));
1919
1456462
  env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
1920
1456462
  env->modify_base_object_count(1);
1921
1456462
}
1922
1923

7923168
BaseObject::~BaseObject() {
1924
2886482
  env()->modify_base_object_count(-1);
1925
2886482
  env()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
1926
1927
2886482
  if (UNLIKELY(has_pointer_data())) {
1928
368084
    PointerData* metadata = pointer_data();
1929
368084
    CHECK_EQ(metadata->strong_ptr_count, 0);
1930
368084
    metadata->self = nullptr;
1931
368084
    if (metadata->weak_ptr_count == 0) delete metadata;
1932
  }
1933
1934
2886482
  if (persistent_handle_.IsEmpty()) {
1935
    // This most likely happened because the weak callback below cleared it.
1936
2150204
    return;
1937
  }
1938
1939
  {
1940
736278
    HandleScope handle_scope(env()->isolate());
1941
1472556
    object()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
1942
  }
1943
}
1944
1945
1243195
void BaseObject::MakeWeak() {
1946
1243195
  if (has_pointer_data()) {
1947
42687
    pointer_data()->wants_weak_jsobj = true;
1948
42687
    if (pointer_data()->strong_ptr_count > 0) return;
1949
  }
1950
1951
2486388
  persistent_handle_.SetWeak(
1952
      this,
1953
1075101
      [](const WeakCallbackInfo<BaseObject>& data) {
1954
1075101
        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
1075101
        obj->persistent_handle_.Reset();
1960

1075101
        CHECK_IMPLIES(obj->has_pointer_data(),
1961
                      obj->pointer_data()->strong_ptr_count == 0);
1962
1075101
        obj->OnGCCollect();
1963
1075101
      },
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
23677
void BaseObject::LazilyInitializedJSTemplateConstructor(
1975
    const FunctionCallbackInfo<Value>& args) {
1976
  DCHECK(args.IsConstructCall());
1977
23677
  CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount);
1978
23677
  args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
1979
                                                &kNodeEmbedderId);
1980
23677
  args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
1981
23677
}
1982
1983
21697
Local<FunctionTemplate> BaseObject::MakeLazilyInitializedJSTemplate(
1984
    Environment* env) {
1985
  Local<FunctionTemplate> t = NewFunctionTemplate(
1986
21697
      env->isolate(), LazilyInitializedJSTemplateConstructor);
1987
21697
  t->Inherit(BaseObject::GetConstructorTemplate(env));
1988
43394
  t->InstanceTemplate()->SetInternalFieldCount(BaseObject::kInternalFieldCount);
1989
21697
  return t;
1990
}
1991
1992
2903121
BaseObject::PointerData* BaseObject::pointer_data() {
1993
2903121
  if (!has_pointer_data()) {
1994
186428
    PointerData* metadata = new PointerData();
1995
186428
    metadata->wants_weak_jsobj = persistent_handle_.IsWeak();
1996
186428
    metadata->self = this;
1997
186428
    pointer_data_ = metadata;
1998
  }
1999
2903121
  CHECK(has_pointer_data());
2000
2903121
  return pointer_data_;
2001
}
2002
2003
745518
void BaseObject::decrease_refcount() {
2004
745518
  CHECK(has_pointer_data());
2005
745518
  PointerData* metadata = pointer_data();
2006
745518
  CHECK_GT(metadata->strong_ptr_count, 0);
2007
745518
  unsigned int new_refcount = --metadata->strong_ptr_count;
2008
745518
  if (new_refcount == 0) {
2009
251239
    if (metadata->is_detached) {
2010
178083
      OnGCCollect();
2011

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

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

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