GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 38 92.1 %
Date: 2022-12-31 04:22:30 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
6531
class HostPort {
21
 public:
22
6243
  HostPort(const std::string& host_name, int port)
23
6243
      : host_name_(host_name), port_(port) {}
24
19605
  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
6713
  const std::string& host() const { return host_name_; }
34
35
6954
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
6954
    CHECK_GE(port_, 0);
38
6954
    return port_;
39
  }
40
41
148
  void Update(const HostPort& other) {
42
148
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
43
148
    if (other.port_ >= 0) port_ = other.port_;
44
148
  }
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
                            std::vector<std::string>* argv) {}
55
93070
  virtual ~Options() = default;
56
};
57
58
struct InspectPublishUid {
59
  bool console;
60
  bool http;
61
};
62
63
// These options are currently essentially per-Environment, but it can be nice
64
// to keep them separate since they are a group of options applying to a very
65
// specific part of Node. It might also make more sense for them to be
66
// per-Isolate, rather than per-Environment.
67
6531
class DebugOptions : public Options {
68
 public:
69
6095
  DebugOptions() = default;
70
13070
  DebugOptions(const DebugOptions&) = default;
71
  DebugOptions& operator=(const DebugOptions&) = default;
72
  DebugOptions(DebugOptions&&) = default;
73
  DebugOptions& operator=(DebugOptions&&) = default;
74
75
  bool allow_attaching_debugger = true;
76
  // --inspect
77
  bool inspector_enabled = false;
78
  // --debug
79
  bool deprecated_debug = false;
80
  // --inspect-brk
81
  bool break_first_line = false;
82
  // --inspect-brk-node
83
  bool break_node_first_line = false;
84
  // --inspect-publish-uid
85
  std::string inspect_publish_uid_string = "stderr,http";
86
87
  InspectPublishUid inspect_publish_uid;
88
89
  enum { kDefaultInspectorPort = 9229 };
90
91
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
92
93
  // Used to patch the options as if --inspect-brk is passed.
94
40
  void EnableBreakFirstLine() {
95
40
    inspector_enabled = true;
96
40
    break_first_line = true;
97
40
  }
98
99
6531
  bool wait_for_connect() const {
100

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