Skip to content

Commit

Permalink
Convert uuid_generate to use existing randomFill function. NFC (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sbc100 authored Dec 30, 2024
1 parent b94d6e6 commit 5a8d9e5
Show file tree
Hide file tree
Showing 25 changed files with 30 additions and 57 deletions.
32 changes: 3 additions & 29 deletions src/library_uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,11 @@ addToLibrary({
// Write a RFC4122 version 4 compliant UUID largely based on the method found in
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
// tweaked slightly in order to use the 'compact' UUID form used by libuuid.
uuid_generate__deps: ['$writeArrayToMemory'],
uuid_generate__deps: ['$writeArrayToMemory', '$randomFill'],
uuid_generate: (out) => {
// void uuid_generate(uuid_t out);
var uuid = null;

if (ENVIRONMENT_IS_NODE) {
#if ENVIRONMENT_MAY_BE_NODE
// If Node.js try to use crypto.randomBytes
try {
var rb = require('crypto')['randomBytes'];
uuid = rb(16);
} catch(e) {}
#endif // ENVIRONMENT_MAY_BE_NODE
} else if (ENVIRONMENT_IS_WEB &&
typeof window.crypto != 'undefined' &&
typeof window.crypto.getRandomValues != 'undefined') {
// If crypto.getRandomValues is available try to use it.
uuid = new Uint8Array(16);
window.crypto.getRandomValues(uuid);
}

// Fall back to Math.random if a higher quality random number generator is not available.
if (!uuid) {
uuid = new Array(16);
var d = new Date().getTime();
for (var i = 0; i < 16; i++) {
var r = ((d + Math.random() * 256) % 256)|0;
d = (d / 256)|0;
uuid[i] = r;
}
}
var uuid = new Uint8Array(16);
randomFill(uuid);

// Makes uuid compliant to RFC-4122
uuid[6] = (uuid[6] & 0x0F) | 0x40; // uuid version
Expand Down
3 changes: 1 addition & 2 deletions src/library_wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,9 +586,8 @@ var WasiLibrary = {
return (view) => crypto_module['randomFillSync'](view);
}
// very old nodejs with the original crypto API
var randomBytes = crypto_module['randomBytes'];
return (view) => (
view.set(randomBytes(view.byteLength)),
view.set(crypto_module['randomBytes'](view.byteLength)),
// Return the original view to match modern native implementations.
view
);
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 @@
8408
8405
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 @@
20486
20468
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 @@
8392
8390
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 @@
20454
20436
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 @@
9413
9412
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 @@
24255
24237
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 @@
8374
8370
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 @@
20379
20361
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8374
8370
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20379
20361
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 @@
8406
8404
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 @@
20510
20492
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 @@
9417
9416
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 @@
24255
24237
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 @@
8408
8405
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 @@
20486
20468
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 @@
3651
3646
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 @@
7910
7892
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 @@
7704
7699
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 @@
18980
18962
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 @@
2882
2880
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 @@
6198
6180
8 changes: 4 additions & 4 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15243,10 +15243,10 @@ def test_uuid(self):

js_out = read_file('test_uuid.js')

# Check that test.js compiled with --closure 1 contains ").randomBytes" and
# "window.crypto.getRandomValues"
self.assertContained(").randomBytes", js_out)
self.assertContained("window.crypto.getRandomValues", js_out)
# Check that test.js compiled with --closure 1 contains ".randomBytes(" and
# ".getRandomValues("
self.assertContained(".randomBytes(", 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 5a8d9e5

Please sign in to comment.