Safer seed of rand with WASM.

This commit is contained in:
Christoffer Lerno
2024-09-24 18:38:11 +02:00
parent 01b087238a
commit 07c49f832e
2 changed files with 15 additions and 1 deletions

View File

@@ -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]);
}

View File

@@ -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.