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,1999 @@
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_runtime_common.h"
7
+ #include "../interpreter/wasm_runtime.h"
8
+ #include "../aot/aot_runtime.h"
9
+ #include "mem_alloc.h"
10
+ #include "wasm_memory.h"
11
+
12
+ #if WASM_ENABLE_SHARED_MEMORY != 0
13
+ #include "../common/wasm_shared_memory.h"
14
+ #endif
15
+
16
+ #if WASM_ENABLE_THREAD_MGR != 0
17
+ #include "../libraries/thread-mgr/thread_manager.h"
18
+ #endif
19
+
20
+ typedef enum Memory_Mode {
21
+ MEMORY_MODE_UNKNOWN = 0,
22
+ MEMORY_MODE_POOL,
23
+ MEMORY_MODE_ALLOCATOR,
24
+ MEMORY_MODE_SYSTEM_ALLOCATOR
25
+ } Memory_Mode;
26
+
27
+ static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
28
+
29
+ static mem_allocator_t pool_allocator = NULL;
30
+
31
+ #if WASM_ENABLE_SHARED_HEAP != 0
32
+ static WASMSharedHeap *shared_heap_list = NULL;
33
+ static korp_mutex shared_heap_list_lock;
34
+ #endif
35
+
36
+ static enlarge_memory_error_callback_t enlarge_memory_error_cb;
37
+ static void *enlarge_memory_error_user_data;
38
+
39
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
40
+ static void *allocator_user_data = NULL;
41
+ #endif
42
+
43
+ static void *(*malloc_func)(
44
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
45
+ mem_alloc_usage_t usage,
46
+ #endif
47
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
48
+ void *user_data,
49
+ #endif
50
+ unsigned int size) = NULL;
51
+
52
+ static void *(*realloc_func)(
53
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
54
+ mem_alloc_usage_t usage, bool full_size_mmaped,
55
+ #endif
56
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
57
+ void *user_data,
58
+ #endif
59
+ void *ptr, unsigned int size) = NULL;
60
+
61
+ static void (*free_func)(
62
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
63
+ mem_alloc_usage_t usage,
64
+ #endif
65
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
66
+ void *user_data,
67
+ #endif
68
+ void *ptr) = NULL;
69
+
70
+ static unsigned int global_pool_size;
71
+
72
+ static uint64
73
+ align_as_and_cast(uint64 size, uint64 alignment)
74
+ {
75
+ uint64 aligned_size = (size + alignment - 1) & ~(alignment - 1);
76
+
77
+ return aligned_size;
78
+ }
79
+
80
+ static bool
81
+ wasm_memory_init_with_pool(void *mem, unsigned int bytes)
82
+ {
83
+ mem_allocator_t allocator = mem_allocator_create(mem, bytes);
84
+
85
+ if (allocator) {
86
+ memory_mode = MEMORY_MODE_POOL;
87
+ pool_allocator = allocator;
88
+ global_pool_size = bytes;
89
+ return true;
90
+ }
91
+ LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
92
+ return false;
93
+ }
94
+
95
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
96
+ static bool
97
+ wasm_memory_init_with_allocator(void *_user_data, void *_malloc_func,
98
+ void *_realloc_func, void *_free_func)
99
+ {
100
+ if (_malloc_func && _free_func && _malloc_func != _free_func) {
101
+ memory_mode = MEMORY_MODE_ALLOCATOR;
102
+ allocator_user_data = _user_data;
103
+ malloc_func = _malloc_func;
104
+ realloc_func = _realloc_func;
105
+ free_func = _free_func;
106
+ return true;
107
+ }
108
+ LOG_ERROR("Init memory with allocator (%p, %p, %p, %p) failed.\n",
109
+ _user_data, _malloc_func, _realloc_func, _free_func);
110
+ return false;
111
+ }
112
+ #else
113
+ static bool
114
+ wasm_memory_init_with_allocator(void *malloc_func_ptr, void *realloc_func_ptr,
115
+ void *free_func_ptr)
116
+ {
117
+ if (malloc_func_ptr && free_func_ptr && malloc_func_ptr != free_func_ptr) {
118
+ memory_mode = MEMORY_MODE_ALLOCATOR;
119
+ malloc_func = malloc_func_ptr;
120
+ realloc_func = realloc_func_ptr;
121
+ free_func = free_func_ptr;
122
+ return true;
123
+ }
124
+ LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
125
+ malloc_func_ptr, realloc_func_ptr, free_func_ptr);
126
+ return false;
127
+ }
128
+ #endif
129
+
130
+ static inline bool
131
+ is_bounds_checks_enabled(WASMModuleInstanceCommon *module_inst)
132
+ {
133
+ #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
134
+ if (!module_inst) {
135
+ return true;
136
+ }
137
+
138
+ return wasm_runtime_is_bounds_checks_enabled(module_inst);
139
+ #else
140
+ return true;
141
+ #endif
142
+ }
143
+
144
+ #if WASM_ENABLE_SHARED_HEAP != 0
145
+ static void *
146
+ wasm_mmap_linear_memory(uint64 map_size, uint64 commit_size);
147
+ static void
148
+ wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size,
149
+ uint64 map_size);
150
+
151
+ static void *
152
+ runtime_malloc(uint64 size)
153
+ {
154
+ void *mem;
155
+
156
+ if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
157
+ LOG_WARNING("Allocate memory failed");
158
+ return NULL;
159
+ }
160
+
161
+ memset(mem, 0, (uint32)size);
162
+ return mem;
163
+ }
164
+
165
+ WASMSharedHeap *
166
+ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
167
+ {
168
+ uint64 heap_struct_size = sizeof(WASMSharedHeap), map_size;
169
+ uint32 size = init_args->size;
170
+ WASMSharedHeap *heap;
171
+
172
+ if (size == 0) {
173
+ goto fail1;
174
+ }
175
+
176
+ if (!(heap = runtime_malloc(heap_struct_size))) {
177
+ goto fail1;
178
+ }
179
+
180
+ size = align_uint(size, os_getpagesize());
181
+ if (size > APP_HEAP_SIZE_MAX || size < APP_HEAP_SIZE_MIN) {
182
+ LOG_WARNING("Invalid size of shared heap");
183
+ goto fail2;
184
+ }
185
+
186
+ heap->size = size;
187
+ heap->start_off_mem64 = UINT64_MAX - heap->size + 1;
188
+ heap->start_off_mem32 = UINT32_MAX - heap->size + 1;
189
+ heap->attached_count = 0;
190
+
191
+ if (init_args->pre_allocated_addr != NULL) {
192
+ /* Create shared heap from a pre allocated buffer, its size need to
193
+ * align with system page */
194
+ if (size != init_args->size) {
195
+ LOG_WARNING("Pre allocated size need to be aligned with system "
196
+ "page size to create shared heap");
197
+ goto fail2;
198
+ }
199
+
200
+ heap->heap_handle = NULL;
201
+ heap->base_addr = init_args->pre_allocated_addr;
202
+ }
203
+ else {
204
+ if (!(heap->heap_handle =
205
+ runtime_malloc(mem_allocator_get_heap_struct_size()))) {
206
+ goto fail2;
207
+ }
208
+
209
+ #ifndef OS_ENABLE_HW_BOUND_CHECK
210
+ map_size = size;
211
+ #else
212
+ /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
213
+ * ea = i + memarg.offset
214
+ * both i and memarg.offset are u32 in range 0 to 4G
215
+ * so the range of ea is 0 to 8G
216
+ */
217
+ map_size = 8 * (uint64)BH_GB;
218
+ #endif
219
+
220
+ if (!(heap->base_addr = wasm_mmap_linear_memory(map_size, size))) {
221
+ goto fail3;
222
+ }
223
+ if (!mem_allocator_create_with_struct_and_pool(
224
+ heap->heap_handle, heap_struct_size, heap->base_addr, size)) {
225
+ LOG_WARNING("init share heap failed");
226
+ goto fail4;
227
+ }
228
+ }
229
+
230
+ os_mutex_lock(&shared_heap_list_lock);
231
+ if (shared_heap_list == NULL) {
232
+ shared_heap_list = heap;
233
+ }
234
+ else {
235
+ heap->next = shared_heap_list;
236
+ shared_heap_list = heap;
237
+ }
238
+ os_mutex_unlock(&shared_heap_list_lock);
239
+ return heap;
240
+
241
+ fail4:
242
+ wasm_munmap_linear_memory(heap->base_addr, size, map_size);
243
+ fail3:
244
+ wasm_runtime_free(heap->heap_handle);
245
+ fail2:
246
+ wasm_runtime_free(heap);
247
+ fail1:
248
+ return NULL;
249
+ }
250
+
251
+ WASMSharedHeap *
252
+ wasm_runtime_chain_shared_heaps(WASMSharedHeap *head, WASMSharedHeap *body)
253
+ {
254
+ WASMSharedHeap *cur;
255
+ bool heap_handle_exist = false;
256
+
257
+ if (!head || !body) {
258
+ LOG_WARNING("Invalid shared heap to chain.");
259
+ return NULL;
260
+ }
261
+ heap_handle_exist = head->heap_handle != NULL;
262
+
263
+ os_mutex_lock(&shared_heap_list_lock);
264
+ if (head->attached_count != 0 || body->attached_count != 0) {
265
+ LOG_WARNING("To create shared heap chain, all shared heap need to be "
266
+ "detached first.");
267
+ os_mutex_unlock(&shared_heap_list_lock);
268
+ return NULL;
269
+ }
270
+ for (cur = shared_heap_list; cur; cur = cur->next) {
271
+ if (cur->chain_next == body || cur->chain_next == head) {
272
+ LOG_WARNING(
273
+ "To create shared heap chain, both the 'head' and 'body' "
274
+ "shared heap can't already be the 'body' in another a chain");
275
+ os_mutex_unlock(&shared_heap_list_lock);
276
+ return NULL;
277
+ }
278
+ if (cur == head && cur->chain_next) {
279
+ LOG_WARNING(
280
+ "To create shared heap chain, the 'head' shared heap can't "
281
+ "already be the 'head' in another a chain");
282
+ os_mutex_unlock(&shared_heap_list_lock);
283
+ return NULL;
284
+ }
285
+ }
286
+ for (cur = body; cur; cur = cur->chain_next) {
287
+ if (cur->heap_handle && heap_handle_exist) {
288
+ LOG_WARNING(
289
+ "To create shared heap chain, only one of shared heap can "
290
+ "dynamically shared_heap_malloc and shared_heap_free, the rest "
291
+ "can only be pre-allocated shared heap");
292
+ os_mutex_unlock(&shared_heap_list_lock);
293
+ return NULL;
294
+ }
295
+ if (cur->heap_handle)
296
+ heap_handle_exist = true;
297
+ }
298
+
299
+ head->start_off_mem64 = body->start_off_mem64 - head->size;
300
+ head->start_off_mem32 = body->start_off_mem32 - head->size;
301
+ head->chain_next = body;
302
+ os_mutex_unlock(&shared_heap_list_lock);
303
+ return head;
304
+ }
305
+
306
+ WASMSharedHeap *
307
+ wasm_runtime_unchain_shared_heaps(WASMSharedHeap *head, bool entire_chain)
308
+ {
309
+ WASMSharedHeap *cur, *tmp;
310
+
311
+ if (!head || !head->chain_next) {
312
+ LOG_WARNING("Invalid shared heap chain to disconnect the head from.");
313
+ return NULL;
314
+ }
315
+
316
+ os_mutex_lock(&shared_heap_list_lock);
317
+ if (head->attached_count != 0) {
318
+ LOG_WARNING("To disconnect the shared heap head from the shared heap "
319
+ "chain, the shared heap chain needs to be detached first.");
320
+ os_mutex_unlock(&shared_heap_list_lock);
321
+ return NULL;
322
+ }
323
+
324
+ cur = head;
325
+ while (cur && cur->chain_next) {
326
+ cur->start_off_mem64 = UINT64_MAX - cur->size + 1;
327
+ cur->start_off_mem32 = UINT32_MAX - cur->size + 1;
328
+ tmp = cur;
329
+ cur = cur->chain_next;
330
+ tmp->chain_next = NULL;
331
+ if (!entire_chain)
332
+ break;
333
+ }
334
+ os_mutex_unlock(&shared_heap_list_lock);
335
+ return cur;
336
+ }
337
+
338
+ static uint8 *
339
+ get_last_used_shared_heap_base_addr_adj(WASMModuleInstanceCommon *module_inst)
340
+ {
341
+ #if WASM_ENABLE_INTERP != 0
342
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
343
+ WASMModuleInstanceExtra *e =
344
+ (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
345
+ return e->shared_heap_base_addr_adj;
346
+ }
347
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
348
+ #if WASM_ENABLE_AOT != 0
349
+ if (module_inst->module_type == Wasm_Module_AoT) {
350
+ AOTModuleInstanceExtra *e =
351
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
352
+ return e->shared_heap_base_addr_adj;
353
+ }
354
+ #endif /* end of WASM_ENABLE_AOT != 0 */
355
+ return 0;
356
+ }
357
+
358
+ static uintptr_t
359
+ get_last_used_shared_heap_start_offset(WASMModuleInstanceCommon *module_inst)
360
+ {
361
+ #if WASM_ENABLE_INTERP != 0
362
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
363
+ WASMModuleInstanceExtra *e =
364
+ (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
365
+ #if UINTPTR_MAX == UINT64_MAX
366
+ return e->shared_heap_start_off.u64;
367
+ #else
368
+ return e->shared_heap_start_off.u32[0];
369
+ #endif
370
+ }
371
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
372
+ #if WASM_ENABLE_AOT != 0
373
+ if (module_inst->module_type == Wasm_Module_AoT) {
374
+ AOTModuleInstanceExtra *e =
375
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
376
+ #if UINTPTR_MAX == UINT64_MAX
377
+ return e->shared_heap_start_off.u64;
378
+ #else
379
+ return e->shared_heap_start_off.u32[0];
380
+ #endif
381
+ }
382
+ #endif /* end of WASM_ENABLE_AOT != 0 */
383
+ return 0;
384
+ }
385
+
386
+ static uintptr_t
387
+ get_last_used_shared_heap_end_offset(WASMModuleInstanceCommon *module_inst)
388
+ {
389
+ #if WASM_ENABLE_INTERP != 0
390
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
391
+ WASMModuleInstanceExtra *e =
392
+ (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
393
+ #if UINTPTR_MAX == UINT64_MAX
394
+ return e->shared_heap_end_off.u64;
395
+ #else
396
+ return e->shared_heap_end_off.u32[0];
397
+ #endif
398
+ }
399
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
400
+ #if WASM_ENABLE_AOT != 0
401
+ if (module_inst->module_type == Wasm_Module_AoT) {
402
+ AOTModuleInstanceExtra *e =
403
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
404
+ #if UINTPTR_MAX == UINT64_MAX
405
+ return e->shared_heap_end_off.u64;
406
+ #else
407
+ return e->shared_heap_end_off.u32[0];
408
+ #endif
409
+ }
410
+ #endif /* end of WASM_ENABLE_AOT != 0 */
411
+ return 0;
412
+ }
413
+
414
+ static void
415
+ update_last_used_shared_heap(WASMModuleInstanceCommon *module_inst,
416
+ WASMSharedHeap *shared_heap, bool is_memory64)
417
+ {
418
+ #if WASM_ENABLE_INTERP != 0
419
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
420
+ WASMModuleInstanceExtra *e =
421
+ (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
422
+ #if UINTPTR_MAX == UINT64_MAX
423
+ if (is_memory64)
424
+ e->shared_heap_start_off.u64 = shared_heap->start_off_mem64;
425
+ else
426
+ e->shared_heap_start_off.u64 = shared_heap->start_off_mem32;
427
+ e->shared_heap_end_off.u64 =
428
+ e->shared_heap_start_off.u64 - 1 + shared_heap->size;
429
+ e->shared_heap_base_addr_adj =
430
+ shared_heap->base_addr - e->shared_heap_start_off.u64;
431
+ #else
432
+ e->shared_heap_start_off.u32[0] = (uint32)shared_heap->start_off_mem32;
433
+ e->shared_heap_end_off.u32[0] =
434
+ e->shared_heap_start_off.u32[0] - 1 + shared_heap->size;
435
+ e->shared_heap_base_addr_adj =
436
+ shared_heap->base_addr - e->shared_heap_start_off.u32[0];
437
+ #endif
438
+ }
439
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
440
+ #if WASM_ENABLE_AOT != 0
441
+ if (module_inst->module_type == Wasm_Module_AoT) {
442
+ AOTModuleInstanceExtra *e =
443
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
444
+ #if UINTPTR_MAX == UINT64_MAX
445
+ if (is_memory64)
446
+ e->shared_heap_start_off.u64 = shared_heap->start_off_mem64;
447
+ else
448
+ e->shared_heap_start_off.u64 = shared_heap->start_off_mem32;
449
+ e->shared_heap_end_off.u64 =
450
+ e->shared_heap_start_off.u64 - 1 + shared_heap->size;
451
+ e->shared_heap_base_addr_adj =
452
+ shared_heap->base_addr - e->shared_heap_start_off.u64;
453
+ #else
454
+ e->shared_heap_start_off.u32[0] = (uint32)shared_heap->start_off_mem32;
455
+ e->shared_heap_end_off.u32[0] =
456
+ e->shared_heap_start_off.u32[0] - 1 + shared_heap->size;
457
+ e->shared_heap_base_addr_adj =
458
+ shared_heap->base_addr - e->shared_heap_start_off.u32[0];
459
+ #endif
460
+ }
461
+ #endif /* end of WASM_ENABLE_AOT != 0 */
462
+ }
463
+
464
+ bool
465
+ wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
466
+ WASMSharedHeap *shared_heap)
467
+ {
468
+ WASMMemoryInstance *memory =
469
+ wasm_get_default_memory((WASMModuleInstance *)module_inst);
470
+ uint64 linear_mem_size;
471
+
472
+ if (!memory)
473
+ return false;
474
+
475
+ linear_mem_size = memory->memory_data_size;
476
+
477
+ /* check if linear memory and shared heap are overlapped */
478
+ if ((memory->is_memory64 && linear_mem_size > shared_heap->start_off_mem64)
479
+ || (!memory->is_memory64
480
+ && linear_mem_size > shared_heap->start_off_mem32)) {
481
+ LOG_WARNING("Linear memory address is overlapped with shared heap");
482
+ return false;
483
+ }
484
+
485
+ #if WASM_ENABLE_INTERP != 0
486
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
487
+ WASMModuleInstanceExtra *e =
488
+ (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
489
+ if (e->shared_heap) {
490
+ LOG_WARNING("A shared heap is already attached");
491
+ return false;
492
+ }
493
+ e->shared_heap = shared_heap;
494
+ }
495
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
496
+ #if WASM_ENABLE_AOT != 0
497
+ if (module_inst->module_type == Wasm_Module_AoT) {
498
+ AOTModuleInstanceExtra *e =
499
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
500
+ if (e->shared_heap) {
501
+ LOG_WARNING("A shared heap is already attached");
502
+ return false;
503
+ }
504
+ e->shared_heap = shared_heap;
505
+ }
506
+ #endif /* end of WASM_ENABLE_AOT != 0 */
507
+ update_last_used_shared_heap(module_inst, shared_heap, memory->is_memory64);
508
+
509
+ os_mutex_lock(&shared_heap_list_lock);
510
+ shared_heap->attached_count++;
511
+ os_mutex_unlock(&shared_heap_list_lock);
512
+ return true;
513
+ }
514
+
515
+ bool
516
+ wasm_runtime_attach_shared_heap(WASMModuleInstanceCommon *module_inst,
517
+ WASMSharedHeap *shared_heap)
518
+ {
519
+ #if WASM_ENABLE_THREAD_MGR != 0
520
+ return wasm_cluster_attach_shared_heap(module_inst, shared_heap);
521
+ #else
522
+ return wasm_runtime_attach_shared_heap_internal(module_inst, shared_heap);
523
+ #endif
524
+ }
525
+
526
+ void
527
+ wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
528
+ {
529
+ /* Reset shared_heap_end_off = UINT64/32_MAX - 1 to handling a corner case,
530
+ app_offset >= shared_heap_start && app_offset <= shared_heap_end-bytes+1
531
+ when bytes=1 and both e->shared_heap_start_off and e->shared_heap_end_off
532
+ is 0xffffffff */
533
+ #if WASM_ENABLE_INTERP != 0
534
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
535
+ WASMModuleInstanceExtra *e =
536
+ (WASMModuleInstanceExtra *)((WASMModuleInstance *)module_inst)->e;
537
+ if (e->shared_heap != NULL) {
538
+ os_mutex_lock(&shared_heap_list_lock);
539
+ e->shared_heap->attached_count--;
540
+ os_mutex_unlock(&shared_heap_list_lock);
541
+ }
542
+ e->shared_heap = NULL;
543
+ #if UINTPTR_MAX == UINT64_MAX
544
+ e->shared_heap_start_off.u64 = UINT64_MAX;
545
+ e->shared_heap_end_off.u64 = UINT64_MAX - 1;
546
+ #else
547
+ e->shared_heap_start_off.u32[0] = UINT32_MAX;
548
+ e->shared_heap_end_off.u32[0] = UINT32_MAX - 1;
549
+ #endif
550
+ e->shared_heap_base_addr_adj = NULL;
551
+ }
552
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
553
+ #if WASM_ENABLE_AOT != 0
554
+ if (module_inst->module_type == Wasm_Module_AoT) {
555
+ AOTModuleInstanceExtra *e =
556
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst)->e;
557
+ if (e->shared_heap != NULL) {
558
+ os_mutex_lock(&shared_heap_list_lock);
559
+ e->shared_heap->attached_count--;
560
+ os_mutex_unlock(&shared_heap_list_lock);
561
+ }
562
+ e->shared_heap = NULL;
563
+ #if UINTPTR_MAX == UINT64_MAX
564
+ e->shared_heap_start_off.u64 = UINT64_MAX;
565
+ e->shared_heap_end_off.u64 = UINT64_MAX - 1;
566
+ #else
567
+ e->shared_heap_start_off.u32[0] = UINT32_MAX;
568
+ e->shared_heap_end_off.u32[0] = UINT32_MAX - 1;
569
+ #endif
570
+ e->shared_heap_base_addr_adj = NULL;
571
+ }
572
+ #endif /* end of WASM_ENABLE_AOT != 0 */
573
+ }
574
+
575
+ void
576
+ wasm_runtime_detach_shared_heap(WASMModuleInstanceCommon *module_inst)
577
+ {
578
+ #if WASM_ENABLE_THREAD_MGR != 0
579
+ wasm_cluster_detach_shared_heap(module_inst);
580
+ #else
581
+ wasm_runtime_detach_shared_heap_internal(module_inst);
582
+ #endif
583
+ }
584
+
585
+ static WASMSharedHeap *
586
+ get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
587
+ {
588
+ #if WASM_ENABLE_INTERP != 0
589
+ if (module_inst_comm->module_type == Wasm_Module_Bytecode) {
590
+ return ((WASMModuleInstance *)module_inst_comm)->e->shared_heap;
591
+ }
592
+ #endif
593
+ #if WASM_ENABLE_AOT != 0
594
+ if (module_inst_comm->module_type == Wasm_Module_AoT) {
595
+ AOTModuleInstanceExtra *e =
596
+ (AOTModuleInstanceExtra *)((AOTModuleInstance *)module_inst_comm)
597
+ ->e;
598
+ return e->shared_heap;
599
+ }
600
+ #endif
601
+ return NULL;
602
+ }
603
+
604
+ WASMSharedHeap *
605
+ wasm_runtime_get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
606
+ {
607
+ return get_shared_heap(module_inst_comm);
608
+ }
609
+
610
+ bool
611
+ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
612
+ bool is_memory64, uint64 app_offset, uint32 bytes)
613
+ {
614
+ WASMSharedHeap *heap = get_shared_heap(module_inst), *cur;
615
+ uint64 shared_heap_start, shared_heap_end;
616
+
617
+ if (!heap) {
618
+ goto fail;
619
+ }
620
+
621
+ if (bytes == 0) {
622
+ bytes = 1;
623
+ }
624
+
625
+ shared_heap_start =
626
+ (uint64)get_last_used_shared_heap_start_offset(module_inst);
627
+ shared_heap_end = (uint64)get_last_used_shared_heap_end_offset(module_inst);
628
+ if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
629
+ && app_offset <= shared_heap_end - bytes + 1) {
630
+ return true;
631
+ }
632
+
633
+ /* Early stop for app start address not in the shared heap(chain) at all */
634
+ shared_heap_start =
635
+ is_memory64 ? heap->start_off_mem64 : heap->start_off_mem32;
636
+ shared_heap_end = is_memory64 ? UINT64_MAX : UINT32_MAX;
637
+ if (bytes - 1 > shared_heap_end || app_offset < shared_heap_start
638
+ || app_offset > shared_heap_end - bytes + 1) {
639
+ goto fail;
640
+ }
641
+
642
+ /* Find the exact shared heap that app addr is in, and update last used
643
+ * shared heap info in module inst extra */
644
+ for (cur = heap; cur; cur = cur->chain_next) {
645
+ shared_heap_start =
646
+ is_memory64 ? cur->start_off_mem64 : cur->start_off_mem32;
647
+ shared_heap_end = shared_heap_start - 1 + cur->size;
648
+ if (bytes - 1 <= shared_heap_end && app_offset >= shared_heap_start
649
+ && app_offset <= shared_heap_end - bytes + 1) {
650
+ update_last_used_shared_heap(module_inst, cur, is_memory64);
651
+ return true;
652
+ }
653
+ }
654
+
655
+ fail:
656
+ return false;
657
+ }
658
+
659
+ static bool
660
+ is_native_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
661
+ bool is_memory64, uint8 *addr, uint32 bytes)
662
+ {
663
+ WASMSharedHeap *cur, *heap = get_shared_heap(module_inst);
664
+ uintptr_t base_addr, addr_int, end_addr;
665
+
666
+ if (!heap) {
667
+ goto fail;
668
+ }
669
+
670
+ /* Iterate through shared heap chain to find whether native addr in one of
671
+ * shared heap */
672
+ for (cur = heap; cur != NULL; cur = cur->chain_next) {
673
+ base_addr = (uintptr_t)cur->base_addr;
674
+ addr_int = (uintptr_t)addr;
675
+ if (addr_int < base_addr)
676
+ continue;
677
+
678
+ end_addr = addr_int + bytes;
679
+ /* Check for overflow */
680
+ if (end_addr <= addr_int)
681
+ continue;
682
+
683
+ if (end_addr > base_addr + cur->size)
684
+ continue;
685
+
686
+ update_last_used_shared_heap(module_inst, cur, is_memory64);
687
+ return true;
688
+ }
689
+
690
+ fail:
691
+ return false;
692
+ }
693
+
694
+ uint64
695
+ wasm_runtime_shared_heap_malloc(WASMModuleInstanceCommon *module_inst,
696
+ uint64 size, void **p_native_addr)
697
+ {
698
+ WASMMemoryInstance *memory =
699
+ wasm_get_default_memory((WASMModuleInstance *)module_inst);
700
+ WASMSharedHeap *shared_heap = get_shared_heap(module_inst);
701
+ void *native_addr = NULL;
702
+
703
+ if (!memory || !shared_heap)
704
+ return 0;
705
+
706
+ while (shared_heap && !shared_heap->heap_handle) {
707
+ shared_heap = shared_heap->chain_next;
708
+ }
709
+ if (!shared_heap) {
710
+ LOG_WARNING("Can't allocate from pre allocated shared heap");
711
+ return 0;
712
+ }
713
+
714
+ native_addr = mem_allocator_malloc(shared_heap->heap_handle, size);
715
+ if (!native_addr)
716
+ return 0;
717
+
718
+ if (p_native_addr) {
719
+ *p_native_addr = native_addr;
720
+ }
721
+
722
+ return memory->is_memory64
723
+ ? shared_heap->start_off_mem64
724
+ : shared_heap->start_off_mem32
725
+ + ((uint8 *)native_addr - shared_heap->base_addr);
726
+ }
727
+
728
+ void
729
+ wasm_runtime_shared_heap_free(WASMModuleInstanceCommon *module_inst, uint64 ptr)
730
+ {
731
+ WASMMemoryInstance *memory =
732
+ wasm_get_default_memory((WASMModuleInstance *)module_inst);
733
+ WASMSharedHeap *shared_heap = get_shared_heap(module_inst);
734
+ uint8 *addr = NULL;
735
+
736
+ if (!memory || !shared_heap) {
737
+ return;
738
+ }
739
+
740
+ while (shared_heap && !shared_heap->heap_handle) {
741
+ shared_heap = shared_heap->chain_next;
742
+ }
743
+ if (!shared_heap) {
744
+ LOG_WARNING("The address to free is from pre allocated shared heap");
745
+ return;
746
+ }
747
+
748
+ if (memory->is_memory64) {
749
+ if (ptr < shared_heap->start_off_mem64) { /* ptr can not > UINT64_MAX */
750
+ LOG_WARNING("The address to free isn't in shared heap");
751
+ return;
752
+ }
753
+ addr = shared_heap->base_addr + (ptr - shared_heap->start_off_mem64);
754
+ }
755
+ else {
756
+ if (ptr < shared_heap->start_off_mem32 || ptr > UINT32_MAX) {
757
+ LOG_WARNING("The address to free isn't in shared heap");
758
+ return;
759
+ }
760
+ addr = shared_heap->base_addr + (ptr - shared_heap->start_off_mem32);
761
+ }
762
+
763
+ mem_allocator_free(shared_heap->heap_handle, addr);
764
+ }
765
+ #endif /* end of WASM_ENABLE_SHARED_HEAP != 0 */
766
+
767
+ bool
768
+ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
769
+ const MemAllocOption *alloc_option)
770
+ {
771
+ bool ret = false;
772
+
773
+ #if WASM_ENABLE_SHARED_HEAP != 0
774
+ if (os_mutex_init(&shared_heap_list_lock)) {
775
+ return false;
776
+ }
777
+ #endif
778
+
779
+ if (mem_alloc_type == Alloc_With_Pool) {
780
+ ret = wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
781
+ alloc_option->pool.heap_size);
782
+ }
783
+ else if (mem_alloc_type == Alloc_With_Allocator) {
784
+ ret = wasm_memory_init_with_allocator(
785
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
786
+ alloc_option->allocator.user_data,
787
+ #endif
788
+ alloc_option->allocator.malloc_func,
789
+ alloc_option->allocator.realloc_func,
790
+ alloc_option->allocator.free_func);
791
+ }
792
+ else if (mem_alloc_type == Alloc_With_System_Allocator) {
793
+ memory_mode = MEMORY_MODE_SYSTEM_ALLOCATOR;
794
+ ret = true;
795
+ }
796
+ else {
797
+ ret = false;
798
+ }
799
+
800
+ #if WASM_ENABLE_SHARED_HEAP != 0
801
+ if (!ret) {
802
+ os_mutex_destroy(&shared_heap_list_lock);
803
+ }
804
+ #endif
805
+
806
+ return ret;
807
+ }
808
+
809
+ #if WASM_ENABLE_SHARED_HEAP != 0
810
+ static void
811
+ destroy_shared_heaps()
812
+ {
813
+ WASMSharedHeap *heap;
814
+ WASMSharedHeap *cur;
815
+ uint64 map_size;
816
+
817
+ os_mutex_lock(&shared_heap_list_lock);
818
+ heap = shared_heap_list;
819
+ shared_heap_list = NULL;
820
+ os_mutex_unlock(&shared_heap_list_lock);
821
+
822
+ while (heap) {
823
+ cur = heap;
824
+ heap = heap->next;
825
+ if (cur->heap_handle) {
826
+ mem_allocator_destroy(cur->heap_handle);
827
+ wasm_runtime_free(cur->heap_handle);
828
+ #ifndef OS_ENABLE_HW_BOUND_CHECK
829
+ map_size = cur->size;
830
+ #else
831
+ map_size = 8 * (uint64)BH_GB;
832
+ #endif
833
+ wasm_munmap_linear_memory(cur->base_addr, cur->size, map_size);
834
+ }
835
+ wasm_runtime_free(cur);
836
+ }
837
+ os_mutex_destroy(&shared_heap_list_lock);
838
+ }
839
+ #endif
840
+
841
+ void
842
+ wasm_runtime_memory_destroy(void)
843
+ {
844
+ #if WASM_ENABLE_SHARED_HEAP != 0
845
+ destroy_shared_heaps();
846
+ #endif
847
+
848
+ if (memory_mode == MEMORY_MODE_POOL) {
849
+ #if BH_ENABLE_GC_VERIFY == 0
850
+ (void)mem_allocator_destroy(pool_allocator);
851
+ #else
852
+ int ret = mem_allocator_destroy(pool_allocator);
853
+ if (ret != 0) {
854
+ /* Memory leak detected */
855
+ exit(-1);
856
+ }
857
+ #endif
858
+ }
859
+ memory_mode = MEMORY_MODE_UNKNOWN;
860
+ }
861
+
862
+ unsigned
863
+ wasm_runtime_memory_pool_size(void)
864
+ {
865
+ if (memory_mode == MEMORY_MODE_POOL)
866
+ return global_pool_size;
867
+ else
868
+ return UINT32_MAX;
869
+ }
870
+
871
+ static inline void *
872
+ wasm_runtime_malloc_internal(unsigned int size)
873
+ {
874
+ if (memory_mode == MEMORY_MODE_UNKNOWN) {
875
+ LOG_WARNING(
876
+ "wasm_runtime_malloc failed: memory hasn't been initialized.\n");
877
+ return NULL;
878
+ }
879
+ else if (memory_mode == MEMORY_MODE_POOL) {
880
+ return mem_allocator_malloc(pool_allocator, size);
881
+ }
882
+ else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
883
+ return malloc_func(
884
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
885
+ Alloc_For_Runtime,
886
+ #endif
887
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
888
+ allocator_user_data,
889
+ #endif
890
+ size);
891
+ }
892
+ else {
893
+ return os_malloc(size);
894
+ }
895
+ }
896
+
897
+ static inline void *
898
+ wasm_runtime_realloc_internal(void *ptr, unsigned int size)
899
+ {
900
+ if (memory_mode == MEMORY_MODE_UNKNOWN) {
901
+ LOG_WARNING(
902
+ "wasm_runtime_realloc failed: memory hasn't been initialized.\n");
903
+ return NULL;
904
+ }
905
+ else if (memory_mode == MEMORY_MODE_POOL) {
906
+ return mem_allocator_realloc(pool_allocator, ptr, size);
907
+ }
908
+ else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
909
+ if (realloc_func)
910
+ return realloc_func(
911
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
912
+ Alloc_For_Runtime, false,
913
+ #endif
914
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
915
+ allocator_user_data,
916
+ #endif
917
+ ptr, size);
918
+ else
919
+ return NULL;
920
+ }
921
+ else {
922
+ return os_realloc(ptr, size);
923
+ }
924
+ }
925
+
926
+ static inline void
927
+ wasm_runtime_free_internal(void *ptr)
928
+ {
929
+ if (!ptr) {
930
+ LOG_WARNING("warning: wasm_runtime_free with NULL pointer\n");
931
+ #if BH_ENABLE_GC_VERIFY != 0
932
+ exit(-1);
933
+ #endif
934
+ return;
935
+ }
936
+
937
+ if (memory_mode == MEMORY_MODE_UNKNOWN) {
938
+ LOG_WARNING("warning: wasm_runtime_free failed: "
939
+ "memory hasn't been initialize.\n");
940
+ }
941
+ else if (memory_mode == MEMORY_MODE_POOL) {
942
+ mem_allocator_free(pool_allocator, ptr);
943
+ }
944
+ else if (memory_mode == MEMORY_MODE_ALLOCATOR) {
945
+ free_func(
946
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
947
+ Alloc_For_Runtime,
948
+ #endif
949
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
950
+ allocator_user_data,
951
+ #endif
952
+ ptr);
953
+ }
954
+ else {
955
+ os_free(ptr);
956
+ }
957
+ }
958
+
959
+ void *
960
+ wasm_runtime_malloc(unsigned int size)
961
+ {
962
+ if (size == 0) {
963
+ LOG_WARNING("warning: wasm_runtime_malloc with size zero\n");
964
+ /* At lease alloc 1 byte to avoid malloc failed */
965
+ size = 1;
966
+ #if BH_ENABLE_GC_VERIFY != 0
967
+ exit(-1);
968
+ #endif
969
+ }
970
+
971
+ #if WASM_ENABLE_FUZZ_TEST != 0
972
+ if (size >= WASM_MEM_ALLOC_MAX_SIZE) {
973
+ LOG_WARNING("warning: wasm_runtime_malloc with too large size\n");
974
+ return NULL;
975
+ }
976
+ #endif
977
+
978
+ return wasm_runtime_malloc_internal(size);
979
+ }
980
+
981
+ void *
982
+ wasm_runtime_realloc(void *ptr, unsigned int size)
983
+ {
984
+ return wasm_runtime_realloc_internal(ptr, size);
985
+ }
986
+
987
+ void
988
+ wasm_runtime_free(void *ptr)
989
+ {
990
+ wasm_runtime_free_internal(ptr);
991
+ }
992
+
993
+ bool
994
+ wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
995
+ {
996
+ if (memory_mode == MEMORY_MODE_POOL) {
997
+ return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
998
+ }
999
+ return false;
1000
+ }
1001
+
1002
+ bool
1003
+ wasm_runtime_validate_app_addr(WASMModuleInstanceCommon *module_inst_comm,
1004
+ uint64 app_offset, uint64 size)
1005
+ {
1006
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1007
+ WASMMemoryInstance *memory_inst;
1008
+ uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
1009
+
1010
+ bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1011
+ || module_inst_comm->module_type == Wasm_Module_AoT);
1012
+
1013
+ if (!is_bounds_checks_enabled(module_inst_comm)) {
1014
+ return true;
1015
+ }
1016
+
1017
+ memory_inst = wasm_get_default_memory(module_inst);
1018
+ if (!memory_inst) {
1019
+ goto fail;
1020
+ }
1021
+
1022
+ #if WASM_ENABLE_SHARED_HEAP != 0
1023
+ if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
1024
+ app_offset, size)) {
1025
+ return true;
1026
+ }
1027
+ #endif
1028
+
1029
+ #if WASM_ENABLE_MEMORY64 != 0
1030
+ if (memory_inst->is_memory64)
1031
+ max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
1032
+ #endif
1033
+ /* boundary overflow check */
1034
+ if (size > max_linear_memory_size
1035
+ || app_offset > max_linear_memory_size - size) {
1036
+ goto fail;
1037
+ }
1038
+
1039
+ SHARED_MEMORY_LOCK(memory_inst);
1040
+
1041
+ if (app_offset + size <= memory_inst->memory_data_size) {
1042
+ SHARED_MEMORY_UNLOCK(memory_inst);
1043
+ return true;
1044
+ }
1045
+
1046
+ SHARED_MEMORY_UNLOCK(memory_inst);
1047
+
1048
+ fail:
1049
+ wasm_set_exception(module_inst, "out of bounds memory access");
1050
+ return false;
1051
+ }
1052
+
1053
+ bool
1054
+ wasm_runtime_validate_app_str_addr(WASMModuleInstanceCommon *module_inst_comm,
1055
+ uint64 app_str_offset)
1056
+ {
1057
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1058
+ WASMMemoryInstance *memory_inst;
1059
+ uint64 app_end_offset, max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
1060
+ char *str, *str_end;
1061
+ #if WASM_ENABLE_SHARED_HEAP != 0
1062
+ uintptr_t shared_heap_end_off;
1063
+ char *shared_heap_base_addr_adj;
1064
+ #endif
1065
+
1066
+ bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1067
+ || module_inst_comm->module_type == Wasm_Module_AoT);
1068
+
1069
+ if (!is_bounds_checks_enabled(module_inst_comm)) {
1070
+ return true;
1071
+ }
1072
+
1073
+ memory_inst = wasm_get_default_memory(module_inst);
1074
+ if (!memory_inst) {
1075
+ goto fail;
1076
+ }
1077
+
1078
+ #if WASM_ENABLE_SHARED_HEAP != 0
1079
+ if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
1080
+ app_str_offset, 1)) {
1081
+ shared_heap_end_off =
1082
+ get_last_used_shared_heap_end_offset(module_inst_comm);
1083
+ shared_heap_base_addr_adj =
1084
+ (char *)get_last_used_shared_heap_base_addr_adj(module_inst_comm);
1085
+ str = shared_heap_base_addr_adj + app_str_offset;
1086
+ str_end = shared_heap_base_addr_adj + shared_heap_end_off + 1;
1087
+ }
1088
+ else
1089
+ #endif
1090
+ {
1091
+ if (!wasm_runtime_get_app_addr_range(module_inst_comm, app_str_offset,
1092
+ NULL, &app_end_offset))
1093
+ goto fail;
1094
+
1095
+ #if WASM_ENABLE_MEMORY64 != 0
1096
+ if (memory_inst->is_memory64)
1097
+ max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
1098
+ #endif
1099
+ /* boundary overflow check, max start offset can be size - 1, while end
1100
+ offset can be size */
1101
+ if (app_str_offset >= max_linear_memory_size
1102
+ || app_end_offset > max_linear_memory_size)
1103
+ goto fail;
1104
+
1105
+ str = wasm_runtime_addr_app_to_native(module_inst_comm, app_str_offset);
1106
+ str_end = str + (app_end_offset - app_str_offset);
1107
+ }
1108
+
1109
+ while (str < str_end && *str != '\0')
1110
+ str++;
1111
+ if (str == str_end)
1112
+ goto fail;
1113
+
1114
+ return true;
1115
+ fail:
1116
+ wasm_set_exception(module_inst, "out of bounds memory access");
1117
+ return false;
1118
+ }
1119
+
1120
+ bool
1121
+ wasm_runtime_validate_native_addr(WASMModuleInstanceCommon *module_inst_comm,
1122
+ void *native_ptr, uint64 size)
1123
+ {
1124
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1125
+ WASMMemoryInstance *memory_inst;
1126
+ uint8 *addr = (uint8 *)native_ptr;
1127
+ uint64 max_linear_memory_size = MAX_LINEAR_MEMORY_SIZE;
1128
+
1129
+ bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1130
+ || module_inst_comm->module_type == Wasm_Module_AoT);
1131
+
1132
+ if (!is_bounds_checks_enabled(module_inst_comm)) {
1133
+ return true;
1134
+ }
1135
+
1136
+ memory_inst = wasm_get_default_memory(module_inst);
1137
+ if (!memory_inst) {
1138
+ goto fail;
1139
+ }
1140
+
1141
+ #if WASM_ENABLE_MEMORY64 != 0
1142
+ if (memory_inst->is_memory64)
1143
+ max_linear_memory_size = MAX_LINEAR_MEM64_MEMORY_SIZE;
1144
+ #endif
1145
+ /* boundary overflow check */
1146
+ if (size > max_linear_memory_size || (uintptr_t)addr > UINTPTR_MAX - size) {
1147
+ goto fail;
1148
+ }
1149
+
1150
+ #if WASM_ENABLE_SHARED_HEAP != 0
1151
+ if (is_native_addr_in_shared_heap(
1152
+ module_inst_comm, memory_inst->is_memory64, native_ptr, size)) {
1153
+ return true;
1154
+ }
1155
+ #endif
1156
+
1157
+ SHARED_MEMORY_LOCK(memory_inst);
1158
+
1159
+ if (memory_inst->memory_data <= addr
1160
+ && addr + size <= memory_inst->memory_data_end) {
1161
+ SHARED_MEMORY_UNLOCK(memory_inst);
1162
+ return true;
1163
+ }
1164
+
1165
+ SHARED_MEMORY_UNLOCK(memory_inst);
1166
+
1167
+ fail:
1168
+ wasm_set_exception(module_inst, "out of bounds memory access");
1169
+ return false;
1170
+ }
1171
+
1172
+ void *
1173
+ wasm_runtime_addr_app_to_native(WASMModuleInstanceCommon *module_inst_comm,
1174
+ uint64 app_offset)
1175
+ {
1176
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1177
+ WASMMemoryInstance *memory_inst;
1178
+ uint8 *addr;
1179
+ bool bounds_checks;
1180
+
1181
+ bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1182
+ || module_inst_comm->module_type == Wasm_Module_AoT);
1183
+
1184
+ bounds_checks = is_bounds_checks_enabled(module_inst_comm);
1185
+
1186
+ memory_inst = wasm_get_default_memory(module_inst);
1187
+ if (!memory_inst) {
1188
+ return NULL;
1189
+ }
1190
+
1191
+ #if WASM_ENABLE_SHARED_HEAP != 0
1192
+ if (is_app_addr_in_shared_heap(module_inst_comm, memory_inst->is_memory64,
1193
+ app_offset, 1)) {
1194
+ return get_last_used_shared_heap_base_addr_adj(module_inst_comm)
1195
+ + app_offset;
1196
+ }
1197
+ #endif
1198
+
1199
+ SHARED_MEMORY_LOCK(memory_inst);
1200
+
1201
+ addr = memory_inst->memory_data + (uintptr_t)app_offset;
1202
+
1203
+ if (bounds_checks) {
1204
+ if (memory_inst->memory_data <= addr
1205
+ && addr < memory_inst->memory_data_end) {
1206
+ SHARED_MEMORY_UNLOCK(memory_inst);
1207
+ return addr;
1208
+ }
1209
+ SHARED_MEMORY_UNLOCK(memory_inst);
1210
+ return NULL;
1211
+ }
1212
+
1213
+ /* If bounds checks is disabled, return the address directly */
1214
+ SHARED_MEMORY_UNLOCK(memory_inst);
1215
+ return addr;
1216
+ }
1217
+
1218
+ uint64
1219
+ wasm_runtime_addr_native_to_app(WASMModuleInstanceCommon *module_inst_comm,
1220
+ void *native_ptr)
1221
+ {
1222
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1223
+ WASMMemoryInstance *memory_inst;
1224
+ uint8 *addr = (uint8 *)native_ptr;
1225
+ bool bounds_checks;
1226
+ uint64 ret;
1227
+
1228
+ bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1229
+ || module_inst_comm->module_type == Wasm_Module_AoT);
1230
+
1231
+ bounds_checks = is_bounds_checks_enabled(module_inst_comm);
1232
+
1233
+ memory_inst = wasm_get_default_memory(module_inst);
1234
+ if (!memory_inst) {
1235
+ return 0;
1236
+ }
1237
+
1238
+ #if WASM_ENABLE_SHARED_HEAP != 0
1239
+ if (is_native_addr_in_shared_heap(module_inst_comm,
1240
+ memory_inst->is_memory64, addr, 1)) {
1241
+ return (uint64)(uintptr_t)(addr
1242
+ - get_last_used_shared_heap_base_addr_adj(
1243
+ module_inst_comm));
1244
+ }
1245
+ #endif
1246
+
1247
+ SHARED_MEMORY_LOCK(memory_inst);
1248
+
1249
+ if (bounds_checks) {
1250
+ if (memory_inst->memory_data <= addr
1251
+ && addr < memory_inst->memory_data_end) {
1252
+ ret = (uint64)(addr - memory_inst->memory_data);
1253
+ SHARED_MEMORY_UNLOCK(memory_inst);
1254
+ return ret;
1255
+ }
1256
+ }
1257
+ /* If bounds checks is disabled, return the offset directly */
1258
+ else if (addr != NULL) {
1259
+ ret = (uint64)(addr - memory_inst->memory_data);
1260
+ SHARED_MEMORY_UNLOCK(memory_inst);
1261
+ return ret;
1262
+ }
1263
+
1264
+ SHARED_MEMORY_UNLOCK(memory_inst);
1265
+ return 0;
1266
+ }
1267
+
1268
+ bool
1269
+ wasm_runtime_get_app_addr_range(WASMModuleInstanceCommon *module_inst_comm,
1270
+ uint64 app_offset, uint64 *p_app_start_offset,
1271
+ uint64 *p_app_end_offset)
1272
+ {
1273
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1274
+ WASMMemoryInstance *memory_inst;
1275
+ uint64 memory_data_size;
1276
+
1277
+ bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1278
+ || module_inst_comm->module_type == Wasm_Module_AoT);
1279
+
1280
+ memory_inst = wasm_get_default_memory(module_inst);
1281
+ if (!memory_inst) {
1282
+ return false;
1283
+ }
1284
+
1285
+ SHARED_MEMORY_LOCK(memory_inst);
1286
+
1287
+ memory_data_size = memory_inst->memory_data_size;
1288
+
1289
+ if (app_offset < memory_data_size) {
1290
+ if (p_app_start_offset)
1291
+ *p_app_start_offset = 0;
1292
+ if (p_app_end_offset)
1293
+ *p_app_end_offset = memory_data_size;
1294
+ SHARED_MEMORY_UNLOCK(memory_inst);
1295
+ return true;
1296
+ }
1297
+
1298
+ SHARED_MEMORY_UNLOCK(memory_inst);
1299
+ return false;
1300
+ }
1301
+
1302
+ bool
1303
+ wasm_runtime_get_native_addr_range(WASMModuleInstanceCommon *module_inst_comm,
1304
+ uint8 *native_ptr,
1305
+ uint8 **p_native_start_addr,
1306
+ uint8 **p_native_end_addr)
1307
+ {
1308
+ WASMModuleInstance *module_inst = (WASMModuleInstance *)module_inst_comm;
1309
+ WASMMemoryInstance *memory_inst;
1310
+ uint8 *addr = (uint8 *)native_ptr;
1311
+
1312
+ bh_assert(module_inst_comm->module_type == Wasm_Module_Bytecode
1313
+ || module_inst_comm->module_type == Wasm_Module_AoT);
1314
+
1315
+ memory_inst = wasm_get_default_memory(module_inst);
1316
+ if (!memory_inst) {
1317
+ return false;
1318
+ }
1319
+
1320
+ SHARED_MEMORY_LOCK(memory_inst);
1321
+
1322
+ if (memory_inst->memory_data <= addr
1323
+ && addr < memory_inst->memory_data_end) {
1324
+ if (p_native_start_addr)
1325
+ *p_native_start_addr = memory_inst->memory_data;
1326
+ if (p_native_end_addr)
1327
+ *p_native_end_addr = memory_inst->memory_data_end;
1328
+ SHARED_MEMORY_UNLOCK(memory_inst);
1329
+ return true;
1330
+ }
1331
+
1332
+ SHARED_MEMORY_UNLOCK(memory_inst);
1333
+ return false;
1334
+ }
1335
+
1336
+ bool
1337
+ wasm_check_app_addr_and_convert(WASMModuleInstance *module_inst, bool is_str,
1338
+ uint64 app_buf_addr, uint64 app_buf_size,
1339
+ void **p_native_addr)
1340
+ {
1341
+ WASMMemoryInstance *memory_inst = wasm_get_default_memory(module_inst);
1342
+ uint8 *native_addr;
1343
+ bool bounds_checks;
1344
+ #if WASM_ENABLE_SHARED_HEAP != 0
1345
+ uint8 *shared_heap_base_addr_adj = NULL;
1346
+ uintptr_t shared_heap_end_off = 0;
1347
+ #endif
1348
+
1349
+ bh_assert(app_buf_addr <= UINTPTR_MAX && app_buf_size <= UINTPTR_MAX);
1350
+
1351
+ if (!memory_inst) {
1352
+ wasm_set_exception(module_inst, "out of bounds memory access");
1353
+ return false;
1354
+ }
1355
+
1356
+ #if WASM_ENABLE_SHARED_HEAP != 0
1357
+ if (is_app_addr_in_shared_heap((WASMModuleInstanceCommon *)module_inst,
1358
+ memory_inst->is_memory64, app_buf_addr,
1359
+ app_buf_size)) {
1360
+ const char *str, *str_end;
1361
+ shared_heap_base_addr_adj = get_last_used_shared_heap_base_addr_adj(
1362
+ (WASMModuleInstanceCommon *)module_inst);
1363
+ shared_heap_end_off = get_last_used_shared_heap_end_offset(
1364
+ (WASMModuleInstanceCommon *)module_inst);
1365
+ native_addr = shared_heap_base_addr_adj + (uintptr_t)app_buf_addr;
1366
+
1367
+ /* The whole string must be in the shared heap */
1368
+ str = (const char *)native_addr;
1369
+ str_end =
1370
+ (const char *)shared_heap_base_addr_adj + shared_heap_end_off + 1;
1371
+ while (str < str_end && *str != '\0')
1372
+ str++;
1373
+ if (str == str_end) {
1374
+ wasm_set_exception(module_inst, "out of bounds memory access");
1375
+ return false;
1376
+ }
1377
+ else
1378
+ goto success;
1379
+ }
1380
+ #endif
1381
+
1382
+ native_addr = memory_inst->memory_data + (uintptr_t)app_buf_addr;
1383
+ bounds_checks =
1384
+ is_bounds_checks_enabled((WASMModuleInstanceCommon *)module_inst);
1385
+
1386
+ if (!bounds_checks) {
1387
+ if (app_buf_addr == 0) {
1388
+ native_addr = NULL;
1389
+ }
1390
+ goto success;
1391
+ }
1392
+
1393
+ /* No need to check the app_offset and buf_size if memory access
1394
+ boundary check with hardware trap is enabled */
1395
+ #ifndef OS_ENABLE_HW_BOUND_CHECK
1396
+ SHARED_MEMORY_LOCK(memory_inst);
1397
+
1398
+ if (app_buf_addr >= memory_inst->memory_data_size) {
1399
+ goto fail;
1400
+ }
1401
+
1402
+ if (!is_str) {
1403
+ if (app_buf_size > memory_inst->memory_data_size - app_buf_addr) {
1404
+ goto fail;
1405
+ }
1406
+ }
1407
+ else {
1408
+ const char *str, *str_end;
1409
+
1410
+ /* The whole string must be in the linear memory */
1411
+ str = (const char *)native_addr;
1412
+ str_end = (const char *)memory_inst->memory_data_end;
1413
+ while (str < str_end && *str != '\0')
1414
+ str++;
1415
+ if (str == str_end)
1416
+ goto fail;
1417
+ }
1418
+
1419
+ SHARED_MEMORY_UNLOCK(memory_inst);
1420
+ #endif
1421
+
1422
+ success:
1423
+ *p_native_addr = (void *)native_addr;
1424
+ return true;
1425
+
1426
+ #ifndef OS_ENABLE_HW_BOUND_CHECK
1427
+ fail:
1428
+ SHARED_MEMORY_UNLOCK(memory_inst);
1429
+ wasm_set_exception(module_inst, "out of bounds memory access");
1430
+ return false;
1431
+ #endif
1432
+ }
1433
+
1434
+ WASMMemoryInstance *
1435
+ wasm_get_default_memory(WASMModuleInstance *module_inst)
1436
+ {
1437
+ if (module_inst->memories)
1438
+ return module_inst->memories[0];
1439
+ else
1440
+ return NULL;
1441
+ }
1442
+
1443
+ WASMMemoryInstance *
1444
+ wasm_get_memory_with_idx(WASMModuleInstance *module_inst, uint32 index)
1445
+ {
1446
+ if ((index >= module_inst->memory_count) || !module_inst->memories)
1447
+ return NULL;
1448
+ return module_inst->memories[index];
1449
+ }
1450
+
1451
+ void
1452
+ wasm_runtime_set_mem_bound_check_bytes(WASMMemoryInstance *memory,
1453
+ uint64 memory_data_size)
1454
+ {
1455
+ #if WASM_ENABLE_FAST_JIT != 0 || WASM_ENABLE_JIT != 0 || WASM_ENABLE_AOT != 0
1456
+ #if UINTPTR_MAX == UINT64_MAX
1457
+ memory->mem_bound_check_1byte.u64 = memory_data_size - 1;
1458
+ memory->mem_bound_check_2bytes.u64 = memory_data_size - 2;
1459
+ memory->mem_bound_check_4bytes.u64 = memory_data_size - 4;
1460
+ memory->mem_bound_check_8bytes.u64 = memory_data_size - 8;
1461
+ memory->mem_bound_check_16bytes.u64 = memory_data_size - 16;
1462
+ #else
1463
+ memory->mem_bound_check_1byte.u32[0] = (uint32)memory_data_size - 1;
1464
+ memory->mem_bound_check_2bytes.u32[0] = (uint32)memory_data_size - 2;
1465
+ memory->mem_bound_check_4bytes.u32[0] = (uint32)memory_data_size - 4;
1466
+ memory->mem_bound_check_8bytes.u32[0] = (uint32)memory_data_size - 8;
1467
+ memory->mem_bound_check_16bytes.u32[0] = (uint32)memory_data_size - 16;
1468
+ #endif
1469
+ #endif
1470
+ }
1471
+
1472
+ static void
1473
+ wasm_munmap_linear_memory(void *mapped_mem, uint64 commit_size, uint64 map_size)
1474
+ {
1475
+ #ifdef BH_PLATFORM_WINDOWS
1476
+ os_mem_decommit(mapped_mem, commit_size);
1477
+ #else
1478
+ (void)commit_size;
1479
+ #endif
1480
+ os_munmap(mapped_mem, map_size);
1481
+ }
1482
+
1483
+ static void *
1484
+ wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
1485
+ uint64 commit_size)
1486
+ {
1487
+ void *new_mem;
1488
+
1489
+ bh_assert(new_size > 0);
1490
+ bh_assert(new_size > old_size);
1491
+
1492
+ #if UINTPTR_MAX == UINT32_MAX
1493
+ if (new_size == 4 * (uint64)BH_GB) {
1494
+ LOG_WARNING("On 32 bit platform, linear memory can't reach maximum "
1495
+ "size of 4GB\n");
1496
+ return NULL;
1497
+ }
1498
+ #endif
1499
+
1500
+ if (mapped_mem) {
1501
+ new_mem = os_mremap(mapped_mem, old_size, new_size);
1502
+ }
1503
+ else {
1504
+ new_mem = os_mmap(NULL, new_size, MMAP_PROT_NONE, MMAP_MAP_NONE,
1505
+ os_get_invalid_handle());
1506
+ }
1507
+ if (!new_mem) {
1508
+ return NULL;
1509
+ }
1510
+
1511
+ #ifdef BH_PLATFORM_WINDOWS
1512
+ if (commit_size > 0
1513
+ && !os_mem_commit(new_mem, commit_size,
1514
+ MMAP_PROT_READ | MMAP_PROT_WRITE)) {
1515
+ os_munmap(new_mem, new_size);
1516
+ return NULL;
1517
+ }
1518
+ #endif
1519
+
1520
+ if (os_mprotect(new_mem, commit_size, MMAP_PROT_READ | MMAP_PROT_WRITE)
1521
+ != 0) {
1522
+ wasm_munmap_linear_memory(new_mem, new_size, new_size);
1523
+ return NULL;
1524
+ }
1525
+
1526
+ return new_mem;
1527
+ }
1528
+
1529
+ static void *
1530
+ wasm_mmap_linear_memory(uint64 map_size, uint64 commit_size)
1531
+ {
1532
+ return wasm_mremap_linear_memory(NULL, 0, map_size, commit_size);
1533
+ }
1534
+
1535
+ static bool
1536
+ wasm_enlarge_memory_internal(WASMModuleInstanceCommon *module,
1537
+ WASMMemoryInstance *memory, uint32 inc_page_count)
1538
+ {
1539
+ #if WASM_ENABLE_SHARED_HEAP != 0
1540
+ WASMSharedHeap *shared_heap;
1541
+ #endif
1542
+ uint8 *memory_data_old, *memory_data_new, *heap_data_old;
1543
+ uint32 num_bytes_per_page, heap_size;
1544
+ uint32 cur_page_count, max_page_count, total_page_count;
1545
+ uint64 total_size_old = 0, total_size_new;
1546
+ bool ret = true, full_size_mmaped;
1547
+ enlarge_memory_error_reason_t failure_reason = INTERNAL_ERROR;
1548
+
1549
+ if (!memory) {
1550
+ ret = false;
1551
+ goto return_func;
1552
+ }
1553
+
1554
+ #ifdef OS_ENABLE_HW_BOUND_CHECK
1555
+ full_size_mmaped = true;
1556
+ #elif WASM_ENABLE_SHARED_MEMORY != 0
1557
+ full_size_mmaped = shared_memory_is_shared(memory);
1558
+ #else
1559
+ full_size_mmaped = false;
1560
+ #endif
1561
+
1562
+ memory_data_old = memory->memory_data;
1563
+ total_size_old = memory->memory_data_size;
1564
+
1565
+ heap_data_old = memory->heap_data;
1566
+ heap_size = (uint32)(memory->heap_data_end - memory->heap_data);
1567
+
1568
+ num_bytes_per_page = memory->num_bytes_per_page;
1569
+ cur_page_count = memory->cur_page_count;
1570
+ max_page_count = memory->max_page_count;
1571
+ total_page_count = inc_page_count + cur_page_count;
1572
+ total_size_new = num_bytes_per_page * (uint64)total_page_count;
1573
+
1574
+ if (inc_page_count <= 0)
1575
+ /* No need to enlarge memory */
1576
+ return true;
1577
+
1578
+ if (total_page_count < cur_page_count) { /* integer overflow */
1579
+ ret = false;
1580
+ goto return_func;
1581
+ }
1582
+
1583
+ if (total_page_count > max_page_count) {
1584
+ failure_reason = MAX_SIZE_REACHED;
1585
+ ret = false;
1586
+ goto return_func;
1587
+ }
1588
+
1589
+ #if WASM_ENABLE_SHARED_HEAP != 0
1590
+ shared_heap = get_shared_heap(module);
1591
+ if (shared_heap) {
1592
+ if (memory->is_memory64
1593
+ && total_size_new > shared_heap->start_off_mem64) {
1594
+ LOG_WARNING("Linear memory address is overlapped with shared heap");
1595
+ ret = false;
1596
+ goto return_func;
1597
+ }
1598
+ else if (!memory->is_memory64
1599
+ && total_size_new > shared_heap->start_off_mem32) {
1600
+ LOG_WARNING("Linear memory address is overlapped with shared heap");
1601
+ ret = false;
1602
+ goto return_func;
1603
+ }
1604
+ }
1605
+ #endif
1606
+
1607
+ bh_assert(total_size_new
1608
+ <= GET_MAX_LINEAR_MEMORY_SIZE(memory->is_memory64));
1609
+
1610
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
1611
+ if (!(memory_data_new =
1612
+ realloc_func(Alloc_For_LinearMemory, full_size_mmaped,
1613
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
1614
+ allocator_user_data,
1615
+ #endif
1616
+ memory_data_old, total_size_new))) {
1617
+ ret = false;
1618
+ goto return_func;
1619
+ }
1620
+ if (heap_size > 0) {
1621
+ if (mem_allocator_migrate(memory->heap_handle,
1622
+ (char *)heap_data_old
1623
+ + (memory_data_new - memory_data_old),
1624
+ heap_size)
1625
+ != 0) {
1626
+ ret = false;
1627
+ }
1628
+ }
1629
+ memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
1630
+ memory->heap_data_end = memory->heap_data + heap_size;
1631
+ memory->memory_data = memory_data_new;
1632
+ #else
1633
+ if (full_size_mmaped) {
1634
+ #ifdef BH_PLATFORM_WINDOWS
1635
+ if (!os_mem_commit(memory->memory_data_end,
1636
+ total_size_new - total_size_old,
1637
+ MMAP_PROT_READ | MMAP_PROT_WRITE)) {
1638
+ ret = false;
1639
+ goto return_func;
1640
+ }
1641
+ #endif
1642
+
1643
+ if (os_mprotect(memory->memory_data_end,
1644
+ total_size_new - total_size_old,
1645
+ MMAP_PROT_READ | MMAP_PROT_WRITE)
1646
+ != 0) {
1647
+ #ifdef BH_PLATFORM_WINDOWS
1648
+ os_mem_decommit(memory->memory_data_end,
1649
+ total_size_new - total_size_old);
1650
+ #endif
1651
+ ret = false;
1652
+ goto return_func;
1653
+ }
1654
+ }
1655
+ else {
1656
+ if (heap_size > 0) {
1657
+ if (mem_allocator_is_heap_corrupted(memory->heap_handle)) {
1658
+ wasm_runtime_show_app_heap_corrupted_prompt();
1659
+ ret = false;
1660
+ goto return_func;
1661
+ }
1662
+ }
1663
+
1664
+ if (!(memory_data_new =
1665
+ wasm_mremap_linear_memory(memory_data_old, total_size_old,
1666
+ total_size_new, total_size_new))) {
1667
+ ret = false;
1668
+ goto return_func;
1669
+ }
1670
+
1671
+ if (heap_size > 0) {
1672
+ if (mem_allocator_migrate(memory->heap_handle,
1673
+ (char *)heap_data_old
1674
+ + (memory_data_new - memory_data_old),
1675
+ heap_size)
1676
+ != 0) {
1677
+ /* Don't return here as memory->memory_data is obsolete and
1678
+ must be updated to be correctly used later. */
1679
+ ret = false;
1680
+ }
1681
+ }
1682
+
1683
+ memory->heap_data = memory_data_new + (heap_data_old - memory_data_old);
1684
+ memory->heap_data_end = memory->heap_data + heap_size;
1685
+ memory->memory_data = memory_data_new;
1686
+ #if defined(os_writegsbase)
1687
+ /* write base addr of linear memory to GS segment register */
1688
+ os_writegsbase(memory_data_new);
1689
+ #endif
1690
+ }
1691
+ #endif /* end of WASM_MEM_ALLOC_WITH_USAGE */
1692
+
1693
+ /*
1694
+ * AOT compiler assumes at least 8 byte alignment.
1695
+ * see aot_check_memory_overflow.
1696
+ */
1697
+ bh_assert(((uintptr_t)memory->memory_data & 0x7) == 0);
1698
+
1699
+ memory->num_bytes_per_page = num_bytes_per_page;
1700
+ memory->cur_page_count = total_page_count;
1701
+ memory->max_page_count = max_page_count;
1702
+ SET_LINEAR_MEMORY_SIZE(memory, total_size_new);
1703
+ memory->memory_data_end = memory->memory_data + total_size_new;
1704
+
1705
+ wasm_runtime_set_mem_bound_check_bytes(memory, total_size_new);
1706
+
1707
+ return_func:
1708
+ if (!ret && module && enlarge_memory_error_cb) {
1709
+ WASMExecEnv *exec_env = NULL;
1710
+
1711
+ #if WASM_ENABLE_INTERP != 0
1712
+ if (module->module_type == Wasm_Module_Bytecode)
1713
+ exec_env = ((WASMModuleInstance *)module)->cur_exec_env;
1714
+ #endif
1715
+ #if WASM_ENABLE_AOT != 0
1716
+ if (module->module_type == Wasm_Module_AoT)
1717
+ exec_env = ((AOTModuleInstance *)module)->cur_exec_env;
1718
+ #endif
1719
+
1720
+ enlarge_memory_error_cb(inc_page_count, total_size_old, 0,
1721
+ failure_reason, module, exec_env,
1722
+ enlarge_memory_error_user_data);
1723
+ }
1724
+
1725
+ return ret;
1726
+ }
1727
+
1728
+ bool
1729
+ wasm_runtime_enlarge_memory(WASMModuleInstanceCommon *module_inst,
1730
+ uint64 inc_page_count)
1731
+ {
1732
+ if (inc_page_count > UINT32_MAX) {
1733
+ return false;
1734
+ }
1735
+
1736
+ #if WASM_ENABLE_AOT != 0
1737
+ if (module_inst->module_type == Wasm_Module_AoT) {
1738
+ return aot_enlarge_memory((AOTModuleInstance *)module_inst,
1739
+ (uint32)inc_page_count);
1740
+ }
1741
+ #endif
1742
+ #if WASM_ENABLE_INTERP != 0
1743
+ if (module_inst->module_type == Wasm_Module_Bytecode) {
1744
+ return wasm_enlarge_memory((WASMModuleInstance *)module_inst,
1745
+ (uint32)inc_page_count);
1746
+ }
1747
+ #endif
1748
+
1749
+ return false;
1750
+ }
1751
+
1752
+ void
1753
+ wasm_runtime_set_enlarge_mem_error_callback(
1754
+ const enlarge_memory_error_callback_t callback, void *user_data)
1755
+ {
1756
+ enlarge_memory_error_cb = callback;
1757
+ enlarge_memory_error_user_data = user_data;
1758
+ }
1759
+
1760
+ bool
1761
+ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
1762
+ {
1763
+ bool ret = false;
1764
+
1765
+ if (module->memory_count > 0) {
1766
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1767
+ shared_memory_lock(module->memories[0]);
1768
+ #endif
1769
+ ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
1770
+ module->memories[0], inc_page_count);
1771
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1772
+ shared_memory_unlock(module->memories[0]);
1773
+ #endif
1774
+ }
1775
+
1776
+ return ret;
1777
+ }
1778
+
1779
+ bool
1780
+ wasm_enlarge_memory_with_idx(WASMModuleInstance *module, uint32 inc_page_count,
1781
+ uint32 memidx)
1782
+ {
1783
+ bool ret = false;
1784
+
1785
+ if (memidx < module->memory_count) {
1786
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1787
+ shared_memory_lock(module->memories[memidx]);
1788
+ #endif
1789
+ ret = wasm_enlarge_memory_internal((WASMModuleInstanceCommon *)module,
1790
+ module->memories[memidx],
1791
+ inc_page_count);
1792
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1793
+ shared_memory_unlock(module->memories[memidx]);
1794
+ #endif
1795
+ }
1796
+
1797
+ return ret;
1798
+ }
1799
+
1800
+ WASMMemoryInstance *
1801
+ wasm_runtime_lookup_memory(WASMModuleInstanceCommon *module_inst,
1802
+ const char *name)
1803
+ {
1804
+ #if WASM_ENABLE_INTERP != 0
1805
+ if (module_inst->module_type == Wasm_Module_Bytecode)
1806
+ return wasm_lookup_memory((WASMModuleInstance *)module_inst, name);
1807
+ #endif
1808
+
1809
+ #if WASM_ENABLE_AOT != 0
1810
+ if (module_inst->module_type == Wasm_Module_AoT)
1811
+ return aot_lookup_memory((WASMModuleInstance *)module_inst, name);
1812
+ #endif
1813
+
1814
+ return NULL;
1815
+ }
1816
+
1817
+ WASMMemoryInstance *
1818
+ wasm_runtime_get_default_memory(WASMModuleInstanceCommon *module_inst)
1819
+ {
1820
+ #if WASM_ENABLE_INTERP != 0
1821
+ if (module_inst->module_type == Wasm_Module_Bytecode)
1822
+ return wasm_get_default_memory((WASMModuleInstance *)module_inst);
1823
+ #endif
1824
+
1825
+ #if WASM_ENABLE_AOT != 0
1826
+ if (module_inst->module_type == Wasm_Module_AoT)
1827
+ return aot_get_default_memory((AOTModuleInstance *)module_inst);
1828
+ #endif
1829
+
1830
+ return NULL;
1831
+ }
1832
+
1833
+ WASMMemoryInstance *
1834
+ wasm_runtime_get_memory(WASMModuleInstanceCommon *module_inst, uint32 index)
1835
+ {
1836
+ #if WASM_ENABLE_INTERP != 0
1837
+ if (module_inst->module_type == Wasm_Module_Bytecode)
1838
+ return wasm_get_memory_with_idx((WASMModuleInstance *)module_inst,
1839
+ index);
1840
+ #endif
1841
+
1842
+ #if WASM_ENABLE_AOT != 0
1843
+ if (module_inst->module_type == Wasm_Module_AoT)
1844
+ return aot_get_memory_with_idx((AOTModuleInstance *)module_inst, index);
1845
+ #endif
1846
+
1847
+ return NULL;
1848
+ }
1849
+
1850
+ uint64
1851
+ wasm_memory_get_cur_page_count(WASMMemoryInstance *memory)
1852
+ {
1853
+ return memory->cur_page_count;
1854
+ }
1855
+
1856
+ uint64
1857
+ wasm_memory_get_max_page_count(WASMMemoryInstance *memory)
1858
+ {
1859
+ return memory->max_page_count;
1860
+ }
1861
+
1862
+ uint64
1863
+ wasm_memory_get_bytes_per_page(WASMMemoryInstance *memory)
1864
+ {
1865
+ return memory->num_bytes_per_page;
1866
+ }
1867
+
1868
+ bool
1869
+ wasm_memory_get_shared(WASMMemoryInstance *memory)
1870
+ {
1871
+ return memory->is_shared_memory;
1872
+ }
1873
+
1874
+ void *
1875
+ wasm_memory_get_base_address(WASMMemoryInstance *memory)
1876
+ {
1877
+ return memory->memory_data;
1878
+ }
1879
+
1880
+ bool
1881
+ wasm_memory_enlarge(WASMMemoryInstance *memory, uint64 inc_page_count)
1882
+ {
1883
+ bool ret = false;
1884
+
1885
+ if (memory) {
1886
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1887
+ shared_memory_lock(memory);
1888
+ #endif
1889
+ ret =
1890
+ wasm_enlarge_memory_internal(NULL, memory, (uint32)inc_page_count);
1891
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1892
+ shared_memory_unlock(memory);
1893
+ #endif
1894
+ }
1895
+
1896
+ return ret;
1897
+ }
1898
+
1899
+ void
1900
+ wasm_deallocate_linear_memory(WASMMemoryInstance *memory_inst)
1901
+ {
1902
+ uint64 map_size;
1903
+
1904
+ bh_assert(memory_inst);
1905
+ bh_assert(memory_inst->memory_data);
1906
+
1907
+ #ifndef OS_ENABLE_HW_BOUND_CHECK
1908
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1909
+ if (shared_memory_is_shared(memory_inst)) {
1910
+ map_size = (uint64)memory_inst->num_bytes_per_page
1911
+ * memory_inst->max_page_count;
1912
+ }
1913
+ else
1914
+ #endif
1915
+ {
1916
+ map_size = (uint64)memory_inst->num_bytes_per_page
1917
+ * memory_inst->cur_page_count;
1918
+ }
1919
+ #else
1920
+ map_size = 8 * (uint64)BH_GB;
1921
+ #endif
1922
+
1923
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
1924
+ (void)map_size;
1925
+ free_func(Alloc_For_LinearMemory,
1926
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
1927
+ allocator_user_data,
1928
+ #endif
1929
+ memory_inst->memory_data);
1930
+ #else
1931
+ wasm_munmap_linear_memory(memory_inst->memory_data,
1932
+ memory_inst->memory_data_size, map_size);
1933
+ #endif
1934
+
1935
+ memory_inst->memory_data = NULL;
1936
+ }
1937
+
1938
+ int
1939
+ wasm_allocate_linear_memory(uint8 **data, bool is_shared_memory,
1940
+ bool is_memory64, uint64 num_bytes_per_page,
1941
+ uint64 init_page_count, uint64 max_page_count,
1942
+ uint64 *memory_data_size)
1943
+ {
1944
+ uint64 map_size, page_size;
1945
+
1946
+ bh_assert(data);
1947
+ bh_assert(memory_data_size);
1948
+
1949
+ #ifndef OS_ENABLE_HW_BOUND_CHECK
1950
+ #if WASM_ENABLE_SHARED_MEMORY != 0
1951
+ if (is_shared_memory) {
1952
+ /* Allocate maximum memory size when memory is shared */
1953
+ map_size = max_page_count * num_bytes_per_page;
1954
+ }
1955
+ else
1956
+ #endif
1957
+ {
1958
+ map_size = init_page_count * num_bytes_per_page;
1959
+ }
1960
+ #else /* else of OS_ENABLE_HW_BOUND_CHECK */
1961
+ /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
1962
+ * ea = i + memarg.offset
1963
+ * both i and memarg.offset are u32 in range 0 to 4G
1964
+ * so the range of ea is 0 to 8G
1965
+ */
1966
+ map_size = 8 * (uint64)BH_GB;
1967
+ #endif /* end of OS_ENABLE_HW_BOUND_CHECK */
1968
+
1969
+ page_size = os_getpagesize();
1970
+ *memory_data_size = init_page_count * num_bytes_per_page;
1971
+
1972
+ bh_assert(*memory_data_size <= GET_MAX_LINEAR_MEMORY_SIZE(is_memory64));
1973
+ *memory_data_size = align_as_and_cast(*memory_data_size, page_size);
1974
+
1975
+ if (map_size > 0) {
1976
+ #if WASM_MEM_ALLOC_WITH_USAGE != 0
1977
+ (void)wasm_mmap_linear_memory;
1978
+ if (!(*data = malloc_func(Alloc_For_LinearMemory,
1979
+ #if WASM_MEM_ALLOC_WITH_USER_DATA != 0
1980
+ allocator_user_data,
1981
+ #endif
1982
+ *memory_data_size))) {
1983
+ return BHT_ERROR;
1984
+ }
1985
+ #else
1986
+ if (!(*data = wasm_mmap_linear_memory(map_size, *memory_data_size))) {
1987
+ return BHT_ERROR;
1988
+ }
1989
+ #endif
1990
+ }
1991
+
1992
+ /*
1993
+ * AOT compiler assumes at least 8 byte alignment.
1994
+ * see aot_check_memory_overflow.
1995
+ */
1996
+ bh_assert(((uintptr_t)*data & 0x7) == 0);
1997
+
1998
+ return BHT_OK;
1999
+ }