Skip to content

Commit b94de39

Browse files
committed
Adding a generic check for allocation in compute_frame_layout
1 parent 92b0111 commit b94de39

3 files changed

Lines changed: 22 additions & 10 deletions

File tree

cranelift/codegen/src/isa/x64/abi.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -824,11 +824,7 @@ impl ABIMachineSpec for X64ABIMachineSpec {
824824
if rsp_relative.saturating_add(probe_margin) > i32::MAX as u64
825825
|| rbp_relative > i32::MAX as u64
826826
{
827-
return Err(crate::CodegenError::Unsupported(
828-
"stack frame size exceeds the 2GB limit supported by the x64 backend \
829-
(see rust-lang/rustc_codegen_cranelift#1656)"
830-
.to_owned(),
831-
));
827+
return Err(crate::CodegenError::ImplLimitExceeded);
832828
}
833829
Ok(())
834830
}

cranelift/codegen/src/machinst/abi.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,12 +2211,12 @@ impl<M: ABIMachineSpec> Callee<M> {
22112211
spillslots: usize,
22122212
clobbered: Vec<Writable<RealReg>>,
22132213
function_calls: FunctionCalls,
2214-
) {
2214+
) -> CodegenResult<()> {
22152215
let bytes = M::word_bytes();
22162216
let total_stacksize = self.stackslots_size + bytes * spillslots as u32;
22172217
let mask = M::stack_align(self.call_conv) - 1;
2218-
let total_stacksize = (total_stacksize + mask) & !mask; // 16-align the stack.
2219-
self.frame_layout = Some(M::compute_frame_layout(
2218+
let total_stacksize = (total_stacksize + mask) & !mask;
2219+
let frame_layout = M::compute_frame_layout(
22202220
self.call_conv,
22212221
&self.flags,
22222222
self.signature(),
@@ -2227,7 +2227,23 @@ impl<M: ABIMachineSpec> Callee<M> {
22272227
self.stackslots_size,
22282228
total_stacksize,
22292229
self.outgoing_args_size,
2230-
));
2230+
);
2231+
2232+
// FrameLayout's fields are all u32, so any backend can in principle
2233+
// overflow a 32-bit sum here; this generic check applies to all targets.
2234+
// x64 needs it's own specific check.
2235+
let total: u64 = frame_layout.incoming_args_size as u64
2236+
+ frame_layout.tail_args_size as u64
2237+
+ frame_layout.setup_area_size as u64
2238+
+ frame_layout.clobber_size as u64
2239+
+ frame_layout.fixed_frame_storage_size as u64
2240+
+ frame_layout.outgoing_args_size as u64;
2241+
if total > u32::MAX as u64 {
2242+
return Err(CodegenError::ImplLimitExceeded);
2243+
}
2244+
2245+
self.frame_layout = Some(frame_layout);
2246+
Ok(())
22312247
}
22322248

22332249
/// Generate a prologue, post-regalloc.

cranelift/codegen/src/machinst/vcode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ impl<I: VCodeInst> VCode<I> {
783783
regalloc.num_spillslots,
784784
clobbers,
785785
function_calls,
786-
);
786+
)?;
787787

788788
let guard_size = 1u32 << flags.probestack_size_log2();
789789
I::ABIMachineSpec::validate_frame_layout(self.abi.frame_layout(), guard_size)?;

0 commit comments

Comments
 (0)