2
0

rallocx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "test/jemalloc_test.h"
  2. static unsigned
  3. get_nsizes_impl(const char *cmd) {
  4. unsigned ret;
  5. size_t z;
  6. z = sizeof(unsigned);
  7. assert_d_eq(mallctl(cmd, (void *)&ret, &z, NULL, 0), 0,
  8. "Unexpected mallctl(\"%s\", ...) failure", cmd);
  9. return ret;
  10. }
  11. static unsigned
  12. get_nlarge(void) {
  13. return get_nsizes_impl("arenas.nlextents");
  14. }
  15. static size_t
  16. get_size_impl(const char *cmd, size_t ind) {
  17. size_t ret;
  18. size_t z;
  19. size_t mib[4];
  20. size_t miblen = 4;
  21. z = sizeof(size_t);
  22. assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
  23. 0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
  24. mib[2] = ind;
  25. z = sizeof(size_t);
  26. assert_d_eq(mallctlbymib(mib, miblen, (void *)&ret, &z, NULL, 0),
  27. 0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
  28. return ret;
  29. }
  30. static size_t
  31. get_large_size(size_t ind) {
  32. return get_size_impl("arenas.lextent.0.size", ind);
  33. }
  34. TEST_BEGIN(test_grow_and_shrink) {
  35. void *p, *q;
  36. size_t tsz;
  37. #define NCYCLES 3
  38. unsigned i, j;
  39. #define NSZS 1024
  40. size_t szs[NSZS];
  41. #define MAXSZ ZU(12 * 1024 * 1024)
  42. p = mallocx(1, 0);
  43. assert_ptr_not_null(p, "Unexpected mallocx() error");
  44. szs[0] = sallocx(p, 0);
  45. for (i = 0; i < NCYCLES; i++) {
  46. for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
  47. q = rallocx(p, szs[j-1]+1, 0);
  48. assert_ptr_not_null(q,
  49. "Unexpected rallocx() error for size=%zu-->%zu",
  50. szs[j-1], szs[j-1]+1);
  51. szs[j] = sallocx(q, 0);
  52. assert_zu_ne(szs[j], szs[j-1]+1,
  53. "Expected size to be at least: %zu", szs[j-1]+1);
  54. p = q;
  55. }
  56. for (j--; j > 0; j--) {
  57. q = rallocx(p, szs[j-1], 0);
  58. assert_ptr_not_null(q,
  59. "Unexpected rallocx() error for size=%zu-->%zu",
  60. szs[j], szs[j-1]);
  61. tsz = sallocx(q, 0);
  62. assert_zu_eq(tsz, szs[j-1],
  63. "Expected size=%zu, got size=%zu", szs[j-1], tsz);
  64. p = q;
  65. }
  66. }
  67. dallocx(p, 0);
  68. #undef MAXSZ
  69. #undef NSZS
  70. #undef NCYCLES
  71. }
  72. TEST_END
  73. static bool
  74. validate_fill(const void *p, uint8_t c, size_t offset, size_t len) {
  75. bool ret = false;
  76. const uint8_t *buf = (const uint8_t *)p;
  77. size_t i;
  78. for (i = 0; i < len; i++) {
  79. uint8_t b = buf[offset+i];
  80. if (b != c) {
  81. test_fail("Allocation at %p (len=%zu) contains %#x "
  82. "rather than %#x at offset %zu", p, len, b, c,
  83. offset+i);
  84. ret = true;
  85. }
  86. }
  87. return ret;
  88. }
  89. TEST_BEGIN(test_zero) {
  90. void *p, *q;
  91. size_t psz, qsz, i, j;
  92. size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
  93. #define FILL_BYTE 0xaaU
  94. #define RANGE 2048
  95. for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
  96. size_t start_size = start_sizes[i];
  97. p = mallocx(start_size, MALLOCX_ZERO);
  98. assert_ptr_not_null(p, "Unexpected mallocx() error");
  99. psz = sallocx(p, 0);
  100. assert_false(validate_fill(p, 0, 0, psz),
  101. "Expected zeroed memory");
  102. memset(p, FILL_BYTE, psz);
  103. assert_false(validate_fill(p, FILL_BYTE, 0, psz),
  104. "Expected filled memory");
  105. for (j = 1; j < RANGE; j++) {
  106. q = rallocx(p, start_size+j, MALLOCX_ZERO);
  107. assert_ptr_not_null(q, "Unexpected rallocx() error");
  108. qsz = sallocx(q, 0);
  109. if (q != p || qsz != psz) {
  110. assert_false(validate_fill(q, FILL_BYTE, 0,
  111. psz), "Expected filled memory");
  112. assert_false(validate_fill(q, 0, psz, qsz-psz),
  113. "Expected zeroed memory");
  114. }
  115. if (psz != qsz) {
  116. memset((void *)((uintptr_t)q+psz), FILL_BYTE,
  117. qsz-psz);
  118. psz = qsz;
  119. }
  120. p = q;
  121. }
  122. assert_false(validate_fill(p, FILL_BYTE, 0, psz),
  123. "Expected filled memory");
  124. dallocx(p, 0);
  125. }
  126. #undef FILL_BYTE
  127. }
  128. TEST_END
  129. TEST_BEGIN(test_align) {
  130. void *p, *q;
  131. size_t align;
  132. #define MAX_ALIGN (ZU(1) << 25)
  133. align = ZU(1);
  134. p = mallocx(1, MALLOCX_ALIGN(align));
  135. assert_ptr_not_null(p, "Unexpected mallocx() error");
  136. for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
  137. q = rallocx(p, 1, MALLOCX_ALIGN(align));
  138. assert_ptr_not_null(q,
  139. "Unexpected rallocx() error for align=%zu", align);
  140. assert_ptr_null(
  141. (void *)((uintptr_t)q & (align-1)),
  142. "%p inadequately aligned for align=%zu",
  143. q, align);
  144. p = q;
  145. }
  146. dallocx(p, 0);
  147. #undef MAX_ALIGN
  148. }
  149. TEST_END
  150. TEST_BEGIN(test_lg_align_and_zero) {
  151. void *p, *q;
  152. unsigned lg_align;
  153. size_t sz;
  154. #define MAX_LG_ALIGN 25
  155. #define MAX_VALIDATE (ZU(1) << 22)
  156. lg_align = 0;
  157. p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  158. assert_ptr_not_null(p, "Unexpected mallocx() error");
  159. for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
  160. q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  161. assert_ptr_not_null(q,
  162. "Unexpected rallocx() error for lg_align=%u", lg_align);
  163. assert_ptr_null(
  164. (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
  165. "%p inadequately aligned for lg_align=%u", q, lg_align);
  166. sz = sallocx(q, 0);
  167. if ((sz << 1) <= MAX_VALIDATE) {
  168. assert_false(validate_fill(q, 0, 0, sz),
  169. "Expected zeroed memory");
  170. } else {
  171. assert_false(validate_fill(q, 0, 0, MAX_VALIDATE),
  172. "Expected zeroed memory");
  173. assert_false(validate_fill(
  174. (void *)((uintptr_t)q+sz-MAX_VALIDATE),
  175. 0, 0, MAX_VALIDATE), "Expected zeroed memory");
  176. }
  177. p = q;
  178. }
  179. dallocx(p, 0);
  180. #undef MAX_VALIDATE
  181. #undef MAX_LG_ALIGN
  182. }
  183. TEST_END
  184. /*
  185. * GCC "-Walloc-size-larger-than" warning detects when one of the memory
  186. * allocation functions is called with a size larger than the maximum size that
  187. * they support. Here we want to explicitly test that the allocation functions
  188. * do indeed fail properly when this is the case, which triggers the warning.
  189. * Therefore we disable the warning for these tests.
  190. */
  191. JEMALLOC_DIAGNOSTIC_PUSH
  192. JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
  193. TEST_BEGIN(test_overflow) {
  194. size_t largemax;
  195. void *p;
  196. largemax = get_large_size(get_nlarge()-1);
  197. p = mallocx(1, 0);
  198. assert_ptr_not_null(p, "Unexpected mallocx() failure");
  199. assert_ptr_null(rallocx(p, largemax+1, 0),
  200. "Expected OOM for rallocx(p, size=%#zx, 0)", largemax+1);
  201. assert_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),
  202. "Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
  203. assert_ptr_null(rallocx(p, SIZE_T_MAX, 0),
  204. "Expected OOM for rallocx(p, size=%#zx, 0)", SIZE_T_MAX);
  205. assert_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
  206. "Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))",
  207. ZU(PTRDIFF_MAX)+1);
  208. dallocx(p, 0);
  209. }
  210. TEST_END
  211. /* Re-enable the "-Walloc-size-larger-than=" warning */
  212. JEMALLOC_DIAGNOSTIC_POP
  213. int
  214. main(void) {
  215. return test(
  216. test_grow_and_shrink,
  217. test_zero,
  218. test_align,
  219. test_lg_align_and_zero,
  220. test_overflow);
  221. }