Skip to content

Commit fabee3a

Browse files
authored
Tag GC-heap object writes with the GC-heap alias region (#13718)
The copying collector's fresh-object header initialization writes now use the canonical GC memflags (like the null collector), and the pre-interned funcref array-element store gets the GC-heap region, matching how those locations are read elsewhere. Note: these missing regions could not result in miscompiles previously because regionless stores are treated as memory fences by alias analysis.
1 parent 2dc3f44 commit fabee3a

23 files changed

Lines changed: 75 additions & 65 deletions

crates/cranelift/src/func_environ.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3818,8 +3818,11 @@ impl FuncEnvironment<'_> {
38183818
}
38193819
CheckedEntity::Array { initialized, .. } => {
38203820
if is_pre_interned_funcref {
3821+
let region = self.alias_regions.gc_heap_region(builder.func);
38213822
builder.ins().store(
3822-
ir::MemFlagsData::trusted().with_endianness(Endianness::Little),
3823+
ir::MemFlagsData::trusted()
3824+
.with_endianness(Endianness::Little)
3825+
.with_alias_region(Some(region)),
38233826
value,
38243827
elem_addr,
38253828
0,

crates/cranelift/src/func_environ/gc/copying.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,16 @@ impl CopyingCompiler {
172172
let heap_offset = uextend_i32_to_pointer_type(builder, pointer_type, gc_ref);
173173
let obj_ptr = builder.ins().iadd(base, heap_offset);
174174

175+
// These header writes target the GC heap, so tag them with the
176+
// GC-heap region (like the null collector does).
177+
let header_flags = func_env.gc_memflags(&mut builder.func);
178+
175179
// Write `VMGcHeader::kind` with inline trace info bits included.
176180
let kind_val = builder
177181
.ins()
178182
.iconst(ir::types::I32, i64::from(kind.as_u32() | reserved_bits));
179183
builder.ins().store(
180-
ir::MemFlagsData::trusted(),
184+
header_flags,
181185
kind_val,
182186
obj_ptr,
183187
i32::try_from(wasmtime_environ::VM_GC_HEADER_KIND_OFFSET).unwrap(),
@@ -186,15 +190,15 @@ impl CopyingCompiler {
186190
// Write `VMGcHeader::type_index`.
187191
let shared_ty = func_env.module_interned_to_shared_ty(&mut builder.cursor(), ty);
188192
builder.ins().store(
189-
ir::MemFlagsData::trusted(),
193+
header_flags,
190194
shared_ty,
191195
obj_ptr,
192196
i32::try_from(wasmtime_environ::VM_GC_HEADER_TYPE_INDEX_OFFSET).unwrap(),
193197
);
194198

195199
// Write `VMCopyingHeader::object_size`.
196200
builder.ins().istore32(
197-
ir::MemFlagsData::trusted(),
201+
header_flags,
198202
aligned_size_64,
199203
obj_ptr,
200204
i32::try_from(wasmtime_environ::VM_GC_HEADER_SIZE).unwrap(),

tests/disas/array-fill-funcref.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
;; @003b brif v44, block3, block2(v29)
6969
;;
7070
;; block2(v45: i64):
71-
;; @003b store.i32 notrap aligned little v41, v45
71+
;; @003b store.i32 notrap aligned little region4 v41, v45
7272
;; v56 = iconst.i64 4
7373
;; v57 = iadd v45, v56 ; v56 = 4
7474
;; @003b v48 = icmp eq v57, v42
@@ -129,7 +129,7 @@
129129
;; @0049 brif v44, block3, block2(v29)
130130
;;
131131
;; block2(v45: i64):
132-
;; @0049 store.i32 notrap aligned little v41, v45
132+
;; @0049 store.i32 notrap aligned little region4 v41, v45
133133
;; v55 = iconst.i64 4
134134
;; v56 = iadd v45, v55 ; v55 = 4
135135
;; @0049 v48 = icmp eq v56, v42
@@ -198,7 +198,7 @@
198198
;; @0057 brif v45, block3, block2(v30)
199199
;;
200200
;; block2(v46: i64):
201-
;; @0057 store.i32 notrap aligned little v42, v46
201+
;; @0057 store.i32 notrap aligned little region4 v42, v46
202202
;; v64 = iconst.i64 4
203203
;; v65 = iadd v46, v64 ; v64 = 4
204204
;; @0057 v49 = icmp eq v65, v43

tests/disas/gc/array-new-data.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@
134134
;; v142 = load.i64 notrap aligned readonly can_move region0 v0+8
135135
;; v143 = load.i64 notrap aligned readonly can_move region6 v142+32
136136
;; @0025 v48 = iadd v143, v31
137-
;; @0025 store notrap aligned v141, v48 ; v141 = -1476395002
137+
;; @0025 store user2 region7 v141, v48 ; v141 = -1476395002
138138
;; v144 = load.i64 notrap aligned readonly can_move region5 v0+40
139139
;; v145 = load.i32 notrap aligned readonly can_move v144
140-
;; @0025 store notrap aligned v145, v48+4
140+
;; @0025 store user2 region7 v145, v48+4
141141
;; v146 = band.i64 v29, v28 ; v28 = -16
142-
;; @0025 istore32 notrap aligned v146, v48+8
142+
;; @0025 istore32 user2 region7 v146, v48+8
143143
;; @0025 jump block4(v24, v48)
144144
;;
145145
;; block3 cold:

tests/disas/gc/array-new-default-anyref.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@
6060
;; v127 = load.i64 notrap aligned readonly can_move region0 v0+8
6161
;; v128 = load.i64 notrap aligned readonly can_move region4 v127+32
6262
;; @001f v36 = iadd v128, v19
63-
;; @001f store notrap aligned v126, v36 ; v126 = -1476394994
63+
;; @001f store user2 region5 v126, v36 ; v126 = -1476394994
6464
;; v129 = load.i64 notrap aligned readonly can_move region3 v0+40
6565
;; v130 = load.i32 notrap aligned readonly can_move v129
66-
;; @001f store notrap aligned v130, v36+4
66+
;; @001f store user2 region5 v130, v36+4
6767
;; v131 = band.i64 v17, v16 ; v16 = -16
68-
;; @001f istore32 notrap aligned v131, v36+8
68+
;; @001f istore32 user2 region5 v131, v36+8
6969
;; @001f jump block4(v12, v36)
7070
;;
7171
;; block3 cold:

tests/disas/gc/array-new-default-exnref.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@
6060
;; v127 = load.i64 notrap aligned readonly can_move region0 v0+8
6161
;; v128 = load.i64 notrap aligned readonly can_move region4 v127+32
6262
;; @001f v36 = iadd v128, v19
63-
;; @001f store notrap aligned v126, v36 ; v126 = -1476394994
63+
;; @001f store user2 region5 v126, v36 ; v126 = -1476394994
6464
;; v129 = load.i64 notrap aligned readonly can_move region3 v0+40
6565
;; v130 = load.i32 notrap aligned readonly can_move v129
66-
;; @001f store notrap aligned v130, v36+4
66+
;; @001f store user2 region5 v130, v36+4
6767
;; v131 = band.i64 v17, v16 ; v16 = -16
68-
;; @001f istore32 notrap aligned v131, v36+8
68+
;; @001f istore32 user2 region5 v131, v36+8
6969
;; @001f jump block4(v12, v36)
7070
;;
7171
;; block3 cold:

tests/disas/gc/array-new-default-externref.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@
6060
;; v127 = load.i64 notrap aligned readonly can_move region0 v0+8
6161
;; v128 = load.i64 notrap aligned readonly can_move region4 v127+32
6262
;; @001f v36 = iadd v128, v19
63-
;; @001f store notrap aligned v126, v36 ; v126 = -1476394994
63+
;; @001f store user2 region5 v126, v36 ; v126 = -1476394994
6464
;; v129 = load.i64 notrap aligned readonly can_move region3 v0+40
6565
;; v130 = load.i32 notrap aligned readonly can_move v129
66-
;; @001f store notrap aligned v130, v36+4
66+
;; @001f store user2 region5 v130, v36+4
6767
;; v131 = band.i64 v17, v16 ; v16 = -16
68-
;; @001f istore32 notrap aligned v131, v36+8
68+
;; @001f istore32 user2 region5 v131, v36+8
6969
;; @001f jump block4(v12, v36)
7070
;;
7171
;; block3 cold:

tests/disas/gc/array-new-default-f32.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
;; v130 = load.i64 notrap aligned readonly can_move region0 v0+8
6464
;; v131 = load.i64 notrap aligned readonly can_move region4 v130+32
6565
;; @001f v36 = iadd v131, v19
66-
;; @001f store notrap aligned v129, v36 ; v129 = -1476395002
66+
;; @001f store user2 region5 v129, v36 ; v129 = -1476395002
6767
;; v132 = load.i64 notrap aligned readonly can_move region3 v0+40
6868
;; v133 = load.i32 notrap aligned readonly can_move v132
69-
;; @001f store notrap aligned v133, v36+4
69+
;; @001f store user2 region5 v133, v36+4
7070
;; v134 = band.i64 v17, v16 ; v16 = -16
71-
;; @001f istore32 notrap aligned v134, v36+8
71+
;; @001f istore32 user2 region5 v134, v36+8
7272
;; @001f jump block4(v12, v36)
7373
;;
7474
;; block3 cold:

tests/disas/gc/array-new-default-f64.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
;; v130 = load.i64 notrap aligned readonly can_move region0 v0+8
6464
;; v131 = load.i64 notrap aligned readonly can_move region4 v130+32
6565
;; @001f v36 = iadd v131, v19
66-
;; @001f store notrap aligned v129, v36 ; v129 = -1476395002
66+
;; @001f store user2 region5 v129, v36 ; v129 = -1476395002
6767
;; v132 = load.i64 notrap aligned readonly can_move region3 v0+40
6868
;; v133 = load.i32 notrap aligned readonly can_move v132
69-
;; @001f store notrap aligned v133, v36+4
69+
;; @001f store user2 region5 v133, v36+4
7070
;; v134 = band.i64 v17, v16 ; v16 = -16
71-
;; @001f istore32 notrap aligned v134, v36+8
71+
;; @001f istore32 user2 region5 v134, v36+8
7272
;; @001f jump block4(v12, v36)
7373
;;
7474
;; block3 cold:

tests/disas/gc/array-new-default-funcref.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@
6363
;; v137 = load.i64 notrap aligned readonly can_move region0 v0+8
6464
;; v138 = load.i64 notrap aligned readonly can_move region4 v137+32
6565
;; @001f v36 = iadd v138, v19
66-
;; @001f store notrap aligned v136, v36 ; v136 = -1476395002
66+
;; @001f store user2 region5 v136, v36 ; v136 = -1476395002
6767
;; v139 = load.i64 notrap aligned readonly can_move region3 v0+40
6868
;; v140 = load.i32 notrap aligned readonly can_move v139
69-
;; @001f store notrap aligned v140, v36+4
69+
;; @001f store user2 region5 v140, v36+4
7070
;; v141 = band.i64 v17, v16 ; v16 = -16
71-
;; @001f istore32 notrap aligned v141, v36+8
71+
;; @001f istore32 user2 region5 v141, v36+8
7272
;; @001f jump block4(v12, v36)
7373
;;
7474
;; block3 cold:
@@ -115,7 +115,7 @@
115115
;; @001f brif v84, block6, block5(v65)
116116
;;
117117
;; block5(v85: i64):
118-
;; @001f store.i32 notrap aligned little v81, v85
118+
;; @001f store.i32 notrap aligned little region5 v81, v85
119119
;; v144 = iconst.i64 4
120120
;; v145 = iadd v85, v144 ; v144 = 4
121121
;; @001f v88 = icmp eq v145, v82

0 commit comments

Comments
 (0)