GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_options.cc Lines: 328 355 92.4 %
Date: 2019-10-06 22:30:00 Branches: 182 270 67.4 %

Line Branch Exec Source
1
#include "node_options.h"  // NOLINT(build/include_inline)
2
#include "node_options-inl.h"
3
4
#include "env-inl.h"
5
#include "node_binding.h"
6
7
#include <cstdlib>  // strtoul, errno
8
9
using v8::Boolean;
10
using v8::Context;
11
using v8::FunctionCallbackInfo;
12
using v8::Integer;
13
using v8::Isolate;
14
using v8::Local;
15
using v8::Map;
16
using v8::Number;
17
using v8::Object;
18
using v8::String;
19
using v8::Undefined;
20
using v8::Value;
21
22
namespace node {
23
24
namespace per_process {
25
5059
Mutex cli_options_mutex;
26
5059
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
27
}  // namespace per_process
28
29
9959
void DebugOptions::CheckOptions(std::vector<std::string>* errors) {
30
#if !NODE_USE_V8_PLATFORM
31
  if (inspector_enabled) {
32
    errors->push_back("Inspector is not available when Node is compiled "
33
                      "--without-v8-platform");
34
  }
35
#endif
36
37
9959
  if (deprecated_debug) {
38
    errors->push_back("[DEP0062]: `node --debug` and `node --debug-brk` "
39
                      "are invalid. Please use `node --inspect` and "
40
5
                      "`node --inspect-brk` instead.");
41
  }
42
43
  std::vector<std::string> destinations =
44
9959
      SplitString(inspect_publish_uid_string, ',');
45
9959
  inspect_publish_uid.console = false;
46
9959
  inspect_publish_uid.http = false;
47
29875
  for (const std::string& destination : destinations) {
48
19916
    if (destination == "stderr") {
49
9958
      inspect_publish_uid.console = true;
50
9958
    } else if (destination == "http") {
51
9958
      inspect_publish_uid.http = true;
52
    } else {
53
      errors->push_back("--inspect-publish-uid destination can be "
54
                        "stderr or http");
55
    }
56
9959
  }
57
9959
}
58
59
9947
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
60
#if HAVE_OPENSSL
61

9947
  if (use_openssl_ca && use_bundled_ca) {
62
    errors->push_back("either --use-openssl-ca or --use-bundled-ca can be "
63
1
                      "used, not both");
64
  }
65
#endif
66
9947
  per_isolate->CheckOptions(errors);
67
9947
}
68
69
9959
void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) {
70
9959
  per_env->CheckOptions(errors);
71
#ifdef NODE_REPORT
72
9959
  if (per_env->experimental_report) {
73
    // Assign the report_signal default value here. Once the
74
    // --experimental-report flag is dropped, move this initialization to
75
    // node_options.h, where report_signal is declared.
76
10
    if (report_signal.empty())
77
10
      report_signal = "SIGUSR2";
78
9969
    return;
79
  }
80
81
9949
  if (!report_directory.empty()) {
82
    errors->push_back("--report-directory option is valid only when "
83
                      "--experimental-report is set");
84
  }
85
86
9949
  if (!report_filename.empty()) {
87
    errors->push_back("--report-filename option is valid only when "
88
                      "--experimental-report is set");
89
  }
90
91
9949
  if (!report_signal.empty()) {
92
    errors->push_back("--report-signal option is valid only when "
93
                      "--experimental-report is set");
94
  }
95
96
9949
  if (report_on_fatalerror) {
97
    errors->push_back(
98
        "--report-on-fatalerror option is valid only when "
99
        "--experimental-report is set");
100
  }
101
102
9949
  if (report_on_signal) {
103
    errors->push_back("--report-on-signal option is valid only when "
104
                      "--experimental-report is set");
105
  }
106
107
9949
  if (report_uncaught_exception) {
108
    errors->push_back(
109
        "--report-uncaught-exception option is valid only when "
110
        "--experimental-report is set");
111
  }
112
#endif  // NODE_REPORT
113
}
114
115
9959
void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
116

9959
  if (!userland_loader.empty() && !experimental_modules) {
117
    errors->push_back("--experimental-loader requires "
118
                      "--experimental-modules be enabled");
119
  }
120

9959
  if (has_policy_integrity_string && experimental_policy.empty()) {
121
    errors->push_back("--policy-integrity requires "
122
                      "--experimental-policy be enabled");
123
  }
124

9959
  if (has_policy_integrity_string && experimental_policy_integrity.empty()) {
125
1
    errors->push_back("--policy-integrity cannot be empty");
126
  }
127
128
9959
  if (!module_type.empty()) {
129
13
    if (!experimental_modules) {
130
      errors->push_back("--input-type requires "
131
                        "--experimental-modules to be enabled");
132
    }
133

13
    if (module_type != "commonjs" && module_type != "module") {
134
      errors->push_back("--input-type must be \"module\" or \"commonjs\"");
135
    }
136
  }
137
138

9959
  if (experimental_wasm_modules && !experimental_modules) {
139
    errors->push_back("--experimental-wasm-modules requires "
140
                      "--experimental-modules be enabled");
141
  }
