GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 38 92.1 %
Date: 2022-11-10 04:21:28 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
6374
class HostPort {
21
 public:
22
6119
  HostPort(const std::string& host_name, int port)
23
6119
      : host_name_(host_name), port_(port) {}
24
19140
  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
7124
  const std::string& host() const { return host_name_; }
34
35
7349
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
7349
    CHECK_GE(port_, 0);
38
7349
    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
89952
  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
6374
class DebugOptions : public Options {
68
 public:
69
5971
  DebugOptions() = default;
70
12760
  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
6374
  bool wait_for_connect() const {
100

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