Handle old OSX/iOS without getentropy().

This commit is contained in:
Mike Pall 2020-07-13 11:54:08 +02:00
parent 60ac12ed6f
commit 570e758ca7

View File

@ -107,7 +107,19 @@ static PRGR libfunc_rgr;
#if LJ_TARGET_LINUX #if LJ_TARGET_LINUX
/* Avoid a dependency on glibc 2.25+ and use the getrandom syscall instead. */ /* Avoid a dependency on glibc 2.25+ and use the getrandom syscall instead. */
#include <sys/syscall.h> #include <sys/syscall.h>
#elif LJ_TARGET_OSX || LJ_TARGET_BSD || LJ_TARGET_SOLARIS || LJ_TARGET_CYGWIN #else
#if LJ_TARGET_OSX
#include <Availability.h>
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200 || \
__IPHONE_OS_VERSION_MIN_REQUIRED >= 100000
#define LJ_TARGET_HAS_GETENTROPY 1
#endif
#elif LJ_TARGET_BSD || LJ_TARGET_SOLARIS || LJ_TARGET_CYGWIN
#define LJ_TARGET_HAS_GETENTROPY 1
#endif
#if LJ_TARGET_HAS_GETENTROPY
extern int getentropy(void *buf, size_t len); extern int getentropy(void *buf, size_t len);
#ifdef __ELF__ #ifdef __ELF__
__attribute__((weak)) __attribute__((weak))
@ -115,6 +127,8 @@ extern int getentropy(void *buf, size_t len);
; ;
#endif #endif
#endif
/* For the /dev/urandom fallback. */ /* For the /dev/urandom fallback. */
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
@ -181,7 +195,7 @@ int LJ_FASTCALL lj_prng_seed_secure(PRNGState *rs)
if (syscall(SYS_getrandom, rs->u, sizeof(rs->u), 0) == (long)sizeof(rs->u)) if (syscall(SYS_getrandom, rs->u, sizeof(rs->u), 0) == (long)sizeof(rs->u))
goto ok; goto ok;
#elif LJ_TARGET_OSX || LJ_TARGET_BSD || LJ_TARGET_SOLARIS || LJ_TARGET_CYGWIN #elif LJ_TARGET_HAS_GETENTROPY
#ifdef __ELF__ #ifdef __ELF__
if (getentropy && getentropy(rs->u, sizeof(rs->u)) == 0) if (getentropy && getentropy(rs->u, sizeof(rs->u)) == 0)