sodium_utils2.c (2559B)
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_utils2" 14 #include "cmptest.h" 15 16 #ifdef __SANITIZE_ADDRESS__ 17 # warning The sodium_utils2 test is expected to fail with address sanitizer 18 #endif 19 20 #undef sodium_malloc 21 #undef sodium_free 22 #undef sodium_allocarray 23 24 __attribute__((noreturn)) static void 25 segv_handler(int sig) 26 { 27 (void) sig; 28 29 printf("Intentional segfault / bus error caught\n"); 30 printf("OK\n"); 31 #ifdef SIG_DFL 32 # ifdef SIGPROT 33 signal(SIGPROT, SIG_DFL); 34 # endif 35 # ifdef SIGSEGV 36 signal(SIGSEGV, SIG_DFL); 37 # endif 38 # ifdef SIGBUS 39 signal(SIGBUS, SIG_DFL); 40 # endif 41 # ifdef SIGABRT 42 signal(SIGABRT, SIG_DFL); 43 # endif 44 #endif 45 _exit(0); 46 } 47 48 int 49 main(void) 50 { 51 void *buf; 52 size_t size; 53 unsigned int i; 54 55 #ifdef BENCHMARKS 56 return 0; 57 #endif 58 59 if (sodium_malloc(SIZE_MAX - 1U) != NULL) { 60 return 1; 61 } 62 if (sodium_malloc(0U) == NULL) { 63 return 1; 64 } 65 if (sodium_allocarray(SIZE_MAX / 2U + 1U, SIZE_MAX / 2U) != NULL) { 66 return 1; 67 } 68 sodium_free(sodium_allocarray(0U, 0U)); 69 sodium_free(sodium_allocarray(0U, 1U)); 70 sodium_free(sodium_allocarray(1U, 0U)); 71 72 buf = sodium_allocarray(1000U, 50U); 73 memset(buf, 0, 50000U); 74 sodium_free(buf); 75 76 sodium_free(sodium_malloc(0U)); 77 sodium_free(NULL); 78 for (i = 0U; i < 10000U; i++) { 79 size = 1U + randombytes_uniform(100000U); 80 buf = sodium_malloc(size); 81 assert(buf != NULL); 82 memset(buf, i, size); 83 sodium_mprotect_noaccess(buf); 84 sodium_free(buf); 85 } 86 printf("OK\n"); 87 #ifdef SIG_DFL 88 # ifdef SIGPROT 89 signal(SIGPROT, segv_handler); 90 # endif 91 # ifdef SIGSEGV 92 signal(SIGSEGV, segv_handler); 93 # endif 94 # ifdef SIGBUS 95 signal(SIGBUS, segv_handler); 96 # endif 97 # ifdef SIGABRT 98 signal(SIGABRT, segv_handler); 99 # endif 100 #endif 101 size = 1U + randombytes_uniform(100000U); 102 buf = sodium_malloc(size); 103 assert(buf != NULL); 104 105 /* old versions of asan emit a warning because they don't support mlock*() */ 106 #ifndef __SANITIZE_ADDRESS__ 107 sodium_mprotect_readonly(buf); 108 sodium_mprotect_readwrite(buf); 109 #endif 110 111 #if defined(HAVE_CATCHABLE_SEGV) && !defined(__EMSCRIPTEN__) && !defined(__SANITIZE_ADDRESS__) 112 sodium_memzero(((unsigned char *) buf) + size, 1U); 113 sodium_mprotect_noaccess(buf); 114 sodium_free(buf); 115 printf("Overflow not caught\n"); 116 #else 117 segv_handler(0); 118 #endif 119 return 0; 120 }