GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_worker.cc Lines: 446 477 93.5 %
Date: 2022-05-31 04:15:47 Branches: 170 236 72.0 %

Line Branch Exec Source
1
#include "node_worker.h"
2
#include "debug_utils-inl.h"
3
#include "histogram-inl.h"
4
#include "memory_tracker-inl.h"
5
#include "node_errors.h"
6
#include "node_external_reference.h"
7
#include "node_buffer.h"
8
#include "node_options-inl.h"
9
#include "node_perf.h"
10
#include "node_snapshot_builder.h"
11
#include "util-inl.h"
12
#include "async_wrap-inl.h"
13
14
#include <memory>
15
#include <string>
16
#include <vector>
17
18
using node::kAllowedInEnvironment;
19
using node::kDisallowedInEnvironment;
20
using v8::Array;
21
using v8::ArrayBuffer;
22
using v8::Boolean;
23
using v8::Context;
24
using v8::Float64Array;
25
using v8::FunctionCallbackInfo;
26
using v8::FunctionTemplate;
27
using v8::HandleScope;
28
using v8::Integer;
29
using v8::Isolate;
30
using v8::Local;
31
using v8::Locker;
32
using v8::Maybe;
33
using v8::MaybeLocal;
34
using v8::Null;
35
using v8::Number;
36
using v8::Object;
37
using v8::ResourceConstraints;
38
using v8::SealHandleScope;
39
using v8::String;
40
using v8::TryCatch;
41
using v8::Value;
42
43
namespace node {
44
namespace worker {
45
46
constexpr double kMB = 1024 * 1024;
47
48
1032
Worker::Worker(Environment* env,
49
               Local<Object> wrap,
50
               const std::string& url,
51
               std::shared_ptr<PerIsolateOptions> per_isolate_opts,
52
               std::vector<std::string>&& exec_argv,
53
               std::shared_ptr<KVStore> env_vars,
54
1032
               const SnapshotData* snapshot_data)
55
    : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER),
56
      per_isolate_opts_(per_isolate_opts),
57
      exec_argv_(exec_argv),
58
1032
      platform_(env->isolate_data()->platform()),
59
1032
      thread_id_(AllocateEnvironmentThreadId()),
60
      env_vars_(env_vars),
61
2064
      snapshot_data_(snapshot_data) {
62
1032
  Debug(this, "Creating new worker instance with thread id %llu",
63
1032
        thread_id_.id);
64
65
  // Set up everything that needs to be set up in the parent environment.
66
1032
  MessagePort* parent_port = MessagePort::New(env, env->context());
67
1032
  if (parent_port == nullptr) {
68
    // This can happen e.g. because execution is terminating.
69
    return;
70
  }
71
72
1032
  child_port_data_ = std::make_unique<MessagePortData>(nullptr);
73
1032
  MessagePort::Entangle(parent_port, child_port_data_.get());
74
75
1032
  object()
76
3096
      ->Set(env->context(), env->message_port_string(), parent_port->object())
77
      .Check();
78
79
1032
  object()->Set(env->context(),
80
                env->thread_id_string(),
81
3096
                Number::New(env->isolate(), static_cast<double>(thread_id_.id)))
82
      .Check();
83
84
2064
  inspector_parent_handle_ = GetInspectorParentHandle(
85
1032
      env, thread_id_, url.c_str());
86
87
2064
  argv_ = std::vector<std::string>{env->argv()[0]};
88
  // Mark this Worker object as weak until we actually start the thread.
89
1032
  MakeWeak();
90
91
1032
  Debug(this, "Preparation for worker %llu finished", thread_id_.id);
92
}
93
94
3166
bool Worker::is_stopped() const {
95
6332
  Mutex::ScopedLock lock(mutex_);
96
3166
  if (env_ != nullptr)
97
788
    return env_->is_stopping();
98
2378
  return stopped_;
99
}
100
101
800
void Worker::UpdateResourceConstraints(ResourceConstraints* constraints) {
102
800
  constraints->set_stack_limit(reinterpret_cast<uint32_t*>(stack_base_));
103
104
800
  if (resource_limits_[kMaxYoungGenerationSizeMb] > 0) {
105
1
    constraints->set_max_young_generation_size_in_bytes(
106
1
        static_cast<size_t>(resource_limits_[kMaxYoungGenerationSizeMb] * kMB));
107
  } else {
108
799
    resource_limits_[kMaxYoungGenerationSizeMb] =
109
799
        constraints->max_young_generation_size_in_bytes() / kMB;
110
  }
111
112
800
  if (resource_limits_[kMaxOldGenerationSizeMb] > 0) {
113
3
    constraints->set_max_old_generation_size_in_bytes(
114
3
        static_cast<size_t>(resource_limits_[kMaxOldGenerationSizeMb] * kMB));
115
  } else {
116
797
    resource_limits_[kMaxOldGenerationSizeMb] =
117
797
        constraints->max_old_generation_size_in_bytes() / kMB;
118
  }
119
120
800
  if (resource_limits_[kCodeRangeSizeMb] > 0) {
121
1
    constraints->set_code_range_size_in_bytes(
122
1
        static_cast<size_t>(resource_limits_[kCodeRangeSizeMb] * kMB));
123
  } else {
124
799
    resource_limits_[kCodeRangeSizeMb] =
125
799
        constraints->code_range_size_in_bytes() / kMB;
126
  }
127
800
}
128
129
// This class contains data that is only relevant to the child thread itself,
130
// and only while it is running.
131
// (Eventually, the Environment instance should probably also be moved here.)
132
class WorkerThreadData {
133
 public:
134
1029
  explicit WorkerThreadData(Worker* w)
135
1029
    : w_(w) {
136
1029
    int ret = uv_loop_init(&loop_);
137
1029
    if (ret != 0) {
138
      char err_buf[128];
139
229
      uv_err_name_r(ret, err_buf, sizeof(err_buf));
140
229
      w->Exit(1, "ERR_WORKER_INIT_FAILED", err_buf);
141
229
      return;
142
    }
143
800
    loop_init_failed_ = false;
144
800
    uv_loop_configure(&loop_, UV_METRICS_IDLE_TIME);
145
146
    std::shared_ptr<ArrayBufferAllocator> allocator =
147
800
        ArrayBufferAllocator::Create();
148
800
    Isolate::CreateParams params;
149
800
    SetIsolateCreateParamsForNode(&params);
150
800
    params.array_buffer_allocator_shared = allocator;
151
152
800
    if (w->snapshot_data() != nullptr) {
153
799
      SnapshotBuilder::InitializeIsolateParams(w->snapshot_data(), &params);
154
    }
155
800
    w->UpdateResourceConstraints(&params.constraints);
156
157
800
    Isolate* isolate = Isolate::Allocate();
158
800
    if (isolate == nullptr) {
159
      w->Exit(1, "ERR_WORKER_INIT_FAILED", "Failed to create new Isolate");
160
      return;
161
    }
162
163
800
    w->platform_->RegisterIsolate(isolate, &loop_);
164
800
    Isolate::Initialize(isolate, params);
165
800
    SetIsolateUpForNode(isolate);
166
167
    // Be sure it's called before Environment::InitializeDiagnostics()
168
    // so that this callback stays when the callback of
169
    // --heapsnapshot-near-heap-limit gets is popped.
170
800
    isolate->AddNearHeapLimitCallback(Worker::NearHeapLimit, w);
171
172
    {
173
800
      Locker locker(isolate);
174
1600
      Isolate::Scope isolate_scope(isolate);
175
      // V8 computes its stack limit the first time a `Locker` is used based on
176
      // --stack-size. Reset it to the correct value.
177
800
      isolate->SetStackLimit(w->stack_base_);
178
179
800
      HandleScope handle_scope(isolate);
180
1600
      isolate_data_.reset(CreateIsolateData(isolate,
181
                                            &loop_,
182
800
                                            w_->platform_,
183
                                            allocator.get()));
184
800
      CHECK(isolate_data_);
185
800
      if (w_->per_isolate_opts_)
186
272
        isolate_data_->set_options(std::move(w_->per_isolate_opts_));
187
800
      isolate_data_->set_worker_context(w_);
188
800
      isolate_data_->max_young_gen_size =
189
800
          params.constraints.max_young_generation_size_in_bytes();
190
    }
191
192
800
    Mutex::ScopedLock lock(w_->mutex_);
193
800
    w_->isolate_ = isolate;
194
  }
195
196
1029
  ~WorkerThreadData() {
197
1029
    Debug(w_, "Worker %llu dispose isolate", w_->thread_id_.id);
198
    Isolate* isolate;
199
    {
200
1029
      Mutex::ScopedLock lock(w_->mutex_);
201
1029
      isolate = w_->isolate_;
202
1029
      w_->isolate_ = nullptr;
203
    }
204
205
1029
    if (isolate != nullptr) {
206
800
      CHECK(!loop_init_failed_);
207
800
      bool platform_finished = false;
208
209
800
      isolate_data_.reset();
210
211
800
      w_->platform_->AddIsolateFinishedCallback(isolate, [](void* data) {
212
800
        *static_cast<bool*>(data) = true;
213
800
      }, &platform_finished);
214
215
      // The order of these calls is important; if the Isolate is first disposed
216
      // and then unregistered, there is a race condition window in which no
217
      // new Isolate at the same address can successfully be registered with
218
      // the platform.
219
      // (Refs: https://github.com/nodejs/node/issues/30846)
220
800
      w_->platform_->UnregisterIsolate(isolate);
221
800
      isolate->Dispose();
222
223
      // Wait until the platform has cleaned up all relevant resources.
224
1600
      while (!platform_finished) {
225
800
        uv_run(&loop_, UV_RUN_ONCE);
226
      }
227
    }
228
1029
    if (!loop_init_failed_) {
229
800
      CheckedUvLoopClose(&loop_);
230
    }
231
1029
  }
232
233
800
  bool loop_is_usable() const { return !loop_init_failed_; }
234
235
 private:
236
  Worker* const w_;
237
  uv_loop_t loop_;
238
  bool loop_init_failed_ = true;
239
  DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data_;
240
  const SnapshotData* snapshot_data_ = nullptr;
241
  friend class Worker;
242
};
243
244
3
size_t Worker::NearHeapLimit(void* data, size_t current_heap_limit,
245
                             size_t initial_heap_limit) {
246
3
  Worker* worker = static_cast<Worker*>(data);
247
3
  worker->Exit(1, "ERR_WORKER_OUT_OF_MEMORY", "JS heap out of memory");
248
  // Give the current GC some extra leeway to let it finish rather than
249
  // crash hard. We are not going to perform further allocations anyway.
250
3
  constexpr size_t kExtraHeapAllowance = 16 * 1024 * 1024;
251
3
  return current_heap_limit + kExtraHeapAllowance;
252
}
253
254
1029
void Worker::Run() {
255
1029
  std::string name = "WorkerThread ";
256
1029
  name += std::to_string(thread_id_.id);
257

1201
  TRACE_EVENT_METADATA1(
258
      "__metadata", "thread_name", "name",
259
      TRACE_STR_COPY(name.c_str()));
260
1029
  CHECK_NOT_NULL(platform_);
261
262
1029
  Debug(this, "Creating isolate for worker with id %llu", thread_id_.id);
263
264
1029
  WorkerThreadData data(this);
265
1029
  if (isolate_ == nullptr) return;
266
800
  CHECK(data.loop_is_usable());
267
268
800
  Debug(this, "Starting worker with id %llu", thread_id_.id);
269
  {
270
800
    Locker locker(isolate_);
271
800
    Isolate::Scope isolate_scope(isolate_);
272
800
    SealHandleScope outer_seal(isolate_);
273
274
800
    DeleteFnPtr<Environment, FreeEnvironment> env_;
275
800
    auto cleanup_env = OnScopeLeave([&]() {
276
      // TODO(addaleax): This call is harmless but should not be necessary.
277
      // Figure out why V8 is raising a DCHECK() here without it
278
      // (in test/parallel/test-async-hooks-worker-asyncfn-terminate-4.js).
279
800
      isolate_->CancelTerminateExecution();
280
281
800
      if (!env_) return;
282
789
      env_->set_can_call_into_js(false);
283
284
      {
285
789
        Mutex::ScopedLock lock(mutex_);
286
789
        stopped_ = true;
287
789
        this->env_ = nullptr;
288
      }
289
290
789
      env_.reset();
291
800
    });
292
293
800
    if (is_stopped()) return;
294
    {
295
789
      HandleScope handle_scope(isolate_);
296
      Local<Context> context;
297
      {
298
        // We create the Context object before we have an Environment* in place
299
        // that we could use for error handling. If creation fails due to
300
        // resource constraints, we need something in place to handle it,
301
        // though.
302
789
        TryCatch try_catch(isolate_);
303
789
        if (snapshot_data_ != nullptr) {
304
788
          context = Context::FromSnapshot(isolate_,
305
788
                                          SnapshotData::kNodeBaseContextIndex)
306
788
                        .ToLocalChecked();
307
1576
          if (!context.IsEmpty() &&
308

2364
              !InitializeContextRuntime(context).IsJust()) {
309
            context = Local<Context>();
310
          }
311
        } else {
312
1
          context = NewContext(isolate_);
313
        }
314
789
        if (context.IsEmpty()) {
315
          Exit(1, "ERR_WORKER_INIT_FAILED", "Failed to create new Context");
316
          return;
317
        }
318
      }
319
320
789
      if (is_stopped()) return;
321
789
      CHECK(!context.IsEmpty());
322
789
      Context::Scope context_scope(context);
323
      {
324
1578
        env_.reset(CreateEnvironment(
325
            data.isolate_data_.get(),
326
            context,
327
789
            std::move(argv_),
328
789
            std::move(exec_argv_),
329
789
            static_cast<EnvironmentFlags::Flags>(environment_flags_),
330
            thread_id_,
331
789
            std::move(inspector_parent_handle_)));
332
789
        if (is_stopped()) return;
333
788
        CHECK_NOT_NULL(env_);
334
788
        env_->set_env_vars(std::move(env_vars_));
335
788
        SetProcessExitHandler(env_.get(), [this](Environment*, int exit_code) {
336
72
          Exit(exit_code);
337
72
        });
338
      }
339
      {
340
788
        Mutex::ScopedLock lock(mutex_);
341
788
        if (stopped_) return;
342
788
        this->env_ = env_.get();
343
      }
344
788
      Debug(this, "Created Environment for worker with id %llu", thread_id_.id);
345
788
      if (is_stopped()) return;
346
      {
347
788
        if (!CreateEnvMessagePort(env_.get())) {
348
          return;
349
        }
350
351
788
        Debug(this, "Created message port for worker %llu", thread_id_.id);
352
1576
        if (LoadEnvironment(env_.get(), StartExecutionCallback{}).IsEmpty())
353
          return;
354
355
788
        Debug(this, "Loaded environment for worker %llu", thread_id_.id);
356
      }
357
    }
358
359
    {
360
788
      Maybe<int> exit_code = SpinEventLoop(env_.get());
361
1576
      Mutex::ScopedLock lock(mutex_);
362

1209
      if (exit_code_ == 0 && exit_code.IsJust()) {
363
402
        exit_code_ = exit_code.FromJust();
364
      }
365
366
788
      Debug(this, "Exiting thread for worker %llu with exit code %d",
367
788
            thread_id_.id, exit_code_);
368
    }
369
  }
370
371
788
  Debug(this, "Worker %llu thread stops", thread_id_.id);
372
}
373
374
788
bool Worker::CreateEnvMessagePort(Environment* env) {
375
1576
  HandleScope handle_scope(isolate_);
376
1576
  std::unique_ptr<MessagePortData> data;
377
  {
378
1576
    Mutex::ScopedLock lock(mutex_);
379
788
    data = std::move(child_port_data_);
380
  }
381
382
  // Set up the message channel for receiving messages in the child.
383
1576
  MessagePort* child_port = MessagePort::New(env,
384
                                             env->context(),
385
788
                                             std::move(data));
386
  // MessagePort::New() may return nullptr if execution is terminated
387
  // within it.
388
788
  if (child_port != nullptr)
389
788
    env->set_message_port(child_port->object(isolate_));
390
391
788
  return child_port;
392
}
393
394
1033
void Worker::JoinThread() {
395
1033
  if (!tid_.has_value())
396
4
    return;
397
1029
  CHECK_EQ(uv_thread_join(&tid_.value()), 0);
398
1029
  tid_.reset();
399
400
1029
  env()->remove_sub_worker_context(this);
401
402
  {
403
2056
    HandleScope handle_scope(env()->isolate());
404
1029
    Context::Scope context_scope(env()->context());
405
406
    // Reset the parent port as we're closing it now anyway.
407
1029
    object()->Set(env()->context(),
408
                  env()->message_port_string(),
409
3087
                  Undefined(env()->isolate())).Check();
410
411
    Local<Value> args[] = {
412
        Integer::New(env()->isolate(), exit_code_),
413
1029
        custom_error_ != nullptr
414
464
            ? OneByteString(env()->isolate(), custom_error_).As<Value>()
415
2391
            : Null(env()->isolate()).As<Value>(),
416
2058
        !custom_error_str_.empty()
417
232
            ? OneByteString(env()->isolate(), custom_error_str_.c_str())
418
232
                  .As<Value>()
419
2391
            : Null(env()->isolate()).As<Value>(),
420

3087
    };
421
422
1029
    MakeCallback(env()->onexit_string(), arraysize(args), args);
423
  }
424
425
  // If we get here, the tid_.has_value() condition at the top of the function
426
  // implies that the thread was running. In that case, its final action will
427
  // be to schedule a callback on the parent thread which will delete this
428
  // object, so there's nothing more to do here.
429
}
430
431
4032
Worker::~Worker() {
432
4032
  Mutex::ScopedLock lock(mutex_);
433
434
2016
  CHECK(stopped_);
435
2016
  CHECK_NULL(env_);
436
2016
  CHECK(!tid_.has_value());
437
438
2016
  Debug(this, "Worker %llu destroyed", thread_id_.id);
439
4032
}
440
441
1036
void Worker::New(const FunctionCallbackInfo<Value>& args) {
442
1036
  Environment* env = Environment::GetCurrent(args);
443
1036
  Isolate* isolate = args.GetIsolate();
444
445
1036
  CHECK(args.IsConstructCall());
446
447
1036
  if (env->isolate_data()->platform() == nullptr) {
448
    THROW_ERR_MISSING_PLATFORM_FOR_WORKER(env);
449
4
    return;
450
  }
451
452
1036
  std::string url;
453
1036
  std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr;
454
1036
  std::shared_ptr<KVStore> env_vars = nullptr;
455
456
1036
  std::vector<std::string> exec_argv_out;
457
458
  // Argument might be a string or URL
459
2072
  if (!args[0]->IsNullOrUndefined()) {
460
    Utf8Value value(
461
1680
        isolate, args[0]->ToString(env->context()).FromMaybe(Local<String>()));
462
560
    url.append(value.out(), value.length());
463
  }
464
465
2072
  if (args[1]->IsNull()) {
466
    // Means worker.env = { ...process.env }.
467
1009
    env_vars = env->env_vars()->Clone(isolate);
468
27
  } else if (args[1]->IsObject()) {
469
    // User provided env.
470
26
    env_vars = KVStore::CreateMapKVStore();
471
26
    env_vars->AssignFromObject(isolate->GetCurrentContext(),
472
78
                               args[1].As<Object>());
473
  } else {
474
    // Env is shared.
475
1
    env_vars = env->env_vars();
476
  }
477
478

2046
  if (args[1]->IsObject() || args[2]->IsArray()) {
479
276
    per_isolate_opts.reset(new PerIsolateOptions());
480
481
276
    HandleEnvOptions(per_isolate_opts->per_env, [&env_vars](const char* name) {
482
2208
      return env_vars->Get(name).FromMaybe("");
483
    });
484
485
#ifndef NODE_WITHOUT_NODE_OPTIONS
486
    MaybeLocal<String> maybe_node_opts =
487
276
        env_vars->Get(isolate, OneByteString(isolate, "NODE_OPTIONS"));
488
    Local<String> node_opts;
489
276
    if (maybe_node_opts.ToLocal(&node_opts)) {
490
825
      std::string node_options(*String::Utf8Value(isolate, node_opts));
491
275
      std::vector<std::string> errors{};
492
      std::vector<std::string> env_argv =
493
275
          ParseNodeOptionsEnvVar(node_options, &errors);
494
      // [0] is expected to be the program name, add dummy string.
495
275
      env_argv.insert(env_argv.begin(), "");
496
275
      std::vector<std::string> invalid_args{};
497
275
      options_parser::Parse(&env_argv,
498
                            nullptr,
499
                            &invalid_args,
500
                            per_isolate_opts.get(),
501
                            kAllowedInEnvironment,
502
                            &errors);
503

276
      if (!errors.empty() && args[1]->IsObject()) {
504
        // Only fail for explicitly provided env, this protects from failures
505
        // when NODE_OPTIONS from parent's env is used (which is the default).
506
        Local<Value> error;
507
2
        if (!ToV8Value(env->context(), errors).ToLocal(&error)) return;
508
        Local<String> key =
509
1
            FIXED_ONE_BYTE_STRING(env->isolate(), "invalidNodeOptions");
510
        // Ignore the return value of Set() because exceptions bubble up to JS
511
        // when we return anyway.
512
1
        USE(args.This()->Set(env->context(), key, error));
513
1
        return;
514
      }
515
    }
516
#endif  // NODE_WITHOUT_NODE_OPTIONS
517
  }
518
519
1035
  if (args[2]->IsArray()) {
520
273
    Local<Array> array = args[2].As<Array>();
521
    // The first argument is reserved for program name, but we don't need it
522
    // in workers.
523
1092
    std::vector<std::string> exec_argv = {""};
524
273
    uint32_t length = array->Length();
525
393
    for (uint32_t i = 0; i < length; i++) {
526
      Local<Value> arg;
527
240
      if (!array->Get(env->context(), i).ToLocal(&arg)) {
528
        return;
529
      }
530
      Local<String> arg_v8;
531
240
      if (!arg->ToString(env->context()).ToLocal(&arg_v8)) {
532
        return;
533
      }
534
240
      Utf8Value arg_utf8_value(args.GetIsolate(), arg_v8);
535
240
      std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
536
120
      exec_argv.push_back(arg_string);
537
    }
538
539
273
    std::vector<std::string> invalid_args{};
540
273
    std::vector<std::string> errors{};
541
    // Using invalid_args as the v8_args argument as it stores unknown
542
    // options for the per isolate parser.
543
273
    options_parser::Parse(
544
        &exec_argv,
545
        &exec_argv_out,
546
        &invalid_args,
547
        per_isolate_opts.get(),
548
        kDisallowedInEnvironment,
549
        &errors);
550
551
    // The first argument is program name.
552
273
    invalid_args.erase(invalid_args.begin());
553

273
    if (errors.size() > 0 || invalid_args.size() > 0) {
554
      Local<Value> error;
555
3
      if (!ToV8Value(env->context(),
556
3
                     errors.size() > 0 ? errors : invalid_args)
557
3
                         .ToLocal(&error)) {
558
        return;
559
      }
560
      Local<String> key =
561
3
          FIXED_ONE_BYTE_STRING(env->isolate(), "invalidExecArgv");
562
      // Ignore the return value of Set() because exceptions bubble up to JS
563
      // when we return anyway.
564
3
      USE(args.This()->Set(env->context(), key, error));
565
3
      return;
566
    }
567
  } else {
568
762
    exec_argv_out = env->exec_argv();
569
  }
570
571
1032
  bool use_node_snapshot = per_process::cli_options->node_snapshot;
572
  const SnapshotData* snapshot_data =
573
1032
      use_node_snapshot ? SnapshotBuilder::GetEmbeddedSnapshotData() : nullptr;
574
575
  Worker* worker = new Worker(env,
576
2064
                              args.This(),
577
                              url,
578
                              per_isolate_opts,
579
1032
                              std::move(exec_argv_out),
580
                              env_vars,
581
1032
                              snapshot_data);
582
583
1032
  CHECK(args[3]->IsFloat64Array());
584
2064
  Local<Float64Array> limit_info = args[3].As<Float64Array>();
585
1032
  CHECK_EQ(limit_info->Length(), kTotalResourceLimitCount);
586
1032
  limit_info->CopyContents(worker->resource_limits_,
587
                           sizeof(worker->resource_limits_));
588
589
1032
  CHECK(args[4]->IsBoolean());
590

1032
  if (args[4]->IsTrue() || env->tracks_unmanaged_fds())
591
1031
    worker->environment_flags_ |= EnvironmentFlags::kTrackUnmanagedFds;
592
1032
  if (env->hide_console_windows())
593
    worker->environment_flags_ |= EnvironmentFlags::kHideConsoleWindows;
594
1032
  if (env->no_native_addons())
595
4
    worker->environment_flags_ |= EnvironmentFlags::kNoNativeAddons;
596
1032
  if (env->no_global_search_paths())
597
    worker->environment_flags_ |= EnvironmentFlags::kNoGlobalSearchPaths;
598
1032
  if (env->no_browser_globals())
599
    worker->environment_flags_ |= EnvironmentFlags::kNoBrowserGlobals;
600
}
601
602
1029
void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
603
  Worker* w;
604
1029
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
605
2058
  Mutex::ScopedLock lock(w->mutex_);
606
607
1029
  w->stopped_ = false;
608
609
1029
  if (w->resource_limits_[kStackSizeMb] > 0) {
610
6
    if (w->resource_limits_[kStackSizeMb] * kMB < kStackBufferSize) {
611
      w->resource_limits_[kStackSizeMb] = kStackBufferSize / kMB;
612
      w->stack_size_ = kStackBufferSize;
613
    } else {
614
12
      w->stack_size_ =
615
6
          static_cast<size_t>(w->resource_limits_[kStackSizeMb] * kMB);
616
    }
617
  } else {
618
1023
    w->resource_limits_[kStackSizeMb] = w->stack_size_ / kMB;
619
  }
620
621
  uv_thread_options_t thread_options;
622
1029
  thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
623
1029
  thread_options.stack_size = w->stack_size_;
624
625
1029
  uv_thread_t* tid = &w->tid_.emplace();  // Create uv_thread_t instance
626
1029
  int ret = uv_thread_create_ex(tid, &thread_options, [](void* arg) {
627
    // XXX: This could become a std::unique_ptr, but that makes at least
628
    // gcc 6.3 detect undefined behaviour when there shouldn't be any.
629
    // gcc 7+ handles this well.
630
1029
    Worker* w = static_cast<Worker*>(arg);
631
1029
    const uintptr_t stack_top = reinterpret_cast<uintptr_t>(&arg);
632
633
    // Leave a few kilobytes just to make sure we're within limits and have
634
    // some space to do work in C++ land.
635
1029
    w->stack_base_ = stack_top - (w->stack_size_ - kStackBufferSize);
636
637
1029
    w->Run();
638
639
1029
    Mutex::ScopedLock lock(w->mutex_);
640
1029
    w->env()->SetImmediateThreadsafe(
641
1007
        [w = std::unique_ptr<Worker>(w)](Environment* env) {
642
1007
          if (w->has_ref_)
643
1005
            env->add_refs(-1);
644
1007
          w->JoinThread();
645
          // implicitly delete w
646
1005
        });
647
1029
  }, static_cast<void*>(w));
648
649
1029
  if (ret == 0) {
650
    // The object now owns the created thread and should not be garbage
651
    // collected until that finishes.
652
1029
    w->ClearWeak();
653
654
1029
    if (w->has_ref_)
655
1029
      w->env()->add_refs(1);
656
657
1029
    w->env()->add_sub_worker_context(w);
658
  } else {
659
    w->stopped_ = true;
660
    w->tid_.reset();
661
662
    char err_buf[128];
663
    uv_err_name_r(ret, err_buf, sizeof(err_buf));
664
    {
665
      Isolate* isolate = w->env()->isolate();
666
      HandleScope handle_scope(isolate);
667
      THROW_ERR_WORKER_INIT_FAILED(isolate, err_buf);
668
    }
669
  }
670
}
671
672
454
void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
673
  Worker* w;
