GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 37 94.6 %
Date: 2022-06-05 04:15:40 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
6044
class HostPort {
21
 public:
22
5647
  HostPort(const std::string& host_name, int port)
23
5647
      : host_name_(host_name), port_(port) {}
24
18150
  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
6650
  const std::string& host() const { return host_name_; }
34
35
6803
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
6803
    CHECK_GE(port_, 0);
38
6803
    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
85082
  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
6044
class DebugOptions : public Options {
67
 public:
68
5547
  DebugOptions() = default;
69
12100
  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
6044
  bool wait_for_connect() const {
98

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