ems_kfc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. memset(base_addr, 0, heap_max_size);
  13. ret = os_mutex_init(&heap->lock);
  14. if (ret != BHT_OK) {
  15. os_printf("[GC_ERROR]failed to init lock\n");
  16. return NULL;
  17. }
  18. /* init all data structures*/
  19. heap->current_size = heap_max_size;
  20. heap->base_addr = (gc_uint8*)base_addr;
  21. heap->heap_id = (gc_handle_t)heap;
  22. heap->total_free_size = heap->current_size;
  23. heap->highmark_size = 0;
  24. root = &heap->kfc_tree_root;
  25. memset(root, 0, sizeof *root);
  26. root->size = sizeof *root;
  27. hmu_set_ut(&root->hmu_header, HMU_FC);
  28. hmu_set_size(&root->hmu_header, sizeof *root);
  29. q = (hmu_tree_node_t *) heap->base_addr;
  30. memset(q, 0, sizeof *q);
  31. hmu_set_ut(&q->hmu_header, HMU_FC);
  32. hmu_set_size(&q->hmu_header, heap->current_size);
  33. hmu_mark_pinuse(&q->hmu_header);
  34. root->right = q;
  35. q->parent = root;
  36. q->size = heap->current_size;
  37. bh_assert(root->size <= HMU_FC_NORMAL_MAX_SIZE);
  38. return heap;
  39. }
  40. gc_handle_t
  41. gc_init_with_pool(char *buf, gc_size_t buf_size)
  42. {
  43. char *buf_end = buf + buf_size;
  44. char *buf_aligned = (char*)(((uintptr_t) buf + 7) & (uintptr_t)~7);
  45. char *base_addr = buf_aligned + sizeof(gc_heap_t);
  46. gc_heap_t *heap = (gc_heap_t*)buf_aligned;
  47. gc_size_t heap_max_size;
  48. if (buf_size < APP_HEAP_SIZE_MIN) {
  49. os_printf("[GC_ERROR]heap init buf size (%u) < %u\n",
  50. buf_size, APP_HEAP_SIZE_MIN);
  51. return NULL;
  52. }
  53. base_addr = (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 (%u) < %u\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 (%u) < %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",
  97. pool_buf_size - heap_max_size);
  98. #endif
  99. return gc_init_internal(heap, base_addr, heap_max_size);
  100. }
  101. int
  102. gc_destroy_with_pool(gc_handle_t handle)
  103. {
  104. gc_heap_t *heap = (gc_heap_t *) handle;
  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. #if WASM_ENABLE_SPEC_TEST != 0
  113. while (1);
  114. #endif
  115. }
  116. #endif
  117. os_mutex_destroy(&heap->lock);
  118. memset(heap->base_addr, 0, heap->current_size);
  119. memset(heap, 0, sizeof(gc_heap_t));
  120. return GC_SUCCESS;
  121. }
  122. uint32
  123. gc_get_heap_struct_size()
  124. {
  125. return sizeof(gc_heap_t);
  126. }
  127. static void
  128. adjust_ptr(uint8 **p_ptr, intptr_t offset)
  129. {
  130. if (*p_ptr)
  131. *p_ptr += offset;
  132. }
  133. int
  134. gc_migrate(gc_handle_t handle,
  135. char *pool_buf_new, gc_size_t pool_buf_size)
  136. {
  137. gc_heap_t *heap = (gc_heap_t *)handle;
  138. char *base_addr_new = pool_buf_new + GC_HEAD_PADDING;
  139. char *pool_buf_end = pool_buf_new + pool_buf_size;
  140. intptr_t offset = (uint8*)base_addr_new - (uint8*)heap->base_addr;
  141. hmu_t *cur = NULL, *end = NULL;
  142. hmu_tree_node_t *tree_node;
  143. gc_size_t heap_max_size, size;
  144. if ((((uintptr_t)pool_buf_new) & 7) != 0) {
  145. os_printf("[GC_ERROR]heap migrate pool buf not 8-byte aligned\n");
  146. return GC_ERROR;
  147. }
  148. heap_max_size = (uint32)(pool_buf_end - base_addr_new) & (uint32)~7;
  149. if (pool_buf_end < base_addr_new
  150. || heap_max_size < heap->current_size) {
  151. os_printf("[GC_ERROR]heap migrate invlaid pool buf size\n");
  152. return GC_ERROR;
  153. }
  154. if (offset == 0)
  155. return 0;
  156. heap->base_addr = (uint8*)base_addr_new;
  157. adjust_ptr((uint8**)&heap->kfc_tree_root.left, offset);
  158. adjust_ptr((uint8**)&heap->kfc_tree_root.right, offset);
  159. adjust_ptr((uint8**)&heap->kfc_tree_root.parent, offset);
  160. cur = (hmu_t*)heap->base_addr;
  161. end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
  162. while (cur < end) {
  163. size = hmu_get_size(cur);
  164. bh_assert(size > 0);
  165. if (hmu_get_ut(cur) == HMU_FC && !HMU_IS_FC_NORMAL(size)) {
  166. tree_node = (hmu_tree_node_t *)cur;
  167. adjust_ptr((uint8**)&tree_node->left, offset);
  168. adjust_ptr((uint8**)&tree_node->right, offset);
  169. if (tree_node->parent != &heap->kfc_tree_root)
  170. /* The root node belongs to heap structure,
  171. it is fixed part and isn't changed. */
  172. adjust_ptr((uint8**)&tree_node->parent, offset);
  173. }
  174. cur = (hmu_t*)((char *)cur + size);
  175. }
  176. bh_assert(cur == end);
  177. return 0;
  178. }
  179. bool
  180. gc_is_heap_corrupted(gc_handle_t handle)
  181. {
  182. gc_heap_t *heap = (gc_heap_t *)handle;
  183. return heap->is_heap_corrupted ? true : false;
  184. }
  185. #if BH_ENABLE_GC_VERIFY != 0
  186. void
  187. gci_verify_heap(gc_heap_t *heap)
  188. {
  189. hmu_t *cur = NULL, *end = NULL;
  190. bh_assert(heap && gci_is_heap_valid(heap));
  191. cur = (hmu_t *)heap->base_addr;
  192. end = (hmu_t *)(heap->base_addr + heap->current_size);
  193. while(cur < end) {
  194. hmu_verify(heap, cur);
  195. cur = (hmu_t *)((gc_uint8*)cur + hmu_get_size(cur));
  196. }
  197. bh_assert(cur == end);
  198. }
  199. #endif
  200. void *
  201. gc_heap_stats(void *heap_arg, uint32* stats, int size)
  202. {
  203. int i;
  204. gc_heap_t *heap = (gc_heap_t *) heap_arg;
  205. for (i = 0; i < size; i++) {
  206. switch (i) {
  207. case GC_STAT_TOTAL:
  208. stats[i] = heap->current_size;
  209. break;
  210. case GC_STAT_FREE:
  211. stats[i] = heap->total_free_size;
  212. break;
  213. case GC_STAT_HIGHMARK:
  214. stats[i] = heap->highmark_size;
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. return heap;
  221. }