Home | History | Annotate | Line # | Download | only in default
      1  1.1  riastrad 
      2  1.1  riastrad #include <stdlib.h>
      3  1.1  riastrad #include <sys/types.h>
      4  1.1  riastrad 
      5  1.1  riastrad #include <limits.h>
      6  1.1  riastrad #include <signal.h>
      7  1.1  riastrad 
      8  1.1  riastrad #define TEST_NAME "sodium_utils3"
      9  1.1  riastrad #include "cmptest.h"
     10  1.1  riastrad 
     11  1.1  riastrad #ifdef __SANITIZE_ADDRESS__
     12  1.1  riastrad # warning The sodium_utils3 test is expected to fail with address sanitizer
     13  1.1  riastrad #endif
     14  1.1  riastrad 
     15  1.1  riastrad __attribute__((noreturn)) static void
     16  1.1  riastrad segv_handler(int sig)
     17  1.1  riastrad {
     18  1.1  riastrad     (void) sig;
     19  1.1  riastrad 
     20  1.1  riastrad     printf("Intentional segfault / bus error caught\n");
     21  1.1  riastrad     printf("OK\n");
     22  1.1  riastrad #ifdef SIGSEGV
     23  1.1  riastrad     signal(SIGSEGV, SIG_DFL);
     24  1.1  riastrad #endif
     25  1.1  riastrad #ifdef SIGBUS
     26  1.1  riastrad     signal(SIGBUS, SIG_DFL);
     27  1.1  riastrad #endif
     28  1.1  riastrad #ifdef SIGABRT
     29  1.1  riastrad     signal(SIGABRT, SIG_DFL);
     30  1.1  riastrad #endif
     31  1.1  riastrad     exit(0);
     32  1.1  riastrad }
     33  1.1  riastrad 
     34  1.1  riastrad int
     35  1.1  riastrad main(void)
     36  1.1  riastrad {
     37  1.1  riastrad     void * buf;
     38  1.1  riastrad     size_t size;
     39  1.1  riastrad 
     40  1.1  riastrad #ifdef SIGSEGV
     41  1.1  riastrad     signal(SIGSEGV, segv_handler);
     42  1.1  riastrad #endif
     43  1.1  riastrad #ifdef SIGBUS
     44  1.1  riastrad     signal(SIGBUS, segv_handler);
     45  1.1  riastrad #endif
     46  1.1  riastrad #ifdef SIGABRT
     47  1.1  riastrad     signal(SIGABRT, segv_handler);
     48  1.1  riastrad #endif
     49  1.1  riastrad     size = 1U + randombytes_uniform(100000U);
     50  1.1  riastrad     buf  = sodium_malloc(size);
     51  1.1  riastrad     assert(buf != NULL);
     52  1.1  riastrad 
     53  1.1  riastrad /* old versions of asan emit a warning because they don't support mlock*() */
     54  1.1  riastrad #ifndef __SANITIZE_ADDRESS__
     55  1.1  riastrad     sodium_mprotect_noaccess(buf);
     56  1.1  riastrad     sodium_mprotect_readwrite(buf);
     57  1.1  riastrad #endif
     58  1.1  riastrad 
     59  1.1  riastrad #if defined(HAVE_CATCHABLE_SEGV) && !defined(__EMSCRIPTEN__) && !defined(__SANITIZE_ADDRESS__)
     60  1.1  riastrad     sodium_memzero(((unsigned char *) buf) - 8, 8U);
     61  1.1  riastrad     sodium_mprotect_readonly(buf);
     62  1.1  riastrad     sodium_free(buf);
     63  1.1  riastrad     printf("Underflow not caught\n");
     64  1.1  riastrad #else
     65  1.1  riastrad     segv_handler(0);
     66  1.1  riastrad #endif
     67  1.1  riastrad     return 0;
     68  1.1  riastrad }
     69