ems_alloc.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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 satisfied.
  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. LOG_ERROR("[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. LOG_ERROR("[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. LOG_ERROR("[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 already 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 BH_ENABLE_GC_CORRUPTION_CHECK != 0
  680. if (heap->is_heap_corrupted) {
  681. os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
  682. return NULL;
  683. }
  684. #endif
  685. LOCK_HEAP(heap);
  686. hmu = alloc_hmu_ex(heap, tot_size);
  687. if (!hmu)
  688. goto finish;
  689. /* Don't memset the memory to improve performance, the caller should
  690. decide whether to memset it or not */
  691. bh_assert(hmu_get_size(hmu) >= tot_size);
  692. /* the total size allocated may be larger than
  693. the required size, reset it here */
  694. tot_size = hmu_get_size(hmu);
  695. #if GC_STAT_DATA != 0
  696. heap->total_size_allocated += tot_size;
  697. #endif
  698. hmu_set_ut(hmu, HMU_WO);
  699. #if GC_MANUALLY != 0
  700. hmu_mark_wo(hmu);
  701. #else
  702. hmu_unmark_wo(hmu);
  703. #endif
  704. #if BH_ENABLE_GC_VERIFY != 0
  705. hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
  706. #endif
  707. ret = hmu_to_obj(hmu);
  708. if (tot_size > tot_size_unaligned)
  709. /* clear buffer appended by GC_ALIGN_8() */
  710. memset((uint8 *)ret + size, 0, tot_size - tot_size_unaligned);
  711. finish:
  712. UNLOCK_HEAP(heap);
  713. return ret;
  714. }
  715. /**
  716. * Do some checking to see if given pointer is a possible valid heap
  717. * @return GC_TRUE if all checking passed, GC_FALSE otherwise
  718. */
  719. int
  720. gci_is_heap_valid(gc_heap_t *heap)
  721. {
  722. if (!heap)
  723. return GC_FALSE;
  724. if (heap->heap_id != (gc_handle_t)heap)
  725. return GC_FALSE;
  726. return GC_TRUE;
  727. }
  728. #if BH_ENABLE_GC_VERIFY == 0
  729. int
  730. gc_free_vo(void *vheap, gc_object_t obj)
  731. #else
  732. int
  733. gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line)
  734. #endif
  735. {
  736. gc_heap_t *heap = (gc_heap_t *)vheap;
  737. gc_uint8 *base_addr, *end_addr;
  738. hmu_t *hmu = NULL;
  739. hmu_t *prev = NULL;
  740. hmu_t *next = NULL;
  741. gc_size_t size = 0;
  742. hmu_type_t ut;
  743. int ret = GC_SUCCESS;
  744. if (!obj) {
  745. return GC_SUCCESS;
  746. }
  747. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  748. if (heap->is_heap_corrupted) {
  749. LOG_ERROR("[GC_ERROR]Heap is corrupted, free memory failed.\n");
  750. return GC_ERROR;
  751. }
  752. #endif
  753. hmu = obj_to_hmu(obj);
  754. base_addr = heap->base_addr;
  755. end_addr = base_addr + heap->current_size;
  756. LOCK_HEAP(heap);
  757. if (hmu_is_in_heap(hmu, base_addr, end_addr)) {
  758. #if BH_ENABLE_GC_VERIFY != 0
  759. hmu_verify(heap, hmu);
  760. #endif
  761. ut = hmu_get_ut(hmu);
  762. if (ut == HMU_VO) {
  763. if (hmu_is_vo_freed(hmu)) {
  764. bh_assert(0);
  765. ret = GC_ERROR;
  766. goto out;
  767. }
  768. size = hmu_get_size(hmu);
  769. heap->total_free_size += size;
  770. #if GC_STAT_DATA != 0
  771. heap->total_size_freed += size;
  772. #endif
  773. if (!hmu_get_pinuse(hmu)) {
  774. prev = (hmu_t *)((char *)hmu - *((int *)hmu - 1));
  775. if (hmu_is_in_heap(prev, base_addr, end_addr)
  776. && hmu_get_ut(prev) == HMU_FC) {
  777. size += hmu_get_size(prev);
  778. hmu = prev;
  779. if (!unlink_hmu(heap, prev)) {
  780. ret = GC_ERROR;
  781. goto out;
  782. }
  783. }
  784. }
  785. next = (hmu_t *)((char *)hmu + size);
  786. if (hmu_is_in_heap(next, base_addr, end_addr)) {
  787. if (hmu_get_ut(next) == HMU_FC) {
  788. size += hmu_get_size(next);
  789. if (!unlink_hmu(heap, next)) {
  790. ret = GC_ERROR;
  791. goto out;
  792. }
  793. next = (hmu_t *)((char *)hmu + size);
  794. }
  795. }
  796. if (!gci_add_fc(heap, hmu, size)) {
  797. ret = GC_ERROR;
  798. goto out;
  799. }
  800. if (hmu_is_in_heap(next, base_addr, end_addr)) {
  801. hmu_unmark_pinuse(next);
  802. }
  803. }
  804. else {
  805. ret = GC_ERROR;
  806. goto out;
  807. }
  808. ret = GC_SUCCESS;
  809. goto out;
  810. }
  811. out:
  812. UNLOCK_HEAP(heap);
  813. return ret;
  814. }
  815. void
  816. gc_dump_heap_stats(gc_heap_t *heap)
  817. {
  818. os_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
  819. os_printf("total free: %" PRIu32 ", current: %" PRIu32
  820. ", highmark: %" PRIu32 "\n",
  821. heap->total_free_size, heap->current_size, heap->highmark_size);
  822. #if GC_STAT_DATA != 0
  823. os_printf("total size allocated: %" PRIu64 ", total size freed: %" PRIu64
  824. ", total occupied: %" PRIu64 "\n",
  825. heap->total_size_allocated, heap->total_size_freed,
  826. heap->total_size_allocated - heap->total_size_freed);
  827. #endif
  828. }
  829. uint32
  830. gc_get_heap_highmark_size(gc_heap_t *heap)
  831. {
  832. return heap->highmark_size;
  833. }
  834. void
  835. gci_dump(gc_heap_t *heap)
  836. {
  837. hmu_t *cur = NULL, *end = NULL;
  838. hmu_type_t ut;
  839. gc_size_t size;
  840. int i = 0, p, mark;
  841. char inuse = 'U';
  842. cur = (hmu_t *)heap->base_addr;
  843. end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  844. while (cur < end) {
  845. ut = hmu_get_ut(cur);
  846. size = hmu_get_size(cur);
  847. p = hmu_get_pinuse(cur);
  848. mark = hmu_is_wo_marked(cur);
  849. if (ut == HMU_VO)
  850. inuse = 'V';
  851. else if (ut == HMU_WO)
  852. inuse = hmu_is_wo_marked(cur) ? 'W' : 'w';
  853. else if (ut == HMU_FC)
  854. inuse = 'F';
  855. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  856. if (size == 0 || size > (uint32)((uint8 *)end - (uint8 *)cur)) {
  857. LOG_ERROR("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
  858. heap->is_heap_corrupted = true;
  859. return;
  860. }
  861. #endif
  862. os_printf("#%d %08" PRIx32 " %" PRIx32 " %d %d"
  863. " %c %" PRId32 "\n",
  864. i, (uint32)((char *)cur - (char *)heap->base_addr),
  865. (uint32)ut, p, mark, inuse, (int32)hmu_obj_size(size));
  866. #if BH_ENABLE_GC_VERIFY != 0
  867. if (inuse == 'V') {
  868. gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1);
  869. os_printf("#%s:%d\n", prefix->file_name, prefix->line_no);
  870. }
  871. #endif
  872. cur = (hmu_t *)((char *)cur + size);
  873. i++;
  874. }
  875. #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
  876. if (cur != end) {
  877. LOG_ERROR("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
  878. heap->is_heap_corrupted = true;
  879. }
  880. #else
  881. bh_assert(cur == end);
  882. #endif
  883. }
  884. #if WASM_ENABLE_GC != 0
  885. extra_info_node_t *
  886. gc_search_extra_info_node(gc_handle_t handle, gc_object_t obj,
  887. gc_size_t *p_index)
  888. {
  889. gc_heap_t *vheap = (gc_heap_t *)handle;
  890. int32 low = 0, high = vheap->extra_info_node_cnt - 1;
  891. int32 mid;
  892. extra_info_node_t *node;
  893. if (!vheap->extra_info_nodes)
  894. return NULL;
  895. while (low <= high) {
  896. mid = (low + high) / 2;
  897. node = vheap->extra_info_nodes[mid];
  898. if (obj == node->obj) {
  899. if (p_index) {
  900. *p_index = mid;
  901. }
  902. return node;
  903. }
  904. else if (obj < node->obj) {
  905. high = mid - 1;
  906. }
  907. else {
  908. low = mid + 1;
  909. }
  910. }
  911. if (p_index) {
  912. *p_index = low;
  913. }
  914. return NULL;
  915. }
  916. static bool
  917. insert_extra_info_node(gc_heap_t *vheap, extra_info_node_t *node)
  918. {
  919. gc_size_t index;
  920. extra_info_node_t *orig_node;
  921. if (!vheap->extra_info_nodes) {
  922. vheap->extra_info_nodes = vheap->extra_info_normal_nodes;
  923. vheap->extra_info_node_capacity = sizeof(vheap->extra_info_normal_nodes)
  924. / sizeof(extra_info_node_t *);
  925. vheap->extra_info_nodes[0] = node;
  926. vheap->extra_info_node_cnt = 1;
  927. return true;
  928. }
  929. /* extend array */
  930. if (vheap->extra_info_node_cnt == vheap->extra_info_node_capacity) {
  931. extra_info_node_t **new_nodes = NULL;
  932. gc_size_t new_capacity = vheap->extra_info_node_capacity * 3 / 2;
  933. gc_size_t total_size = sizeof(extra_info_node_t *) * new_capacity;
  934. new_nodes = (extra_info_node_t **)BH_MALLOC(total_size);
  935. if (!new_nodes) {
  936. LOG_ERROR("alloc extra info nodes failed");
  937. return false;
  938. }
  939. bh_memcpy_s(new_nodes, total_size, vheap->extra_info_nodes,
  940. sizeof(extra_info_node_t *) * vheap->extra_info_node_cnt);
  941. if (vheap->extra_info_nodes != vheap->extra_info_normal_nodes) {
  942. BH_FREE(vheap->extra_info_nodes);
  943. }
  944. vheap->extra_info_nodes = new_nodes;
  945. vheap->extra_info_node_capacity = new_capacity;
  946. }
  947. orig_node = gc_search_extra_info_node(vheap, node->obj, &index);
  948. if (orig_node) {
  949. /* replace the old node */
  950. vheap->extra_info_nodes[index] = node;
  951. BH_FREE(orig_node);
  952. }
  953. else {
  954. bh_memmove_s(vheap->extra_info_nodes + index + 1,
  955. (vheap->extra_info_node_capacity - index - 1)
  956. * sizeof(extra_info_node_t *),
  957. vheap->extra_info_nodes + index,
  958. (vheap->extra_info_node_cnt - index)
  959. * sizeof(extra_info_node_t *));
  960. vheap->extra_info_nodes[index] = node;
  961. vheap->extra_info_node_cnt += 1;
  962. }
  963. return true;
  964. }
  965. bool
  966. gc_set_finalizer(gc_handle_t handle, gc_object_t obj, gc_finalizer_t cb,
  967. void *data)
  968. {
  969. extra_info_node_t *node = NULL;
  970. gc_heap_t *vheap = (gc_heap_t *)handle;
  971. node = (extra_info_node_t *)BH_MALLOC(sizeof(extra_info_node_t));
  972. if (!node) {
  973. LOG_ERROR("alloc a new extra info node failed");
  974. return GC_FALSE;
  975. }
  976. memset(node, 0, sizeof(extra_info_node_t));
  977. node->finalizer = cb;
  978. node->obj = obj;
  979. node->data = data;
  980. LOCK_HEAP(vheap);
  981. if (!insert_extra_info_node(vheap, node)) {
  982. BH_FREE(node);
  983. UNLOCK_HEAP(vheap);
  984. return GC_FALSE;
  985. }
  986. UNLOCK_HEAP(vheap);
  987. gct_vm_set_extra_info_flag(obj, true);
  988. return GC_TRUE;
  989. }
  990. void
  991. gc_unset_finalizer(gc_handle_t handle, gc_object_t obj)
  992. {
  993. gc_size_t index;
  994. gc_heap_t *vheap = (gc_heap_t *)handle;
  995. extra_info_node_t *node;
  996. LOCK_HEAP(vheap);
  997. node = gc_search_extra_info_node(vheap, obj, &index);
  998. if (!node) {
  999. UNLOCK_HEAP(vheap);
  1000. return;
  1001. }
  1002. BH_FREE(node);
  1003. bh_memmove_s(
  1004. vheap->extra_info_nodes + index,
  1005. (vheap->extra_info_node_capacity - index) * sizeof(extra_info_node_t *),
  1006. vheap->extra_info_nodes + index + 1,
  1007. (vheap->extra_info_node_cnt - index - 1) * sizeof(extra_info_node_t *));
  1008. vheap->extra_info_node_cnt -= 1;
  1009. UNLOCK_HEAP(vheap);
  1010. gct_vm_set_extra_info_flag(obj, false);
  1011. }
  1012. #endif