Skip to content

Commit c29dd5f

Browse files
authored
Fix multithreaded wasm crash (solves #164) (#165)
Signed-off-by: Joe Richey <[email protected]>
1 parent 3a7e605 commit c29dd5f

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ jobs:
102102
rust: nightly
103103
install:
104104
- rustup target add wasm32-unknown-unknown
105-
- cargo --list | egrep "^\s*deadlinks$" -q || cargo install cargo-deadlinks
105+
- cargo install cargo-deadlinks
106106
- cargo deadlinks -V
107107
script:
108108
# Check that setting various features does not break the build

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ wasi = "0.10"
3131
stdweb = { version = "0.4.18", default-features = false, optional = true }
3232
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dependencies]
3333
wasm-bindgen = { version = "0.2.62", default-features = false, optional = true }
34+
js-sys = { version = "0.3", optional = true }
3435
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown", not(cargo_web)))'.dev-dependencies]
3536
wasm-bindgen-test = "0.3.18"
3637

@@ -40,7 +41,7 @@ std = []
4041
# Feature to enable fallback RDRAND-based implementation on x86/x86_64
4142
rdrand = []
4243
# Feature to enable JavaScript bindings on wasm32-unknown-unknown
43-
js = ["stdweb", "wasm-bindgen"]
44+
js = ["stdweb", "wasm-bindgen", "js-sys"]
4445
# Feature to enable custom RNG implementations
4546
custom = []
4647
# Unstable feature to support being a libstd dependency

src/wasm-bindgen.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ use crate::Error;
1010
extern crate std;
1111
use std::thread_local;
1212

13+
use js_sys::Uint8Array;
1314
use wasm_bindgen::prelude::*;
1415

16+
// Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
17+
const BROWSER_CRYPTO_BUFFER_SIZE: usize = 256;
18+
1519
enum RngSource {
1620
Node(NodeCrypto),
17-
Browser(BrowserCrypto),
21+
Browser(BrowserCrypto, Uint8Array),
1822
}
1923

2024
// JsValues are always per-thread, so we initialize RngSource for each thread.
@@ -33,17 +37,18 @@ pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
3337
return Err(Error::NODE_RANDOM_FILL_SYNC);
3438
}
3539
}
36-
RngSource::Browser(n) => {
37-
// see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
38-
//
39-
// where it says:
40-
//
41-
// > A QuotaExceededError DOMException is thrown if the
42-
// > requested length is greater than 65536 bytes.
43-
for chunk in dest.chunks_mut(65536) {
44-
if n.get_random_values(chunk).is_err() {
40+
RngSource::Browser(crypto, buf) => {
41+
// getRandomValues does not work with all types of WASM memory,
42+
// so we initially write to browser memory to avoid exceptions.
43+
for chunk in dest.chunks_mut(BROWSER_CRYPTO_BUFFER_SIZE) {
44+
// The chunk can be smaller than buf's length, so we call to
45+
// JS to create a smaller view of buf without allocation.
46+
let sub_buf = buf.subarray(0, chunk.len() as u32);
47+
48+
if crypto.get_random_values(&sub_buf).is_err() {
4549
return Err(Error::WEB_GET_RANDOM_VALUES);
4650
}
51+
sub_buf.copy_to(chunk);
4752
}
4853
}
4954
};
@@ -63,7 +68,9 @@ fn getrandom_init() -> Result<RngSource, Error> {
6368
(_, crypto) if !crypto.is_undefined() => crypto,
6469
_ => return Err(Error::WEB_CRYPTO),
6570
};
66-
return Ok(RngSource::Browser(crypto));
71+
72+
let buf = Uint8Array::new_with_length(BROWSER_CRYPTO_BUFFER_SIZE as u32);
73+
return Ok(RngSource::Browser(crypto, buf));
6774
}
6875

6976
let crypto = MODULE.require("crypto").map_err(|_| Error::NODE_CRYPTO)?;
@@ -84,7 +91,7 @@ extern "C" {
8491

8592
type BrowserCrypto;
8693
#[wasm_bindgen(method, js_name = getRandomValues, catch)]
87-
fn get_random_values(me: &BrowserCrypto, buf: &mut [u8]) -> Result<(), JsValue>;
94+
fn get_random_values(me: &BrowserCrypto, buf: &Uint8Array) -> Result<(), JsValue>;
8895

8996
#[wasm_bindgen(js_name = module)]
9097
static MODULE: NodeModule;

0 commit comments

Comments
 (0)