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-08-07 22:13:19 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
21749
class HostPort {
17
 public:
18
4584
  HostPort(const std::string& host_name, int port)
19
4584
      : host_name_(host_name), port_(port) {}
20
14277
  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
4815
  const std::string& host() const { return host_name_; }
30
31
4916
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
4916
    CHECK_GE(port_, 0);
34
4916
    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
41700
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
34146
  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
17358
class DebugOptions : public Options {
63
 public:
64
4484
  DebugOptions() = default;
65
9518
  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
4758
  bool wait_for_connect() const {
94

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