sodium_utils3.c (1813B)
1 2 #include <stdlib.h> 3 #include <sys/types.h> 4 5 #include <limits.h> 6 #ifdef HAVE_CATCHABLE_SEGV 7 # include <signal.h> 8 #endif 9 #ifndef _WIN32 10 # include <unistd.h> 11 #endif 12 13 #define TEST_NAME "sodium_utils3" 14 #include "cmptest.h" 15 16 #ifdef __SANITIZE_ADDRESS__ 17 # warning The sodium_utils3 test is expected to fail with address sanitizer 18 #endif 19 #ifdef __SANITIZE_THREAD__ 20 # warning The sodium_utils3 test is expected to fail with thread sanitizer 21 #endif 22 23 __attribute__((noreturn)) static void 24 segv_handler(int sig) 25 { 26 (void) sig; 27 28 printf("Intentional segfault / bus error caught\n"); 29 printf("OK\n"); 30 #ifdef SIG_DFL 31 # ifdef SIGPROT 32 signal(SIGPROT, SIG_DFL); 33 # endif 34 # ifdef SIGSEGV 35 signal(SIGSEGV, SIG_DFL); 36 # endif 37 # ifdef SIGBUS 38 signal(SIGBUS, SIG_DFL); 39 # endif 40 # ifdef SIGABRT 41 signal(SIGABRT, SIG_DFL); 42 # endif 43 #endif 44 _exit(0); 45 } 46 47 int 48 main(void) 49 { 50 void * buf; 51 size_t size; 52 53 #ifdef BENCHMARKS 54 return 0; 55 #endif 56 57 #ifdef SIG_DFL 58 # ifdef SIGPROT 59 signal(SIGPROT, segv_handler); 60 # endif 61 # ifdef SIGSEGV 62 signal(SIGSEGV, segv_handler); 63 # endif 64 # ifdef SIGBUS 65 signal(SIGBUS, segv_handler); 66 # endif 67 # ifdef SIGABRT 68 signal(SIGABRT, segv_handler); 69 # endif 70 #endif 71 size = 1U + randombytes_uniform(100000U); 72 buf = sodium_malloc(size); 73 assert(buf != NULL); 74 75 /* old versions of asan emit a warning because they don't support mlock*() */ 76 #ifndef __SANITIZE_ADDRESS__ 77 sodium_mprotect_noaccess(buf); 78 sodium_mprotect_readwrite(buf); 79 #endif 80 81 #if defined(HAVE_CATCHABLE_SEGV) && !defined(__EMSCRIPTEN__) && !defined(__SANITIZE_ADDRESS__) && !defined(__SANITIZE_THREAD__) 82 sodium_memzero(((unsigned char *) buf) - 8, 8U); 83 sodium_mprotect_readonly(buf); 84 sodium_free(buf); 85 printf("Underflow not caught\n"); 86 #else 87 segv_handler(0); 88 #endif 89 return 0; 90 }