@@ -165,7 +165,7 @@ runtime_malloc(uint64 size)
165
165
WASMSharedHeap *
166
166
wasm_runtime_create_shared_heap (SharedHeapInitArgs * init_args )
167
167
{
168
- uint64 heap_struct_size = sizeof (WASMSharedHeap );
168
+ uint64 heap_struct_size = sizeof (WASMSharedHeap ), map_size ;
169
169
uint32 size = init_args -> size ;
170
170
WASMSharedHeap * heap ;
171
171
@@ -192,7 +192,18 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
192
192
goto fail3 ;
193
193
}
194
194
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 ))) {
196
207
goto fail3 ;
197
208
}
198
209
if (!mem_allocator_create_with_struct_and_pool (
@@ -213,7 +224,7 @@ wasm_runtime_create_shared_heap(SharedHeapInitArgs *init_args)
213
224
return heap ;
214
225
215
226
fail4 :
216
- wasm_munmap_linear_memory (heap -> base_addr , size , size );
227
+ wasm_munmap_linear_memory (heap -> base_addr , size , map_size );
217
228
fail3 :
218
229
wasm_runtime_free (heap -> heap_handle );
219
230
fail2 :
@@ -245,18 +256,52 @@ wasm_runtime_attach_shared_heap_internal(WASMModuleInstanceCommon *module_inst,
245
256
246
257
#if WASM_ENABLE_INTERP != 0
247
258
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 ) {
249
262
LOG_WARNING ("A shared heap is already attached" );
250
263
return false;
251
264
}
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 ];
254
278
#endif
279
+ #endif /* end of WASM_ENABLE_JIT != 0 */
280
+ }
281
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
255
282
#if WASM_ENABLE_AOT != 0
256
283
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 ];
259
302
#endif
303
+ }
304
+ #endif /* end of WASM_ENABLE_AOT != 0 */
260
305
261
306
return true;
262
307
}
@@ -277,14 +322,32 @@ wasm_runtime_detach_shared_heap_internal(WASMModuleInstanceCommon *module_inst)
277
322
{
278
323
#if WASM_ENABLE_INTERP != 0
279
324
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 ;
282
335
#endif
336
+ }
337
+ #endif /* end of WASM_ENABLE_INTERP != 0 */
283
338
#if WASM_ENABLE_AOT != 0
284
339
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 ;
287
347
#endif
348
+ e -> shared_heap_base_addr_adj = NULL ;
349
+ }
350
+ #endif /* end of WASM_ENABLE_AOT != 0 */
288
351
}
289
352
290
353
void
@@ -307,13 +370,21 @@ get_shared_heap(WASMModuleInstanceCommon *module_inst_comm)
307
370
#endif
308
371
#if WASM_ENABLE_AOT != 0
309
372
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 ;
312
377
}
313
378
#endif
314
379
return NULL ;
315
380
}
316
381
382
+ WASMSharedHeap *
383
+ wasm_runtime_get_shared_heap (WASMModuleInstanceCommon * module_inst_comm )
384
+ {
385
+ return get_shared_heap (module_inst_comm );
386
+ }
387
+
317
388
static bool
318
389
is_app_addr_in_shared_heap (WASMModuleInstanceCommon * module_inst ,
319
390
bool is_memory64 , uint64 app_offset , uint32 bytes )
@@ -324,6 +395,10 @@ is_app_addr_in_shared_heap(WASMModuleInstanceCommon *module_inst,
324
395
return false;
325
396
}
326
397
398
+ if (bytes == 0 ) {
399
+ bytes = 1 ;
400
+ }
401
+
327
402
if (!is_memory64 ) {
328
403
if (app_offset >= heap -> start_off_mem32
329
404
&& app_offset <= UINT32_MAX - bytes + 1 ) {
@@ -457,17 +532,23 @@ wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
457
532
458
533
#if WASM_ENABLE_SHARED_HEAP != 0
459
534
static void
460
- wasm_runtime_destroy_shared_heaps ()
535
+ destroy_shared_heaps ()
461
536
{
462
537
WASMSharedHeap * heap = shared_heap_list ;
463
538
WASMSharedHeap * cur ;
539
+ uint64 map_size ;
464
540
465
541
while (heap ) {
466
542
cur = heap ;
467
543
heap = heap -> next ;
468
544
mem_allocator_destroy (cur -> heap_handle );
469
545
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 );
471
552
wasm_runtime_free (cur );
472
553
}
473
554
}
477
558
wasm_runtime_memory_destroy (void )
478
559
{
479
560
#if WASM_ENABLE_SHARED_HEAP != 0
480
- wasm_runtime_destroy_shared_heaps ();
561
+ destroy_shared_heaps ();
481
562
#endif
482
563
483
564
if (memory_mode == MEMORY_MODE_POOL ) {
@@ -1178,7 +1259,7 @@ wasm_mremap_linear_memory(void *mapped_mem, uint64 old_size, uint64 new_size,
1178
1259
}
1179
1260
1180
1261
static void *
1181
- wasm_mmap_linear_memory (uint64_t map_size , uint64 commit_size )
1262
+ wasm_mmap_linear_memory (uint64 map_size , uint64 commit_size )
1182
1263
{
1183
1264
return wasm_mremap_linear_memory (NULL , 0 , map_size , commit_size );
1184
1265
}
0 commit comments