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: 325 353 92.1 %
Date: 2019-09-13 22:28:55 Branches: 186 276 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
5031
Mutex cli_options_mutex;
26
5031
std::shared_ptr<PerProcessOptions> cli_options{new PerProcessOptions()};
27
}  // namespace per_process
28
29
9905
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
9905
  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
9905
      SplitString(inspect_publish_uid_string, ',');
45
9905
  inspect_publish_uid.console = false;
46
9905
  inspect_publish_uid.http = false;
47
29713
  for (const std::string& destination : destinations) {
48
19808
    if (destination == "stderr") {
49
9904
      inspect_publish_uid.console = true;
50
9904
    } else if (destination == "http") {
51
9904
      inspect_publish_uid.http = true;
52
    } else {
53
      errors->push_back("--inspect-publish-uid destination can be "
54
                        "stderr or http");
55
    }
56
9905
  }
57
9905
}
58
59
9893
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
60
#if HAVE_OPENSSL
61

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

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

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

9905
  if (has_policy_integrity_string && experimental_policy_integrity.empty()) {
124
1
    errors->push_back("--policy-integrity cannot be empty");
125
  }
126
127
9905
  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

9905
  if (experimental_wasm_modules && !experimental_modules) {
138
    errors->push_back("--experimental-wasm-modules requires "
139
                      "--experimental-modules be enabled");
140
  }
141
142
9905
  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

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

9905
  if (http_parser != "legacy" && http_parser != "llhttp") {
158
    errors->push_back("invalid value for --http-parser");
159
  }
160
161

19819
  if (!unhandled_rejections.empty() &&
162
16
      unhandled_rejections != "strict" &&
163

9917
      unhandled_rejections != "warn" &&
164
5
      unhandled_rejections != "none") {
165
1
    errors->push_back("invalid value for --unhandled-rejections");
166
  }
167
168

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


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

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

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

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


481089
    switch (option_info.type) {
804
      case kNoOp:
805
      case kV8Option:
806
        // Special case for --abort-on-uncaught-exception which is also
807
        // respected by Node.js internals
808
51730
        if (item.first == "--abort-on-uncaught-exception") {
809
          value = Boolean::New(
810
10346
            isolate, original_per_env->abort_on_uncaught_exception);
811
        } else {
812
46557
          value = Undefined(isolate);
813
        }
814
51730
        break;
815
      case kBoolean:
816
        value = Boolean::New(isolate,
817
537992
                             *_ppop_instance.Lookup<bool>(field, opts));
818
268996
        break;
819
      case kInteger:
820
        value = Number::New(isolate,
821
10346
                            *_ppop_instance.Lookup<int64_t>(field, opts));
822
5173
        break;
823
      case kUInteger:
824
        value = Number::New(isolate,
825
31038
                            *_ppop_instance.Lookup<uint64_t>(field, opts));
826
15519
        break;
827
      case kString:
828
248304
        if (!ToV8Value(context,
829
248304
                       *_ppop_instance.Lookup<std::string>(field, opts))
830
372456
                 .ToLocal(&value)) {
831
          return;
832
        }
833
124152
        break;
834
      case kStringList:
835
20692
        if (!ToV8Value(context,
836
20692
                       *_ppop_instance.Lookup<StringVector>(field, opts))
837
31038
                 .ToLocal(&value)) {
838
          return;
839
        }
840
10346
        break;
841
      case kHostPort: {
842
        const HostPort& host_port =
843
5173
          *_ppop_instance.Lookup<HostPort>(field, opts);
844
5173
        Local<Object> obj = Object::New(isolate);
845
        Local<Value> host;
846

31038
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
847


46557
            obj->Set(context, env->host_string(), host).IsNothing() ||
848
            obj->Set(context,
849
                     env->port_string(),
850

20692
                     Integer::New(isolate, host_port.port()))
851
25865
                .IsNothing()) {
852
          return;
853
        }
854
5173
        value = obj;
855
5173
        break;
856
      }
857
      default:
858
        UNREACHABLE();
859
    }
860
481089
    CHECK(!value.IsEmpty());
861
862
962178
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
863
481089
    Local<Object> info = Object::New(isolate);
864
    Local<Value> help_text;
865

3367623
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
866
1443267
        !info->Set(context, env->help_text_string(), help_text)
867

4329801
             .FromMaybe(false) ||
868
        !info->Set(context,
869
                   env->env_var_settings_string(),
870
                   Integer::New(isolate,
871

1924356
                                static_cast<int>(option_info.env_setting)))
872

4810890
             .FromMaybe(false) ||
873
        !info->Set(context,
874
                   env->type_string(),
875

1924356
                   Integer::New(isolate, static_cast<int>(option_info.type)))
876

4329801
             .FromMaybe(false) ||
877


4810890
        info->Set(context, env->value_string(), value).IsNothing() ||
878

1924356
        options->Set(context, name, info).IsEmpty()) {
879
      return;
880
    }
881
481089
  }
882
883
  Local<Value> aliases;
884
10346
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
885
886
5173
  Local<Object> ret = Object::New(isolate);
887



41384
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
888

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

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