Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift: fix several integer-arithmetic overflows for large stack frames/stack slots. (#14651)

Chris Fallin3d973a6d1cea
diff --git a/cranelift/codegen/src/isa/aarch64/inst.isle b/cranelift/codegen/src/isa/aarch64/inst.isle
index 26fd2a9020..c10eafba12 100644
--- a/cranelift/codegen/src/isa/aarch64/inst.isle
+++ b/cranelift/codegen/src/isa/aarch64/inst.isle
@@ -4143,8 +4143,8 @@
(rule 3
(amode ty (stack_addr _ slot offset1) offset2)
- (AMode.SlotOffset
- (abi_stackslot_offset_into_slot_region slot offset1 offset2)))
+ (if-let slot_offset (abi_stackslot_offset_into_slot_region slot offset1 offset2))
+ (AMode.SlotOffset slot_offset))
(attr amode_no_more_iconst (veri chain))
(decl amode_no_more_iconst (Type Value i32) AMode)
diff --git a/cranelift/codegen/src/isa/x64/inst.isle b/cranelift/codegen/src/isa/x64/inst.isle
index bc28c4c1d2..c571d34993 100644
--- a/cranelift/codegen/src/isa/x64/inst.isle
+++ b/cranelift/codegen/src/isa/x64/inst.isle
@@ -435,11 +435,6 @@
(decl synthetic_amode_slot (i32) SyntheticAmode)
(extern constructor synthetic_amode_slot synthetic_amode_slot)
-;; Helper for loads/stores to/from stackslots.
-(decl stackslot_amode (StackSlot Offset32 Offset32) SyntheticAmode)
-(rule (stackslot_amode slot offset1 offset2)
- (let ((slot_offset i32 (abi_stackslot_offset_into_slot_region slot offset1 offset2)))
- (synthetic_amode_slot slot_offset)))
;; An `Amode` represents a possible addressing mode that can be used
;; in instructions. These denote a 64-bit value only.
@@ -539,7 +534,8 @@
(rule 2
(to_amode flags (stack_addr _ slot offset1) offset2)
- (stackslot_amode slot offset1 offset2))
+ (if-let slot_offset (abi_stackslot_offset_into_slot_region slot offset1 offset2))
+ (synthetic_amode_slot slot_offset))
;; Same as `to_amode`, except that the base address is computed via the addition
;; of the two `Value` arguments provided.
diff --git a/cranelift/codegen/src/machinst/abi.rs b/cranelift/codegen/src/machinst/abi.rs
index 55ab974abc..e424ec8011 100644
--- a/cranelift/codegen/src/machinst/abi.rs
+++ b/cranelift/codegen/src/machinst/abi.rs
@@ -1223,6 +1223,19 @@ fn checked_round_up(val: u32, mask: u32) -> Option<u32> {
Some(val.checked_add(mask)? & !mask)
}
+/// Returns `slot_offset + offset` as an offset into the stack slot area, if
+/// its magnitude is less than the maximum frame size. Since the whole frame is
+/// also limited to that size, adding the rest of the frame (e.g. the outgoing
+/// argument area) to such an offset can't overflow an `i32`.
+fn stackslot_area_offset<M: ABIMachineSpec>(slot_offset: u32, offset: i64) -> Option<i32> {
+ let offset = i64::from(slot_offset).checked_add(offset)?;
+ if offset.unsigned_abs() < u64::from(M::maximum_frame_size()) {
+ i32::try_from(offset).ok()
+ } else {
+ None
+ }
+}
+
impl<M: ABIMachineSpec> Callee<M> {
/// Create a new body ABI instance.
pub fn new(
@@ -1316,6 +1329,30 @@ impl<M: ABIMachineSpec> Callee<M> {
let stackslots_size = checked_round_up(end_offset, M::word_bytes() - 1)
.ok_or(CodegenError::ImplLimitExceeded)?;
+ // The whole frame must fit in `maximum_frame_size` (checked with the
+ // final layout in `compute_frame_layout`), so reject oversized stack
+ // slots now. Also check the offsets that `stack_addr` adds to them, so
+ // that every stack slot address computed during lowering is in range.
+ if stackslots_size > M::maximum_frame_size() {
+ return Err(CodegenError::ImplLimitExceeded);
+ }
+ for block in f.layout.blocks() {
+ for inst in f.layout.block_insts(block) {
+ if let ir::InstructionData::StackAddr {
+ stack_slot, offset, ..
+ } = f.dfg.insts[inst]
+ {
+ let offset = i64::from(offset);
+ if offset < 0
+ || stackslot_area_offset::<M>(sized_stackslots[stack_slot], offset)
+ .is_none()
+ {
+ return Err(CodegenError::ImplLimitExceeded);
+ }
+ }
+ }
+ }
+
let mut dynamic_type_sizes = HashMap::with_capacity(f.dfg.dynamic_types.len());
for (dyn_ty, _data) in f.dfg.dynamic_types.iter() {
let ty = f
@@ -2210,6 +2247,13 @@ impl<M: ABIMachineSpec> Callee<M> {
self.sized_stackslots[slot]
}
+ /// The offset of `offset` bytes into a sized stackslot, relative to the
+ /// start of the stackslot area, if it is small enough to address directly
+ /// (see `stackslot_area_offset`).
+ pub fn sized_stackslot_area_offset(&self, slot: StackSlot, offset: i64) -> Option<i32> {
+ stackslot_area_offset::<M>(self.sized_stackslots[slot], offset)
+ }
+
/// Produce an instruction that computes a sized stackslot address.
pub fn sized_stackslot_addr(
&self,
@@ -2260,9 +2304,14 @@ impl<M: ABIMachineSpec> Callee<M> {
function_calls: FunctionCalls,
) -> CodegenResult<()> {
let bytes = M::word_bytes();
- let total_stacksize = self.stackslots_size + bytes * spillslots as u32;
let mask = M::stack_align(self.call_conv) - 1;
- let total_stacksize = (total_stacksize + mask) & !mask; // 16-align the stack.
+ let total_stacksize = u32::try_from(spillslots)
+ .ok()
+ .and_then(|spillslots| spillslots.checked_mul(bytes))
+ .and_then(|spillslot_bytes| self.stackslots_size.checked_add(spillslot_bytes))
+ // Align the stack.
+ .and_then(|size| checked_round_up(size, mask))
+ .ok_or(CodegenError::ImplLimitExceeded)?;
let frame_layout = M::compute_frame_layout(
self.call_conv,
&self.flags,
diff --git a/cranelift/codegen/src/machinst/isle.rs b/cranelift/codegen/src/machinst/isle.rs
index a8c81dd3cb..3e895caa6b 100644
--- a/cranelift/codegen/src/machinst/isle.rs
+++ b/cranelift/codegen/src/machinst/isle.rs
@@ -556,15 +556,11 @@ macro_rules! isle_lower_prelude_methods {
stack_slot: StackSlot,
offset1: Offset32,
offset2: Offset32,
- ) -> i32 {
- let offset1 = i32::from(offset1);
- let offset2 = i32::from(offset2);
- i32::try_from(self.lower_ctx.abi().sized_stackslot_offset(stack_slot))
- .expect("Stack slot region cannot be larger than 2GiB")
- .checked_add(offset1)
- .expect("Stack slot region cannot be larger than 2GiB")
- .checked_add(offset2)
- .expect("Stack slot region cannot be larger than 2GiB")
+ ) -> Option<i32> {
+ let offset = i64::from(i32::from(offset1)) + i64::from(i32::from(offset2));
+ self.lower_ctx
+ .abi()
+ .sized_stackslot_area_offset(stack_slot, offset)
}
fn abi_dynamic_stackslot_addr(
diff --git a/cranelift/codegen/src/prelude_lower.isle b/cranelift/codegen/src/prelude_lower.isle
index 3e0d88672d..d12101c847 100644
--- a/cranelift/codegen/src/prelude_lower.isle
+++ b/cranelift/codegen/src/prelude_lower.isle
@@ -1458,8 +1458,9 @@
(decl abi_stackslot_addr (WritableReg StackSlot Offset32) MInst)
(extern constructor abi_stackslot_addr abi_stackslot_addr)
-;; StackSlot raw offset into slot region
-(decl abi_stackslot_offset_into_slot_region (StackSlot Offset32 Offset32) i32)
+;; StackSlot raw offset into slot region, if it is small enough to address
+;; directly; otherwise, the address must be computed in a register.
+(decl pure partial abi_stackslot_offset_into_slot_region (StackSlot Offset32 Offset32) i32)
(extern constructor abi_stackslot_offset_into_slot_region abi_stackslot_offset_into_slot_region)
;; DynamicStackSlot addr
diff --git a/cranelift/filetests/filetests/isa/aarch64/stackslot-impl-limits.clif b/cranelift/filetests/filetests/isa/aarch64/stackslot-impl-limits.clif
new file mode 100644
index 0000000000..f7b86f88e5
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/aarch64/stackslot-impl-limits.clif
@@ -0,0 +1,39 @@
+; Stack slots and `stack_addr` offsets that can't fit in a frame (limited to
+; 1 GiB) fail with an implementation-limit error rather than overflowing the
+; frame-size arithmetic or panicking during lowering.
+test compile expect-fail
+target aarch64
+
+; A slot so large that the frame size would overflow a u32.
+function %huge_slot() -> i64 {
+ ss0 = explicit_slot 4294967288
+block0:
+ v0 = stack_addr.i64 ss0
+ return v0
+}
+
+; A slot just over the frame size limit.
+function %slot_over_limit() -> i64 {
+ ss0 = explicit_slot 1073741832
+block0:
+ v0 = stack_addr.i64 ss0
+ return v0
+}
+
+; A `stack_addr` offset beyond the frame size limit.
+function %large_offset() -> i64 {
+ ss0 = explicit_slot 8
+ ss1 = explicit_slot 8
+block0:
+ v0 = stack_addr.i64 ss1+2147483647
+ return v0
+}
+
+; A negative `stack_addr` offset.
+function %negative_offset() -> i64 {
+ ss0 = explicit_slot 8
+ ss1 = explicit_slot 8
+block0:
+ v0 = stack_addr.i64 ss1-8
+ return v0
+}
diff --git a/cranelift/filetests/filetests/isa/aarch64/stackslot-large-load-offset.clif b/cranelift/filetests/filetests/isa/aarch64/stackslot-large-load-offset.clif
new file mode 100644
index 0000000000..820722d27c
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/aarch64/stackslot-large-load-offset.clif
@@ -0,0 +1,40 @@
+; A load whose offset from a `stack_addr` is too large to fold into a
+; stack-slot address mode: the slot's address is computed in a register and
+; the offset applied to the load instead.
+test compile precise-output
+target aarch64
+
+function %f() -> i64 {
+ ss0 = explicit_slot 16
+ ss1 = explicit_slot 16
+block0:
+ v0 = stack_addr.i64 ss1
+ v1 = load.i64 notrap v0+0x7ffffffc
+ return v1
+}
+
+; VCode:
+; stp fp, lr, [sp, #-16]!
+; mov fp, sp
+; sub sp, sp, #32
+; block0:
+; add x2, sp, #16
+; orr x1, xzr, #2147483644
+; ldr x0, [x2, x1]
+; add sp, sp, #32
+; ldp fp, lr, [sp], #16
+; ret
+;
+; Disassembled:
+; block0: ; offset 0x0
+; stp x29, x30, [sp, #-0x10]!
+; mov x29, sp
+; sub sp, sp, #0x20
+; block1: ; offset 0xc
+; add x2, sp, #0x10
+; mov x1, #0x7ffffffc
+; ldr x0, [x2, x1]
+; add sp, sp, #0x20
+; ldp x29, x30, [sp], #0x10
+; ret
+
diff --git a/cranelift/filetests/filetests/isa/x64/stackslot-impl-limits.clif b/cranelift/filetests/filetests/isa/x64/stackslot-impl-limits.clif
new file mode 100644
index 0000000000..9c0057d866
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/x64/stackslot-impl-limits.clif
@@ -0,0 +1,39 @@
+; Stack slots and `stack_addr` offsets that can't fit in a frame (limited to
+; 1 GiB) fail with an implementation-limit error rather than overflowing the
+; frame-size arithmetic or panicking during lowering.
+test compile expect-fail
+target x86_64
+
+; A slot so large that the frame size would overflow a u32.
+function %huge_slot() -> i64 {
+ ss0 = explicit_slot 4294967288
+block0:
+ v0 = stack_addr.i64 ss0
+ return v0
+}
+
+; A slot just over the frame size limit.
+function %slot_over_limit() -> i64 {
+ ss0 = explicit_slot 1073741832
+block0:
+ v0 = stack_addr.i64 ss0
+ return v0
+}
+
+; A `stack_addr` offset beyond the frame size limit.
+function %large_offset() -> i64 {
+ ss0 = explicit_slot 8
+ ss1 = explicit_slot 8
+block0:
+ v0 = stack_addr.i64 ss1+2147483647
+ return v0
+}
+
+; A negative `stack_addr` offset.
+function %negative_offset() -> i64 {
+ ss0 = explicit_slot 8
+ ss1 = explicit_slot 8
+block0:
+ v0 = stack_addr.i64 ss1-8
+ return v0
+}
diff --git a/cranelift/filetests/filetests/isa/x64/stackslot-large-load-offset.clif b/cranelift/filetests/filetests/isa/x64/stackslot-large-load-offset.clif
new file mode 100644
index 0000000000..2a4b145a9e
--- /dev/null
+++ b/cranelift/filetests/filetests/isa/x64/stackslot-large-load-offset.clif
@@ -0,0 +1,40 @@
+; A load whose offset from a `stack_addr` is too large to fold into a
+; stack-slot address mode: the slot's address is computed in a register and
+; the offset applied to the load instead.
+test compile precise-output
+target x86_64
+
+function %f() -> i64 {
+ ss0 = explicit_slot 16
+ ss1 = explicit_slot 16
+block0:
+ v0 = stack_addr.i64 ss1
+ v1 = load.i64 notrap v0+0x7ffffffc
+ return v1
+}
+
+; VCode:
+; pushq %rbp
+; movq %rsp, %rbp
+; subq $0x20, %rsp
+; block0:
+; leaq <offset:1>+0x10(%rsp), %rcx
+; movq 0x7ffffffc(%rcx), %rax
+; addq $0x20, %rsp
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+;
+; Disassembled:
+; block0: ; offset 0x0
+; pushq %rbp
+; movq %rsp, %rbp
+; subq $0x20, %rsp
+; block1: ; offset 0x8
+; leaq 0x10(%rsp), %rcx
+; movq 0x7ffffffc(%rcx), %rax
+; addq $0x20, %rsp
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+