GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_options.h Lines: 45 47 95.7 %
Date: 2021-06-03 04:12: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
namespace node {
15
16
23720
class HostPort {
17
 public:
18
5060
  HostPort(const std::string& host_name, int port)
19
5060
      : host_name_(host_name), port_(port) {}
20
15717
  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
5289
  const std::string& host() const { return host_name_; }
30
31
5412
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
5412
    CHECK_GE(port_, 0);
34
5412
    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
45926
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
37263
  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
18953
class DebugOptions : public Options {
63
 public:
64
4960
  DebugOptions() = default;
65
10478
  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
5231
  bool wait_for_connect() const {
94

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