From 07c49f832e00d78c0e3cd67976e335aed005a9a6 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Tue, 24 Sep 2024 18:38:11 +0200 Subject: [PATCH] Safer seed of rand with WASM. --- lib/std/math/random/math.seeder.c3 | 15 ++++++++++++++- releasenotes.md | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) 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.