142
143
9959
  if (!es_module_specifier_resolution.empty()) {
144
1
    if (!experimental_modules) {
145
      errors->push_back("--es-module-specifier-resolution requires "
146
                        "--experimental-modules be enabled");
147
    }
148

1
    if (es_module_specifier_resolution != "node" &&
149
        es_module_specifier_resolution != "explicit") {
150
      errors->push_back("invalid value for --es-module-specifier-resolution");
151
    }
152
  }
153
154

9959
  if (syntax_check_only && has_eval_string) {
155
4
    errors->push_back("either --check or --eval can be used, not both");
156
  }
157
158

19927
  if (!unhandled_rejections.empty() &&
159
16
      unhandled_rejections != "strict" &&
160

9971
      unhandled_rejections != "warn" &&
161
5
      unhandled_rejections != "none") {
162
1
    errors->push_back("invalid value for --unhandled-rejections");
163
  }
164
165

9959
  if (tls_min_v1_3 && tls_max_v1_2) {
166
    errors->push_back("either --tls-min-v1.3 or --tls-max-v1.2 can be "
167
1
                      "used, not both");
168
  }
169
170
#if HAVE_INSPECTOR
171
9959
  if (!cpu_prof) {
172
9950
    if (!cpu_prof_name.empty()) {
173
1
      errors->push_back("--cpu-prof-name must be used with --cpu-prof");
174
    }
175
9950
    if (!cpu_prof_dir.empty()) {
176
1
      errors->push_back("--cpu-prof-dir must be used with --cpu-prof");
177
    }
178
    // We can't catch the case where the value passed is the default value,
179
    // then the option just becomes a noop which is fine.
180
9950
    if (cpu_prof_interval != kDefaultCpuProfInterval) {
181
1
      errors->push_back("--cpu-prof-interval must be used with --cpu-prof");
182
    }
183
  }
184
185
9959
  if (!heap_prof) {
186
9950
    if (!heap_prof_name.empty()) {
187
1
      errors->push_back("--heap-prof-name must be used with --heap-prof");
188
    }
189
9950
    if (!heap_prof_dir.empty()) {
190
1
      errors->push_back("--heap-prof-dir must be used with --heap-prof");
191
    }
192
    // We can't catch the case where the value passed is the default value,
193
    // then the option just becomes a noop which is fine.
194
9950
    if (heap_prof_interval != kDefaultHeapProfInterval) {
195
1
      errors->push_back("--heap-prof-interval must be used with --heap-prof");
196
    }
197
  }
198
9959
  debug_options_.CheckOptions(errors);
