Skip to content

Fix getentropy/random_get in Node and shell#27122

Merged
hoodmane merged 4 commits into
emscripten-core:mainfrom
hoodmane:fix-get-entropy
Jun 16, 2026
Merged

Fix getentropy/random_get in Node and shell#27122
hoodmane merged 4 commits into
emscripten-core:mainfrom
hoodmane:fix-get-entropy

Conversation

@hoodmane

Copy link
Copy Markdown
Collaborator

PR #26526 refactored random_get to return randomFill()'s value rather than a hardcoded 0, but the node and shell implementations of randomFill() were not updated.

PR emscripten-core#26526 refactored random_get to return randomFill()'s value
rather than a hardcoded 0, but the node and shell implementations of
randomFill() were not updated.
@hoodmane

Copy link
Copy Markdown
Collaborator Author

Hopefully the codesize checks will smile upon me...

Comment thread test/test_other.py Outdated
Comment thread test/test_other.py Outdated

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that must have a pain to debug. Sorry about that :(

@hoodmane

Copy link
Copy Markdown
Collaborator Author

Yeah it's kind of... special... that ToWebAssemblyValue sends any length two or greater typed array to 0 but a length one typed array goes to the value inside.

@hoodmane

Copy link
Copy Markdown
Collaborator Author

Right, it calls ToInt32 which calls ToIntegerOrInfinity which ToNumber(ToPrimitive(x)). ToPrimitive(x) converts to a string and if the length is 1 ToNumber() converts it to the number inside and otherwise converts to NaN. Then ToIntegerOrInfinity converts the NaN to 0 and everything else passes through.

@sbc100

sbc100 commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Right, it calls ToInt32 which calls ToIntegerOrInfinity which ToNumber(ToPrimitive(x)). ToPrimitive(x) converts to a string and if the length is 1 ToNumber() converts it to the number inside and otherwise converts to NaN. Then ToIntegerOrInfinity converts the NaN to 0 and everything else passes through.

And yet it won't coerse the number 0 to an i64 without bigint suffix on the 0n. Seems crazy.

@hoodmane

Copy link
Copy Markdown
Collaborator Author

Ah so wasmFunctionWhichExpectsBigInt(new Uint8Array(7)) sees a 7, but wasmFunctionWhichExpectsBigInt(7) is a type error. Right.

@sbc100

sbc100 commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

Ah so wasmFunctionWhichExpectsBigInt(new Uint8Array(7)) sees a 7, but wasmFunctionWhichExpectsBigInt(7) is a type error. Right.

Really, I would expect wasmFunctionWhichExpectsBigInt(new Uint8Array(7)) not to works too.. does it work?

@hoodmane

Copy link
Copy Markdown
Collaborator Author

It converts it to a string and ToBigInt happily converts a string to a bigint just not a number
https://tc39.es/ecma262/multipage/abstract-operations.html#sec-tobigint

const bytes = new Uint8Array([
  0, 97, 115, 109, 1, 0, 0, 0,   // magic + version
  1, 6, 1, 96, 1, 126, 1, 127,   // type section: (i64) -> i32
  3, 2, 1, 0,                          // function section
  7, 8, 1, 4, 119, 114, 97, 112, 0, 0, // export "wrap"
  10, 7, 1, 5, 0, 32, 0, 167, 11,      // code: local.get 0; i32.wrap_i64; end
]);

const arr = new Uint8Array([200]);

WebAssembly.instantiate(bytes).then(({ instance }) => {
  const { wrap } = instance.exports;
  console.log("result:", wrap(arr)); // logs 200
});

Comment thread ChangeLog.md Outdated
@hoodmane hoodmane enabled auto-merge (squash) June 16, 2026 01:13
@hoodmane hoodmane merged commit 11f2290 into emscripten-core:main Jun 16, 2026
39 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants