xallocx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include "test/jemalloc_test.h"
  2. TEST_BEGIN(test_same_size)
  3. {
  4. void *p;
  5. size_t sz, tsz;
  6. p = mallocx(42, 0);
  7. assert_ptr_not_null(p, "Unexpected mallocx() error");
  8. sz = sallocx(p, 0);
  9. tsz = xallocx(p, sz, 0, 0);
  10. assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
  11. dallocx(p, 0);
  12. }
  13. TEST_END
  14. TEST_BEGIN(test_extra_no_move)
  15. {
  16. void *p;
  17. size_t sz, tsz;
  18. p = mallocx(42, 0);
  19. assert_ptr_not_null(p, "Unexpected mallocx() error");
  20. sz = sallocx(p, 0);
  21. tsz = xallocx(p, sz, sz-42, 0);
  22. assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
  23. dallocx(p, 0);
  24. }
  25. TEST_END
  26. TEST_BEGIN(test_no_move_fail)
  27. {
  28. void *p;
  29. size_t sz, tsz;
  30. p = mallocx(42, 0);
  31. assert_ptr_not_null(p, "Unexpected mallocx() error");
  32. sz = sallocx(p, 0);
  33. tsz = xallocx(p, sz + 5, 0, 0);
  34. assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
  35. dallocx(p, 0);
  36. }
  37. TEST_END
  38. static unsigned
  39. get_nsizes_impl(const char *cmd)
  40. {
  41. unsigned ret;
  42. size_t z;
  43. z = sizeof(unsigned);
  44. assert_d_eq(mallctl(cmd, &ret, &z, NULL, 0), 0,
  45. "Unexpected mallctl(\"%s\", ...) failure", cmd);
  46. return (ret);
  47. }
  48. static unsigned
  49. get_nsmall(void)
  50. {
  51. return (get_nsizes_impl("arenas.nbins"));
  52. }
  53. static unsigned
  54. get_nlarge(void)
  55. {
  56. return (get_nsizes_impl("arenas.nlruns"));
  57. }
  58. static unsigned
  59. get_nhuge(void)
  60. {
  61. return (get_nsizes_impl("arenas.nhchunks"));
  62. }
  63. static size_t
  64. get_size_impl(const char *cmd, size_t ind)
  65. {
  66. size_t ret;
  67. size_t z;
  68. size_t mib[4];
  69. size_t miblen = 4;
  70. z = sizeof(size_t);
  71. assert_d_eq(mallctlnametomib(cmd, mib, &miblen),
  72. 0, "Unexpected mallctlnametomib(\"%s\", ...) failure", cmd);
  73. mib[2] = ind;
  74. z = sizeof(size_t);
  75. assert_d_eq(mallctlbymib(mib, miblen, &ret, &z, NULL, 0),
  76. 0, "Unexpected mallctlbymib([\"%s\", %zu], ...) failure", cmd, ind);
  77. return (ret);
  78. }
  79. static size_t
  80. get_small_size(size_t ind)
  81. {
  82. return (get_size_impl("arenas.bin.0.size", ind));
  83. }
  84. static size_t
  85. get_large_size(size_t ind)
  86. {
  87. return (get_size_impl("arenas.lrun.0.size", ind));
  88. }
  89. static size_t
  90. get_huge_size(size_t ind)
  91. {
  92. return (get_size_impl("arenas.hchunk.0.size", ind));
  93. }
  94. TEST_BEGIN(test_size)
  95. {
  96. size_t small0, hugemax;
  97. void *p;
  98. /* Get size classes. */
  99. small0 = get_small_size(0);
  100. hugemax = get_huge_size(get_nhuge()-1);
  101. p = mallocx(small0, 0);
  102. assert_ptr_not_null(p, "Unexpected mallocx() error");
  103. /* Test smallest supported size. */
  104. assert_zu_eq(xallocx(p, 1, 0, 0), small0,
  105. "Unexpected xallocx() behavior");
  106. /* Test largest supported size. */
  107. assert_zu_le(xallocx(p, hugemax, 0, 0), hugemax,
  108. "Unexpected xallocx() behavior");
  109. /* Test size overflow. */
  110. assert_zu_le(xallocx(p, hugemax+1, 0, 0), hugemax,
  111. "Unexpected xallocx() behavior");
  112. assert_zu_le(xallocx(p, SIZE_T_MAX, 0, 0), hugemax,
  113. "Unexpected xallocx() behavior");
  114. dallocx(p, 0);
  115. }
  116. TEST_END
  117. TEST_BEGIN(test_size_extra_overflow)
  118. {
  119. size_t small0, hugemax;
  120. void *p;
  121. /* Get size classes. */
  122. small0 = get_small_size(0);
  123. hugemax = get_huge_size(get_nhuge()-1);
  124. p = mallocx(small0, 0);
  125. assert_ptr_not_null(p, "Unexpected mallocx() error");
  126. /* Test overflows that can be resolved by clamping extra. */
  127. assert_zu_le(xallocx(p, hugemax-1, 2, 0), hugemax,
  128. "Unexpected xallocx() behavior");
  129. assert_zu_le(xallocx(p, hugemax, 1, 0), hugemax,
  130. "Unexpected xallocx() behavior");
  131. /* Test overflow such that hugemax-size underflows. */
  132. assert_zu_le(xallocx(p, hugemax+1, 2, 0), hugemax,
  133. "Unexpected xallocx() behavior");
  134. assert_zu_le(xallocx(p, hugemax+2, 3, 0), hugemax,
  135. "Unexpected xallocx() behavior");
  136. assert_zu_le(xallocx(p, SIZE_T_MAX-2, 2, 0), hugemax,
  137. "Unexpected xallocx() behavior");
  138. assert_zu_le(xallocx(p, SIZE_T_MAX-1, 1, 0), hugemax,
  139. "Unexpected xallocx() behavior");
  140. dallocx(p, 0);
  141. }
  142. TEST_END
  143. TEST_BEGIN(test_extra_small)
  144. {
  145. size_t small0, small1, hugemax;
  146. void *p;
  147. /* Get size classes. */
  148. small0 = get_small_size(0);
  149. small1 = get_small_size(1);
  150. hugemax = get_huge_size(get_nhuge()-1);
  151. p = mallocx(small0, 0);
  152. assert_ptr_not_null(p, "Unexpected mallocx() error");
  153. assert_zu_eq(xallocx(p, small1, 0, 0), small0,
  154. "Unexpected xallocx() behavior");
  155. assert_zu_eq(xallocx(p, small1, 0, 0), small0,
  156. "Unexpected xallocx() behavior");
  157. assert_zu_eq(xallocx(p, small0, small1 - small0, 0), small0,
  158. "Unexpected xallocx() behavior");
  159. /* Test size+extra overflow. */
  160. assert_zu_eq(xallocx(p, small0, hugemax - small0 + 1, 0), small0,
  161. "Unexpected xallocx() behavior");
  162. assert_zu_eq(xallocx(p, small0, SIZE_T_MAX - small0, 0), small0,
  163. "Unexpected xallocx() behavior");
  164. dallocx(p, 0);
  165. }
  166. TEST_END
  167. TEST_BEGIN(test_extra_large)
  168. {
  169. size_t smallmax, large0, large1, large2, huge0, hugemax;
  170. void *p;
  171. /* Get size classes. */
  172. smallmax = get_small_size(get_nsmall()-1);
  173. large0 = get_large_size(0);
  174. large1 = get_large_size(1);
  175. large2 = get_large_size(2);
  176. huge0 = get_huge_size(0);
  177. hugemax = get_huge_size(get_nhuge()-1);
  178. p = mallocx(large2, 0);
  179. assert_ptr_not_null(p, "Unexpected mallocx() error");
  180. assert_zu_eq(xallocx(p, large2, 0, 0), large2,
  181. "Unexpected xallocx() behavior");
  182. /* Test size decrease with zero extra. */
  183. assert_zu_eq(xallocx(p, large0, 0, 0), large0,
  184. "Unexpected xallocx() behavior");
  185. assert_zu_eq(xallocx(p, smallmax, 0, 0), large0,
  186. "Unexpected xallocx() behavior");
  187. assert_zu_eq(xallocx(p, large2, 0, 0), large2,
  188. "Unexpected xallocx() behavior");
  189. /* Test size decrease with non-zero extra. */
  190. assert_zu_eq(xallocx(p, large0, large2 - large0, 0), large2,
  191. "Unexpected xallocx() behavior");
  192. assert_zu_eq(xallocx(p, large1, large2 - large1, 0), large2,
  193. "Unexpected xallocx() behavior");
  194. assert_zu_eq(xallocx(p, large0, large1 - large0, 0), large1,
  195. "Unexpected xallocx() behavior");
  196. assert_zu_eq(xallocx(p, smallmax, large0 - smallmax, 0), large0,
  197. "Unexpected xallocx() behavior");
  198. assert_zu_eq(xallocx(p, large0, 0, 0), large0,
  199. "Unexpected xallocx() behavior");
  200. /* Test size increase with zero extra. */
  201. assert_zu_eq(xallocx(p, large2, 0, 0), large2,
  202. "Unexpected xallocx() behavior");
  203. assert_zu_eq(xallocx(p, huge0, 0, 0), large2,
  204. "Unexpected xallocx() behavior");
  205. assert_zu_eq(xallocx(p, large0, 0, 0), large0,
  206. "Unexpected xallocx() behavior");
  207. /* Test size increase with non-zero extra. */
  208. assert_zu_lt(xallocx(p, large0, huge0 - large0, 0), huge0,
  209. "Unexpected xallocx() behavior");
  210. assert_zu_eq(xallocx(p, large0, 0, 0), large0,
  211. "Unexpected xallocx() behavior");
  212. /* Test size increase with non-zero extra. */
  213. assert_zu_eq(xallocx(p, large0, large2 - large0, 0), large2,
  214. "Unexpected xallocx() behavior");
  215. assert_zu_eq(xallocx(p, large2, 0, 0), large2,
  216. "Unexpected xallocx() behavior");
  217. /* Test size+extra overflow. */
  218. assert_zu_lt(xallocx(p, large2, hugemax - large2 + 1, 0), huge0,
  219. "Unexpected xallocx() behavior");
  220. dallocx(p, 0);
  221. }
  222. TEST_END
  223. TEST_BEGIN(test_extra_huge)
  224. {
  225. size_t largemax, huge0, huge1, huge2, hugemax;
  226. void *p;
  227. /* Get size classes. */
  228. largemax = get_large_size(get_nlarge()-1);
  229. huge0 = get_huge_size(0);
  230. huge1 = get_huge_size(1);
  231. huge2 = get_huge_size(2);
  232. hugemax = get_huge_size(get_nhuge()-1);
  233. p = mallocx(huge2, 0);
  234. assert_ptr_not_null(p, "Unexpected mallocx() error");
  235. assert_zu_eq(xallocx(p, huge2, 0, 0), huge2,
  236. "Unexpected xallocx() behavior");
  237. /* Test size decrease with zero extra. */
  238. assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
  239. "Unexpected xallocx() behavior");
  240. assert_zu_ge(xallocx(p, largemax, 0, 0), huge0,
  241. "Unexpected xallocx() behavior");
  242. assert_zu_eq(xallocx(p, huge2, 0, 0), huge2,
  243. "Unexpected xallocx() behavior");
  244. /* Test size decrease with non-zero extra. */
  245. assert_zu_eq(xallocx(p, huge0, huge2 - huge0, 0), huge2,
  246. "Unexpected xallocx() behavior");
  247. assert_zu_eq(xallocx(p, huge1, huge2 - huge1, 0), huge2,
  248. "Unexpected xallocx() behavior");
  249. assert_zu_eq(xallocx(p, huge0, huge1 - huge0, 0), huge1,
  250. "Unexpected xallocx() behavior");
  251. assert_zu_ge(xallocx(p, largemax, huge0 - largemax, 0), huge0,
  252. "Unexpected xallocx() behavior");
  253. assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
  254. "Unexpected xallocx() behavior");
  255. /* Test size increase with zero extra. */
  256. assert_zu_le(xallocx(p, huge2, 0, 0), huge2,
  257. "Unexpected xallocx() behavior");
  258. assert_zu_le(xallocx(p, hugemax+1, 0, 0), huge2,
  259. "Unexpected xallocx() behavior");
  260. assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
  261. "Unexpected xallocx() behavior");
  262. /* Test size increase with non-zero extra. */
  263. assert_zu_le(xallocx(p, huge0, SIZE_T_MAX - huge0, 0), hugemax,
  264. "Unexpected xallocx() behavior");
  265. assert_zu_ge(xallocx(p, huge0, 0, 0), huge0,
  266. "Unexpected xallocx() behavior");
  267. /* Test size increase with non-zero extra. */
  268. assert_zu_le(xallocx(p, huge0, huge2 - huge0, 0), huge2,
  269. "Unexpected xallocx() behavior");
  270. assert_zu_eq(xallocx(p, huge2, 0, 0), huge2,
  271. "Unexpected xallocx() behavior");
  272. /* Test size+extra overflow. */
  273. assert_zu_le(xallocx(p, huge2, hugemax - huge2 + 1, 0), hugemax,
  274. "Unexpected xallocx() behavior");
  275. dallocx(p, 0);
  276. }
  277. TEST_END
  278. static void
  279. print_filled_extents(const void *p, uint8_t c, size_t len)
  280. {
  281. const uint8_t *pc = (const uint8_t *)p;
  282. size_t i, range0;
  283. uint8_t c0;
  284. malloc_printf(" p=%p, c=%#x, len=%zu:", p, c, len);
  285. range0 = 0;
  286. c0 = pc[0];
  287. for (i = 0; i < len; i++) {
  288. if (pc[i] != c0) {
  289. malloc_printf(" %#x[%zu..%zu)", c0, range0, i);
  290. range0 = i;
  291. c0 = pc[i];
  292. }
  293. }
  294. malloc_printf(" %#x[%zu..%zu)\n", c0, range0, i);
  295. }
  296. static bool
  297. validate_fill(const void *p, uint8_t c, size_t offset, size_t len)
  298. {
  299. const uint8_t *pc = (const uint8_t *)p;
  300. bool err;
  301. size_t i;
  302. for (i = offset, err = false; i < offset+len; i++) {
  303. if (pc[i] != c)
  304. err = true;
  305. }
  306. if (err)
  307. print_filled_extents(p, c, offset + len);
  308. return (err);
  309. }
  310. static void
  311. test_zero(size_t szmin, size_t szmax)
  312. {
  313. size_t sz, nsz;
  314. void *p;
  315. #define FILL_BYTE 0x7aU
  316. sz = szmax;
  317. p = mallocx(sz, MALLOCX_ZERO);
  318. assert_ptr_not_null(p, "Unexpected mallocx() error");
  319. assert_false(validate_fill(p, 0x00, 0, sz), "Memory not filled: sz=%zu",
  320. sz);
  321. /*
  322. * Fill with non-zero so that non-debug builds are more likely to detect
  323. * errors.
  324. */
  325. memset(p, FILL_BYTE, sz);
  326. assert_false(validate_fill(p, FILL_BYTE, 0, sz),
  327. "Memory not filled: sz=%zu", sz);
  328. /* Shrink in place so that we can expect growing in place to succeed. */
  329. sz = szmin;
  330. assert_zu_eq(xallocx(p, sz, 0, MALLOCX_ZERO), sz,
  331. "Unexpected xallocx() error");
  332. assert_false(validate_fill(p, FILL_BYTE, 0, sz),
  333. "Memory not filled: sz=%zu", sz);
  334. for (sz = szmin; sz < szmax; sz = nsz) {
  335. nsz = nallocx(sz+1, MALLOCX_ZERO);
  336. assert_zu_eq(xallocx(p, sz+1, 0, MALLOCX_ZERO), nsz,
  337. "Unexpected xallocx() failure");
  338. assert_false(validate_fill(p, FILL_BYTE, 0, sz),
  339. "Memory not filled: sz=%zu", sz);
  340. assert_false(validate_fill(p, 0x00, sz, nsz-sz),
  341. "Memory not filled: sz=%zu, nsz-sz=%zu", sz, nsz-sz);
  342. memset((void *)((uintptr_t)p + sz), FILL_BYTE, nsz-sz);
  343. assert_false(validate_fill(p, FILL_BYTE, 0, nsz),
  344. "Memory not filled: nsz=%zu", nsz);
  345. }
  346. dallocx(p, 0);
  347. }
  348. TEST_BEGIN(test_zero_large)
  349. {
  350. size_t large0, largemax;
  351. /* Get size classes. */
  352. large0 = get_large_size(0);
  353. largemax = get_large_size(get_nlarge()-1);
  354. test_zero(large0, largemax);
  355. }
  356. TEST_END
  357. TEST_BEGIN(test_zero_huge)
  358. {
  359. size_t huge0, huge1;
  360. /* Get size classes. */
  361. huge0 = get_huge_size(0);
  362. huge1 = get_huge_size(1);
  363. test_zero(huge1, huge0 * 2);
  364. }
  365. TEST_END
  366. int
  367. main(void)
  368. {
  369. return (test(
  370. test_same_size,
  371. test_extra_no_move,
  372. test_no_move_fail,
  373. test_size,
  374. test_size_extra_overflow,
  375. test_extra_small,
  376. test_extra_large,
  377. test_extra_huge,
  378. test_zero_large,
  379. test_zero_huge));
  380. }