Skip to content

Commit 76b5d39

Browse files
committed
WebAssembly: use getentropy on Wasi
1 parent 9e57436 commit 76b5d39

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

stdlib/public/stubs/Random.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
#include "swift/Runtime/Mutex.h"
4343
#include "../SwiftShims/Random.h"
4444

45+
#ifdef __wasi__
46+
#include <algorithm> // std::min
47+
#endif
48+
4549
#if defined(__APPLE__)
4650

4751
SWIFT_RUNTIME_STDLIB_API
@@ -84,7 +88,7 @@ void swift::swift_stdlib_random(void *buf, __swift_size_t nbytes) {
8488
if (getrandom_available) {
8589
actual_nbytes = WHILE_EINTR(syscall(__NR_getrandom, buf, nbytes, 0));
8690
}
87-
#elif __has_include(<sys/random.h>) && (defined(__CYGWIN__) || defined(__Fuchsia__))
91+
#elif __has_include(<sys/random.h>) && (defined(__CYGWIN__) || defined(__Fuchsia__) || defined(__wasi__))
8892
__swift_size_t getentropy_nbytes = std::min(nbytes, __swift_size_t{256});
8993

9094
if (0 == getentropy(buf, getentropy_nbytes)) {

0 commit comments

Comments
 (0)