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: 2020-07-19 22:14:24 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
24349
class HostPort {
17
 public:
18
5133
  HostPort(const std::string& host_name, int port)
19
5133
      : host_name_(host_name), port_(port) {}
20
15909
  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
77
  void set_port(int port) { port_ = port; }
28
29
5360
  const std::string& host() const { return host_name_; }
30
31
5463
  int port() const {
32
    // TODO(joyeecheung): make port a uint16_t
33
5463
    CHECK_GE(port_, 0);
34
5463
    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
46617
class Options {
48
 public:
49
  virtual void CheckOptions(std::vector<std::string>* errors) {}
50
38327
  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
19445
class DebugOptions : public Options {
63
 public:
64
5033
  DebugOptions() = default;
65
10606
  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
5302
  bool wait_for_connect() const {
94

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