size_check.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/safety_check.h"
  3. bool fake_abort_called;
  4. void fake_abort(const char *message) {
  5. (void)message;
  6. fake_abort_called = true;
  7. }
  8. #define SMALL_SIZE1 SC_SMALL_MAXCLASS
  9. #define SMALL_SIZE2 (SC_SMALL_MAXCLASS / 2)
  10. #define LARGE_SIZE1 SC_LARGE_MINCLASS
  11. #define LARGE_SIZE2 (LARGE_SIZE1 * 2)
  12. void *
  13. test_invalid_size_pre(size_t sz) {
  14. safety_check_set_abort(&fake_abort);
  15. fake_abort_called = false;
  16. void *ptr = malloc(sz);
  17. assert_ptr_not_null(ptr, "Unexpected failure");
  18. return ptr;
  19. }
  20. void
  21. test_invalid_size_post(void) {
  22. expect_true(fake_abort_called, "Safety check didn't fire");
  23. safety_check_set_abort(NULL);
  24. }
  25. TEST_BEGIN(test_invalid_size_sdallocx) {
  26. test_skip_if(!config_opt_size_checks);
  27. void *ptr = test_invalid_size_pre(SMALL_SIZE1);
  28. sdallocx(ptr, SMALL_SIZE2, 0);
  29. test_invalid_size_post();
  30. ptr = test_invalid_size_pre(LARGE_SIZE1);
  31. sdallocx(ptr, LARGE_SIZE2, 0);
  32. test_invalid_size_post();
  33. }
  34. TEST_END
  35. TEST_BEGIN(test_invalid_size_sdallocx_nonzero_flag) {
  36. test_skip_if(!config_opt_size_checks);
  37. void *ptr = test_invalid_size_pre(SMALL_SIZE1);
  38. sdallocx(ptr, SMALL_SIZE2, MALLOCX_TCACHE_NONE);
  39. test_invalid_size_post();
  40. ptr = test_invalid_size_pre(LARGE_SIZE1);
  41. sdallocx(ptr, LARGE_SIZE2, MALLOCX_TCACHE_NONE);
  42. test_invalid_size_post();
  43. }
  44. TEST_END
  45. TEST_BEGIN(test_invalid_size_sdallocx_noflags) {
  46. test_skip_if(!config_opt_size_checks);
  47. void *ptr = test_invalid_size_pre(SMALL_SIZE1);
  48. je_sdallocx_noflags(ptr, SMALL_SIZE2);
  49. test_invalid_size_post();
  50. ptr = test_invalid_size_pre(LARGE_SIZE1);
  51. je_sdallocx_noflags(ptr, LARGE_SIZE2);
  52. test_invalid_size_post();
  53. }
  54. TEST_END
  55. int
  56. main(void) {
  57. return test(
  58. test_invalid_size_sdallocx,
  59. test_invalid_size_sdallocx_nonzero_flag,
  60. test_invalid_size_sdallocx_noflags);
  61. }