GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_worker.cc Lines: 369 424 87.0 %
Date: 2020-02-27 22:14:15 Branches: 165 238 69.3 %

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

460
  TRACE_EVENT_METADATA1(
252
      "__metadata", "thread_name", "name",
253
      TRACE_STR_COPY(name.c_str()));
254
229
  CHECK_NOT_NULL(platform_);
255
256
229
  Debug(this, "Creating isolate for worker with id %llu", thread_id_);
257
258
457
  WorkerThreadData data(this);
259
229
  if (isolate_ == nullptr) return;
260
229
  CHECK(!data.w_->loop_init_failed_);
261
262
229
  Debug(this, "Starting worker with id %llu", thread_id_);
263
  {
264
457
    Locker locker(isolate_);
265
457
    Isolate::Scope isolate_scope(isolate_);
266
457
    SealHandleScope outer_seal(isolate_);
267
268
457
    DeleteFnPtr<Environment, FreeEnvironment> env_;
269
227
    auto cleanup_env = OnScopeLeave([&]() {
270
1362
      if (!env_) return;
271
227
      env_->set_can_call_into_js(false);
272
1822
      Isolate::DisallowJavascriptExecutionScope disallow_js(isolate_,
273
456
          Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE);
274
275
      {
276
227
        Context::Scope context_scope(env_->context());
277
        {
278
454
          Mutex::ScopedLock lock(mutex_);
279
228
          stopped_ = true;
280
228
          this->env_ = nullptr;
281
        }
282
228
        env_->set_stopping(true);
283
228
        env_->stop_sub_worker_contexts();
284
226
        env_->RunCleanup();
285
226
        RunAtExit(env_.get());
286
287
        // This call needs to be made while the `Environment` is still alive
288
        // because we assume that it is available for async tracking in the
289
        // NodePlatform implementation.
290
912
        platform_->DrainTasks(isolate_);
291
      }
292
457
    });
293
294
229
    if (is_stopped()) return;
295
    {
296
456
      HandleScope handle_scope(isolate_);
297
      Local<Context> context;
298
      {
299
        // We create the Context object before we have an Environment* in place
300
        // that we could use for error handling. If creation fails due to
301
        // resource constraints, we need something in place to handle it,
302
        // though.
303
456
        TryCatch try_catch(isolate_);
304
228
        context = NewContext(isolate_);
305
228
        if (context.IsEmpty()) {
306
          custom_error_ = "ERR_WORKER_OUT_OF_MEMORY";
307
          custom_error_str_ = "Failed to create new Context";
308
          return;
309
        }
310
      }
311
312
228
      if (is_stopped()) return;
313
228
      CHECK(!context.IsEmpty());
314
228
      Context::Scope context_scope(context);
315
      {
316
        // TODO(addaleax): Use CreateEnvironment(), or generally another
317
        // public API.
318
912
        env_.reset(new Environment(data.isolate_data_.get(),
319
                                   context,
320
228
                                   std::move(argv_),
321
228
                                   std::move(exec_argv_),
322
                                   Environment::kNoFlags,
323
228
                                   thread_id_));
324
228
        CHECK_NOT_NULL(env_);
325
228
        env_->set_env_vars(std::move(env_vars_));
326
228
        env_->set_abort_on_uncaught_exception(false);
327
228
        env_->set_worker_context(this);
328
329
228
        env_->InitializeLibuv(start_profiler_idle_notifier_);
330
      }
331
      {
332
456
        Mutex::ScopedLock lock(mutex_);
333
228
        if (stopped_) return;
334
228
        this->env_ = env_.get();
335
      }
336
228
      Debug(this, "Created Environment for worker with id %llu", thread_id_);
337
228
      if (is_stopped()) return;
338
      {
339
228
        env_->InitializeDiagnostics();
340
#if HAVE_INSPECTOR
341
228
        env_->InitializeInspector(std::move(inspector_parent_handle_));
342
#endif
343
456
        HandleScope handle_scope(isolate_);
344
345
456
        if (!env_->RunBootstrapping().IsEmpty()) {
346
228
          CreateEnvMessagePort(env_.get());
347
228
          if (is_stopped()) return;
348
227
          Debug(this, "Created message port for worker %llu", thread_id_);
349
228
          USE(StartExecution(env_.get(), "internal/main/worker_thread"));
350
        }
351
352
227
        Debug(this, "Loaded environment for worker %llu", thread_id_);
353
      }
354
355
228
      if (is_stopped()) return;
356
      {
357
456
        SealHandleScope seal(isolate_);
358
        bool more;
359
228
        env_->performance_state()->Mark(
360
228
            node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
361
170
        do {
362
229
          if (is_stopped()) break;
363
229
          uv_run(&data.loop_, UV_RUN_DEFAULT);
364
228
          if (is_stopped()) break;
365
366
170
          platform_->DrainTasks(isolate_);
367
368
171
          more = uv_loop_alive(&data.loop_);
369

170
          if (more && !is_stopped()) continue;
370
371
170
          EmitBeforeExit(env_.get());
372
373
          // Emit `beforeExit` if the loop became alive either after emitting
374
          // event, or after running some callbacks.
375
169
          more = uv_loop_alive(&data.loop_);
376

170
        } while (more == true && !is_stopped());
377
227
        env_->performance_state()->Mark(
378
228
            node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
379
      }
380
    }
381
382
    {
383
      int exit_code;
384
228
      bool stopped = is_stopped();
385
226
      if (!stopped)
386
169
        exit_code = EmitExit(env_.get());
387
452
      Mutex::ScopedLock lock(mutex_);
388

228
      if (exit_code_ == 0 && !stopped)
389
165
        exit_code_ = exit_code;
390
391
228
      Debug(this, "Exiting thread for worker %llu with exit code %d",
392
            thread_id_, exit_code_);
393
    }
394
  }
395
396
228
  Debug(this, "Worker %llu thread stops", thread_id_);
397
}
398
399
228
void Worker::CreateEnvMessagePort(Environment* env) {
400
456
  HandleScope handle_scope(isolate_);
401
456
  Mutex::ScopedLock lock(mutex_);
402
  // Set up the message channel for receiving messages in the child.
403
455
  MessagePort* child_port = MessagePort::New(env,
404
                                             env->context(),
405
456
                                             std::move(child_port_data_));
406
  // MessagePort::New() may return nullptr if execution is terminated
407
  // within it.
408
228
  if (child_port != nullptr)
409
228
    env->set_message_port(child_port->object(isolate_));
410
228
}
411
412
229
void Worker::JoinThread() {
413
229
  if (thread_joined_)
414
    return;
415
229
  CHECK_EQ(uv_thread_join(&tid_), 0);
416
229
  thread_joined_ = true;
417
418
229
  env()->remove_sub_worker_context(this);
419
420
  {
421
454
    HandleScope handle_scope(env()->isolate());
422
229
    Context::Scope context_scope(env()->context());
423
424
    // Reset the parent port as we're closing it now anyway.
425
687
    object()->Set(env()->context(),
426
                  env()->message_port_string(),
427
1145
                  Undefined(env()->isolate())).Check();
428
429
    Local<Value> args[] = {
430
        Integer::New(env()->isolate(), exit_code_),
431
229
        custom_error_ != nullptr
432
230
            ? OneByteString(env()->isolate(), custom_error_).As<Value>()
433
685
            : Null(env()->isolate()).As<Value>(),
434
229
        !custom_error_str_.empty()
435
230
            ? OneByteString(env()->isolate(), custom_error_str_.c_str())
436
                  .As<Value>()
437
685
            : Null(env()->isolate()).As<Value>(),
438

1832
    };
439
440
229
    MakeCallback(env()->onexit_string(), arraysize(args), args);
441
  }
442
443
  // We cleared all libuv handles bound to this Worker above,
444
  // the C++ object is no longer needed for anything now.
445
225
  MakeWeak();
446
}
447
448
645
Worker::~Worker() {
449
430
  Mutex::ScopedLock lock(mutex_);
450
451
215
  CHECK(stopped_);
452
215
  CHECK_NULL(env_);
453
215
  CHECK(thread_joined_);
454
455
215
  Debug(this, "Worker %llu destroyed", thread_id_);
456
430
}
457
458
233
void Worker::New(const FunctionCallbackInfo<Value>& args) {
459
233
  Environment* env = Environment::GetCurrent(args);
460
233
  Isolate* isolate = args.GetIsolate();
461
462
233
  CHECK(args.IsConstructCall());
463
464
233
  if (env->isolate_data()->platform() == nullptr) {
465
    THROW_ERR_MISSING_PLATFORM_FOR_WORKER(env);
466
    return;
467
  }
468
469
463
  std::string url;
470
463
  std::shared_ptr<PerIsolateOptions> per_isolate_opts = nullptr;
471
463
  std::shared_ptr<KVStore> env_vars = nullptr;
472
473
463
  std::vector<std::string> exec_argv_out;
474
475
233
  CHECK_EQ(args.Length(), 4);
476
  // Argument might be a string or URL
477
699
  if (!args[0]->IsNullOrUndefined()) {
478
    Utf8Value value(
479
465
        isolate, args[0]->ToString(env->context()).FromMaybe(Local<String>()));
480
93
    url.append(value.out(), value.length());
481
  }
482
483
699
  if (args[1]->IsNull()) {
484
    // Means worker.env = { ...process.env }.
485
210
    env_vars = env->env_vars()->Clone(isolate);
486
46
  } else if (args[1]->IsObject()) {
487
    // User provided env.
488
22
    env_vars = KVStore::CreateMapKVStore();
489
22
    env_vars->AssignFromObject(isolate->GetCurrentContext(),
490
66
                               args[1].As<Object>());
491
  } else {
492
    // Env is shared.
493
1
    env_vars = env->env_vars();
494
  }
495
496

888
  if (args[1]->IsObject() || args[2]->IsArray()) {
497
31
    per_isolate_opts.reset(new PerIsolateOptions());
498
499
93
    HandleEnvOptions(
500
405
        per_isolate_opts->per_env, [isolate, &env_vars](const char* name) {
501
          MaybeLocal<String> value =
502
372
              env_vars->Get(isolate, OneByteString(isolate, name));
503
124
          return value.IsEmpty() ? std::string{}
504
128
                                 : std::string(*String::Utf8Value(
505

252
                                       isolate, value.ToLocalChecked()));
506
31
        });
507
508
#ifndef NODE_WITHOUT_NODE_OPTIONS
509
    MaybeLocal<String> maybe_node_opts =
510
31
        env_vars->Get(isolate, OneByteString(isolate, "NODE_OPTIONS"));
511
31
    if (!maybe_node_opts.IsEmpty()) {
512
      std::string node_options(
513
90
          *String::Utf8Value(isolate, maybe_node_opts.ToLocalChecked()));
514
60
      std::vector<std::string> errors{};
515
      std::vector<std::string> env_argv =
516
60
          ParseNodeOptionsEnvVar(node_options, &errors);
517
      // [0] is expected to be the program name, add dummy string.
518
30
      env_argv.insert(env_argv.begin(), "");
519
60
      std::vector<std::string> invalid_args{};
520
30
      options_parser::Parse(&env_argv,
521
                            nullptr,
522
                            &invalid_args,
523
                            per_isolate_opts.get(),
524
                            kAllowedInEnvironment,
525
30
                            &errors);
526

30
      if (errors.size() > 0 && args[1]->IsObject()) {
527
        // Only fail for explicitly provided env, this protects from failures
528
        // when NODE_OPTIONS from parent's env is used (which is the default).
529
        Local<Value> error;
530
        if (!ToV8Value(env->context(), errors).ToLocal(&error)) return;
531
        Local<String> key =
532
            FIXED_ONE_BYTE_STRING(env->isolate(), "invalidNodeOptions");
533
        // Ignore the return value of Set() because exceptions bubble up to JS
534
        // when we return anyway.
535
        USE(args.This()->Set(env->context(), key, error));
536
        return;
537
      }
538
    }
539
#endif
540
  }
541
542
466
  if (args[2]->IsArray()) {
543
58
    Local<Array> array = args[2].As<Array>();
544
    // The first argument is reserved for program name, but we don't need it
545
    // in workers.
546
55
    std::vector<std::string> exec_argv = {""};
547
29
    uint32_t length = array->Length();
548
43
    for (uint32_t i = 0; i < length; i++) {
549
      Local<Value> arg;
550
42
      if (!array->Get(env->context(), i).ToLocal(&arg)) {
551
        return;
552
      }
553
      MaybeLocal<String> arg_v8_string =
554
28
          arg->ToString(env->context());
555
14
      if (arg_v8_string.IsEmpty()) {
556
        return;
557
      }
558
      Utf8Value arg_utf8_value(
559
          args.GetIsolate(),
560
28
          arg_v8_string.FromMaybe(Local<String>()));
561
28
      std::string arg_string(arg_utf8_value.out(), arg_utf8_value.length());
562
14
      exec_argv.push_back(arg_string);
563
    }
564
565
55
    std::vector<std::string> invalid_args{};
566
55
    std::vector<std::string> errors{};
567
    // Using invalid_args as the v8_args argument as it stores unknown
568
    // options for the per isolate parser.
569
29
    options_parser::Parse(
570
        &exec_argv,
571
        &exec_argv_out,
572
        &invalid_args,
573
        per_isolate_opts.get(),
574
        kDisallowedInEnvironment,
575
29
        &errors);
576
577
    // The first argument is program name.
578
29
    invalid_args.erase(invalid_args.begin());
579

29
    if (errors.size() > 0 || invalid_args.size() > 0) {
580
      Local<Value> error;
581
6
      if (!ToV8Value(env->context(),
582
3
                     errors.size() > 0 ? errors : invalid_args)
583
3
                         .ToLocal(&error)) {
584
        return;
585
      }
586
      Local<String> key =
587
3
          FIXED_ONE_BYTE_STRING(env->isolate(), "invalidExecArgv");
588
      // Ignore the return value of Set() because exceptions bubble up to JS
589
      // when we return anyway.
590
9
      USE(args.This()->Set(env->context(), key, error));
591
3
      return;
592
    }
593
  } else {
594
204
    exec_argv_out = env->exec_argv();
595
  }
596
597
  Worker* worker = new Worker(env,
598
460
                              args.This(),
599
                              url,
600
                              per_isolate_opts,
601
                              std::move(exec_argv_out),
602
230
                              env_vars);
603
604
460
  CHECK(args[3]->IsFloat64Array());
605
460
  Local<Float64Array> limit_info = args[3].As<Float64Array>();
606
230
  CHECK_EQ(limit_info->Length(), kTotalResourceLimitCount);
607
460
  limit_info->CopyContents(worker->resource_limits_,
608
230
                           sizeof(worker->resource_limits_));
609
}
610
611
229
void Worker::StartThread(const FunctionCallbackInfo<Value>& args) {
612
  Worker* w;
613
229
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
614
458
  Mutex::ScopedLock lock(w->mutex_);
615
616
  // The object now owns the created thread and should not be garbage collected
617
  // until that finishes.
618
229
  w->ClearWeak();
619
620
229
  w->env()->add_sub_worker_context(w);
621
229
  w->stopped_ = false;
622
229
  w->thread_joined_ = false;
623
624
229
  if (w->has_ref_)
625
229
    w->env()->add_refs(1);
626
627
  uv_thread_options_t thread_options;
628
229
  thread_options.flags = UV_THREAD_HAS_STACK_SIZE;
629
229
  thread_options.stack_size = kStackSize;
630

1805
  CHECK_EQ(uv_thread_create_ex(&w->tid_, &thread_options, [](void* arg) {
631
    // XXX: This could become a std::unique_ptr, but that makes at least
632
    // gcc 6.3 detect undefined behaviour when there shouldn't be any.
633
    // gcc 7+ handles this well.
634
    Worker* w = static_cast<Worker*>(arg);
635
    const uintptr_t stack_top = reinterpret_cast<uintptr_t>(&arg);
636
637
    // Leave a few kilobytes just to make sure we're within limits and have
638
    // some space to do work in C++ land.
639
    w->stack_base_ = stack_top - (kStackSize - kStackBufferSize);
640
641
    w->Run();
642
643
    Mutex::ScopedLock lock(w->mutex_);
644
    w->env()->SetImmediateThreadsafe(
645
        [w = std::unique_ptr<Worker>(w)](Environment* env) {
646
          if (w->has_ref_)
647
            env->add_refs(-1);
648
          w->JoinThread();
649
          // implicitly delete w
650
        });
651
  }, static_cast<void*>(w)), 0);
652
}
653
654
20
void Worker::StopThread(const FunctionCallbackInfo<Value>& args) {
655
  Worker* w;
656
20
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
657
658
20
  Debug(w, "Worker %llu is getting stopped by parent", w->thread_id_);
659
20
  w->Exit(1);
660
}
661
662
22
void Worker::Ref(const FunctionCallbackInfo<Value>& args) {
663
  Worker* w;
664
22
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
665
22
  if (!w->has_ref_) {
666
3
    w->has_ref_ = true;
667
3
    w->env()->add_refs(1);
668
  }
669
}
670
671
4
void Worker::Unref(const FunctionCallbackInfo<Value>& args) {
672
  Worker* w;
673
4
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
674
4
  if (w->has_ref_) {
675
4
    w->has_ref_ = false;
676
4
    w->env()->add_refs(-1);
677
  }
678
}
679
680
1
void Worker::GetResourceLimits(const FunctionCallbackInfo<Value>& args) {
681
  Worker* w;
682
1
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
683
3
  args.GetReturnValue().Set(w->GetResourceLimits(args.GetIsolate()));
684
}
685
686
229
Local<Float64Array> Worker::GetResourceLimits(Isolate* isolate) const {
687
229
  Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(resource_limits_));
688
689
687
  memcpy(ab->GetBackingStore()->Data(),
690
         resource_limits_,
691
229
         sizeof(resource_limits_));
692
229
  return Float64Array::New(ab, 0, kTotalResourceLimitCount);
693
}
694
695
63
void Worker::Exit(int code) {
696
126
  Mutex::ScopedLock lock(mutex_);
697
63
  Debug(this, "Worker %llu called Exit(%d)", thread_id_, code);
698
63
  if (env_ != nullptr) {
699
61
    exit_code_ = code;
700
61
    Stop(env_);
701
  } else {
702
2
    stopped_ = true;
703
  }
704
63
}
705
706
void Worker::MemoryInfo(MemoryTracker* tracker) const {
707
  tracker->TrackField("parent_port", parent_port_);
708
}
709
710
class WorkerHeapSnapshotTaker : public AsyncWrap {
711
 public:
712
  WorkerHeapSnapshotTaker(Environment* env, Local<Object> obj)
713
    : AsyncWrap(env, obj, AsyncWrap::PROVIDER_WORKERHEAPSNAPSHOT) {}
714
715
  SET_NO_MEMORY_INFO()
716
  SET_MEMORY_INFO_NAME(WorkerHeapSnapshotTaker)
717
  SET_SELF_SIZE(WorkerHeapSnapshotTaker)
718
};
719
720
void Worker::TakeHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
721
  Worker* w;
