krillplayer-plugin 0.6.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 +1 -0
  3. package/dist/cli.js +351 -73
  4. package/dist/crypto.js +37 -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 +8 -0
  144. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +8 -0
  145. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +8 -0
  146. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +26 -9
  147. package/skill/krillplayer-plugin/references/cases.md +19 -1
  148. package/skill/krillplayer-plugin/references/contract.d.ts +104 -11
  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,497 @@
1
+ /*
2
+ * Copyright (C) 2022 Tencent Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "ems_gc.h"
7
+ #include "ems_gc_internal.h"
8
+
9
+ #define GB (1 << 30UL)
10
+
11
+ #define MARK_NODE_OBJ_CNT 256
12
+
13
+ #if WASM_ENABLE_GC != 0
14
+
15
+ /* mark node is used for gc marker*/
16
+ typedef struct mark_node_struct {
17
+ /* number of to-expand objects can be saved in this node */
18
+ gc_size_t cnt;
19
+
20
+ /* the first unused index */
21
+ uint32 idx;
22
+
23
+ /* next node on the node list */
24
+ struct mark_node_struct *next;
25
+
26
+ /* the actual to-expand objects list */
27
+ gc_object_t set[MARK_NODE_OBJ_CNT];
28
+ } mark_node_t;
29
+
30
+ /**
31
+ * Alloc a mark node from the native heap
32
+ *
33
+ * @return a valid mark node if success, NULL otherwise
34
+ */
35
+ static mark_node_t *
36
+ alloc_mark_node(void)
37
+ {
38
+ mark_node_t *ret = (mark_node_t *)BH_MALLOC(sizeof(mark_node_t));
39
+
40
+ if (!ret) {
41
+ LOG_ERROR("alloc a new mark node failed");
42
+ return NULL;
43
+ }
44
+ ret->cnt = sizeof(ret->set) / sizeof(ret->set[0]);
45
+ ret->idx = 0;
46
+ ret->next = NULL;
47
+ return ret;
48
+ }
49
+
50
+ /* Free a mark node to the native heap
51
+ *
52
+ * @param node the mark node to free, should not be NULL
53
+ */
54
+ static void
55
+ free_mark_node(mark_node_t *node)
56
+ {
57
+ bh_assert(node);
58
+ BH_FREE((gc_object_t)node);
59
+ }
60
+
61
+ /**
62
+ * Sweep phase of mark_sweep algorithm
63
+ * @param heap the heap to sweep, should be a valid instance heap
64
+ * which has already been marked
65
+ */
66
+ static void
67
+ sweep_instance_heap(gc_heap_t *heap)
68
+ {
69
+ hmu_t *cur = NULL, *end = NULL, *last = NULL;
70
+ hmu_type_t ut;
71
+ gc_size_t size;
72
+ int i, lsize;
73
+ gc_size_t tot_free = 0;
74
+
75
+ bh_assert(gci_is_heap_valid(heap));
76
+
77
+ cur = (hmu_t *)heap->base_addr;
78
+ last = NULL;
79
+ end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
80
+
81
+ /* reset KFC */
82
+ lsize =
83
+ (int)(sizeof(heap->kfc_normal_list) / sizeof(heap->kfc_normal_list[0]));
84
+ for (i = 0; i < lsize; i++) {
85
+ heap->kfc_normal_list[i].next = NULL;
86
+ }
87
+ heap->kfc_tree_root->right = NULL;
88
+ heap->root_set = NULL;
89
+
90
+ while (cur < end) {
91
+ ut = hmu_get_ut(cur);
92
+ size = hmu_get_size(cur);
93
+ bh_assert(size > 0);
94
+
95
+ if (ut == HMU_FC || ut == HMU_FM
96
+ || (ut == HMU_VO && hmu_is_vo_freed(cur))
97
+ || (ut == HMU_WO && !hmu_is_wo_marked(cur))) {
98
+ /* merge previous free areas with current one */
99
+ if (!last)
100
+ last = cur;
101
+
102
+ if (ut == HMU_WO) {
103
+ /* Invoke registered finalizer */
104
+ gc_object_t cur_obj = hmu_to_obj(cur);
105
+ if (gct_vm_get_extra_info_flag(cur_obj)) {
106
+ extra_info_node_t *node = gc_search_extra_info_node(
107
+ (gc_handle_t)heap, cur_obj, NULL);
108
+ bh_assert(node);
109
+ node->finalizer(node->obj, node->data);
110
+ gc_unset_finalizer((gc_handle_t)heap, cur_obj);
111
+ }
112
+ }
113
+ }
114
+ else {
115
+ /* current block is still live */
116
+ if (last) {
117
+ tot_free += (gc_size_t)((char *)cur - (char *)last);
118
+ gci_add_fc(heap, last, (gc_size_t)((char *)cur - (char *)last));
119
+ hmu_mark_pinuse(last);
120
+ last = NULL;
121
+ }
122
+
123
+ if (ut == HMU_WO) {
124
+ /* unmark it */
125
+ hmu_unmark_wo(cur);
126
+ }
127
+ }
128
+
129
+ cur = (hmu_t *)((char *)cur + size);
130
+ }
131
+
132
+ bh_assert(cur == end);
133
+
134
+ if (last) {
135
+ tot_free += (gc_size_t)((char *)cur - (char *)last);
136
+ gci_add_fc(heap, last, (gc_size_t)((char *)cur - (char *)last));
137
+ hmu_mark_pinuse(last);
138
+ }
139
+
140
+ heap->total_free_size = tot_free;
141
+
142
+ #if GC_STAT_DATA != 0
143
+ heap->total_gc_count++;
144
+ if ((heap->current_size - tot_free) > heap->highmark_size)
145
+ heap->highmark_size = heap->current_size - tot_free;
146
+
147
+ #endif
148
+ gc_update_threshold(heap);
149
+ }
150
+
151
+ /**
152
+ * Add a to-expand node to the to-expand list
153
+ *
154
+ * @param heap should be a valid instance heap
155
+ * @param obj should be a valid wo inside @heap
156
+ *
157
+ * @return GC_ERROR if there is no more resource for marking,
158
+ * GC_SUCCESS if success
159
+ */
160
+ static int
161
+ add_wo_to_expand(gc_heap_t *heap, gc_object_t obj)
162
+ {
163
+ mark_node_t *mark_node = NULL, *new_node = NULL;
164
+ hmu_t *hmu = NULL;
165
+
166
+ bh_assert(obj);
167
+
168
+ hmu = obj_to_hmu(obj);
169
+
170
+ bh_assert(gci_is_heap_valid(heap));
171
+ bh_assert((gc_uint8 *)hmu >= heap->base_addr
172
+ && (gc_uint8 *)hmu < heap->base_addr + heap->current_size);
173
+ bh_assert(hmu_get_ut(hmu) == HMU_WO);
174
+
175
+ if (hmu_is_wo_marked(hmu))
176
+ return GC_SUCCESS; /* already marked*/
177
+
178
+ mark_node = (mark_node_t *)heap->root_set;
179
+ if (!mark_node || mark_node->idx == mark_node->cnt) {
180
+ new_node = alloc_mark_node();
181
+ if (!new_node) {
182
+ LOG_ERROR("can not add obj to mark node because of mark node "
183
+ "allocation failed");
184
+ return GC_ERROR;
185
+ }
186
+ new_node->next = mark_node;
187
+ heap->root_set = new_node;
188
+ mark_node = new_node;
189
+ }
190
+
191
+ mark_node->set[mark_node->idx++] = obj;
192
+ hmu_mark_wo(hmu);
193
+ return GC_SUCCESS;
194
+ }
195
+
196
+ /* Check ems_gc.h for description*/
197
+ int
198
+ gc_add_root(void *heap_p, gc_object_t obj)
199
+ {
200
+ gc_heap_t *heap = (gc_heap_t *)heap_p;
201
+ hmu_t *hmu = NULL;
202
+
203
+ if (!obj) {
204
+ LOG_ERROR("gc_add_root with NULL obj");
205
+ return GC_ERROR;
206
+ }
207
+
208
+ hmu = obj_to_hmu(obj);
209
+
210
+ if (!gci_is_heap_valid(heap)) {
211
+ LOG_ERROR("vm_get_gc_handle_for_current_instance returns invalid heap");
212
+ return GC_ERROR;
213
+ }
214
+
215
+ if (!((gc_uint8 *)hmu >= heap->base_addr
216
+ && (gc_uint8 *)hmu < heap->base_addr + heap->current_size)) {
217
+ LOG_ERROR("Obj is not a object in current instance heap");
218
+ return GC_ERROR;
219
+ }
220
+
221
+ if (hmu_get_ut(hmu) != HMU_WO) {
222
+ LOG_ERROR("Given object is not wo");
223
+ return GC_ERROR;
224
+ }
225
+
226
+ if (add_wo_to_expand(heap, obj) != GC_SUCCESS) {
227
+ heap->is_fast_marking_failed = 1;
228
+ return GC_ERROR;
229
+ }
230
+
231
+ return GC_SUCCESS;
232
+ }
233
+
234
+ /**
235
+ * Unmark all marked objects to do rollback
236
+ *
237
+ * @param heap the heap to do rollback, should be a valid instance heap
238
+ */
239
+ static void
240
+ rollback_mark(gc_heap_t *heap)
241
+ {
242
+ mark_node_t *mark_node = NULL, *next_mark_node = NULL;
243
+ hmu_t *cur = NULL, *end = NULL;
244
+ hmu_type_t ut;
245
+ gc_size_t size;
246
+
247
+ bh_assert(gci_is_heap_valid(heap));
248
+
249
+ /* roll back*/
250
+ mark_node = (mark_node_t *)heap->root_set;
251
+ while (mark_node) {
252
+ next_mark_node = mark_node->next;
253
+ free_mark_node(mark_node);
254
+ mark_node = next_mark_node;
255
+ }
256
+
257
+ heap->root_set = NULL;
258
+
259
+ /* then traverse the heap to unmark all marked wos*/
260
+
261
+ cur = (hmu_t *)heap->base_addr;
262
+ end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
263
+
264
+ while (cur < end) {
265
+ ut = hmu_get_ut(cur);
266
+ size = hmu_get_size(cur);
267
+
268
+ if (ut == HMU_WO && hmu_is_wo_marked(cur)) {
269
+ hmu_unmark_wo(cur);
270
+ }
271
+
272
+ cur = (hmu_t *)((char *)cur + size);
273
+ }
274
+
275
+ bh_assert(cur == end);
276
+ }
277
+
278
+ /**
279
+ * Reclaim GC instance heap
280
+ *
281
+ * @param heap the heap to reclaim, should be a valid instance heap
282
+ *
283
+ * @return GC_SUCCESS if success, GC_ERROR otherwise
284
+ */
285
+ static int
286
+ reclaim_instance_heap(gc_heap_t *heap)
287
+ {
288
+ mark_node_t *mark_node = NULL;
289
+ int idx = 0, j = 0;
290
+ bool ret, is_compact_mode = false;
291
+ gc_object_t obj = NULL, ref = NULL;
292
+ hmu_t *hmu = NULL;
293
+ gc_uint32 ref_num = 0, ref_start_offset = 0, size = 0, offset = 0;
294
+ gc_uint16 *ref_list = NULL;
295
+
296
+ bh_assert(gci_is_heap_valid(heap));
297
+
298
+ heap->root_set = NULL;
299
+
300
+ #if WASM_ENABLE_THREAD_MGR == 0
301
+ if (!heap->exec_env)
302
+ return GC_SUCCESS;
303
+ ret = gct_vm_begin_rootset_enumeration(heap->exec_env, heap);
304
+ #else
305
+ if (!heap->cluster)
306
+ return GC_SUCCESS;
307
+ ret = gct_vm_begin_rootset_enumeration(heap->cluster, heap);
308
+ #endif
309
+ if (!ret)
310
+ return GC_ERROR;
311
+
312
+ #if BH_ENABLE_GC_VERIFY != 0
313
+ /* no matter whether the enumeration is successful or not, the data
314
+ collected should be checked at first */
315
+ mark_node = (mark_node_t *)heap->root_set;
316
+ while (mark_node) {
317
+ /* all nodes except first should be full filled */
318
+ bh_assert(mark_node == (mark_node_t *)heap->root_set
319
+ || mark_node->idx == mark_node->cnt);
320
+
321
+ /* all nodes should be non-empty */
322
+ bh_assert(mark_node->idx > 0);
323
+
324
+ for (idx = 0; idx < (int)mark_node->idx; idx++) {
325
+ obj = mark_node->set[idx];
326
+ hmu = obj_to_hmu(obj);
327
+ bh_assert(hmu_is_wo_marked(hmu));
328
+ bh_assert((gc_uint8 *)hmu >= heap->base_addr
329
+ && (gc_uint8 *)hmu
330
+ < heap->base_addr + heap->current_size);
331
+ }
332
+
333
+ mark_node = mark_node->next;
334
+ }
335
+ #endif
336
+
337
+ /* TODO: when fast marking failed, we can still do slow
338
+ marking, currently just simply roll it back. */
339
+ if (heap->is_fast_marking_failed) {
340
+ LOG_ERROR("enumerate rootset failed");
341
+ LOG_ERROR("all marked wos will be unmarked to keep heap consistency");
342
+
343
+ rollback_mark(heap);
344
+ heap->is_fast_marking_failed = 0;
345
+ return GC_ERROR;
346
+ }
347
+
348
+ /* the algorithm we use to mark all objects */
349
+ /* 1. mark rootset and organize them into a mark_node list (last marked
350
+ * roots at list header, i.e. stack top) */
351
+ /* 2. in every iteration, we use the top node to expand*/
352
+ /* 3. execute step 2 till no expanding */
353
+ /* this is a BFS & DFS mixed algorithm, but more like DFS */
354
+ mark_node = (mark_node_t *)heap->root_set;
355
+ while (mark_node) {
356
+ heap->root_set = mark_node->next;
357
+
358
+ /* note that mark_node->idx may change in each loop */
359
+ for (idx = 0; idx < (int)mark_node->idx; idx++) {
360
+ obj = mark_node->set[idx];
361
+ hmu = obj_to_hmu(obj);
362
+ size = hmu_get_size(hmu);
363
+
364
+ if (!gct_vm_get_wasm_object_ref_list(obj, &is_compact_mode,
365
+ &ref_num, &ref_list,
366
+ &ref_start_offset)) {
367
+ LOG_ERROR("mark process failed because failed "
368
+ "vm_get_wasm_object_ref_list");
369
+ break;
370
+ }
371
+
372
+ if (ref_num >= 2U * GB) {
373
+ LOG_ERROR("Invalid ref_num returned");
374
+ break;
375
+ }
376
+
377
+ if (is_compact_mode) {
378
+ for (j = 0; j < (int)ref_num; j++) {
379
+ offset = ref_start_offset + j * sizeof(void *);
380
+ bh_assert(offset + sizeof(void *) < size);
381
+ ref = *(gc_object_t *)(((gc_uint8 *)obj) + offset);
382
+ if (ref == NULL_REF || ((uintptr_t)ref & 1))
383
+ continue; /* null object or i31 object */
384
+ if (add_wo_to_expand(heap, ref) == GC_ERROR) {
385
+ LOG_ERROR("add_wo_to_expand failed");
386
+ break;
387
+ }
388
+ }
389
+ if (j < (int)ref_num)
390
+ break;
391
+ }
392
+ else {
393
+ for (j = 0; j < (int)ref_num; j++) {
394
+ offset = ref_list[j];
395
+ bh_assert(offset + sizeof(void *) < size);
396
+
397
+ ref = *(gc_object_t *)(((gc_uint8 *)obj) + offset);
398
+ if (ref == NULL_REF || ((uintptr_t)ref & 1))
399
+ continue; /* null object or i31 object */
400
+ if (add_wo_to_expand(heap, ref) == GC_ERROR) {
401
+ LOG_ERROR("mark process failed");
402
+ break;
403
+ }
404
+ }
405
+ if (j < (int)ref_num)
406
+ break;
407
+ }
408
+ }
409
+ if (idx < (int)mark_node->idx)
410
+ break; /* not yet done */
411
+
412
+ /* obj's in mark_node are all expanded */
413
+ free_mark_node(mark_node);
414
+ mark_node = heap->root_set;
415
+ }
416
+
417
+ if (mark_node) {
418
+ LOG_ERROR("mark process is not successfully finished");
419
+
420
+ free_mark_node(mark_node);
421
+ /* roll back is required */
422
+ rollback_mark(heap);
423
+
424
+ return GC_ERROR;
425
+ }
426
+
427
+ /* now sweep */
428
+ sweep_instance_heap(heap);
429
+
430
+ (void)size;
431
+
432
+ return GC_SUCCESS;
433
+ }
434
+
435
+ /**
436
+ * Do GC on given heap
437
+ *
438
+ * @param the heap to do GC, should be a valid heap
439
+ *
440
+ * @return GC_SUCCESS if success, GC_ERROR otherwise
441
+ */
442
+ int
443
+ gci_gc_heap(void *h)
444
+ {
445
+ int ret = GC_ERROR;
446
+ gc_heap_t *heap = (gc_heap_t *)h;
447
+
448
+ bh_assert(gci_is_heap_valid(heap));
449
+
450
+ LOG_VERBOSE("#reclaim instance heap %p", heap);
451
+
452
+ /* TODO: get exec_env of current thread when GC multi-threading
453
+ is enabled, and pass it to runtime */
454
+ gct_vm_gc_prepare(NULL);
455
+
456
+ gct_vm_mutex_lock(&heap->lock);
457
+ heap->is_doing_reclaim = 1;
458
+
459
+ ret = reclaim_instance_heap(heap);
460
+
461
+ heap->is_doing_reclaim = 0;
462
+ gct_vm_mutex_unlock(&heap->lock);
463
+
464
+ /* TODO: get exec_env of current thread when GC multi-threading
465
+ is enabled, and pass it to runtime */
466
+ gct_vm_gc_finished(NULL);
467
+
468
+ LOG_VERBOSE("#reclaim instance heap %p done", heap);
469
+
470
+ #if BH_ENABLE_GC_VERIFY != 0
471
+ gci_verify_heap(heap);
472
+ #endif
473
+
474
+ #if GC_STAT_SHOW != 0
475
+ gc_show_stat(heap);
476
+ gc_show_fragment(heap);
477
+ #endif
478
+
479
+ return ret;
480
+ }
481
+
482
+ int
483
+ gc_is_dead_object(void *obj)
484
+ {
485
+ return !hmu_is_wo_marked(obj_to_hmu(obj));
486
+ }
487
+
488
+ #else
489
+
490
+ int
491
+ gci_gc_heap(void *h)
492
+ {
493
+ (void)h;
494
+ return GC_ERROR;
495
+ }
496
+
497
+ #endif /* end of WASM_ENABLE_GC != 0 */