Skip to content

Commit 5e3fab4

Browse files
authored
Cleanup randomFill. NFC (#23260)
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
1 parent 5a8d9e5 commit 5e3fab4

35 files changed

+51
-70
lines changed

src/closure-externs/node-externs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,5 @@ path.isAbsolute;
137137
* @type {Object.<string,*>}
138138
*/
139139
path.posix;
140+
141+
crypto.randomFillSync;

src/library_fs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,8 @@ FS.staticInit();
14111411
var randomBuffer = new Uint8Array(1024), randomLeft = 0;
14121412
var randomByte = () => {
14131413
if (randomLeft === 0) {
1414-
randomLeft = randomFill(randomBuffer).byteLength;
1414+
randomFill(randomBuffer);
1415+
randomLeft = randomBuffer.byteLength;
14151416
}
14161417
return randomBuffer[--randomLeft];
14171418
};

src/library_wasi.js

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -561,53 +561,35 @@ var WasiLibrary = {
561561
// random.h
562562

563563
$initRandomFill: () => {
564-
if (typeof crypto == 'object' && typeof crypto['getRandomValues'] == 'function') {
565-
// for modern web browsers
564+
if (typeof crypto == 'object' && typeof crypto.getRandomValues == 'function') {
566565
#if SHARED_MEMORY
567566
// like with most Web APIs, we can't use Web Crypto API directly on shared memory,
568567
// so we need to create an intermediate buffer and copy it to the destination
569-
return (view) => (
570-
view.set(crypto.getRandomValues(new Uint8Array(view.byteLength))),
571-
// Return the original view to match modern native implementations.
572-
view
573-
);
568+
return (view) => view.set(crypto.getRandomValues(new Uint8Array(view.byteLength)));
574569
#else
575570
return (view) => crypto.getRandomValues(view);
576571
#endif
577-
} else
572+
}
573+
578574
#if ENVIRONMENT_MAY_BE_NODE
579575
if (ENVIRONMENT_IS_NODE) {
580-
// for nodejs with or without crypto support included
581-
try {
582-
var crypto_module = require('crypto');
583-
var randomFillSync = crypto_module['randomFillSync'];
584-
if (randomFillSync) {
585-
// nodejs with LTS crypto support
586-
return (view) => crypto_module['randomFillSync'](view);
587-
}
588-
// very old nodejs with the original crypto API
589-
return (view) => (
590-
view.set(crypto_module['randomBytes'](view.byteLength)),
591-
// Return the original view to match modern native implementations.
592-
view
593-
);
594-
} catch (e) {
595-
// nodejs doesn't have crypto support
596-
}
576+
return (view) => require('crypto').randomFillSync(view);
597577
}
598578
#endif // ENVIRONMENT_MAY_BE_NODE
599-
// we couldn't find a proper implementation, as Math.random() is not suitable for /dev/random, see emscripten-core/emscripten/pull/7096
579+
580+
// we couldn't find a proper implementation, as Math.random() is not
581+
// suitable for /dev/random, see emscripten-core/emscripten/pull/7096
600582
#if ASSERTIONS
601-
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 } };');
583+
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 } };');
602584
#else
603-
abort('initRandomDevice');
585+
abort();
604586
#endif
605587
},
606588

607589
$randomFill__deps: ['$initRandomFill'],
608590
$randomFill: (view) => {
609591
// Lazily init on the first invocation.
610-
return (randomFill = initRandomFill())(view);
592+
(randomFill = initRandomFill())(view);
611593
},
612594

613595
random_get__proxy: 'none',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8405
1+
8365
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20468
1+
20373
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
128894
1+
128914
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8390
1+
8347
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20436
1+
20341
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
128343
1+
128363
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9412
1+
9369
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24237
1+
24141
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
170928
1+
170948
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8370
1+
8329
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20361
1+
20266
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
142143
1+
142163
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8370
1+
8329
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20361
1+
20266
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
144730
1+
144750
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8404
1+
8363
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20492
1+
20397
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
121844
1+
121864
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9416
1+
9375
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24237
1+
24142
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
232437
1+
232457
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8405
1+
8365
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20468
1+
20373
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
131701
1+
131721
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3646
1+
3600
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7892
1+
7794
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
168848
1+
168868
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7699
1+
7656
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18962
1+
18867
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2880
1+
2837
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6180
1+
6083

test/test_other.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15235,18 +15235,14 @@ def test_js_only_settings(self):
1523515235
def test_uuid(self):
1523615236
# We run this test in Node/SPIDERMONKEY and browser environments because we
1523715237
# try to make use of high quality crypto random number generators such as
15238-
# crypto.getRandomValues or randomBytes (if available).
15239-
15240-
# Use closure compiler so we can check that require('crypto').randomBytes and
15241-
# window.crypto.getRandomValues doesn't get minified out.
15238+
# `getRandomValues` or `randomFillSync`.
1524215239
self.do_runf('test_uuid.c', emcc_args=['-O2', '--closure=1', '-luuid'])
1524315240

15241+
# Check that test.js compiled with --closure 1 contains `randomFillSync` and
15242+
# `getRandomValues`
1524415243
js_out = read_file('test_uuid.js')
15245-
15246-
# Check that test.js compiled with --closure 1 contains ".randomBytes(" and
15247-
# ".getRandomValues("
15248-
self.assertContained(".randomBytes(", js_out)
15249-
self.assertContained(".getRandomValues(", js_out)
15244+
self.assertContained('.randomFillSync(', js_out)
15245+
self.assertContained('.getRandomValues(', js_out)
1525015246

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

0 commit comments

Comments
 (0)