674
454
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
675
676
454
  Debug(w, "Worker %llu is getting stopped by parent", w->thread_id_.id);
677
454
  w->Exit(1);
678
}
679
680
458
void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
681
  Worker* w;
682
458
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
683

458
  if (!w->has_ref_ && w->tid_.has_value()) {
684
5
    w->has_ref_ = true;
685
5
    w->env()->add_refs(1);
686
  }
687
}
688
689
9
void Worker::HasRef(const FunctionCallbackInfo<Value>& args) {
690
  Worker* w;
691
9
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
692
16
  args.GetReturnValue().Set(w->has_ref_);
693
}
694
695
7
void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
696
  Worker* w;
697
7
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
698

7
  if (w->has_ref_ && w->tid_.has_value()) {
699
7
    w->has_ref_ = false;
700
7
    w->env()->add_refs(-1);
701
  }
702
}
703
704
1
void Worker::GetResourceLimits(const FunctionCallbackInfo<Value>& args) {
705
  Worker* w;
706
1
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
707
2
  args.GetReturnValue().Set(w->GetResourceLimits(args.GetIsolate()));
708
}
709
710
790
Local<Float64Array> Worker::GetResourceLimits(Isolate* isolate) const {
711
790
  Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(resource_limits_));
712
713
790
  memcpy(ab->GetBackingStore()->Data(),
714
790
         resource_limits_,
715
         sizeof(resource_limits_));
