Skip to content

Commit 2bc4f61

Browse files
authored
[wasm] Automate patching the emsdk installation to add controlled indeterminism. (#75532)
1 parent 67d2570 commit 2bc4f61

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

docs/workflow/debugging/mono/wasm-debugging.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,10 @@ and change the
8383
This will hopefully cause the failure to happen reliably.
8484

8585
There is another random number generator in `upstream/emscripten/src/deterministic.js`
86-
which needs the same treatment:
87-
```
88-
var randomBuffer3 = new Uint8Array(2);
89-
crypto.getRandomValues(randomBuffer3);
86+
which needs the same treatment.
9087

91-
var MAGIC = (randomBuffer3 [0] << 8) | randomBuffer3 [1];
92-
console.log ("SEED2: " + MAGIC);
93-
```
88+
Running `make patch-deterministic` in `src/mono/wasm` will patch the
89+
emscripten installation in `src/mono/wasm/emsdk` with these changes.
9490

9591
# Debugging signature mismatch errors
9692

src/mono/wasm/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,6 @@ build-dbg-proxy:
140140
$(DOTNET) build $(TOP)/src/mono/wasm/debugger/BrowserDebugHost $(MSBUILD_ARGS)
141141
build-dbg-testsuite:
142142
$(DOTNET) build $(TOP)/src/mono/wasm/debugger/DebuggerTestSuite $(MSBUILD_ARGS)
143+
144+
patch-deterministic:
145+
cd emsdk/upstream/emscripten/ && patch -p1 < ../../../runtime/deterministic.diff
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
diff --git a/src/deterministic.js b/src/deterministic.js
2+
index 0b894b4ac..05f50abba 100644
3+
--- a/src/deterministic.js
4+
+++ b/src/deterministic.js
5+
@@ -4,7 +4,13 @@
6+
* SPDX-License-Identifier: MIT
7+
*/
8+
9+
-var MAGIC = 0;
10+
+var randomBuffer3 = new Uint8Array(2);
11+
+crypto.getRandomValues(randomBuffer3);
12+
+
13+
+var MAGIC = (randomBuffer3 [0] << 8) | randomBuffer3 [1];
14+
+console.log ("SEED2: " + MAGIC);
15+
+
16+
+//var MAGIC = 0;
17+
Math.random = () => {
18+
MAGIC = Math.pow(MAGIC + 1.8912, 3) % 1;
19+
return MAGIC;
20+
diff --git a/src/library.js b/src/library.js
21+
index 603f94dbf..698e9fe29 100644
22+
--- a/src/library.js
23+
+++ b/src/library.js
24+
@@ -2196,6 +2196,17 @@ mergeInto(LibraryManager.library, {
25+
// TODO: consider allowing the API to get a parameter for the number of
26+
// bytes.
27+
$getRandomDevice: function() {
28+
+ var randomBuffer2 = new Uint8Array(2);
29+
+ crypto.getRandomValues(randomBuffer2);
30+
+
31+
+ if (FS.seed2 == null)
32+
+ FS.seed2 = (randomBuffer2 [0] << 8) | randomBuffer2 [1];
33+
+ console.log('SEED: ' + FS.seed2);
34+
+ return function() {
35+
+ FS.seed2 = FS.seed2 * 16807 % 2147483647;
36+
+ return FS.seed2;
37+
+ };
38+
+
39+
if (typeof crypto == 'object' && typeof crypto['getRandomValues'] == 'function') {
40+
// for modern web browsers
41+
var randomBuffer = new Uint8Array(1);

0 commit comments

Comments
 (0)