Skip to content

Commit 5aab925

Browse files
committed
Account for Firefox SharedArrayBuffer bug in shared workers
1 parent d0b6d66 commit 5aab925

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/backends/wasm_js.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use crate::util::{inner_u32, inner_u64};
1616
#[cfg(not(all(target_arch = "wasm32", any(target_os = "unknown", target_os = "none"))))]
1717
compile_error!("`wasm_js` backend can be enabled only for OS-less WASM targets!");
1818

19-
use js_sys::{SharedArrayBuffer, Uint8Array, WebAssembly::Memory};
19+
use js_sys::{JsString, Object, SharedArrayBuffer, Uint8Array, WebAssembly::Memory};
2020
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue};
2121

2222
// Size of our temporary Uint8Array buffer used with WebCrypto methods
@@ -38,8 +38,24 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
3838
MEMORY_KIND_NOT_SHARED => false,
3939
MEMORY_KIND_SHARED => true,
4040
MEMORY_KIND_UNINIT => {
41-
let memory: Memory = wasm_bindgen::memory().unchecked_into();
42-
let val = if memory.buffer().is_instance_of::<SharedArrayBuffer>() {
41+
let buffer = wasm_bindgen::memory().unchecked_into::<Memory>().buffer();
42+
43+
// `SharedArrayBuffer` is only available with COOP & COEP. But even without its
44+
// possible to create a shared `WebAssembly.Memory`, so we check for that via
45+
// the constructor name.
46+
//
47+
// Keep in mind that `crossOriginIsolated` is not available on Node.js, in
48+
// which case we can still use `instanceof` because `SharedArrayBuffer` is
49+
// always available.
50+
let shared = match CROSS_ORIGIN_ISOLATED.with(Option::clone) {
51+
Some(true) | None => buffer.is_instance_of::<SharedArrayBuffer>(),
52+
Some(false) => {
53+
let constructor_name = Object::from(buffer).constructor().name();
54+
SHARED_ARRAY_BUFFER_NAME.with(|name| &constructor_name == name)
55+
}
56+
};
57+
58+
let val = if shared {
4359
MEMORY_KIND_SHARED
4460
} else {
4561
MEMORY_KIND_NOT_SHARED
@@ -87,6 +103,7 @@ pub fn fill_inner(dest: &mut [MaybeUninit<u8>]) -> Result<(), Error> {
87103
}
88104

89105
#[wasm_bindgen]
106+
#[rustfmt::skip]
90107
extern "C" {
91108
// Web Crypto API: Crypto interface (https://www.w3.org/TR/WebCryptoAPI/)
92109
type Crypto;
@@ -98,4 +115,9 @@ extern "C" {
98115
fn get_random_values(this: &Crypto, buf: &Uint8Array) -> Result<(), JsValue>;
99116
#[wasm_bindgen(method, js_name = getRandomValues, catch)]
100117
fn get_random_values_ref(this: &Crypto, buf: &mut [u8]) -> Result<(), JsValue>;
118+
// Returns the [`crossOriginIsolated`](https://developer.mozilla.org/en-US/docs/Web/API/crossOriginIsolated) global property.
119+
#[wasm_bindgen(thread_local_v2, js_namespace = globalThis, js_name = crossOriginIsolated)]
120+
static CROSS_ORIGIN_ISOLATED: Option<bool>;
121+
#[wasm_bindgen(thread_local_v2, static_string)]
122+
static SHARED_ARRAY_BUFFER_NAME: JsString = "SharedArrayBuffer";
101123
}

0 commit comments

Comments
 (0)