GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 37 94.6 %
Date: 2022-09-18 04:22:26 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
6295
class HostPort {
21
 public:
22
6036
  HostPort(const std::string& host_name, int port)
23
6036
      : host_name_(host_name), port_(port) {}
24
18903
  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
132
  void set_port(int port) { port_ = port; }
32
33
7045
  const std::string& host() const { return host_name_; }
34
35
7272
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
7272
    CHECK_GE(port_, 0);
38
7272
    return port_;
39
  }
40
41
145
  void Update(const HostPort& other) {
42
145
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
43
145
    if (other.port_ >= 0) port_ = other.port_;
44
145
  }
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
89004
  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
6295
class DebugOptions : public Options {
67
 public:
68
5891
  DebugOptions() = default;
69
12602
  DebugOptions(const DebugOptions&) = default;
70
  DebugOptions& operator=(const DebugOptions&) = default;
71
  DebugOptions(DebugOptions&&) = default;
72
  DebugOptions& operator=(DebugOptions&&) = default;
73
74
  bool allow_attaching_debugger = true;
75
  // --inspect
76
  bool inspector_enabled = false;
77
  // --debug
78
  bool deprecated_debug = false;
79
  // --inspect-brk
80
  bool break_first_line = false;
81
  // --inspect-brk-node
82
  bool break_node_first_line = false;
83
  // --inspect-publish-uid
84
  std::string inspect_publish_uid_string = "stderr,http";
85
86
  InspectPublishUid inspect_publish_uid;
87
88
  enum { kDefaultInspectorPort = 9229 };
89
90
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
91
92
  // Used to patch the options as if --inspect-brk is passed.
93
40
  void EnableBreakFirstLine() {
94
40
    inspector_enabled = true;
95
40
    break_first_line = true;
96
40
  }
97
98
6295
  bool wait_for_connect() const {
99

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