Skip to content

fix: launch crash when null device is disabled on Windows #47870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api/command-line-switches.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,11 @@ Disables the Chromium [sandbox](https://www.chromium.org/developers/design-docum
Forces renderer process and Chromium helper processes to run un-sandboxed.
Should only be used for testing.

### --no-stdio-init

Disable stdio initialization during node initialization.
Used to avoid node initialization crash when the nul device is disabled on Windows platform.

### --proxy-bypass-list=`hosts`

Instructs Electron to bypass the proxy server for the given semi-colon-separated
Expand Down
29 changes: 25 additions & 4 deletions shell/app/node_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "shell/common/node_bindings.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
#include "shell/common/options_switches.h"
#include "shell/common/platform_util.h"

#if BUILDFLAG(IS_WIN)
#include "chrome/child/v8_crashpad_support_win.h"
Expand Down Expand Up @@ -189,14 +191,33 @@
NodeBindings::RegisterBuiltinBindings();

// Parse Node.js cli flags and strip out disallowed options.
const std::vector<std::string> args = ElectronCommandLine::AsUtf8();
std::vector<std::string> args = ElectronCommandLine::AsUtf8();
ExitIfContainsDisallowedFlags(args);

uint64_t process_flags =
node::ProcessInitializationFlags::kNoInitializeV8 |
node::ProcessInitializationFlags::kNoInitializeNodeV8Platform;

base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();

Check failure on line 201 in shell/app/node_main.cc

View workflow job for this annotation

GitHub Actions / linux-x64-asan / build / build

declaration shadows a local variable [-Werror,-Wshadow]

Check failure on line 201 in shell/app/node_main.cc

View workflow job for this annotation

GitHub Actions / linux-x64 / build / build

declaration shadows a local variable [-Werror,-Wshadow]

Check failure on line 201 in shell/app/node_main.cc

View workflow job for this annotation

GitHub Actions / linux-arm / build / build

declaration shadows a local variable [-Werror,-Wshadow]

Check failure on line 201 in shell/app/node_main.cc

View workflow job for this annotation

GitHub Actions / linux-arm64 / build / build

declaration shadows a local variable [-Werror,-Wshadow]
if (command_line->HasSwitch(switches::kNoStdioInit)) {
process_flags |= node::ProcessInitializationFlags::kNoStdioInitialization;
// remove the option to avoid node error "bad option: --no-stdio-init"
std::string option = std::string("--") + switches::kNoStdioInit;
std::erase(args, option);
} else {
#if BUILDFLAG(IS_WIN)
if (!platform_util::IsNulDeviceEnabled()) {
LOG(FATAL) << "Unable to open nul device needed for initialization,"
"aborting startup. As a workaround, try starting with --"
<< switches::kNoStdioInit;
}
#endif
}

std::shared_ptr<node::InitializationResult> result =
node::InitializeOncePerProcess(
args,
{node::ProcessInitializationFlags::kNoInitializeV8,
node::ProcessInitializationFlags::kNoInitializeNodeV8Platform});
args, static_cast<node::ProcessInitializationFlags::Flags>(
process_flags));

for (const std::string& error : result->errors())
std::cerr << args[0] << ": " << error << '\n';
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_utility_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ UtilityProcessWrapper::UtilityProcessWrapper(
OPEN_EXISTING, 0, nullptr);
if (handle == INVALID_HANDLE_VALUE) {
PLOG(ERROR) << "Failed to create null handle";
Emit("error", "Failed to create null handle for ignoring stdio");
return;
}
if (io_handle == IOHandle::STDOUT) {
Expand Down
3 changes: 2 additions & 1 deletion shell/browser/electron_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches(
if (process_type == ::switches::kUtilityProcess ||
process_type == ::switches::kRendererProcess) {
// Copy following switches to child process.
static constexpr std::array<const char*, 9U> kCommonSwitchNames = {
static constexpr std::array<const char*, 10U> kCommonSwitchNames = {
switches::kStandardSchemes.c_str(),
switches::kEnableSandbox.c_str(),
switches::kSecureSchemes.c_str(),
Expand All @@ -542,6 +542,7 @@ void ElectronBrowserClient::AppendExtraCommandLineSwitches(
switches::kFetchSchemes.c_str(),
switches::kServiceWorkerSchemes.c_str(),
switches::kStreamingSchemes.c_str(),
switches::kNoStdioInit.c_str(),
switches::kCodeCacheSchemes.c_str()};
command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
kCommonSwitchNames);
Expand Down
15 changes: 15 additions & 0 deletions shell/common/node_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "shell/common/mac/main_application_bundle.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
#include "shell/common/options_switches.h"
#include "shell/common/platform_util.h"
#include "shell/common/process_util.h"
#include "shell/common/world_ids.h"
#include "third_party/blink/public/web/web_local_frame.h"
Expand Down Expand Up @@ -617,6 +619,19 @@ void NodeBindings::Initialize(v8::Isolate* const isolate,
if (!fuses::IsNodeOptionsEnabled())
process_flags |= node::ProcessInitializationFlags::kDisableNodeOptionsEnv;

base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kNoStdioInit)) {
process_flags |= node::ProcessInitializationFlags::kNoStdioInitialization;
} else {
#if BUILDFLAG(IS_WIN)
if (!platform_util::IsNulDeviceEnabled()) {
LOG(FATAL) << "Unable to open nul device needed for initialization,"
"aborting startup. As a workaround, try starting with --"
<< switches::kNoStdioInit;
}
#endif
}

std::shared_ptr<node::InitializationResult> result =
node::InitializeOncePerProcess(
args,
Expand Down
4 changes: 4 additions & 0 deletions shell/common/options_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ inline constexpr base::cstring_view kDisableNTLMv2 = "disable-ntlm-v2";
inline constexpr base::cstring_view kServiceWorkerPreload =
"service-worker-preload";

// If set, flag node::ProcessInitializationFlags::kNoStdioInitialization would
// be set for node initialization.
inline constexpr base::cstring_view kNoStdioInit = "no-stdio-init";

} // namespace switches

} // namespace electron
Expand Down
3 changes: 3 additions & 0 deletions shell/common/platform_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ void Beep();
#if BUILDFLAG(IS_WIN)
// SHGetFolderPath calls not covered by Chromium
bool GetFolderPath(int key, base::FilePath* result);

// Check if nul device can be used.
bool IsNulDeviceEnabled();
#endif

#if BUILDFLAG(IS_MAC)
Expand Down
13 changes: 13 additions & 0 deletions shell/common/platform_util_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <comdef.h>
#include <commdlg.h>
#include <dwmapi.h>
#include <fcntl.h>
#include <io.h>
#include <objbase.h>
#include <shellapi.h>
#include <shlobj.h>
Expand Down Expand Up @@ -450,4 +452,15 @@ void Beep() {
MessageBeep(MB_OK);
}

bool IsNulDeviceEnabled() {
bool ret = true;
int fd = _open("nul", _O_RDWR);
if (fd < 0) {
ret = false;
} else {
_close(fd);
}
return ret;
}

} // namespace platform_util
Loading