GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 37 94.6 %
Date: 2022-05-18 04:18:05 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
6025
class HostPort {
21
 public:
22
5627
  HostPort(const std::string& host_name, int port)
23
5627
      : host_name_(host_name), port_(port) {}
24
18093
  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
6627
  const std::string& host() const { return host_name_; }
34
35
6774
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
6774
    CHECK_GE(port_, 0);
38
6774
    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
85838
  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
6025
class DebugOptions : public Options {
67
 public:
68
5527
  DebugOptions() = default;
69
12062
  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
6025
  bool wait_for_connect() const {
98

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