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,302 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "platform_api_vmcore.h"
7
+
8
+ #if defined(__APPLE__) || defined(__MACH__)
9
+ #include <libkern/OSCacheControl.h>
10
+ #include <TargetConditionals.h>
11
+ #endif
12
+
13
+ #ifndef BH_ENABLE_TRACE_MMAP
14
+ #define BH_ENABLE_TRACE_MMAP 0
15
+ #endif
16
+
17
+ #if BH_ENABLE_TRACE_MMAP != 0
18
+ static size_t total_size_mmapped = 0;
19
+ static size_t total_size_munmapped = 0;
20
+ #endif
21
+
22
+ #define HUGE_PAGE_SIZE (2 * 1024 * 1024)
23
+
24
+ #if !defined(__APPLE__) && !defined(__NuttX__) && defined(MADV_HUGEPAGE)
25
+ static inline uintptr_t
26
+ round_up(uintptr_t v, uintptr_t b)
27
+ {
28
+ uintptr_t m = b - 1;
29
+ return (v + m) & ~m;
30
+ }
31
+
32
+ static inline uintptr_t
33
+ round_down(uintptr_t v, uintptr_t b)
34
+ {
35
+ uintptr_t m = b - 1;
36
+ return v & ~m;
37
+ }
38
+ #endif
39
+
40
+ void *
41
+ os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
42
+ {
43
+ int map_prot = PROT_NONE;
44
+ #if (defined(__APPLE__) || defined(__MACH__)) && defined(__arm64__) \
45
+ && defined(TARGET_OS_OSX) && TARGET_OS_OSX != 0
46
+ int map_flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT;
47
+ #else
48
+ int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
49
+ #endif
50
+ uint64 request_size, page_size;
51
+ uint8 *addr = MAP_FAILED;
52
+ uint32 i;
53
+
54
+ page_size = (uint64)getpagesize();
55
+ request_size = (size + page_size - 1) & ~(page_size - 1);
56
+
57
+ #if !defined(__APPLE__) && !defined(__NuttX__) && defined(MADV_HUGEPAGE)
58
+ /* huge page isn't supported on MacOS and NuttX */
59
+ if (request_size >= HUGE_PAGE_SIZE)
60
+ /* apply one extra huge page */
61
+ request_size += HUGE_PAGE_SIZE;
62
+ #endif
63
+
64
+ if ((size_t)request_size < size) {
65
+ os_printf("mmap failed: request size overflow due to paging\n");
66
+ return NULL;
67
+ }
68
+
69
+ #if WASM_ENABLE_MEMORY64 == 0
70
+ if (request_size > 16 * (uint64)UINT32_MAX) {
71
+ os_printf("mmap failed: for memory64 at most 64G is allowed\n");
72
+ return NULL;
73
+ }
74
+ #endif
75
+
76
+ if (prot & MMAP_PROT_READ)
77
+ map_prot |= PROT_READ;
78
+
79
+ if (prot & MMAP_PROT_WRITE)
80
+ map_prot |= PROT_WRITE;
81
+
82
+ if (prot & MMAP_PROT_EXEC)
83
+ map_prot |= PROT_EXEC;
84
+
85
+ #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64)
86
+ #ifndef __APPLE__
87
+ if (flags & MMAP_MAP_32BIT)
88
+ map_flags |= MAP_32BIT;
89
+ #endif
90
+ #endif
91
+
92
+ if (flags & MMAP_MAP_FIXED)
93
+ map_flags |= MAP_FIXED;
94
+
95
+ #if defined(BUILD_TARGET_RISCV64_LP64D) || defined(BUILD_TARGET_RISCV64_LP64)
96
+ /* As AOT relocation in RISCV64 may require that the code/data mapped
97
+ * is in range 0 to 2GB, we try to map the memory with hint address
98
+ * (mmap's first argument) to meet the requirement.
99
+ */
100
+ if (!hint && !(flags & MMAP_MAP_FIXED) && (flags & MMAP_MAP_32BIT)) {
101
+ uint8 *stack_addr = (uint8 *)&map_prot;
102
+ uint8 *text_addr = (uint8 *)os_mmap;
103
+ /* hint address begins with 1MB */
104
+ static uint8 *hint_addr = (uint8 *)(uintptr_t)BH_MB;
105
+
106
+ if ((hint_addr - text_addr >= 0 && hint_addr - text_addr < 100 * BH_MB)
107
+ || (text_addr - hint_addr >= 0
108
+ && text_addr - hint_addr < 100 * BH_MB)) {
109
+ /* hint address is possibly in text section, skip it */
110
+ hint_addr += 100 * BH_MB;
111
+ }
112
+
113
+ if ((hint_addr - stack_addr >= 0 && hint_addr - stack_addr < 8 * BH_MB)
114
+ || (stack_addr - hint_addr >= 0
115
+ && stack_addr - hint_addr < 8 * BH_MB)) {
116
+ /* hint address is possibly in native stack area, skip it */
117
+ hint_addr += 8 * BH_MB;
118
+ }
119
+
120
+ /* try 10 times, step with 1MB each time */
121
+ for (i = 0; i < 10 && hint_addr < (uint8 *)(uintptr_t)(2ULL * BH_GB);
122
+ i++) {
123
+ addr = mmap(hint_addr, request_size, map_prot, map_flags, file, 0);
124
+ if (addr != MAP_FAILED) {
125
+ if (addr > (uint8 *)(uintptr_t)(2ULL * BH_GB)) {
126
+ /* unmap and try again if the mapped address doesn't
127
+ * meet the requirement */
128
+ os_munmap(addr, request_size);
129
+ }
130
+ else {
131
+ /* success, reset next hint address */
132
+ hint_addr += request_size;
133
+ break;
134
+ }
135
+ }
136
+ hint_addr += BH_MB;
137
+ }
138
+ }
139
+ #endif /* end of BUILD_TARGET_RISCV64_LP64D || BUILD_TARGET_RISCV64_LP64 */
140
+
141
+ /* memory hasn't been mapped or was mapped failed previously */
142
+ if (addr == MAP_FAILED) {
143
+ /* try 5 times on EAGAIN or ENOMEM, and keep retrying on EINTR */
144
+ i = 0;
145
+ while (i < 5) {
146
+ addr = mmap(hint, request_size, map_prot, map_flags, file, 0);
147
+ if (addr != MAP_FAILED)
148
+ break;
149
+ if (errno == EINTR)
150
+ continue;
151
+ if (errno != EAGAIN && errno != ENOMEM) {
152
+ break;
153
+ }
154
+ i++;
155
+ }
156
+ }
157
+
158
+ if (addr == MAP_FAILED) {
159
+ os_printf("mmap failed with errno: %d, hint: %p, size: %" PRIu64
160
+ ", prot: %d, flags: %d\n",
161
+ errno, hint, request_size, map_prot, map_flags);
162
+ return NULL;
163
+ }
164
+
165
+ #if BH_ENABLE_TRACE_MMAP != 0
166
+ total_size_mmapped += request_size;
167
+ os_printf("mmap return: %p with size: %zu, total_size_mmapped: %zu, "
168
+ "total_size_munmapped: %zu\n",
169
+ addr, request_size, total_size_mmapped, total_size_munmapped);
170
+ #endif
171
+
172
+ #if !defined(__APPLE__) && !defined(__NuttX__) && defined(MADV_HUGEPAGE)
173
+ /* huge page isn't supported on MacOS and NuttX */
174
+ if (request_size > HUGE_PAGE_SIZE) {
175
+ uintptr_t huge_start, huge_end;
176
+ size_t prefix_size = 0, suffix_size = HUGE_PAGE_SIZE;
177
+
178
+ huge_start = round_up((uintptr_t)addr, HUGE_PAGE_SIZE);
179
+
180
+ if (huge_start > (uintptr_t)addr) {
181
+ prefix_size += huge_start - (uintptr_t)addr;
182
+ suffix_size -= huge_start - (uintptr_t)addr;
183
+ }
184
+
185
+ /* unmap one extra huge page */
186
+
187
+ if (prefix_size > 0) {
188
+ munmap(addr, prefix_size);
189
+ #if BH_ENABLE_TRACE_MMAP != 0
190
+ total_size_munmapped += prefix_size;
191
+ os_printf("munmap %p with size: %zu, total_size_mmapped: %zu, "
192
+ "total_size_munmapped: %zu\n",
193
+ addr, prefix_size, total_size_mmapped,
194
+ total_size_munmapped);
195
+ #endif
196
+ }
197
+ if (suffix_size > 0) {
198
+ munmap(addr + request_size - suffix_size, suffix_size);
199
+ #if BH_ENABLE_TRACE_MMAP != 0
200
+ total_size_munmapped += suffix_size;
201
+ os_printf("munmap %p with size: %zu, total_size_mmapped: %zu, "
202
+ "total_size_munmapped: %zu\n",
203
+ addr + request_size - suffix_size, suffix_size,
204
+ total_size_mmapped, total_size_munmapped);
205
+ #endif
206
+ }
207
+
208
+ addr = (uint8 *)huge_start;
209
+ request_size -= HUGE_PAGE_SIZE;
210
+
211
+ huge_end = round_down(huge_start + request_size, HUGE_PAGE_SIZE);
212
+ if (huge_end > huge_start) {
213
+ int ret = madvise((void *)huge_start, huge_end - huge_start,
214
+ MADV_HUGEPAGE);
215
+ if (ret) {
216
+ #if BH_ENABLE_TRACE_MMAP != 0
217
+ os_printf(
218
+ "warning: madvise(%p, %lu) huge page failed, return %d\n",
219
+ (void *)huge_start, huge_end - huge_start, ret);
220
+ #endif
221
+ }
222
+ }
223
+ }
224
+ #endif /* end of __APPLE__ || __NuttX__ || !MADV_HUGEPAGE */
225
+
226
+ return addr;
227
+ }
228
+
229
+ void
230
+ os_munmap(void *addr, size_t size)
231
+ {
232
+ uint64 page_size = (uint64)getpagesize();
233
+ uint64 request_size = (size + page_size - 1) & ~(page_size - 1);
234
+
235
+ if (addr) {
236
+ if (munmap(addr, request_size)) {
237
+ os_printf("os_munmap error addr:%p, size:0x%" PRIx64 ", errno:%d\n",
238
+ addr, request_size, errno);
239
+ return;
240
+ }
241
+ #if BH_ENABLE_TRACE_MMAP != 0
242
+ total_size_munmapped += request_size;
243
+ os_printf("munmap %p with size: %zu, total_size_mmapped: %zu, "
244
+ "total_size_munmapped: %zu\n",
245
+ addr, request_size, total_size_mmapped, total_size_munmapped);
246
+ #endif
247
+ }
248
+ }
249
+
250
+ #if WASM_HAVE_MREMAP != 0
251
+ void *
252
+ os_mremap(void *old_addr, size_t old_size, size_t new_size)
253
+ {
254
+ void *ptr = mremap(old_addr, old_size, new_size, MREMAP_MAYMOVE);
255
+
256
+ if (ptr == MAP_FAILED) {
257
+ #if BH_ENABLE_TRACE_MMAP != 0
258
+ os_printf("mremap failed: %d\n", errno);
259
+ #endif
260
+ return os_mremap_slow(old_addr, old_size, new_size);
261
+ }
262
+
263
+ return ptr;
264
+ }
265
+ #endif
266
+
267
+ int
268
+ os_mprotect(void *addr, size_t size, int prot)
269
+ {
270
+ int map_prot = PROT_NONE;
271
+ uint64 page_size = (uint64)getpagesize();
272
+ uint64 request_size = (size + page_size - 1) & ~(page_size - 1);
273
+
274
+ if (!addr)
275
+ return 0;
276
+
277
+ if (prot & MMAP_PROT_READ)
278
+ map_prot |= PROT_READ;
279
+
280
+ if (prot & MMAP_PROT_WRITE)
281
+ map_prot |= PROT_WRITE;
282
+
283
+ if (prot & MMAP_PROT_EXEC)
284
+ map_prot |= PROT_EXEC;
285
+
286
+ return mprotect(addr, request_size, map_prot);
287
+ }
288
+
289
+ void
290
+ os_dcache_flush(void)
291
+ {}
292
+
293
+ void
294
+ os_icache_flush(void *start, size_t len)
295
+ {
296
+ #if defined(__APPLE__) || defined(__MACH__)
297
+ sys_icache_invalidate(start, len);
298
+ #else
299
+ (void)start;
300
+ (void)len;
301
+ #endif
302
+ }
@@ -0,0 +1,20 @@
1
+ /*
2
+ * Copyright (C) 2023 Midokura Japan KK. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include <time.h>
7
+
8
+ #include "platform_api_extension.h"
9
+
10
+ int
11
+ os_usleep(uint32 usec)
12
+ {
13
+ struct timespec ts;
14
+ int ret;
15
+
16
+ ts.tv_sec = usec / 1000000;
17
+ ts.tv_nsec = (usec % 1000000) * 1000;
18
+ ret = nanosleep(&ts, NULL);
19
+ return ret == 0 ? 0 : -1;
20
+ }