|
| 1 | +From 0b6d3fa928f5c6f0b77c5c77ca771a0260f2e8a4 Mon Sep 17 00:00:00 2001 |
| 2 | +From: " [email protected]" <Wasm Labs Team> |
| 3 | +Date: Thu, 13 Apr 2023 10:21:04 +0200 |
| 4 | +Subject: [PATCH 18/18] fix random_bytes failing on Windows |
| 5 | + |
| 6 | + |
| 7 | + 100.0% ext/random/ |
| 8 | +diff --git a/ext/random/random.c b/ext/random/random.c |
| 9 | +index 161eb8e6..12f431ca 100644 |
| 10 | +--- a/ext/random/random.c |
| 11 | ++++ b/ext/random/random.c |
| 12 | +@@ -510,7 +510,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) |
| 13 | + #else |
| 14 | + size_t read_bytes = 0; |
| 15 | + ssize_t n; |
| 16 | +-# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || defined(__sun) |
| 17 | ++# if (defined(__linux__) && defined(SYS_getrandom)) || (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || (defined(__DragonFly__) && __DragonFly_version >= 500700) || defined(__sun) || defined(__wasi__) |
| 18 | + /* Linux getrandom(2) syscall or FreeBSD/DragonFlyBSD getrandom(2) function*/ |
| 19 | + /* Keep reading until we get enough entropy */ |
| 20 | + while (read_bytes < size) { |
| 21 | +@@ -527,6 +527,14 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) |
| 22 | + size_t amount_to_read = size - read_bytes; |
| 23 | + # if defined(__linux__) |
| 24 | + n = syscall(SYS_getrandom, bytes + read_bytes, amount_to_read, 0); |
| 25 | ++# elif defined(__wasi__) |
| 26 | ++ // getentropy always reads the amount requested on success (0) |
| 27 | ++ // and returns -1 otherwise |
| 28 | ++ if (getentropy(bytes + read_bytes, amount_to_read) == 0) { |
| 29 | ++ n = amount_to_read; |
| 30 | ++ } else { |
| 31 | ++ n = -1; |
| 32 | ++ } |
| 33 | + # else |
| 34 | + n = getrandom(bytes + read_bytes, amount_to_read, 0); |
| 35 | + # endif |
| 36 | +@@ -560,7 +568,7 @@ PHPAPI int php_random_bytes(void *bytes, size_t size, bool should_throw) |
| 37 | + |
| 38 | + if (fd < 0) { |
| 39 | + errno = 0; |
| 40 | +-# if HAVE_DEV_URANDOM |
| 41 | ++# if defined(HAVE_DEV_URANDOM) && !defined(__wasi__) |
| 42 | + fd = open("/dev/urandom", O_RDONLY); |
| 43 | + # endif |
| 44 | + if (fd < 0) { |
| 45 | +-- |
| 46 | +2.40.0 |
| 47 | + |
0 commit comments