GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 35 37 94.6 %
Date: 2022-02-01 04:14:12 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
5536
class HostPort {
21
 public:
22
5357
  HostPort(const std::string& host_name, int port)
23
5357
      : host_name_(host_name), port_(port) {}
24
16626
  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
5613
  const std::string& host() const { return host_name_; }
34
35
5756
  int port() const {
36
    // TODO(joyeecheung): make port a uint16_t
37
5756
    CHECK_GE(port_, 0);
38
5756
    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
80030
  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
5536
class DebugOptions : public Options {
67
 public:
68
5257
  DebugOptions() = default;
69
11084
  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
5536
  bool wait_for_connect() const {
98

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