krillplayer-plugin 0.5.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/LICENSE +2 -1
  2. package/README.md +2 -0
  3. package/dist/cli.js +397 -80
  4. package/dist/crypto.js +38 -11
  5. package/dist/engine/engine.c +6 -3
  6. package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
  7. package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
  8. package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
  9. package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
  10. package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
  11. package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
  12. package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
  13. package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
  14. package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
  15. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
  16. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
  17. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
  18. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
  19. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
  20. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
  21. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
  22. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
  23. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
  24. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
  25. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
  26. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
  27. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
  28. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
  29. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
  30. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
  31. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
  32. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
  33. package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
  34. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
  35. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
  36. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
  37. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
  38. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
  39. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
  40. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
  41. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
  42. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
  43. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
  44. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
  45. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
  46. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
  47. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
  48. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
  49. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
  50. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
  51. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
  52. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
  53. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
  54. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
  55. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
  56. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
  57. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
  58. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
  59. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
  60. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
  61. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
  62. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
  63. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
  64. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
  65. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
  66. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
  67. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
  68. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
  69. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
  70. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
  71. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
  72. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
  73. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
  74. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
  75. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
  76. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
  77. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
  78. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
  79. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
  80. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
  81. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
  82. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
  83. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
  84. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
  85. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
  86. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
  87. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
  88. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
  89. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
  90. package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
  91. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
  92. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
  93. package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
  94. package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
  95. package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
  96. package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
  97. package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
  98. package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
  99. package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
  100. package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
  101. package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
  102. package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
  103. package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
  104. package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
  105. package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
  106. package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
  107. package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
  108. package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
  109. package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
  110. package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
  111. package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
  112. package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
  113. package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
  114. package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
  115. package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
  116. package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
  117. package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
  118. package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
  119. package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
  120. package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
  121. package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
  122. package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
  123. package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
  124. package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
  125. package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
  126. package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
  127. package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
  128. package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
  129. package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
  130. package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
  131. package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
  132. package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
  133. package/kernel/Tests/KernelTests/CasePins.js +50 -0
  134. package/package.json +2 -2
  135. package/skill/krillplayer-plugin/SKILL.md +2 -0
  136. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
  137. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
  138. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
  139. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
  140. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
  141. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
  142. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
  143. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +46 -0
  144. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +46 -0
  145. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +21 -1
  146. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +39 -4
  147. package/skill/krillplayer-plugin/references/cases.md +20 -2
  148. package/skill/krillplayer-plugin/references/contract.d.ts +114 -12
  149. package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
  150. package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
