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: 36 45 80.0 %
Date: 2021-02-19 04:08:54 Branches: 3 10 30.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
707
class HostPort {
17
 public:
18
122
  HostPort(const std::string& host_name, int port)
19
122
      : host_name_(host_name), port_(port) {}
20
456
  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
  void set_port(int port) { port_ = port; }
28
29
120
  const std::string& host() const { return host_name_; }
30
31
120
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
120
    CHECK_GE(port_, 0);
34
120
    return port_;
35
  }
36
37
  void Update(const HostPort& other) {
38
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
39
    if (other.port_ >= 0) port_ = other.port_;
40
  }
41
42
 private:
43
  std::string host_name_;
44
  int port_;
45
};
46
47
1241
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
1072
  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
559
class DebugOptions : public Options {
63
 public:
64
122
  DebugOptions() = default;
65
304
  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
  void EnableBreakFirstLine() {
89
    inspector_enabled = true;
90
    break_first_line = true;
91
  }
92
93
145
  bool wait_for_connect() const {
94

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