GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 37 94.6 %
Date: 2021-10-26 04:13:54 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
#if HAVE_OPENSSL
15
#include "openssl/opensslv.h"
16
#endif
17
18
namespace node {
19
20
5455
class HostPort {
21
 public:
22
5282
  HostPort(const std::string& host_name, int port)
23
5282
      : host_name_(host_name), port_(port) {}
24
16383
  HostPort(const HostPort&) = default;
25
  HostPort& operator=(const HostPort&) = default;
26
  HostPort(HostPort&&) = default;
27
  HostPort& operator=(HostPort&&) = default;
28
29
  void set_host(const std::string& host) { host_name_ = host; }
30
31
96
  void set_port(int port) { port_ = port; }
32
33
5527
  const std::string& host() const { return host_name_; }
34
35
5654
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
5654
    CHECK_GE(port_, 0);
38
5654
    return port_;
39
  }
40
41
100
  void Update(const HostPort& other) {
42
100
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
43
100
    if (other.port_ >= 0) port_ = other.port_;
44
100
  }
45
46
 private:
47
  std::string host_name_;
48
  int port_;
49
};
50
51
class Options {
52
 public:
53
  virtual void CheckOptions(std::vector<std::string>* errors) {}
54
78844
  virtual ~Options() = default;
55
};
56
57
struct InspectPublishUid {
58
  bool console;
59
  bool http;
60
};
61
62
// These options are currently essentially per-Environment, but it can be nice
63
// to keep them separate since they are a group of options applying to a very
64
// specific part of Node. It might also make more sense for them to be
65
// per-Isolate, rather than per-Environment.
66
5455
class DebugOptions : public Options {
67
 public:
68
5182
  DebugOptions() = default;
69
10922
  DebugOptions(const DebugOptions&) = default;
70
  DebugOptions& operator=(const DebugOptions&) = default;
71
  DebugOptions(DebugOptions&&) = default;
72
  DebugOptions& operator=(DebugOptions&&) = default;
73
74
  // --inspect
75
  bool inspector_enabled = false;
76
  // --debug
77
  bool deprecated_debug = false;
78
  // --inspect-brk
79
  bool break_first_line = false;
80
  // --inspect-brk-node
81
  bool break_node_first_line = false;
82
  // --inspect-publish-uid
83
  std::string inspect_publish_uid_string = "stderr,http";
84
85
  InspectPublishUid inspect_publish_uid;
86
87
  enum { kDefaultInspectorPort = 9229 };
88
89
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
90
91
  // Used to patch the options as if --inspect-brk is passed.
92
38
  void EnableBreakFirstLine() {
93
38
    inspector_enabled = true;
94
38
    break_first_line = true;
95
38
  }
96
97
5455
  bool wait_for_connect() const {
98

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