GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 37 94.6 %
Date: 2022-07-24 04:16:20 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
6565
class HostPort {
21
 public:
22
5731
  HostPort(const std::string& host_name, int port)
23
5731
      : host_name_(host_name), port_(port) {}
24
19713
  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
7182
  const std::string& host() const { return host_name_; }
34
35
7339
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
7339
    CHECK_GE(port_, 0);
38
7339
    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
90798
  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
6565
class DebugOptions : public Options {
67
 public:
68
5631
  DebugOptions() = default;
69
13142
  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
6565
  bool wait_for_connect() const {
98

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