716
790
  return Float64Array::New(ab, 0, kTotalResourceLimitCount);
717
}
718
719
784
void Worker::Exit(int code, const char* error_code, const char* error_message) {
720
1568
  Mutex::ScopedLock lock(mutex_);
721
784
  Debug(this, "Worker %llu called Exit(%d, %s, %s)",
722
784
        thread_id_.id, code, error_code, error_message);
723
724
784
  if (error_code != nullptr) {
725
232
    custom_error_ = error_code;
726
232
    custom_error_str_ = error_message;
727
  }
728
729
784
  if (env_ != nullptr) {
730
386
    exit_code_ = code;
731
386
    Stop(env_);
732
  } else {
733
398
    stopped_ = true;
734
  }
735
784
}
736
737
bool Worker::IsNotIndicativeOfMemoryLeakAtExit() const {
738
  // Worker objects always stay alive as long as the child thread, regardless
739
  // of whether they are being referenced in the parent thread.
740
  return true;
741
}
742
743
class WorkerHeapSnapshotTaker : public AsyncWrap {
744
 public:
745
1
  WorkerHeapSnapshotTaker(Environment* env, Local<Object> obj)
746
1
    : AsyncWrap(env, obj, AsyncWrap::PROVIDER_WORKERHEAPSNAPSHOT) {}
747
748
  SET_NO_MEMORY_INFO()
749
  SET_MEMORY_INFO_NAME(WorkerHeapSnapshotTaker)
750
  SET_SELF_SIZE(WorkerHeapSnapshotTaker)
751
};
752
753
1
void Worker::TakeHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
754
  Worker* w;
