From cbd945052246acfdbfad7fec2aa9c6c12affa67c Mon Sep 17 00:00:00 2001 From: Mitch Foley Date: Thu, 31 Oct 2024 14:31:28 -0400 Subject: [PATCH 1/2] implement getentropy via __wasi_random_get. --- system/lib/standalone/standalone.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/lib/standalone/standalone.c b/system/lib/standalone/standalone.c index 14936ea97dd61..b4039626f175f 100644 --- a/system/lib/standalone/standalone.c +++ b/system/lib/standalone/standalone.c @@ -139,7 +139,7 @@ weak int __syscall_lstat64(intptr_t path, intptr_t buf) { // There is no good source of entropy without an import. Make this weak so that // it can be replaced with a pRNG or a proper import. weak int getentropy(void* buffer, size_t length) { - abort(); + return __wasi_random_get(buffer, length); } // Emscripten additions From 985b7bab3f4e31743a9446d25b6ce639f2bcd6d6 Mon Sep 17 00:00:00 2001 From: Mitch Foley Date: Thu, 31 Oct 2024 15:09:29 -0400 Subject: [PATCH 2/2] match wasi-libc's implementation --- system/lib/standalone/standalone.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/system/lib/standalone/standalone.c b/system/lib/standalone/standalone.c index b4039626f175f..51753e59fa6bb 100644 --- a/system/lib/standalone/standalone.c +++ b/system/lib/standalone/standalone.c @@ -136,10 +136,22 @@ weak int __syscall_lstat64(intptr_t path, intptr_t buf) { return -ENOSYS; } -// There is no good source of entropy without an import. Make this weak so that -// it can be replaced with a pRNG or a proper import. +// copied from wasi-libc's getentropy.c: +// https://github.com/WebAssembly/wasi-libc/blob/main/libc-bottom-half/sources/getentropy.c weak int getentropy(void* buffer, size_t length) { - return __wasi_random_get(buffer, length); + if (length > 256) { + errno = EIO; + return -1; + } + + int r = __wasi_random_get(buffer, length); + + if (r != 0) { + errno = r; + return -1; + } + + return 0; } // Emscripten additions