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,246 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #ifndef _WASM_C_API_INTERNAL_H
7
+ #define _WASM_C_API_INTERNAL_H
8
+
9
+ #include "../include/wasm_c_api.h"
10
+ #include "wasm_runtime_common.h"
11
+
12
+ #ifndef own
13
+ #define own
14
+ #endif
15
+
16
+ /* Vectors */
17
+ /* we will malloc resource for the vector's data field */
18
+ /* we will release resource of data */
19
+ /* caller needs to take care resource for the vector itself */
20
+ #define DEFAULT_VECTOR_INIT_LENGTH (64)
21
+
22
+ WASM_DECLARE_VEC(instance, *)
23
+ WASM_DECLARE_VEC(module, *)
24
+ WASM_DECLARE_VEC(store, *)
25
+
26
+ /* Runtime Environment */
27
+ struct wasm_engine_t {
28
+ uint32 ref_count;
29
+ /* list of wasm_module_ex_t */
30
+ Vector modules;
31
+ /* list of stores which are classified according to tids */
32
+ Vector stores_by_tid;
33
+ };
34
+
35
+ struct wasm_store_t {
36
+ /* maybe should remove the list */
37
+ wasm_module_vec_t *modules;
38
+ wasm_instance_vec_t *instances;
39
+ Vector *foreigns;
40
+ };
41
+
42
+ /* Type Representations */
43
+ struct wasm_valtype_t {
44
+ wasm_valkind_t kind;
45
+ };
46
+
47
+ struct wasm_functype_t {
48
+ uint32 extern_kind;
49
+ /* gona to new and delete own */
50
+ wasm_valtype_vec_t *params;
51
+ wasm_valtype_vec_t *results;
52
+ };
53
+
54
+ struct wasm_globaltype_t {
55
+ uint32 extern_kind;
56
+ /* gona to new and delete own */
57
+ wasm_valtype_t *val_type;
58
+ wasm_mutability_t mutability;
59
+ };
60
+
61
+ struct wasm_tabletype_t {
62
+ uint32 extern_kind;
63
+ wasm_valtype_t *val_type;
64
+ wasm_limits_t limits;
65
+ };
66
+
67
+ struct wasm_memorytype_t {
68
+ uint32 extern_kind;
69
+ wasm_limits_t limits;
70
+ };
71
+
72
+ struct wasm_externtype_t {
73
+ uint32 extern_kind;
74
+ /* reserved space */
75
+ uint8 data[1];
76
+ };
77
+
78
+ struct wasm_importtype_t {
79
+ wasm_name_t *module_name;
80
+ wasm_name_t *name;
81
+ wasm_externtype_t *extern_type;
82
+ };
83
+
84
+ struct wasm_exporttype_t {
85
+ wasm_name_t *name;
86
+ wasm_externtype_t *extern_type;
87
+ };
88
+
89
+ /* Runtime Objects */
90
+ enum wasm_reference_kind {
91
+ WASM_REF_foreign,
92
+ WASM_REF_func,
93
+ WASM_REF_global,
94
+ WASM_REF_memory,
95
+ WASM_REF_table,
96
+ };
97
+
98
+ struct wasm_host_info {
99
+ void *info;
100
+ void (*finalizer)(void *);
101
+ };
102
+
103
+ struct wasm_ref_t {
104
+ wasm_store_t *store;
105
+ enum wasm_reference_kind kind;
106
+ struct wasm_host_info host_info;
107
+ uint32 ref_idx_rt;
108
+ WASMModuleInstanceCommon *inst_comm_rt;
109
+ };
110
+
111
+ struct wasm_trap_t {
112
+ wasm_byte_vec_t *message;
113
+ Vector *frames;
114
+ };
115
+
116
+ struct wasm_foreign_t {
117
+ wasm_store_t *store;
118
+ enum wasm_reference_kind kind;
119
+ struct wasm_host_info host_info;
120
+ int32 ref_cnt;
121
+ uint32 foreign_idx_rt;
122
+ WASMModuleInstanceCommon *inst_comm_rt;
123
+ };
124
+
125
+ struct wasm_func_t {
126
+ wasm_store_t *store;
127
+ wasm_name_t *module_name;
128
+ wasm_name_t *name;
129
+ uint16 kind;
130
+
131
+ struct wasm_host_info host_info;
132
+ wasm_functype_t *type;
133
+ uint16 param_count;
134
+ uint16 result_count;
135
+
136
+ bool with_env;
137
+ union {
138
+ wasm_func_callback_t cb;
139
+ struct callback_ext {
140
+ void *env;
141
+ wasm_func_callback_with_env_t cb;
142
+ void (*finalizer)(void *);
143
+ } cb_env;
144
+ } u;
145
+ /*
146
+ * an index in both functions runtime instance lists
147
+ * of interpreter mode and aot mode
148
+ */
149
+ uint16 func_idx_rt;
150
+ WASMModuleInstanceCommon *inst_comm_rt;
151
+ WASMFunctionInstanceCommon *func_comm_rt;
152
+ };
153
+
154
+ struct wasm_global_t {
155
+ wasm_store_t *store;
156
+ wasm_name_t *module_name;
157
+ wasm_name_t *name;
158
+ uint16 kind;
159
+
160
+ struct wasm_host_info host_info;
161
+ wasm_globaltype_t *type;
162
+ wasm_val_t *init;
163
+ /*
164
+ * an index in both global runtime instance lists
165
+ * of interpreter mode and aot mode
166
+ */
167
+ uint16 global_idx_rt;
168
+ WASMModuleInstanceCommon *inst_comm_rt;
169
+ };
170
+
171
+ struct wasm_memory_t {
172
+ wasm_store_t *store;
173
+ wasm_name_t *module_name;
174
+ wasm_name_t *name;
175
+ uint16 kind;
176
+
177
+ struct wasm_host_info host_info;
178
+ wasm_memorytype_t *type;
179
+ /*
180
+ * an index in both memory runtime instance lists
181
+ * of interpreter mode and aot mode
182
+ */
183
+ uint16 memory_idx_rt;
184
+ WASMModuleInstanceCommon *inst_comm_rt;
185
+ };
186
+
187
+ struct wasm_table_t {
188
+ wasm_store_t *store;
189
+ wasm_name_t *module_name;
190
+ wasm_name_t *name;
191
+ uint16 kind;
192
+
193
+ struct wasm_host_info host_info;
194
+ wasm_tabletype_t *type;
195
+ /*
196
+ * an index in both table runtime instance lists
197
+ * of interpreter mode and aot mode
198
+ */
199
+ uint16 table_idx_rt;
200
+ WASMModuleInstanceCommon *inst_comm_rt;
201
+ };
202
+
203
+ struct wasm_extern_t {
204
+ wasm_store_t *store;
205
+ wasm_name_t *module_name;
206
+ wasm_name_t *name;
207
+ wasm_externkind_t kind;
208
+ /* reserved space */
209
+ uint8 data[1];
210
+ };
211
+
212
+ struct wasm_instance_t {
213
+ wasm_store_t *store;
214
+ wasm_extern_vec_t *exports;
215
+ struct wasm_host_info host_info;
216
+ WASMModuleInstanceCommon *inst_comm_rt;
217
+ };
218
+
219
+ wasm_ref_t *
220
+ wasm_ref_new_internal(wasm_store_t *store, enum wasm_reference_kind kind,
221
+ uint32 obj_idx_rt,
222
+ WASMModuleInstanceCommon *inst_comm_rt);
223
+
224
+ wasm_foreign_t *
225
+ wasm_foreign_new_internal(wasm_store_t *store, uint32 foreign_idx_rt,
226
+ WASMModuleInstanceCommon *inst_comm_rt);
227
+
228
+ wasm_func_t *
229
+ wasm_func_new_internal(wasm_store_t *store, uint16 func_idx_rt,
230
+ WASMModuleInstanceCommon *inst_comm_rt);
231
+
232
+ wasm_global_t *
233
+ wasm_global_new_internal(wasm_store_t *store, uint16 global_idx_rt,
234
+ WASMModuleInstanceCommon *inst_comm_rt);
235
+
236
+ wasm_memory_t *
237
+ wasm_memory_new_internal(wasm_store_t *store, uint16 memory_idx_rt,
238
+ WASMModuleInstanceCommon *inst_comm_rt);
239
+
240
+ wasm_table_t *
241
+ wasm_table_new_internal(wasm_store_t *store, uint16 table_idx_rt,
242
+ WASMModuleInstanceCommon *inst_comm_rt);
243
+
244
+ void
245
+ wasm_frame_vec_clone_internal(Vector *src, Vector *out);
246
+ #endif /* _WASM_C_API_INTERNAL_H */
@@ -0,0 +1,336 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "wasm_exec_env.h"
7
+ #include "wasm_runtime_common.h"
8
+ #if WASM_ENABLE_GC != 0
9
+ #include "mem_alloc.h"
10
+ #endif
11
+ #if WASM_ENABLE_INTERP != 0
12
+ #include "../interpreter/wasm_runtime.h"
13
+ #endif
14
+ #if WASM_ENABLE_AOT != 0
15
+ #include "../aot/aot_runtime.h"
16
+ #endif
17
+
18
+ #if WASM_ENABLE_AOT != 0
19
+ #include "aot_runtime.h"
20
+ #endif
21
+
22
+ #if WASM_ENABLE_THREAD_MGR != 0
23
+ #include "../libraries/thread-mgr/thread_manager.h"
24
+ #if WASM_ENABLE_DEBUG_INTERP != 0
25
+ #include "../libraries/debug-engine/debug_engine.h"
26
+ #endif
27
+ #endif
28
+
29
+ WASMExecEnv *
30
+ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
31
+ uint32 stack_size)
32
+ {
33
+ uint64 total_size =
34
+ offsetof(WASMExecEnv, wasm_stack_u.bottom) + (uint64)stack_size;
35
+ WASMExecEnv *exec_env;
36
+
37
+ if (total_size >= UINT32_MAX
38
+ || !(exec_env = wasm_runtime_malloc((uint32)total_size)))
39
+ return NULL;
40
+
41
+ memset(exec_env, 0, (uint32)total_size);
42
+
43
+ #if WASM_ENABLE_AOT != 0
44
+ if (!(exec_env->argv_buf = wasm_runtime_malloc(sizeof(uint32) * 64))) {
45
+ goto fail1;
46
+ }
47
+ #endif
48
+
49
+ #if WASM_ENABLE_THREAD_MGR != 0
50
+ if (os_mutex_init(&exec_env->wait_lock) != 0)
51
+ goto fail2;
52
+
53
+ if (os_cond_init(&exec_env->wait_cond) != 0)
54
+ goto fail3;
55
+
56
+ #if WASM_ENABLE_DEBUG_INTERP != 0
57
+ if (!(exec_env->current_status = wasm_cluster_create_exenv_status()))
58
+ goto fail4;
59
+ #endif
60
+ #endif
61
+
62
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
63
+ if (!(exec_env->exce_check_guard_page =
64
+ os_mmap(NULL, os_getpagesize(), MMAP_PROT_NONE, MMAP_MAP_NONE,
65
+ os_get_invalid_handle())))
66
+ goto fail5;
67
+ #endif
68
+
69
+ exec_env->module_inst = module_inst;
70
+ exec_env->wasm_stack_size = stack_size;
71
+ exec_env->wasm_stack.bottom = exec_env->wasm_stack_u.bottom;
72
+ exec_env->wasm_stack.top_boundary =
73
+ exec_env->wasm_stack.bottom + stack_size;
74
+ exec_env->wasm_stack.top = exec_env->wasm_stack.bottom;
75
+
76
+ #if WASM_ENABLE_AOT != 0
77
+ if (module_inst->module_type == Wasm_Module_AoT) {
78
+ AOTModuleInstance *i = (AOTModuleInstance *)module_inst;
79
+ AOTModule *m = (AOTModule *)i->module;
80
+ exec_env->native_symbol = m->native_symbol_list;
81
+ }
82
+ #endif
83
+
84
+ #if WASM_ENABLE_MEMORY_TRACING != 0
85
+ wasm_runtime_dump_exec_env_mem_consumption(exec_env);
86
+ #endif
87
+
88
+ #if WASM_ENABLE_INSTRUCTION_METERING != 0
89
+ exec_env->instructions_to_execute = -1;
90
+ #endif
91
+
92
+ return exec_env;
93
+
94
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
95
+ fail5:
96
+ #if WASM_ENABLE_THREAD_MGR != 0 && WASM_ENABLE_DEBUG_INTERP != 0
97
+ wasm_cluster_destroy_exenv_status(exec_env->current_status);
98
+ #endif
99
+ #endif
100
+ #if WASM_ENABLE_THREAD_MGR != 0
101
+ #if WASM_ENABLE_DEBUG_INTERP != 0
102
+ fail4:
103
+ os_cond_destroy(&exec_env->wait_cond);
104
+ #endif
105
+ fail3:
106
+ os_mutex_destroy(&exec_env->wait_lock);
107
+ fail2:
108
+ #endif
109
+ #if WASM_ENABLE_AOT != 0
110
+ wasm_runtime_free(exec_env->argv_buf);
111
+ fail1:
112
+ #endif
113
+ wasm_runtime_free(exec_env);
114
+ return NULL;
115
+ }
116
+
117
+ void
118
+ wasm_exec_env_destroy_internal(WASMExecEnv *exec_env)
119
+ {
120
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
121
+ os_munmap(exec_env->exce_check_guard_page, os_getpagesize());
122
+ #endif
123
+ #if WASM_ENABLE_THREAD_MGR != 0
124
+ os_mutex_destroy(&exec_env->wait_lock);
125
+ os_cond_destroy(&exec_env->wait_cond);
126
+ #if WASM_ENABLE_DEBUG_INTERP != 0
127
+ wasm_cluster_destroy_exenv_status(exec_env->current_status);
128
+ #endif
129
+ #endif
130
+ #if WASM_ENABLE_AOT != 0
131
+ wasm_runtime_free(exec_env->argv_buf);
132
+ #endif
133
+ wasm_runtime_free(exec_env);
134
+ }
135
+
136
+ WASMExecEnv *
137
+ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
138
+ uint32 stack_size)
139
+ {
140
+ #if WASM_ENABLE_THREAD_MGR != 0
141
+ WASMCluster *cluster;
142
+ #endif
143
+ WASMExecEnv *exec_env =
144
+ wasm_exec_env_create_internal(module_inst, stack_size);
145
+ #if WASM_ENABLE_GC != 0
146
+ void *gc_heap_handle = NULL;
147
+ #endif
148
+
149
+ if (!exec_env)
150
+ return NULL;
151
+
152
+ #if WASM_ENABLE_INTERP != 0
153
+ /* Set the aux_stack_boundary and aux_stack_bottom */
154
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
155
+ WASMModule *module = ((WASMModuleInstance *)module_inst)->module;
156
+ exec_env->aux_stack_bottom = (uintptr_t)module->aux_stack_bottom;
157
+ exec_env->aux_stack_boundary =
158
+ (uintptr_t)module->aux_stack_bottom - module->aux_stack_size;
159
+ #if WASM_ENABLE_GC != 0
160
+ gc_heap_handle =
161
+ ((WASMModuleInstance *)module_inst)->e->common.gc_heap_pool;
162
+ #endif
163
+ }
164
+ #endif
165
+ #if WASM_ENABLE_AOT != 0
166
+ /* Set the aux_stack_boundary and aux_stack_bottom */
167
+ if (module_inst->module_type == Wasm_Module_AoT) {
168
+ AOTModule *module =
169
+ (AOTModule *)((AOTModuleInstance *)module_inst)->module;
170
+ exec_env->aux_stack_bottom = (uintptr_t)module->aux_stack_bottom;
171
+ exec_env->aux_stack_boundary =
172
+ (uintptr_t)module->aux_stack_bottom - module->aux_stack_size;
173
+ #if WASM_ENABLE_GC != 0
174
+ gc_heap_handle =
175
+ ((AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e)
176
+ ->common.gc_heap_handle;
177
+ #endif
178
+ }
179
+ #endif
180
+
181
+ #if WASM_ENABLE_THREAD_MGR != 0
182
+ /* Create a new cluster for this exec_env */
183
+ if (!(cluster = wasm_cluster_create(exec_env))) {
184
+ wasm_exec_env_destroy_internal(exec_env);
185
+ return NULL;
186
+ }
187
+ #if WASM_ENABLE_GC != 0
188
+ mem_allocator_enable_gc_reclaim(gc_heap_handle, cluster);
189
+ #endif
190
+ #else
191
+ #if WASM_ENABLE_GC != 0
192
+ mem_allocator_enable_gc_reclaim(gc_heap_handle, exec_env);
193
+ #endif
194
+ #endif /* end of WASM_ENABLE_THREAD_MGR */
195
+
196
+ return exec_env;
197
+ }
198
+
199
+ void
200
+ wasm_exec_env_destroy(WASMExecEnv *exec_env)
201
+ {
202
+ #if WASM_ENABLE_THREAD_MGR != 0
203
+ /* Wait for all sub-threads */
204
+ WASMCluster *cluster = wasm_exec_env_get_cluster(exec_env);
205
+ if (cluster) {
206
+ wasm_cluster_wait_for_all_except_self(cluster, exec_env);
207
+ #if WASM_ENABLE_DEBUG_INTERP != 0
208
+ /* Must fire exit event after other threads exits, otherwise
209
+ the stopped thread will be overridden by other threads */
210
+ wasm_cluster_thread_exited(exec_env);
211
+ #endif
212
+ /* We have waited for other threads, this is the only alive thread, so
213
+ * we don't acquire cluster->lock because the cluster will be destroyed
214
+ * inside this function */
215
+ wasm_cluster_del_exec_env(cluster, exec_env);
216
+ }
217
+ #endif /* end of WASM_ENABLE_THREAD_MGR */
218
+
219
+ wasm_exec_env_destroy_internal(exec_env);
220
+ }
221
+
222
+ WASMModuleInstanceCommon *
223
+ wasm_exec_env_get_module_inst(WASMExecEnv *exec_env)
224
+ {
225
+ return exec_env->module_inst;
226
+ }
227
+
228
+ void
229
+ wasm_exec_env_set_module_inst(WASMExecEnv *exec_env,
230
+ WASMModuleInstanceCommon *const module_inst)
231
+ {
232
+ #if WASM_ENABLE_THREAD_MGR != 0
233
+ wasm_cluster_traverse_lock(exec_env);
234
+ #endif
235
+ exec_env->module_inst = module_inst;
236
+ #if WASM_ENABLE_THREAD_MGR != 0
237
+ wasm_cluster_traverse_unlock(exec_env);
238
+ #endif
239
+ }
240
+
241
+ void
242
+ wasm_exec_env_restore_module_inst(
243
+ WASMExecEnv *exec_env, WASMModuleInstanceCommon *const module_inst_common)
244
+ {
245
+ WASMModuleInstanceCommon *old_module_inst_common = exec_env->module_inst;
246
+ WASMModuleInstance *old_module_inst =
247
+ (WASMModuleInstance *)old_module_inst_common;
248
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_common;
249
+ char cur_exception[EXCEPTION_BUF_LEN];
250
+
251
+ #if WASM_ENABLE_THREAD_MGR != 0
252
+ wasm_cluster_traverse_lock(exec_env);
253
+ #endif
254
+ exec_env->module_inst = module_inst_common;
255
+ /*
256
+ * propagate an exception if any.
257
+ */
258
+ exception_lock(old_module_inst);
259
+ if (old_module_inst->cur_exception[0] != '\0') {
260
+ bh_memcpy_s(cur_exception, sizeof(cur_exception),
261
+ old_module_inst->cur_exception,
262
+ sizeof(old_module_inst->cur_exception));
263
+ }
264
+ else {
265
+ cur_exception[0] = '\0';
266
+ }
267
+ exception_unlock(old_module_inst);
268
+ #if WASM_ENABLE_THREAD_MGR != 0
269
+ wasm_cluster_traverse_unlock(exec_env);
270
+ #endif
271
+ if (cur_exception[0] != '\0') {
272
+ exception_lock(module_inst);
273
+ bh_memcpy_s(module_inst->cur_exception,
274
+ sizeof(module_inst->cur_exception), cur_exception,
275
+ sizeof(cur_exception));
276
+ exception_unlock(module_inst);
277
+ }
278
+ }
279
+
280
+ void
281
+ wasm_exec_env_set_thread_info(WASMExecEnv *exec_env)
282
+ {
283
+ #if WASM_ENABLE_THREAD_MGR != 0
284
+ os_mutex_lock(&exec_env->wait_lock);
285
+ #endif
286
+ exec_env->handle = os_self_thread();
287
+ if (exec_env->user_native_stack_boundary)
288
+ /* WASM_STACK_GUARD_SIZE isn't added for flexibility to developer,
289
+ he must ensure that enough guard bytes are kept. */
290
+ exec_env->native_stack_boundary = exec_env->user_native_stack_boundary;
291
+ else {
292
+ uint8 *stack_boundary = os_thread_get_stack_boundary();
293
+ exec_env->native_stack_boundary =
294
+ stack_boundary ? stack_boundary + WASM_STACK_GUARD_SIZE : NULL;
295
+ }
296
+ exec_env->native_stack_top_min = (void *)UINTPTR_MAX;
297
+ #if WASM_ENABLE_THREAD_MGR != 0
298
+ os_mutex_unlock(&exec_env->wait_lock);
299
+ #endif
300
+ }
301
+
302
+ #if WASM_ENABLE_THREAD_MGR != 0
303
+ void *
304
+ wasm_exec_env_get_thread_arg(WASMExecEnv *exec_env)
305
+ {
306
+ return exec_env->thread_arg;
307
+ }
308
+
309
+ void
310
+ wasm_exec_env_set_thread_arg(WASMExecEnv *exec_env, void *thread_arg)
311
+ {
312
+ exec_env->thread_arg = thread_arg;
313
+ }
314
+ #endif
315
+
316
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
317
+ void
318
+ wasm_exec_env_push_jmpbuf(WASMExecEnv *exec_env, WASMJmpBuf *jmpbuf)
319
+ {
320
+ jmpbuf->prev = exec_env->jmpbuf_stack_top;
321
+ exec_env->jmpbuf_stack_top = jmpbuf;
322
+ }
323
+
324
+ WASMJmpBuf *
325
+ wasm_exec_env_pop_jmpbuf(WASMExecEnv *exec_env)
326
+ {
327
+ WASMJmpBuf *stack_top = exec_env->jmpbuf_stack_top;
328
+
329
+ if (stack_top) {
330
+ exec_env->jmpbuf_stack_top = stack_top->prev;
331
+ return stack_top;
332
+ }
333
+
334
+ return NULL;
335
+ }
336
+ #endif