@@ -1016,37 +1016,49 @@ impl<'module_environment> FuncEnvironment<'module_environment> {
1016
1016
}
1017
1017
}
1018
1018
1019
- /// Add one level of indirection to a pointer-and-memtype pair:
1020
- /// generate a load in the code at the specified offset, and if
1021
- /// memtypes are in use, add a field to the original struct and
1022
- /// generate a new memtype for the pointee.
1023
- fn load_pointer_with_memtypes (
1019
+ /// Create an `ir::Global` that does `load(ptr + offset)` and, when PCC and
1020
+ /// memory types are enabled, adds a field to the pointer's memory type for
1021
+ /// this value we are loading.
1022
+ pub ( crate ) fn global_load_with_memory_type (
1024
1023
& mut self ,
1025
1024
func : & mut ir:: Function ,
1025
+ ptr : ir:: GlobalValue ,
1026
1026
offset : u32 ,
1027
- readonly : bool ,
1028
- memtype : Option < ir:: MemoryType > ,
1027
+ flags : ir :: MemFlags ,
1028
+ ptr_mem_ty : Option < ir:: MemoryType > ,
1029
1029
) -> ( ir:: GlobalValue , Option < ir:: MemoryType > ) {
1030
- let vmctx = self . vmctx ( func) ;
1031
1030
let pointee = func. create_global_value ( ir:: GlobalValueData :: Load {
1032
- base : vmctx ,
1031
+ base : ptr ,
1033
1032
offset : Offset32 :: new ( i32:: try_from ( offset) . unwrap ( ) ) ,
1034
1033
global_type : self . pointer_type ( ) ,
1035
- flags : MemFlags :: trusted ( ) . with_readonly ( ) . with_can_move ( ) ,
1034
+ flags,
1036
1035
} ) ;
1037
1036
1038
- let mt = memtype . map ( |mt | {
1039
- let pointee_mt = self . create_empty_struct_memtype ( func) ;
1040
- self . add_field_to_memtype ( func, mt , offset, pointee_mt , readonly) ;
1037
+ let pointee_mem_ty = ptr_mem_ty . map ( |ptr_mem_ty | {
1038
+ let pointee_mem_ty = self . create_empty_struct_memtype ( func) ;
1039
+ self . add_field_to_memtype ( func, ptr_mem_ty , offset, pointee_mem_ty , flags . readonly ( ) ) ;
1041
1040
func. global_value_facts [ pointee] = Some ( Fact :: Mem {
1042
- ty : pointee_mt ,
1041
+ ty : pointee_mem_ty ,
1043
1042
min_offset : 0 ,
1044
1043
max_offset : 0 ,
1045
1044
nullable : false ,
1046
1045
} ) ;
1047
- pointee_mt
1046
+ pointee_mem_ty
1048
1047
} ) ;
1049
- ( pointee, mt)
1048
+
1049
+ ( pointee, pointee_mem_ty)
1050
+ }
1051
+
1052
+ /// Like `global_load_with_memory_type` but specialized for loads out of the
1053
+ /// `vmctx`.
1054
+ pub ( crate ) fn global_load_from_vmctx_with_memory_type (
1055
+ & mut self ,
1056
+ func : & mut ir:: Function ,
1057
+ offset : u32 ,
1058
+ flags : ir:: MemFlags ,
1059
+ ) -> ( ir:: GlobalValue , Option < ir:: MemoryType > ) {
1060
+ let vmctx = self . vmctx ( func) ;
1061
+ self . global_load_with_memory_type ( func, vmctx, offset, flags, self . pcc_vmctx_memtype )
1050
1062
}
1051
1063
1052
1064
/// Helper to emit a conditional trap based on `trap_cond`.
@@ -2330,7 +2342,7 @@ impl FuncEnvironment<'_> {
2330
2342
let memory = self . module . memories [ index] ;
2331
2343
let is_shared = memory. shared ;
2332
2344
2333
- let ( ptr , base_offset, current_length_offset, ptr_memtype) = {
2345
+ let ( base_ptr , base_offset, current_length_offset, ptr_memtype) = {
2334
2346
let vmctx = self . vmctx ( func) ;
2335
2347
if let Some ( def_index) = self . module . defined_memory_index ( index) {
2336
2348
if is_shared {
@@ -2339,11 +2351,10 @@ impl FuncEnvironment<'_> {
2339
2351
// VMMemoryDefinition` to it and dereference that when
2340
2352
// atomically growing it.
2341
2353
let from_offset = self . offsets . vmctx_vmmemory_pointer ( def_index) ;
2342
- let ( memory, def_mt) = self . load_pointer_with_memtypes (
2354
+ let ( memory, def_mt) = self . global_load_from_vmctx_with_memory_type (
2343
2355
func,
2344
2356
from_offset,
2345
- true ,
2346
- self . pcc_vmctx_memtype ,
2357
+ ir:: MemFlags :: trusted ( ) . with_readonly ( ) . with_can_move ( ) ,
2347
2358
) ;
2348
2359
let base_offset = i32:: from ( self . offsets . ptr . vmmemory_definition_base ( ) ) ;
2349
2360
let current_length_offset =
@@ -2367,11 +2378,10 @@ impl FuncEnvironment<'_> {
2367
2378
}
2368
2379
} else {
2369
2380
let from_offset = self . offsets . vmctx_vmmemory_import_from ( index) ;
2370
- let ( memory, def_mt) = self . load_pointer_with_memtypes (
2381
+ let ( memory, def_mt) = self . global_load_from_vmctx_with_memory_type (
2371
2382
func,
2372
2383
from_offset,
2373
- true ,
2374
- self . pcc_vmctx_memtype ,
2384
+ ir:: MemFlags :: trusted ( ) . with_readonly ( ) . with_can_move ( ) ,
2375
2385
) ;
2376
2386
let base_offset = i32:: from ( self . offsets . ptr . vmmemory_definition_base ( ) ) ;
2377
2387
let current_length_offset =
@@ -2380,13 +2390,66 @@ impl FuncEnvironment<'_> {
2380
2390
}
2381
2391
} ;
2382
2392
2383
- let heap_bound = func. create_global_value ( ir:: GlobalValueData :: Load {
2384
- base : ptr ,
2393
+ let bound = func. create_global_value ( ir:: GlobalValueData :: Load {
2394
+ base : base_ptr ,
2385
2395
offset : Offset32 :: new ( current_length_offset) ,
2386
2396
global_type : pointer_type,
2387
2397
flags : MemFlags :: trusted ( ) ,
2388
2398
} ) ;
2389
2399
2400
+ let ( base_fact, pcc_memory_type) = self . make_pcc_base_fact_and_type_for_memory (
2401
+ func,
2402
+ memory,
2403
+ base_offset,
2404
+ current_length_offset,
2405
+ ptr_memtype,
2406
+ bound,
2407
+ ) ;
2408
+
2409
+ let base = self . make_heap_base ( func, memory, base_ptr, base_offset, base_fact) ;
2410
+
2411
+ Ok ( self . heaps . push ( HeapData {
2412
+ base,
2413
+ bound,
2414
+ pcc_memory_type,
2415
+ memory,
2416
+ } ) )
2417
+ }
2418
+
2419
+ pub ( crate ) fn make_heap_base (
2420
+ & self ,
2421
+ func : & mut Function ,
2422
+ memory : Memory ,
2423
+ ptr : ir:: GlobalValue ,
2424
+ offset : i32 ,
2425
+ fact : Option < Fact > ,
2426
+ ) -> ir:: GlobalValue {
2427
+ let pointer_type = self . pointer_type ( ) ;
2428
+
2429
+ let mut flags = ir:: MemFlags :: trusted ( ) . with_checked ( ) . with_can_move ( ) ;
2430
+ if !memory. memory_may_move ( self . tunables ) {
2431
+ flags. set_readonly ( ) ;
2432
+ }
2433
+
2434
+ let heap_base = func. create_global_value ( ir:: GlobalValueData :: Load {
2435
+ base : ptr,
2436
+ offset : Offset32 :: new ( offset) ,
2437
+ global_type : pointer_type,
2438
+ flags,
2439
+ } ) ;
2440
+ func. global_value_facts [ heap_base] = fact;
2441
+ heap_base
2442
+ }
2443
+
2444
+ pub ( crate ) fn make_pcc_base_fact_and_type_for_memory (
2445
+ & mut self ,
2446
+ func : & mut Function ,
2447
+ memory : Memory ,
2448
+ base_offset : i32 ,
2449
+ current_length_offset : i32 ,
2450
+ ptr_memtype : Option < ir:: MemoryType > ,
2451
+ heap_bound : ir:: GlobalValue ,
2452
+ ) -> ( Option < Fact > , Option < ir:: MemoryType > ) {
2390
2453
// If we have a declared maximum, we can make this a "static" heap, which is
2391
2454
// allocated up front and never moved.
2392
2455
let host_page_size_log2 = self . target_config ( ) . page_size_align_log2 ;
@@ -2493,25 +2556,7 @@ impl FuncEnvironment<'_> {
2493
2556
( None , None )
2494
2557
}
2495
2558
} ;
2496
-
2497
- let mut flags = MemFlags :: trusted ( ) . with_checked ( ) . with_can_move ( ) ;
2498
- if !memory. memory_may_move ( self . tunables ) {
2499
- flags. set_readonly ( ) ;
2500
- }
2501
- let heap_base = func. create_global_value ( ir:: GlobalValueData :: Load {
2502
- base : ptr,
2503
- offset : Offset32 :: new ( base_offset) ,
2504
- global_type : pointer_type,
2505
- flags,
2506
- } ) ;
2507
- func. global_value_facts [ heap_base] = base_fact;
2508
-
2509
- Ok ( self . heaps . push ( HeapData {
2510
- base : heap_base,
2511
- bound : heap_bound,
2512
- pcc_memory_type : memory_type,
2513
- memory,
2514
- } ) )
2559
+ ( base_fact, memory_type)
2515
2560
}
2516
2561
2517
2562
pub fn make_global (
@@ -3080,15 +3125,11 @@ impl FuncEnvironment<'_> {
3080
3125
Ok ( ( ) )
3081
3126
}
3082
3127
3083
- pub fn before_unconditionally_trapping_memory_access (
3084
- & mut self ,
3085
- builder : & mut FunctionBuilder ,
3086
- ) -> WasmResult < ( ) > {
3128
+ pub fn before_unconditionally_trapping_memory_access ( & mut self , builder : & mut FunctionBuilder ) {
3087
3129
if self . tunables . consume_fuel {
3088
3130
self . fuel_increment_var ( builder) ;
3089
3131
self . fuel_save_from_var ( builder) ;
3090
3132
}
3091
- Ok ( ( ) )
3092
3133
}
3093
3134
3094
3135
pub fn before_translate_function (
0 commit comments