You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.6 KiB

  1. #include <sys/types.h>
  2. #include <limits.h>
  3. #include <signal.h>
  4. #define TEST_NAME "sodium_utils2"
  5. #include "cmptest.h"
  6. #ifdef __SANITIZE_ADDRESS__
  7. # error This test requires address sanitizer to be off
  8. #endif
  9. static void segv_handler(int sig)
  10. {
  11. printf("Intentional segfault / bus error caught\n");
  12. printf("OK\n");
  13. #ifdef SIGSEGV
  14. signal(SIGSEGV, SIG_DFL);
  15. #endif
  16. #ifdef SIGBUS
  17. signal(SIGBUS, SIG_DFL);
  18. #endif
  19. #ifdef SIGABRT
  20. signal(SIGABRT, SIG_DFL);
  21. #endif
  22. exit(0);
  23. }
  24. int main(void)
  25. {
  26. void *buf;
  27. size_t size;
  28. unsigned int i;
  29. if (sodium_malloc(SIZE_MAX - 1U) != NULL) {
  30. return 1;
  31. }
  32. if (sodium_allocarray(SIZE_MAX / 2U + 1U, SIZE_MAX / 2U) != NULL) {
  33. return 1;
  34. }
  35. buf = sodium_allocarray(1000U, 50U);
  36. memset(buf, 0, 50000U);
  37. sodium_free(buf);
  38. sodium_free(sodium_malloc(0U));
  39. sodium_free(NULL);
  40. for (i = 0U; i < 10000U; i++) {
  41. size = randombytes_uniform(100000U);
  42. buf = sodium_malloc(size);
  43. memset(buf, i, size);
  44. sodium_mprotect_readonly(buf);
  45. sodium_free(buf);
  46. }
  47. printf("OK\n");
  48. #ifdef SIGSEGV
  49. signal(SIGSEGV, segv_handler);
  50. #endif
  51. #ifdef SIGBUS
  52. signal(SIGBUS, segv_handler);
  53. #endif
  54. #ifdef SIGABRT
  55. signal(SIGABRT, segv_handler);
  56. #endif
  57. size = randombytes_uniform(100000U);
  58. buf = sodium_malloc(size);
  59. sodium_mprotect_readonly(buf);
  60. sodium_mprotect_readwrite(buf);
  61. sodium_memzero(((unsigned char *)buf) + size, 1U);
  62. sodium_mprotect_noaccess(buf);
  63. sodium_free(buf);
  64. printf("Overflow not caught\n");
  65. return 0;
  66. }