overflow.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "test/jemalloc_test.h"
  2. /*
  3. * GCC "-Walloc-size-larger-than" warning detects when one of the memory
  4. * allocation functions is called with a size larger than the maximum size that
  5. * they support. Here we want to explicitly test that the allocation functions
  6. * do indeed fail properly when this is the case, which triggers the warning.
  7. * Therefore we disable the warning for these tests.
  8. */
  9. JEMALLOC_DIAGNOSTIC_PUSH
  10. JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
  11. TEST_BEGIN(test_overflow) {
  12. unsigned nlextents;
  13. size_t mib[4];
  14. size_t sz, miblen, max_size_class;
  15. void *p;
  16. sz = sizeof(unsigned);
  17. assert_d_eq(mallctl("arenas.nlextents", (void *)&nlextents, &sz, NULL,
  18. 0), 0, "Unexpected mallctl() error");
  19. miblen = sizeof(mib) / sizeof(size_t);
  20. assert_d_eq(mallctlnametomib("arenas.lextent.0.size", mib, &miblen), 0,
  21. "Unexpected mallctlnametomib() error");
  22. mib[2] = nlextents - 1;
  23. sz = sizeof(size_t);
  24. assert_d_eq(mallctlbymib(mib, miblen, (void *)&max_size_class, &sz,
  25. NULL, 0), 0, "Unexpected mallctlbymib() error");
  26. assert_ptr_null(malloc(max_size_class + 1),
  27. "Expected OOM due to over-sized allocation request");
  28. assert_ptr_null(malloc(SIZE_T_MAX),
  29. "Expected OOM due to over-sized allocation request");
  30. assert_ptr_null(calloc(1, max_size_class + 1),
  31. "Expected OOM due to over-sized allocation request");
  32. assert_ptr_null(calloc(1, SIZE_T_MAX),
  33. "Expected OOM due to over-sized allocation request");
  34. p = malloc(1);
  35. assert_ptr_not_null(p, "Unexpected malloc() OOM");
  36. assert_ptr_null(realloc(p, max_size_class + 1),
  37. "Expected OOM due to over-sized allocation request");
  38. assert_ptr_null(realloc(p, SIZE_T_MAX),
  39. "Expected OOM due to over-sized allocation request");
  40. free(p);
  41. }
  42. TEST_END
  43. /* Re-enable the "-Walloc-size-larger-than=" warning */
  44. JEMALLOC_DIAGNOSTIC_POP
  45. int
  46. main(void) {
  47. return test(
  48. test_overflow);
  49. }