8 #define TEST_NAME "sodium_utils2"
11 #ifdef __SANITIZE_ADDRESS__
12 # warning The sodium_utils2 test is expected to fail with address sanitizer
17 #undef sodium_allocarray
19 __attribute__((noreturn)) static void
24 printf("Intentional segfault / bus error caught\n");
27 signal(SIGSEGV, SIG_DFL);
30 signal(SIGBUS, SIG_DFL);
33 signal(SIGABRT, SIG_DFL);
45 if (sodium_malloc(SIZE_MAX - 1U) != NULL) {
48 if (sodium_malloc(0U) == NULL) {
51 if (sodium_allocarray(SIZE_MAX / 2U + 1U, SIZE_MAX / 2U) != NULL) {
54 sodium_free(sodium_allocarray(0U, 0U));
55 sodium_free(sodium_allocarray(0U, 1U));
56 sodium_free(sodium_allocarray(1U, 0U));
58 buf = sodium_allocarray(1000U, 50U);
59 memset(buf, 0, 50000U);
62 sodium_free(sodium_malloc(0U));
64 for (i = 0U; i < 10000U; i++) {
65 size = 1U + randombytes_uniform(100000U);
66 buf = sodium_malloc(size);
69 sodium_mprotect_noaccess(buf);
75 signal(SIGSEGV, segv_handler);
78 signal(SIGBUS, segv_handler);
81 signal(SIGABRT, segv_handler);
83 size = 1U + randombytes_uniform(100000U);
84 buf = sodium_malloc(size);
87 /* old versions of asan emit a warning because they don't support mlock*() */
88 #ifndef __SANITIZE_ADDRESS__
89 sodium_mprotect_readonly(buf);
90 sodium_mprotect_readwrite(buf);
93 #if defined(HAVE_CATCHABLE_SEGV) && !defined(__EMSCRIPTEN__) && !defined(__SANITIZE_ADDRESS__)
94 sodium_memzero(((unsigned char *) buf) + size, 1U);
95 sodium_mprotect_noaccess(buf);
97 printf("Overflow not caught\n");