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,547 @@
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
+ static gc_handle_t
9
+ gc_init_internal(gc_heap_t *heap, char *base_addr, gc_size_t heap_max_size)
10
+ {
11
+ hmu_tree_node_t *root = NULL, *q = NULL;
12
+ int ret;
13
+
14
+ memset(heap, 0, sizeof *heap);
15
+ memset(base_addr, 0, heap_max_size);
16
+
17
+ ret = os_mutex_init(&heap->lock);
18
+ if (ret != BHT_OK) {
19
+ LOG_ERROR("[GC_ERROR]failed to init lock\n");
20
+ return NULL;
21
+ }
22
+
23
+ /* init all data structures*/
24
+ heap->current_size = heap_max_size;
25
+ heap->base_addr = (gc_uint8 *)base_addr;
26
+ heap->heap_id = (gc_handle_t)heap;
27
+
28
+ heap->total_free_size = heap->current_size;
29
+ heap->highmark_size = 0;
30
+ #if WASM_ENABLE_GC != 0
31
+ heap->gc_threshold_factor = GC_DEFAULT_THRESHOLD_FACTOR;
32
+ gc_update_threshold(heap);
33
+ #endif
34
+
35
+ root = heap->kfc_tree_root = (hmu_tree_node_t *)heap->kfc_tree_root_buf;
36
+ memset(root, 0, sizeof *root);
37
+ root->size = sizeof *root;
38
+ hmu_set_ut(&root->hmu_header, HMU_FC);
39
+ hmu_set_size(&root->hmu_header, sizeof *root);
40
+
41
+ q = (hmu_tree_node_t *)heap->base_addr;
42
+ memset(q, 0, sizeof *q);
43
+ hmu_set_ut(&q->hmu_header, HMU_FC);
44
+ hmu_set_size(&q->hmu_header, heap->current_size);
45
+
46
+ ASSERT_TREE_NODE_ALIGNED_ACCESS(q);
47
+ ASSERT_TREE_NODE_ALIGNED_ACCESS(root);
48
+
49
+ hmu_mark_pinuse(&q->hmu_header);
50
+ root->right = q;
51
+ q->parent = root;
52
+ q->size = heap->current_size;
53
+
54
+ bh_assert(root->size <= HMU_FC_NORMAL_MAX_SIZE);
55
+
56
+ return heap;
57
+ }
58
+
59
+ gc_handle_t
60
+ gc_init_with_pool(char *buf, gc_size_t buf_size)
61
+ {
62
+ char *buf_end = buf + buf_size;
63
+ char *buf_aligned = (char *)(((uintptr_t)buf + 7) & (uintptr_t)~7);
64
+ char *base_addr = buf_aligned + sizeof(gc_heap_t);
65
+ gc_heap_t *heap = (gc_heap_t *)buf_aligned;
66
+ gc_size_t heap_max_size;
67
+
68
+ if (buf_size < APP_HEAP_SIZE_MIN) {
69
+ LOG_ERROR("[GC_ERROR]heap init buf size (%" PRIu32 ") < %" PRIu32 "\n",
70
+ buf_size, (uint32)APP_HEAP_SIZE_MIN);
71
+ return NULL;
72
+ }
73
+
74
+ base_addr =
75
+ (char *)(((uintptr_t)base_addr + 7) & (uintptr_t)~7) + GC_HEAD_PADDING;
76
+ heap_max_size = (uint32)(buf_end - base_addr) & (uint32)~7;
77
+
78
+ #if WASM_ENABLE_MEMORY_TRACING != 0
79
+ os_printf("Heap created, total size: %u\n", buf_size);
80
+ os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
81
+ os_printf(" actual heap size: %u\n", heap_max_size);
82
+ os_printf(" padding bytes: %u\n",
83
+ buf_size - sizeof(gc_heap_t) - heap_max_size);
84
+ #endif
85
+ return gc_init_internal(heap, base_addr, heap_max_size);
86
+ }
87
+
88
+ gc_handle_t
89
+ gc_init_with_struct_and_pool(char *struct_buf, gc_size_t struct_buf_size,
90
+ char *pool_buf, gc_size_t pool_buf_size)
91
+ {
92
+ gc_heap_t *heap = (gc_heap_t *)struct_buf;
93
+ char *base_addr = pool_buf + GC_HEAD_PADDING;
94
+ char *pool_buf_end = pool_buf + pool_buf_size;
95
+ gc_size_t heap_max_size;
96
+
97
+ if ((((uintptr_t)struct_buf) & 7) != 0) {
98
+ LOG_ERROR("[GC_ERROR]heap init struct buf not 8-byte aligned\n");
99
+ return NULL;
100
+ }
101
+
102
+ if (struct_buf_size < sizeof(gc_handle_t)) {
103
+ LOG_ERROR("[GC_ERROR]heap init struct buf size (%" PRIu32 ") < %zu\n",
104
+ struct_buf_size, sizeof(gc_handle_t));
105
+ return NULL;
106
+ }
107
+
108
+ if ((((uintptr_t)pool_buf) & 7) != 0) {
109
+ LOG_ERROR("[GC_ERROR]heap init pool buf not 8-byte aligned\n");
110
+ return NULL;
111
+ }
112
+
113
+ if (pool_buf_size < APP_HEAP_SIZE_MIN) {
114
+ LOG_ERROR("[GC_ERROR]heap init buf size (%" PRIu32 ") < %u\n",
115
+ pool_buf_size, APP_HEAP_SIZE_MIN);
116
+ return NULL;
117
+ }
118
+
119
+ heap_max_size = (uint32)(pool_buf_end - base_addr) & (uint32)~7;
120
+
121
+ #if WASM_ENABLE_MEMORY_TRACING != 0
122
+ os_printf("Heap created, total size: %u\n",
123
+ struct_buf_size + pool_buf_size);
124
+ os_printf(" heap struct size: %u\n", sizeof(gc_heap_t));
125
+ os_printf(" actual heap size: %u\n", heap_max_size);
126
+ os_printf(" padding bytes: %u\n", pool_buf_size - heap_max_size);
127
+ #endif
128
+ return gc_init_internal(heap, base_addr, heap_max_size);
129
+ }
130
+
131
+ int
132
+ gc_destroy_with_pool(gc_handle_t handle)
133
+ {
134
+ gc_heap_t *heap = (gc_heap_t *)handle;
135
+ int ret = GC_SUCCESS;
136
+
137
+ #if WASM_ENABLE_GC != 0
138
+ gc_size_t i = 0;
139
+
140
+ if (heap->extra_info_node_cnt > 0) {
141
+ for (i = 0; i < heap->extra_info_node_cnt; i++) {
142
+ extra_info_node_t *node = heap->extra_info_nodes[i];
143
+ #if BH_ENABLE_GC_VERIFY != 0
144
+ os_printf("Memory leak detected: gc object [%p] not claimed\n",
145
+ node->obj);
146
+ #endif
147
+ bh_assert(heap->is_reclaim_enabled);
148
+ node->finalizer(node->obj, node->data);
149
+
150
+ BH_FREE(heap->extra_info_nodes[i]);
151
+ }
152
+
153
+ if (heap->extra_info_nodes != heap->extra_info_normal_nodes) {
154
+ BH_FREE(heap->extra_info_nodes);
155
+ }
156
+ }
157
+ #endif
158
+
159
+ #if BH_ENABLE_GC_VERIFY != 0
160
+ hmu_t *cur = (hmu_t *)heap->base_addr;
161
+ hmu_t *end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
162
+
163
+ if (
164
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
165
+ !heap->is_heap_corrupted &&
166
+ #endif
167
+ (hmu_t *)((char *)cur + hmu_get_size(cur)) != end) {
168
+ LOG_WARNING("Memory leak detected:\n");
169
+ gci_dump(heap);
170
+ ret = GC_ERROR;
171
+ }
172
+ #endif
173
+
174
+ os_mutex_destroy(&heap->lock);
175
+ memset(heap->base_addr, 0, heap->current_size);
176
+ memset(heap, 0, sizeof(gc_heap_t));
177
+ return ret;
178
+ }
179
+
180
+ #if WASM_ENABLE_GC != 0
181
+ #if WASM_ENABLE_THREAD_MGR == 0
182
+ void
183
+ gc_enable_gc_reclaim(gc_handle_t handle, void *exec_env)
184
+ {
185
+ gc_heap_t *heap = (gc_heap_t *)handle;
186
+
187
+ heap->is_reclaim_enabled = 1;
188
+ heap->exec_env = exec_env;
189
+ }
190
+ #else
191
+ void
192
+ gc_enable_gc_reclaim(gc_handle_t handle, void *cluster)
193
+ {
194
+ gc_heap_t *heap = (gc_heap_t *)handle;
195
+
196
+ heap->is_reclaim_enabled = 1;
197
+ heap->cluster = cluster;
198
+ }
199
+ #endif
200
+ #endif
201
+
202
+ uint32
203
+ gc_get_heap_struct_size()
204
+ {
205
+ return sizeof(gc_heap_t);
206
+ }
207
+
208
+ static void
209
+ adjust_ptr(uint8 **p_ptr, intptr_t offset)
210
+ {
211
+ if ((!*p_ptr)) {
212
+ return;
213
+ }
214
+
215
+ /*
216
+ * to resolve a possible signed integer overflow issue
217
+ * when p_ptr is over 0x8000000000000000 by not using
218
+ * `(intptr_t)`
219
+ */
220
+ uintptr_t offset_val = 0;
221
+ #if UINTPTR_MAX == UINT64_MAX
222
+ offset_val = labs(offset);
223
+ #else
224
+ offset_val = abs(offset);
225
+ #endif
226
+
227
+ if (offset > 0) {
228
+ *p_ptr = (uint8 *)((uintptr_t)(*p_ptr) + offset_val);
229
+ }
230
+ else {
231
+ *p_ptr = (uint8 *)((uintptr_t)(*p_ptr) - offset_val);
232
+ }
233
+ }
234
+
235
+ int
236
+ gc_migrate(gc_handle_t handle, char *pool_buf_new, gc_size_t pool_buf_size)
237
+ {
238
+ gc_heap_t *heap = (gc_heap_t *)handle;
239
+ char *base_addr_new = pool_buf_new + GC_HEAD_PADDING;
240
+ char *pool_buf_end = pool_buf_new + pool_buf_size;
241
+ intptr_t offset = (uint8 *)base_addr_new - (uint8 *)heap->base_addr;
242
+ hmu_t *cur = NULL, *end = NULL;
243
+ hmu_tree_node_t *tree_node;
244
+ uint8 **p_left, **p_right, **p_parent;
245
+ gc_size_t heap_max_size, size;
246
+
247
+ if ((((uintptr_t)pool_buf_new) & 7) != 0) {
248
+ LOG_ERROR("[GC_ERROR]heap migrate pool buf not 8-byte aligned\n");
249
+ return GC_ERROR;
250
+ }
251
+
252
+ heap_max_size = (uint32)(pool_buf_end - base_addr_new) & (uint32)~7;
253
+
254
+ if (pool_buf_end < base_addr_new || heap_max_size < heap->current_size) {
255
+ LOG_ERROR("[GC_ERROR]heap migrate invalid pool buf size\n");
256
+ return GC_ERROR;
257
+ }
258
+
259
+ if (offset == 0)
260
+ return 0;
261
+
262
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
263
+ if (heap->is_heap_corrupted) {
264
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
265
+ return GC_ERROR;
266
+ }
267
+ #endif
268
+
269
+ heap->base_addr = (uint8 *)base_addr_new;
270
+
271
+ ASSERT_TREE_NODE_ALIGNED_ACCESS(heap->kfc_tree_root);
272
+
273
+ p_left = (uint8 **)((uint8 *)heap->kfc_tree_root
274
+ + offsetof(hmu_tree_node_t, left));
275
+ p_right = (uint8 **)((uint8 *)heap->kfc_tree_root
276
+ + offsetof(hmu_tree_node_t, right));
277
+ p_parent = (uint8 **)((uint8 *)heap->kfc_tree_root
278
+ + offsetof(hmu_tree_node_t, parent));
279
+ adjust_ptr(p_left, offset);
280
+ adjust_ptr(p_right, offset);
281
+ adjust_ptr(p_parent, offset);
282
+
283
+ cur = (hmu_t *)heap->base_addr;
284
+ end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
285
+
286
+ while (cur < end) {
287
+ size = hmu_get_size(cur);
288
+
289
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
290
+ if (size <= 0 || size > (uint32)((uint8 *)end - (uint8 *)cur)) {
291
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
292
+ heap->is_heap_corrupted = true;
293
+ return GC_ERROR;
294
+ }
295
+ #endif
296
+
297
+ if (hmu_get_ut(cur) == HMU_FC && !HMU_IS_FC_NORMAL(size)) {
298
+ tree_node = (hmu_tree_node_t *)cur;
299
+
300
+ ASSERT_TREE_NODE_ALIGNED_ACCESS(tree_node);
301
+
302
+ p_left = (uint8 **)((uint8 *)tree_node
303
+ + offsetof(hmu_tree_node_t, left));
304
+ p_right = (uint8 **)((uint8 *)tree_node
305
+ + offsetof(hmu_tree_node_t, right));
306
+ p_parent = (uint8 **)((uint8 *)tree_node
307
+ + offsetof(hmu_tree_node_t, parent));
308
+ adjust_ptr(p_left, offset);
309
+ adjust_ptr(p_right, offset);
310
+ if (tree_node->parent != heap->kfc_tree_root)
311
+ /* The root node belongs to heap structure,
312
+ it is fixed part and isn't changed. */
313
+ adjust_ptr(p_parent, offset);
314
+ }
315
+ cur = (hmu_t *)((char *)cur + size);
316
+ }
317
+
318
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
319
+ if (cur != end) {
320
+ LOG_ERROR("[GC_ERROR]Heap is corrupted, heap migrate failed.\n");
321
+ heap->is_heap_corrupted = true;
322
+ return GC_ERROR;
323
+ }
324
+ #else
325
+ bh_assert(cur == end);
326
+ #endif
327
+
328
+ return 0;
329
+ }
330
+
331
+ bool
332
+ gc_is_heap_corrupted(gc_handle_t handle)
333
+ {
334
+ #if BH_ENABLE_GC_CORRUPTION_CHECK != 0
335
+ gc_heap_t *heap = (gc_heap_t *)handle;
336
+
337
+ return heap->is_heap_corrupted ? true : false;
338
+ #else
339
+ return false;
340
+ #endif
341
+ }
342
+
343
+ #if BH_ENABLE_GC_VERIFY != 0
344
+ void
345
+ gci_verify_heap(gc_heap_t *heap)
346
+ {
347
+ hmu_t *cur = NULL, *end = NULL;
348
+
349
+ bh_assert(heap && gci_is_heap_valid(heap));
350
+ cur = (hmu_t *)heap->base_addr;
351
+ end = (hmu_t *)(heap->base_addr + heap->current_size);
352
+ while (cur < end) {
353
+ hmu_verify(heap, cur);
354
+ cur = (hmu_t *)((gc_uint8 *)cur + hmu_get_size(cur));
355
+ }
356
+ bh_assert(cur == end);
357
+ }
358
+ #endif
359
+
360
+ void
361
+ gc_heap_stat(void *heap_ptr, gc_stat_t *stat)
362
+ {
363
+ hmu_t *cur = NULL, *end = NULL;
364
+ hmu_type_t ut;
365
+ gc_size_t size;
366
+ gc_heap_t *heap = (gc_heap_t *)heap_ptr;
367
+
368
+ memset(stat, 0, sizeof(gc_stat_t));
369
+ cur = (hmu_t *)heap->base_addr;
370
+ end = (hmu_t *)((char *)heap->base_addr + heap->current_size);
371
+
372
+ while (cur < end) {
373
+ ut = hmu_get_ut(cur);
374
+ size = hmu_get_size(cur);
375
+ bh_assert(size > 0);
376
+
377
+ if (ut == HMU_FC || ut == HMU_FM
378
+ || (ut == HMU_VO && hmu_is_vo_freed(cur))
379
+ || (ut == HMU_WO && !hmu_is_wo_marked(cur))) {
380
+ if (ut == HMU_VO)
381
+ stat->vo_free += size;
382
+ if (ut == HMU_WO)
383
+ stat->wo_free += size;
384
+ stat->free += size;
385
+ stat->free_block++;
386
+ if (size / sizeof(int) < GC_HEAP_STAT_SIZE - 1)
387
+ stat->free_sizes[size / sizeof(int)] += 1;
388
+ else
389
+ stat->free_sizes[GC_HEAP_STAT_SIZE - 1] += 1;
390
+ }
391
+ else {
392
+ if (ut == HMU_VO)
393
+ stat->vo_usage += size;
394
+ if (ut == HMU_WO)
395
+ stat->wo_usage += size;
396
+ stat->usage += size;
397
+ stat->usage_block++;
398
+ if (size / sizeof(int) < GC_HEAP_STAT_SIZE - 1)
399
+ stat->usage_sizes[size / sizeof(int)] += 1;
400
+ else
401
+ stat->usage_sizes[GC_HEAP_STAT_SIZE - 1] += 1;
402
+ }
403
+
404
+ cur = (hmu_t *)((char *)cur + size);
405
+ }
406
+ }
407
+
408
+ void
409
+ gc_print_stat(void *heap_ptr, int verbose)
410
+ {
411
+ gc_stat_t stat;
412
+ int i;
413
+
414
+ bh_assert(heap_ptr != NULL);
415
+ gc_heap_t *heap = (gc_heap_t *)(heap_ptr);
416
+
417
+ gc_heap_stat(heap, &stat);
418
+
419
+ os_printf("# stat %s %p use %d free %d \n", "instance", heap, stat.usage,
420
+ stat.free);
421
+ os_printf("# stat %s %p wo_usage %d vo_usage %d \n", "instance", heap,
422
+ stat.wo_usage, stat.vo_usage);
423
+ os_printf("# stat %s %p wo_free %d vo_free %d \n", "instance", heap,
424
+ stat.wo_free, stat.vo_free);
425
+ #if WASM_ENABLE_GC == 0
426
+ os_printf("# stat free size %" PRIu32 " high %" PRIu32 "\n",
427
+ heap->total_free_size, heap->highmark_size);
428
+ #else
429
+ os_printf("# stat gc %" PRIu32 " free size %" PRIu32 " high %" PRIu32 "\n",
430
+ heap->total_gc_count, heap->total_free_size, heap->highmark_size);
431
+ #endif
432
+ if (verbose) {
433
+ os_printf("usage sizes: \n");
434
+ for (i = 0; i < GC_HEAP_STAT_SIZE; i++)
435
+ if (stat.usage_sizes[i])
436
+ os_printf(" %d: %d; ", i * 4, stat.usage_sizes[i]);
437
+ os_printf(" \n");
438
+ os_printf("free sizes: \n");
439
+ for (i = 0; i < GC_HEAP_STAT_SIZE; i++)
440
+ if (stat.free_sizes[i])
441
+ os_printf(" %d: %d; ", i * 4, stat.free_sizes[i]);
442
+ }
443
+ }
444
+
445
+ void *
446
+ gc_heap_stats(void *heap_arg, uint32 *stats, int size)
447
+ {
448
+ int i;
449
+ gc_heap_t *heap = (gc_heap_t *)heap_arg;
450
+
451
+ if (!gci_is_heap_valid(heap)) {
452
+ for (i = 0; i < size; i++)
453
+ stats[i] = 0;
454
+ return NULL;
455
+ }
456
+
457
+ for (i = 0; i < size; i++) {
458
+ switch (i) {
459
+ case GC_STAT_TOTAL:
460
+ stats[i] = heap->current_size;
461
+ break;
462
+ case GC_STAT_FREE:
463
+ stats[i] = heap->total_free_size;
464
+ break;
465
+ case GC_STAT_HIGHMARK:
466
+ stats[i] = heap->highmark_size;
467
+ break;
468
+ #if WASM_ENABLE_GC != 0
469
+ case GC_STAT_COUNT:
470
+ stats[i] = heap->total_gc_count;
471
+ break;
472
+ case GC_STAT_TIME:
473
+ stats[i] = heap->total_gc_time;
474
+ break;
475
+ #endif
476
+ default:
477
+ break;
478
+ }
479
+ }
480
+
481
+ return heap;
482
+ }
483
+
484
+ void
485
+ gc_traverse_tree(hmu_tree_node_t *node, gc_size_t *stats, int *n)
486
+ {
487
+ if (!node)
488
+ return;
489
+
490
+ if (*n > 0)
491
+ gc_traverse_tree(node->right, stats, n);
492
+
493
+ if (*n > 0) {
494
+ (*n)--;
495
+ stats[*n] = node->size;
496
+ }
497
+
498
+ if (*n > 0)
499
+ gc_traverse_tree(node->left, stats, n);
500
+ }
501
+
502
+ void
503
+ gc_show_stat(void *heap)
504
+ {
505
+
506
+ uint32 stats[GC_STAT_MAX];
507
+
508
+ heap = gc_heap_stats(heap, stats, GC_STAT_MAX);
509
+
510
+ os_printf("\n[GC stats %p] %" PRIu32 " %" PRIu32 " %" PRIu32 " %" PRIu32
511
+ " %" PRIu32 "\n",
512
+ heap, stats[0], stats[1], stats[2], stats[3], stats[4]);
513
+ }
514
+
515
+ #if WASM_ENABLE_GC != 0
516
+ void
517
+ gc_show_fragment(void *heap_arg)
518
+ {
519
+ uint32 stats[3];
520
+ int n = 3;
521
+ gc_heap_t *heap = (gc_heap_t *)heap_arg;
522
+
523
+ memset(stats, 0, n * sizeof(int));
524
+ gct_vm_mutex_lock(&heap->lock);
525
+ gc_traverse_tree(heap->kfc_tree_root, (gc_size_t *)stats, &n);
526
+ gct_vm_mutex_unlock(&heap->lock);
527
+ os_printf("\n[GC %p top sizes] %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", heap,
528
+ stats[0], stats[1], stats[2]);
529
+ }
530
+
531
+ #if WASM_ENABLE_GC_PERF_PROFILING != 0
532
+ void
533
+ gc_dump_perf_profiling(gc_handle_t *handle)
534
+ {
535
+ gc_heap_t *gc_heap_handle = (void *)handle;
536
+ if (gc_heap_handle) {
537
+ os_printf("\nGC performance summary\n");
538
+ os_printf(" Total GC time (ms): %u\n",
539
+ gc_heap_handle->total_gc_time);
540
+ os_printf(" Max GC time (ms): %u\n", gc_heap_handle->max_gc_time);
541
+ }
542
+ else {
543
+ os_printf("Failed to dump GC performance\n");
544
+ }
545
+ }
546
+ #endif
547
+ #endif