diff --git a/configure.ac b/configure.ac index ce76c172..28c5f14e 100644 --- a/configure.ac +++ b/configure.ac @@ -295,6 +295,8 @@ AX_CHECK_LINK_FLAG([-Wl,-z,relro], [LDFLAGS="$LDFLAGS -Wl,-z,relro"]) AX_CHECK_LINK_FLAG([-Wl,-z,now], [LDFLAGS="$LDFLAGS -Wl,-z,now"]) AX_CHECK_LINK_FLAG([-Wl,-z,noexecstack], [LDFLAGS="$LDFLAGS -Wl,-z,noexecstack"]) +AX_CHECK_CATCHABLE_SEGV + LT_INIT AC_SUBST(LIBTOOL_DEPS) diff --git a/m4/ax_check_catchable_segv.m4 b/m4/ax_check_catchable_segv.m4 new file mode 100644 index 00000000..ddfc1dc0 --- /dev/null +++ b/m4/ax_check_catchable_segv.m4 @@ -0,0 +1,42 @@ +# SYNOPSIS +# +# AX_CHECK_CATCHABLE_SEGV +# +# DESCRIPTION +# +# Check whether segmentation violations can be caught using signal handlers. + +#serial 1 + +AC_DEFUN([AX_CHECK_CATCHABLE_SEGV], [dnl + AC_PREREQ(2.64) + AS_VAR_PUSHDEF([CACHEVAR], [ax_cv_check_[]_AC_LANG_ABBREV[]CATCHABLE_SEGV])dnl + AC_CACHE_CHECK([whether segmentation violations can be caught when using the _AC_LANG compiler], CACHEVAR, [ + AC_RUN_IFELSE([ + AC_LANG_PROGRAM([[ +#include +#include +static void sig(int _) { exit(0); } + ]], [[ +volatile unsigned char * volatile x = malloc(8); +size_t i; + +signal(SIGSEGV, sig); +signal(SIGBUS, sig); +#ifndef __SANITIZE_ADDRESS__ +for (i = 0; i < 10000000; i += 1024) { x[-i] = x[i] = (unsigned char) i; } +#endif +free((void *) x); +exit(1) + ]])], + [AS_VAR_SET(CACHEVAR, [yes])], + [AS_VAR_SET(CACHEVAR, [no])], + [AS_VAR_SET(CACHEVAR, [unknown])] + ) + ]) + AS_VAR_IF(CACHEVAR, yes, + [AC_DEFINE([HAVE_CATCHABLE_SEGV], [1], [Define if segmentation violations can be caught using signal handlers])], + [AC_MSG_WARN([On this platform, segmentation violations cannot be caught using signal handlers. This is expected if you enabled a tool such as Address Sanitizer (-fsanitize=address), but be aware that using Address Sanitizer may also significantly reduce performance.])] + ) + AS_VAR_POPDEF([CACHEVAR])dnl +]) diff --git a/test/default/sodium_utils2.c b/test/default/sodium_utils2.c index e688a3ba..19c9b007 100644 --- a/test/default/sodium_utils2.c +++ b/test/default/sodium_utils2.c @@ -9,7 +9,7 @@ #include "cmptest.h" #ifdef __SANITIZE_ADDRESS__ -#warning The sodium_utils2 test is expected to fail with address sanitizer +# warning The sodium_utils2 test is expected to fail with address sanitizer #endif __attribute__((noreturn)) static void @@ -81,7 +81,7 @@ main(void) assert(buf != NULL); sodium_mprotect_readonly(buf); sodium_mprotect_readwrite(buf); -#ifndef __EMSCRIPTEN__ +#if defined(HAVE_CATCHABLE_SEGV) && !defined(__EMSCRIPTEN__) && !defined(__SANITIZE_ADDRESS__) sodium_memzero(((unsigned char *) buf) + size, 1U); sodium_mprotect_noaccess(buf); sodium_free(buf); diff --git a/test/default/sodium_utils3.c b/test/default/sodium_utils3.c index 0bb98f26..778946a0 100644 --- a/test/default/sodium_utils3.c +++ b/test/default/sodium_utils3.c @@ -9,7 +9,7 @@ #include "cmptest.h" #ifdef __SANITIZE_ADDRESS__ -#warning The sodium_utils3 test is expected to fail with address sanitizer +# warning The sodium_utils3 test is expected to fail with address sanitizer #endif __attribute__((noreturn)) static void @@ -51,7 +51,7 @@ main(void) assert(buf != NULL); sodium_mprotect_noaccess(buf); sodium_mprotect_readwrite(buf); -#ifndef __EMSCRIPTEN__ +#if defined(HAVE_CATCHABLE_SEGV) && !defined(__EMSCRIPTEN__) && !defined(__SANITIZE_ADDRESS__) sodium_memzero(((unsigned char *) buf) - 8, 8U); sodium_mprotect_readonly(buf); sodium_free(buf);