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

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

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

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

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

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

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

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

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

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


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

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

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

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


484664
    switch (option_info.type) {
800
      case kNoOp:
801
      case kV8Option:
802
        // Special case for --abort-on-uncaught-exception which is also
803
        // respected by Node.js internals
804
56716
        if (item.first == "--abort-on-uncaught-exception") {
805
          value = Boolean::New(
806
10312
            isolate, original_per_env->abort_on_uncaught_exception);
807
        } else {
808
51560
          value = Undefined(isolate);
809
        }
810
56716
        break;
811
      case kBoolean:
812
        value = Boolean::New(isolate,
813
546536
                             *_ppop_instance.Lookup<bool>(field, opts));
814
273268
        break;
815
      case kInteger:
816
        value = Number::New(isolate,
817
10312
                            *_ppop_instance.Lookup<int64_t>(field, opts));
818
5156
        break;
819
      case kUInteger:
820
        value = Number::New(isolate,
821
30936
                            *_ppop_instance.Lookup<uint64_t>(field, opts));
822
15468
        break;
823
      case kString:
824
237176
        if (!ToV8Value(context,
825
237176
                       *_ppop_instance.Lookup<std::string>(field, opts))
826
355764
                 .ToLocal(&value)) {
827
          return;
828
        }
829
118588
        break;
830
      case kStringList:
831
20624
        if (!ToV8Value(context,
832
20624
                       *_ppop_instance.Lookup<StringVector>(field, opts))
833
30936
                 .ToLocal(&value)) {
834
          return;
835
        }
836
10312
        break;
837
      case kHostPort: {
838
        const HostPort& host_port =
839
5156
          *_ppop_instance.Lookup<HostPort>(field, opts);
840
5156
        Local<Object> obj = Object::New(isolate);
841
        Local<Value> host;
842

30936
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
843


46404
            obj->Set(context, env->host_string(), host).IsNothing() ||
844
            obj->Set(context,
845
                     env->port_string(),
846

20624
                     Integer::New(isolate, host_port.port()))
847
25780
                .IsNothing()) {
848
          return;
849
        }
850
5156
        value = obj;
851
5156
        break;
852
      }
853
      default:
854
        UNREACHABLE();
855
    }
856
484664
    CHECK(!value.IsEmpty());
857
858
969328
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
859
484664
    Local<Object> info = Object::New(isolate);
860
    Local<Value> help_text;
861

3392648
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
862
1453992
        !info->Set(context, env->help_text_string(), help_text)
863

4361976
             .FromMaybe(false) ||
864
        !info->Set(context,
865
                   env->env_var_settings_string(),
866
                   Integer::New(isolate,
867

1938656
                                static_cast<int>(option_info.env_setting)))
868

4846640
             .FromMaybe(false) ||
869
        !info->Set(context,
870
                   env->type_string(),
871

1938656
                   Integer::New(isolate, static_cast<int>(option_info.type)))
872

4361976
             .FromMaybe(false) ||
873


4846640
        info->Set(context, env->value_string(), value).IsNothing() ||
874

1938656
        options->Set(context, name, info).IsEmpty()) {
875
      return;
876
    }
877
484664
  }
878
879
  Local<Value> aliases;
880
10312
  if (!ToV8Value(context, _ppop_instance.aliases_).ToLocal(&aliases)) return;
881
882
5156
  Local<Object> ret = Object::New(isolate);
883



41248
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
884

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

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