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,784 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #ifndef _GNU_SOURCE
7
+ #if !defined(__RTTHREAD__)
8
+ #define _GNU_SOURCE
9
+ #endif
10
+ #endif
11
+ #include "platform_api_vmcore.h"
12
+ #include "platform_api_extension.h"
13
+
14
+ #if defined(__APPLE__) || defined(__MACH__)
15
+ #include <TargetConditionals.h>
16
+ #endif
17
+
18
+ typedef struct {
19
+ thread_start_routine_t start;
20
+ void *arg;
21
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
22
+ os_signal_handler signal_handler;
23
+ #endif
24
+ } thread_wrapper_arg;
25
+
26
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
27
+ /* The signal handler passed to os_thread_signal_init() */
28
+ static os_thread_local_attribute os_signal_handler signal_handler;
29
+ #endif
30
+
31
+ static void *
32
+ os_thread_wrapper(void *arg)
33
+ {
34
+ thread_wrapper_arg *targ = arg;
35
+ thread_start_routine_t start_func = targ->start;
36
+ void *thread_arg = targ->arg;
37
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
38
+ os_signal_handler handler = targ->signal_handler;
39
+ #endif
40
+
41
+ #if 0
42
+ os_printf("THREAD CREATED %jx\n", (uintmax_t)(uintptr_t)pthread_self());
43
+ #endif
44
+ BH_FREE(targ);
45
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
46
+ if (os_thread_signal_init(handler) != 0)
47
+ return NULL;
48
+ #endif
49
+ #ifdef OS_ENABLE_WAKEUP_BLOCKING_OP
50
+ os_end_blocking_op();
51
+ #endif
52
+ #if BH_DEBUG != 0
53
+ #if defined __APPLE__
54
+ pthread_setname_np("wamr");
55
+ #else
56
+ pthread_setname_np(pthread_self(), "wamr");
57
+ #endif
58
+ #endif
59
+ start_func(thread_arg);
60
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
61
+ os_thread_signal_destroy();
62
+ #endif
63
+ return NULL;
64
+ }
65
+
66
+ int
67
+ os_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
68
+ void *arg, unsigned int stack_size, int prio)
69
+ {
70
+ pthread_attr_t tattr;
71
+ thread_wrapper_arg *targ;
72
+
73
+ assert(stack_size > 0);
74
+ assert(tid);
75
+ assert(start);
76
+
77
+ pthread_attr_init(&tattr);
78
+ pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
79
+ if (pthread_attr_setstacksize(&tattr, stack_size) != 0) {
80
+ os_printf("Invalid thread stack size %u. "
81
+ "Min stack size on Linux = %u\n",
82
+ stack_size, (unsigned int)PTHREAD_STACK_MIN);
83
+ pthread_attr_destroy(&tattr);
84
+ return BHT_ERROR;
85
+ }
86
+
87
+ targ = (thread_wrapper_arg *)BH_MALLOC(sizeof(*targ));
88
+ if (!targ) {
89
+ pthread_attr_destroy(&tattr);
90
+ return BHT_ERROR;
91
+ }
92
+
93
+ targ->start = start;
94
+ targ->arg = arg;
95
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
96
+ targ->signal_handler = signal_handler;
97
+ #endif
98
+
99
+ if (pthread_create(tid, &tattr, os_thread_wrapper, targ) != 0) {
100
+ pthread_attr_destroy(&tattr);
101
+ BH_FREE(targ);
102
+ return BHT_ERROR;
103
+ }
104
+
105
+ pthread_attr_destroy(&tattr);
106
+ return BHT_OK;
107
+ }
108
+
109
+ int
110
+ os_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
111
+ unsigned int stack_size)
112
+ {
113
+ return os_thread_create_with_prio(tid, start, arg, stack_size,
114
+ BH_THREAD_DEFAULT_PRIORITY);
115
+ }
116
+
117
+ korp_tid
118
+ os_self_thread()
119
+ {
120
+ return (korp_tid)pthread_self();
121
+ }
122
+
123
+ int
124
+ os_mutex_init(korp_mutex *mutex)
125
+ {
126
+ return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
127
+ }
128
+
129
+ int
130
+ os_recursive_mutex_init(korp_mutex *mutex)
131
+ {
132
+ int ret;
133
+
134
+ pthread_mutexattr_t mattr;
135
+
136
+ assert(mutex);
137
+ ret = pthread_mutexattr_init(&mattr);
138
+ if (ret)
139
+ return BHT_ERROR;
140
+
141
+ pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE);
142
+ ret = pthread_mutex_init(mutex, &mattr);
143
+ pthread_mutexattr_destroy(&mattr);
144
+
145
+ return ret == 0 ? BHT_OK : BHT_ERROR;
146
+ }
147
+
148
+ int
149
+ os_mutex_destroy(korp_mutex *mutex)
150
+ {
151
+ int ret;
152
+
153
+ assert(mutex);
154
+ ret = pthread_mutex_destroy(mutex);
155
+
156
+ return ret == 0 ? BHT_OK : BHT_ERROR;
157
+ }
158
+
159
+ int
160
+ os_mutex_lock(korp_mutex *mutex)
161
+ {
162
+ int ret;
163
+
164
+ assert(mutex);
165
+ ret = pthread_mutex_lock(mutex);
166
+
167
+ return ret == 0 ? BHT_OK : BHT_ERROR;
168
+ }
169
+
170
+ int
171
+ os_mutex_unlock(korp_mutex *mutex)
172
+ {
173
+ int ret;
174
+
175
+ assert(mutex);
176
+ ret = pthread_mutex_unlock(mutex);
177
+
178
+ return ret == 0 ? BHT_OK : BHT_ERROR;
179
+ }
180
+
181
+ int
182
+ os_cond_init(korp_cond *cond)
183
+ {
184
+ assert(cond);
185
+
186
+ if (pthread_cond_init(cond, NULL) != BHT_OK)
187
+ return BHT_ERROR;
188
+
189
+ return BHT_OK;
190
+ }
191
+
192
+ int
193
+ os_cond_destroy(korp_cond *cond)
194
+ {
195
+ assert(cond);
196
+
197
+ if (pthread_cond_destroy(cond) != BHT_OK)
198
+ return BHT_ERROR;
199
+
200
+ return BHT_OK;
201
+ }
202
+
203
+ int
204
+ os_cond_wait(korp_cond *cond, korp_mutex *mutex)
205
+ {
206
+ assert(cond);
207
+ assert(mutex);
208
+
209
+ if (pthread_cond_wait(cond, mutex) != BHT_OK)
210
+ return BHT_ERROR;
211
+
212
+ return BHT_OK;
213
+ }
214
+
215
+ korp_sem *
216
+ os_sem_open(const char *name, int oflags, int mode, int val)
217
+ {
218
+ return sem_open(name, oflags, mode, val);
219
+ }
220
+
221
+ int
222
+ os_sem_close(korp_sem *sem)
223
+ {
224
+ return sem_close(sem);
225
+ }
226
+
227
+ int
228
+ os_sem_wait(korp_sem *sem)
229
+ {
230
+ return sem_wait(sem);
231
+ }
232
+
233
+ int
234
+ os_sem_trywait(korp_sem *sem)
235
+ {
236
+ return sem_trywait(sem);
237
+ }
238
+
239
+ int
240
+ os_sem_post(korp_sem *sem)
241
+ {
242
+ return sem_post(sem);
243
+ }
244
+
245
+ int
246
+ os_sem_getvalue(korp_sem *sem, int *sval)
247
+ {
248
+ #if defined(__APPLE__)
249
+ /*
250
+ * macOS doesn't have working sem_getvalue.
251
+ * It's marked as deprecated in the system header.
252
+ * Mock it up here to avoid compile-time deprecation warnings.
253
+ */
254
+ errno = ENOSYS;
255
+ return -1;
256
+ #else
257
+ return sem_getvalue(sem, sval);
258
+ #endif
259
+ }
260
+
261
+ int
262
+ os_sem_unlink(const char *name)
263
+ {
264
+ return sem_unlink(name);
265
+ }
266
+
267
+ static void
268
+ msec_nsec_to_abstime(struct timespec *ts, uint64 usec)
269
+ {
270
+ struct timeval tv;
271
+ time_t tv_sec_new;
272
+ long int tv_nsec_new;
273
+
274
+ gettimeofday(&tv, NULL);
275
+
276
+ tv_sec_new = (time_t)(tv.tv_sec + usec / 1000000);
277
+ if (tv_sec_new >= tv.tv_sec) {
278
+ ts->tv_sec = tv_sec_new;
279
+ }
280
+ else {
281
+ /* integer overflow */
282
+ ts->tv_sec = BH_TIME_T_MAX;
283
+ os_printf("Warning: os_cond_reltimedwait exceeds limit, "
284
+ "set to max timeout instead\n");
285
+ }
286
+
287
+ tv_nsec_new = (long int)(tv.tv_usec * 1000 + (usec % 1000000) * 1000);
288
+ if (tv.tv_usec * 1000 >= tv.tv_usec && tv_nsec_new >= tv.tv_usec * 1000) {
289
+ ts->tv_nsec = tv_nsec_new;
290
+ }
291
+ else {
292
+ /* integer overflow */
293
+ ts->tv_nsec = LONG_MAX;
294
+ os_printf("Warning: os_cond_reltimedwait exceeds limit, "
295
+ "set to max timeout instead\n");
296
+ }
297
+
298
+ if (ts->tv_nsec >= 1000000000L && ts->tv_sec < BH_TIME_T_MAX) {
299
+ ts->tv_sec++;
300
+ ts->tv_nsec -= 1000000000L;
301
+ }
302
+ }
303
+
304
+ int
305
+ os_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, uint64 useconds)
306
+ {
307
+ int ret;
308
+ struct timespec abstime;
309
+
310
+ if (useconds == BHT_WAIT_FOREVER)
311
+ ret = pthread_cond_wait(cond, mutex);
312
+ else {
313
+ msec_nsec_to_abstime(&abstime, useconds);
314
+ ret = pthread_cond_timedwait(cond, mutex, &abstime);
315
+ }
316
+
317
+ if (ret != BHT_OK && ret != ETIMEDOUT)
318
+ return BHT_ERROR;
319
+
320
+ return ret;
321
+ }
322
+
323
+ int
324
+ os_cond_signal(korp_cond *cond)
325
+ {
326
+ assert(cond);
327
+
328
+ if (pthread_cond_signal(cond) != BHT_OK)
329
+ return BHT_ERROR;
330
+
331
+ return BHT_OK;
332
+ }
333
+
334
+ int
335
+ os_cond_broadcast(korp_cond *cond)
336
+ {
337
+ assert(cond);
338
+
339
+ if (pthread_cond_broadcast(cond) != BHT_OK)
340
+ return BHT_ERROR;
341
+
342
+ return BHT_OK;
343
+ }
344
+
345
+ int
346
+ os_rwlock_init(korp_rwlock *lock)
347
+ {
348
+ assert(lock);
349
+
350
+ if (pthread_rwlock_init(lock, NULL) != BHT_OK)
351
+ return BHT_ERROR;
352
+
353
+ return BHT_OK;
354
+ }
355
+
356
+ int
357
+ os_rwlock_rdlock(korp_rwlock *lock)
358
+ {
359
+ assert(lock);
360
+
361
+ if (pthread_rwlock_rdlock(lock) != BHT_OK)
362
+ return BHT_ERROR;
363
+
364
+ return BHT_OK;
365
+ }
366
+
367
+ int
368
+ os_rwlock_wrlock(korp_rwlock *lock)
369
+ {
370
+ assert(lock);
371
+
372
+ if (pthread_rwlock_wrlock(lock) != BHT_OK)
373
+ return BHT_ERROR;
374
+
375
+ return BHT_OK;
376
+ }
377
+
378
+ int
379
+ os_rwlock_unlock(korp_rwlock *lock)
380
+ {
381
+ assert(lock);
382
+
383
+ if (pthread_rwlock_unlock(lock) != BHT_OK)
384
+ return BHT_ERROR;
385
+
386
+ return BHT_OK;
387
+ }
388
+
389
+ int
390
+ os_rwlock_destroy(korp_rwlock *lock)
391
+ {
392
+ assert(lock);
393
+
394
+ if (pthread_rwlock_destroy(lock) != BHT_OK)
395
+ return BHT_ERROR;
396
+
397
+ return BHT_OK;
398
+ }
399
+
400
+ int
401
+ os_thread_join(korp_tid thread, void **value_ptr)
402
+ {
403
+ return pthread_join(thread, value_ptr);
404
+ }
405
+
406
+ int
407
+ os_thread_detach(korp_tid thread)
408
+ {
409
+ return pthread_detach(thread);
410
+ }
411
+
412
+ void
413
+ os_thread_exit(void *retval)
414
+ {
415
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
416
+ os_thread_signal_destroy();
417
+ #endif
418
+ return pthread_exit(retval);
419
+ }
420
+
421
+ #if defined(os_thread_local_attribute)
422
+ static os_thread_local_attribute uint8 *thread_stack_boundary = NULL;
423
+ #endif
424
+
425
+ uint8 *
426
+ os_thread_get_stack_boundary()
427
+ {
428
+ pthread_t self;
429
+ #ifdef __linux__
430
+ pthread_attr_t attr;
431
+ size_t guard_size;
432
+ #endif
433
+ uint8 *addr = NULL;
434
+ size_t stack_size, max_stack_size;
435
+ int page_size;
436
+
437
+ #if defined(os_thread_local_attribute)
438
+ if (thread_stack_boundary)
439
+ return thread_stack_boundary;
440
+ #endif
441
+
442
+ page_size = getpagesize();
443
+ self = pthread_self();
444
+ max_stack_size =
445
+ (size_t)(APP_THREAD_STACK_SIZE_MAX + page_size - 1) & ~(page_size - 1);
446
+
447
+ if (max_stack_size < APP_THREAD_STACK_SIZE_DEFAULT)
448
+ max_stack_size = APP_THREAD_STACK_SIZE_DEFAULT;
449
+
450
+ #ifdef __linux__
451
+ if (pthread_getattr_np(self, &attr) == 0) {
452
+ pthread_attr_getstack(&attr, (void **)&addr, &stack_size);
453
+ pthread_attr_getguardsize(&attr, &guard_size);
454
+ pthread_attr_destroy(&attr);
455
+ if (stack_size > max_stack_size)
456
+ addr = addr + stack_size - max_stack_size;
457
+ addr += guard_size;
458
+ }
459
+ (void)stack_size;
460
+ #elif defined(__APPLE__) || defined(__NuttX__) || defined(__RTTHREAD__)
461
+ if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) {
462
+ stack_size = pthread_get_stacksize_np(self);
463
+
464
+ /**
465
+ * Check whether stack_addr is the base or end of the stack,
466
+ * change it to the base if it is the end of stack.
467
+ */
468
+ if (addr <= (uint8 *)&stack_size)
469
+ addr = addr + stack_size;
470
+
471
+ if (stack_size > max_stack_size)
472
+ stack_size = max_stack_size;
473
+
474
+ addr -= stack_size;
475
+ }
476
+ #endif
477
+
478
+ #if defined(os_thread_local_attribute)
479
+ thread_stack_boundary = addr;
480
+ #endif
481
+ return addr;
482
+ }
483
+
484
+ void
485
+ os_thread_jit_write_protect_np(bool enabled)
486
+ {
487
+ #if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) \
488
+ && defined(TARGET_OS_OSX) && TARGET_OS_OSX != 0
489
+ pthread_jit_write_protect_np(enabled);
490
+ #endif
491
+ }
492
+
493
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
494
+
495
+ #define SIG_ALT_STACK_SIZE (32 * 1024)
496
+
497
+ /**
498
+ * Whether thread signal environment is initialized:
499
+ * the signal handler is registered, the stack pages are touched,
500
+ * the stack guard pages are set and signal alternate stack are set.
501
+ */
502
+ static os_thread_local_attribute bool thread_signal_inited = false;
503
+
504
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
505
+ /* The signal alternate stack base addr */
506
+ static os_thread_local_attribute uint8 *sigalt_stack_base_addr;
507
+ /* The previous signal alternate stack */
508
+ static os_thread_local_attribute stack_t prev_sigalt_stack;
509
+
510
+ /*
511
+ * ASAN is not designed to work with custom stack unwind or other low-level
512
+ * things. Ignore a function that does some low-level magic. (e.g. walking
513
+ * through the thread's stack bypassing the frame boundaries)
514
+ */
515
+ #if defined(__clang__)
516
+ #pragma clang optimize off
517
+ __attribute__((no_sanitize_address))
518
+ #elif defined(__GNUC__)
519
+ #pragma GCC push_options
520
+ #pragma GCC optimize("O0")
521
+ __attribute__((no_sanitize_address))
522
+ #endif
523
+ static uint32
524
+ touch_pages(uint8 *stack_min_addr, uint32 page_size)
525
+ {
526
+ uint8 sum = 0;
527
+ while (1) {
528
+ volatile uint8 *touch_addr = (volatile uint8 *)os_alloca(page_size / 2);
529
+ if (touch_addr < stack_min_addr + page_size) {
530
+ sum += *(stack_min_addr + page_size - 1);
531
+ break;
532
+ }
533
+ *touch_addr = 0;
534
+ sum += *touch_addr;
535
+ }
536
+ return sum;
537
+ }
538
+ #if defined(__clang__)
539
+ #pragma clang optimize on
540
+ #elif defined(__GNUC__)
541
+ #pragma GCC pop_options
542
+ #endif
543
+
544
+ static bool
545
+ init_stack_guard_pages()
546
+ {
547
+ uint32 page_size = os_getpagesize();
548
+ uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
549
+ uint8 *stack_min_addr = os_thread_get_stack_boundary();
550
+
551
+ if (stack_min_addr == NULL)
552
+ return false;
553
+
554
+ /* Touch each stack page to ensure that it has been mapped: the OS
555
+ may lazily grow the stack mapping as a guard page is hit. */
556
+ (void)touch_pages(stack_min_addr, page_size);
557
+ /* First time to call aot function, protect guard pages */
558
+ if (os_mprotect(stack_min_addr, page_size * guard_page_count,
559
+ MMAP_PROT_NONE)
560
+ != 0) {
561
+ return false;
562
+ }
563
+ return true;
564
+ }
565
+
566
+ static void
567
+ destroy_stack_guard_pages()
568
+ {
569
+ uint32 page_size = os_getpagesize();
570
+ uint32 guard_page_count = STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT;
571
+ uint8 *stack_min_addr = os_thread_get_stack_boundary();
572
+
573
+ os_mprotect(stack_min_addr, page_size * guard_page_count,
574
+ MMAP_PROT_READ | MMAP_PROT_WRITE);
575
+ }
576
+ #endif /* end of WASM_DISABLE_STACK_HW_BOUND_CHECK == 0 */
577
+
578
+ /*
579
+ * ASAN is not designed to work with custom stack unwind or other low-level
580
+ * things. Ignore a function that does some low-level magic. (e.g. walking
581
+ * through the thread's stack bypassing the frame boundaries)
582
+ */
583
+ #if defined(__GNUC__) || defined(__clang__)
584
+ __attribute__((no_sanitize_address))
585
+ #endif
586
+ static void
587
+ mask_signals(int how)
588
+ {
589
+ sigset_t set;
590
+
591
+ sigemptyset(&set);
592
+ sigaddset(&set, SIGSEGV);
593
+ sigaddset(&set, SIGBUS);
594
+ pthread_sigmask(how, &set, NULL);
595
+ }
596
+
597
+ static struct sigaction prev_sig_act_SIGSEGV;
598
+ static struct sigaction prev_sig_act_SIGBUS;
599
+
600
+ /*
601
+ * ASAN is not designed to work with custom stack unwind or other low-level
602
+ * things. Ignore a function that does some low-level magic. (e.g. walking
603
+ * through the thread's stack bypassing the frame boundaries)
604
+ */
605
+ #if defined(__GNUC__) || defined(__clang__)
606
+ __attribute__((no_sanitize_address))
607
+ #endif
608
+ static void
609
+ signal_callback(int sig_num, siginfo_t *sig_info, void *sig_ucontext)
610
+ {
611
+ void *sig_addr = sig_info->si_addr;
612
+ struct sigaction *prev_sig_act = NULL;
613
+
614
+ mask_signals(SIG_BLOCK);
615
+
616
+ /* Try to handle signal with the registered signal handler */
617
+ if (signal_handler && (sig_num == SIGSEGV || sig_num == SIGBUS)) {
618
+ signal_handler(sig_addr);
619
+ }
620
+
621
+ if (sig_num == SIGSEGV)
622
+ prev_sig_act = &prev_sig_act_SIGSEGV;
623
+ else if (sig_num == SIGBUS)
624
+ prev_sig_act = &prev_sig_act_SIGBUS;
625
+
626
+ /* Forward the signal to next handler if found */
627
+ if (prev_sig_act && (prev_sig_act->sa_flags & SA_SIGINFO)) {
628
+ prev_sig_act->sa_sigaction(sig_num, sig_info, sig_ucontext);
629
+ }
630
+ else if (prev_sig_act
631
+ && prev_sig_act->sa_handler
632
+ /* Filter out SIG_DFL and SIG_IGN here, they will
633
+ run into the else branch below */
634
+ && (void *)prev_sig_act->sa_handler != SIG_DFL
635
+ && (void *)prev_sig_act->sa_handler != SIG_IGN) {
636
+ prev_sig_act->sa_handler(sig_num);
637
+ }
638
+ /* Output signal info and then crash if signal is unhandled */
639
+ else {
640
+ switch (sig_num) {
641
+ case SIGSEGV:
642
+ os_printf("unhandled SIGSEGV, si_addr: %p\n", sig_addr);
643
+ break;
644
+ case SIGBUS:
645
+ os_printf("unhandled SIGBUS, si_addr: %p\n", sig_addr);
646
+ break;
647
+ default:
648
+ os_printf("unhandle signal %d, si_addr: %p\n", sig_num,
649
+ sig_addr);
650
+ break;
651
+ }
652
+
653
+ abort();
654
+ }
655
+ }
656
+
657
+ int
658
+ os_thread_signal_init(os_signal_handler handler)
659
+ {
660
+ struct sigaction sig_act;
661
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
662
+ stack_t sigalt_stack_info;
663
+ uint32 map_size = SIG_ALT_STACK_SIZE;
664
+ uint8 *map_addr;
665
+ #endif
666
+
667
+ if (thread_signal_inited)
668
+ return 0;
669
+
670
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
671
+ if (!init_stack_guard_pages()) {
672
+ os_printf("Failed to init stack guard pages\n");
673
+ return -1;
674
+ }
675
+
676
+ /* Initialize memory for signal alternate stack of current thread */
677
+ if (!(map_addr = os_mmap(NULL, map_size, MMAP_PROT_READ | MMAP_PROT_WRITE,
678
+ MMAP_MAP_NONE, os_get_invalid_handle()))) {
679
+ os_printf("Failed to mmap memory for alternate stack\n");
680
+ goto fail1;
681
+ }
682
+
683
+ /* Initialize signal alternate stack */
684
+ memset(map_addr, 0, map_size);
685
+ sigalt_stack_info.ss_sp = map_addr;
686
+ sigalt_stack_info.ss_size = map_size;
687
+ sigalt_stack_info.ss_flags = 0;
688
+ memset(&prev_sigalt_stack, 0, sizeof(stack_t));
689
+ /* Set signal alternate stack and save the previous one */
690
+ if (sigaltstack(&sigalt_stack_info, &prev_sigalt_stack) != 0) {
691
+ os_printf("Failed to init signal alternate stack\n");
692
+ goto fail2;
693
+ }
694
+ #endif
695
+
696
+ memset(&prev_sig_act_SIGSEGV, 0, sizeof(struct sigaction));
697
+ memset(&prev_sig_act_SIGBUS, 0, sizeof(struct sigaction));
698
+
699
+ /* Install signal handler */
700
+ sig_act.sa_sigaction = signal_callback;
701
+ sig_act.sa_flags = SA_SIGINFO | SA_NODEFER;
702
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
703
+ sig_act.sa_flags |= SA_ONSTACK;
704
+ #endif
705
+ sigemptyset(&sig_act.sa_mask);
706
+ if (sigaction(SIGSEGV, &sig_act, &prev_sig_act_SIGSEGV) != 0
707
+ || sigaction(SIGBUS, &sig_act, &prev_sig_act_SIGBUS) != 0) {
708
+ os_printf("Failed to register signal handler\n");
709
+ goto fail3;
710
+ }
711
+
712
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
713
+ sigalt_stack_base_addr = map_addr;
714
+ #endif
715
+
716
+ #if defined(os_thread_local_attribute)
717
+ // calculate and cache the new stack boundary.
718
+ // see https://github.com/bytecodealliance/wasm-micro-runtime/issues/3966
719
+ (void)os_thread_get_stack_boundary();
720
+ #endif
721
+
722
+ signal_handler = handler;
723
+ thread_signal_inited = true;
724
+ return 0;
725
+
726
+ fail3:
727
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
728
+ memset(&sigalt_stack_info, 0, sizeof(stack_t));
729
+ sigalt_stack_info.ss_flags = SS_DISABLE;
730
+ sigalt_stack_info.ss_size = map_size;
731
+ sigaltstack(&sigalt_stack_info, NULL);
732
+ fail2:
733
+ os_munmap(map_addr, map_size);
734
+ fail1:
735
+ destroy_stack_guard_pages();
736
+ #endif
737
+ return -1;
738
+ }
739
+
740
+ void
741
+ os_thread_signal_destroy()
742
+ {
743
+ if (!thread_signal_inited)
744
+ return;
745
+
746
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
747
+ /* Restore the previous signal alternate stack */
748
+ sigaltstack(&prev_sigalt_stack, NULL);
749
+
750
+ os_munmap(sigalt_stack_base_addr, SIG_ALT_STACK_SIZE);
751
+
752
+ destroy_stack_guard_pages();
753
+ #endif
754
+
755
+ thread_signal_inited = false;
756
+ }
757
+
758
+ bool
759
+ os_thread_signal_inited()
760
+ {
761
+ return thread_signal_inited;
762
+ }
763
+
764
+ void
765
+ os_signal_unmask()
766
+ {
767
+ mask_signals(SIG_UNBLOCK);
768
+ }
769
+
770
+ void
771
+ os_sigreturn()
772
+ {
773
+ #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0
774
+ #if defined(__APPLE__)
775
+ #define UC_RESET_ALT_STACK 0x80000000
776
+ extern int __sigreturn(void *, int);
777
+
778
+ /* It's necessary to call __sigreturn to restore the sigaltstack state
779
+ after exiting the signal handler. */
780
+ __sigreturn(NULL, UC_RESET_ALT_STACK);
781
+ #endif
782
+ #endif
783
+ }
784
+ #endif /* end of OS_ENABLE_HW_BOUND_CHECK */