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