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.h Lines: 45 47 95.7 %
Date: 2020-09-03 22:13:26 Branches: 8 10 80.0 %

Line Branch Exec Source
1
#ifndef SRC_NODE_OPTIONS_H_
2
#define SRC_NODE_OPTIONS_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include <memory>
7
#include <string>
8
#include <unordered_map>
9
#include <vector>
10
#include "node_constants.h"
11
#include "node_mutex.h"
12
#include "util.h"
13
14
namespace node {
15
16
22225
class HostPort {
17
 public:
18
4671
  HostPort(const std::string& host_name, int port)
19
4671
      : host_name_(host_name), port_(port) {}
20
14559
  HostPort(const HostPort&) = default;
21
  HostPort& operator=(const HostPort&) = default;
22
  HostPort(HostPort&&) = default;
23
  HostPort& operator=(HostPort&&) = default;
24
25
  void set_host(const std::string& host) { host_name_ = host; }
26
27
77
  void set_port(int port) { port_ = port; }
28
29
4911
  const std::string& host() const { return host_name_; }
30
31
5012
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
5012
    CHECK_GE(port_, 0);
34
5012
    return port_;
35
  }
36
37
100
  void Update(const HostPort& other) {
38
100
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
39
100
    if (other.port_ >= 0) port_ = other.port_;
40
100
  }
41
42
 private:
43
  std::string host_name_;
44
  int port_;
45
};
46
47
42463
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
34832
  virtual ~Options() = default;