722
  ASSIGN_OR_RETURN_UNWRAP(&w, args.This());
723
724
  Debug(w, "Worker %llu taking heap snapshot", w->thread_id_);
725
726
  Environment* env = w->env();
727
  AsyncHooks::DefaultTriggerAsyncIdScope trigger_id_scope(w);
728
  Local<Object> wrap;
729
  if (!env->worker_heap_snapshot_taker_template()
730
      ->NewInstance(env->context()).ToLocal(&wrap)) {
731
    return;
732
  }
733
  BaseObjectPtr<WorkerHeapSnapshotTaker> taker =
734
      MakeDetachedBaseObject<WorkerHeapSnapshotTaker>(env, wrap);
735
736
  // Interrupt the worker thread and take a snapshot, then schedule a call
737
  // on the parent thread that turns that snapshot into a readable stream.
738
  bool scheduled = w->RequestInterrupt([taker, env](Environment* worker_env) {
739
    heap::HeapSnapshotPointer snapshot {
740
        worker_env->isolate()->GetHeapProfiler()->TakeHeapSnapshot() };
741
    CHECK(snapshot);
742
    env->SetImmediateThreadsafe(
743
        [taker, snapshot = std::move(snapshot)](Environment* env) mutable {
744
          HandleScope handle_scope(env->isolate());
745
          Context::Scope context_scope(env->context());
746
747
          AsyncHooks::DefaultTriggerAsyncIdScope trigger_id_scope(taker.get());
748
          BaseObjectPtr<AsyncWrap> stream = heap::CreateHeapSnapshotStream(
749
              env, std::move(snapshot));
750
          Local<Value> args[] = { stream->object() };
751
          taker->MakeCallback(env->ondone_string(), arraysize(args), args);
752
        });
753
  });