199
#endif  // HAVE_INSPECTOR
200
9959
}
201
202
namespace options_parser {
203
204
5059
class DebugOptionsParser : public OptionsParser<DebugOptions> {
205
 public:
206
  DebugOptionsParser();
207
};
208
209
5059
class EnvironmentOptionsParser : public OptionsParser<EnvironmentOptions> {
210
 public:
211
  EnvironmentOptionsParser();
212
5059
  explicit EnvironmentOptionsParser(const DebugOptionsParser& dop)
213
5059
    : EnvironmentOptionsParser() {
214
5059
    Insert(dop, &EnvironmentOptions::get_debug_options);
215
5059
  }
216
};
217
218
5059
class PerIsolateOptionsParser : public OptionsParser<PerIsolateOptions> {
219
 public:
220
  PerIsolateOptionsParser() = delete;
221
  explicit PerIsolateOptionsParser(const EnvironmentOptionsParser& eop);
222
};
223
224
5059
class PerProcessOptionsParser : public OptionsParser<PerProcessOptions> {
225
 public:
226
  PerProcessOptionsParser() = delete;
227
  explicit PerProcessOptionsParser(const PerIsolateOptionsParser& iop);
228
};
229
230
#if HAVE_INSPECTOR
231
5059
const DebugOptionsParser _dop_instance{};
232
5059
const EnvironmentOptionsParser _eop_instance{_dop_instance};
233
234
// This Parse is not dead code. It is used by embedders (e.g., Electron).
235
template <>
236
void Parse(
237
  StringVector* const args, StringVector* const exec_args,
238
  StringVector* const v8_args,
239
  DebugOptions* const options,
240
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
241
  _dop_instance.Parse(
242
    args, exec_args, v8_args, options, required_env_settings, errors);
243
}
244
#else
245
const EnvironmentOptionsParser _eop_instance{};
246
#endif  // HAVE_INSPECTOR
247
5059
const PerIsolateOptionsParser _piop_instance{_eop_instance};
248
5059
const PerProcessOptionsParser _ppop_instance{_piop_instance};
249
250
template <>
251
12
void Parse(
252
  StringVector* const args, StringVector* const exec_args,
253
  StringVector* const v8_args,
254
  PerIsolateOptions* const options,
255
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
256
  _piop_instance.Parse(
257
12
    args, exec_args, v8_args, options, required_env_settings, errors);
258
12
}
259
260
template <>
261
9947
void Parse(
262
  StringVector* const args, StringVector* const exec_args,
263
  StringVector* const v8_args,
264
  PerProcessOptions* const options,
265
  OptionEnvvarSettings required_env_settings, StringVector* const errors) {
266
  _ppop_instance.Parse(
267
9947
    args, exec_args, v8_args, options, required_env_settings, errors);
268
9947
}
269
270
// XXX: If you add an option here, please also add it to doc/node.1 and
271
// doc/api/cli.md
272
// TODO(addaleax): Make that unnecessary.
273
274
5059
DebugOptionsParser::DebugOptionsParser() {
275
  AddOption("--inspect-port",
276
            "set host:port for inspector",
277
            &DebugOptions::host_port,
278
5059
            kAllowedInEnvironment);
279
5059
  AddAlias("--debug-port", "--inspect-port");
280
281
  AddOption("--inspect",
282
            "activate inspector on host:port (default: 127.0.0.1:9229)",
283
            &DebugOptions::inspector_enabled,
284
5059
            kAllowedInEnvironment);
285
5059
  AddAlias("--inspect=", { "--inspect-port", "--inspect" });
286
287
5059
  AddOption("--debug", "", &DebugOptions::deprecated_debug);
288
5059
  AddAlias("--debug=", "--debug");
289
5059
  AddOption("--debug-brk", "", &DebugOptions::deprecated_debug);
290
5059
  AddAlias("--debug-brk=", "--debug-brk");
291
292
  AddOption("--inspect-brk",
293
            "activate inspector on host:port and break at start of user script",
294
            &DebugOptions::break_first_line,
295
5059
            kAllowedInEnvironment);
296
5059
  Implies("--inspect-brk", "--inspect");
297
5059
  AddAlias("--inspect-brk=", { "--inspect-port", "--inspect-brk" });
298
299
5059
  AddOption("--inspect-brk-node", "", &DebugOptions::break_node_first_line);
300
5059
  Implies("--inspect-brk-node", "--inspect");
301
5059
  AddAlias("--inspect-brk-node=", { "--inspect-port", "--inspect-brk-node" });
302
303
  AddOption("--inspect-publish-uid",
304
            "comma separated list of destinations for inspector uid"
305
            "(default: stderr,http)",
306
            &DebugOptions::inspect_publish_uid_string,
307
5059
            kAllowedInEnvironment);
308
5059
}
309
310
5059
EnvironmentOptionsParser::EnvironmentOptionsParser() {
311
  AddOption("--enable-source-maps",
312
            "experimental Source Map V3 support",
313
            &EnvironmentOptions::enable_source_maps,
314
5059
            kAllowedInEnvironment);
315
  AddOption("--experimental-exports",
316
            "experimental support for exports in package.json",
317
            &EnvironmentOptions::experimental_exports,
318
5059
            kAllowedInEnvironment);
319
  AddOption("--experimental-loader",
320
            "(with --experimental-modules) use the specified file as a "
321
            "custom loader",
322
            &EnvironmentOptions::userland_loader,
323
5059
            kAllowedInEnvironment);
324
5059
  AddAlias("--loader", "--experimental-loader");
325
  AddOption("--experimental-modules",
326
            "experimental ES Module support and caching modules",
327
            &EnvironmentOptions::experimental_modules,
328
5059
            kAllowedInEnvironment);
329
5059
  Implies("--experimental-modules", "--experimental-exports");
330
  AddOption("--experimental-wasm-modules",
331
            "experimental ES Module support for webassembly modules",
332
            &EnvironmentOptions::experimental_wasm_modules,
333
5059
            kAllowedInEnvironment);
334
  AddOption("--experimental-policy",
335
            "use the specified file as a "
336
            "security policy",
337
            &EnvironmentOptions::experimental_policy,
338
5059
            kAllowedInEnvironment);
339
  AddOption("[has_policy_integrity_string]",
340
            "",
341
5059
            &EnvironmentOptions::has_policy_integrity_string);
342
  AddOption("--policy-integrity",
343
            "ensure the security policy contents match "
344
            "the specified integrity",
345
            &EnvironmentOptions::experimental_policy_integrity,
346
5059
            kAllowedInEnvironment);
347
5059
  Implies("--policy-integrity", "[has_policy_integrity_string]");
348
  AddOption("--experimental-repl-await",
349
            "experimental await keyword support in REPL",
350
            &EnvironmentOptions::experimental_repl_await,
351
5059
            kAllowedInEnvironment);
352
  AddOption("--experimental-vm-modules",
353
            "experimental ES Module support in vm module",
354
            &EnvironmentOptions::experimental_vm_modules,
355
5059
            kAllowedInEnvironment);
356
5059
  AddOption("--experimental-worker", "", NoOp{}, kAllowedInEnvironment);
357
#ifdef NODE_REPORT
358
  AddOption("--experimental-report",
359
            "enable report generation",
360
            &EnvironmentOptions::experimental_report,
361
5059
            kAllowedInEnvironment);
362
#endif  // NODE_REPORT
363
5059
  AddOption("--expose-internals", "", &EnvironmentOptions::expose_internals);
364
  AddOption("--frozen-intrinsics",
365
            "experimental frozen intrinsics support",
366
            &EnvironmentOptions::frozen_intrinsics,
367
5059
            kAllowedInEnvironment);
368
  AddOption("--heapsnapshot-signal",
369
            "Generate heap snapshot on specified signal",
370
            &EnvironmentOptions::heap_snapshot_signal,
371
5059
            kAllowedInEnvironment);
372
5059
  AddOption("--http-parser", "", NoOp{}, kAllowedInEnvironment);
373
  AddOption("--input-type",
374
            "set module type for string input",
375
            &EnvironmentOptions::module_type,
376
5059
            kAllowedInEnvironment);
377
  AddOption("--es-module-specifier-resolution",
378
            "Select extension resolution algorithm for es modules; "
379
            "either 'explicit' (default) or 'node'",
380
            &EnvironmentOptions::es_module_specifier_resolution,
381
5059
            kAllowedInEnvironment);
382
  AddOption("--no-deprecation",
383
            "silence deprecation warnings",
384
            &EnvironmentOptions::no_deprecation,
385
5059
            kAllowedInEnvironment);
386
  AddOption("--no-force-async-hooks-checks",
387
            "disable checks for async_hooks",
388
            &EnvironmentOptions::no_force_async_hooks_checks,
389
5059
            kAllowedInEnvironment);
390
  AddOption("--no-warnings",
391
            "silence all process warnings",
392
            &EnvironmentOptions::no_warnings,
393
5059
            kAllowedInEnvironment);
394
  AddOption("--force-context-aware",
395
            "disable loading non-context-aware addons",
396
            &EnvironmentOptions::force_context_aware,
397
5059
            kAllowedInEnvironment);
398
  AddOption("--pending-deprecation",
399
            "emit pending deprecation warnings",
400
            &EnvironmentOptions::pending_deprecation,
401
5059
            kAllowedInEnvironment);
402
  AddOption("--preserve-symlinks",
403
            "preserve symbolic links when resolving",
404
            &EnvironmentOptions::preserve_symlinks,
405
5059
            kAllowedInEnvironment);
406
  AddOption("--preserve-symlinks-main",
407
            "preserve symbolic links when resolving the main module",
408
            &EnvironmentOptions::preserve_symlinks_main,
409
5059
            kAllowedInEnvironment);
410
  AddOption("--prof-process",
411
            "process V8 profiler output generated using --prof",
412
5059
            &EnvironmentOptions::prof_process);
413
  // Options after --prof-process are passed through to the prof processor.
414
5059
  AddAlias("--prof-process", { "--prof-process", "--" });
415
#if HAVE_INSPECTOR
416
  AddOption("--cpu-prof",
417
            "Start the V8 CPU profiler on start up, and write the CPU profile "
418
            "to disk before exit. If --cpu-prof-dir is not specified, write "
419
            "the profile to the current working directory.",
420
5059
            &EnvironmentOptions::cpu_prof);
421
  AddOption("--cpu-prof-name",
422
            "specified file name of the V8 CPU profile generated with "
423
            "--cpu-prof",
424
5059
            &EnvironmentOptions::cpu_prof_name);
425
  AddOption("--cpu-prof-interval",
426
            "specified sampling interval in microseconds for the V8 CPU "
427
            "profile generated with --cpu-prof. (default: 1000)",
428
5059
            &EnvironmentOptions::cpu_prof_interval);
429
  AddOption("--cpu-prof-dir",
430
            "Directory where the V8 profiles generated by --cpu-prof will be "
431
            "placed. Does not affect --prof.",
432
5059
            &EnvironmentOptions::cpu_prof_dir);
433
  AddOption(
434
      "--heap-prof",
435
      "Start the V8 heap profiler on start up, and write the heap profile "
436
      "to disk before exit. If --heap-prof-dir is not specified, write "
437
      "the profile to the current working directory.",
438
5059
      &EnvironmentOptions::heap_prof);
439
  AddOption("--heap-prof-name",
440
            "specified file name of the V8 CPU profile generated with "
441
            "--heap-prof",
442
5059
            &EnvironmentOptions::heap_prof_name);
443
  AddOption("--heap-prof-dir",
444
            "Directory where the V8 heap profiles generated by --heap-prof "
445
            "will be placed.",
446
5059
            &EnvironmentOptions::heap_prof_dir);
447
  AddOption("--heap-prof-interval",
448
            "specified sampling interval in bytes for the V8 heap "
449
            "profile generated with --heap-prof. (default: 512 * 1024)",
450
5059
            &EnvironmentOptions::heap_prof_interval);
451
#endif  // HAVE_INSPECTOR
452
  AddOption("--redirect-warnings",
453
            "write warnings to file instead of stderr",
454
            &EnvironmentOptions::redirect_warnings,
455
5059
            kAllowedInEnvironment);
456
  AddOption("--test-udp-no-try-send", "",  // For testing only.
457
5059
            &EnvironmentOptions::test_udp_no_try_send);
458
  AddOption("--throw-deprecation",
459
            "throw an exception on deprecations",
460
            &EnvironmentOptions::throw_deprecation,
461
5059
            kAllowedInEnvironment);
462
  AddOption("--trace-deprecation",
463
            "show stack traces on deprecations",
464
            &EnvironmentOptions::trace_deprecation,
465
5059
            kAllowedInEnvironment);
466
  AddOption("--trace-sync-io",
467
            "show stack trace when use of sync IO is detected after the "
468
            "first tick",
469
            &EnvironmentOptions::trace_sync_io,
470
5059
            kAllowedInEnvironment);
471
  AddOption("--trace-tls",
472
            "prints TLS packet trace information to stderr",
473
            &EnvironmentOptions::trace_tls,
474
5059
            kAllowedInEnvironment);
475
  AddOption("--trace-warnings",
476
            "show stack traces on process warnings",
477
            &EnvironmentOptions::trace_warnings,
478
5059
            kAllowedInEnvironment);
479
  AddOption("--unhandled-rejections",
480
            "define unhandled rejections behavior. Options are 'strict' (raise "
481
            "an error), 'warn' (enforce warnings) or 'none' (silence warnings)",
482
            &EnvironmentOptions::unhandled_rejections,
483
5059
            kAllowedInEnvironment);
484
485
  AddOption("--check",
486
            "syntax check script without executing",
487
5059
            &EnvironmentOptions::syntax_check_only);
488
5059
  AddAlias("-c", "--check");
489
  // This option is only so that we can tell --eval with an empty string from
490
  // no eval at all. Having it not start with a dash makes it inaccessible
491
  // from the parser itself, but available for using Implies().
492
  // TODO(addaleax): When moving --help over to something generated from the
493
  // programmatic descriptions, this will need some special care.
494
  // (See also [ssl_openssl_cert_store] below.)
495
5059
  AddOption("[has_eval_string]", "", &EnvironmentOptions::has_eval_string);
496
5059
  AddOption("--eval", "evaluate script", &EnvironmentOptions::eval_string);
497
5059
  Implies("--eval", "[has_eval_string]");
498
  AddOption("--print",
499
            "evaluate script and print result",
500
5059
            &EnvironmentOptions::print_eval);
501
5059
  AddAlias("-e", "--eval");
502
5059
  AddAlias("--print <arg>", "-pe");
503
5059
  AddAlias("-pe", { "--print", "--eval" });
504
5059
  AddAlias("-p", "--print");
505
  AddOption("--require",
506
            "module to preload (option can be repeated)",
507
            &EnvironmentOptions::preload_modules,
508
5059
            kAllowedInEnvironment);
509
5059
  AddAlias("-r", "--require");
510
  AddOption("--interactive",
511
            "always enter the REPL even if stdin does not appear "
512
            "to be a terminal",
513
5059
            &EnvironmentOptions::force_repl);
514
5059
  AddAlias("-i", "--interactive");
515
516
5059
  AddOption("--napi-modules", "", NoOp{}, kAllowedInEnvironment);
517
518
  AddOption("--tls-min-v1.0",
519
            "set default TLS minimum to TLSv1.0 (default: TLSv1.2)",
520
            &EnvironmentOptions::tls_min_v1_0,
521
5059
            kAllowedInEnvironment);
522
  AddOption("--tls-min-v1.1",
523
            "set default TLS minimum to TLSv1.1 (default: TLSv1.2)",
524
            &EnvironmentOptions::tls_min_v1_1,
525
5059
            kAllowedInEnvironment);
526
  AddOption("--tls-min-v1.2",
527
            "set default TLS minimum to TLSv1.2 (default: TLSv1.2)",
528
            &EnvironmentOptions::tls_min_v1_2,
529
5059
            kAllowedInEnvironment);
530
  AddOption("--tls-min-v1.3",
531
            "set default TLS minimum to TLSv1.3 (default: TLSv1.2)",
532
            &EnvironmentOptions::tls_min_v1_3,
533
5059
            kAllowedInEnvironment);
534
  AddOption("--tls-max-v1.2",
535
            "set default TLS maximum to TLSv1.2 (default: TLSv1.3)",
536
            &EnvironmentOptions::tls_max_v1_2,
537
5059
            kAllowedInEnvironment);
538
  // Current plan is:
539
  // - 11.x and below: TLS1.3 is opt-in with --tls-max-v1.3
540
  // - 12.x: TLS1.3 is opt-out with --tls-max-v1.2
541
  // In either case, support both options they are uniformly available.
542
  AddOption("--tls-max-v1.3",
543
            "set default TLS maximum to TLSv1.3 (default: TLSv1.3)",
544
            &EnvironmentOptions::tls_max_v1_3,
545
5059
            kAllowedInEnvironment);
546
5059
}
547
548
5059
PerIsolateOptionsParser::PerIsolateOptionsParser(
549
5059
  const EnvironmentOptionsParser& eop) {
550
  AddOption("--track-heap-objects",
551
            "track heap object allocations for heap snapshots",
552
            &PerIsolateOptions::track_heap_objects,
553
5059
            kAllowedInEnvironment);
554
  AddOption("--no-node-snapshot",
555
            "",  // It's a debug-only option.
556
            &PerIsolateOptions::no_node_snapshot,
557
5059
            kAllowedInEnvironment);
558
559
  // Explicitly add some V8 flags to mark them as allowed in NODE_OPTIONS.
560
  AddOption("--abort-on-uncaught-exception",
561
            "aborting instead of exiting causes a core file to be generated "
562
            "for analysis",
563
            V8Option{},
564
5059
            kAllowedInEnvironment);
565
  AddOption("--interpreted-frames-native-stack",
566
            "help system profilers to translate JavaScript interpreted frames",
567
5059
            V8Option{}, kAllowedInEnvironment);
568
5059
  AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvironment);
569
5059
  AddOption("--perf-basic-prof", "", V8Option{}, kAllowedInEnvironment);
570
  AddOption("--perf-basic-prof-only-functions",
571
            "",
572
            V8Option{},
573
5059
            kAllowedInEnvironment);
574
5059
  AddOption("--perf-prof", "", V8Option{}, kAllowedInEnvironment);
575
  AddOption("--perf-prof-unwinding-info",
576
            "",
577
            V8Option{},
578
5059
            kAllowedInEnvironment);
579
5059
  AddOption("--stack-trace-limit", "", V8Option{}, kAllowedInEnvironment);
580
581
#ifdef NODE_REPORT
582
  AddOption("--report-uncaught-exception",
583
            "generate diagnostic report on uncaught exceptions",
584
            &PerIsolateOptions::report_uncaught_exception,
585
5059
            kAllowedInEnvironment);
586
  AddOption("--report-on-signal",
587
            "generate diagnostic report upon receiving signals",
588
            &PerIsolateOptions::report_on_signal,
589
5059
            kAllowedInEnvironment);
590
  AddOption("--report-on-fatalerror",
591
            "generate diagnostic report on fatal (internal) errors",
592
            &PerIsolateOptions::report_on_fatalerror,
593
5059
            kAllowedInEnvironment);
594
  AddOption("--report-signal",
595
            "causes diagnostic report to be produced on provided signal,"
596
            " unsupported in Windows. (default: SIGUSR2)",
597
            &PerIsolateOptions::report_signal,
598
5059
            kAllowedInEnvironment);
599
5059
  Implies("--report-signal", "--report-on-signal");
600
  AddOption("--report-filename",
601
            "define custom report file name."
602
            " (default: YYYYMMDD.HHMMSS.PID.SEQUENCE#.txt)",
603
            &PerIsolateOptions::report_filename,
604
5059
            kAllowedInEnvironment);
605
  AddOption("--report-directory",
606
            "define custom report pathname."
607
            " (default: current working directory of Node.js process)",
608
            &PerIsolateOptions::report_directory,
609
5059
            kAllowedInEnvironment);
610
#endif  // NODE_REPORT
611
612
5059
  Insert(eop, &PerIsolateOptions::get_per_env_options);
613
5059
}
614
615
5059
PerProcessOptionsParser::PerProcessOptionsParser(
616
5059
  const PerIsolateOptionsParser& iop) {
617
  AddOption("--title",
618
            "the process title to use on startup",
619
            &PerProcessOptions::title,
620
5059
            kAllowedInEnvironment);
621
  AddOption("--trace-event-categories",
622
            "comma separated list of trace event categories to record",
623
            &PerProcessOptions::trace_event_categories,
624
5059
            kAllowedInEnvironment);
625
  AddOption("--trace-event-file-pattern",
626
            "Template string specifying the filepath for the trace-events "
627
            "data, it supports ${rotation} and ${pid}.",
628
            &PerProcessOptions::trace_event_file_pattern,
629
5059
            kAllowedInEnvironment);
630
  AddAlias("--trace-events-enabled", {
631
5059
    "--trace-event-categories", "v8,node,node.async_hooks" });
632
  AddOption("--max-http-header-size",
633
            "set the maximum size of HTTP headers (default: 8KB)",
634
            &PerProcessOptions::max_http_header_size,
635
5059
            kAllowedInEnvironment);
636
  AddOption("--v8-pool-size",
637
            "set V8's thread pool size",
638
            &PerProcessOptions::v8_thread_pool_size,
639
5059
            kAllowedInEnvironment);
640
  AddOption("--zero-fill-buffers",
641
            "automatically zero-fill all newly allocated Buffer and "
642
            "SlowBuffer instances",
643
            &PerProcessOptions::zero_fill_all_buffers,
644
5059
            kAllowedInEnvironment);
645
  AddOption("--debug-arraybuffer-allocations",
646
            "", /* undocumented, only for debugging */
647
            &PerProcessOptions::debug_arraybuffer_allocations,
648
5059
            kAllowedInEnvironment);
649
650
651
  // 12.x renamed this inadvertently, so alias it for consistency within the
652
  // release line, while using the original name for consistency with older
653
  // release lines.
654
5059
  AddOption("--security-revert", "", &PerProcessOptions::security_reverts);
655
5059
  AddAlias("--security-reverts", "--security-revert");
656
  AddOption("--completion-bash",
657
            "print source-able bash completion script",
658
5059
            &PerProcessOptions::print_bash_completion);
659
  AddOption("--help",
660
            "print node command line options",
661
5059
            &PerProcessOptions::print_help);
662
5059
  AddAlias("-h", "--help");
663
  AddOption(
664
5059
      "--version", "print Node.js version", &PerProcessOptions::print_version);
665
5059
  AddAlias("-v", "--version");
666
  AddOption("--v8-options",
667
            "print V8 command line options",
668
5059
            &PerProcessOptions::print_v8_help);
669
670
#ifdef NODE_HAVE_I18N_SUPPORT
671
  AddOption("--icu-data-dir",
672
            "set ICU data load path to dir (overrides NODE_ICU_DATA)"
673
#ifndef NODE_HAVE_SMALL_ICU
674
            " (note: linked-in ICU data is present)\n"
675
#endif
676
            ,
677
            &PerProcessOptions::icu_data_dir,
678
5059
            kAllowedInEnvironment);
679
#endif
680
681
#if HAVE_OPENSSL
682
  AddOption("--openssl-config",
683
            "load OpenSSL configuration from the specified file "
684
            "(overrides OPENSSL_CONF)",
685
            &PerProcessOptions::openssl_config,
686
5059
            kAllowedInEnvironment);
687
  AddOption("--tls-cipher-list",
688
            "use an alternative default TLS cipher list",
689
            &PerProcessOptions::tls_cipher_list,
690
5059
            kAllowedInEnvironment);
691
  AddOption("--use-openssl-ca",
692
            "use OpenSSL's default CA store"
693
#if defined(NODE_OPENSSL_CERT_STORE)
694
            " (default)"
695
#endif
696
            ,
697
            &PerProcessOptions::use_openssl_ca,
698
5059
            kAllowedInEnvironment);
699
  AddOption("--use-bundled-ca",
700
            "use bundled CA store"
701
#if !defined(NODE_OPENSSL_CERT_STORE)
702
            " (default)"
703
#endif
704
            ,
705
            &PerProcessOptions::use_bundled_ca,
706
5059
            kAllowedInEnvironment);
707
  // Similar to [has_eval_string] above, except that the separation between
708
  // this and use_openssl_ca only exists for option validation after parsing.
709
  // This is not ideal.
710
  AddOption("[ssl_openssl_cert_store]",
711
            "",
712
5059
            &PerProcessOptions::ssl_openssl_cert_store);
713
5059
  Implies("--use-openssl-ca", "[ssl_openssl_cert_store]");
714
5059
  ImpliesNot("--use-bundled-ca", "[ssl_openssl_cert_store]");
715
#if NODE_FIPS_MODE
716
  AddOption("--enable-fips",
717
            "enable FIPS crypto at startup",
718
            &PerProcessOptions::enable_fips_crypto,
719
            kAllowedInEnvironment);
720
  AddOption("--force-fips",
721
            "force FIPS crypto (cannot be disabled)",
722
            &PerProcessOptions::force_fips_crypto,
723
            kAllowedInEnvironment);
724
#endif
725
#endif
726
727
5059
  Insert(iop, &PerProcessOptions::get_per_isolate_options);
728
5059
}
729
730
110
inline std::string RemoveBrackets(const std::string& host) {
731


110
  if (!host.empty() && host.front() == '[' && host.back() == ']')
732
4
    return host.substr(1, host.size() - 2);
733
  else
734
106
    return host;
735
}
736
737
98
inline int ParseAndValidatePort(const std::string& port,
738
                                std::vector<std::string>* errors) {
739
  char* endptr;
740
98
  errno = 0;
741
  const unsigned long result =                 // NOLINT(runtime/int)
742
98
    strtoul(port.c_str(), &endptr, 10);
743

98
  if (errno != 0 || *endptr != '\0'||
744

63
      (result != 0 && result < 1024) || result > 65535) {
745
    errors->push_back(" must be 0 or in range 1024 to 65535.");
746
  }
747
98
  return static_cast<int>(result);
748
}
749
750
98
HostPort SplitHostPort(const std::string& arg,
751
                      std::vector<std::string>* errors) {
752
  // remove_brackets only works if no port is specified
753
  // so if it has an effect only an IPv6 address was specified.
754
98
  std::string host = RemoveBrackets(arg);
755
98
  if (host.length() < arg.length())
756
    return HostPort{host, DebugOptions::kDefaultInspectorPort};
757
758
98
  size_t colon = arg.rfind(':');
759
98
  if (colon == std::string::npos) {
760
    // Either a port number or a host name.  Assume that
761
    // if it's not all decimal digits, it's a host name.
762
386
    for (char c : arg) {
763

300
      if (c < '0' || c > '9') {
764
        return HostPort{arg, DebugOptions::kDefaultInspectorPort};
765
      }
766
    }
767
86
    return HostPort { "", ParseAndValidatePort(arg, errors) };
768
  }
769
  // Host and port found:
770
  return HostPort { RemoveBrackets(arg.substr(0, colon)),
771
12
                    ParseAndValidatePort(arg.substr(colon + 1), errors) };
772
}
773
774
// Return a map containing all the options and their metadata as well
775
// as the aliases
776
5208
void GetOptions(const FunctionCallbackInfo<Value>& args) {
777
5208
  Mutex::ScopedLock lock(per_process::cli_options_mutex);
778
5208
  Environment* env = Environment::GetCurrent(args);
779
5208
  if (!env->has_run_bootstrapping_code()) {
780
    // No code because this is an assertion.
781
    return env->ThrowError(
782
        "Should not query options before bootstrapping is done");
783
  }
784
5208
  env->set_has_serialized_options(true);
785
786
5208
  Isolate* isolate = env->isolate();
787
5208
  Local<Context> context = env->context();
788
789
  // Temporarily act as if the current Environment's/IsolateData's options were
790
  // the default options, i.e. like they are the ones we'd access for global
791
  // options parsing, so that all options are available from the main parser.
792
10416
  auto original_per_isolate = per_process::cli_options->per_isolate;
793
5208
  per_process::cli_options->per_isolate = env->isolate_data()->options();
794
10416
  auto original_per_env = per_process::cli_options->per_isolate->per_env;
795
5208
  per_process::cli_options->per_isolate->per_env = env->options();
796
5208
  OnScopeLeave on_scope_leave([&]() {
797
5208
    per_process::cli_options->per_isolate->per_env = original_per_env;
798
5208
    per_process::cli_options->per_isolate = original_per_isolate;
799
15624
  });
800
801
5208
  Local<Map> options = Map::New(isolate);
802
505176
  for (const auto& item : _ppop_instance.options_) {
803
    Local<Value> value;
804
499968
    const auto& option_info = item.second;
805
499968
    auto field = option_info.field;
806
499968
    PerProcessOptions* opts = per_process::cli_options.get();
807


499968
    switch (option_info.type) {
808
      case kNoOp:
809
      case kV8Option:
810
        // Special case for --abort-on-uncaught-exception which is also
811
        // respected by Node.js internals
812
57288
        if (item.first == "--abort-on-uncaught-exception") {
813
          value = Boolean::New(
814
10416
            isolate, original_per_env->abort_on_uncaught_exception);
815
        } else {
816
52080
          value = Undefined(isolate);
817
        }
818
57288
        break;
819
      case kBoolean:
820
        value = Boolean::New(isolate,
821
572880
                             *_ppop_instance.Lookup<bool>(field, opts));
822
286440
        break;
823
      case kInteger:
824
        value = Number::New(isolate,
825
10416
                            *_ppop_instance.Lookup<int64_t>(field, opts));
826
5208
        break;
827
      case kUInteger:
828
        value = Number::New(isolate,
829
31248
                            *_ppop_instance.Lookup<uint64_t>(field, opts));
830
15624
        break;
831
      case kString:
832
239568
        if (!ToV8Value(context,
833
239568
                       *_ppop_instance.Lookup<std::string>(field, opts))
834
359352
                 .ToLocal(&value)) {
835
          return;
836
        }
837
119784
        break;
838
      case kStringList:
839
20832
        if (!ToV8Value(context,
840
20832
                       *_ppop_instance.Lookup<StringVector>(field, opts))
841
31248
                 .ToLocal(&value)) {
842
          return;
843
        }
844
10416
        break;
845
      case kHostPort: {
846
        const HostPort& host_port =
847
5208
          *_ppop_instance.Lookup<HostPort>(field, opts);
848
5208
        Local<Object> obj = Object::New(isolate);
849
        Local<Value> host;
850

31248
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
851


46872
            obj->Set(context, env->host_string(), host).IsNothing() ||
852
            obj->Set(context,
853
                     env->port_string(),
854

20832
                     Integer::New(isolate, host_port.port()))
855
26040
                .IsNothing()) {
856
          return;
857
        }
858
5208
        value = obj;
859
5208
        break;
860
      }
861
      default:
862
        UNREACHABLE();
863
    }
864
499968
    CHECK(!value.IsEmpty());
865
866
999936
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
867
499968
    Local<Object> info = Object::New(isolate);
868
    Local<Value> help_text;
869

3499776
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
870
1499904
        !info->Set(context, env->help_text_string(), help_text)
871

4499712
             .FromMaybe(false) ||
872
        !info->Set(context,
873
                   env->env_var_settings_string(),
874
                   Integer::New(isolate,
875

1999872
                                static_cast<int>(option_info.env_setting)))
876

4999680
             .FromMaybe(false) ||
877
        !info->Set(context,
878
                   env->type_string(),
879

1999872
                   Integer::New(isolate, static_cast<int>(option_info.type)))
880

4499712
             .FromMaybe(false) ||
881


4999680
        info->Set(context, env->value_string(), value).IsNothing() ||
882

1999872
        options->Set(context, name, info).IsEmpty()) {
883
      return;
884
    }