51
};
52
53
struct InspectPublishUid {
54
  bool console;
55
  bool http;
56
};
57
58
// These options are currently essentially per-Environment, but it can be nice
59
// to keep them separate since they are a group of options applying to a very
60
// specific part of Node. It might also make more sense for them to be
61
// per-Isolate, rather than per-Environment.
62
17737
class DebugOptions : public Options {
63
 public:
64
4571
  DebugOptions() = default;
65
9706
  DebugOptions(const DebugOptions&) = default;
66
  DebugOptions& operator=(const DebugOptions&) = default;
67
  DebugOptions(DebugOptions&&) = default;
68
  DebugOptions& operator=(DebugOptions&&) = default;
69
70
  // --inspect
71
  bool inspector_enabled = false;
72
  // --debug
73
  bool deprecated_debug = false;
74
  // --inspect-brk
75
  bool break_first_line = false;
76
  // --inspect-brk-node
77
  bool break_node_first_line = false;
78
  // --inspect-publish-uid
79
  std::string inspect_publish_uid_string = "stderr,http";
80
81
  InspectPublishUid inspect_publish_uid;
82
83
  enum { kDefaultInspectorPort = 9229 };
84
85
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
86
87
  // Used to patch the options as if --inspect-brk is passed.
88
38
  void EnableBreakFirstLine() {
89
38
    inspector_enabled = true;
90
38
    break_first_line = true;
91
38
  }
92
93
4852
  bool wait_for_connect() const {
94

4852
    return break_first_line || break_node_first_line;
95
  }
96
97
  void CheckOptions(std::vector<std::string>* errors) override;
98
};
99
100
26418
class EnvironmentOptions : public Options {
101
 public:
102
  bool abort_on_uncaught_exception = false;
103
  std::vector<std::string> conditions;
104
  bool enable_source_maps = false;
105
  bool experimental_json_modules = false;
106
  bool experimental_modules = false;
107
  std::string experimental_specifier_resolution;
108
  std::string es_module_specifier_resolution;
109
  bool experimental_wasm_modules = false;
110
  bool experimental_import_meta_resolve = false;
111
  std::string module_type;
112
  std::string experimental_policy;
113
  std::string experimental_policy_integrity;
114
  bool has_policy_integrity_string;
115
  bool experimental_repl_await = false;
116
  bool experimental_vm_modules = false;
117
  bool expose_internals = false;
118
  bool frozen_intrinsics = false;
119
  std::string heap_snapshot_signal;
120
  uint64_t max_http_header_size = 16 * 1024;
121
  bool no_deprecation = false;
122
  bool no_force_async_hooks_checks = false;
123
  bool no_warnings = false;
124
  bool force_context_aware = false;
125
  bool pending_deprecation = false;
126
  bool preserve_symlinks = false;
127
  bool preserve_symlinks_main = false;
128
  bool prof_process = false;
129
#if HAVE_INSPECTOR
130
  std::string cpu_prof_dir;
131
  static const uint64_t kDefaultCpuProfInterval = 1000;
132
  uint64_t cpu_prof_interval = kDefaultCpuProfInterval;
133
  std::string cpu_prof_name;
134
  bool cpu_prof = false;
135
  std::string heap_prof_dir;
136
  std::string heap_prof_name;
137
  static const uint64_t kDefaultHeapProfInterval = 512 * 1024;
138
  uint64_t heap_prof_interval = kDefaultHeapProfInterval;
139
  bool heap_prof = false;
140
#endif  // HAVE_INSPECTOR
141
  std::string redirect_warnings;
142
  std::string diagnostic_dir;
143
  bool test_udp_no_try_send = false;
144
  bool throw_deprecation = false;
145
  bool trace_atomics_wait = false;
146
  bool trace_deprecation = false;
147
  bool trace_exit = false;
148
  bool trace_sync_io = false;
149
  bool trace_tls = false;
150
  bool trace_uncaught = false;
151
  bool trace_warnings = false;
152
  std::string unhandled_rejections;
153
  std::string userland_loader;
154
155
  bool syntax_check_only = false;
156
  bool has_eval_string = false;
157
  bool experimental_wasi = false;
158
  std::string eval_string;
159
  bool print_eval = false;
160
  bool force_repl = false;
161
162
  bool insecure_http_parser = false;
163
164
  bool tls_min_v1_0 = false;
165
  bool tls_min_v1_1 = false;
166
  bool tls_min_v1_2 = false;
167
  bool tls_min_v1_3 = false;
168
  bool tls_max_v1_2 = false;
169
  bool tls_max_v1_3 = false;
170
  std::string tls_keylog;
171
172
  std::vector<std::string> preload_modules;
173
174
  std::vector<std::string> user_argv;
175
176
34017
  inline DebugOptions* get_debug_options() { return &debug_options_; }
177
19410
  inline const DebugOptions& debug_options() const { return debug_options_; }
178
179
  void CheckOptions(std::vector<std::string>* errors) override;
180
181
 private:
182
  DebugOptions debug_options_;
183
};
184
185
31960
class PerIsolateOptions : public Options {
186
 public:
187
4571
  std::shared_ptr<EnvironmentOptions> per_env { new EnvironmentOptions() };
188
  bool track_heap_objects = false;
189
  bool no_node_snapshot = false;
190
  bool report_uncaught_exception = false;
191
  bool report_on_signal = false;
192
  bool experimental_top_level_await = true;
193
  std::string report_signal = "SIGUSR2";
194
  inline EnvironmentOptions* get_per_env_options();
195
  void CheckOptions(std::vector<std::string>* errors) override;
196
};
197
198
17888
class PerProcessOptions : public Options {
199
 public:
200
  // Options shouldn't be here unless they affect the entire process scope, and
201
  // that should avoided when possible.
202
  //
203
  // When an option is used during process initialization, it does not need
204
  // protection, but any use after that will likely require synchronization
205
  // using the node::per_process::cli_options_mutex, typically:
206
  //
207
  //     Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
208
4472
  std::shared_ptr<PerIsolateOptions> per_isolate { new PerIsolateOptions() };
209
210
  std::string title;
211
  std::string trace_event_categories;
212
  std::string trace_event_file_pattern = "node_trace.${rotation}.log";
213
  int64_t v8_thread_pool_size = 4;
214
  bool zero_fill_all_buffers = false;
215
  bool debug_arraybuffer_allocations = false;
216
  std::string disable_proto;
217
218
  std::vector<std::string> security_reverts;
219
  bool print_bash_completion = false;
220
  bool print_help = false;
221
  bool print_v8_help = false;
222
  bool print_version = false;
223
224
#ifdef NODE_HAVE_I18N_SUPPORT
225
  std::string icu_data_dir;
226
#endif
227
228
  // Per-process because they affect singleton OpenSSL shared library state,
229
  // or are used once during process intialization.
230
#if HAVE_OPENSSL
231
  std::string openssl_config;
232
  std::string tls_cipher_list = DEFAULT_CIPHER_LIST_CORE;
233
#ifdef NODE_OPENSSL_CERT_STORE
234
  bool ssl_openssl_cert_store = true;
235
#else
236
  bool ssl_openssl_cert_store = false;
237
#endif
238
  bool use_openssl_ca = false;
239
  bool use_bundled_ca = false;
240
#if NODE_FIPS_MODE
241
  bool enable_fips_crypto = false;
242
  bool force_fips_crypto = false;
243
#endif
244
#endif
245
246
  // Per-process because reports can be triggered outside a known V8 context.
247
  bool report_on_fatalerror = false;
248
  bool report_compact = false;
249
  std::string report_directory;
250
  std::string report_filename;
251
252
  // TODO(addaleax): Some of these could probably be per-Environment.
253
  std::string use_largepages = "off";
254
  bool trace_sigint = false;
255
  std::vector<std::string> cmdline;
256
257
  inline PerIsolateOptions* get_per_isolate_options();
258
  void CheckOptions(std::vector<std::string>* errors) override;
259
};
260
261
// The actual options parser, as opposed to the structs containing them:
262
263
namespace options_parser {
264
265
HostPort SplitHostPort(const std::string& arg,
266
    std::vector<std::string>* errors);
267
void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
268
std::string GetBashCompletion();
269
270
enum OptionType {
271
  kNoOp,
272
  kV8Option,
273
  kBoolean,
274
  kInteger,
275
  kUInteger,
276
  kString,
277
  kHostPort,
278
  kStringList,
279
};
280
281
template <typename Options>
282
17888
class OptionsParser {
283
 public:
284
17888
  virtual ~OptionsParser() = default;
285
286
  typedef Options TargetType;
287
288
  struct NoOp {};
289
  struct V8Option {};
290
291
  // These methods add a single option to the parser. Optionally, it can be
292
  // specified whether the option should be allowed from environment variable
293
  // sources (i.e. NODE_OPTIONS).
294
  void AddOption(const char* name,
295
                 const char* help_text,
296
                 bool Options::* field,
297
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
298
  void AddOption(const char* name,
299
                 const char* help_text,
300
                 uint64_t Options::* field,
301
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
302
  void AddOption(const char* name,
303
                 const char* help_text,
304
                 int64_t Options::* field,
305
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
306
  void AddOption(const char* name,
307
                 const char* help_text,
308
                 std::string Options::* field,
309
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
310
  void AddOption(const char* name,
311
                 const char* help_text,
312
                 std::vector<std::string> Options::* field,
313
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
314
  void AddOption(const char* name,
315
                 const char* help_text,
316
                 HostPort Options::* field,
317
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
318
  void AddOption(const char* name,
319
                 const char* help_text,
320
                 NoOp no_op_tag,
321
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
322
  void AddOption(const char* name,
323
                 const char* help_text,
324
                 V8Option v8_option_tag,
325
                 OptionEnvvarSettings env_setting = kDisallowedInEnvironment);
326
327
  // Adds aliases. An alias can be of the form "--option-a" -> "--option-b",
328
  // or have a more complex group expansion, like
329
  //   "--option-a" -> { "--option-b", "--harmony-foobar", "--eval", "42" }
330
  // If `from` has the form "--option-a=", the alias will only be expanded if
331
  // the option is presented in that form (i.e. with a '=').
332
  // If `from` has the form "--option-a <arg>", the alias will only be expanded
333
  // if the option has a non-option argument (not starting with -) following it.
334
  void AddAlias(const char* from, const char* to);
335
  void AddAlias(const char* from, const std::vector<std::string>& to);
336
  void AddAlias(const char* from,
337
                const std::initializer_list<std::string>& to);
338
339
  // Add implications from some arbitrary option to a boolean one, either
340
  // in a way that makes `from` set `to` to true or to false.
341
  void Implies(const char* from, const char* to);
342
  void ImpliesNot(const char* from, const char* to);
343
344
  // Insert options from another options parser into this one, along with
345
  // a method that yields the target options type from this parser's options
346
  // type.
347
  template <typename ChildOptions>
348
  void Insert(const OptionsParser<ChildOptions>& child_options_parser,
349
              ChildOptions* (Options::* get_child)());
350
351
  // Parse a sequence of options into an options struct, a list of
352
  // arguments that were parsed as options, a list of unknown/JS engine options,
353
  // and leave the remainder in the input `args` vector.
354
  //
355
  // For example, an `args` input of
356
  //
357
  //   node --foo --harmony-bar --fizzle=42 -- /path/to/cow moo
358
  //
359
  // expands as
360
  //
361
  // - `args` -> { "node", "/path/to/cow", "moo" }
362
  // - `exec_args` -> { "--foo", "--harmony-bar", "--fizzle=42" }
363
  // - `v8_args` -> `{ "node", "--harmony-bar" }
364
  // - `options->foo == true`, `options->fizzle == 42`.
365
  //
366
  // If `*error` is set, the result of the parsing should be discarded and the
367
  // contents of any of the argument vectors should be considered undefined.
368
  void Parse(std::vector<std::string>* const args,
369
             std::vector<std::string>* const exec_args,
370
             std::vector<std::string>* const v8_args,
371
             Options* const options,
372
             OptionEnvvarSettings required_env_settings,
373
             std::vector<std::string>* const errors) const;
374
375
 private:
376
  // We support the wide variety of different option types by remembering
377
  // how to access them, given a certain `Options` struct.
378
379
  // Represents a field within `Options`.
380
1278992
  class BaseOptionField {
381
   public:
382
1278992
    virtual ~BaseOptionField() = default;
383
    virtual void* LookupImpl(Options* options) const = 0;
384
385
    template <typename T>
386
480001
    inline T* Lookup(Options* options) const {
387
480001
      return static_cast<T*>(LookupImpl(options));
388
    }
389
  };
390
391
  // Represents a field of type T within `Options` that can be looked up
392
  // as a C++ member field.
393
  template <typename T>
394
442728
  class SimpleOptionField : public BaseOptionField {
395
   public:
396
442728
    explicit SimpleOptionField(T Options::* field) : field_(field) {}
397
480001
    void* LookupImpl(Options* options) const override {
398
480001
      return static_cast<void*>(&(options->*field_));
399
    }
400
401
   private:
402
    T Options::* field_;
403
  };
404
405
  template <typename T>
406
479643
  inline T* Lookup(std::shared_ptr<BaseOptionField> field,
407
                   Options* options) const {
408
479643
    return field->template Lookup<T>(options);
409
  }
410
411
  // An option consists of:
412
  // - A type.
413
  // - A way to store/access the property value.
414
  // - The information of whether it may occur in an env var or not.
415
5424536
  struct OptionInfo {
416
    OptionType type;
417
    std::shared_ptr<BaseOptionField> field;
418
    OptionEnvvarSettings env_setting;
419
    std::string help_text;
420
  };
421
422
  // An implied option is composed of the information on where to store a
423
  // specific boolean value (if another specific option is encountered).
424
447200
  struct Implication {
425
    OptionType type;
426
    std::string name;
427
    std::shared_ptr<BaseOptionField> target_field;
428
    bool target_value;
429
  };
430
431
  // These are helpers that make `Insert()` support properties of other
432
  // options structs, if we know how to access them.
433
  template <typename OriginalField, typename ChildOptions>
434
  static auto Convert(
435
      std::shared_ptr<OriginalField> original,
436
      ChildOptions* (Options::* get_child)());
437
  template <typename ChildOptions>
438
  static auto Convert(
439
      typename OptionsParser<ChildOptions>::OptionInfo original,
440
      ChildOptions* (Options::* get_child)());
441
  template <typename ChildOptions>
442
  static auto Convert(
443
      typename OptionsParser<ChildOptions>::Implication original,
444
      ChildOptions* (Options::* get_child)());
445
446
  std::unordered_map<std::string, OptionInfo> options_;
447
  std::unordered_map<std::string, std::vector<std::string>> aliases_;
448
  std::unordered_multimap<std::string, Implication> implications_;
449
450
  template <typename OtherOptions>
451
  friend class OptionsParser;
452
453
  friend void GetOptions(const v8::FunctionCallbackInfo<v8::Value>& args);
454
  friend std::string GetBashCompletion();
455
};
456
457
using StringVector = std::vector<std::string>;
458
template <class OptionsType, class = Options>
459
void Parse(
460
  StringVector* const args, StringVector* const exec_args,
461
  StringVector* const v8_args, OptionsType* const options,
462
  OptionEnvvarSettings required_env_settings, StringVector* const errors);
463
464
}  // namespace options_parser
465
466
namespace per_process {
467
468
extern Mutex cli_options_mutex;
469
extern std::shared_ptr<PerProcessOptions> cli_options;
470
471
}  // namespace per_process
472
473
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options);
474
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
475
                      std::function<std::string(const char*)> opt_getter);
476
477
std::vector<std::string> ParseNodeOptionsEnvVar(
478
    const std::string& node_options, std::vector<std::string>* errors);
479
}  // namespace node
480
481
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
482
483
#endif  // SRC_NODE_OPTIONS_H_