hook.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/hook.h"
  3. static void *arg_extra;
  4. static int arg_type;
  5. static void *arg_result;
  6. static void *arg_address;
  7. static size_t arg_old_usize;
  8. static size_t arg_new_usize;
  9. static uintptr_t arg_result_raw;
  10. static uintptr_t arg_args_raw[4];
  11. static int call_count = 0;
  12. static void
  13. reset_args() {
  14. arg_extra = NULL;
  15. arg_type = 12345;
  16. arg_result = NULL;
  17. arg_address = NULL;
  18. arg_old_usize = 0;
  19. arg_new_usize = 0;
  20. arg_result_raw = 0;
  21. memset(arg_args_raw, 77, sizeof(arg_args_raw));
  22. }
  23. static void
  24. alloc_free_size(size_t sz) {
  25. void *ptr = mallocx(1, 0);
  26. free(ptr);
  27. ptr = mallocx(1, 0);
  28. free(ptr);
  29. ptr = mallocx(1, MALLOCX_TCACHE_NONE);
  30. dallocx(ptr, MALLOCX_TCACHE_NONE);
  31. }
  32. /*
  33. * We want to support a degree of user reentrancy. This tests a variety of
  34. * allocation scenarios.
  35. */
  36. static void
  37. be_reentrant() {
  38. /* Let's make sure the tcache is non-empty if enabled. */
  39. alloc_free_size(1);
  40. alloc_free_size(1024);
  41. alloc_free_size(64 * 1024);
  42. alloc_free_size(256 * 1024);
  43. alloc_free_size(1024 * 1024);
  44. /* Some reallocation. */
  45. void *ptr = mallocx(129, 0);
  46. ptr = rallocx(ptr, 130, 0);
  47. free(ptr);
  48. ptr = mallocx(2 * 1024 * 1024, 0);
  49. free(ptr);
  50. ptr = mallocx(1 * 1024 * 1024, 0);
  51. ptr = rallocx(ptr, 2 * 1024 * 1024, 0);
  52. free(ptr);
  53. ptr = mallocx(1, 0);
  54. ptr = rallocx(ptr, 1000, 0);
  55. free(ptr);
  56. }
  57. static void
  58. set_args_raw(uintptr_t *args_raw, int nargs) {
  59. memcpy(arg_args_raw, args_raw, sizeof(uintptr_t) * nargs);
  60. }
  61. static void
  62. expect_args_raw(uintptr_t *args_raw_expected, int nargs) {
  63. int cmp = memcmp(args_raw_expected, arg_args_raw,
  64. sizeof(uintptr_t) * nargs);
  65. expect_d_eq(cmp, 0, "Raw args mismatch");
  66. }
  67. static void
  68. reset() {
  69. call_count = 0;
  70. reset_args();
  71. }
  72. static void
  73. test_alloc_hook(void *extra, hook_alloc_t type, void *result,
  74. uintptr_t result_raw, uintptr_t args_raw[3]) {
  75. call_count++;
  76. arg_extra = extra;
  77. arg_type = (int)type;
  78. arg_result = result;
  79. arg_result_raw = result_raw;
  80. set_args_raw(args_raw, 3);
  81. be_reentrant();
  82. }
  83. static void
  84. test_dalloc_hook(void *extra, hook_dalloc_t type, void *address,
  85. uintptr_t args_raw[3]) {
  86. call_count++;
  87. arg_extra = extra;
  88. arg_type = (int)type;
  89. arg_address = address;
  90. set_args_raw(args_raw, 3);
  91. be_reentrant();
  92. }
  93. static void
  94. test_expand_hook(void *extra, hook_expand_t type, void *address,
  95. size_t old_usize, size_t new_usize, uintptr_t result_raw,
  96. uintptr_t args_raw[4]) {
  97. call_count++;
  98. arg_extra = extra;
  99. arg_type = (int)type;
  100. arg_address = address;
  101. arg_old_usize = old_usize;
  102. arg_new_usize = new_usize;
  103. arg_result_raw = result_raw;
  104. set_args_raw(args_raw, 4);
  105. be_reentrant();
  106. }
  107. TEST_BEGIN(test_hooks_basic) {
  108. /* Just verify that the record their arguments correctly. */
  109. hooks_t hooks = {
  110. &test_alloc_hook, &test_dalloc_hook, &test_expand_hook,
  111. (void *)111};
  112. void *handle = hook_install(TSDN_NULL, &hooks);
  113. uintptr_t args_raw[4] = {10, 20, 30, 40};
  114. /* Alloc */
  115. reset_args();
  116. hook_invoke_alloc(hook_alloc_posix_memalign, (void *)222, 333,
  117. args_raw);
  118. expect_ptr_eq(arg_extra, (void *)111, "Passed wrong user pointer");
  119. expect_d_eq((int)hook_alloc_posix_memalign, arg_type,
  120. "Passed wrong alloc type");
  121. expect_ptr_eq((void *)222, arg_result, "Passed wrong result address");
  122. expect_u64_eq(333, arg_result_raw, "Passed wrong result");
  123. expect_args_raw(args_raw, 3);
  124. /* Dalloc */
  125. reset_args();
  126. hook_invoke_dalloc(hook_dalloc_sdallocx, (void *)222, args_raw);
  127. expect_d_eq((int)hook_dalloc_sdallocx, arg_type,
  128. "Passed wrong dalloc type");
  129. expect_ptr_eq((void *)111, arg_extra, "Passed wrong user pointer");
  130. expect_ptr_eq((void *)222, arg_address, "Passed wrong address");
  131. expect_args_raw(args_raw, 3);
  132. /* Expand */
  133. reset_args();
  134. hook_invoke_expand(hook_expand_xallocx, (void *)222, 333, 444, 555,
  135. args_raw);
  136. expect_d_eq((int)hook_expand_xallocx, arg_type,
  137. "Passed wrong expand type");
  138. expect_ptr_eq((void *)111, arg_extra, "Passed wrong user pointer");
  139. expect_ptr_eq((void *)222, arg_address, "Passed wrong address");
  140. expect_zu_eq(333, arg_old_usize, "Passed wrong old usize");
  141. expect_zu_eq(444, arg_new_usize, "Passed wrong new usize");
  142. expect_zu_eq(555, arg_result_raw, "Passed wrong result");
  143. expect_args_raw(args_raw, 4);
  144. hook_remove(TSDN_NULL, handle);
  145. }
  146. TEST_END
  147. TEST_BEGIN(test_hooks_null) {
  148. /* Null hooks should be ignored, not crash. */
  149. hooks_t hooks1 = {NULL, NULL, NULL, NULL};
  150. hooks_t hooks2 = {&test_alloc_hook, NULL, NULL, NULL};
  151. hooks_t hooks3 = {NULL, &test_dalloc_hook, NULL, NULL};
  152. hooks_t hooks4 = {NULL, NULL, &test_expand_hook, NULL};
  153. void *handle1 = hook_install(TSDN_NULL, &hooks1);
  154. void *handle2 = hook_install(TSDN_NULL, &hooks2);
  155. void *handle3 = hook_install(TSDN_NULL, &hooks3);
  156. void *handle4 = hook_install(TSDN_NULL, &hooks4);
  157. expect_ptr_ne(handle1, NULL, "Hook installation failed");
  158. expect_ptr_ne(handle2, NULL, "Hook installation failed");
  159. expect_ptr_ne(handle3, NULL, "Hook installation failed");
  160. expect_ptr_ne(handle4, NULL, "Hook installation failed");
  161. uintptr_t args_raw[4] = {10, 20, 30, 40};
  162. call_count = 0;
  163. hook_invoke_alloc(hook_alloc_malloc, NULL, 0, args_raw);
  164. expect_d_eq(call_count, 1, "Called wrong number of times");
  165. call_count = 0;
  166. hook_invoke_dalloc(hook_dalloc_free, NULL, args_raw);
  167. expect_d_eq(call_count, 1, "Called wrong number of times");
  168. call_count = 0;
  169. hook_invoke_expand(hook_expand_realloc, NULL, 0, 0, 0, args_raw);
  170. expect_d_eq(call_count, 1, "Called wrong number of times");
  171. hook_remove(TSDN_NULL, handle1);
  172. hook_remove(TSDN_NULL, handle2);
  173. hook_remove(TSDN_NULL, handle3);
  174. hook_remove(TSDN_NULL, handle4);
  175. }
  176. TEST_END
  177. TEST_BEGIN(test_hooks_remove) {
  178. hooks_t hooks = {&test_alloc_hook, NULL, NULL, NULL};
  179. void *handle = hook_install(TSDN_NULL, &hooks);
  180. expect_ptr_ne(handle, NULL, "Hook installation failed");
  181. call_count = 0;
  182. uintptr_t args_raw[4] = {10, 20, 30, 40};
  183. hook_invoke_alloc(hook_alloc_malloc, NULL, 0, args_raw);
  184. expect_d_eq(call_count, 1, "Hook not invoked");
  185. call_count = 0;
  186. hook_remove(TSDN_NULL, handle);
  187. hook_invoke_alloc(hook_alloc_malloc, NULL, 0, NULL);
  188. expect_d_eq(call_count, 0, "Hook invoked after removal");
  189. }
  190. TEST_END
  191. TEST_BEGIN(test_hooks_alloc_simple) {
  192. /* "Simple" in the sense that we're not in a realloc variant. */
  193. hooks_t hooks = {&test_alloc_hook, NULL, NULL, (void *)123};
  194. void *handle = hook_install(TSDN_NULL, &hooks);
  195. expect_ptr_ne(handle, NULL, "Hook installation failed");
  196. /* Stop malloc from being optimized away. */
  197. volatile int err;
  198. void *volatile ptr;
  199. /* malloc */
  200. reset();
  201. ptr = malloc(1);
  202. expect_d_eq(call_count, 1, "Hook not called");
  203. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  204. expect_d_eq(arg_type, (int)hook_alloc_malloc, "Wrong hook type");
  205. expect_ptr_eq(ptr, arg_result, "Wrong result");
  206. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  207. "Wrong raw result");
  208. expect_u64_eq((uintptr_t)1, arg_args_raw[0], "Wrong argument");
  209. free(ptr);
  210. /* posix_memalign */
  211. reset();
  212. err = posix_memalign((void **)&ptr, 1024, 1);
  213. expect_d_eq(call_count, 1, "Hook not called");
  214. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  215. expect_d_eq(arg_type, (int)hook_alloc_posix_memalign,
  216. "Wrong hook type");
  217. expect_ptr_eq(ptr, arg_result, "Wrong result");
  218. expect_u64_eq((uintptr_t)err, (uintptr_t)arg_result_raw,
  219. "Wrong raw result");
  220. expect_u64_eq((uintptr_t)&ptr, arg_args_raw[0], "Wrong argument");
  221. expect_u64_eq((uintptr_t)1024, arg_args_raw[1], "Wrong argument");
  222. expect_u64_eq((uintptr_t)1, arg_args_raw[2], "Wrong argument");
  223. free(ptr);
  224. /* aligned_alloc */
  225. reset();
  226. ptr = aligned_alloc(1024, 1);
  227. expect_d_eq(call_count, 1, "Hook not called");
  228. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  229. expect_d_eq(arg_type, (int)hook_alloc_aligned_alloc,
  230. "Wrong hook type");
  231. expect_ptr_eq(ptr, arg_result, "Wrong result");
  232. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  233. "Wrong raw result");
  234. expect_u64_eq((uintptr_t)1024, arg_args_raw[0], "Wrong argument");
  235. expect_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong argument");
  236. free(ptr);
  237. /* calloc */
  238. reset();
  239. ptr = calloc(11, 13);
  240. expect_d_eq(call_count, 1, "Hook not called");
  241. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  242. expect_d_eq(arg_type, (int)hook_alloc_calloc, "Wrong hook type");
  243. expect_ptr_eq(ptr, arg_result, "Wrong result");
  244. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  245. "Wrong raw result");
  246. expect_u64_eq((uintptr_t)11, arg_args_raw[0], "Wrong argument");
  247. expect_u64_eq((uintptr_t)13, arg_args_raw[1], "Wrong argument");
  248. free(ptr);
  249. /* memalign */
  250. #ifdef JEMALLOC_OVERRIDE_MEMALIGN
  251. reset();
  252. ptr = memalign(1024, 1);
  253. expect_d_eq(call_count, 1, "Hook not called");
  254. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  255. expect_d_eq(arg_type, (int)hook_alloc_memalign, "Wrong hook type");
  256. expect_ptr_eq(ptr, arg_result, "Wrong result");
  257. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  258. "Wrong raw result");
  259. expect_u64_eq((uintptr_t)1024, arg_args_raw[0], "Wrong argument");
  260. expect_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong argument");
  261. free(ptr);
  262. #endif /* JEMALLOC_OVERRIDE_MEMALIGN */
  263. /* valloc */
  264. #ifdef JEMALLOC_OVERRIDE_VALLOC
  265. reset();
  266. ptr = valloc(1);
  267. expect_d_eq(call_count, 1, "Hook not called");
  268. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  269. expect_d_eq(arg_type, (int)hook_alloc_valloc, "Wrong hook type");
  270. expect_ptr_eq(ptr, arg_result, "Wrong result");
  271. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  272. "Wrong raw result");
  273. expect_u64_eq((uintptr_t)1, arg_args_raw[0], "Wrong argument");
  274. free(ptr);
  275. #endif /* JEMALLOC_OVERRIDE_VALLOC */
  276. /* mallocx */
  277. reset();
  278. ptr = mallocx(1, MALLOCX_LG_ALIGN(10));
  279. expect_d_eq(call_count, 1, "Hook not called");
  280. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  281. expect_d_eq(arg_type, (int)hook_alloc_mallocx, "Wrong hook type");
  282. expect_ptr_eq(ptr, arg_result, "Wrong result");
  283. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  284. "Wrong raw result");
  285. expect_u64_eq((uintptr_t)1, arg_args_raw[0], "Wrong argument");
  286. expect_u64_eq((uintptr_t)MALLOCX_LG_ALIGN(10), arg_args_raw[1],
  287. "Wrong flags");
  288. free(ptr);
  289. hook_remove(TSDN_NULL, handle);
  290. }
  291. TEST_END
  292. TEST_BEGIN(test_hooks_dalloc_simple) {
  293. /* "Simple" in the sense that we're not in a realloc variant. */
  294. hooks_t hooks = {NULL, &test_dalloc_hook, NULL, (void *)123};
  295. void *handle = hook_install(TSDN_NULL, &hooks);
  296. expect_ptr_ne(handle, NULL, "Hook installation failed");
  297. void *volatile ptr;
  298. /* free() */
  299. reset();
  300. ptr = malloc(1);
  301. free(ptr);
  302. expect_d_eq(call_count, 1, "Hook not called");
  303. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  304. expect_d_eq(arg_type, (int)hook_dalloc_free, "Wrong hook type");
  305. expect_ptr_eq(ptr, arg_address, "Wrong pointer freed");
  306. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
  307. /* dallocx() */
  308. reset();
  309. ptr = malloc(1);
  310. dallocx(ptr, MALLOCX_TCACHE_NONE);
  311. expect_d_eq(call_count, 1, "Hook not called");
  312. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  313. expect_d_eq(arg_type, (int)hook_dalloc_dallocx, "Wrong hook type");
  314. expect_ptr_eq(ptr, arg_address, "Wrong pointer freed");
  315. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
  316. expect_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[1],
  317. "Wrong raw arg");
  318. /* sdallocx() */
  319. reset();
  320. ptr = malloc(1);
  321. sdallocx(ptr, 1, MALLOCX_TCACHE_NONE);
  322. expect_d_eq(call_count, 1, "Hook not called");
  323. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  324. expect_d_eq(arg_type, (int)hook_dalloc_sdallocx, "Wrong hook type");
  325. expect_ptr_eq(ptr, arg_address, "Wrong pointer freed");
  326. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong raw arg");
  327. expect_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong raw arg");
  328. expect_u64_eq((uintptr_t)MALLOCX_TCACHE_NONE, arg_args_raw[2],
  329. "Wrong raw arg");
  330. hook_remove(TSDN_NULL, handle);
  331. }
  332. TEST_END
  333. TEST_BEGIN(test_hooks_expand_simple) {
  334. /* "Simple" in the sense that we're not in a realloc variant. */
  335. hooks_t hooks = {NULL, NULL, &test_expand_hook, (void *)123};
  336. void *handle = hook_install(TSDN_NULL, &hooks);
  337. expect_ptr_ne(handle, NULL, "Hook installation failed");
  338. void *volatile ptr;
  339. /* xallocx() */
  340. reset();
  341. ptr = malloc(1);
  342. size_t new_usize = xallocx(ptr, 100, 200, MALLOCX_TCACHE_NONE);
  343. expect_d_eq(call_count, 1, "Hook not called");
  344. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  345. expect_d_eq(arg_type, (int)hook_expand_xallocx, "Wrong hook type");
  346. expect_ptr_eq(ptr, arg_address, "Wrong pointer expanded");
  347. expect_u64_eq(arg_old_usize, nallocx(1, 0), "Wrong old usize");
  348. expect_u64_eq(arg_new_usize, sallocx(ptr, 0), "Wrong new usize");
  349. expect_u64_eq(new_usize, arg_result_raw, "Wrong result");
  350. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong arg");
  351. expect_u64_eq(100, arg_args_raw[1], "Wrong arg");
  352. expect_u64_eq(200, arg_args_raw[2], "Wrong arg");
  353. expect_u64_eq(MALLOCX_TCACHE_NONE, arg_args_raw[3], "Wrong arg");
  354. hook_remove(TSDN_NULL, handle);
  355. }
  356. TEST_END
  357. TEST_BEGIN(test_hooks_realloc_as_malloc_or_free) {
  358. hooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,
  359. &test_expand_hook, (void *)123};
  360. void *handle = hook_install(TSDN_NULL, &hooks);
  361. expect_ptr_ne(handle, NULL, "Hook installation failed");
  362. void *volatile ptr;
  363. /* realloc(NULL, size) as malloc */
  364. reset();
  365. ptr = realloc(NULL, 1);
  366. expect_d_eq(call_count, 1, "Hook not called");
  367. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  368. expect_d_eq(arg_type, (int)hook_alloc_realloc, "Wrong hook type");
  369. expect_ptr_eq(ptr, arg_result, "Wrong result");
  370. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  371. "Wrong raw result");
  372. expect_u64_eq((uintptr_t)NULL, arg_args_raw[0], "Wrong argument");
  373. expect_u64_eq((uintptr_t)1, arg_args_raw[1], "Wrong argument");
  374. free(ptr);
  375. /* realloc(ptr, 0) as free */
  376. if (opt_zero_realloc_action == zero_realloc_action_free) {
  377. ptr = malloc(1);
  378. reset();
  379. realloc(ptr, 0);
  380. expect_d_eq(call_count, 1, "Hook not called");
  381. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  382. expect_d_eq(arg_type, (int)hook_dalloc_realloc,
  383. "Wrong hook type");
  384. expect_ptr_eq(ptr, arg_address,
  385. "Wrong pointer freed");
  386. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0],
  387. "Wrong raw arg");
  388. expect_u64_eq((uintptr_t)0, arg_args_raw[1],
  389. "Wrong raw arg");
  390. }
  391. /* realloc(NULL, 0) as malloc(0) */
  392. reset();
  393. ptr = realloc(NULL, 0);
  394. expect_d_eq(call_count, 1, "Hook not called");
  395. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  396. expect_d_eq(arg_type, (int)hook_alloc_realloc, "Wrong hook type");
  397. expect_ptr_eq(ptr, arg_result, "Wrong result");
  398. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  399. "Wrong raw result");
  400. expect_u64_eq((uintptr_t)NULL, arg_args_raw[0], "Wrong argument");
  401. expect_u64_eq((uintptr_t)0, arg_args_raw[1], "Wrong argument");
  402. free(ptr);
  403. hook_remove(TSDN_NULL, handle);
  404. }
  405. TEST_END
  406. static void
  407. do_realloc_test(void *(*ralloc)(void *, size_t, int), int flags,
  408. int expand_type, int dalloc_type) {
  409. hooks_t hooks = {&test_alloc_hook, &test_dalloc_hook,
  410. &test_expand_hook, (void *)123};
  411. void *handle = hook_install(TSDN_NULL, &hooks);
  412. expect_ptr_ne(handle, NULL, "Hook installation failed");
  413. void *volatile ptr;
  414. void *volatile ptr2;
  415. /* Realloc in-place, small. */
  416. ptr = malloc(129);
  417. reset();
  418. ptr2 = ralloc(ptr, 130, flags);
  419. expect_ptr_eq(ptr, ptr2, "Small realloc moved");
  420. expect_d_eq(call_count, 1, "Hook not called");
  421. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  422. expect_d_eq(arg_type, expand_type, "Wrong hook type");
  423. expect_ptr_eq(ptr, arg_address, "Wrong address");
  424. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  425. "Wrong raw result");
  426. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong argument");
  427. expect_u64_eq((uintptr_t)130, arg_args_raw[1], "Wrong argument");
  428. free(ptr);
  429. /*
  430. * Realloc in-place, large. Since we can't guarantee the large case
  431. * across all platforms, we stay resilient to moving results.
  432. */
  433. ptr = malloc(2 * 1024 * 1024);
  434. free(ptr);
  435. ptr2 = malloc(1 * 1024 * 1024);
  436. reset();
  437. ptr = ralloc(ptr2, 2 * 1024 * 1024, flags);
  438. /* ptr is the new address, ptr2 is the old address. */
  439. if (ptr == ptr2) {
  440. expect_d_eq(call_count, 1, "Hook not called");
  441. expect_d_eq(arg_type, expand_type, "Wrong hook type");
  442. } else {
  443. expect_d_eq(call_count, 2, "Wrong hooks called");
  444. expect_ptr_eq(ptr, arg_result, "Wrong address");
  445. expect_d_eq(arg_type, dalloc_type, "Wrong hook type");
  446. }
  447. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  448. expect_ptr_eq(ptr2, arg_address, "Wrong address");
  449. expect_u64_eq((uintptr_t)ptr, (uintptr_t)arg_result_raw,
  450. "Wrong raw result");
  451. expect_u64_eq((uintptr_t)ptr2, arg_args_raw[0], "Wrong argument");
  452. expect_u64_eq((uintptr_t)2 * 1024 * 1024, arg_args_raw[1],
  453. "Wrong argument");
  454. free(ptr);
  455. /* Realloc with move, small. */
  456. ptr = malloc(8);
  457. reset();
  458. ptr2 = ralloc(ptr, 128, flags);
  459. expect_ptr_ne(ptr, ptr2, "Small realloc didn't move");
  460. expect_d_eq(call_count, 2, "Hook not called");
  461. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  462. expect_d_eq(arg_type, dalloc_type, "Wrong hook type");
  463. expect_ptr_eq(ptr, arg_address, "Wrong address");
  464. expect_ptr_eq(ptr2, arg_result, "Wrong address");
  465. expect_u64_eq((uintptr_t)ptr2, (uintptr_t)arg_result_raw,
  466. "Wrong raw result");
  467. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong argument");
  468. expect_u64_eq((uintptr_t)128, arg_args_raw[1], "Wrong argument");
  469. free(ptr2);
  470. /* Realloc with move, large. */
  471. ptr = malloc(1);
  472. reset();
  473. ptr2 = ralloc(ptr, 2 * 1024 * 1024, flags);
  474. expect_ptr_ne(ptr, ptr2, "Large realloc didn't move");
  475. expect_d_eq(call_count, 2, "Hook not called");
  476. expect_ptr_eq(arg_extra, (void *)123, "Wrong extra");
  477. expect_d_eq(arg_type, dalloc_type, "Wrong hook type");
  478. expect_ptr_eq(ptr, arg_address, "Wrong address");
  479. expect_ptr_eq(ptr2, arg_result, "Wrong address");
  480. expect_u64_eq((uintptr_t)ptr2, (uintptr_t)arg_result_raw,
  481. "Wrong raw result");
  482. expect_u64_eq((uintptr_t)ptr, arg_args_raw[0], "Wrong argument");
  483. expect_u64_eq((uintptr_t)2 * 1024 * 1024, arg_args_raw[1],
  484. "Wrong argument");
  485. free(ptr2);
  486. hook_remove(TSDN_NULL, handle);
  487. }
  488. static void *
  489. realloc_wrapper(void *ptr, size_t size, UNUSED int flags) {
  490. return realloc(ptr, size);
  491. }
  492. TEST_BEGIN(test_hooks_realloc) {
  493. do_realloc_test(&realloc_wrapper, 0, hook_expand_realloc,
  494. hook_dalloc_realloc);
  495. }
  496. TEST_END
  497. TEST_BEGIN(test_hooks_rallocx) {
  498. do_realloc_test(&rallocx, MALLOCX_TCACHE_NONE, hook_expand_rallocx,
  499. hook_dalloc_rallocx);
  500. }
  501. TEST_END
  502. int
  503. main(void) {
  504. /* We assert on call counts. */
  505. return test_no_reentrancy(
  506. test_hooks_basic,
  507. test_hooks_null,
  508. test_hooks_remove,
  509. test_hooks_alloc_simple,
  510. test_hooks_dalloc_simple,
  511. test_hooks_expand_simple,
  512. test_hooks_realloc_as_malloc_or_free,
  513. test_hooks_realloc,
  514. test_hooks_rallocx);
  515. }