ems_kfc.c 7.9 KB

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