libsodium 1.0.8
[tomato.git] / release / src / router / libsodium / test / default / sodium_utils2.c
blob7bf1b30fe6c541ff8ee21baa0b944a194700c11e
2 #include <stdlib.h>
3 #include <sys/types.h>
5 #include <limits.h>
6 #include <signal.h>
8 #define TEST_NAME "sodium_utils2"
9 #include "cmptest.h"
11 #ifdef __SANITIZE_ADDRESS__
12 # warning The sodium_utils2 test is expected to fail with address sanitizer
13 #endif
15 static void segv_handler(int sig)
17 printf("Intentional segfault / bus error caught\n");
18 printf("OK\n");
19 #ifdef SIGSEGV
20 signal(SIGSEGV, SIG_DFL);
21 #endif
22 #ifdef SIGBUS
23 signal(SIGBUS, SIG_DFL);
24 #endif
25 #ifdef SIGABRT
26 signal(SIGABRT, SIG_DFL);
27 #endif
28 exit(0);
31 int main(void)
33 void *buf;
34 size_t size;
35 unsigned int i;
37 if (sodium_malloc(SIZE_MAX - 1U) != NULL) {
38 return 1;
40 if (sodium_allocarray(SIZE_MAX / 2U + 1U, SIZE_MAX / 2U) != NULL) {
41 return 1;
43 sodium_free(sodium_allocarray(0U, 0U));
44 sodium_free(sodium_allocarray(0U, 1U));
45 sodium_free(sodium_allocarray(1U, 0U));
47 buf = sodium_allocarray(1000U, 50U);
48 memset(buf, 0, 50000U);
49 sodium_free(buf);
51 sodium_free(sodium_malloc(0U));
52 sodium_free(NULL);
53 for (i = 0U; i < 10000U; i++) {
54 size = randombytes_uniform(100000U);
55 buf = sodium_malloc(size);
56 assert(buf != NULL);
57 memset(buf, i, size);
58 sodium_mprotect_noaccess(buf);
59 sodium_free(buf);
61 printf("OK\n");
63 #ifdef SIGSEGV
64 signal(SIGSEGV, segv_handler);
65 #endif
66 #ifdef SIGBUS
67 signal(SIGBUS, segv_handler);
68 #endif
69 #ifdef SIGABRT
70 signal(SIGABRT, segv_handler);
71 #endif
72 size = randombytes_uniform(100000U);
73 buf = sodium_malloc(size);
74 assert(buf != NULL);
75 sodium_mprotect_readonly(buf);
76 sodium_mprotect_readwrite(buf);
77 #ifndef __EMSCRIPTEN__
78 sodium_memzero(((unsigned char *)buf) + size, 1U);
79 sodium_mprotect_noaccess(buf);
80 sodium_free(buf);
81 printf("Overflow not caught\n");
82 #endif
83 return 0;