755
1
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
756
757
1
  Debug(w, "Worker %llu taking heap snapshot", w->thread_id_.id);
758
759
1
  Environment* env = w->env();
760
1
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_id_scope(w);
761
  Local<Object> wrap;
762
1
  if (!env->worker_heap_snapshot_taker_template()
763
2
      ->NewInstance(env->context()).ToLocal(&wrap)) {
764
    return;
765
  }
766
  BaseObjectPtr<WorkerHeapSnapshotTaker> taker =
767
1
      MakeDetachedBaseObject<WorkerHeapSnapshotTaker>(env, wrap);
768
769
  // Interrupt the worker thread and take a snapshot, then schedule a call
770
  // on the parent thread that turns that snapshot into a readable stream.
771
1
  bool scheduled = w->RequestInterrupt([taker, env](Environment* worker_env) {
772
    heap::HeapSnapshotPointer snapshot {
773
1
        worker_env->isolate()->GetHeapProfiler()->TakeHeapSnapshot() };
774
1
    CHECK(snapshot);
775
2
    env->SetImmediateThreadsafe(
776
1
        [taker, snapshot = std::move(snapshot)](Environment* env) mutable {
777
2
          HandleScope handle_scope(env->isolate());
778
1
          Context::Scope context_scope(env->context());
779
780
2
          AsyncHooks::DefaultTriggerAsyncIdScope trigger_id_scope(taker.get());
781
          BaseObjectPtr<AsyncWrap> stream = heap::CreateHeapSnapshotStream(
782
2
              env, std::move(snapshot));
783
1
          Local<Value> args[] = { stream->object() };
784
1
          taker->MakeCallback(env->ondone_string(), arraysize(args), args);
785
1
        }, CallbackFlags::kUnrefed);
786
1
  });
