first two calls to Math.random have the same first 4 digits when using spidermonkey compiled to wasm
Categories
(Core :: JavaScript Engine, defect, P1)
Tracking
()
Tracking | Status | |
---|---|---|
firefox104 | --- | fixed |
People
(Reporter: me, Assigned: me)
References
(Blocks 1 open bug)
Details
Attachments
(1 file)
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0
Steps to reproduce:
compile spidermonkey to wasm and execute:
for (let i = 0; i < 5; i++)
print(Math.random());
The first two calls have the same first 3 or 4 digits
This is because js::GenerateXorShift128PlusSeed wants two seed values and uses js::GenerateRandomSeed() to get those values, js::GenerateRandomSeed() tries to get values from mozilla::RandomUint64 but that is returning Nothing(), causing js::GenerateRandomSeed() to fallback to using the current time as it's return value.
Having the same value for both seeds seems to have the unintended result that the first two calls to the PRNG have the same first 3 or 4 digits.
This can be reproduced on this page which uses spidermonkey compiled to wasm https://jandem.github.io/sm-wasi/?source=for%20(let%20i%20%3D%200%3B%20i%20%3C%205%3B%20i%2B%2B)%0A%20%20%20%20print(Math.random())%3B%0A
Actual results:
The first two calls have the same first 3 or 4 digits
Expected results:
The first two calls should not have the same first 3 or 4 digits
Assignee | ||
Comment 1•3 years ago
|
||
wasi-libc provides arc4random methods - https://github.com/WebAssembly/wasi-libc/blob/7a21011e98dd9268a33f90fd282db92cbdd1b9d1/libc-top-half/musl/include/stdlib.h#L205-L213
Without this change, js::GenerateRandomSeed will fallback to using a timestamp for the random seed (https://searchfox.org/mozilla-central/rev/3e1a721bce1da3ae04675539b39a4e95b25a046d/js/src/jsmath.cpp#491-494) which is used as both seed values within the PRNG(https://searchfox.org/mozilla-central/rev/3e1a721bce1da3ae04675539b39a4e95b25a046d/js/src/jsmath.cpp#501-502) - having the same value for both seeds seems to cause the first two PRNG calls to produce the same first 4 decimal places.
This change stops the timestamp fallback from being used when compiling to wasm/wasi which avoids this issue.
Comment 2•3 years ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::JavaScript: WebAssembly' component, and is moving the bug to that component. Please correct in case you think the bot is wrong.
Updated•3 years ago
|
Updated•3 years ago
|
Updated•3 years ago
|
Updated•3 years ago
|
Comment 4•3 years ago
|
||
bugherder |
Description
•