754
  args.GetReturnValue().Set(scheduled ? taker->object() : Local<Object>());
755
}
756
757
namespace {
758
759
// Return the MessagePort that is global for this Environment and communicates
760
// with the internal [kPort] port of the JS Worker class in the parent thread.
761
457
void GetEnvMessagePort(const FunctionCallbackInfo<Value>& args) {
762
457
  Environment* env = Environment::GetCurrent(args);
763
457
  Local<Object> port = env->message_port();
764
457
  if (!port.IsEmpty()) {
765
1371
    CHECK_EQ(port->CreationContext()->GetIsolate(), args.GetIsolate());
766
914
    args.GetReturnValue().Set(port);
767
  }
768
457
}
769
770
3686
void InitWorker(Local<Object> target,
771
                Local<Value> unused,
772
                Local<Context> context,
773
                void* priv) {
774
3686
  Environment* env = Environment::GetCurrent(context);
775
776
  {
777
3686
    Local<FunctionTemplate> w = env->NewFunctionTemplate(Worker::New);
778
779
7372
    w->InstanceTemplate()->SetInternalFieldCount(1);
780
7372
    w->Inherit(AsyncWrap::GetConstructorTemplate(env));
781
782
3686
    env->SetProtoMethod(w, "startThread", Worker::StartThread);
783
3686
    env->SetProtoMethod(w, "stopThread", Worker::StopThread);
784
3686
    env->SetProtoMethod(w, "ref", Worker::Ref);
785
3686
    env->SetProtoMethod(w, "unref", Worker::Unref);
786
3686
    env->SetProtoMethod(w, "getResourceLimits", Worker::GetResourceLimits);
787
3686
    env->SetProtoMethod(w, "takeHeapSnapshot", Worker::TakeHeapSnapshot);
788
789
    Local<String> workerString =
790
3686
        FIXED_ONE_BYTE_STRING(env->isolate(), "Worker");
791
3686
    w->SetClassName(workerString);
792
7372
    target->Set(env->context(),
793
                workerString,
794
18430
                w->GetFunction(env->context()).ToLocalChecked()).Check();
795
  }
796
797
  {
798
3686
    Local<FunctionTemplate> wst = FunctionTemplate::New(env->isolate());
799
800
7372
    wst->InstanceTemplate()->SetInternalFieldCount(1);
801
7372
    wst->Inherit(AsyncWrap::GetConstructorTemplate(env));
802
803
    Local<String> wst_string =
804
3686
        FIXED_ONE_BYTE_STRING(env->isolate(), "WorkerHeapSnapshotTaker");
805
3686
    wst->SetClassName(wst_string);
806
3686
    env->set_worker_heap_snapshot_taker_template(wst->InstanceTemplate());
807
  }
808
809
3686
  env->SetMethod(target, "getEnvMessagePort", GetEnvMessagePort);
810
811
  target
812
7372
      ->Set(env->context(),
813
            env->thread_id_string(),
814
18430
            Number::New(env->isolate(), static_cast<double>(env->thread_id())))
815
      .Check();
816
817
  target
818
7372
      ->Set(env->context(),
819
            FIXED_ONE_BYTE_STRING(env->isolate(), "isMainThread"),
820
18430
            Boolean::New(env->isolate(), env->is_main_thread()))
821
      .Check();
822
823
  target
824
7372
      ->Set(env->context(),
825
            FIXED_ONE_BYTE_STRING(env->isolate(), "ownsProcessState"),
826
18430
            Boolean::New(env->isolate(), env->owns_process_state()))
827
      .Check();
828
829
3686
  if (!env->is_main_thread()) {
830
    target
831
456
        ->Set(env->context(),
832
              FIXED_ONE_BYTE_STRING(env->isolate(), "resourceLimits"),
833
1140
              env->worker_context()->GetResourceLimits(env->isolate()))
834
        .Check();
835
  }
836
837
14744
  NODE_DEFINE_CONSTANT(target, kMaxYoungGenerationSizeMb);
838
14744
  NODE_DEFINE_CONSTANT(target, kMaxOldGenerationSizeMb);
839
14744
  NODE_DEFINE_CONSTANT(target, kCodeRangeSizeMb);
840
14744
  NODE_DEFINE_CONSTANT(target, kTotalResourceLimitCount);
841
3686
}
842
843
}  // anonymous namespace
844
845
}  // namespace worker
846
}  // namespace node
847
848
4201
NODE_MODULE_CONTEXT_AWARE_INTERNAL(worker, node::worker::InitWorker)