GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_options.cc Lines: 212 223 95.1 %
Date: 2019-01-07 12:15:22 Branches: 113 178 63.5 %

Line Branch Exec Source
1
#include <errno.h>
2
#include "node_internals.h"
3
#include "node_options-inl.h"
4
5
using v8::Boolean;
6
using v8::Context;
7
using v8::FunctionCallbackInfo;
8
using v8::Integer;
9
using v8::Isolate;
10
using v8::Local;
11
using v8::Map;
12
using v8::Number;
13
using v8::Object;
14
using v8::String;
15
using v8::Undefined;
16
using v8::Value;
17
18
namespace node {
19
20
7027
void PerProcessOptions::CheckOptions(std::vector<std::string>* errors) {
21
#if HAVE_OPENSSL
22

7027
  if (use_openssl_ca && use_bundled_ca) {
23
    errors->push_back("either --use-openssl-ca or --use-bundled-ca can be "
24
1
                      "used, not both");
25
  }
26
#endif
27
7027
  per_isolate->CheckOptions(errors);
28
7027
}
29
30
7027
void PerIsolateOptions::CheckOptions(std::vector<std::string>* errors) {
31
7027
  per_env->CheckOptions(errors);
32
7027
}
33
34
7027
void EnvironmentOptions::CheckOptions(std::vector<std::string>* errors) {
35

7027
  if (!userland_loader.empty() && !experimental_modules) {
36
    errors->push_back("--loader requires --experimental-modules be enabled");
37
  }
38
39

7027
  if (syntax_check_only && has_eval_string) {
40
4
    errors->push_back("either --check or --eval can be used, not both");
41
  }
42
43

7027
  if (http_parser != "legacy" && http_parser != "llhttp") {
44
    errors->push_back("invalid value for --http-parser");
45
  }
46
47
#if HAVE_INSPECTOR
48
7027
  debug_options_.CheckOptions(errors);
49
#endif  // HAVE_INSPECTOR
50
7027
}
51
52
namespace options_parser {
53
54
// XXX: If you add an option here, please also add it to doc/node.1 and
55
// doc/api/cli.md
56
// TODO(addaleax): Make that unnecessary.
57
58
3597
DebugOptionsParser::DebugOptionsParser() {
59
  AddOption("--inspect-port",
60
            "set host:port for inspector",
61
            &DebugOptions::host_port,
62
3597
            kAllowedInEnvironment);
63
3597
  AddAlias("--debug-port", "--inspect-port");
64
65
  AddOption("--inspect",
66
            "activate inspector on host:port (default: 127.0.0.1:9229)",
67
            &DebugOptions::inspector_enabled,
68
3597
            kAllowedInEnvironment);
69
3597
  AddAlias("--inspect=", { "--inspect-port", "--inspect" });
70
71
3597
  AddOption("--debug", "", &DebugOptions::deprecated_debug);
72
3597
  AddAlias("--debug=", { "--inspect-port", "--debug" });
73
74
  AddOption("--inspect-brk",
75
            "activate inspector on host:port and break at start of user script",
76
            &DebugOptions::break_first_line,
77
3597
            kAllowedInEnvironment);
78
3597
  Implies("--inspect-brk", "--inspect");
79
3597
  AddAlias("--inspect-brk=", { "--inspect-port", "--inspect-brk" });
80
81
3597
  AddOption("--inspect-brk-node", "", &DebugOptions::break_node_first_line);
82
3597
  Implies("--inspect-brk-node", "--inspect");
83
3597
  AddAlias("--inspect-brk-node=", { "--inspect-port", "--inspect-brk-node" });
84
85
3597
  AddOption("--debug-brk", "", &DebugOptions::break_first_line);
86
3597
  Implies("--debug-brk", "--debug");
87
3597
  AddAlias("--debug-brk=", { "--inspect-port", "--debug-brk" });
88
3597
}
89
90
#if HAVE_INSPECTOR
91
3597
const DebugOptionsParser DebugOptionsParser::instance;
92
#endif  // HAVE_INSPECTOR
93
94
3597
EnvironmentOptionsParser::EnvironmentOptionsParser() {
95
  AddOption("--experimental-modules",
96
            "experimental ES Module support and caching modules",
97
            &EnvironmentOptions::experimental_modules,
98
3597
            kAllowedInEnvironment);
99
  AddOption("--experimental-repl-await",
100
            "experimental await keyword support in REPL",
101
            &EnvironmentOptions::experimental_repl_await,
102
3597
            kAllowedInEnvironment);
103
  AddOption("--experimental-vm-modules",
104
            "experimental ES Module support in vm module",
105
            &EnvironmentOptions::experimental_vm_modules,
106
3597
            kAllowedInEnvironment);
107
  AddOption("--experimental-worker",
108
            "experimental threaded Worker support",
109
            &EnvironmentOptions::experimental_worker,
110
3597
            kAllowedInEnvironment);
111
3597
  AddOption("--expose-internals", "", &EnvironmentOptions::expose_internals);
112
  AddOption("--http-parser",
113
            "Select which HTTP parser to use; either 'legacy' or 'llhttp' "
114
            "(default: llhttp).",
115
            &EnvironmentOptions::http_parser,
116
3597
            kAllowedInEnvironment);
117
  AddOption("--loader",
118
            "(with --experimental-modules) use the specified file as a "
119
            "custom loader",
120
            &EnvironmentOptions::userland_loader,
121
3597
            kAllowedInEnvironment);
122
  AddOption("--no-deprecation",
123
            "silence deprecation warnings",
124
            &EnvironmentOptions::no_deprecation,
125
3597
            kAllowedInEnvironment);
126
  AddOption("--no-force-async-hooks-checks",
127
            "disable checks for async_hooks",
128
            &EnvironmentOptions::no_force_async_hooks_checks,
129
3597
            kAllowedInEnvironment);
130
  AddOption("--no-warnings",
131
            "silence all process warnings",
132
            &EnvironmentOptions::no_warnings,
133
3597
            kAllowedInEnvironment);
134
  AddOption("--pending-deprecation",
135
            "emit pending deprecation warnings",
136
            &EnvironmentOptions::pending_deprecation,
137
3597
            kAllowedInEnvironment);
138
  AddOption("--preserve-symlinks",
139
            "preserve symbolic links when resolving",
140
3597
            &EnvironmentOptions::preserve_symlinks);
141
  AddOption("--preserve-symlinks-main",
142
            "preserve symbolic links when resolving the main module",
143
3597
            &EnvironmentOptions::preserve_symlinks_main);
144
  AddOption("--prof-process",
145
            "process V8 profiler output generated using --prof",
146
3597
            &EnvironmentOptions::prof_process);
147
  // Options after --prof-process are passed through to the prof processor.
148
3597
  AddAlias("--prof-process", { "--prof-process", "--" });
149
  AddOption("--redirect-warnings",
150
            "write warnings to file instead of stderr",
151
            &EnvironmentOptions::redirect_warnings,
152
3597
            kAllowedInEnvironment);
153
  AddOption("--throw-deprecation",
154
            "throw an exception on deprecations",
155
            &EnvironmentOptions::throw_deprecation,
156
3597
            kAllowedInEnvironment);
157
  AddOption("--trace-deprecation",
158
            "show stack traces on deprecations",
159
            &EnvironmentOptions::trace_deprecation,
160
3597
            kAllowedInEnvironment);
161
  AddOption("--trace-sync-io",
162
            "show stack trace when use of sync IO is detected after the "
163
            "first tick",
164
            &EnvironmentOptions::trace_sync_io,
165
3597
            kAllowedInEnvironment);
166
  AddOption("--trace-warnings",
167
            "show stack traces on process warnings",
168
            &EnvironmentOptions::trace_warnings,
169
3597
            kAllowedInEnvironment);
170
171
  AddOption("--check",
172
            "syntax check script without executing",
173
3597
            &EnvironmentOptions::syntax_check_only);
174
3597
  AddAlias("-c", "--check");
175
  // This option is only so that we can tell --eval with an empty string from
176
  // no eval at all. Having it not start with a dash makes it inaccessible
177
  // from the parser itself, but available for using Implies().
178
  // TODO(addaleax): When moving --help over to something generated from the
179
  // programmatic descriptions, this will need some special care.
180
  // (See also [ssl_openssl_cert_store] below.)
181
3597
  AddOption("[has_eval_string]", "", &EnvironmentOptions::has_eval_string);
182
3597
  AddOption("--eval", "evaluate script", &EnvironmentOptions::eval_string);
183
3597
  Implies("--eval", "[has_eval_string]");
184
  AddOption("--print",
185
            "evaluate script and print result",
186
3597
            &EnvironmentOptions::print_eval);
187
3597
  AddAlias("-e", "--eval");
188
3597
  AddAlias("--print <arg>", "-pe");
189
3597
  AddAlias("-pe", { "--print", "--eval" });
190
3597
  AddAlias("-p", "--print");
191
  AddOption("--require",
192
            "module to preload (option can be repeated)",
193
            &EnvironmentOptions::preload_modules,
194
3597
            kAllowedInEnvironment);
195
3597
  AddAlias("-r", "--require");
196
  AddOption("--interactive",
197
            "always enter the REPL even if stdin does not appear "
198
            "to be a terminal",
199
3597
            &EnvironmentOptions::force_repl);
200
3597
  AddAlias("-i", "--interactive");
201
202
3597
  AddOption("--napi-modules", "", NoOp{}, kAllowedInEnvironment);
203
204
#if HAVE_OPENSSL
205
  AddOption("--tls-v1.0",
206
            "enable TLSv1.0 and greater by default",
207
            &EnvironmentOptions::tls_v1_0,
208
3597
            kAllowedInEnvironment);
209
  AddOption("--tls-v1.1",
210
            "enable TLSv1.1 and greater by default",
211
            &EnvironmentOptions::tls_v1_1,
212
3597
            kAllowedInEnvironment);
213
#endif
214
215
#if HAVE_INSPECTOR
216
  Insert(&DebugOptionsParser::instance,
217
3597
         &EnvironmentOptions::get_debug_options);
218
#endif  // HAVE_INSPECTOR
219
3597
}
220
221
3597
const EnvironmentOptionsParser EnvironmentOptionsParser::instance;
222
223
3597
PerIsolateOptionsParser::PerIsolateOptionsParser() {
224
  AddOption("--track-heap-objects",
225
            "track heap object allocations for heap snapshots",
226
            &PerIsolateOptions::track_heap_objects,
227
3597
            kAllowedInEnvironment);
228
229
  // Explicitly add some V8 flags to mark them as allowed in NODE_OPTIONS.
230
  AddOption("--abort-on-uncaught-exception",
231
            "aborting instead of exiting causes a core file to be generated "
232
            "for analysis",
233
            V8Option{},
234
3597
            kAllowedInEnvironment);
235
3597
  AddOption("--max-old-space-size", "", V8Option{}, kAllowedInEnvironment);
236
3597
  AddOption("--perf-basic-prof", "", V8Option{}, kAllowedInEnvironment);
237
3597
  AddOption("--perf-prof", "", V8Option{}, kAllowedInEnvironment);
238
3597
  AddOption("--stack-trace-limit", "", V8Option{}, kAllowedInEnvironment);
239
240
  Insert(&EnvironmentOptionsParser::instance,
241
3597
         &PerIsolateOptions::get_per_env_options);
242
3597
}
243
244
3597
const PerIsolateOptionsParser PerIsolateOptionsParser::instance;
245
246
3597
PerProcessOptionsParser::PerProcessOptionsParser() {
247
  AddOption("--title",
248
            "the process title to use on startup",
249
            &PerProcessOptions::title,
250
3597
            kAllowedInEnvironment);
251
  AddOption("--trace-event-categories",
252
            "comma separated list of trace event categories to record",
253
            &PerProcessOptions::trace_event_categories,
254
3597
            kAllowedInEnvironment);
255
  AddOption("--trace-event-file-pattern",
256
            "Template string specifying the filepath for the trace-events "
257
            "data, it supports ${rotation} and ${pid}.",
258
            &PerProcessOptions::trace_event_file_pattern,
259
3597
            kAllowedInEnvironment);
260
  AddAlias("--trace-events-enabled", {
261
3597
    "--trace-event-categories", "v8,node,node.async_hooks" });
262
  AddOption("--max-http-header-size",
263
            "set the maximum size of HTTP headers (default: 8KB)",
264
            &PerProcessOptions::max_http_header_size,
265
3597
            kAllowedInEnvironment);
266
  AddOption("--v8-pool-size",
267
            "set V8's thread pool size",
268
            &PerProcessOptions::v8_thread_pool_size,
269
3597
            kAllowedInEnvironment);
270
  AddOption("--zero-fill-buffers",
271
            "automatically zero-fill all newly allocated Buffer and "
272
            "SlowBuffer instances",
273
            &PerProcessOptions::zero_fill_all_buffers,
274
3597
            kAllowedInEnvironment);
275
276
3597
  AddOption("--security-reverts", "", &PerProcessOptions::security_reverts);
277
  AddOption("--completion-bash",
278
            "print source-able bash completion script",
279
3597
            &PerProcessOptions::print_bash_completion);
280
  AddOption("--help",
281
            "print node command line options",
282
3597
            &PerProcessOptions::print_help);
283
3597
  AddAlias("-h", "--help");
284
  AddOption(
285
3597
      "--version", "print Node.js version", &PerProcessOptions::print_version);
286
3597
  AddAlias("-v", "--version");
287
  AddOption("--v8-options",
288
            "print V8 command line options",
289
3597
            &PerProcessOptions::print_v8_help);
290
291
#ifdef NODE_HAVE_I18N_SUPPORT
292
  AddOption("--icu-data-dir",
293
            "set ICU data load path to dir (overrides NODE_ICU_DATA)"
294
#ifndef NODE_HAVE_SMALL_ICU
295
            " (note: linked-in ICU data is present)\n"
296
#endif
297
            ,
298
            &PerProcessOptions::icu_data_dir,
299
3597
            kAllowedInEnvironment);
300
#endif
301
302
#if HAVE_OPENSSL
303
  AddOption("--openssl-config",
304
            "load OpenSSL configuration from the specified file "
305
            "(overrides OPENSSL_CONF)",
306
            &PerProcessOptions::openssl_config,
307
3597
            kAllowedInEnvironment);
308
  AddOption("--tls-cipher-list",
309
            "use an alternative default TLS cipher list",
310
            &PerProcessOptions::tls_cipher_list,
311
3597
            kAllowedInEnvironment);
312
  AddOption("--use-openssl-ca",
313
            "use OpenSSL's default CA store"
314
#if defined(NODE_OPENSSL_CERT_STORE)
315
            " (default)"
316
#endif
317
            ,
318
            &PerProcessOptions::use_openssl_ca,
319
3597
            kAllowedInEnvironment);
320
  AddOption("--use-bundled-ca",
321
            "use bundled CA store"
322
#if !defined(NODE_OPENSSL_CERT_STORE)
323
            " (default)"
324
#endif
325
            ,
326
            &PerProcessOptions::use_bundled_ca,
327
3597
            kAllowedInEnvironment);
328
  // Similar to [has_eval_string] above, except that the separation between
329
  // this and use_openssl_ca only exists for option validation after parsing.
330
  // This is not ideal.
331
  AddOption("[ssl_openssl_cert_store]",
332
            "",
333
3597
            &PerProcessOptions::ssl_openssl_cert_store);
334
3597
  Implies("--use-openssl-ca", "[ssl_openssl_cert_store]");
335
3597
  ImpliesNot("--use-bundled-ca", "[ssl_openssl_cert_store]");
336
#if NODE_FIPS_MODE
337
  AddOption("--enable-fips",
338
            "enable FIPS crypto at startup",
339
            &PerProcessOptions::enable_fips_crypto,
340
            kAllowedInEnvironment);
341
  AddOption("--force-fips",
342
            "force FIPS crypto (cannot be disabled)",
343
            &PerProcessOptions::force_fips_crypto,
344
            kAllowedInEnvironment);
345
#endif
346
#endif
347
348
  Insert(&PerIsolateOptionsParser::instance,
349
3597
         &PerProcessOptions::get_per_isolate_options);
350
3597
}
351
352
3597
const PerProcessOptionsParser PerProcessOptionsParser::instance;
353
354
106
inline std::string RemoveBrackets(const std::string& host) {
355


106
  if (!host.empty() && host.front() == '[' && host.back() == ']')
356
4
    return host.substr(1, host.size() - 2);
357
  else
358
102
    return host;
359
}
360
361
94
inline int ParseAndValidatePort(const std::string& port,
362
                                std::vector<std::string>* errors) {
363
  char* endptr;
364
94
  errno = 0;
365
94
  const long result = strtol(port.c_str(), &endptr, 10);  // NOLINT(runtime/int)
366

94
  if (errno != 0 || *endptr != '\0'||
367

63
      (result != 0 && result < 1024) || result > 65535) {
368
    errors->push_back(" must be 0 or in range 1024 to 65535.");
369
  }
370
94
  return static_cast<int>(result);
371
}
372
373
94
HostPort SplitHostPort(const std::string& arg,
374
                      std::vector<std::string>* errors) {
375
  // remove_brackets only works if no port is specified
376
  // so if it has an effect only an IPv6 address was specified.
377
94
  std::string host = RemoveBrackets(arg);
378
94
  if (host.length() < arg.length())
379
    return HostPort{host, DebugOptions::kDefaultInspectorPort};
380
381
94
  size_t colon = arg.rfind(':');
382
94
  if (colon == std::string::npos) {
383
    // Either a port number or a host name.  Assume that
384
    // if it's not all decimal digits, it's a host name.
385
378
    for (char c : arg) {
386

296
      if (c < '0' || c > '9') {
387
        return HostPort{arg, DebugOptions::kDefaultInspectorPort};
388
      }
389
    }
390
82
    return HostPort { "", ParseAndValidatePort(arg, errors) };
391
  }
392
  // Host and port found:
393
  return HostPort { RemoveBrackets(arg.substr(0, colon)),
394
12
                    ParseAndValidatePort(arg.substr(colon + 1), errors) };
395
}
396
397
// Return a map containing all the options and their metadata as well
398
// as the aliases
399
3671
void GetOptions(const FunctionCallbackInfo<Value>& args) {
400
3671
  Mutex::ScopedLock lock(per_process_opts_mutex);
401
3671
  Environment* env = Environment::GetCurrent(args);
402
3671
  Isolate* isolate = env->isolate();
403
3671
  Local<Context> context = env->context();
404
405
  // Temporarily act as if the current Environment's/IsolateData's options were
406
  // the default options, i.e. like they are the ones we'd access for global
407
  // options parsing, so that all options are available from the main parser.
408
7342
  auto original_per_isolate = per_process_opts->per_isolate;
409
3671
  per_process_opts->per_isolate = env->isolate_data()->options();
410
7342
  auto original_per_env = per_process_opts->per_isolate->per_env;
411
3671
  per_process_opts->per_isolate->per_env = env->options();
412
3671
  OnScopeLeave on_scope_leave([&]() {
413
3671
    per_process_opts->per_isolate->per_env = original_per_env;
414
3671
    per_process_opts->per_isolate = original_per_isolate;
415
11013
  });
416
417
3671
  const auto& parser = PerProcessOptionsParser::instance;
418
419
3671
  Local<Map> options = Map::New(isolate);
420
212918
  for (const auto& item : parser.options_) {
421
    Local<Value> value;
422
209247
    const auto& option_info = item.second;
423
209247
    auto field = option_info.field;
424
209247
    PerProcessOptions* opts = per_process_opts.get();
425


209247
    switch (option_info.type) {
426
      case kNoOp:
427
      case kV8Option:
428
22026
        value = Undefined(isolate);
429
22026
        break;
430
      case kBoolean:
431
264312
        value = Boolean::New(isolate, *parser.Lookup<bool>(field, opts));
432
132156
        break;
433
      case kInteger:
434
7342
        value = Number::New(isolate, *parser.Lookup<int64_t>(field, opts));
435
3671
        break;
436
      case kUInteger:
437
7342
        value = Number::New(isolate, *parser.Lookup<uint64_t>(field, opts));
438
3671
        break;
439
      case kString:
440
110130
        if (!ToV8Value(context, *parser.Lookup<std::string>(field, opts))
441
110130
                 .ToLocal(&value)) {
442
          return;
443
        }
444
36710
        break;
445
      case kStringList:
446
14684
        if (!ToV8Value(context,
447
14684
                       *parser.Lookup<std::vector<std::string>>(field, opts))
448
22026
                 .ToLocal(&value)) {
449
          return;
450
        }
451
7342
        break;
452
      case kHostPort: {
453
3671
        const HostPort& host_port = *parser.Lookup<HostPort>(field, opts);
454
3671
        Local<Object> obj = Object::New(isolate);
455
        Local<Value> host;
456

22026
        if (!ToV8Value(context, host_port.host()).ToLocal(&host) ||
457


33039
            obj->Set(context, env->host_string(), host).IsNothing() ||
458
            obj->Set(context,
459
                     env->port_string(),
460

14684
                     Integer::New(isolate, host_port.port()))
461
18355
                .IsNothing()) {
462
          return;
463
        }
464
3671
        value = obj;
465
3671
        break;
466
      }
467
      default:
468
        UNREACHABLE();
469
    }
470
209247
    CHECK(!value.IsEmpty());
471
472
418494
    Local<Value> name = ToV8Value(context, item.first).ToLocalChecked();
473
209247
    Local<Object> info = Object::New(isolate);
474
    Local<Value> help_text;
475

1464729
    if (!ToV8Value(context, option_info.help_text).ToLocal(&help_text) ||
476
627741
        !info->Set(context, env->help_text_string(), help_text)
477

1883223
             .FromMaybe(false) ||
478
        !info->Set(context,
479
                   env->env_var_settings_string(),
480
                   Integer::New(isolate,
481

836988
                                static_cast<int>(option_info.env_setting)))
482

2092470
             .FromMaybe(false) ||
483
        !info->Set(context,
484
                   env->type_string(),
485

836988
                   Integer::New(isolate, static_cast<int>(option_info.type)))
486

1883223
             .FromMaybe(false) ||
487


2092470
        info->Set(context, env->value_string(), value).IsNothing() ||
488

836988
        options->Set(context, name, info).IsEmpty()) {
489
      return;
490
    }
491
209247
  }
492
493
  Local<Value> aliases;
494
7342
  if (!ToV8Value(context, parser.aliases_).ToLocal(&aliases)) return;
495
496
3671
  Local<Object> ret = Object::New(isolate);
497



29368
  if (ret->Set(context, env->options_string(), options).IsNothing() ||
498

18355
      ret->Set(context, env->aliases_string(), aliases).IsNothing()) {
499
    return;
500
  }
501
502
11013
  args.GetReturnValue().Set(ret);
503
}
504
505
3670
void Initialize(Local<Object> target,
506
                Local<Value> unused,
507
                Local<Context> context,
508
                void* priv) {
509
3670
  Environment* env = Environment::GetCurrent(context);
510
3671
  Isolate* isolate = env->isolate();
511
3671
  env->SetMethodNoSideEffect(target, "getOptions", GetOptions);
512
513
3670
  Local<Object> env_settings = Object::New(isolate);
514
14684
  NODE_DEFINE_CONSTANT(env_settings, kAllowedInEnvironment);
515
14683
  NODE_DEFINE_CONSTANT(env_settings, kDisallowedInEnvironment);
516
  target
517
      ->Set(
518
11012
          context, FIXED_ONE_BYTE_STRING(isolate, "envSettings"), env_settings)
519
7342
      .FromJust();
520
521
3671
  Local<Object> types = Object::New(isolate);
522
14682
  NODE_DEFINE_CONSTANT(types, kNoOp);
523
14683
  NODE_DEFINE_CONSTANT(types, kV8Option);
524
14684
  NODE_DEFINE_CONSTANT(types, kBoolean);
525
14684
  NODE_DEFINE_CONSTANT(types, kInteger);
526
14684
  NODE_DEFINE_CONSTANT(types, kUInteger);
527
14684
  NODE_DEFINE_CONSTANT(types, kString);
528
14684
  NODE_DEFINE_CONSTANT(types, kHostPort);
529
14684
  NODE_DEFINE_CONSTANT(types, kStringList);
530
11013
  target->Set(context, FIXED_ONE_BYTE_STRING(isolate, "types"), types)
531
7342
      .FromJust();
532
3671
}
533
534
}  // namespace options_parser
535
}  // namespace node
536
537

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