From 64129657a5c67f3bab84562aa8d57dacc685cc75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20P=2E=20Tjern=C3=B8?= Date: Thu, 16 Sep 2021 11:01:24 -0700 Subject: [PATCH] Portability improvements (#1105) * Move `raise` code to be behind an autoconf check This moves the call to `raise` behind a `HAVE_RAISE` autoconf check, in addition to `__wasm__`. This is intended to help porting to other platforms that don't support `raise` (e.g. modern game consoles). * Add autoconf check for `sysconf` Only try to invoke `sysconf` if the target platform supports it, and don't warn about unknown page size if `PAGE_SIZE` was defined. Add an include for `sys/param.h` to increase likelihood of finding `PAGE_SIZE`. This is intended to help porting to other platforms that don't support `sysconf` (e.g. modern game consoles) that have a fixed hardware page size. * Don't try to use raise & sysconf in a WASI environment Co-authored-by: Frank Denis <124872+jedisct1@users.noreply.github.com> --- configure.ac | 5 +++-- src/libsodium/sodium/utils.c | 12 ++++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 993662e1..20f1c551 100644 --- a/configure.ac +++ b/configure.ac @@ -594,7 +594,7 @@ AC_SUBST(CFLAGS_AESNI) AC_SUBST(CFLAGS_PCLMUL) AC_SUBST(CFLAGS_RDRAND) -AC_CHECK_HEADERS([sys/mman.h sys/random.h intrin.h sys/auxv.h]) +AC_CHECK_HEADERS([sys/mman.h sys/param.h sys/random.h intrin.h sys/auxv.h]) AC_MSG_CHECKING([if _xgetbv() is available]) AC_LINK_IFELSE( @@ -820,8 +820,9 @@ AS_IF([test "x$EMSCRIPTEN" = "x"],[ AC_CHECK_FUNCS([arc4random arc4random_buf]) AS_IF([test "x$WASI" = "x"],[ AC_CHECK_FUNCS([mmap mlock madvise mprotect]) + AC_CHECK_FUNCS([raise]) + AC_CHECK_FUNCS([sysconf]) ]) - AC_MSG_CHECKING(for getrandom with a standard API) AC_LINK_IFELSE([AC_LANG_PROGRAM([[ #include diff --git a/src/libsodium/sodium/utils.c b/src/libsodium/sodium/utils.c index 3c2de173..12503dc3 100644 --- a/src/libsodium/sodium/utils.c +++ b/src/libsodium/sodium/utils.c @@ -9,7 +9,7 @@ #include #include -#ifndef __wasm__ +#if defined(HAVE_RAISE) && !defined(__wasm__) # include #endif @@ -17,6 +17,10 @@ # include #endif +#ifdef HAVE_SYS_PARAM_H +# include +#endif + #ifdef _WIN32 # include # include @@ -402,7 +406,7 @@ int _sodium_alloc_init(void) { #ifdef HAVE_ALIGNED_MALLOC -# if defined(_SC_PAGESIZE) +# if defined(_SC_PAGESIZE) && defined(HAVE_SYSCONF) long page_size_ = sysconf(_SC_PAGESIZE); if (page_size_ > 0L) { page_size = (size_t) page_size_; @@ -411,7 +415,7 @@ _sodium_alloc_init(void) SYSTEM_INFO si; GetSystemInfo(&si); page_size = (size_t) si.dwPageSize; -# else +# elif !defined(PAGE_SIZE) # warning Unknown page size # endif if (page_size < CANARY_SIZE || page_size < sizeof(size_t)) { @@ -503,7 +507,7 @@ _mprotect_readwrite(void *ptr, size_t size) __attribute__((noreturn)) static void _out_of_bounds(void) { -# ifndef __wasm__ +# if defined(HAVE_RAISE) && !defined(__wasm__) # ifdef SIGSEGV raise(SIGSEGV); # elif defined(SIGKILL)