ems_alloc.c 31 KB

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