@@ -165,7 +165,7 @@ runtime_malloc(uint64 size)
165165WASMSharedHeap *
166166wasm_runtime_create_shared_heap (SharedHeapInitArgs * init_args )
167167{
168- uint64 heap_struct_size = sizeof (WASMSharedHeap );
168+ uint64 heap_struct_size = sizeof (WASMSharedHeap ), map_size ;
169169 uint32 size = init_args -> size ;
170170 WASMSharedHeap * heap ;
171171
@@ -192,7 +192,18 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
192192 goto fail3 ;
193193 }
194194
195- if (!(heap -> base_addr = wasm_mmap_linear_memory (size , size ))) {
195+ #ifndef OS_ENABLE_HW_BOUND_CHECK
196+ map_size = size ;
197+ #else
198+ /* Totally 8G is mapped, the opcode load/store address range is 0 to 8G:
199+ * ea = i + memarg.offset
200+ * both i and memarg.offset are u32 in range 0 to 4G
201+ * so the range of ea is 0 to 8G
202+ */
203+ map_size = 8 * (uint64 )BH_GB ;
204+ #endif
205+
206+ if (!(heap -> base_addr = wasm_mmap_linear_memory (map_size , size ))) {
196207 goto fail3 ;
197208 }
198209 if (!mem_allocator_create_with_struct_and_pool (
@@ -213,7 +224,7 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
213224 return heap ;
214225
215226fail4 :
216- wasm_munmap_linear_memory (heap -> base_addr , size , size );
227+ wasm_munmap_linear_memory (heap -> base_addr , size , map_size );
217228fail3 :
218229 wasm_runtime_free (heap -> heap_handle );
219230fail2 :
@@ -245,18 +256,52 @@ wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
245256
246257#if WASM_ENABLE_INTERP != 0
247258 if (module_inst -> module_type == Wasm_Module_Bytecode ) {
248- if (((WASMModuleInstance * )module_inst )-> e -> shared_heap ) {
259+ WASMModuleInstanceExtra * e =
260+ (WASMModuleInstanceExtra * )((WASMModuleInstance * )module_inst )-> e ;
261+ if (e -> shared_heap ) {
249262 LOG_WARNING ("A shared heap is already attached" );
250263 return false;
251264 }
252- ((WASMModuleInstance * )module_inst )-> e -> shared_heap = shared_heap ;
253- }
265+ e -> shared_heap = shared_heap ;
266+ #if WASM_ENABLE_JIT != 0
267+ #if UINTPTR_MAX == UINT64_MAX
268+ if (memory -> is_memory64 )
269+ e -> shared_heap_start_off .u64 = shared_heap -> start_off_mem64 ;
270+ else
271+ e -> shared_heap_start_off .u64 = shared_heap -> start_off_mem32 ;
272+ e -> shared_heap_base_addr_adj =
273+ shared_heap -> base_addr - e -> shared_heap_start_off .u64 ;
274+ #else
275+ e -> shared_heap_start_off .u32 [0 ] = (uint32 )shared_heap -> start_off_mem32 ;
276+ e -> shared_heap_base_addr_adj =
277+ shared_heap -> base_addr - e -> shared_heap_start_off .u32 [0 ];
254278#endif
279+ #endif /* end of WASM_ENABLE_JIT != 0 */
280+ }
281+ #endif /* end of WASM_ENABLE_INTERP != 0 */
255282#if WASM_ENABLE_AOT != 0
256283 if (module_inst -> module_type == Wasm_Module_AoT ) {
257- // TODO
258- }
284+ AOTModuleInstanceExtra * e =
285+ (AOTModuleInstanceExtra * )((AOTModuleInstance * )module_inst )-> e ;
286+ if (e -> shared_heap ) {
287+ LOG_WARNING ("A shared heap is already attached" );
288+ return false;
289+ }
290+ e -> shared_heap = shared_heap ;
291+ #if UINTPTR_MAX == UINT64_MAX
292+ if (memory -> is_memory64 )
293+ e -> shared_heap_start_off .u64 = shared_heap -> start_off_mem64 ;
294+ else
295+ e -> shared_heap_start_off .u64 = shared_heap -> start_off_mem32 ;
296+ e -> shared_heap_base_addr_adj =
297+ shared_heap -> base_addr - e -> shared_heap_start_off .u64 ;
298+ #else
299+ e -> shared_heap_start_off .u32 [0 ] = (uint32 )shared_heap -> start_off_mem32 ;
300+ e -> shared_heap_base_addr_adj =
301+ shared_heap -> base_addr - e -> shared_heap_start_off .u32 [0 ];
259302#endif
303+ }
304+ #endif /* end of WASM_ENABLE_AOT != 0 */
260305
261306 return true;
262307}
@@ -277,14 +322,32 @@ wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
277322{
278323#if WASM_ENABLE_INTERP != 0
279324 if (module_inst -> module_type == Wasm_Module_Bytecode ) {
280- ((WASMModuleInstance * )module_inst )-> e -> shared_heap = NULL ;
281- }
325+ WASMModuleInstanceExtra * e =
326+ (WASMModuleInstanceExtra * )((WASMModuleInstance * )module_inst )-> e ;
327+ e -> shared_heap = NULL ;
328+ #if WASM_ENABLE_JIT != 0
329+ #if UINTPTR_MAX == UINT64_MAX
330+ e -> shared_heap_start_off .u64 = UINT64_MAX ;
331+ #else
332+ e -> shared_heap_start_off .u32 [0 ] = UINT32_MAX ;
333+ #endif
334+ e -> shared_heap_base_addr_adj = NULL ;
282335#endif
336+ }
337+ #endif /* end of WASM_ENABLE_INTERP != 0 */
283338#if WASM_ENABLE_AOT != 0
284339 if (module_inst -> module_type == Wasm_Module_AoT ) {
285- // TODO
286- }
340+ AOTModuleInstanceExtra * e =
341+ (AOTModuleInstanceExtra * )((AOTModuleInstance * )module_inst )-> e ;
342+ e -> shared_heap = NULL ;
343+ #if UINTPTR_MAX == UINT64_MAX
344+ e -> shared_heap_start_off .u64 = UINT64_MAX ;
345+ #else
346+ e -> shared_heap_start_off .u32 [0 ] = UINT32_MAX ;
287347#endif
348+ e -> shared_heap_base_addr_adj = NULL ;
349+ }
350+ #endif /* end of WASM_ENABLE_AOT != 0 */
288351}
289352
290353void
@@ -307,13 +370,21 @@ get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
307370#endif
308371#if WASM_ENABLE_AOT != 0
309372 if (module_inst_comm -> module_type == Wasm_Module_AoT ) {
310- // TODO
311- return NULL ;
373+ AOTModuleInstanceExtra * e =
374+ (AOTModuleInstanceExtra * )((AOTModuleInstance * )module_inst_comm )
375+ -> e ;
376+ return e -> shared_heap ;
312377 }
313378#endif
314379 return NULL ;
315380}
316381
382+ WASMSharedHeap *
383+ wasm_runtime_get_shared_heap (WASMModuleInstanceCommon * module_inst_comm )
384+ {
385+ return get_shared_heap (module_inst_comm );
386+ }
387+
317388static bool
318389is_app_addr_in_shared_heap (WASMModuleInstanceCommon * module_inst ,
319390 bool is_memory64 , uint64 app_offset , uint32 bytes )
@@ -324,6 +395,10 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
324395 return false;
325396 }
326397
398+ if (bytes == 0 ) {
399+ bytes = 1 ;
400+ }
401+
327402 if (!is_memory64 ) {
328403 if (app_offset >= heap -> start_off_mem32
329404 && app_offset <= UINT32_MAX - bytes + 1 ) {
@@ -457,17 +532,23 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
457532
458533#if WASM_ENABLE_SHARED_HEAP != 0
459534static void
460- wasm_runtime_destroy_shared_heaps ()
535+ destroy_shared_heaps ()
461536{
462537 WASMSharedHeap * heap = shared_heap_list ;
463538 WASMSharedHeap * cur ;
539+ uint64 map_size ;
464540
465541 while (heap ) {
466542 cur = heap ;
467543 heap = heap -> next ;
468544 mem_allocator_destroy (cur -> heap_handle );
469545 wasm_runtime_free (cur -> heap_handle );
470- wasm_munmap_linear_memory (cur -> base_addr , cur -> size , cur -> size );
546+ #ifndef OS_ENABLE_HW_BOUND_CHECK
547+ map_size = cur -> size ;
548+ #else
549+ map_size = 8 * (uint64 )BH_GB ;
550+ #endif
551+ wasm_munmap_linear_memory (cur -> base_addr , cur -> size , map_size );
471552 wasm_runtime_free (cur );
472553 }
473554}
477558wasm_runtime_memory_destroy (void )
478559{
479560#if WASM_ENABLE_SHARED_HEAP != 0
480- wasm_runtime_destroy_shared_heaps ();
561+ destroy_shared_heaps ();
481562#endif
482563
483564 if (memory_mode == MEMORY_MODE_POOL ) {
@@ -1178,7 +1259,7 @@ wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
11781259}
11791260
11801261static void *
1181- wasm_mmap_linear_memory (uint64_t map_size , uint64 commit_size )
1262+ wasm_mmap_linear_memory (uint64 map_size , uint64 commit_size )
11821263{
11831264 return wasm_mremap_linear_memory (NULL , 0 , map_size , commit_size );
11841265}
0 commit comments