diff --git a/lib/std/math/random/math.seeder.c3 b/lib/std/math/random/math.seeder.c3 index 562e32c62..acd5ffc1a 100644 --- a/lib/std/math/random/math.seeder.c3 +++ b/lib/std/math/random/math.seeder.c3 @@ -72,8 +72,9 @@ macro uint hash(value) @local return fnv32a::encode(&&bitcast(value, char[$sizeof(value)])); } -fn char[8 * 4] entropy() +fn char[8 * 4] entropy() @if(!env::WASM_NOLIBC) { + void* addr = malloc(1); free(addr); static uint random_int; @@ -89,4 +90,16 @@ fn char[8 * 4] entropy() hash(allocator::heap()) }; return bitcast(entropy_data, char[8 * 4]); +} + +fn char[8 * 4] entropy() @if(env::WASM_NOLIBC) +{ + static uint random_int; + random_int += 0xedf19156; + uint[8] entropy_data = { + hash($$TIME), + hash(&entropy), + random_int, + }; + return bitcast(entropy_data, char[8 * 4]); } \ No newline at end of file diff --git a/releasenotes.md b/releasenotes.md index 7a48a16bf..d6a89eb37 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -56,6 +56,7 @@ - Fix bug when passing a type as a compile time value. - Fix bug due to enum associated values not being checked for liveness. - Regression when compile time accessing a union field not last assigned to. +- Safer seed of rand() for WASM without libc. ### Stdlib changes - Additional init functions for hashmap.