safety_check.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/safety_check.h"
  3. /*
  4. * Note that we get called through safety_check.sh, which turns on sampling for
  5. * everything.
  6. */
  7. bool fake_abort_called;
  8. void fake_abort(const char *message) {
  9. (void)message;
  10. fake_abort_called = true;
  11. }
  12. static void
  13. buffer_overflow_write(char *ptr, size_t size) {
  14. /* Avoid overflow warnings. */
  15. volatile size_t idx = size;
  16. ptr[idx] = 0;
  17. }
  18. TEST_BEGIN(test_malloc_free_overflow) {
  19. test_skip_if(!config_prof);
  20. test_skip_if(!config_opt_safety_checks);
  21. safety_check_set_abort(&fake_abort);
  22. /* Buffer overflow! */
  23. char* ptr = malloc(128);
  24. buffer_overflow_write(ptr, 128);
  25. free(ptr);
  26. safety_check_set_abort(NULL);
  27. expect_b_eq(fake_abort_called, true, "Redzone check didn't fire.");
  28. fake_abort_called = false;
  29. }
  30. TEST_END
  31. TEST_BEGIN(test_mallocx_dallocx_overflow) {
  32. test_skip_if(!config_prof);
  33. test_skip_if(!config_opt_safety_checks);
  34. safety_check_set_abort(&fake_abort);
  35. /* Buffer overflow! */
  36. char* ptr = mallocx(128, 0);
  37. buffer_overflow_write(ptr, 128);
  38. dallocx(ptr, 0);
  39. safety_check_set_abort(NULL);
  40. expect_b_eq(fake_abort_called, true, "Redzone check didn't fire.");
  41. fake_abort_called = false;
  42. }
  43. TEST_END
  44. TEST_BEGIN(test_malloc_sdallocx_overflow) {
  45. test_skip_if(!config_prof);
  46. test_skip_if(!config_opt_safety_checks);
  47. safety_check_set_abort(&fake_abort);
  48. /* Buffer overflow! */
  49. char* ptr = malloc(128);
  50. buffer_overflow_write(ptr, 128);
  51. sdallocx(ptr, 128, 0);
  52. safety_check_set_abort(NULL);
  53. expect_b_eq(fake_abort_called, true, "Redzone check didn't fire.");
  54. fake_abort_called = false;
  55. }
  56. TEST_END
  57. TEST_BEGIN(test_realloc_overflow) {
  58. test_skip_if(!config_prof);
  59. test_skip_if(!config_opt_safety_checks);
  60. safety_check_set_abort(&fake_abort);
  61. /* Buffer overflow! */
  62. char* ptr = malloc(128);
  63. buffer_overflow_write(ptr, 128);
  64. ptr = realloc(ptr, 129);
  65. safety_check_set_abort(NULL);
  66. free(ptr);
  67. expect_b_eq(fake_abort_called, true, "Redzone check didn't fire.");
  68. fake_abort_called = false;
  69. }
  70. TEST_END
  71. TEST_BEGIN(test_rallocx_overflow) {
  72. test_skip_if(!config_prof);
  73. test_skip_if(!config_opt_safety_checks);
  74. safety_check_set_abort(&fake_abort);
  75. /* Buffer overflow! */
  76. char* ptr = malloc(128);
  77. buffer_overflow_write(ptr, 128);
  78. ptr = rallocx(ptr, 129, 0);
  79. safety_check_set_abort(NULL);
  80. free(ptr);
  81. expect_b_eq(fake_abort_called, true, "Redzone check didn't fire.");
  82. fake_abort_called = false;
  83. }
  84. TEST_END
  85. TEST_BEGIN(test_xallocx_overflow) {
  86. test_skip_if(!config_prof);
  87. test_skip_if(!config_opt_safety_checks);
  88. safety_check_set_abort(&fake_abort);
  89. /* Buffer overflow! */
  90. char* ptr = malloc(128);
  91. buffer_overflow_write(ptr, 128);
  92. size_t result = xallocx(ptr, 129, 0, 0);
  93. expect_zu_eq(result, 128, "");
  94. free(ptr);
  95. expect_b_eq(fake_abort_called, true, "Redzone check didn't fire.");
  96. fake_abort_called = false;
  97. safety_check_set_abort(NULL);
  98. }
  99. TEST_END
  100. TEST_BEGIN(test_realloc_no_overflow) {
  101. char* ptr = malloc(128);
  102. ptr = realloc(ptr, 256);
  103. ptr[128] = 0;
  104. ptr[255] = 0;
  105. free(ptr);
  106. ptr = malloc(128);
  107. ptr = realloc(ptr, 64);
  108. ptr[63] = 0;
  109. ptr[0] = 0;
  110. free(ptr);
  111. }
  112. TEST_END
  113. TEST_BEGIN(test_rallocx_no_overflow) {
  114. char* ptr = malloc(128);
  115. ptr = rallocx(ptr, 256, 0);
  116. ptr[128] = 0;
  117. ptr[255] = 0;
  118. free(ptr);
  119. ptr = malloc(128);
  120. ptr = rallocx(ptr, 64, 0);
  121. ptr[63] = 0;
  122. ptr[0] = 0;
  123. free(ptr);
  124. }
  125. TEST_END
  126. int
  127. main(void) {
  128. return test(
  129. test_malloc_free_overflow,
  130. test_mallocx_dallocx_overflow,
  131. test_malloc_sdallocx_overflow,
  132. test_realloc_overflow,
  133. test_rallocx_overflow,
  134. test_xallocx_overflow,
  135. test_realloc_no_overflow,
  136. test_rallocx_no_overflow);
  137. }