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: 324 351 92.3 %
Date: 2019-09-23 22:30:09 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
5048
Mutex cli_options_mutex;
26
5048
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
27
}  // namespace per_process
28
29
9939
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
9939
  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
9939
      SplitString(inspect_publish_uid_string, ',');
45
9939
  inspect_publish_uid.console = false;
46
9939
  inspect_publish_uid.http = false;
47
29815
  for (const std::string& destination : destinations) {
48
19876
    if (destination == "stderr") {
49
9938
      inspect_publish_uid.console = true;
50
9938
    } else if (destination == "http") {
51
9938
      inspect_publish_uid.http = true;
52
    } else {
53
      errors->push_back("--inspect-publish-uid destination can be "
54
                        "stderr or http");
55
    }
56
9939
  }
57
9939
}
58
59
9927
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
60
#if HAVE_OPENSSL
61

9927
  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
9927
  per_isolate->CheckOptions(errors);
67
9927
}
68
69
9939
void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) {
70
9939
  per_env->CheckOptions(errors);
71
#ifdef NODE_REPORT
72
9939
  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
9949
    return;
79
  }
80
81
9929
  if (!report_directory.empty()) {
82
    errors->push_back("--report-directory option is valid only when "
83
                      "--experimental-report is set");
84
  }
85
86
9929
  if (!report_filename.empty()) {
87
    errors->push_back("--report-filename option is valid only when "
88
                      "--experimental-report is set");
89
  }
90
91
9929
  if (!report_signal.empty()) {
92
    errors->push_back("--report-signal option is valid only when "
93
                      "--experimental-report is set");
94
  }
95
96
9929
  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
9929
  if (report_on_signal) {
103
    errors->push_back("--report-on-signal option is valid only when "
104
                      "--experimental-report is set");
105
  }
106
107
9929
  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
9939
void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
116

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

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

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

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

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

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

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

19887
  if (!unhandled_rejections.empty() &&
158
16
      unhandled_rejections != "strict" &&
159

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

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


110
  if (!host.empty() && host.front() == '[' && host.back() == ']')
720
4
    return host.substr(1, host.size() - 2);
721
  else
722
106
    return host;
723
}
724
725
98
inline int ParseAndValidatePort(const std::string& port,
726
                                std::vector<std::string>* errors) {
727
  char* endptr;
728
98
  errno = 0;
729
  const unsigned long result =                 // NOLINT(runtime/int)
730
98
    strtoul(port.c_str(), &endptr, 10);
731

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

63
      (result != 0 && result < 1024) || result > 65535) {
733
    errors->push_back(" must be 0 or in range 1024 to 65535.");
734
  }
735
98
  return static_cast<int>(result);
