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