Skip to content
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

fix: repl crash when SharedArrayBuffer disabled #30456

Merged
merged 1 commit into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions patches/node/.patches
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ fix_crypto_tests_to_run_with_bssl.patch
fix_account_for_debugger_agent_race_condition.patch
fix_use_new_v8_error_message_property_access_format.patch
add_should_read_node_options_from_env_option_to_disable_node_options.patch
repl_fix_crash_when_sharedarraybuffer_disabled.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Mon, 9 Aug 2021 18:42:15 +0200
Subject: repl: fix crash when SharedArrayBuffer disabled

It's possible for SharedArrayBuffers to be disabled with
--no-harmony-sharedarraybuffer so we first need to check that this
isn't the case before attempting to use them in the repl or a crash occurs.

Upstreamed at https://github.com/nodejs/node/pull/39718.

diff --git a/benchmark/worker/atomics-wait.js b/benchmark/worker/atomics-wait.js
index a771b1813731edf4f0dd60f3505799e389f1d876..b9461677e2d7d1df192e752496e62cca837717b5 100644
--- a/benchmark/worker/atomics-wait.js
+++ b/benchmark/worker/atomics-wait.js
@@ -7,6 +7,10 @@ const bench = common.createBenchmark(main, {
});

function main({ n }) {
+ if (typeof SharedArrayBuffer === 'undefined') {
+ throw new Error('SharedArrayBuffers must be enabled to run this benchmark');
+ }
+
const i32arr = new Int32Array(new SharedArrayBuffer(4));
bench.start();
for (let i = 0; i < n; i++)
diff --git a/lib/internal/main/worker_thread.js b/lib/internal/main/worker_thread.js
index d6434ff96e118535bc6ded88ee4d2f0ff253a8f7..1b8894f44e71c6f25f1f50e84293006afe158513 100644
--- a/lib/internal/main/worker_thread.js
+++ b/lib/internal/main/worker_thread.js
@@ -9,7 +9,7 @@ const {
ArrayPrototypeSplice,
ObjectDefineProperty,
PromisePrototypeCatch,
- globalThis: { Atomics },
+ globalThis: { Atomics, SharedArrayBuffer },
} = primordials;

const {
@@ -140,6 +140,9 @@ port.on('message', (message) => {
const originalCwd = process.cwd;

process.cwd = function() {
+ // SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
+ if (typeof SharedArrayBuffer === 'undefined') return originalCwd();
+
const currentCounter = Atomics.load(cwdCounter, 0);
if (currentCounter === lastCounter)
return cachedCwd;
diff --git a/lib/internal/worker.js b/lib/internal/worker.js
index 931bce0c518fc3355a9f94a7760556b6f0b75b96..99baa3e7f747bb3b351cb13c6ed512f2bb88812a 100644
--- a/lib/internal/worker.js
+++ b/lib/internal/worker.js
@@ -92,7 +92,8 @@ let cwdCounter;

const environmentData = new SafeMap();

-if (isMainThread) {
+// SharedArrayBuffers can be disabled with --no-harmony-sharedarraybuffer.
+if (isMainThread && typeof SharedArrayBuffer !== 'undefined') {
cwdCounter = new Uint32Array(new SharedArrayBuffer(4));
const originalChdir = process.chdir;
process.chdir = function(path) {