736
}
737
738
98
HostPort SplitHostPort(const std::string& arg,
739
                      std::vector<std::string>* errors) {
740
  // remove_brackets only works if no port is specified
741
  // so if it has an effect only an IPv6 address was specified.
742
98
  std::string host = RemoveBrackets(arg);
743
98
  if (host.length() < arg.length())
744
    return HostPort{host, DebugOptions::kDefaultInspectorPort};
745
746
98
  size_t colon = arg.rfind(':');
747
98
  if (colon == std::string::npos) {
748
    // Either a port number or a host name.  Assume that
749
    // if it's not all decimal digits, it's a host name.
750
386
    for (char c : arg) {
751

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


482949
    switch (option_info.type) {
796
      case kNoOp:
797
      case kV8Option:
798
        // Special case for --abort-on-uncaught-exception which is also
799
        // respected by Node.js internals
800
57123
        if (item.first == "--abort-on-uncaught-exception") {
801
          value = Boolean::New(
802
10386
            isolate, original_per_env->abort_on_uncaught_exception);
803
        } else {
804
51930
          value = Undefined(isolate);
805
        }
806
57123
        break;
807
      case kBoolean:
808
        value = Boolean::New(isolate,
809
540072
                             *_ppop_instance.Lookup<bool>(field, opts));
810
270036
        break;
811
      case kInteger:
812
        value = Number::New(isolate,
813
10386
                            *_ppop_instance.Lookup<int64_t>(field, opts));
814
5193
        break;
815
      case kUInteger:
816
        value = Number::New(isolate,
817
31158
                            *_ppop_instance.Lookup<uint64_t>(field, opts));
818
15579
        break;
819
      case kString:
820
238878
        if (!ToV8Value(context,
821
238878
                       *_ppop_instance.Lookup<std::string>(field, opts))
822
358317
                 .ToLocal(&value)) {
823
          return;
824
        }
825
119439
        break;
826
      case kStringList:
827
20772
        if (!ToV8Value(context,
828
20772
                       *_ppop_instance.Lookup<StringVector>(field, opts))
829
31158
                 .ToLocal(&value)) {
830
          return;
831
        }
832
10386
        break;
833
      case kHostPort: {
834
        const HostPort& host_port =
835
5193
          *_ppop_instance.Lookup<HostPort>(field, opts);
836
5193
        Local<Object> obj = Object::New(isolate);
837
        Local<Value> host;
838

31158
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
839


46737
            obj->Set(context, env->host_string(), host).IsNothing() ||
840
            obj->Set(context,
841
                     env->port_string(),
842

20772
                     Integer::New(isolate, host_port.port()))
843
25965
                .IsNothing()) {
844
          return;
845
        }
846
5193
        value = obj;
847
5193
        break;
848
      }
849
      default:
850
        UNREACHABLE();
851
    }
852
482949
    CHECK(!value.IsEmpty());
853
854
965898
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
855
482949
    Local<Object> info = Object::New(isolate);
856
    Local<Value> help_text;
857

3380643
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
858
1448847
        !info->Set(context, env->help_text_string(), help_text)
859

4346541
             .FromMaybe(false) ||
860
        !info->Set(context,
861
                   env->env_var_settings_string(),
862
                   Integer::New(isolate,
863

1931796
                                static_cast<int>(option_info.env_setting)))
864

4829490
             .FromMaybe(false) ||
865
        !info->Set(context,
866
                   env->type_string(),
867

1931796
                   Integer::New(isolate, static_cast<int>(option_info.type)))
868

4346541
             .FromMaybe(false) ||
869


4829490
        info->Set(context, env->value_string(), value).IsNothing() ||
870

1931796
        options->Set(context, name, info).IsEmpty()) {
871
      return;
872
    }
873
482949
  }
874
875
  Local<Value> aliases;
876
10386
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
877
878
5193
  Local<Object> ret = Object::New(isolate);
879



41544
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
880

25965
      ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
881
    return;
882
  }
883
884
15579
  args.GetReturnValue().Set(ret);
885
}
886
887
5193
void Initialize(Local<Object> target,
888
                Local<Value> unused,
889
                Local<Context> context,
890
                void* priv) {
891
5193
  Environment* env = Environment::GetCurrent(context);
892
5193
  Isolate* isolate = env->isolate();
893
5193
  env->SetMethodNoSideEffect(target, "getOptions", GetOptions);
894
895
5193
  Local<Object> env_settings = Object::New(isolate);
896
20772
  NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment);
897
20772
  NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment);
898
  target
899
      ->Set(
900
15579
          context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
901
10386
      .Check();
902
903
5193
  Local<Object> types = Object::New(isolate);
904
20772
  NODE_DEFINE_CONSTANT(types, kNoOp);
905
20772
  NODE_DEFINE_CONSTANT(types, kV8Option);
906
20772
  NODE_DEFINE_CONSTANT(types, kBoolean);
907
20772
  NODE_DEFINE_CONSTANT(types, kInteger);
908
20772
  NODE_DEFINE_CONSTANT(types, kUInteger);
909
20772
  NODE_DEFINE_CONSTANT(types, kString);
910
20772
  NODE_DEFINE_CONSTANT(types, kHostPort);
911
20772
  NODE_DEFINE_CONSTANT(types, kStringList);
912
15579
  target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types)
913
10386
      .Check();
914
5193
}
915
916
}  // namespace options_parser
917
}  // namespace node
918
919

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