885
499968
  }
886
887
  Local<Value> aliases;
888
10416
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
889
890
5208
  Local<Object> ret = Object::New(isolate);
891



41664
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
892

26040
      ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
893
    return;
894
  }
895
896
15624
  args.GetReturnValue().Set(ret);
897
}
898
899
5208
void Initialize(Local<Object> target,
900
                Local<Value> unused,
901
                Local<Context> context,
902
                void* priv) {
903
5208
  Environment* env = Environment::GetCurrent(context);
904
5208
  Isolate* isolate = env->isolate();
905
5208
  env->SetMethodNoSideEffect(target, "getOptions", GetOptions);
906
907
5208
  Local<Object> env_settings = Object::New(isolate);
908
20832
  NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment);
909
20832
  NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment);
910
  target
911
      ->Set(
912
15624
          context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
913
10416
      .Check();
914
915
5208
  Local<Object> types = Object::New(isolate);
916
20832
  NODE_DEFINE_CONSTANT(types, kNoOp);
917
20832
  NODE_DEFINE_CONSTANT(types, kV8Option);
918
20832
  NODE_DEFINE_CONSTANT(types, kBoolean);
919
20832
  NODE_DEFINE_CONSTANT(types, kInteger);
920
20832
  NODE_DEFINE_CONSTANT(types, kUInteger);
921
20832
  NODE_DEFINE_CONSTANT(types, kString);
922
20832
  NODE_DEFINE_CONSTANT(types, kHostPort);
923
20832
  NODE_DEFINE_CONSTANT(types, kStringList);
924
15624
  target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types)
925
10416
      .Check();
926
5208
}
927
928
}  // namespace options_parser
929
}  // namespace node
930
931

20235
NODE_MODULE_CONTEXT_AWARE_INTERNAL(options, node::options_parser::Initialize)