GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_options.h Lines: 34 36 94.4 %
Date: 2021-10-06 04:12:32 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
namespace node {
15
16
5456
class HostPort {
17
 public:
18
5293
  HostPort(const std::string& host_name, int port)
19
5293
      : host_name_(host_name), port_(port) {}
20
16386
  HostPort(const HostPort&) = default;
21
  HostPort& operator=(const HostPort&) = default;
22
  HostPort(HostPort&&) = default;
23
  HostPort& operator=(HostPort&&) = default;
24
25
  void set_host(const std::string& host) { host_name_ = host; }
26
27
96
  void set_port(int port) { port_ = port; }
28
29
5527
  const std::string& host() const { return host_name_; }
30
31
5654
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
5654
    CHECK_GE(port_, 0);
34
5654
    return port_;
35
  }
36
37
100
  void Update(const HostPort& other) {
38
100
    if (!other.host_name_.empty()) host_name_ = other.host_name_;
39
100
    if (other.port_ >= 0) port_ = other.port_;
40
100
  }
41
42
 private:
43
  std::string host_name_;
44
  int port_;
45
};
46
47
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
78938
  virtual ~Options() = default;
51
};
52
53
struct InspectPublishUid {
54
  bool console;
55
  bool http;
56
};
57
58
// These options are currently essentially per-Environment, but it can be nice
59
// to keep them separate since they are a group of options applying to a very
60
// specific part of Node. It might also make more sense for them to be
61
// per-Isolate, rather than per-Environment.
62
5456
class DebugOptions : public Options {
63
 public:
64
5193
  DebugOptions() = default;
65
10924
  DebugOptions(const DebugOptions&) = default;
66
  DebugOptions& operator=(const DebugOptions&) = default;
67
  DebugOptions(DebugOptions&&) = default;
68
  DebugOptions& operator=(DebugOptions&&) = default;
69
70
  // --inspect
71
  bool inspector_enabled = false;
72
  // --debug
73
  bool deprecated_debug = false;
74
  // --inspect-brk
75
  bool break_first_line = false;
76
  // --inspect-brk-node
77
  bool break_node_first_line = false;
78
  // --inspect-publish-uid
79
  std::string inspect_publish_uid_string = "stderr,http";
80
81
  InspectPublishUid inspect_publish_uid;
82
83
  enum { kDefaultInspectorPort = 9229 };
84
85
  HostPort host_port{"127.0.0.1", kDefaultInspectorPort};
86
87
  // Used to patch the options as if --inspect-brk is passed.
88
38
  void EnableBreakFirstLine() {
89
38
    inspector_enabled = true;
90
38
    break_first_line = true;
91
38
  }
92
93
5456
  bool wait_for_connect() const {
94

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