GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_realm.cc Lines: 115 139 82.7 %
Date: 2022-09-29 04:23:51 Branches: 327 706 46.3 %

Line Branch Exec Source
1
#include "node_realm.h"
2
#include "env-inl.h"
3
4
#include "memory_tracker-inl.h"
5
#include "node_builtins.h"
6
#include "node_process.h"
7
#include "util.h"
8
9
namespace node {
10
11
using builtins::BuiltinLoader;
12
using v8::Context;
13
using v8::EscapableHandleScope;
14
using v8::Function;
15
using v8::HandleScope;
16
using v8::Local;
17
using v8::MaybeLocal;
18
using v8::Object;
19
using v8::SnapshotCreator;
20
using v8::String;
21
using v8::Value;
22
23
6305
Realm::Realm(Environment* env,
24
             v8::Local<v8::Context> context,
25
6305
             const RealmSerializeInfo* realm_info)
26
6305
    : env_(env), isolate_(context->GetIsolate()) {
27
6305
  context_.Reset(isolate_, context);
28
29
  // Create properties if not deserializing from snapshot.
30
  // Or the properties are deserialized with DeserializeProperties() when the
31
  // env drained the deserialize requests.
32
6305
  if (realm_info == nullptr) {
33
781
    CreateProperties();
34
  }
35
6305
}
36
37
756096
Realm::~Realm() {
38
11456
  CHECK_EQ(base_object_count_, 0);
39
22912
}
40
41
25
void Realm::MemoryInfo(MemoryTracker* tracker) const {
42
#define V(PropertyName, TypeName)                                              \
43
  tracker->TrackField(#PropertyName, PropertyName());
44
25
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
45
#undef V
46
47
25
  tracker->TrackField("env", env_);
48
25
  tracker->TrackField("cleanup_queue", cleanup_queue_);
49
50
25
  ForEachBaseObject([&](BaseObject* obj) {
51
650
    if (obj->IsDoneInitializing()) {
52
649
      tracker->Track(obj);
53
    }
54
650
  });
55
25
}
56
57
781
void Realm::CreateProperties() {
58
1562
  HandleScope handle_scope(isolate_);
59
781
  Local<Context> ctx = context();
60
61
  // Store primordials setup by the per-context script in the environment.
62
  Local<Object> per_context_bindings =
63
1562
      GetPerContextExports(ctx).ToLocalChecked();
64
  Local<Value> primordials =
65
1562
      per_context_bindings->Get(ctx, env_->primordials_string())
66
781
          .ToLocalChecked();
67
781
  CHECK(primordials->IsObject());
68
781
  set_primordials(primordials.As<Object>());
69
70
  Local<String> prototype_string =
71
781
      FIXED_ONE_BYTE_STRING(isolate(), "prototype");
72
73
#define V(EnvPropertyName, PrimordialsPropertyName)                            \
74
  {                                                                            \
75
    Local<Value> ctor =                                                        \
76
        primordials.As<Object>()                                               \
77
            ->Get(ctx,                                                         \
78
                  FIXED_ONE_BYTE_STRING(isolate(), PrimordialsPropertyName))   \
79
            .ToLocalChecked();                                                 \
80
    CHECK(ctor->IsObject());                                                   \
81
    Local<Value> prototype =                                                   \
82
        ctor.As<Object>()->Get(ctx, prototype_string).ToLocalChecked();        \
83
    CHECK(prototype->IsObject());                                              \
84
    set_##EnvPropertyName(prototype.As<Object>());                             \
85
  }
86
87

4686
  V(primordials_safe_map_prototype_object, "SafeMap");
88

4686
  V(primordials_safe_set_prototype_object, "SafeSet");
89

4686
  V(primordials_safe_weak_map_prototype_object, "SafeWeakMap");
90

4686
  V(primordials_safe_weak_set_prototype_object, "SafeWeakSet");
91
#undef V
92
93
  // TODO(legendecas): some methods probably doesn't need to be created with
94
  // process. Distinguish them and create process object only in the principal
95
  // realm.
96
  Local<Object> process_object =
97
781
      node::CreateProcessObject(this).FromMaybe(Local<Object>());
98
781
  set_process_object(process_object);
99
781
}
100
101
6
RealmSerializeInfo Realm::Serialize(SnapshotCreator* creator) {
102
6
  RealmSerializeInfo info;
103
6
  Local<Context> ctx = context();
104
105
6
  uint32_t id = 0;
106
#define V(PropertyName, TypeName)                                              \
107
  do {                                                                         \
108
    Local<TypeName> field = PropertyName();                                    \
109
    if (!field.IsEmpty()) {                                                    \
110
      size_t index = creator->AddData(ctx, field);                             \
111
      info.persistent_values.push_back({#PropertyName, id, index});            \
112
    }                                                                          \
113
    id++;                                                                      \
114
  } while (0);
115































582
  PER_REALM_STRONG_PERSISTENT_VALUES(V)
116
#undef V
117
118
  // Do this after other creator->AddData() calls so that Snapshotable objects
119
  // can use 0 to indicate that a SnapshotIndex is invalid.
120
6
  SerializeSnapshotableObjects(this, creator, &info);
121
122
6
  info.context = creator->AddData(ctx, ctx);
123
6
  return info;
124
}
125
126
5524
void Realm::DeserializeProperties(const RealmSerializeInfo* info) {
127
5524
  Local<Context> ctx = context();
128
129
5524
  const std::vector<PropInfo>& values = info->persistent_values;
130
5524
  size_t i = 0;  // index to the array
131
5524
  uint32_t id = 0;
132
#define V(PropertyName, TypeName)                                              \
133
  do {                                                                         \
134
    if (values.size() > i && id == values[i].id) {                             \
135
      const PropInfo& d = values[i];                                           \
136
      DCHECK_EQ(d.name, #PropertyName);                                        \
137
      MaybeLocal<TypeName> maybe_field =                                       \
138
          ctx->GetDataFromSnapshotOnce<TypeName>(d.index);                     \
139
      Local<TypeName> field;                                                   \
140
      if (!maybe_field.ToLocal(&field)) {                                      \
141
        fprintf(stderr,                                                        \
142
                "Failed to deserialize realm value " #PropertyName "\n");      \
143
      }                                                                        \
144
      set_##PropertyName(field);                                               \
145
      i++;                                                                     \
146
    }                                                                          \
147
    id++;                                                                      \
148
  } while (0);
149
150






























































































































552400
  PER_REALM_STRONG_PERSISTENT_VALUES(V);
151
#undef V
152
153
  MaybeLocal<Context> maybe_ctx_from_snapshot =
154
11048
      ctx->GetDataFromSnapshotOnce<Context>(info->context);
155
  Local<Context> ctx_from_snapshot;
156
5524
  if (!maybe_ctx_from_snapshot.ToLocal(&ctx_from_snapshot)) {
157
    fprintf(stderr,
158
            "Failed to deserialize context back reference from the snapshot\n");
159
  }
160
5524
  CHECK_EQ(ctx_from_snapshot, ctx);
161
162
5524
  DoneBootstrapping();
163
5524
}
164
165
10195
MaybeLocal<Value> Realm::ExecuteBootstrapper(const char* id) {
166
10195
  EscapableHandleScope scope(isolate());
167
10195
  Local<Context> ctx = context();
168
10195
  MaybeLocal<Value> result = BuiltinLoader::CompileAndCall(ctx, id, this);
169
170
  // If there was an error during bootstrap, it must be unrecoverable
171
  // (e.g. max call stack exceeded). Clear the stack so that the
172
  // AsyncCallbackScope destructor doesn't fail on the id check.
173
  // There are only two ways to have a stack size > 1: 1) the user manually
174
  // called MakeCallback or 2) user awaited during bootstrap, which triggered
175
  // _tickCallback().
176
9922
  if (result.IsEmpty()) {
177
31
    env()->async_hooks()->clear_async_id_stack();
178
  }
179
180
9922
  return scope.EscapeMaybe(result);
181
}
182
183
781
MaybeLocal<Value> Realm::BootstrapInternalLoaders() {
184
781
  EscapableHandleScope scope(isolate_);
185
186
  // Bootstrap internal loaders
187
  Local<Value> loader_exports;
188
781
  if (!ExecuteBootstrapper("internal/bootstrap/loaders")
189
781
           .ToLocal(&loader_exports)) {
190
    return MaybeLocal<Value>();
191
  }
192
781
  CHECK(loader_exports->IsObject());
193
781
  Local<Object> loader_exports_obj = loader_exports.As<Object>();
194
  Local<Value> internal_binding_loader =
195
1562
      loader_exports_obj->Get(context(), env_->internal_binding_string())
196
781
          .ToLocalChecked();
197
781
  CHECK(internal_binding_loader->IsFunction());
198
781
  set_internal_binding_loader(internal_binding_loader.As<Function>());
199
  Local<Value> require =
200
1562
      loader_exports_obj->Get(context(), env_->require_string())
201
781
          .ToLocalChecked();
202
781
  CHECK(require->IsFunction());
203
781
  set_builtin_module_require(require.As<Function>());
204
205
781
  return scope.Escape(loader_exports);
206
}
207
208
781
MaybeLocal<Value> Realm::BootstrapNode() {
209
781
  EscapableHandleScope scope(isolate_);
210
211
781
  MaybeLocal<Value> result = ExecuteBootstrapper("internal/bootstrap/node");
212
213
781
  if (result.IsEmpty()) {
214
    return MaybeLocal<Value>();
215
  }
216
217
781
  if (!env_->no_browser_globals()) {
218
781
    result = ExecuteBootstrapper("internal/bootstrap/browser");
219
220
781
    if (result.IsEmpty()) {
221
      return MaybeLocal<Value>();
222
    }
223
  }
224
225
  // TODO(joyeecheung): skip these in the snapshot building for workers.
226
  auto thread_switch_id =
227
781
      env_->is_main_thread() ? "internal/bootstrap/switches/is_main_thread"
228
781
                             : "internal/bootstrap/switches/is_not_main_thread";
229
781
  result = ExecuteBootstrapper(thread_switch_id);
230
231
781
  if (result.IsEmpty()) {
232
    return MaybeLocal<Value>();
233
  }
234
235
  auto process_state_switch_id =
236
781
      env_->owns_process_state()
237
781
          ? "internal/bootstrap/switches/does_own_process_state"
238
781
          : "internal/bootstrap/switches/does_not_own_process_state";
239
781
  result = ExecuteBootstrapper(process_state_switch_id);
240
241
781
  if (result.IsEmpty()) {
242
    return MaybeLocal<Value>();
243
  }
244
245
781
  Local<String> env_string = FIXED_ONE_BYTE_STRING(isolate_, "env");
246
  Local<Object> env_var_proxy;
247
2343
  if (!CreateEnvVarProxy(context(), isolate_).ToLocal(&env_var_proxy) ||
248

3124
      process_object()->Set(context(), env_string, env_var_proxy).IsNothing()) {
249
    return MaybeLocal<Value>();
250
  }
251
252
781
  return scope.EscapeMaybe(result);
253
}
254
255
781
MaybeLocal<Value> Realm::RunBootstrapping() {
256
781
  EscapableHandleScope scope(isolate_);
257
258
781
  CHECK(!has_run_bootstrapping_code());
259
260
1562
  if (BootstrapInternalLoaders().IsEmpty()) {
261
    return MaybeLocal<Value>();
262
  }
263
264
  Local<Value> result;
265
1562
  if (!BootstrapNode().ToLocal(&result)) {
266
    return MaybeLocal<Value>();
267
  }
268
269
781
  DoneBootstrapping();
270
271
781
  return scope.Escape(result);
272
}
273
274
6305
void Realm::DoneBootstrapping() {
275
  // Make sure that no request or handle is created during bootstrap -
276
  // if necessary those should be done in pre-execution.
277
  // Usually, doing so would trigger the checks present in the ReqWrap and
278
  // HandleWrap classes, so this is only a consistency check.
279
280
  // TODO(legendecas): track req_wrap and handle_wrap by realms instead of
281
  // environments.
282
6305
  CHECK(env_->req_wrap_queue()->IsEmpty());
283
6305
  CHECK(env_->handle_wrap_queue()->IsEmpty());
284
285
6305
  has_run_bootstrapping_code_ = true;
286
287
  // This adjusts the return value of base_object_created_after_bootstrap() so
288
  // that tests that check the count do not have to account for internally
289
  // created BaseObjects.
290
6305
  base_object_created_by_bootstrap_ = base_object_count_;
291
6305
}
292
293
5722
void Realm::RunCleanup() {
294

16411
  TRACE_EVENT0(TRACING_CATEGORY_NODE1(realm), "RunCleanup");
295
296
5722
  cleanup_queue_.Drain();
297
5722
}
298
299
void Realm::PrintInfoForSnapshot() {
300
  fprintf(stderr, "Realm = %p\n", this);
301
  fprintf(stderr, "BaseObjects of the Realm:\n");
302
  size_t i = 0;
303
  ForEachBaseObject([&](BaseObject* obj) {
304
    std::cout << "#" << i++ << " " << obj << ": " << obj->MemoryInfoName()
305
              << "\n";
306
  });
307
  fprintf(stderr, "End of the Realm.\n");
308
}
309
310
5345
void Realm::VerifyNoStrongBaseObjects() {
311
  // When a process exits cleanly, i.e. because the event loop ends up without
312
  // things to wait for, the Node.js objects that are left on the heap should
313
  // be:
314
  //
315
  //   1. weak, i.e. ready for garbage collection once no longer referenced, or
316
  //   2. detached, i.e. scheduled for destruction once no longer referenced, or
317
  //   3. an unrefed libuv handle, i.e. does not keep the event loop alive, or
318
  //   4. an inactive libuv handle (essentially the same here)
319
  //
320
  // There are a few exceptions to this rule, but generally, if there are
321
  // C++-backed Node.js objects on the heap that do not fall into the above
322
  // categories, we may be looking at a potential memory leak. Most likely,
323
  // the cause is a missing MakeWeak() call on the corresponding object.
324
  //
325
  // In order to avoid this kind of problem, we check the list of BaseObjects
326
  // for these criteria. Currently, we only do so when explicitly instructed to
327
  // or when in debug mode (where --verify-base-objects is always-on).
328
329
  // TODO(legendecas): introduce per-realm options.
330
5345
  if (!env()->options()->verify_base_objects) return;
331
332
  ForEachBaseObject([](BaseObject* obj) {
333
    if (obj->IsNotIndicativeOfMemoryLeakAtExit()) return;
334
    fprintf(stderr,
335
            "Found bad BaseObject during clean exit: %s\n",
336
            obj->MemoryInfoName().c_str());
337
    fflush(stderr);
338
    ABORT();
339
  });
340
}
341
342
}  // namespace node