On finding an static OOB heap addr, split the Ebb and recompute the CFG.

This commit is contained in:
Tyler McMullen
2018-01-18 15:33:28 -08:00
committed by Jakob Stoklund Olesen
parent 14e39db428
commit 7826fce44f

View File

@@ -9,7 +9,7 @@ use ir::{self, InstBuilder, MemFlags};
use ir::condcodes::IntCC; use ir::condcodes::IntCC;
/// Expand a `heap_addr` instruction according to the definition of the heap. /// Expand a `heap_addr` instruction according to the definition of the heap.
pub fn expand_heap_addr(inst: ir::Inst, func: &mut ir::Function, _cfg: &mut ControlFlowGraph) { pub fn expand_heap_addr(inst: ir::Inst, func: &mut ir::Function, cfg: &mut ControlFlowGraph) {
// Unpack the instruction. // Unpack the instruction.
let (heap, offset, size) = match func.dfg[inst] { let (heap, offset, size) = match func.dfg[inst] {
ir::InstructionData::HeapAddr { ir::InstructionData::HeapAddr {
@@ -29,7 +29,7 @@ pub fn expand_heap_addr(inst: ir::Inst, func: &mut ir::Function, _cfg: &mut Cont
dynamic_addr(inst, heap, offset, size, bound_gv, func) dynamic_addr(inst, heap, offset, size, bound_gv, func)
} }
ir::HeapStyle::Static { bound } => { ir::HeapStyle::Static { bound } => {
static_addr(inst, heap, offset, size, bound.into(), func) static_addr(inst, heap, offset, size, bound.into(), func, cfg)
} }
} }
} }
@@ -95,6 +95,7 @@ fn static_addr(
size: u32, size: u32,
bound: i64, bound: i64,
func: &mut ir::Function, func: &mut ir::Function,
cfg: &mut ControlFlowGraph,
) { ) {
let size = i64::from(size); let size = i64::from(size);
let offset_ty = func.dfg.value_type(offset); let offset_ty = func.dfg.value_type(offset);
@@ -107,6 +108,13 @@ fn static_addr(
// This will simply always trap since `offset >= 0`. // This will simply always trap since `offset >= 0`.
pos.ins().trap(ir::TrapCode::HeapOutOfBounds); pos.ins().trap(ir::TrapCode::HeapOutOfBounds);
pos.func.dfg.replace(inst).iconst(addr_ty, 0); pos.func.dfg.replace(inst).iconst(addr_ty, 0);
// Split Ebb, as the trap is a terminator instruction.
let curr_ebb = pos.current_ebb().expect("Cursor is not in an ebb");
let new_ebb = pos.func.dfg.make_ebb();
pos.insert_ebb(new_ebb);
cfg.recompute_ebb(pos.func, curr_ebb);
cfg.recompute_ebb(pos.func, new_ebb);
return; return;
} }