rallocx.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. expect_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. expect_d_eq(mallctlnametomib(cmd, mib, &miblen),
  23. 0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
  24. mib[2] = ind;
  25. z = sizeof(size_t);
  26. expect_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. /*
  36. * Use volatile to workaround buffer overflow false positives
  37. * (-D_FORTIFY_SOURCE=3).
  38. */
  39. void *volatile p, *volatile q;
  40. size_t tsz;
  41. #define NCYCLES 3
  42. unsigned i, j;
  43. #define NSZS 1024
  44. size_t szs[NSZS];
  45. #define MAXSZ ZU(12 * 1024 * 1024)
  46. p = mallocx(1, 0);
  47. expect_ptr_not_null(p, "Unexpected mallocx() error");
  48. szs[0] = sallocx(p, 0);
  49. for (i = 0; i < NCYCLES; i++) {
  50. for (j = 1; j < NSZS && szs[j-1] < MAXSZ; j++) {
  51. q = rallocx(p, szs[j-1]+1, 0);
  52. expect_ptr_not_null(q,
  53. "Unexpected rallocx() error for size=%zu-->%zu",
  54. szs[j-1], szs[j-1]+1);
  55. szs[j] = sallocx(q, 0);
  56. expect_zu_ne(szs[j], szs[j-1]+1,
  57. "Expected size to be at least: %zu", szs[j-1]+1);
  58. p = q;
  59. }
  60. for (j--; j > 0; j--) {
  61. q = rallocx(p, szs[j-1], 0);
  62. expect_ptr_not_null(q,
  63. "Unexpected rallocx() error for size=%zu-->%zu",
  64. szs[j], szs[j-1]);
  65. tsz = sallocx(q, 0);
  66. expect_zu_eq(tsz, szs[j-1],
  67. "Expected size=%zu, got size=%zu", szs[j-1], tsz);
  68. p = q;
  69. }
  70. }
  71. dallocx(p, 0);
  72. #undef MAXSZ
  73. #undef NSZS
  74. #undef NCYCLES
  75. }
  76. TEST_END
  77. static bool
  78. validate_fill(void *p, uint8_t c, size_t offset, size_t len) {
  79. bool ret = false;
  80. /*
  81. * Use volatile to workaround buffer overflow false positives
  82. * (-D_FORTIFY_SOURCE=3).
  83. */
  84. uint8_t *volatile buf = (uint8_t *)p;
  85. size_t i;
  86. for (i = 0; i < len; i++) {
  87. uint8_t b = buf[offset+i];
  88. if (b != c) {
  89. test_fail("Allocation at %p (len=%zu) contains %#x "
  90. "rather than %#x at offset %zu", p, len, b, c,
  91. offset+i);
  92. ret = true;
  93. }
  94. }
  95. return ret;
  96. }
  97. TEST_BEGIN(test_zero) {
  98. /*
  99. * Use volatile to workaround buffer overflow false positives
  100. * (-D_FORTIFY_SOURCE=3).
  101. */
  102. void *volatile p, *volatile q;
  103. size_t psz, qsz, i, j;
  104. size_t start_sizes[] = {1, 3*1024, 63*1024, 4095*1024};
  105. #define FILL_BYTE 0xaaU
  106. #define RANGE 2048
  107. for (i = 0; i < sizeof(start_sizes)/sizeof(size_t); i++) {
  108. size_t start_size = start_sizes[i];
  109. p = mallocx(start_size, MALLOCX_ZERO);
  110. expect_ptr_not_null(p, "Unexpected mallocx() error");
  111. psz = sallocx(p, 0);
  112. expect_false(validate_fill(p, 0, 0, psz),
  113. "Expected zeroed memory");
  114. memset(p, FILL_BYTE, psz);
  115. expect_false(validate_fill(p, FILL_BYTE, 0, psz),
  116. "Expected filled memory");
  117. for (j = 1; j < RANGE; j++) {
  118. q = rallocx(p, start_size+j, MALLOCX_ZERO);
  119. expect_ptr_not_null(q, "Unexpected rallocx() error");
  120. qsz = sallocx(q, 0);
  121. if (q != p || qsz != psz) {
  122. expect_false(validate_fill(q, FILL_BYTE, 0,
  123. psz), "Expected filled memory");
  124. expect_false(validate_fill(q, 0, psz, qsz-psz),
  125. "Expected zeroed memory");
  126. }
  127. if (psz != qsz) {
  128. memset((void *)((uintptr_t)q+psz), FILL_BYTE,
  129. qsz-psz);
  130. psz = qsz;
  131. }
  132. p = q;
  133. }
  134. expect_false(validate_fill(p, FILL_BYTE, 0, psz),
  135. "Expected filled memory");
  136. dallocx(p, 0);
  137. }
  138. #undef FILL_BYTE
  139. }
  140. TEST_END
  141. TEST_BEGIN(test_align) {
  142. void *p, *q;
  143. size_t align;
  144. #define MAX_ALIGN (ZU(1) << 25)
  145. align = ZU(1);
  146. p = mallocx(1, MALLOCX_ALIGN(align));
  147. expect_ptr_not_null(p, "Unexpected mallocx() error");
  148. for (align <<= 1; align <= MAX_ALIGN; align <<= 1) {
  149. q = rallocx(p, 1, MALLOCX_ALIGN(align));
  150. expect_ptr_not_null(q,
  151. "Unexpected rallocx() error for align=%zu", align);
  152. expect_ptr_null(
  153. (void *)((uintptr_t)q & (align-1)),
  154. "%p inadequately aligned for align=%zu",
  155. q, align);
  156. p = q;
  157. }
  158. dallocx(p, 0);
  159. #undef MAX_ALIGN
  160. }
  161. TEST_END
  162. TEST_BEGIN(test_align_enum) {
  163. /* Span both small sizes and large sizes. */
  164. #define LG_MIN 12
  165. #define LG_MAX 15
  166. for (size_t lg_align = LG_MIN; lg_align <= LG_MAX; ++lg_align) {
  167. for (size_t lg_size = LG_MIN; lg_size <= LG_MAX; ++lg_size) {
  168. size_t size = 1 << lg_size;
  169. for (size_t lg_align_next = LG_MIN;
  170. lg_align_next <= LG_MAX; ++lg_align_next) {
  171. int flags = MALLOCX_LG_ALIGN(lg_align);
  172. void *p = mallocx(1, flags);
  173. assert_ptr_not_null(p,
  174. "Unexpected mallocx() error");
  175. assert_zu_eq(nallocx(1, flags),
  176. TEST_MALLOC_SIZE(p),
  177. "Wrong mallocx() usable size");
  178. int flags_next =
  179. MALLOCX_LG_ALIGN(lg_align_next);
  180. p = rallocx(p, size, flags_next);
  181. assert_ptr_not_null(p,
  182. "Unexpected rallocx() error");
  183. expect_zu_eq(nallocx(size, flags_next),
  184. TEST_MALLOC_SIZE(p),
  185. "Wrong rallocx() usable size");
  186. free(p);
  187. }
  188. }
  189. }
  190. #undef LG_MAX
  191. #undef LG_MIN
  192. }
  193. TEST_END
  194. TEST_BEGIN(test_lg_align_and_zero) {
  195. /*
  196. * Use volatile to workaround buffer overflow false positives
  197. * (-D_FORTIFY_SOURCE=3).
  198. */
  199. void *volatile p, *volatile q;
  200. unsigned lg_align;
  201. size_t sz;
  202. #define MAX_LG_ALIGN 25
  203. #define MAX_VALIDATE (ZU(1) << 22)
  204. lg_align = 0;
  205. p = mallocx(1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  206. expect_ptr_not_null(p, "Unexpected mallocx() error");
  207. for (lg_align++; lg_align <= MAX_LG_ALIGN; lg_align++) {
  208. q = rallocx(p, 1, MALLOCX_LG_ALIGN(lg_align)|MALLOCX_ZERO);
  209. expect_ptr_not_null(q,
  210. "Unexpected rallocx() error for lg_align=%u", lg_align);
  211. expect_ptr_null(
  212. (void *)((uintptr_t)q & ((ZU(1) << lg_align)-1)),
  213. "%p inadequately aligned for lg_align=%u", q, lg_align);
  214. sz = sallocx(q, 0);
  215. if ((sz << 1) <= MAX_VALIDATE) {
  216. expect_false(validate_fill(q, 0, 0, sz),
  217. "Expected zeroed memory");
  218. } else {
  219. expect_false(validate_fill(q, 0, 0, MAX_VALIDATE),
  220. "Expected zeroed memory");
  221. expect_false(validate_fill(
  222. (void *)((uintptr_t)q+sz-MAX_VALIDATE),
  223. 0, 0, MAX_VALIDATE), "Expected zeroed memory");
  224. }
  225. p = q;
  226. }
  227. dallocx(p, 0);
  228. #undef MAX_VALIDATE
  229. #undef MAX_LG_ALIGN
  230. }
  231. TEST_END
  232. /*
  233. * GCC "-Walloc-size-larger-than" warning detects when one of the memory
  234. * allocation functions is called with a size larger than the maximum size that
  235. * they support. Here we want to explicitly test that the allocation functions
  236. * do indeed fail properly when this is the case, which triggers the warning.
  237. * Therefore we disable the warning for these tests.
  238. */
  239. JEMALLOC_DIAGNOSTIC_PUSH
  240. JEMALLOC_DIAGNOSTIC_IGNORE_ALLOC_SIZE_LARGER_THAN
  241. TEST_BEGIN(test_overflow) {
  242. size_t largemax;
  243. void *p;
  244. largemax = get_large_size(get_nlarge()-1);
  245. p = mallocx(1, 0);
  246. expect_ptr_not_null(p, "Unexpected mallocx() failure");
  247. expect_ptr_null(rallocx(p, largemax+1, 0),
  248. "Expected OOM for rallocx(p, size=%#zx, 0)", largemax+1);
  249. expect_ptr_null(rallocx(p, ZU(PTRDIFF_MAX)+1, 0),
  250. "Expected OOM for rallocx(p, size=%#zx, 0)", ZU(PTRDIFF_MAX)+1);
  251. expect_ptr_null(rallocx(p, SIZE_T_MAX, 0),
  252. "Expected OOM for rallocx(p, size=%#zx, 0)", SIZE_T_MAX);
  253. expect_ptr_null(rallocx(p, 1, MALLOCX_ALIGN(ZU(PTRDIFF_MAX)+1)),
  254. "Expected OOM for rallocx(p, size=1, MALLOCX_ALIGN(%#zx))",
  255. ZU(PTRDIFF_MAX)+1);
  256. dallocx(p, 0);
  257. }
  258. TEST_END
  259. /* Re-enable the "-Walloc-size-larger-than=" warning */
  260. JEMALLOC_DIAGNOSTIC_POP
  261. int
  262. main(void) {
  263. return test(
  264. test_grow_and_shrink,
  265. test_zero,
  266. test_align,
  267. test_align_enum,
  268. test_lg_align_and_zero,
  269. test_overflow);
  270. }