ems_kfc.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. static gc_handle_t
  7. gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size)
  8. {
  9. hmu_tree_node_t *root = NULL, *q = NULL;
  10. int ret;
  11. memset(heap, 0, sizeof *heap);
  12. ret = os_mutex_init(&heap->lock);
  13. if (ret != BHT_OK) {
  14. os_printf("[GC_ERROR]failed to init lock\n");
  15. return NULL;
  16. }
  17. /* init all data structures*/
  18. heap->current_size = heap_max_size;
  19. heap->base_addr = (gc_uint8 *)base_addr;
  20. heap->heap_id = (gc_handle_t)heap;
  21. heap->total_free_size = heap->current_size;
  22. heap->highmark_size = 0;
  23. root = heap->kfc_tree_root = (hmu_tree_node_t *)heap->kfc_tree_root_buf;
  24. memset(root, 0, sizeof *root);
  25. root->size = sizeof *root;
  26. hmu_set_ut(&root->hmu_header, HMU_FC);
  27. hmu_set_size(&root->hmu_header, sizeof *root);
  28. q = (hmu_tree_node_t *)heap->base_addr;
  29. memset(q, 0, sizeof *q);
  30. hmu_set_ut(&q->hmu_header, HMU_FC);
  31. hmu_set_size(&q->hmu_header, heap->current_size);
  32. ASSERT_TREE_NODE_ALIGNED_ACCESS(q);
  33. ASSERT_TREE_NODE_ALIGNED_ACCESS(root);
  34. hmu_mark_pinuse(&q->hmu_header);
  35. root->right = q;
  36. q->parent = root;
  37. q->size = heap->current_size;
  38. bh_assert(root->size <= HMU_FC_NORMAL_MAX_SIZE);
  39. return heap;
  40. }
  41. gc_handle_t
  42. gc_init_with_pool(char *buf, gc_size_t buf_size)
  43. {
  44. char *buf_end = buf + buf_size;
  45. char *buf_aligned = (char *)(((uintptr_t)buf + 7) & (uintptr_t)~7);
  46. char *base_addr = buf_aligned + sizeof(gc_heap_t);
  47. gc_heap_t *heap = (gc_heap_t *)buf_aligned;
  48. gc_size_t heap_max_size;
  49. if (buf_size < APP_HEAP_SIZE_MIN) {
  50. os_printf("[GC_ERROR]heap init buf size (%" PRIu32 ") < %" PRIu32 "\n",
  51. buf_size, (uint32)APP_HEAP_SIZE_MIN);
  52. return NULL;
  53. }
  54. base_addr =
  55. (char *)(((uintptr_t)base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
  56. heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
  57. #if WASM_ENABLE_MEMORY_TRACING != 0
  58. os_printf("Heap created, total size: %u\n", buf_size);
  59. os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
  60. os_printf(" actual heap size: %u\n", heap_max_size);
  61. os_printf(" padding bytes: %u\n",
  62. buf_size - sizeof(gc_heap_t) - heap_max_size);
  63. #endif
  64. return gc_init_internal(heap, base_addr, heap_max_size);
  65. }
  66. gc_handle_t
  67. gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
  68. char *pool_buf, gc_size_t pool_buf_size)
  69. {
  70. gc_heap_t *heap = (gc_heap_t *)struct_buf;
  71. char *base_addr = pool_buf + GC_HEAD_PADDING;
  72. char *pool_buf_end = pool_buf + pool_buf_size;
  73. gc_size_t heap_max_size;
  74. if ((((uintptr_t)struct_buf) & 7) != 0) {
  75. os_printf("[GC_ERROR]heap init struct buf not 8-byte aligned\n");
  76. return NULL;
  77. }
  78. if (struct_buf_size < sizeof(gc_handle_t)) {
  79. os_printf("[GC_ERROR]heap init struct buf size (%" PRIu32 ") < %zu\n",
  80. struct_buf_size, sizeof(gc_handle_t));
  81. return NULL;
  82. }
  83. if ((((uintptr_t)pool_buf) & 7) != 0) {
  84. os_printf("[GC_ERROR]heap init pool buf not 8-byte aligned\n");
  85. return NULL;
  86. }
  87. if (pool_buf_size < APP_HEAP_SIZE_MIN) {
  88. os_printf("[GC_ERROR]heap init buf size (%" PRIu32 ") < %u\n",
  89. pool_buf_size, APP_HEAP_SIZE_MIN);
  90. return NULL;
  91. }
  92. heap_max_size = (uint32)(pool_buf_end - base_addr) & (uint32)~7;
  93. #if WASM_ENABLE_MEMORY_TRACING != 0
  94. os_printf("Heap created, total size: %u\n",
  95. struct_buf_size + pool_buf_size);
  96. os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
  97. os_printf(" actual heap size: %u\n", heap_max_size);
  98. os_printf(" padding bytes: %u\n", pool_buf_size - heap_max_size);
  99. #endif
  100. return gc_init_internal(heap, base_addr, heap_max_size);
  101. }
  102. int
  103. gc_destroy_with_pool(gc_handle_t handle)
  104. {
  105. gc_heap_t *heap = (gc_heap_t *)handle;
  106. int ret = GC_SUCCESS;
  107. #if BH_ENABLE_GC_VERIFY != 0
  108. hmu_t *cur = (hmu_t *)heap->base_addr;
  109. hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  110. if (!heap->is_heap_corrupted
  111. && (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) {
  112. os_printf("Memory leak detected:\n");
  113. gci_dump(heap);
  114. ret = GC_ERROR;
  115. }
  116. #endif
  117. os_mutex_destroy(&heap->lock);
  118. memset(heap, 0, sizeof(gc_heap_t));
  119. return ret;
  120. }
  121. uint32
  122. gc_get_heap_struct_size()
  123. {
  124. return sizeof(gc_heap_t);
  125. }
  126. static void
  127. adjust_ptr(uint8 **p_ptr, intptr_t offset)
  128. {
  129. if (*p_ptr)
  130. *p_ptr = (uint8 *)((intptr_t)(*p_ptr) + offset);
  131. }
  132. int
  133. gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size)
  134. {
  135. gc_heap_t *heap = (gc_heap_t *)handle;
  136. char *base_addr_new = pool_buf_new + GC_HEAD_PADDING;
  137. char *pool_buf_end = pool_buf_new + pool_buf_size;
  138. intptr_t offset = (uint8 *)base_addr_new - (uint8 *)heap->base_addr;
  139. hmu_t *cur = NULL, *end = NULL;
  140. hmu_tree_node_t *tree_node;
  141. uint8 **p_left, **p_right, **p_parent;
  142. gc_size_t heap_max_size, size;
  143. if ((((uintptr_t)pool_buf_new) & 7) != 0) {
  144. os_printf("[GC_ERROR]heap migrate pool buf not 8-byte aligned\n");
  145. return GC_ERROR;
  146. }
  147. heap_max_size = (uint32)(pool_buf_end - base_addr_new) & (uint32)~7;
  148. if (pool_buf_end < base_addr_new || heap_max_size < heap->current_size) {
  149. os_printf("[GC_ERROR]heap migrate invlaid pool buf size\n");
  150. return GC_ERROR;
  151. }
  152. if (offset == 0)
  153. return 0;
  154. if (heap->is_heap_corrupted) {
  155. os_printf("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
  156. return GC_ERROR;
  157. }
  158. heap->base_addr = (uint8 *)base_addr_new;
  159. ASSERT_TREE_NODE_ALIGNED_ACCESS(heap->kfc_tree_root);
  160. p_left = (uint8 **)((uint8 *)heap->kfc_tree_root
  161. + offsetof(hmu_tree_node_t, left));
  162. p_right = (uint8 **)((uint8 *)heap->kfc_tree_root
  163. + offsetof(hmu_tree_node_t, right));
  164. p_parent = (uint8 **)((uint8 *)heap->kfc_tree_root
  165. + offsetof(hmu_tree_node_t, parent));
  166. adjust_ptr(p_left, offset);
  167. adjust_ptr(p_right, offset);
  168. adjust_ptr(p_parent, offset);
  169. cur = (hmu_t *)heap->base_addr;
  170. end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
  171. while (cur < end) {
  172. size = hmu_get_size(cur);
  173. if (size <= 0 || size > (uint32)((uint8 *)end - (uint8 *)cur)) {
  174. os_printf("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
  175. heap->is_heap_corrupted = true;
  176. return GC_ERROR;
  177. }
  178. if (hmu_get_ut(cur) == HMU_FC && !HMU_IS_FC_NORMAL(size)) {
  179. tree_node = (hmu_tree_node_t *)cur;
  180. ASSERT_TREE_NODE_ALIGNED_ACCESS(tree_node);
  181. p_left = (uint8 **)((uint8 *)tree_node
  182. + offsetof(hmu_tree_node_t, left));
  183. p_right = (uint8 **)((uint8 *)tree_node
  184. + offsetof(hmu_tree_node_t, right));
  185. p_parent = (uint8 **)((uint8 *)tree_node
  186. + offsetof(hmu_tree_node_t, parent));
  187. adjust_ptr(p_left, offset);
  188. adjust_ptr(p_right, offset);
  189. if (tree_node->parent != heap->kfc_tree_root)
  190. /* The root node belongs to heap structure,
  191. it is fixed part and isn't changed. */
  192. adjust_ptr(p_parent, offset);
  193. }
  194. cur = (hmu_t *)((char *)cur + size);
  195. }
  196. if (cur != end) {
  197. os_printf("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
  198. heap->is_heap_corrupted = true;
  199. return GC_ERROR;
  200. }
  201. return 0;
  202. }
  203. bool
  204. gc_is_heap_corrupted(gc_handle_t handle)
  205. {
  206. gc_heap_t *heap = (gc_heap_t *)handle;
  207. return heap->is_heap_corrupted ? true : false;
  208. }
  209. #if BH_ENABLE_GC_VERIFY != 0
  210. void
  211. gci_verify_heap(gc_heap_t *heap)
  212. {
  213. hmu_t *cur = NULL, *end = NULL;
  214. bh_assert(heap && gci_is_heap_valid(heap));
  215. cur = (hmu_t *)heap->base_addr;
  216. end = (hmu_t *)(heap->base_addr + heap->current_size);
  217. while (cur < end) {
  218. hmu_verify(heap, cur);
  219. cur = (hmu_t *)((gc_uint8 *)cur + hmu_get_size(cur));
  220. }
  221. bh_assert(cur == end);
  222. }
  223. #endif
  224. void *
  225. gc_heap_stats(void *heap_arg, uint32 *stats, int size)
  226. {
  227. int i;
  228. gc_heap_t *heap = (gc_heap_t *)heap_arg;
  229. for (i = 0; i < size; i++) {
  230. switch (i) {
  231. case GC_STAT_TOTAL:
  232. stats[i] = heap->current_size;
  233. break;
  234. case GC_STAT_FREE:
  235. stats[i] = heap->total_free_size;
  236. break;
  237. case GC_STAT_HIGHMARK:
  238. stats[i] = heap->highmark_size;
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. return heap;
  245. }