GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 37 94.6 %
Date: 2022-08-28 04:20:35 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
6140
class HostPort {
21
 public:
22
5842
  HostPort(const std::string& host_name, int port)
23
5842
      : host_name_(host_name), port_(port) {}
24
18438
  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
6799
  const std::string& host() const { return host_name_; }
34
35
6998
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
6998
    CHECK_GE(port_, 0);
38
6998
    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
87918
  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
6140
class DebugOptions : public Options {
67
 public:
68
5742
  DebugOptions() = default;
69
12292
  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
6140
  bool wait_for_connect() const {
98

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