Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions wasmtime-wasi/sandboxed-system-primitives/src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
#define CONFIG_HAS_ARC4RANDOM_BUF 0
#endif

// On Linux, prefer to use getentropy, though it isn't available in
// On Linux, prefer to use getrandom, though it isn't available in
// GLIBC before 2.25.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still the correct version?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

#if defined(__linux__) && \
(!defined(__GLIBC__) || \
__GLIBC__ > 2 || \
(__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))
#define CONFIG_HAS_GETENTROPY 1
#define CONFIG_HAS_GETRANDOM 1
#else
#define CONFIG_HAS_GETENTROPY 0
#define CONFIG_HAS_GETRANDOM 0
#endif

#if defined(__CloudABI__)
Expand Down
20 changes: 18 additions & 2 deletions wasmtime-wasi/sandboxed-system-primitives/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "random.h"
Expand All @@ -25,10 +27,24 @@ void random_buf(void *buf, size_t len) {
arc4random_buf(buf, len);
}

#elif CONFIG_HAS_GETENTROPY
#elif CONFIG_HAS_GETRANDOM

#include <sys/random.h>

void random_buf(void *buf, size_t len) {
getentropy(buf, len);
for (;;) {
ssize_t x = getrandom(buf, len, 0);
if (x < 0) {
if (errno == EINTR)
continue;
fprintf(stderr, "getrandom failed: %s", strerror(errno));
abort();
}
if (x == len)
return;
buf = (void *)((unsigned char *)buf + x);
len -= x;
}
}

#else
Expand Down