787
2
  args.GetReturnValue().Set(scheduled ? taker->object() : Local<Object>());
788
}
789
790
7
void Worker::LoopIdleTime(const FunctionCallbackInfo<Value>& args) {
791
  Worker* w;
792
7
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
793
794
7
  Mutex::ScopedLock lock(w->mutex_);
795
  // Using w->is_stopped() here leads to a deadlock, and checking is_stopped()
796
  // before locking the mutex is a race condition. So manually do the same
797
  // check.
798

7
  if (w->stopped_ || w->env_ == nullptr)
799
    return args.GetReturnValue().Set(-1);
800
801
7
  uint64_t idle_time = uv_metrics_idle_time(w->env_->event_loop());
802
14
  args.GetReturnValue().Set(1.0 * idle_time / 1e6);
803
}
804
805
1
void Worker::LoopStartTime(const FunctionCallbackInfo<Value>& args) {
806
  Worker* w;
807
1
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
808
809
1
  Mutex::ScopedLock lock(w->mutex_);
810
  // Using w->is_stopped() here leads to a deadlock, and checking is_stopped()
811
  // before locking the mutex is a race condition. So manually do the same
812
  // check.
813

1
  if (w->stopped_ || w->env_ == nullptr)
814
    return args.GetReturnValue().Set(-1);
815
816
1
  double loop_start_time = w->env_->performance_state()->milestones[
817
1
      node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START];
818
1
  CHECK_GE(loop_start_time, 0);
819
1
  args.GetReturnValue().Set(
820
1
      (loop_start_time - node::performance::timeOrigin) / 1e6);
821
}
822
823
namespace {
824
825
// Return the MessagePort that is global for this Environment and communicates
826
// with the internal [kPort] port of the JS Worker class in the parent thread.
827
1577
void GetEnvMessagePort(const FunctionCallbackInfo<Value>& args) {
828
1577
  Environment* env = Environment::GetCurrent(args);
829
1577
  Local<Object> port = env->message_port();
830

3154
  CHECK_IMPLIES(!env->is_main_thread(), !port.IsEmpty());
831
1577
  if (!port.IsEmpty()) {
832
4731
    CHECK_EQ(port->GetCreationContext().ToLocalChecked()->GetIsolate(),
833
             args.GetIsolate());
834
3154
    args.GetReturnValue().Set(port);
835
  }
836
1577
}
837
838
845
void InitWorker(Local<Object> target,
839
                Local<Value> unused,
840
                Local<Context> context,
841
                void* priv) {
842
845
  Environment* env = Environment::GetCurrent(context);
843
844
  {
845
845
    Local<FunctionTemplate> w = env->NewFunctionTemplate(Worker::New);
846
847
1690
    w->InstanceTemplate()->SetInternalFieldCount(
848
        Worker::kInternalFieldCount);
849
845
    w->Inherit(AsyncWrap::GetConstructorTemplate(env));
850
851
845
    env->SetProtoMethod(w, "startThread", Worker::StartThread);
852
845
    env->SetProtoMethod(w, "stopThread", Worker::StopThread);
853
845
    env->SetProtoMethod(w, "hasRef", Worker::HasRef);
854
845
    env->SetProtoMethod(w, "ref", Worker::Ref);
855
845
    env->SetProtoMethod(w, "unref", Worker::Unref);
856
845
    env->SetProtoMethod(w, "getResourceLimits", Worker::GetResourceLimits);
857
845
    env->SetProtoMethod(w, "takeHeapSnapshot", Worker::TakeHeapSnapshot);
858
845
    env->SetProtoMethod(w, "loopIdleTime", Worker::LoopIdleTime);
859
845
    env->SetProtoMethod(w, "loopStartTime", Worker::LoopStartTime);
860
861
845
    env->SetConstructorFunction(target, "Worker", w);
862
  }
863
864
  {
865
845
    Local<FunctionTemplate> wst = FunctionTemplate::New(env->isolate());
866
867
1690
    wst->InstanceTemplate()->SetInternalFieldCount(
868
        WorkerHeapSnapshotTaker::kInternalFieldCount);
869
845
    wst->Inherit(AsyncWrap::GetConstructorTemplate(env));
870
871
    Local<String> wst_string =
872
845
        FIXED_ONE_BYTE_STRING(env->isolate(), "WorkerHeapSnapshotTaker");
873
845
    wst->SetClassName(wst_string);
874
845
    env->set_worker_heap_snapshot_taker_template(wst->InstanceTemplate());
875
  }
876
877
845
  env->SetMethod(target, "getEnvMessagePort", GetEnvMessagePort);
878
879
  target
880
845
      ->Set(env->context(),
881
            env->thread_id_string(),
882
3380
            Number::New(env->isolate(), static_cast<double>(env->thread_id())))
883
      .Check();
884
885
  target
886
845
      ->Set(env->context(),
887
            FIXED_ONE_BYTE_STRING(env->isolate(), "isMainThread"),
888
3380
            Boolean::New(env->isolate(), env->is_main_thread()))
889
      .Check();
890
891
  target
892
845
      ->Set(env->context(),
893
            FIXED_ONE_BYTE_STRING(env->isolate(), "ownsProcessState"),
894
2535
            Boolean::New(env->isolate(), env->owns_process_state()))
895
      .Check();
896
897
845
  if (!env->is_main_thread()) {
898
    target
899
789
        ->Set(env->context(),
900
              FIXED_ONE_BYTE_STRING(env->isolate(), "resourceLimits"),
901
3156
              env->worker_context()->GetResourceLimits(env->isolate()))
902
        .Check();
903
  }
904
905
2535
  NODE_DEFINE_CONSTANT(target, kMaxYoungGenerationSizeMb);
906
2535
  NODE_DEFINE_CONSTANT(target, kMaxOldGenerationSizeMb);
907
2535
  NODE_DEFINE_CONSTANT(target, kCodeRangeSizeMb);
908
2535
  NODE_DEFINE_CONSTANT(target, kStackSizeMb);
909
1690
  NODE_DEFINE_CONSTANT(target, kTotalResourceLimitCount);
910
845
}
911
912
5201
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
913
5201
  registry->Register(GetEnvMessagePort);
914
5201
  registry->Register(Worker::New);
915
5201
  registry->Register(Worker::StartThread);
916
5201
  registry->Register(Worker::StopThread);
917
5201
  registry->Register(Worker::HasRef);
918
5201
  registry->Register(Worker::Ref);
919
5201
  registry->Register(Worker::Unref);
920
5201
  registry->Register(Worker::GetResourceLimits);
921
5201
  registry->Register(Worker::TakeHeapSnapshot);
922
5201
  registry->Register(Worker::LoopIdleTime);
923
5201
  registry->Register(Worker::LoopStartTime);
924
5201
}
925
926
}  // anonymous namespace
927
}  // namespace worker
928
}  // namespace node
929
930
5269
NODE_MODULE_CONTEXT_AWARE_INTERNAL(worker, node::worker::InitWorker)
931
5201
NODE_MODULE_EXTERNAL_REFERENCE(worker, node::worker::RegisterExternalReferences)