@@ -0,0 +1,1169 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "ems_gc_internal.h"
7
+
8
+ #if WASM_ENABLE_GC != 0
9
+ #define LOCK_HEAP(heap) \
10
+ do { \
11
+ if (!heap->is_doing_reclaim) \
12
+ /* If the heap is doing reclaim, it must have been locked, \
13
+ we should not lock the heap again. */ \
14
+ os_mutex_lock(&heap->lock); \
15
+ } while (0)
16
+ #define UNLOCK_HEAP(heap) \
17
+ do { \
18
+ if (!heap->is_doing_reclaim) \
19
+ /* If the heap is doing reclaim, it must have been locked, \
20
+ and will be unlocked after reclaim, we should not \
21
+ unlock the heap again. */ \
22
+ os_mutex_unlock(&heap->lock); \
23
+ } while (0)
24
+ #else
25
+ #define LOCK_HEAP(heap) os_mutex_lock(&heap->lock)
26
+ #define UNLOCK_HEAP(heap) os_mutex_unlock(&heap->lock)
27
+ #endif
28
+
29
+ static inline bool
30
+ hmu_is_in_heap(void *hmu, gc_uint8 *heap_base_addr, gc_uint8 *heap_end_addr)
31
+ {
32
+ gc_uint8 *addr = (gc_uint8 *)hmu;
33
+ return (addr >= heap_base_addr && addr < heap_end_addr) ? true : false;
34
+ }
35
+
36
+ /**
37
+ * Remove a node from the tree it belongs to
38
+ *
39
+ * @param p the node to remove, can not be NULL, can not be the ROOT node
40
+ * the node will be removed from the tree, and the left, right and
41
+ * parent pointers of the node @p will be set to be NULL. Other fields
42
+ * won't be touched. The tree will be re-organized so that the order
43
+ * conditions are still satisfied.
44
+ */
45
+ static bool
46
+ remove_tree_node(gc_heap_t *heap, hmu_tree_node_t *p)
47
+ {
48
+ hmu_tree_node_t *q = NULL, **slot = NULL;
49
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
50
+ hmu_tree_node_t *root = heap->kfc_tree_root, *parent;
51
+ gc_uint8 *base_addr = heap->base_addr;
52
+ gc_uint8 *end_addr = base_addr + heap->current_size;
53
+ #endif
54
+
55
+ bh_assert(p);
56
+
57
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
58
+ parent = p->parent;
59
+ if (!parent || p == root /* p can not be the ROOT node */
60
+ || !hmu_is_in_heap(p, base_addr, end_addr)
61
+ || (parent != root && !hmu_is_in_heap(parent, base_addr, end_addr))) {
62
+ goto fail;
63
+ }
64
+ #endif
65
+
66
+ /* get the slot which holds pointer to node p */
67
+ if (p == p->parent->right) {
68
+ /* Don't use `slot = &p->parent->right` to avoid compiler warning */
69
+ slot = (hmu_tree_node_t **)((uint8 *)p->parent
70
+ + offsetof(hmu_tree_node_t, right));
71
+ }
72
+ else if (p == p->parent->left) {
73
+ /* p should be a child of its parent */
74
+ /* Don't use `slot = &p->parent->left` to avoid compiler warning */
75
+ slot = (hmu_tree_node_t **)((uint8 *)p->parent
76
+ + offsetof(hmu_tree_node_t, left));
77
+ }
78
+ else {
79
+ goto fail;
80
+ }
81
+
82
+ /**
83
+ * algorithms used to remove node p
84
+ * case 1: if p has no left child, replace p with its right child
85
+ * case 2: if p has no right child, replace p with its left child
86
+ * case 3: otherwise, find p's predecessor, remove it from the tree
87
+ * and replace p with it.
88
+ * use predecessor can keep the left <= root < right condition.
89
+ */
90
+
91
+ if (!p->left) {
92
+ /* move right child up*/
93
+ *slot = p->right;
94
+ if (p->right) {
95
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
96
+ if (!hmu_is_in_heap(p->right, base_addr, end_addr)) {
97
+ goto fail;
98
+ }
99
+ #endif
100
+ p->right->parent = p->parent;
101
+ }
102
+
103
+ p->left = p->right = p->parent = NULL;
104
+ return true;
105
+ }
106
+
107
+ if (!p->right) {
108
+ /* move left child up*/
109
+ *slot = p->left;
110
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
111
+ if (!hmu_is_in_heap(p->left, base_addr, end_addr)) {
112
+ goto fail;
113
+ }
114
+ #endif
115
+ /* p->left can never be NULL unless it is corrupted. */
116
+ p->left->parent = p->parent;
117
+
118
+ p->left = p->right = p->parent = NULL;
119
+ return true;
120
+ }
121
+
122
+ /* both left & right exist, find p's predecessor at first*/
123
+ q = p->left;
124
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
125
+ if (!hmu_is_in_heap(q, base_addr, end_addr)) {
126
+ goto fail;
127
+ }
128
+ #endif
129
+ while (q->right) {
130
+ q = q->right;
131
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
132
+ if (!hmu_is_in_heap(q, base_addr, end_addr)) {
133
+ goto fail;
134
+ }
135
+ #endif
136
+ }
137
+
138
+ /* remove from the tree*/
139
+ if (!remove_tree_node(heap, q))
140
+ return false;
141
+
142
+ *slot = q;
143
+ q->parent = p->parent;
144
+ q->left = p->left;
145
+ q->right = p->right;
146
+ if (q->left) {
147
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
148
+ if (!hmu_is_in_heap(q->left, base_addr, end_addr)) {
149
+ goto fail;
150
+ }
151
+ #endif
152
+ q->left->parent = q;
153
+ }
154
+ if (q->right) {
155
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
156
+ if (!hmu_is_in_heap(q->right, base_addr, end_addr)) {
157
+ goto fail;
158
+ }
159
+ #endif
160
+ q->right->parent = q;
161
+ }
162
+
163
+ p->left = p->right = p->parent = NULL;
164
+
165
+ return true;
166
+ fail:
167
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
168
+ heap->is_heap_corrupted = true;
169
+ #endif
170
+ return false;
171
+ }
172
+
173
+ static bool
174
+ unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
175
+ {
176
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
177
+ gc_uint8 *base_addr, *end_addr;
178
+ #endif
179
+ gc_size_t size;
180
+
181
+ bh_assert(gci_is_heap_valid(heap));
182
+ bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr
183
+ && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
184
+
185
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
186
+ if (hmu_get_ut(hmu) != HMU_FC) {
187
+ heap->is_heap_corrupted = true;
188
+ return false;
189
+ }
190
+ #endif
191
+
192
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
193
+ base_addr = heap->base_addr;
194
+ end_addr = base_addr + heap->current_size;
195
+ #endif
196
+ size = hmu_get_size(hmu);
197
+
198
+ if (HMU_IS_FC_NORMAL(size)) {
199
+ uint32 node_idx = size >> 3;
200
+ hmu_normal_node_t *node_prev = NULL, *node_next;
201
+ hmu_normal_node_t *node = heap->kfc_normal_list[node_idx].next;
202
+
203
+ while (node) {
204
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
205
+ if (!hmu_is_in_heap(node, base_addr, end_addr)) {
206
+ heap->is_heap_corrupted = true;
207
+ return false;
208
+ }
209
+ #endif
210
+ node_next = get_hmu_normal_node_next(node);
211
+ if ((hmu_t *)node == hmu) {
212
+ if (!node_prev) /* list head */
213
+ heap->kfc_normal_list[node_idx].next = node_next;
214
+ else
215
+ set_hmu_normal_node_next(node_prev, node_next);
216
+ break;
217
+ }
218
+ node_prev = node;
219
+ node = node_next;
220
+ }
221
+
222
+ if (!node) {
223
+ LOG_ERROR("[GC_ERROR]couldn't find the node in the normal list\n");
224
+ }
225
+ }
226
+ else {
227
+ if (!remove_tree_node(heap, (hmu_tree_node_t *)hmu))
228
+ return false;
229
+ }
230
+ return true;
231
+ }
232
+
233
+ static void
234
+ hmu_set_free_size(hmu_t *hmu)
235
+ {
236
+ gc_size_t size;
237
+ bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
238
+
239
+ size = hmu_get_size(hmu);
240
+ *((uint32 *)((char *)hmu + size) - 1) = size;
241
+ }
242
+
243
+ /**
244
+ * Add free chunk back to KFC
245
+ *
246
+ * @param heap should not be NULL and it should be a valid heap
247
+ * @param hmu should not be NULL and it should be a HMU of length @size inside
248
+ * @heap hmu should be 8-bytes aligned
249
+ * @param size should be positive and multiple of 8
250
+ * hmu with size @size will be added into KFC as a new FC.
251
+ */
252
+ bool
253
+ gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
254
+ {
255
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
256
+ gc_uint8 *base_addr, *end_addr;
257
+ #endif
258
+ hmu_normal_node_t *np = NULL;
259
+ hmu_tree_node_t *root = NULL, *tp = NULL, *node = NULL;
260
+ uint32 node_idx;
261
+
262
+ bh_assert(gci_is_heap_valid(heap));
263
+ bh_assert(hmu && (gc_uint8 *)hmu >= heap->base_addr
264
+ && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
265
+ bh_assert(((gc_uint32)(uintptr_t)hmu_to_obj(hmu) & 7) == 0);
266
+ bh_assert(size > 0
267
+ && ((gc_uint8 *)hmu) + size
268
+ <= heap->base_addr + heap->current_size);
269
+ bh_assert(!(size & 7));
270
+
271
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
272
+ base_addr = heap->base_addr;
273
+ end_addr = base_addr + heap->current_size;
274
+ #endif
275
+
276
+ hmu_set_ut(hmu, HMU_FC);
277
+ hmu_set_size(hmu, size);
278
+ hmu_set_free_size(hmu);
279
+
280
+ if (HMU_IS_FC_NORMAL(size)) {
281
+ np = (hmu_normal_node_t *)hmu;
282
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
283
+ if (!hmu_is_in_heap(np, base_addr, end_addr)) {
284
+ heap->is_heap_corrupted = true;
285
+ return false;
286
+ }
287
+ #endif
288
+
289
+ node_idx = size >> 3;
290
+ set_hmu_normal_node_next(np, heap->kfc_normal_list[node_idx].next);
291
+ heap->kfc_normal_list[node_idx].next = np;
292
+ return true;
293
+ }
294
+
295
+ /* big block */
296
+ node = (hmu_tree_node_t *)hmu;
297
+ node->size = size;
298
+ node->left = node->right = node->parent = NULL;
299
+
300
+ /* find proper node to link this new node to */
301
+ root = heap->kfc_tree_root;
302
+ tp = root;
303
+ bh_assert(tp->size < size);
304
+ while (1) {
305
+ if (tp->size < size) {
306
+ if (!tp->right) {
307
+ tp->right = node;
308
+ node->parent = tp;
309
+ break;
310
+ }
311
+ tp = tp->right;
312
+ }
313
+ else { /* tp->size >= size */
314
+ if (!tp->left) {
315
+ tp->left = node;
316
+ node->parent = tp;
317
+ break;
318
+ }
319
+ tp = tp->left;
320
+ }
321
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
322
+ if (!hmu_is_in_heap(tp, base_addr, end_addr)) {
323
+ heap->is_heap_corrupted = true;
324
+ return false;
325
+ }
326
+ #endif
327
+ }
328
+ return true;
329
+ }
330
+
331
+ /**
332
+ * Find a proper hmu for required memory size
333
+ *
334
+ * @param heap should not be NULL and should be a valid heap
335
+ * @param size should cover the header and should be 8 bytes aligned
336
+ * GC will not be performed here.
337
+ * Heap extension will not be performed here.
338
+ *
339
+ * @return hmu allocated if success, which will be aligned to 8 bytes,
340
+ * NULL otherwise
341
+ */
342
+ static hmu_t *
343
+ alloc_hmu(gc_heap_t *heap, gc_size_t size)
344
+ {
345
+ gc_uint8 *base_addr, *end_addr;
346
+ hmu_normal_list_t *normal_head = NULL;
347
+ hmu_normal_node_t *p = NULL;
348
+ uint32 node_idx = 0, init_node_idx = 0;
349
+ hmu_tree_node_t *root = NULL, *tp = NULL, *last_tp = NULL;
350
+ hmu_t *next, *rest;
351
+ uintptr_t tp_ret;
352
+
353
+ bh_assert(gci_is_heap_valid(heap));
354
+ bh_assert(size > 0 && !(size & 7));
355
+
356
+ #if WASM_ENABLE_GC != 0
357
+ /* In doing reclaim, gc must not alloc memory again. */
358
+ bh_assert(!heap->is_doing_reclaim);
359
+ #endif
360
+
361
+ base_addr = heap->base_addr;
362
+ end_addr = base_addr + heap->current_size;
363
+
364
+ if (size < GC_SMALLEST_SIZE)
365
+ size = GC_SMALLEST_SIZE;
366
+
367
+ /* check normal list at first*/
368
+ if (HMU_IS_FC_NORMAL(size)) {
369
+ /* find a non-empty slot in normal_node_list with good size*/
370
+ init_node_idx = (size >> 3);
371
+ for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT;
372
+ node_idx++) {
373
+ normal_head = heap->kfc_normal_list + node_idx;
374
+ if (normal_head->next)
375
+ break;
376
+ normal_head = NULL;
377
+ }
378
+
379
+ /* found in normal list*/
380
+ if (normal_head) {
381
+ bh_assert(node_idx >= init_node_idx);
382
+
383
+ p = normal_head->next;
384
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
385
+ if (!hmu_is_in_heap(p, base_addr, end_addr)) {
386
+ heap->is_heap_corrupted = true;
387
+ return NULL;
388
+ }
389
+ #endif
390
+ normal_head->next = get_hmu_normal_node_next(p);
391
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
392
+ if (((gc_int32)(uintptr_t)hmu_to_obj(p) & 7) != 0) {
393
+ heap->is_heap_corrupted = true;
394
+ return NULL;
395
+ }
396
+ #endif
397
+
398
+ if ((gc_size_t)node_idx != (uint32)init_node_idx
399
+ /* with bigger size*/
400
+ && ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) {
401
+ rest = (hmu_t *)(((char *)p) + size);
402
+ if (!gci_add_fc(heap, rest, (node_idx << 3) - size)) {
403
+ return NULL;
404
+ }
405
+ hmu_mark_pinuse(rest);
406
+ }
407
+ else {
408
+ size = node_idx << 3;
409
+ next = (hmu_t *)((char *)p + size);
410
+ if (hmu_is_in_heap(next, base_addr, end_addr))
411
+ hmu_mark_pinuse(next);
412
+ }
413
+
414
+ heap->total_free_size -= size;
415
+ if ((heap->current_size - heap->total_free_size)
416
+ > heap->highmark_size)
417
+ heap->highmark_size =
418
+ heap->current_size - heap->total_free_size;
419
+
420
+ hmu_set_size((hmu_t *)p, size);
421
+ return (hmu_t *)p;
422
+ }
423
+ }
424
+
425
+ /* need to find a node in tree*/
426
+ root = heap->kfc_tree_root;
427
+
428
+ /* find the best node*/
429
+ bh_assert(root);
430
+ tp = root->right;
431
+ while (tp) {
432
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
433
+ if (!hmu_is_in_heap(tp, base_addr, end_addr)) {
434
+ heap->is_heap_corrupted = true;
435
+ return NULL;
436
+ }
437
+ #endif
438
+
439
+ if (tp->size < size) {
440
+ tp = tp->right;
441
+ continue;
442
+ }
443
+
444
+ /* record the last node with size equal to or bigger than given size*/
445
+ last_tp = tp;
446
+ tp = tp->left;
447
+ }
448
+
449
+ if (last_tp) {
450
+ bh_assert(last_tp->size >= size);
451
+
452
+ /* alloc in last_p*/
453
+
454
+ /* remove node last_p from tree*/
455
+ if (!remove_tree_node(heap, last_tp))
456
+ return NULL;
457
+
458
+ if (last_tp->size >= size + GC_SMALLEST_SIZE) {
459
+ rest = (hmu_t *)((char *)last_tp + size);
460
+ if (!gci_add_fc(heap, rest, last_tp->size - size))
461
+ return NULL;
462
+ hmu_mark_pinuse(rest);
463
+ }
464
+ else {
465
+ size = last_tp->size;
466
+ next = (hmu_t *)((char *)last_tp + size);
467
+ if (hmu_is_in_heap(next, base_addr, end_addr))
468
+ hmu_mark_pinuse(next);
469
+ }
470
+
471
+ heap->total_free_size -= size;
472
+ if ((heap->current_size - heap->total_free_size) > heap->highmark_size)
473
+ heap->highmark_size = heap->current_size - heap->total_free_size;
474
+
475
+ hmu_set_size((hmu_t *)last_tp, size);
476
+ tp_ret = (uintptr_t)last_tp;
477
+ return (hmu_t *)tp_ret;
478
+ }
479
+
480
+ return NULL;
481
+ }
482
+
483
+ #if WASM_ENABLE_GC != 0
484
+ static int
485
+ do_gc_heap(gc_heap_t *heap)
486
+ {
487
+ int ret = GC_SUCCESS;
488
+ #if WASM_ENABLE_GC_PERF_PROFILING != 0
489
+ uint64 start = 0, end = 0, time = 0;
490
+
491
+ start = os_time_get_boot_microsecond();
492
+ #endif
493
+ if (heap->is_reclaim_enabled) {
494
+ UNLOCK_HEAP(heap);
495
+ ret = gci_gc_heap(heap);
496
+ LOCK_HEAP(heap);
497
+ }
498
+ #if WASM_ENABLE_GC_PERF_PROFILING != 0
499
+ end = os_time_get_boot_microsecond();
500
+ time = end - start;
501
+ heap->total_gc_time += time;
502
+ if (time > heap->max_gc_time) {
503
+ heap->max_gc_time = time;
504
+ }
505
+ heap->total_gc_count += 1;
506
+ #endif
507
+ return ret;
508
+ }
509
+ #endif
510
+
511
+ /**
512
+ * Find a proper HMU with given size
513
+ *
514
+ * @param heap should not be NULL and should be a valid heap
515
+ * @param size should cover the header and should be 8 bytes aligned
516
+ *
517
+ * Note: This function will try several ways to satisfy the allocation request:
518
+ * 1. Find a proper on available HMUs.
519
+ * 2. GC will be triggered if 1 failed.
520
+ * 3. Find a proper on available HMUS.
521
+ * 4. Return NULL if 3 failed
522
+ *
523
+ * @return hmu allocated if success, which will be aligned to 8 bytes,
524
+ * NULL otherwise
525
+ */
526
+ static hmu_t *
527
+ alloc_hmu_ex(gc_heap_t *heap, gc_size_t size)
528
+ {
529
+ bh_assert(gci_is_heap_valid(heap));
530
+ bh_assert(size > 0 && !(size & 7));
531
+
532
+ #if WASM_ENABLE_GC != 0
533
+ #if GC_IN_EVERY_ALLOCATION != 0
534
+ if (GC_SUCCESS != do_gc_heap(heap))
535
+ return NULL;
536
+ #else
537
+ if (heap->total_free_size < heap->gc_threshold) {
538
+ if (GC_SUCCESS != do_gc_heap(heap))
539
+ return NULL;
540
+ }
541
+ else {
542
+ hmu_t *ret = NULL;
543
+ if ((ret = alloc_hmu(heap, size))) {
544
+ return ret;
545
+ }
546
+ if (GC_SUCCESS != do_gc_heap(heap))
547
+ return NULL;
548
+ }
549
+ #endif
550
+ #endif
551
+
552
+ return alloc_hmu(heap, size);
553
+ }
554
+
555
+ #if BH_ENABLE_GC_VERIFY == 0
556
+ gc_object_t
557
+ gc_alloc_vo(void *vheap, gc_size_t size)
558
+ #else
559
+ gc_object_t
560
+ gc_alloc_vo_internal(void *vheap, gc_size_t size, const char *file, int line)
561
+ #endif
562
+ {
563
+ gc_heap_t *heap = (gc_heap_t *)vheap;
564
+ hmu_t *hmu = NULL;
565
+ gc_object_t ret = (gc_object_t)NULL;
566
+ gc_size_t tot_size = 0, tot_size_unaligned;
567
+
568
+ /* hmu header + prefix + obj + suffix */
569
+ tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
570
+ /* aligned size*/
571
+ tot_size = GC_ALIGN_8(tot_size_unaligned);
572
+ if (tot_size < size)
573
+ /* integer overflow */
574
+ return NULL;
575
+
576
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
577
+ if (heap->is_heap_corrupted) {
578
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
579
+ return NULL;
580
+ }
581
+ #endif
582
+
583
+ LOCK_HEAP(heap);
584
+
585
+ hmu = alloc_hmu_ex(heap, tot_size);
586
+ if (!hmu)
587
+ goto finish;
588
+
589
+ bh_assert(hmu_get_size(hmu) >= tot_size);
590
+ /* the total size allocated may be larger than
591
+ the required size, reset it here */
592
+ tot_size = hmu_get_size(hmu);
593
+
594
+ #if GC_STAT_DATA != 0
595
+ heap->total_size_allocated += tot_size;
596
+ #endif
597
+
598
+ hmu_set_ut(hmu, HMU_VO);
599
+ hmu_unfree_vo(hmu);
600
+
601
+ #if BH_ENABLE_GC_VERIFY != 0
602
+ hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
603
+ #endif
604
+
605
+ ret = hmu_to_obj(hmu);
606
+ if (tot_size > tot_size_unaligned)
607
+ /* clear buffer appended by GC_ALIGN_8() */
608
+ memset((uint8 *)ret + size, 0, tot_size - tot_size_unaligned);
609
+
610
+ finish:
611
+ UNLOCK_HEAP(heap);
612
+ return ret;
613
+ }
614
+
615
+ #if BH_ENABLE_GC_VERIFY == 0
616
+ gc_object_t
617
+ gc_realloc_vo(void *vheap, void *ptr, gc_size_t size)
618
+ #else
619
+ gc_object_t
620
+ gc_realloc_vo_internal(void *vheap, void *ptr, gc_size_t size, const char *file,
621
+ int line)
622
+ #endif
623
+ {
624
+ gc_heap_t *heap = (gc_heap_t *)vheap;
625
+ hmu_t *hmu = NULL, *hmu_old = NULL, *hmu_next;
626
+ gc_object_t ret = (gc_object_t)NULL, obj_old = (gc_object_t)ptr;
627
+ gc_size_t tot_size, tot_size_unaligned, tot_size_old = 0, tot_size_next;
628
+ gc_size_t obj_size, obj_size_old;
629
+ gc_uint8 *base_addr, *end_addr;
630
+ hmu_type_t ut;
631
+
632
+ /* hmu header + prefix + obj + suffix */
633
+ tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
634
+ /* aligned size*/
635
+ tot_size = GC_ALIGN_8(tot_size_unaligned);
636
+ if (tot_size < size)
637
+ /* integer overflow */
638
+ return NULL;
639
+
640
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
641
+ if (heap->is_heap_corrupted) {
642
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
643
+ return NULL;
644
+ }
645
+ #endif
646
+
647
+ if (obj_old) {
648
+ hmu_old = obj_to_hmu(obj_old);
649
+ tot_size_old = hmu_get_size(hmu_old);
650
+ if (tot_size <= tot_size_old)
651
+ /* current node already meets requirement */
652
+ return obj_old;
653
+ }
654
+
655
+ base_addr = heap->base_addr;
656
+ end_addr = base_addr + heap->current_size;
657
+
658
+ LOCK_HEAP(heap);
659
+
660
+ if (hmu_old) {
661
+ hmu_next = (hmu_t *)((char *)hmu_old + tot_size_old);
662
+ if (hmu_is_in_heap(hmu_next, base_addr, end_addr)) {
663
+ ut = hmu_get_ut(hmu_next);
664
+ tot_size_next = hmu_get_size(hmu_next);
665
+ if (ut == HMU_FC && tot_size <= tot_size_old + tot_size_next) {
666
+ /* current node and next node meets requirement */
667
+ if (!unlink_hmu(heap, hmu_next)) {
668
+ UNLOCK_HEAP(heap);
669
+ return NULL;
670
+ }
671
+ hmu_set_size(hmu_old, tot_size);
672
+ memset((char *)hmu_old + tot_size_old, 0,
673
+ tot_size - tot_size_old);
674
+ #if BH_ENABLE_GC_VERIFY != 0
675
+ hmu_init_prefix_and_suffix(hmu_old, tot_size, file, line);
676
+ #endif
677
+ if (tot_size < tot_size_old + tot_size_next) {
678
+ hmu_next = (hmu_t *)((char *)hmu_old + tot_size);
679
+ tot_size_next = tot_size_old + tot_size_next - tot_size;
680
+ if (!gci_add_fc(heap, hmu_next, tot_size_next)) {
681
+ UNLOCK_HEAP(heap);
682
+ return NULL;
683
+ }
684
+ hmu_mark_pinuse(hmu_next);
685
+ }
686
+ UNLOCK_HEAP(heap);
687
+ return obj_old;
688
+ }
689
+ }
690
+ }
691
+
692
+ hmu = alloc_hmu_ex(heap, tot_size);
693
+ if (!hmu)
694
+ goto finish;
695
+
696
+ bh_assert(hmu_get_size(hmu) >= tot_size);
697
+ /* the total size allocated may be larger than
698
+ the required size, reset it here */
699
+ tot_size = hmu_get_size(hmu);
700
+
701
+ #if GC_STAT_DATA != 0
702
+ heap->total_size_allocated += tot_size;
703
+ #endif
704
+
705
+ hmu_set_ut(hmu, HMU_VO);
706
+ hmu_unfree_vo(hmu);
707
+
708
+ #if BH_ENABLE_GC_VERIFY != 0
709
+ hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
710
+ #endif
711
+
712
+ ret = hmu_to_obj(hmu);
713
+
714
+ finish:
715
+
716
+ if (ret) {
717
+ obj_size = tot_size - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
718
+ memset(ret, 0, obj_size);
719
+ if (obj_old) {
720
+ obj_size_old =
721
+ tot_size_old - HMU_SIZE - OBJ_PREFIX_SIZE - OBJ_SUFFIX_SIZE;
722
+ bh_memcpy_s(ret, obj_size, obj_old, obj_size_old);
723
+ }
724
+ }
725
+
726
+ UNLOCK_HEAP(heap);
727
+
728
+ if (ret && obj_old)
729
+ gc_free_vo(vheap, obj_old);
730
+
731
+ return ret;
732
+ }
733
+
734
+ #if GC_MANUALLY != 0
735
+ void
736
+ gc_free_wo(void *vheap, void *ptr)
737
+ {
738
+ gc_heap_t *heap = (gc_heap_t *)vheap;
739
+ gc_object_t *obj = (gc_object_t *)ptr;
740
+ hmu_t *hmu = obj_to_hmu(obj);
741
+
742
+ bh_assert(gci_is_heap_valid(heap));
743
+ bh_assert(obj);
744
+ bh_assert((gc_uint8 *)hmu >= heap->base_addr
745
+ && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
746
+ bh_assert(hmu_get_ut(hmu) == HMU_WO);
747
+
748
+ hmu_unmark_wo(hmu);
749
+ (void)heap;
750
+ }
751
+ #endif
752
+
753
+ /* see ems_gc.h for description*/
754
+ #if BH_ENABLE_GC_VERIFY == 0
755
+ gc_object_t
756
+ gc_alloc_wo(void *vheap, gc_size_t size)
757
+ #else
758
+ gc_object_t
759
+ gc_alloc_wo_internal(void *vheap, gc_size_t size, const char *file, int line)
760
+ #endif
761
+ {
762
+ gc_heap_t *heap = (gc_heap_t *)vheap;
763
+ hmu_t *hmu = NULL;
764
+ gc_object_t ret = (gc_object_t)NULL;
765
+ gc_size_t tot_size = 0, tot_size_unaligned;
766
+
767
+ /* hmu header + prefix + obj + suffix */
768
+ tot_size_unaligned = HMU_SIZE + OBJ_PREFIX_SIZE + size + OBJ_SUFFIX_SIZE;
769
+ /* aligned size*/
770
+ tot_size = GC_ALIGN_8(tot_size_unaligned);
771
+ if (tot_size < size)
772
+ /* integer overflow */
773
+ return NULL;
774
+
775
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
776
+ if (heap->is_heap_corrupted) {
777
+ os_printf("[GC_ERROR]Heap is corrupted, allocate memory failed.\n");
778
+ return NULL;
779
+ }
780
+ #endif
781
+
782
+ LOCK_HEAP(heap);
783
+
784
+ hmu = alloc_hmu_ex(heap, tot_size);
785
+ if (!hmu)
786
+ goto finish;
787
+
788
+ /* Don't memset the memory to improve performance, the caller should
789
+ decide whether to memset it or not */
790
+
791
+ bh_assert(hmu_get_size(hmu) >= tot_size);
792
+ /* the total size allocated may be larger than
793
+ the required size, reset it here */
794
+ tot_size = hmu_get_size(hmu);
795
+
796
+ #if GC_STAT_DATA != 0
797
+ heap->total_size_allocated += tot_size;
798
+ #endif
799
+
800
+ hmu_set_ut(hmu, HMU_WO);
801
+ #if GC_MANUALLY != 0
802
+ hmu_mark_wo(hmu);
803
+ #else
804
+ hmu_unmark_wo(hmu);
805
+ #endif
806
+
807
+ #if BH_ENABLE_GC_VERIFY != 0
808
+ hmu_init_prefix_and_suffix(hmu, tot_size, file, line);
809
+ #endif
810
+
811
+ ret = hmu_to_obj(hmu);
812
+ if (tot_size > tot_size_unaligned)
813
+ /* clear buffer appended by GC_ALIGN_8() */
814
+ memset((uint8 *)ret + size, 0, tot_size - tot_size_unaligned);
815
+
816
+ finish:
817
+ UNLOCK_HEAP(heap);
818
+ return ret;
819
+ }
820
+
821
+ /**
822
+ * Do some checking to see if given pointer is a possible valid heap
823
+ * @return GC_TRUE if all checking passed, GC_FALSE otherwise
824
+ */
825
+ int
826
+ gci_is_heap_valid(gc_heap_t *heap)
827
+ {
828
+ if (!heap)
829
+ return GC_FALSE;
830
+ if (heap->heap_id != (gc_handle_t)heap)
831
+ return GC_FALSE;
832
+
833
+ return GC_TRUE;
834
+ }
835
+
836
+ #if BH_ENABLE_GC_VERIFY == 0
837
+ int
838
+ gc_free_vo(void *vheap, gc_object_t obj)
839
+ #else
840
+ int
841
+ gc_free_vo_internal(void *vheap, gc_object_t obj, const char *file, int line)
842
+ #endif
843
+ {
844
+ gc_heap_t *heap = (gc_heap_t *)vheap;
845
+ gc_uint8 *base_addr, *end_addr;
846
+ hmu_t *hmu = NULL;
847
+ hmu_t *prev = NULL;
848
+ hmu_t *next = NULL;
849
+ gc_size_t size = 0;
850
+ hmu_type_t ut;
851
+ int ret = GC_SUCCESS;
852
+
853
+ if (!obj) {
854
+ return GC_SUCCESS;
855
+ }
856
+
857
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
858
+ if (heap->is_heap_corrupted) {
859
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, free memory failed.\n");
860
+ return GC_ERROR;
861
+ }
862
+ #endif
863
+
864
+ hmu = obj_to_hmu(obj);
865
+
866
+ base_addr = heap->base_addr;
867
+ end_addr = base_addr + heap->current_size;
868
+
869
+ LOCK_HEAP(heap);
870
+
871
+ if (hmu_is_in_heap(hmu, base_addr, end_addr)) {
872
+ #if BH_ENABLE_GC_VERIFY != 0
873
+ hmu_verify(heap, hmu);
874
+ #endif
875
+ ut = hmu_get_ut(hmu);
876
+ if (ut == HMU_VO) {
877
+ if (hmu_is_vo_freed(hmu)) {
878
+ bh_assert(0);
879
+ ret = GC_ERROR;
880
+ goto out;
881
+ }
882
+
883
+ size = hmu_get_size(hmu);
884
+
885
+ heap->total_free_size += size;
886
+
887
+ #if GC_STAT_DATA != 0
888
+ heap->total_size_freed += size;
889
+ #endif
890
+
891
+ if (!hmu_get_pinuse(hmu)) {
892
+ prev = (hmu_t *)((char *)hmu - *((int *)hmu - 1));
893
+
894
+ if (hmu_is_in_heap(prev, base_addr, end_addr)
895
+ && hmu_get_ut(prev) == HMU_FC) {
896
+ size += hmu_get_size(prev);
897
+ hmu = prev;
898
+ if (!unlink_hmu(heap, prev)) {
899
+ ret = GC_ERROR;
900
+ goto out;
901
+ }
902
+ }
903
+ }
904
+
905
+ next = (hmu_t *)((char *)hmu + size);
906
+ if (hmu_is_in_heap(next, base_addr, end_addr)) {
907
+ if (hmu_get_ut(next) == HMU_FC) {
908
+ size += hmu_get_size(next);
909
+ if (!unlink_hmu(heap, next)) {
910
+ ret = GC_ERROR;
911
+ goto out;
912
+ }
913
+ next = (hmu_t *)((char *)hmu + size);
914
+ }
915
+ }
916
+
917
+ if (!gci_add_fc(heap, hmu, size)) {
918
+ ret = GC_ERROR;
919
+ goto out;
920
+ }
921
+
922
+ if (hmu_is_in_heap(next, base_addr, end_addr)) {
923
+ hmu_unmark_pinuse(next);
924
+ }
925
+ }
926
+ else {
927
+ ret = GC_ERROR;
928
+ goto out;
929
+ }
930
+ ret = GC_SUCCESS;
931
+ goto out;
932
+ }
933
+
934
+ out:
935
+ UNLOCK_HEAP(heap);
936
+ return ret;
937
+ }
938
+
939
+ void
940
+ gc_dump_heap_stats(gc_heap_t *heap)
941
+ {
942
+ os_printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
943
+ os_printf("total free: %" PRIu32 ", current: %" PRIu32
944
+ ", highmark: %" PRIu32 "\n",
945
+ heap->total_free_size, heap->current_size, heap->highmark_size);
946
+ #if GC_STAT_DATA != 0
947
+ os_printf("total size allocated: %" PRIu64 ", total size freed: %" PRIu64
948
+ ", total occupied: %" PRIu64 "\n",
949
+ heap->total_size_allocated, heap->total_size_freed,
950
+ heap->total_size_allocated - heap->total_size_freed);
951
+ #endif
952
+ }
953
+
954
+ uint32
955
+ gc_get_heap_highmark_size(gc_heap_t *heap)
956
+ {
957
+ return heap->highmark_size;
958
+ }
959
+
960
+ void
961
+ gci_dump(gc_heap_t *heap)
962
+ {
963
+ hmu_t *cur = NULL, *end = NULL;
964
+ hmu_type_t ut;
965
+ gc_size_t size;
966
+ int i = 0, p, mark;
967
+ char inuse = 'U';
968
+
969
+ cur = (hmu_t *)heap->base_addr;
970
+ end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
971
+
972
+ while (cur < end) {
973
+ ut = hmu_get_ut(cur);
974
+ size = hmu_get_size(cur);
975
+ p = hmu_get_pinuse(cur);
976
+ mark = hmu_is_wo_marked(cur);
977
+
978
+ if (ut == HMU_VO)
979
+ inuse = 'V';
980
+ else if (ut == HMU_WO)
981
+ inuse = hmu_is_wo_marked(cur) ? 'W' : 'w';
982
+ else if (ut == HMU_FC)
983
+ inuse = 'F';
984
+
985
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
986
+ if (size == 0 || size > (uint32)((uint8 *)end - (uint8 *)cur)) {
987
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
988
+ heap->is_heap_corrupted = true;
989
+ return;
990
+ }
991
+ #endif
992
+
993
+ os_printf("#%d %08" PRIx32 " %" PRIx32 " %d %d"
994
+ " %c %" PRId32 "\n",
995
+ i, (uint32)((char *)cur - (char *)heap->base_addr),
996
+ (uint32)ut, p, mark, inuse, (int32)hmu_obj_size(size));
997
+ #if BH_ENABLE_GC_VERIFY != 0
998
+ if (inuse == 'V') {
999
+ gc_object_prefix_t *prefix = (gc_object_prefix_t *)(cur + 1);
1000
+ os_printf("#%s:%d\n", prefix->file_name, prefix->line_no);
1001
+ }
1002
+ #endif
1003
+
1004
+ cur = (hmu_t *)((char *)cur + size);
1005
+ i++;
1006
+ }
1007
+
1008
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
1009
+ if (cur != end) {
1010
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, heap dump failed.\n");
1011
+ heap->is_heap_corrupted = true;
1012
+ }
1013
+ #else
1014
+ bh_assert(cur == end);
1015
+ #endif
1016
+ }
1017
+
1018
+ #if WASM_ENABLE_GC != 0
1019
+ extra_info_node_t *
1020
+ gc_search_extra_info_node(gc_handle_t handle, gc_object_t obj,
1021
+ gc_size_t *p_index)
1022
+ {
1023
+ gc_heap_t *vheap = (gc_heap_t *)handle;
1024
+ int32 low = 0, high = vheap->extra_info_node_cnt - 1;
1025
+ int32 mid;
1026
+ extra_info_node_t *node;
1027
+
1028
+ if (!vheap->extra_info_nodes)
1029
+ return NULL;
1030
+
1031
+ while (low <= high) {
1032
+ mid = (low + high) / 2;
1033
+ node = vheap->extra_info_nodes[mid];
1034
+
1035
+ if (obj == node->obj) {
1036
+ if (p_index) {
1037
+ *p_index = mid;
1038
+ }
1039
+ return node;
1040
+ }
1041
+ else if (obj < node->obj) {
1042
+ high = mid - 1;
1043
+ }
1044
+ else {
1045
+ low = mid + 1;
1046
+ }
1047
+ }
1048
+
1049
+ if (p_index) {
1050
+ *p_index = low;
1051
+ }
1052
+ return NULL;
1053
+ }
1054
+
1055
+ static bool
1056
+ insert_extra_info_node(gc_heap_t *vheap, extra_info_node_t *node)
1057
+ {
1058
+ gc_size_t index;
1059
+ extra_info_node_t *orig_node;
1060
+
1061
+ if (!vheap->extra_info_nodes) {
1062
+ vheap->extra_info_nodes = vheap->extra_info_normal_nodes;
1063
+ vheap->extra_info_node_capacity = sizeof(vheap->extra_info_normal_nodes)
1064
+ / sizeof(extra_info_node_t *);
1065
+ vheap->extra_info_nodes[0] = node;
1066
+ vheap->extra_info_node_cnt = 1;
1067
+ return true;
1068
+ }
1069
+
1070
+ /* extend array */
1071
+ if (vheap->extra_info_node_cnt == vheap->extra_info_node_capacity) {
1072
+ extra_info_node_t **new_nodes = NULL;
1073
+ gc_size_t new_capacity = vheap->extra_info_node_capacity * 3 / 2;
1074
+ gc_size_t total_size = sizeof(extra_info_node_t *) * new_capacity;
1075
+
1076
+ new_nodes = (extra_info_node_t **)BH_MALLOC(total_size);
1077
+ if (!new_nodes) {
1078
+ LOG_ERROR("alloc extra info nodes failed");
1079
+ return false;
1080
+ }
1081
+
1082
+ bh_memcpy_s(new_nodes, total_size, vheap->extra_info_nodes,
1083
+ sizeof(extra_info_node_t *) * vheap->extra_info_node_cnt);
1084
+ if (vheap->extra_info_nodes != vheap->extra_info_normal_nodes) {
1085
+ BH_FREE(vheap->extra_info_nodes);
1086
+ }
1087
+
1088
+ vheap->extra_info_nodes = new_nodes;
1089
+ vheap->extra_info_node_capacity = new_capacity;
1090
+ }
1091
+
1092
+ orig_node = gc_search_extra_info_node(vheap, node->obj, &index);
1093
+ if (orig_node) {
1094
+ /* replace the old node */
1095
+ vheap->extra_info_nodes[index] = node;
1096
+ BH_FREE(orig_node);
1097
+ }
1098
+ else {
1099
+ bh_memmove_s(vheap->extra_info_nodes + index + 1,
1100
+ (vheap->extra_info_node_capacity - index - 1)
1101
+ * sizeof(extra_info_node_t *),
1102
+ vheap->extra_info_nodes + index,
1103
+ (vheap->extra_info_node_cnt - index)
1104
+ * sizeof(extra_info_node_t *));
1105
+ vheap->extra_info_nodes[index] = node;
1106
+ vheap->extra_info_node_cnt += 1;
1107
+ }
1108
+
1109
+ return true;
1110
+ }
1111
+
1112
+ bool
1113
+ gc_set_finalizer(gc_handle_t handle, gc_object_t obj, gc_finalizer_t cb,
1114
+ void *data)
1115
+ {
1116
+ extra_info_node_t *node = NULL;
1117
+ gc_heap_t *vheap = (gc_heap_t *)handle;
1118
+
1119
+ node = (extra_info_node_t *)BH_MALLOC(sizeof(extra_info_node_t));
1120
+
1121
+ if (!node) {
1122
+ LOG_ERROR("alloc a new extra info node failed");
1123
+ return GC_FALSE;
1124
+ }
1125
+ memset(node, 0, sizeof(extra_info_node_t));
1126
+
1127
+ node->finalizer = cb;
1128
+ node->obj = obj;
1129
+ node->data = data;
1130
+
1131
+ LOCK_HEAP(vheap);
1132
+ if (!insert_extra_info_node(vheap, node)) {
1133
+ BH_FREE(node);
1134
+ UNLOCK_HEAP(vheap);
1135
+ return GC_FALSE;
1136
+ }
1137
+ UNLOCK_HEAP(vheap);
1138
+
1139
+ gct_vm_set_extra_info_flag(obj, true);
1140
+ return GC_TRUE;
1141
+ }
1142
+
1143
+ void
1144
+ gc_unset_finalizer(gc_handle_t handle, gc_object_t obj)
1145
+ {
1146
+ gc_size_t index;
1147
+ gc_heap_t *vheap = (gc_heap_t *)handle;
1148
+ extra_info_node_t *node;
1149
+
1150
+ LOCK_HEAP(vheap);
1151
+ node = gc_search_extra_info_node(vheap, obj, &index);
1152
+
1153
+ if (!node) {
1154
+ UNLOCK_HEAP(vheap);
1155
+ return;
1156
+ }
1157
+
1158
+ BH_FREE(node);
1159
+ bh_memmove_s(
1160
+ vheap->extra_info_nodes + index,
1161
+ (vheap->extra_info_node_capacity - index) * sizeof(extra_info_node_t *),
1162
+ vheap->extra_info_nodes + index + 1,
1163
+ (vheap->extra_info_node_cnt - index - 1) * sizeof(extra_info_node_t *));
1164
+ vheap->extra_info_node_cnt -= 1;
1165
+ UNLOCK_HEAP(vheap);
1166
+
1167
+ gct_vm_set_extra_info_flag(obj, false);
1168
+ }
1169
+ #endif