ems_alloc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "ems_gc_internal.h"
  6. #if WASM_ENABLE_GC != 0
  7. #define LOCK_HEAP(heap) \
  8. do { \
  9. if (!heap->is_doing_reclaim) \
  10. /* If the heap is doing reclaim, it must have been locked, \
  11. we should not lock the heap again. */ \
  12. os_mutex_lock(&heap->lock); \
  13. } while (0)
  14. #define UNLOCK_HEAP(heap) \
  15. do { \
  16. if (!heap->is_doing_reclaim) \
  17. /* If the heap is doing reclaim, it must have been locked, \
  18. and will be unlocked after reclaim, we should not \
  19. unlock the heap again. */ \
  20. os_mutex_unlock(&heap->lock); \
  21. } while (0)
  22. #else
  23. #define LOCK_HEAP(heap) os_mutex_lock(&heap->lock)
  24. #define UNLOCK_HEAP(heap) os_mutex_unlock(&heap->lock)
  25. #endif
  26. static inline bool
  27. hmu_is_in_heap(void *hmu, gc_uint8 *heap_base_addr, gc_uint8 *heap_end_addr)
  28. {
  29. gc_uint8 *addr = (gc_uint8 *)hmu;
  30. return (addr >= heap_base_addr && addr < heap_end_addr) ? true : false;
  31. }
  32. /**
  33. * Remove a node from the tree it belongs to
  34. *
  35. * @param p the node to remove, can not be NULL, can not be the ROOT node
  36. * the node will be removed from the tree, and the left, right and
  37. * parent pointers of the node @p will be set to be NULL. Other fields
  38. * won't be touched. The tree will be re-organized so that the order
  39. * conditions are still satisified.
  40. */
  41. static bool
  42. remove_tree_node(gc_heap_t *heap, hmu_tree_node_t *p)
  43. {
  44. hmu_tree_node_t *q = NULL, **slot = NULL;
  45. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  46. hmu_tree_node_t *root = heap->kfc_tree_root, *parent;
  47. gc_uint8 *base_addr = heap->base_addr;
  48. gc_uint8 *end_addr = base_addr + heap->current_size;
  49. #endif
  50. bh_assert(p);
  51. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  52. parent = p->parent;
  53. if (!parent || p == root /* p can not be the ROOT node */
  54. || !hmu_is_in_heap(p, base_addr, end_addr)
  55. || (parent != root && !hmu_is_in_heap(parent, base_addr, end_addr))) {
  56. goto fail;
  57. }
  58. #endif
  59. /* get the slot which holds pointer to node p */
  60. if (p == p->parent->right) {
  61. /* Don't use `slot = &p->parent->right` to avoid compiler warning */
  62. slot = (hmu_tree_node_t **)((uint8 *)p->parent
  63. + offsetof(hmu_tree_node_t, right));
  64. }
  65. else if (p == p->parent->left) {
  66. /* p should be a child of its parent */
  67. /* Don't use `slot = &p->parent->left` to avoid compiler warning */
  68. slot = (hmu_tree_node_t **)((uint8 *)p->parent
  69. + offsetof(hmu_tree_node_t, left));
  70. }
  71. else {
  72. goto fail;
  73. }
  74. /**
  75. * algorithms used to remove node p
  76. * case 1: if p has no left child, replace p with its right child
  77. * case 2: if p has no right child, replace p with its left child
  78. * case 3: otherwise, find p's predecessor, remove it from the tree
  79. * and replace p with it.
  80. * use predecessor can keep the left <= root < right condition.
  81. */
  82. if (!p->left) {
  83. /* move right child up*/
  84. *slot = p->right;
  85. if (p->right) {
  86. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  87. if (!hmu_is_in_heap(p->right, base_addr, end_addr)) {
  88. goto fail;
  89. }
  90. #endif
  91. p->right->parent = p->parent;
  92. }
  93. p->left = p->right = p->parent = NULL;
  94. return true;
  95. }
  96. if (!p->right) {
  97. /* move left child up*/
  98. *slot = p->left;
  99. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  100. if (!hmu_is_in_heap(p->left, base_addr, end_addr)) {
  101. goto fail;
  102. }
  103. #endif
  104. /* p->left can never be NULL unless it is corrupted. */
  105. p->left->parent = p->parent;
  106. p->left = p->right = p->parent = NULL;
  107. return true;
  108. }
  109. /* both left & right exist, find p's predecessor at first*/
  110. q = p->left;
  111. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  112. if (!hmu_is_in_heap(q, base_addr, end_addr)) {
  113. goto fail;
  114. }
  115. #endif
  116. while (q->right) {
  117. q = q->right;
  118. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  119. if (!hmu_is_in_heap(q, base_addr, end_addr)) {
  120. goto fail;
  121. }
  122. #endif
  123. }
  124. /* remove from the tree*/
  125. if (!remove_tree_node(heap, q))
  126. return false;
  127. *slot = q;
  128. q->parent = p->parent;
  129. q->left = p->left;
  130. q->right = p->right;
  131. if (q->left) {
  132. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  133. if (!hmu_is_in_heap(q->left, base_addr, end_addr)) {
  134. goto fail;
  135. }
  136. #endif
  137. q->left->parent = q;
  138. }
  139. if (q->right) {
  140. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  141. if (!hmu_is_in_heap(q->right, base_addr, end_addr)) {
  142. goto fail;
  143. }
  144. #endif
  145. q->right->parent = q;
  146. }
  147. p->left = p->right = p->parent = NULL;
  148. return true;
  149. fail:
  150. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  151. heap->is_heap_corrupted = true;
  152. #endif
  153. return false;
  154. }
  155. static bool
  156. unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
  157. {
  158. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  159. gc_uint8 *base_addr, *end_addr;
  160. #endif
  161. gc_size_t size;
  162. bh_assert(gci_is_heap_valid(heap));
  163. bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr
  164. && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
  165. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  166. if (hmu_get_ut(hmu) != HMU_FC) {
  167. heap->is_heap_corrupted = true;
  168. return false;
  169. }
  170. #endif
  171. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  172. base_addr = heap->base_addr;
  173. end_addr = base_addr + heap->current_size;
  174. #endif
  175. size = hmu_get_size(hmu);
  176. if (HMU_IS_FC_NORMAL(size)) {
  177. uint32 node_idx = size >> 3;
  178. hmu_normal_node_t *node_prev = NULL, *node_next;
  179. hmu_normal_node_t *node = heap->kfc_normal_list[node_idx].next;
  180. while (node) {
  181. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  182. if (!hmu_is_in_heap(node, base_addr, end_addr)) {
  183. heap->is_heap_corrupted = true;
  184. return false;
  185. }
  186. #endif
  187. node_next = get_hmu_normal_node_next(node);
  188. if ((hmu_t *)node == hmu) {
  189. if (!node_prev) /* list head */
  190. heap->kfc_normal_list[node_idx].next = node_next;
  191. else
  192. set_hmu_normal_node_next(node_prev, node_next);
  193. break;
  194. }
  195. node_prev = node;
  196. node = node_next;
  197. }
  198. if (!node) {
  199. os_printf("[GC_ERROR]couldn't find the node in the normal list\n");
  200. }
  201. }
  202. else {
  203. if (!remove_tree_node(heap, (hmu_tree_node_t *)hmu))
  204. return false;
  205. }
  206. return true;
  207. }
  208. static void
  209. hmu_set_free_size(hmu_t *hmu)
  210. {
  211. gc_size_t size;
  212. bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
  213. size = hmu_get_size(hmu);
  214. *((uint32 *)((char *)hmu + size) - 1) = size;
  215. }
  216. /**
  217. * Add free chunk back to KFC
  218. *
  219. * @param heap should not be NULL and it should be a valid heap
  220. * @param hmu should not be NULL and it should be a HMU of length @size inside
  221. * @heap hmu should be 8-bytes aligned
  222. * @param size should be positive and multiple of 8
  223. * hmu with size @size will be added into KFC as a new FC.
  224. */
  225. bool
  226. gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
  227. {
  228. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  229. gc_uint8 *base_addr, *end_addr;
  230. #endif
  231. hmu_normal_node_t *np = NULL;
  232. hmu_tree_node_t *root = NULL, *tp = NULL, *node = NULL;
  233. uint32 node_idx;
  234. bh_assert(gci_is_heap_valid(heap));
  235. bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr
  236. && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
  237. bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0);
  238. bh_assert(size > 0
  239. && ((gc_uint8 *)hmu) + size
  240. <= heap->base_addr + heap->current_size);
  241. bh_assert(!(size & 7));
  242. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  243. base_addr = heap->base_addr;
  244. end_addr = base_addr + heap->current_size;
  245. #endif
  246. hmu_set_ut(hmu, HMU_FC);
  247. hmu_set_size(hmu, size);
  248. hmu_set_free_size(hmu);
  249. if (HMU_IS_FC_NORMAL(size)) {
  250. np = (hmu_normal_node_t *)hmu;
  251. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  252. if (!hmu_is_in_heap(np, base_addr, end_addr)) {
  253. heap->is_heap_corrupted = true;
  254. return false;
  255. }
  256. #endif
  257. node_idx = size >> 3;
  258. set_hmu_normal_node_next(np, heap->kfc_normal_list[node_idx].next);
  259. heap->kfc_normal_list[node_idx].next = np;
  260. return true;
  261. }
  262. /* big block */
  263. node = (hmu_tree_node_t *)hmu;
  264. node->size = size;
  265. node->left = node->right = node->parent = NULL;
  266. /* find proper node to link this new node to */
  267. root = heap->kfc_tree_root;
  268. tp = root;
  269. bh_assert(tp->size < size);
  270. while (1) {
  271. if (tp->size < size) {
  272. if (!tp->right) {
  273. tp->right = node;
  274. node->parent = tp;
  275. break;
  276. }
  277. tp = tp->right;
  278. }
  279. else { /* tp->size >= size */
  280. if (!tp->left) {
  281. tp->left = node;
  282. node->parent = tp;
  283. break;
  284. }
  285. tp = tp->left;
  286. }
  287. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  288. if (!hmu_is_in_heap(tp, base_addr, end_addr)) {
  289. heap->is_heap_corrupted = true;
  290. return false;
  291. }
  292. #endif
  293. }
  294. return true;
  295. }
  296. /**
  297. * Find a proper hmu for required memory size
  298. *
  299. * @param heap should not be NULL and should be a valid heap
  300. * @param size should cover the header and should be 8 bytes aligned
  301. * GC will not be performed here.
  302. * Heap extension will not be performed here.
  303. *
  304. * @return hmu allocated if success, which will be aligned to 8 bytes,
  305. * NULL otherwise
  306. */
  307. static hmu_t *
  308. alloc_hmu(gc_heap_t *heap, gc_size_t size)
  309. {
  310. gc_uint8 *base_addr, *end_addr;
  311. hmu_normal_list_t *normal_head = NULL;
  312. hmu_normal_node_t *p = NULL;
  313. uint32 node_idx = 0, init_node_idx = 0;
  314. hmu_tree_node_t *root = NULL, *tp = NULL, *last_tp = NULL;
  315. hmu_t *next, *rest;
  316. uintptr_t tp_ret;
  317. bh_assert(gci_is_heap_valid(heap));
  318. bh_assert(size > 0 && !(size & 7));
  319. #if WASM_ENABLE_GC != 0
  320. /* In doing reclaim, gc must not alloc memory again. */
  321. bh_assert(!heap->is_doing_reclaim);
  322. #endif
  323. base_addr = heap->base_addr;
  324. end_addr = base_addr + heap->current_size;
  325. if (size < GC_SMALLEST_SIZE)
  326. size = GC_SMALLEST_SIZE;
  327. /* check normal list at first*/
  328. if (HMU_IS_FC_NORMAL(size)) {
  329. /* find a non-empty slot in normal_node_list with good size*/
  330. init_node_idx = (size >> 3);
  331. for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT;
  332. node_idx++) {
  333. normal_head = heap->kfc_normal_list + node_idx;
  334. if (normal_head->next)
  335. break;
  336. normal_head = NULL;
  337. }
  338. /* found in normal list*/
  339. if (normal_head) {
  340. bh_assert(node_idx >= init_node_idx);
  341. p = normal_head->next;
  342. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  343. if (!hmu_is_in_heap(p, base_addr, end_addr)) {
  344. heap->is_heap_corrupted = true;
  345. return NULL;
  346. }
  347. #endif
  348. normal_head->next = get_hmu_normal_node_next(p);
  349. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  350. if (((gc_int32)(uintptr_t)hmu_to_obj(p) & 7) != 0) {
  351. heap->is_heap_corrupted = true;
  352. return NULL;
  353. }
  354. #endif
  355. if ((gc_size_t)node_idx != (uint32)init_node_idx
  356. /* with bigger size*/
  357. && ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) {
  358. rest = (hmu_t *)(((char *)p) + size);
  359. if (!gci_add_fc(heap, rest, (node_idx << 3) - size)) {
  360. return NULL;
  361. }
  362. hmu_mark_pinuse(rest);
  363. }
  364. else {
  365. size = node_idx << 3;
  366. next = (hmu_t *)((char *)p + size);
  367. if (hmu_is_in_heap(next, base_addr, end_addr))
  368. hmu_mark_pinuse(next);
  369. }
  370. heap->total_free_size -= size;
  371. if ((heap->current_size - heap->total_free_size)
  372. > heap->highmark_size)
  373. heap->highmark_size =
  374. heap->current_size - heap->total_free_size;
  375. hmu_set_size((hmu_t *)p, size);
  376. return (hmu_t *)p;
  377. }
  378. }
  379. /* need to find a node in tree*/
  380. root = heap->kfc_tree_root;
  381. /* find the best node*/
  382. bh_assert(root);
  383. tp = root->right;
  384. while (tp) {
  385. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  386. if (!hmu_is_in_heap(tp, base_addr, end_addr)) {
  387. heap->is_heap_corrupted = true;
  388. return NULL;
  389. }
  390. #endif
  391. if (tp->size < size) {
  392. tp = tp->right;
  393. continue;
  394. }
  395. /* record the last node with size equal to or bigger than given size*/
  396. last_tp = tp;
  397. tp = tp->left;
  398. }
  399. if (last_tp) {
  400. bh_assert(last_tp->size >= size);
  401. /* alloc in last_p*/
  402. /* remove node last_p from tree*/
  403. if (!remove_tree_node(heap, last_tp))
  404. return NULL;
  405. if (last_tp->size >= size + GC_SMALLEST_SIZE) {
  406. rest = (hmu_t *)((char *)last_tp + size);
  407. if (!gci_add_fc(heap, rest, last_tp->size - size))
  408. return NULL;
  409. hmu_mark_pinuse(rest);
  410. }
  411. else {
  412. size = last_tp->size;
  413. next = (hmu_t *)((char *)last_tp + size);
  414. if (hmu_is_in_heap(next, base_addr, end_addr))
  415. hmu_mark_pinuse(next);
  416. }
  417. heap->total_free_size -= size;
  418. if ((heap->current_size - heap->total_free_size) > heap->highmark_size)
  419. heap->highmark_size = heap->current_size - heap->total_free_size;
  420. hmu_set_size((hmu_t *)last_tp, size);
  421. tp_ret = (uintptr_t)last_tp;
  422. return (hmu_t *)tp_ret;
  423. }
  424. return NULL;
  425. }
  426. #if WASM_ENABLE_GC != 0
  427. static int
  428. do_gc_heap(gc_heap_t *heap)
  429. {
  430. int ret = GC_SUCCESS;
  431. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  432. uint64 start = 0, end = 0, time = 0;
  433. start = os_time_get_boot_microsecond();
  434. #endif
  435. if (heap->is_reclaim_enabled) {
  436. UNLOCK_HEAP(heap);
  437. ret = gci_gc_heap(heap);
  438. LOCK_HEAP(heap);
  439. }
  440. #if WASM_ENABLE_GC_PERF_PROFILING != 0
  441. end = os_time_get_boot_microsecond();
  442. time = end - start;
  443. heap->total_gc_time += time;
  444. if (time > heap->max_gc_time) {
  445. heap->max_gc_time = time;
  446. }
  447. heap->total_gc_count += 1;
  448. #endif
  449. return ret;
  450. }
  451. #endif
  452. /**
  453. * Find a proper HMU with given size
  454. *
  455. * @param heap should not be NULL and should be a valid heap
  456. * @param size should cover the header and should be 8 bytes aligned
  457. *
  458. * Note: This function will try several ways to satisfy the allocation request:
  459. * 1. Find a proper on available HMUs.
  460. * 2. GC will be triggered if 1 failed.
  461. * 3. Find a proper on available HMUS.
  462. * 4. Return NULL if 3 failed
  463. *
  464. * @return hmu allocated if success, which will be aligned to 8 bytes,
  465. * NULL otherwise
  466. */
  467. static hmu_t *
  468. alloc_hmu_ex(gc_heap_t *heap, gc_size_t size)
  469. {
  470. bh_assert(gci_is_heap_valid(heap));
  471. bh_assert(size > 0 && !(size & 7));
  472. #if WASM_ENABLE_GC != 0
  473. #if GC_IN_EVERY_ALLOCATION != 0
  474. if (GC_SUCCESS != do_gc_heap(heap))
  475. return NULL;
  476. #else
  477. if (heap->total_free_size < heap->gc_threshold) {
  478. if (GC_SUCCESS != do_gc_heap(heap))
  479. return NULL;
  480. }
  481. else {
  482. hmu_t *ret = NULL;
  483. if ((ret = alloc_hmu(heap, size))) {
  484. return ret;
  485. }
  486. if (GC_SUCCESS != do_gc_heap(heap))
  487. return NULL;
  488. }
  489. #endif
  490. #endif
  491. return alloc_hmu(heap, size);
  492. }
  493. #if BH_ENABLE_GC_VERIFY == 0
  494. gc_object_t
  495. gc_alloc_vo(void *vheap, gc_size_t size)
  496. #else
  497. gc_object_t
  498. gc_alloc_vo_internal(void *vheap, gc_size_t size, const char *file, int line)
  499. #endif
  500. {
  501. gc_heap_t *heap = (gc_heap_t *)vheap;
  502. hmu_t *hmu = NULL;
  503. gc_object_t ret = (gc_object_t)NULL;
  504. gc_size_t tot_size = 0, tot_size_unaligned;
  505. /* hmu header + prefix + obj + suffix */
  506. tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
  507. /* aligned size*/
  508. tot_size = GC_ALIGN_8(tot_size_unaligned);
  509. if (tot_size < size)
  510. /* integer overflow */
  511. return NULL;
  512. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  513. if (heap->is_heap_corrupted) {
  514. os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
  515. return NULL;
  516. }
  517. #endif
  518. LOCK_HEAP(heap);
  519. hmu = alloc_hmu_ex(heap, tot_size);
  520. if (!hmu)
  521. goto finish;
  522. bh_assert(hmu_get_size(hmu) >= tot_size);
  523. /* the total size allocated may be larger than
  524. the required size, reset it here */
  525. tot_size = hmu_get_size(hmu);
  526. #if GC_STAT_DATA != 0
  527. heap->total_size_allocated += tot_size;
  528. #endif
  529. hmu_set_ut(hmu, HMU_VO);
  530. hmu_unfree_vo(hmu);
  531. #if BH_ENABLE_GC_VERIFY != 0
  532. hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
  533. #endif
  534. ret = hmu_to_obj(hmu);
  535. if (tot_size > tot_size_unaligned)
  536. /* clear buffer appended by GC_ALIGN_8() */
  537. memset((uint8 *)ret + size, 0, tot_size - tot_size_unaligned);
  538. finish:
  539. UNLOCK_HEAP(heap);
  540. return ret;
  541. }
  542. #if BH_ENABLE_GC_VERIFY == 0
  543. gc_object_t
  544. gc_realloc_vo(void *vheap, void *ptr, gc_size_t size)
  545. #else
  546. gc_object_t
  547. gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file,
  548. int line)
  549. #endif
  550. {
  551. gc_heap_t *heap = (gc_heap_t *)vheap;
  552. hmu_t *hmu = NULL, *hmu_old = NULL, *hmu_next;
  553. gc_object_t ret = (gc_object_t)NULL, obj_old = (gc_object_t)ptr;
  554. gc_size_t tot_size, tot_size_unaligned, tot_size_old = 0, tot_size_next;
  555. gc_size_t obj_size, obj_size_old;
  556. gc_uint8 *base_addr, *end_addr;
  557. hmu_type_t ut;
  558. /* hmu header + prefix + obj + suffix */
  559. tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
  560. /* aligned size*/
  561. tot_size = GC_ALIGN_8(tot_size_unaligned);
  562. if (tot_size < size)
  563. /* integer overflow */
  564. return NULL;
  565. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  566. if (heap->is_heap_corrupted) {
  567. os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
  568. return NULL;
  569. }
  570. #endif
  571. if (obj_old) {
  572. hmu_old = obj_to_hmu(obj_old);
  573. tot_size_old = hmu_get_size(hmu_old);
  574. if (tot_size <= tot_size_old)
  575. /* current node alreay meets requirement */
  576. return obj_old;
  577. }
  578. base_addr = heap->base_addr;
  579. end_addr = base_addr + heap->current_size;
  580. LOCK_HEAP(heap);
  581. if (hmu_old) {
  582. hmu_next = (hmu_t *)((char *)hmu_old + tot_size_old);
  583. if (hmu_is_in_heap(hmu_next, base_addr, end_addr)) {
  584. ut = hmu_get_ut(hmu_next);
  585. tot_size_next = hmu_get_size(hmu_next);
  586. if (ut == HMU_FC && tot_size <= tot_size_old + tot_size_next) {
  587. /* current node and next node meets requirement */
  588. if (!unlink_hmu(heap, hmu_next)) {
  589. UNLOCK_HEAP(heap);
  590. return NULL;
  591. }
  592. hmu_set_size(hmu_old, tot_size);
  593. memset((char *)hmu_old + tot_size_old, 0,
  594. tot_size - tot_size_old);
  595. #if BH_ENABLE_GC_VERIFY != 0
  596. hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line);
  597. #endif
  598. if (tot_size < tot_size_old + tot_size_next) {
  599. hmu_next = (hmu_t *)((char *)hmu_old + tot_size);
  600. tot_size_next = tot_size_old + tot_size_next - tot_size;
  601. if (!gci_add_fc(heap, hmu_next, tot_size_next)) {
  602. UNLOCK_HEAP(heap);
  603. return NULL;
  604. }
  605. hmu_mark_pinuse(hmu_next);
  606. }
  607. UNLOCK_HEAP(heap);
  608. return obj_old;
  609. }
  610. }
  611. }
  612. hmu = alloc_hmu_ex(heap, tot_size);
  613. if (!hmu)
  614. goto finish;
  615. bh_assert(hmu_get_size(hmu) >= tot_size);
  616. /* the total size allocated may be larger than
  617. the required size, reset it here */
  618. tot_size = hmu_get_size(hmu);
  619. #if GC_STAT_DATA != 0
  620. heap->total_size_allocated += tot_size;
  621. #endif
  622. hmu_set_ut(hmu, HMU_VO);
  623. hmu_unfree_vo(hmu);
  624. #if BH_ENABLE_GC_VERIFY != 0
  625. hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
  626. #endif
  627. ret = hmu_to_obj(hmu);
  628. finish:
  629. if (ret) {
  630. obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
  631. memset(ret, 0, obj_size);
  632. if (obj_old) {
  633. obj_size_old =
  634. tot_size_old - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
  635. bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
  636. }
  637. }
  638. UNLOCK_HEAP(heap);
  639. if (ret && obj_old)
  640. gc_free_vo(vheap, obj_old);
  641. return ret;
  642. }
  643. #if GC_MANUALLY != 0
  644. void
  645. gc_free_wo(void *vheap, void *ptr)
  646. {
  647. gc_heap_t *heap = (gc_heap_t *)vheap;
  648. gc_object_t *obj = (gc_object_t *)ptr;
  649. hmu_t *hmu = obj_to_hmu(obj);
  650. bh_assert(gci_is_heap_valid(heap));
  651. bh_assert(obj);
  652. bh_assert((gc_uint8 *)hmu >= heap->base_addr
  653. && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
  654. bh_assert(hmu_get_ut(hmu) == HMU_WO);
  655. hmu_unmark_wo(hmu);
  656. (void)heap;
  657. }
  658. #endif
  659. /* see ems_gc.h for description*/
  660. #if BH_ENABLE_GC_VERIFY == 0
  661. gc_object_t
  662. gc_alloc_wo(void *vheap, gc_size_t size)
  663. #else
  664. gc_object_t
  665. gc_alloc_wo_internal(void *vheap, gc_size_t size, const char *file, int line)
  666. #endif
  667. {
  668. gc_heap_t *heap = (gc_heap_t *)vheap;
  669. hmu_t *hmu = NULL;
  670. gc_object_t ret = (gc_object_t)NULL;
  671. gc_size_t tot_size = 0, tot_size_unaligned;
  672. /* hmu header + prefix + obj + suffix */
  673. tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
  674. /* aligned size*/
  675. tot_size = GC_ALIGN_8(tot_size_unaligned);
  676. if (tot_size < size)
  677. /* integer overflow */
  678. return NULL;
  679. if (heap->is_heap_corrupted) {
  680. os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
  681. return NULL;
  682. }
  683. LOCK_HEAP(heap);
  684. hmu = alloc_hmu_ex(heap, tot_size);
  685. if (!hmu)
  686. goto finish;
  687. /* Do we need to memset the memory to 0? */
  688. /* memset((char *)hmu + sizeof(*hmu), 0, tot_size - sizeof(*hmu)); */
  689. bh_assert(hmu_get_size(hmu) >= tot_size);
  690. /* the total size allocated may be larger than
  691. the required size, reset it here */
  692. tot_size = hmu_get_size(hmu);
  693. #if GC_STAT_DATA != 0
  694. heap->total_size_allocated += tot_size;
  695. #endif
  696. hmu_set_ut(hmu, HMU_WO);
  697. #if GC_MANUALLY != 0
  698. hmu_mark_wo(hmu);
  699. #else
  700. hmu_unmark_wo(hmu);
  701. #endif
  702. #if BH_ENABLE_GC_VERIFY != 0
  703. hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
  704. #endif
  705. ret = hmu_to_obj(hmu);
  706. if (tot_size > tot_size_unaligned)
  707. /* clear buffer appended by GC_ALIGN_8() */
  708. memset((uint8 *)ret + size, 0, tot_size - tot_size_unaligned);
  709. finish:
  710. UNLOCK_HEAP(heap);
  711. return ret;
  712. }
  713. /**
  714. * Do some checking to see if given pointer is a possible valid heap
  715. * @return GC_TRUE if all checking passed, GC_FALSE otherwise
  716. */
  717. int
  718. gci_is_heap_valid(gc_heap_t *heap)
  719. {
  720. if (!heap)
  721. return GC_FALSE;
  722. if (heap->heap_id != (gc_handle_t)heap)
  723. return GC_FALSE;
  724. return GC_TRUE;
  725. }
  726. #if BH_ENABLE_GC_VERIFY == 0
  727. int
  728. gc_free_vo(void *vheap, gc_object_t obj)
  729. #else
  730. int
  731. gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line)
  732. #endif
  733. {
  734. gc_heap_t *heap = (gc_heap_t *)vheap;
  735. gc_uint8 *base_addr, *end_addr;
  736. hmu_t *hmu = NULL;
  737. hmu_t *prev = NULL;
  738. hmu_t *next = NULL;
  739. gc_size_t size = 0;
  740. hmu_type_t ut;
  741. int ret = GC_SUCCESS;
  742. if (!obj) {
  743. return GC_SUCCESS;
  744. }
  745. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  746. if (heap->is_heap_corrupted) {
  747. os_printf("[GC_ERROR]Heap is corrupted, free memory failed.\n");
  748. return GC_ERROR;
  749. }
  750. #endif
  751. hmu = obj_to_hmu(obj);
  752. base_addr = heap->base_addr;
  753. end_addr = base_addr + heap->current_size;
  754. LOCK_HEAP(heap);
  755. if (hmu_is_in_heap(hmu, base_addr, end_addr)) {
  756. #if BH_ENABLE_GC_VERIFY != 0
  757. hmu_verify(heap, hmu);
  758. #endif
  759. ut = hmu_get_ut(hmu);
  760. if (ut == HMU_VO) {
  761. if (hmu_is_vo_freed(hmu)) {
  762. bh_assert(0);
  763. ret = GC_ERROR;
  764. goto out;
  765. }
  766. size = hmu_get_size(hmu);
  767. heap->total_free_size += size;
  768. #if GC_STAT_DATA != 0
  769. heap->total_size_freed += size;
  770. #endif
  771. if (!hmu_get_pinuse(hmu)) {
  772. prev = (hmu_t *)((char *)hmu - *((int *)hmu - 1));
  773. if (hmu_is_in_heap(prev, base_addr, end_addr)
  774. && hmu_get_ut(prev) == HMU_FC) {
  775. size += hmu_get_size(prev);
  776. hmu = prev;
  777. if (!unlink_hmu(heap, prev)) {
  778. ret = GC_ERROR;
  779. goto out;
  780. }
  781. }
  782. }
  783. next = (hmu_t *)((char *)hmu + size);
  784. if (hmu_is_in_heap(next, base_addr, end_addr)) {
  785. if (hmu_get_ut(next) == HMU_FC) {
  786. size += hmu_get_size(next);
  787. if (!unlink_hmu(heap, next)) {
  788. ret = GC_ERROR;
  789. goto out;
  790. }
  791. next = (hmu_t *)((char *)hmu + size);
  792. }
  793. }
  794. if (!gci_add_fc(heap, hmu, size)) {
  795. ret = GC_ERROR;
  796. goto out;
  797. }
  798. if (hmu_is_in_heap(next, base_addr, end_addr)) {
  799. hmu_unmark_pinuse(next);
  800. }
  801. }
  802. else {
  803. ret = GC_ERROR;
  804. goto out;
  805. }
  806. ret = GC_SUCCESS;
  807. goto out;
  808. }
  809. out:
  810. UNLOCK_HEAP(heap);
  811. return ret;
  812. }
  813. void
  814. gc_dump_heap_stats(gc_heap_t *heap)
  815. {
  816. os_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
  817. os_printf("total free: %" PRIu32 ", current: %" PRIu32
  818. ", highmark: %" PRIu32 "\n",
  819. heap->total_free_size, heap->current_size, heap->highmark_size);
  820. #if GC_STAT_DATA != 0
  821. os_printf("total size allocated: %" PRIu64 ", total size freed: %" PRIu64
  822. ", total occupied: %" PRIu64 "\n",
  823. heap->total_size_allocated, heap->total_size_freed,
  824. heap->total_size_allocated - heap->total_size_freed);
  825. #endif
  826. }
  827. uint32
  828. gc_get_heap_highmark_size(gc_heap_t *heap)
  829. {
  830. return heap->highmark_size;
  831. }
  832. void
  833. gci_dump(gc_heap_t *heap)
  834. {
  835. hmu_t *cur = NULL, *end = NULL;
  836. hmu_type_t ut;
  837. gc_size_t size;
  838. int i = 0, p, mark;
  839. char inuse = 'U';
  840. cur = (hmu_t *)heap->base_addr;
  841. end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  842. while (cur < end) {
  843. ut = hmu_get_ut(cur);
  844. size = hmu_get_size(cur);
  845. p = hmu_get_pinuse(cur);
  846. mark = hmu_is_wo_marked(cur);
  847. if (ut == HMU_VO)
  848. inuse = 'V';
  849. else if (ut == HMU_WO)
  850. inuse = hmu_is_wo_marked(cur) ? 'W' : 'w';
  851. else if (ut == HMU_FC)
  852. inuse = 'F';
  853. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  854. if (size == 0 || size > (uint32)((uint8 *)end - (uint8 *)cur)) {
  855. os_printf("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
  856. heap->is_heap_corrupted = true;
  857. return;
  858. }
  859. #endif
  860. os_printf("#%d %08" PRIx32 " %" PRIx32 " %d %d"
  861. " %c %" PRId32 "\n",
  862. i, (int32)((char *)cur - (char *)heap->base_addr), (int32)ut,
  863. p, mark, inuse, (int32)hmu_obj_size(size));
  864. #if BH_ENABLE_GC_VERIFY != 0
  865. if (inuse == 'V') {
  866. gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1);
  867. os_printf("#%s:%d\n", prefix->file_name, prefix->line_no);
  868. }
  869. #endif
  870. cur = (hmu_t *)((char *)cur + size);
  871. i++;
  872. }
  873. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  874. if (cur != end) {
  875. os_printf("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
  876. heap->is_heap_corrupted = true;
  877. }
  878. #else
  879. bh_assert(cur == end);
  880. #endif
  881. }
  882. #if WASM_ENABLE_GC != 0
  883. extra_info_node_t *
  884. gc_search_extra_info_node(gc_handle_t handle, gc_object_t obj,
  885. gc_size_t *p_index)
  886. {
  887. gc_heap_t *vheap = (gc_heap_t *)handle;
  888. int32 low = 0, high = vheap->extra_info_node_cnt - 1;
  889. int32 mid;
  890. extra_info_node_t *node;
  891. if (!vheap->extra_info_nodes)
  892. return NULL;
  893. while (low <= high) {
  894. mid = (low + high) / 2;
  895. node = vheap->extra_info_nodes[mid];
  896. if (obj == node->obj) {
  897. if (p_index) {
  898. *p_index = mid;
  899. }
  900. return node;
  901. }
  902. else if (obj < node->obj) {
  903. high = mid - 1;
  904. }
  905. else {
  906. low = mid + 1;
  907. }
  908. }
  909. if (p_index) {
  910. *p_index = low;
  911. }
  912. return NULL;
  913. }
  914. static bool
  915. insert_extra_info_node(gc_heap_t *vheap, extra_info_node_t *node)
  916. {
  917. gc_size_t index;
  918. extra_info_node_t *orig_node;
  919. if (!vheap->extra_info_nodes) {
  920. vheap->extra_info_nodes = vheap->extra_info_normal_nodes;
  921. vheap->extra_info_node_capacity = sizeof(vheap->extra_info_normal_nodes)
  922. / sizeof(extra_info_node_t *);
  923. vheap->extra_info_nodes[0] = node;
  924. vheap->extra_info_node_cnt = 1;
  925. return true;
  926. }
  927. /* extend array */
  928. if (vheap->extra_info_node_cnt == vheap->extra_info_node_capacity) {
  929. extra_info_node_t **new_nodes = NULL;
  930. gc_size_t new_capacity = vheap->extra_info_node_capacity * 3 / 2;
  931. gc_size_t total_size = sizeof(extra_info_node_t *) * new_capacity;
  932. new_nodes = (extra_info_node_t **)BH_MALLOC(total_size);
  933. if (!new_nodes) {
  934. LOG_ERROR("alloc extra info nodes failed");
  935. return false;
  936. }
  937. bh_memcpy_s(new_nodes, total_size, vheap->extra_info_nodes,
  938. sizeof(extra_info_node_t *) * vheap->extra_info_node_cnt);
  939. if (vheap->extra_info_nodes != vheap->extra_info_normal_nodes) {
  940. BH_FREE(vheap->extra_info_nodes);
  941. }
  942. vheap->extra_info_nodes = new_nodes;
  943. vheap->extra_info_node_capacity = new_capacity;
  944. }
  945. orig_node = gc_search_extra_info_node(vheap, node->obj, &index);
  946. if (orig_node) {
  947. /* replace the old node */
  948. vheap->extra_info_nodes[index] = node;
  949. BH_FREE(orig_node);
  950. }
  951. else {
  952. bh_memmove_s(vheap->extra_info_nodes + index + 1,
  953. (vheap->extra_info_node_capacity - index - 1)
  954. * sizeof(extra_info_node_t *),
  955. vheap->extra_info_nodes + index,
  956. (vheap->extra_info_node_cnt - index)
  957. * sizeof(extra_info_node_t *));
  958. vheap->extra_info_nodes[index] = node;
  959. vheap->extra_info_node_cnt += 1;
  960. }
  961. return true;
  962. }
  963. bool
  964. gc_set_finalizer(gc_handle_t handle, gc_object_t obj, gc_finalizer_t cb,
  965. void *data)
  966. {
  967. extra_info_node_t *node = NULL;
  968. gc_heap_t *vheap = (gc_heap_t *)handle;
  969. node = (extra_info_node_t *)BH_MALLOC(sizeof(extra_info_node_t));
  970. if (!node) {
  971. LOG_ERROR("alloc a new extra info node failed");
  972. return GC_FALSE;
  973. }
  974. memset(node, 0, sizeof(extra_info_node_t));
  975. node->finalizer = cb;
  976. node->obj = obj;
  977. node->data = data;
  978. LOCK_HEAP(vheap);
  979. if (!insert_extra_info_node(vheap, node)) {
  980. BH_FREE(node);
  981. UNLOCK_HEAP(vheap);
  982. return GC_FALSE;
  983. }
  984. UNLOCK_HEAP(vheap);
  985. gct_vm_set_extra_info_flag(obj, true);
  986. return GC_TRUE;
  987. }
  988. void
  989. gc_unset_finalizer(gc_handle_t handle, gc_object_t obj)
  990. {
  991. gc_size_t index;
  992. gc_heap_t *vheap = (gc_heap_t *)handle;
  993. extra_info_node_t *node;
  994. LOCK_HEAP(vheap);
  995. node = gc_search_extra_info_node(vheap, obj, &index);
  996. if (!node) {
  997. UNLOCK_HEAP(vheap);
  998. return;
  999. }
  1000. BH_FREE(node);
  1001. bh_memmove_s(
  1002. vheap->extra_info_nodes + index,
  1003. (vheap->extra_info_node_capacity - index) * sizeof(extra_info_node_t *),
  1004. vheap->extra_info_nodes + index + 1,
  1005. (vheap->extra_info_node_cnt - index - 1) * sizeof(extra_info_node_t *));
  1006. vheap->extra_info_node_cnt -= 1;
  1007. UNLOCK_HEAP(vheap);
  1008. gct_vm_set_extra_info_flag(obj, false);
  1009. }
  1010. #endif