1
mirror of https://github.com/jedisct1/libsodium.git synced 2024-12-19 10:05:05 -07:00

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>
This commit is contained in:
Jørgen P. Tjernø 2021-09-16 11:01:24 -07:00 committed by GitHub
parent fe1d6d11aa
commit 64129657a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -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 <stdlib.h>

View File

@ -9,7 +9,7 @@
#include <stdlib.h>
#include <string.h>
#ifndef __wasm__
#if defined(HAVE_RAISE) && !defined(__wasm__)
# include <signal.h>
#endif
@ -17,6 +17,10 @@
# include <sys/mman.h>
#endif
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef _WIN32
# include <windows.h>
# include <wincrypt.h>
@ -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)