From bcd88f84f6a287c5b457755dbdf863163c012c5f Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 10 Feb 2022 12:17:56 +0100 Subject: [PATCH] src: add flags for controlling process behavior https://github.com/nodejs/node/pull/40339 --- patches/node/.patches | 2 - ...m_env_option_to_disable_node_options.patch | 68 ---------------- ...s_for_low-level_hooks_and_exceptions.patch | 81 ------------------- shell/common/node_bindings.cc | 10 ++- 4 files changed, 6 insertions(+), 155 deletions(-) delete mode 100644 patches/node/add_should_read_node_options_from_env_option_to_disable_node_options.patch delete mode 100644 patches/node/feat_add_flags_for_low-level_hooks_and_exceptions.patch diff --git a/patches/node/.patches b/patches/node/.patches index 7a36ac8cbf7a6..b0ca21548a750 100644 --- a/patches/node/.patches +++ b/patches/node/.patches @@ -4,7 +4,6 @@ feat_initialize_asar_support.patch expose_get_builtin_module_function.patch build_add_gn_build_files.patch fix_add_default_values_for_variables_in_common_gypi.patch -feat_add_flags_for_low-level_hooks_and_exceptions.patch fix_expose_tracing_agent_and_use_tracing_tracingcontroller_instead.patch pass_all_globals_through_require.patch build_modify_js2c_py_to_allow_injection_of_original-fs_and_custom_embedder_js.patch @@ -19,7 +18,6 @@ fix_allow_preventing_initializeinspector_in_env.patch src_allow_embedders_to_provide_a_custom_pageallocator_to.patch fix_crypto_tests_to_run_with_bssl.patch fix_account_for_debugger_agent_race_condition.patch -add_should_read_node_options_from_env_option_to_disable_node_options.patch repl_fix_crash_when_sharedarraybuffer_disabled.patch fix_readbarrier_undefined_symbol_error_on_woa_arm64.patch chore_fix_-wimplicit-fallthrough.patch diff --git a/patches/node/add_should_read_node_options_from_env_option_to_disable_node_options.patch b/patches/node/add_should_read_node_options_from_env_option_to_disable_node_options.patch deleted file mode 100644 index 8f7b86fbef1fb..0000000000000 --- a/patches/node/add_should_read_node_options_from_env_option_to_disable_node_options.patch +++ /dev/null @@ -1,68 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Samuel Attard -Date: Wed, 21 Jul 2021 13:40:59 -0700 -Subject: add should_read_node_options_from_env option to disable NODE_OPTIONS - parsing at runtime - -We can remove the NODE_OPTIONS environment variable but it in theory could be injected / re-inserted at runtime and be used for workers. In order to ensure the fuse is respected we need a hard runtime toggle for NODE_OPTION support. - -diff --git a/src/env.cc b/src/env.cc -index 2cb2dccdf32ba4ad58972bb1d2b185996104b5ee..0abd61d6ddd1fb55afde40b38cf7c9400a8c3d41 100644 ---- a/src/env.cc -+++ b/src/env.cc -@@ -329,6 +329,9 @@ std::string GetExecPath(const std::vector& argv) { - return exec_path; - } - -+/* static */ -+bool Environment::should_read_node_options_from_env_ = true; -+ - Environment::Environment(IsolateData* isolate_data, - Isolate* isolate, - const std::vector& args, -diff --git a/src/env.h b/src/env.h -index e0deca497feb111622b257b952c9ed9161c7d001..ab8334bf0e3405fee4d21a4b541bd1164d92ca89 100644 ---- a/src/env.h -+++ b/src/env.h -@@ -1145,6 +1145,8 @@ class Environment : public MemoryRetainer { - inline double trigger_async_id(); - inline double get_default_trigger_async_id(); - -+ static bool should_read_node_options_from_env_; -+ - // List of id's that have been destroyed and need the destroy() cb called. - inline std::vector* destroy_async_id_list(); - -diff --git a/src/node.cc b/src/node.cc -index 207a95d202b4e422a39f837241f1655f7111b1e3..14b9002dbd918b59b05d8b12c5441080695ed9f0 100644 ---- a/src/node.cc -+++ b/src/node.cc -@@ -875,7 +875,7 @@ int InitializeNodeWithArgs(std::vector* argv, - #if !defined(NODE_WITHOUT_NODE_OPTIONS) - std::string node_options; - -- if (credentials::SafeGetenv("NODE_OPTIONS", &node_options)) { -+ if (Environment::should_read_node_options_from_env_ && credentials::SafeGetenv("NODE_OPTIONS", &node_options)) { - std::vector env_argv = - ParseNodeOptionsEnvVar(node_options, errors); - -diff --git a/src/node_worker.cc b/src/node_worker.cc -index 16b7be36f284311f38583fa1df28a2945560b524..62a7dae080fad7e18863968dee22dbe4b461ab82 100644 ---- a/src/node_worker.cc -+++ b/src/node_worker.cc -@@ -467,6 +467,7 @@ void Worker::New(const FunctionCallbackInfo& args) { - }); - - #ifndef NODE_WITHOUT_NODE_OPTIONS -+ if (Environment::should_read_node_options_from_env_) { - MaybeLocal maybe_node_opts = - env_vars->Get(isolate, OneByteString(isolate, "NODE_OPTIONS")); - Local node_opts; -@@ -497,6 +498,7 @@ void Worker::New(const FunctionCallbackInfo& args) { - return; - } - } -+ } - #endif - } - diff --git a/patches/node/feat_add_flags_for_low-level_hooks_and_exceptions.patch b/patches/node/feat_add_flags_for_low-level_hooks_and_exceptions.patch deleted file mode 100644 index bfe9ef5ec6ade..0000000000000 --- a/patches/node/feat_add_flags_for_low-level_hooks_and_exceptions.patch +++ /dev/null @@ -1,81 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Shelley Vohr -Date: Thu, 13 Sep 2018 08:42:22 -0700 -Subject: feat: add flags for low-level hooks and exceptions - -This commit adds two new exposed methods to `node.cc`, `InitGeneric` and -`LoadEnvironmentGeneric` that allow for control of which mode Node -should run, which i have called `standalone_mode` and -`upstream_node_mode`. Default behavior of `Init` and `LoadEnvironment` -remain the same. - -We have 3 modes when running Node in Electron: -1. In the main process, we want to have a full Node environment, but -with signal handlers and other low level things disabled -2. In renderer process, we want Node to reuse the web page's context -3. In `ELECTRON_RUN_AS_NODE`, we want Node to run as it runs officially -by default - -For modes 1 and 3, we have Node create a new V8 context with a Node -Environment on it. However, for mode 2, since the V8 context is created -by blink for web frames and web workers we make Node create the Node -Environment on the V8 context of blink, so no new V8 context is created. - -As a result, a renderer process may have multiple Node Environments in it. - -diff --git a/src/node.cc b/src/node.cc -index e942c108a15a3c50d92c08b344f6691050c7859d..207a95d202b4e422a39f837241f1655f7111b1e3 100644 ---- a/src/node.cc -+++ b/src/node.cc -@@ -139,6 +139,8 @@ using v8::Undefined; - using v8::V8; - using v8::Value; - -+bool g_upstream_node_mode = true; -+ - namespace per_process { - - // node_revert.h -@@ -853,7 +855,9 @@ int InitializeNodeWithArgs(std::vector* argv, - binding::RegisterBuiltinModules(); - - // Make inherited handles noninheritable. -- uv_disable_stdio_inheritance(); -+ if (g_upstream_node_mode) { -+ uv_disable_stdio_inheritance(); -+ } - - // Cache the original command line to be - // used in diagnostic reports. -@@ -887,7 +891,8 @@ int InitializeNodeWithArgs(std::vector* argv, - if (exit_code != 0) return exit_code; - } - #endif -- -+ if (g_upstream_node_mode) { -+ // NOTE(jeremy): indentation is intentionally wrong here, to ease rebasing. - const int exit_code = ProcessGlobalArgs(argv, - exec_argv, - errors, -@@ -930,7 +935,7 @@ int InitializeNodeWithArgs(std::vector* argv, - return 9; - } - per_process::metadata.versions.InitializeIntlVersions(); -- -+ } // g_upstream_node_mode - # ifndef __POSIX__ - std::string tz; - if (credentials::SafeGetenv("TZ", &tz) && !tz.empty()) { -diff --git a/src/node.h b/src/node.h -index 1f9afa558d0c8b7950a0f5862017e09a08118ec1..45de72bd94cf669ac2badf89d23164cb7022a5b3 100644 ---- a/src/node.h -+++ b/src/node.h -@@ -213,6 +213,8 @@ namespace node { - - class IsolateData; - class Environment; -+// Whether node should open some low level hooks. -+NODE_EXTERN extern bool g_upstream_node_mode; - - // TODO(addaleax): Officially deprecate this and replace it with something - // better suited for a public embedder API. diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 99b017dc425fe..88a0118ccbfe0 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -365,7 +365,6 @@ bool NodeBindings::IsInitialized() { void NodeBindings::Initialize() { TRACE_EVENT0("electron", "NodeBindings::Initialize"); // Open node's error reporting system for browser process. - node::g_upstream_node_mode = false; #if BUILDFLAG(IS_LINUX) // Get real command line in renderer process forked by zygote. @@ -381,14 +380,17 @@ void NodeBindings::Initialize() { auto env = base::Environment::Create(); SetNodeOptions(env.get()); - node::Environment::should_read_node_options_from_env_ = - fuses::IsNodeOptionsEnabled(); std::vector argv = {"electron"}; std::vector exec_argv; std::vector errors; + uint64_t process_flags = node::ProcessFlags::kEnableStdioInheritance; + if (!fuses::IsNodeOptionsEnabled()) + process_flags |= node::ProcessFlags::kDisableNodeOptionsEnv; - int exit_code = node::InitializeNodeWithArgs(&argv, &exec_argv, &errors); + int exit_code = node::InitializeNodeWithArgs( + &argv, &exec_argv, &errors, + static_cast(process_flags)); for (const std::string& error : errors) fprintf(stderr, "%s: %s\n", argv[0].c_str(), error.c_str());