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,477 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "bh_log.h"
7
+ #include "wasm_shared_memory.h"
8
+ #if WASM_ENABLE_THREAD_MGR != 0
9
+ #include "../libraries/thread-mgr/thread_manager.h"
10
+ #endif
11
+ #if WASM_ENABLE_AOT != 0
12
+ #include "../aot/aot_runtime.h"
13
+ #endif
14
+
15
+ /*
16
+ * Note: this lock can be per memory.
17
+ *
18
+ * For now, just use a global because:
19
+ * - it's a bit cumbersome to extend WASMMemoryInstance w/o breaking
20
+ * the AOT ABI.
21
+ * - If you care performance, it's better to make the interpreters
22
+ * use atomic ops.
23
+ */
24
+ korp_mutex g_shared_memory_lock;
25
+
26
+ /* clang-format off */
27
+ enum {
28
+ S_WAITING,
29
+ S_NOTIFIED
30
+ };
31
+ /* clang-format on */
32
+
33
+ typedef struct AtomicWaitInfo {
34
+ bh_list wait_list_head;
35
+ bh_list *wait_list;
36
+ /* WARNING: insert to the list allowed only in acquire_wait_info
37
+ otherwise there will be data race as described in PR #2016 */
38
+ } AtomicWaitInfo;
39
+
40
+ typedef struct AtomicWaitNode {
41
+ bh_list_link l;
42
+ uint8 status;
43
+ korp_cond wait_cond;
44
+ } AtomicWaitNode;
45
+
46
+ /* Atomic wait map */
47
+ static HashMap *wait_map;
48
+
49
+ static uint32
50
+ wait_address_hash(const void *address);
51
+
52
+ static bool
53
+ wait_address_equal(void *h1, void *h2);
54
+
55
+ static void
56
+ destroy_wait_info(void *wait_info);
57
+
58
+ bool
59
+ wasm_shared_memory_init()
60
+ {
61
+ if (os_mutex_init(&g_shared_memory_lock) != 0)
62
+ return false;
63
+ /* wait map not exists, create new map */
64
+ if (!(wait_map = bh_hash_map_create(32, true, (HashFunc)wait_address_hash,
65
+ (KeyEqualFunc)wait_address_equal, NULL,
66
+ destroy_wait_info))) {
67
+ os_mutex_destroy(&g_shared_memory_lock);
68
+ return false;
69
+ }
70
+ return true;
71
+ }
72
+
73
+ void
74
+ wasm_shared_memory_destroy()
75
+ {
76
+ bh_hash_map_destroy(wait_map);
77
+ os_mutex_destroy(&g_shared_memory_lock);
78
+ }
79
+
80
+ uint16
81
+ shared_memory_inc_reference(WASMMemoryInstance *memory)
82
+ {
83
+ bh_assert(shared_memory_is_shared(memory));
84
+ uint16 old;
85
+ #if BH_ATOMIC_16_IS_ATOMIC == 0
86
+ os_mutex_lock(&g_shared_memory_lock);
87
+ #endif
88
+ old = BH_ATOMIC_16_FETCH_ADD(memory->ref_count, 1);
89
+ #if BH_ATOMIC_16_IS_ATOMIC == 0
90
+ os_mutex_unlock(&g_shared_memory_lock);
91
+ #endif
92
+ bh_assert(old >= 1);
93
+ bh_assert(old < UINT16_MAX);
94
+ return old + 1;
95
+ }
96
+
97
+ uint16
98
+ shared_memory_dec_reference(WASMMemoryInstance *memory)
99
+ {
100
+ bh_assert(shared_memory_is_shared(memory));
101
+ uint16 old;
102
+ #if BH_ATOMIC_16_IS_ATOMIC == 0
103
+ os_mutex_lock(&g_shared_memory_lock);
104
+ #endif
105
+ old = BH_ATOMIC_16_FETCH_SUB(memory->ref_count, 1);
106
+ #if BH_ATOMIC_16_IS_ATOMIC == 0
107
+ os_mutex_unlock(&g_shared_memory_lock);
108
+ #endif
109
+ bh_assert(old > 0);
110
+ return old - 1;
111
+ }
112
+
113
+ static korp_mutex *
114
+ shared_memory_get_lock_pointer(WASMMemoryInstance *memory)
115
+ {
116
+ bh_assert(memory != NULL);
117
+ return &g_shared_memory_lock;
118
+ }
119
+
120
+ /* Atomics wait && notify APIs */
121
+ static uint32
122
+ wait_address_hash(const void *address)
123
+ {
124
+ return (uint32)(uintptr_t)address;
125
+ }
126
+
127
+ static bool
128
+ wait_address_equal(void *h1, void *h2)
129
+ {
130
+ return h1 == h2 ? true : false;
131
+ }
132
+
133
+ static bool
134
+ is_wait_node_exists(bh_list *wait_list, AtomicWaitNode *node)
135
+ {
136
+ AtomicWaitNode *curr;
137
+ curr = bh_list_first_elem(wait_list);
138
+
139
+ while (curr) {
140
+ if (curr == node) {
141
+ return true;
142
+ }
143
+ curr = bh_list_elem_next(curr);
144
+ }
145
+
146
+ return false;
147
+ }
148
+
149
+ static uint32
150
+ notify_wait_list(bh_list *wait_list, uint32 count)
151
+ {
152
+ AtomicWaitNode *node, *next;
153
+ uint32 i, notify_count = count;
154
+
155
+ if (count > wait_list->len)
156
+ notify_count = wait_list->len;
157
+
158
+ node = bh_list_first_elem(wait_list);
159
+ if (!node)
160
+ return 0;
161
+
162
+ for (i = 0; i < notify_count; i++) {
163
+ bh_assert(node);
164
+ next = bh_list_elem_next(node);
165
+
166
+ node->status = S_NOTIFIED;
167
+ /* wakeup */
168
+ os_cond_signal(&node->wait_cond);
169
+
170
+ node = next;
171
+ }
172
+
173
+ return notify_count;
174
+ }
175
+
176
+ static AtomicWaitInfo *
177
+ acquire_wait_info(void *address, AtomicWaitNode *wait_node)
178
+ {
179
+ AtomicWaitInfo *wait_info = NULL;
180
+ bh_list_status ret;
181
+
182
+ bh_assert(address != NULL);
183
+
184
+ wait_info = (AtomicWaitInfo *)bh_hash_map_find(wait_map, address);
185
+
186
+ if (!wait_node) {
187
+ return wait_info;
188
+ }
189
+
190
+ /* No wait info on this address, create new info */
191
+ if (!wait_info) {
192
+ if (!(wait_info = (AtomicWaitInfo *)wasm_runtime_malloc(
193
+ sizeof(AtomicWaitInfo)))) {
194
+ return NULL;
195
+ }
196
+ memset(wait_info, 0, sizeof(AtomicWaitInfo));
197
+
198
+ /* init wait list */
199
+ wait_info->wait_list = &wait_info->wait_list_head;
200
+ ret = bh_list_init(wait_info->wait_list);
201
+ bh_assert(ret == BH_LIST_SUCCESS);
202
+ (void)ret;
203
+
204
+ if (!bh_hash_map_insert(wait_map, address, (void *)wait_info)) {
205
+ wasm_runtime_free(wait_info);
206
+ return NULL;
207
+ }
208
+ }
209
+
210
+ ret = bh_list_insert(wait_info->wait_list, wait_node);
211
+ bh_assert(ret == BH_LIST_SUCCESS);
212
+ (void)ret;
213
+
214
+ return wait_info;
215
+ }
216
+
217
+ static void
218
+ destroy_wait_info(void *wait_info)
219
+ {
220
+ AtomicWaitNode *node, *next;
221
+
222
+ if (wait_info) {
223
+
224
+ node = bh_list_first_elem(((AtomicWaitInfo *)wait_info)->wait_list);
225
+
226
+ while (node) {
227
+ next = bh_list_elem_next(node);
228
+ os_cond_destroy(&node->wait_cond);
229
+ wasm_runtime_free(node);
230
+ node = next;
231
+ }
232
+
233
+ wasm_runtime_free(wait_info);
234
+ }
235
+ }
236
+
237
+ static void
238
+ map_try_release_wait_info(HashMap *wait_hash_map, AtomicWaitInfo *wait_info,
239
+ void *address)
240
+ {
241
+ if (wait_info->wait_list->len > 0) {
242
+ return;
243
+ }
244
+
245
+ bh_hash_map_remove(wait_hash_map, address, NULL, NULL);
246
+ destroy_wait_info(wait_info);
247
+ }
248
+
249
+ #if WASM_ENABLE_SHARED_HEAP != 0
250
+ static bool
251
+ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
252
+ uint8 *addr, uint32 bytes)
253
+ {
254
+ WASMSharedHeap *shared_heap = NULL;
255
+
256
+ #if WASM_ENABLE_INTERP != 0
257
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
258
+ shared_heap = ((WASMModuleInstance *)module_inst)->e->shared_heap;
259
+ }
260
+ #endif
261
+ #if WASM_ENABLE_AOT != 0
262
+ if (module_inst->module_type == Wasm_Module_AoT) {
263
+ AOTModuleInstanceExtra *e =
264
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
265
+ shared_heap = e->shared_heap;
266
+ }
267
+ #endif
268
+
269
+ return shared_heap && addr >= shared_heap->base_addr
270
+ && addr + bytes <= shared_heap->base_addr + shared_heap->size;
271
+ }
272
+ #endif
273
+
274
+ uint32
275
+ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
276
+ uint64 expect, int64 timeout, bool wait64)
277
+ {
278
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module;
279
+ AtomicWaitInfo *wait_info;
280
+ AtomicWaitNode *wait_node;
281
+ korp_mutex *lock;
282
+ #if WASM_ENABLE_THREAD_MGR != 0
283
+ WASMExecEnv *exec_env;
284
+ #endif
285
+ uint64 timeout_left, timeout_wait, timeout_1sec;
286
+ bool check_ret, is_timeout, no_wait;
287
+
288
+ bh_assert(module->module_type == Wasm_Module_Bytecode
289
+ || module->module_type == Wasm_Module_AoT);
290
+
291
+ if (wasm_copy_exception(module_inst, NULL)) {
292
+ return -1;
293
+ }
294
+
295
+ /* Currently we have only one memory instance */
296
+ if (!shared_memory_is_shared(module_inst->memories[0])) {
297
+ wasm_runtime_set_exception(module, "expected shared memory");
298
+ return -1;
299
+ }
300
+
301
+ shared_memory_lock(module_inst->memories[0]);
302
+ if (
303
+ #if WASM_ENABLE_SHARED_HEAP != 0
304
+ /* not in shared heap */
305
+ !is_native_addr_in_shared_heap((WASMModuleInstanceCommon *)module_inst,
306
+ address, wait64 ? 8 : 4)
307
+ &&
308
+ #endif
309
+ /* and not in linear memory */
310
+ ((uint8 *)address < module_inst->memories[0]->memory_data
311
+ || (uint8 *)address + (wait64 ? 8 : 4)
312
+ > module_inst->memories[0]->memory_data_end)) {
313
+ shared_memory_unlock(module_inst->memories[0]);
314
+ wasm_runtime_set_exception(module, "out of bounds memory access");
315
+ return -1;
316
+ }
317
+ shared_memory_unlock(module_inst->memories[0]);
318
+
319
+ #if WASM_ENABLE_THREAD_MGR != 0
320
+ exec_env =
321
+ wasm_clusters_search_exec_env((WASMModuleInstanceCommon *)module_inst);
322
+ bh_assert(exec_env);
323
+ #endif
324
+
325
+ lock = shared_memory_get_lock_pointer(module_inst->memories[0]);
326
+
327
+ /* Lock the shared_mem_lock for the whole atomic wait process,
328
+ and use it to os_cond_reltimedwait */
329
+ os_mutex_lock(lock);
330
+
331
+ no_wait = (!wait64 && *(uint32 *)address != (uint32)expect)
332
+ || (wait64 && *(uint64 *)address != expect);
333
+
334
+ if (no_wait) {
335
+ os_mutex_unlock(lock);
336
+ return 1;
337
+ }
338
+
339
+ if (!(wait_node = wasm_runtime_malloc(sizeof(AtomicWaitNode)))) {
340
+ os_mutex_unlock(lock);
341
+ wasm_runtime_set_exception(module, "failed to create wait node");
342
+ return -1;
343
+ }
344
+ memset(wait_node, 0, sizeof(AtomicWaitNode));
345
+
346
+ if (0 != os_cond_init(&wait_node->wait_cond)) {
347
+ os_mutex_unlock(lock);
348
+ wasm_runtime_free(wait_node);
349
+ wasm_runtime_set_exception(module, "failed to init wait cond");
350
+ return -1;
351
+ }
352
+
353
+ wait_node->status = S_WAITING;
354
+
355
+ /* Acquire the wait info, create new one if not exists */
356
+ wait_info = acquire_wait_info(address, wait_node);
357
+
358
+ if (!wait_info) {
359
+ os_mutex_unlock(lock);
360
+ os_cond_destroy(&wait_node->wait_cond);
361
+ wasm_runtime_free(wait_node);
362
+ wasm_runtime_set_exception(module, "failed to acquire wait_info");
363
+ return -1;
364
+ }
365
+
366
+ /* unit of timeout is nsec, convert it to usec */
367
+ timeout_left = (uint64)timeout / 1000;
368
+ timeout_1sec = (uint64)1e6;
369
+
370
+ while (1) {
371
+ if (timeout < 0) {
372
+ /* wait forever until it is notified or terminated
373
+ here we keep waiting and checking every second */
374
+ os_cond_reltimedwait(&wait_node->wait_cond, lock,
375
+ (uint64)timeout_1sec);
376
+ if (wait_node->status == S_NOTIFIED /* notified by atomic.notify */
377
+ #if WASM_ENABLE_THREAD_MGR != 0
378
+ /* terminated by other thread */
379
+ || wasm_cluster_is_thread_terminated(exec_env)
380
+ #endif
381
+ ) {
382
+ break;
383
+ }
384
+ }
385
+ else {
386
+ timeout_wait =
387
+ timeout_left < timeout_1sec ? timeout_left : timeout_1sec;
388
+ os_cond_reltimedwait(&wait_node->wait_cond, lock, timeout_wait);
389
+ if (wait_node->status == S_NOTIFIED /* notified by atomic.notify */
390
+ || timeout_left <= timeout_wait /* time out */
391
+ #if WASM_ENABLE_THREAD_MGR != 0
392
+ /* terminated by other thread */
393
+ || wasm_cluster_is_thread_terminated(exec_env)
394
+ #endif
395
+ ) {
396
+ break;
397
+ }
398
+ timeout_left -= timeout_wait;
399
+ }
400
+ }
401
+
402
+ is_timeout = wait_node->status == S_WAITING ? true : false;
403
+
404
+ check_ret = is_wait_node_exists(wait_info->wait_list, wait_node);
405
+ bh_assert(check_ret);
406
+ (void)check_ret;
407
+
408
+ /* Remove wait node from wait list */
409
+ bh_list_remove(wait_info->wait_list, wait_node);
410
+ os_cond_destroy(&wait_node->wait_cond);
411
+ wasm_runtime_free(wait_node);
412
+
413
+ /* Release wait info if no wait nodes are attached */
414
+ map_try_release_wait_info(wait_map, wait_info, address);
415
+
416
+ os_mutex_unlock(lock);
417
+
418
+ return is_timeout ? 2 : 0;
419
+ }
420
+
421
+ uint32
422
+ wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
423
+ uint32 count)
424
+ {
425
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module;
426
+ uint32 notify_result;
427
+ AtomicWaitInfo *wait_info;
428
+ korp_mutex *lock;
429
+ bool out_of_bounds;
430
+
431
+ bh_assert(module->module_type == Wasm_Module_Bytecode
432
+ || module->module_type == Wasm_Module_AoT);
433
+
434
+ shared_memory_lock(module_inst->memories[0]);
435
+ out_of_bounds =
436
+ #if WASM_ENABLE_SHARED_HEAP != 0
437
+ /* not in shared heap */
438
+ !is_native_addr_in_shared_heap(module, address, 4) &&
439
+ #endif
440
+ /* and not in linear memory */
441
+ ((uint8 *)address < module_inst->memories[0]->memory_data
442
+ || (uint8 *)address + 4 > module_inst->memories[0]->memory_data_end);
443
+ shared_memory_unlock(module_inst->memories[0]);
444
+
445
+ if (out_of_bounds) {
446
+ wasm_runtime_set_exception(module, "out of bounds memory access");
447
+ return -1;
448
+ }
449
+
450
+ /* Currently we have only one memory instance */
451
+ if (!shared_memory_is_shared(module_inst->memories[0])) {
452
+ /* Always return 0 for ushared linear memory since there is
453
+ no way to create a waiter on it */
454
+ return 0;
455
+ }
456
+
457
+ lock = shared_memory_get_lock_pointer(module_inst->memories[0]);
458
+
459
+ /* Lock the shared_mem_lock for the whole atomic notify process,
460
+ and use it to os_cond_signal */
461
+ os_mutex_lock(lock);
462
+
463
+ wait_info = acquire_wait_info(address, NULL);
464
+
465
+ /* Nobody wait on this address */
466
+ if (!wait_info) {
467
+ os_mutex_unlock(lock);
468
+ return 0;
469
+ }
470
+
471
+ /* Notify each wait node in the wait list */
472
+ notify_result = notify_wait_list(wait_info->wait_list, count);
473
+
474
+ os_mutex_unlock(lock);
475
+
476
+ return notify_result;
477
+ }
@@ -0,0 +1,63 @@
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_SHARED_MEMORY_H
7
+ #define _WASM_SHARED_MEMORY_H
8
+
9
+ #include "bh_common.h"
10
+ #include "../interpreter/wasm_runtime.h"
11
+ #include "wasm_runtime_common.h"
12
+
13
+ #ifdef __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ extern korp_mutex g_shared_memory_lock;
18
+
19
+ bool
20
+ wasm_shared_memory_init(void);
21
+
22
+ void
23
+ wasm_shared_memory_destroy(void);
24
+
25
+ uint16
26
+ shared_memory_inc_reference(WASMMemoryInstance *memory);
27
+
28
+ uint16
29
+ shared_memory_dec_reference(WASMMemoryInstance *memory);
30
+
31
+ #define shared_memory_is_shared(memory) memory->is_shared_memory
32
+
33
+ #define shared_memory_lock(memory) \
34
+ do { \
35
+ /* \
36
+ * Note: exception logic is currently abusing this lock. \
37
+ * cf. \
38
+ * https://github.com/bytecodealliance/wasm-micro-runtime/issues/2407 \
39
+ */ \
40
+ bh_assert(memory != NULL); \
41
+ if (memory->is_shared_memory) \
42
+ os_mutex_lock(&g_shared_memory_lock); \
43
+ } while (0)
44
+
45
+ #define shared_memory_unlock(memory) \
46
+ do { \
47
+ if (memory->is_shared_memory) \
48
+ os_mutex_unlock(&g_shared_memory_lock); \
49
+ } while (0)
50
+
51
+ uint32
52
+ wasm_runtime_atomic_wait(WASMModuleInstanceCommon *module, void *address,
53
+ uint64 expect, int64 timeout, bool wait64);
54
+
55
+ uint32
56
+ wasm_runtime_atomic_notify(WASMModuleInstanceCommon *module, void *address,
57
+ uint32 count);
58
+
59
+ #ifdef __cplusplus
60
+ }
61
+ #endif
62
+
63
+ #endif /* end of _WASM_SHARED_MEMORY_H */
@@ -0,0 +1,52 @@
1
+ /*
2
+ * Copyright (C) 2023 Amazon Inc. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #ifndef _WASM_SUSPEND_FLAGS_H
7
+ #define _WASM_SUSPEND_FLAGS_H
8
+
9
+ #include "bh_atomic.h"
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ /* Need to terminate */
16
+ #define WASM_SUSPEND_FLAG_TERMINATE 0x1
17
+ /* Need to suspend */
18
+ #define WASM_SUSPEND_FLAG_SUSPEND 0x2
19
+ /* Need to go into breakpoint */
20
+ #define WASM_SUSPEND_FLAG_BREAKPOINT 0x4
21
+ /* Return from pthread_exit */
22
+ #define WASM_SUSPEND_FLAG_EXIT 0x8
23
+ /* The thread might be blocking */
24
+ #define WASM_SUSPEND_FLAG_BLOCKING 0x10
25
+
26
+ typedef union WASMSuspendFlags {
27
+ bh_atomic_32_t flags;
28
+ uintptr_t __padding__;
29
+ } WASMSuspendFlags;
30
+
31
+ #define WASM_SUSPEND_FLAGS_IS_ATOMIC BH_ATOMIC_32_IS_ATOMIC
32
+ #define WASM_SUSPEND_FLAGS_GET(s_flags) BH_ATOMIC_32_LOAD(s_flags.flags)
33
+ #define WASM_SUSPEND_FLAGS_FETCH_OR(s_flags, val) \
34
+ BH_ATOMIC_32_FETCH_OR(s_flags.flags, val)
35
+ #define WASM_SUSPEND_FLAGS_FETCH_AND(s_flags, val) \
36
+ BH_ATOMIC_32_FETCH_AND(s_flags.flags, val)
37
+
38
+ #define WASM_SUSPEND_FLAG_INHERIT_MASK (~WASM_SUSPEND_FLAG_BLOCKING)
39
+
40
+ #if WASM_SUSPEND_FLAGS_IS_ATOMIC != 0
41
+ #define WASM_SUSPEND_FLAGS_LOCK(lock) (void)0
42
+ #define WASM_SUSPEND_FLAGS_UNLOCK(lock) (void)0
43
+ #else /* else of WASM_SUSPEND_FLAGS_IS_ATOMIC */
44
+ #define WASM_SUSPEND_FLAGS_LOCK(lock) os_mutex_lock(&lock)
45
+ #define WASM_SUSPEND_FLAGS_UNLOCK(lock) os_mutex_unlock(&lock);
46
+ #endif /* WASM_SUSPEND_FLAGS_IS_ATOMIC */
47
+
48
+ #ifdef __cplusplus
49
+ }
50
+ #endif
51
+
52
+ #endif /* end of _WASM_SUSPEND_FLAGS_H */