Skip to content

Commit a0505b1

Browse files
authored
winch: Use correct heap types in explicit bounds checks (#8157)
This commit is a follow-up to #8059. Instead of arbitrarily using the target's pointer size, it derives the use from the heap information, in order to do bounds check calculations, this enables checking the right limits.
1 parent 7baeafb commit a0505b1

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

winch/codegen/src/codegen/bounds.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub(crate) fn ensure_index_and_offset<M: MacroAssembler>(
125125
masm: &mut M,
126126
index: Index,
127127
offset: u64,
128-
ptr_size: OperandSize,
128+
heap_ty_size: OperandSize,
129129
) -> ImmOffset {
130130
match u32::try_from(offset) {
131131
// If the immediate offset fits in a u32, then we simply return.
@@ -137,7 +137,7 @@ pub(crate) fn ensure_index_and_offset<M: MacroAssembler>(
137137
index.as_typed_reg().into(),
138138
index.as_typed_reg().into(),
139139
RegImm::i64(offset as i64),
140-
ptr_size,
140+
heap_ty_size,
141141
TrapCode::HeapOutOfBounds,
142142
);
143143

winch/codegen/src/codegen/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ where
492492
let memory_index = MemoryIndex::from_u32(memarg.memory);
493493
let heap = self.env.resolve_heap(memory_index);
494494
let index = Index::from_typed_reg(self.context.pop_to_reg(self.masm, None));
495-
let offset = bounds::ensure_index_and_offset(self.masm, index, memarg.offset, ptr_size);
495+
let offset =
496+
bounds::ensure_index_and_offset(self.masm, index, memarg.offset, heap.ty.into());
496497
let offset_with_access_size = add_offset_and_access_size(offset, access_size);
497498

498499
let addr = match heap.style {
@@ -528,7 +529,7 @@ where
528529
index_offset_and_access_size,
529530
index_offset_and_access_size,
530531
RegImm::i64(offset_with_access_size as i64),
531-
ptr_size,
532+
heap.ty.into(),
532533
TrapCode::HeapOutOfBounds,
533534
);
534535

@@ -627,7 +628,11 @@ where
627628
|masm, bounds, index| {
628629
let adjusted_bounds = bounds.as_u64() - offset_with_access_size;
629630
let index_reg = index.as_typed_reg().reg;
630-
masm.cmp(RegImm::i64(adjusted_bounds as i64), index_reg, ptr_size);
631+
masm.cmp(
632+
RegImm::i64(adjusted_bounds as i64),
633+
index_reg,
634+
heap.ty.into(),
635+
);
631636
IntCmpKind::GtU
632637
},
633638
);

0 commit comments

Comments
 (0)