Skip to content

Commit b81e874

Browse files
authored
Cranelift: add a vreg limit check to correctly return an error on too-large inputs. (#4882) (#4887)
Previously, Cranelift panicked (via a a panic in regalloc2) when the virtual-register limit of 2M (2^21) was reached. This resulted in a perplexing and unhelpful failure when the user provided a too-large input (such as the Wasm module in #4865). This PR adds an explicit check when allocating vregs that fails with a "code too large" error when the limit is hit, producing output such as (on the minimized testcase from #4865): ``` Error: failed to compile wasm function 3785 at offset 0xa3f3 Caused by: Compilation error: Code for function is too large ``` Fixes #4865.
1 parent 310eac0 commit b81e874

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

cranelift/codegen/src/machinst/lower.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::machinst::{
1919
LoweredBlock, MachLabel, Reg, SigSet, VCode, VCodeBuilder, VCodeConstant, VCodeConstantData,
2020
VCodeConstants, VCodeInst, ValueRegs, Writable,
2121
};
22-
use crate::{trace, CodegenResult};
22+
use crate::{trace, CodegenError, CodegenResult};
2323
use alloc::vec::Vec;
2424
use regalloc2::VReg;
2525
use smallvec::{smallvec, SmallVec};
@@ -323,6 +323,10 @@ fn alloc_vregs<I: VCodeInst>(
323323
let v = *next_vreg;
324324
let (regclasses, tys) = I::rc_for_type(ty)?;
325325
*next_vreg += regclasses.len();
326+
if *next_vreg >= VReg::MAX {
327+
return Err(CodegenError::CodeTooLarge);
328+
}
329+
326330
let regs: ValueRegs<Reg> = match regclasses {
327331
&[rc0] => ValueRegs::one(VReg::new(v, rc0).into()),
328332
&[rc0, rc1] => ValueRegs::two(VReg::new(v, rc0).into(), VReg::new(v + 1, rc1).into()),

0 commit comments

Comments
 (0)