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_utils2"
      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_utils2 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     unsigned int i;
     40  1.1  riastrad 
     41  1.1  riastrad     if (sodium_malloc(SIZE_MAX - 1U) != NULL) {
     42  1.1  riastrad         return 1;
     43  1.1  riastrad     }
     44  1.1  riastrad     if (sodium_malloc(0U) == NULL) {
     45  1.1  riastrad         return 1;
     46  1.1  riastrad     }
     47  1.1  riastrad     if (sodium_allocarray(SIZE_MAX / 2U + 1U, SIZE_MAX / 2U) != NULL) {
     48  1.1  riastrad         return 1;
     49  1.1  riastrad     }
     50  1.1  riastrad     sodium_free(sodium_allocarray(0U, 0U));
     51  1.1  riastrad     sodium_free(sodium_allocarray(0U, 1U));
     52  1.1  riastrad     sodium_free(sodium_allocarray(1U, 0U));
     53  1.1  riastrad 
     54  1.1  riastrad     buf = sodium_allocarray(1000U, 50U);
     55  1.1  riastrad     memset(buf, 0, 50000U);
     56  1.1  riastrad     sodium_free(buf);
     57  1.1  riastrad 
     58  1.1  riastrad     sodium_free(sodium_malloc(0U));
     59  1.1  riastrad     sodium_free(NULL);
     60  1.1  riastrad     for (i = 0U; i < 10000U; i++) {
     61  1.1  riastrad         size = 1U + randombytes_uniform(100000U);
     62  1.1  riastrad         buf  = sodium_malloc(size);
     63  1.1  riastrad         assert(buf != NULL);
     64  1.1  riastrad         memset(buf, i, size);
     65  1.1  riastrad         sodium_mprotect_noaccess(buf);
     66  1.1  riastrad         sodium_free(buf);
     67  1.1  riastrad     }
     68  1.1  riastrad     printf("OK\n");
     69  1.1  riastrad 
     70  1.1  riastrad #ifdef SIGSEGV
     71  1.1  riastrad     signal(SIGSEGV, segv_handler);
     72  1.1  riastrad #endif
     73  1.1  riastrad #ifdef SIGBUS
     74  1.1  riastrad     signal(SIGBUS, segv_handler);
     75  1.1  riastrad #endif
     76  1.1  riastrad #ifdef SIGABRT
     77  1.1  riastrad     signal(SIGABRT, segv_handler);
     78  1.1  riastrad #endif
     79  1.1  riastrad     size = 1U + randombytes_uniform(100000U);
     80  1.1  riastrad     buf  = sodium_malloc(size);
     81  1.1  riastrad     assert(buf != NULL);
     82  1.1  riastrad 
     83  1.1  riastrad /* old versions of asan emit a warning because they don't support mlock*() */
     84  1.1  riastrad #ifndef __SANITIZE_ADDRESS__
     85  1.1  riastrad     sodium_mprotect_readonly(buf);
     86  1.1  riastrad     sodium_mprotect_readwrite(buf);
     87  1.1  riastrad #endif
     88  1.1  riastrad 
     89  1.1  riastrad #if defined(HAVE_CATCHABLE_SEGV) && !defined(__EMSCRIPTEN__) && !defined(__SANITIZE_ADDRESS__)
     90  1.1  riastrad     sodium_memzero(((unsigned char *) buf) + size, 1U);
     91  1.1  riastrad     sodium_mprotect_noaccess(buf);
     92  1.1  riastrad     sodium_free(buf);
     93  1.1  riastrad     printf("Overflow not caught\n");
     94  1.1  riastrad #else
     95  1.1  riastrad     segv_handler(0);
     96  1.1  riastrad #endif
     97  1.1  riastrad     return 0;
     98  1.1  riastrad }
     99