Skip to content

Commit

Permalink
Cleanup randomFill. NFC (#23260)
Browse files Browse the repository at this point in the history
Under node we can rely on `randomFillSync` always being available since
its been around since v9.0.0:


https://nodejs.org/api/crypto.html#cryptorandomfillsyncbuffer-offset-size

Also, remove the return value from this function since it always fills
the entire buffer.

https://caniuse.com/getrandomvalues
  • Loading branch information
sbc100 authored Dec 30, 2024
1 parent 5a8d9e5 commit 5e3fab4
Show file tree
Hide file tree
Showing 35 changed files with 51 additions and 70 deletions.
2 changes: 2 additions & 0 deletions src/closure-externs/node-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,5 @@ path.isAbsolute;
* @type {Object.<string,*>}
*/
path.posix;

crypto.randomFillSync;
3 changes: 2 additions & 1 deletion src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,8 @@ FS.staticInit();
var randomBuffer = new Uint8Array(1024), randomLeft = 0;
var randomByte = () => {
if (randomLeft === 0) {
randomLeft = randomFill(randomBuffer).byteLength;
randomFill(randomBuffer);
randomLeft = randomBuffer.byteLength;
}
return randomBuffer[--randomLeft];
};
Expand Down
40 changes: 11 additions & 29 deletions src/library_wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,53 +561,35 @@ var WasiLibrary = {
// random.h

$initRandomFill: () => {
if (typeof crypto == 'object' && typeof crypto['getRandomValues'] == 'function') {
// for modern web browsers
if (typeof crypto == 'object' && typeof crypto.getRandomValues == 'function') {
#if SHARED_MEMORY
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
// so we need to create an intermediate buffer and copy it to the destination
return (view) => (
view.set(crypto.getRandomValues(new Uint8Array(view.byteLength))),
// Return the original view to match modern native implementations.
view
);
return (view) => view.set(crypto.getRandomValues(new Uint8Array(view.byteLength)));
#else
return (view) => crypto.getRandomValues(view);
#endif
} else
}

#if ENVIRONMENT_MAY_BE_NODE
if (ENVIRONMENT_IS_NODE) {
// for nodejs with or without crypto support included
try {
var crypto_module = require('crypto');
var randomFillSync = crypto_module['randomFillSync'];
if (randomFillSync) {
// nodejs with LTS crypto support
return (view) => crypto_module['randomFillSync'](view);
}
// very old nodejs with the original crypto API
return (view) => (
view.set(crypto_module['randomBytes'](view.byteLength)),
// Return the original view to match modern native implementations.
view
);
} catch (e) {
// nodejs doesn't have crypto support
}
return (view) => require('crypto').randomFillSync(view);
}
#endif // ENVIRONMENT_MAY_BE_NODE
// we couldn't find a proper implementation, as Math.random() is not suitable for /dev/random, see emscripten-core/emscripten/pull/7096

// we couldn't find a proper implementation, as Math.random() is not
// suitable for /dev/random, see emscripten-core/emscripten/pull/7096
#if ASSERTIONS
abort('no cryptographic support found for randomDevice. consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: (array) => { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };');
abort('no cryptographic support found for random function. consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: (array) => { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };');
#else
abort('initRandomDevice');
abort();
#endif
},

$randomFill__deps: ['$initRandomFill'],
$randomFill: (view) => {
// Lazily init on the first invocation.
return (randomFill = initRandomFill())(view);
(randomFill = initRandomFill())(view);
},

random_get__proxy: 'none',
Expand Down
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8405
8365
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20468
20373
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors1.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128894
128914
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8390
8347
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20436
20341
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_ctors2.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128343
128363
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9412
9369
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24237
24141
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
170928
170948
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8370
8329
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20361
20266
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_except_wasm.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142143
142163
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8370
8329
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20361
20266
Original file line number Diff line number Diff line change
@@ -1 +1 @@
144730
144750
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8404
8363
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20492
20397
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_lto.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
121844
121864
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9416
9375
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24237
24142
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_mangle.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
232437
232457
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8405
8365
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20468
20373
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_noexcept.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131701
131721
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3646
3600
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7892
7794
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_cxx_wasmfs.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
168848
168868
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7699
7656
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
18962
18867
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.gzsize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2880
2837
2 changes: 1 addition & 1 deletion test/other/codesize/test_codesize_files_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6180
6083
14 changes: 5 additions & 9 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15235,18 +15235,14 @@ def test_js_only_settings(self):
def test_uuid(self):
# We run this test in Node/SPIDERMONKEY and browser environments because we
# try to make use of high quality crypto random number generators such as
# crypto.getRandomValues or randomBytes (if available).

# Use closure compiler so we can check that require('crypto').randomBytes and
# window.crypto.getRandomValues doesn't get minified out.
# `getRandomValues` or `randomFillSync`.
self.do_runf('test_uuid.c', emcc_args=['-O2', '--closure=1', '-luuid'])

# Check that test.js compiled with --closure 1 contains `randomFillSync` and
# `getRandomValues`
js_out = read_file('test_uuid.js')

# Check that test.js compiled with --closure 1 contains ".randomBytes(" and
# ".getRandomValues("
self.assertContained(".randomBytes(", js_out)
self.assertContained(".getRandomValues(", js_out)
self.assertContained('.randomFillSync(', js_out)
self.assertContained('.getRandomValues(', js_out)

def test_wasm64_no_asan(self):
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sMEMORY64', '-fsanitize=address'])
Expand Down

0 comments on commit 5e3fab4

Please sign in to comment.