Add heaps to the Cretonne IL.

Add preamble syntax for declaring static and dynamic heaps, and update
the langref section on heaps. Add IR support for heap references.

Remove the heap_load and heap_store as discussed in #144. We will use
heap_addr along with native load and store instructions in their place.

Add the heap_addr instruction and document its bounds checking
semantics.
This commit is contained in:
Jakob Stoklund Olesen
2017-08-18 12:51:54 -07:00
parent a9238eda7a
commit 3b71a27632
17 changed files with 405 additions and 83 deletions

View File

@@ -30,3 +30,6 @@ func_ref = EntityRefKind('func_ref', 'An external function.')
#: A reference to a jump table declared in the function preamble.
jump_table = EntityRefKind(
'jump_table', 'A jump table.', default_member='table')
#: A reference to a heap declared in the function preamble.
heap = EntityRefKind('heap', 'A heap.')

View File

@@ -8,10 +8,11 @@ in this module.
from __future__ import absolute_import
from cdsl.formats import InstructionFormat
from cdsl.operands import VALUE, VARIABLE_ARGS
from .immediates import imm64, uimm8, ieee32, ieee64, offset32, uoffset32
from .immediates import imm64, uimm8, uimm32, ieee32, ieee64
from .immediates import offset32, uoffset32
from .immediates import boolean, intcc, floatcc, memflags, regunit
from . import entities
from .entities import ebb, sig_ref, func_ref, stack_slot
from .entities import ebb, sig_ref, func_ref, stack_slot, heap
Nullary = InstructionFormat()
@@ -59,6 +60,7 @@ StackStore = InstructionFormat(VALUE, stack_slot, offset32)
# TODO: Add a reference to a `heap` declared in the preamble.
HeapLoad = InstructionFormat(VALUE, uoffset32)
HeapStore = InstructionFormat(VALUE, VALUE, uoffset32)
HeapAddr = InstructionFormat(heap, VALUE, uimm32)
RegMove = InstructionFormat(VALUE, ('src', regunit), ('dst', regunit))

View File

@@ -9,7 +9,7 @@ from cdsl.operands import Operand, VARIABLE_ARGS
from cdsl.typevar import TypeVar
from cdsl.instructions import Instruction, InstructionGroup
from base.types import f32, f64, b1
from base.immediates import imm64, uimm8, ieee32, ieee64, offset32, uoffset32
from base.immediates import imm64, uimm8, uimm32, ieee32, ieee64, offset32
from base.immediates import boolean, intcc, floatcc, memflags, regunit
from base import entities
from cdsl.ti import WiderOrEq
@@ -360,40 +360,26 @@ global_addr = Instruction(
#
# WebAssembly bounds-checked heap accesses.
#
# TODO: Add a `heap` operand that selects between multiple heaps.
# TODO: Should the immediate offset be a `u32`?
# TODO: Distinguish between `iAddr` for a heap and for a target address? i.e.,
# 32-bit WebAssembly on a 64-bit target has two different types.
Offset = Operand('Offset', uoffset32, 'Unsigned offset to effective address')
HeapOffset = TypeVar('HeapOffset', 'An unsigned heap offset', ints=(32, 64))
heap_load = Instruction(
'heap_load', r"""
Load a value at the address :math:`p + Offset` in the heap H.
Trap if the heap access would be out of bounds.
""",
ins=(p, Offset), outs=a, can_load=True)
heap_store = Instruction(
'heap_store', r"""
Store a value at the address :math:`p + Offset` in the heap H.
Trap if the heap access would be out of bounds.
""",
ins=(x, p, Offset), can_store=True)
H = Operand('H', entities.heap)
p = Operand('p', HeapOffset)
Size = Operand('Size', uimm32, 'Size in bytes')
heap_addr = Instruction(
'heap_addr', r"""
Bounds check and compute absolute address of heap memory.
Verify that the address range ``p .. p + Size - 1`` is valid in the
heap H, and trap if not.
Verify that the offset range ``p .. p + Size - 1`` is in bounds for the
heap H, and generate an absolute address that is safe to dereference.
Convert the heap-relative address in ``p`` to a real absolute address
and return it.
1. If ``p + Size`` is not greater than the heap bound, return an
absolute address corresponding to a byte offset of ``p`` from the
heap's base address.
2. If ``p + Size`` is greater than the heap bound, generate a trap.
""",
ins=(p, Offset), outs=addr)
ins=(H, p, Size), outs=addr)
#
# Materializing constants.

View File

@@ -98,6 +98,11 @@ entity_impl!(FuncRef, "fn");
pub struct SigRef(u32);
entity_impl!(SigRef, "sig");
/// A reference to a heap.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Heap(u32);
entity_impl!(Heap, "heap");
/// A reference to any of the entities defined in this module.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub enum AnyEntity {
@@ -119,6 +124,8 @@ pub enum AnyEntity {
FuncRef(FuncRef),
/// A function call signature.
SigRef(SigRef),
/// A heap.
Heap(Heap),
}
impl fmt::Display for AnyEntity {
@@ -133,6 +140,7 @@ impl fmt::Display for AnyEntity {
AnyEntity::JumpTable(r) => r.fmt(f),
AnyEntity::FuncRef(r) => r.fmt(f),
AnyEntity::SigRef(r) => r.fmt(f),
AnyEntity::Heap(r) => r.fmt(f),
}
}
}
@@ -185,6 +193,12 @@ impl From<SigRef> for AnyEntity {
}
}
impl From<Heap> for AnyEntity {
fn from(r: Heap) -> AnyEntity {
AnyEntity::Heap(r)
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -29,6 +29,9 @@ pub struct Function {
/// Global variables referenced.
pub global_vars: PrimaryMap<ir::GlobalVar, ir::GlobalVarData>,
/// Heaps referenced.
pub heaps: PrimaryMap<ir::Heap, ir::HeapData>,
/// Jump tables used in this function.
pub jump_tables: JumpTables,
@@ -61,6 +64,7 @@ impl Function {
signature: sig,
stack_slots: StackSlots::new(),
global_vars: PrimaryMap::new(),
heaps: PrimaryMap::new(),
jump_tables: PrimaryMap::new(),
dfg: DataFlowGraph::new(),
layout: Layout::new(),

View File

@@ -0,0 +1,70 @@
//! Heaps.
use ir::immediates::Imm64;
use ir::GlobalVar;
use std::fmt;
/// Information about a heap declaration.
#[derive(Clone)]
pub struct HeapData {
/// Method for determining the heap base address.
pub base: HeapBase,
/// Guaranteed minimum heap size in bytes. Heap accesses before `min_size` don't need bounds
/// checking.
pub min_size: Imm64,
/// Size in bytes of the guard pages following the heap.
pub guard_size: Imm64,
/// Heap style, with additional style-specific info.
pub style: HeapStyle,
}
/// Method for determining the base address of a heap.
#[derive(Clone)]
pub enum HeapBase {
/// The heap base lives in a reserved register.
ReservedReg,
/// The heap base is in a global variable.
GlobalVar(GlobalVar),
}
/// Style of heap including style-specific information.
#[derive(Clone)]
pub enum HeapStyle {
/// A dynamic heap can be relocated to a different base address when it is grown.
Dynamic {
/// Global variable holding the current bound of the heap in bytes.
bound_gv: GlobalVar,
},
/// A static heap has a fixed base address and a number of not-yet-allocated pages before the
/// guard pages.
Static {
/// Heap bound in bytes. The guard pages are allocated after the bound.
bound: Imm64,
},
}
impl fmt::Display for HeapData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self.style {
HeapStyle::Dynamic { .. } => "dynamic",
HeapStyle::Static { .. } => "static",
})?;
match self.base {
HeapBase::ReservedReg => write!(f, " reserved_reg")?,
HeapBase::GlobalVar(gv) => write!(f, " {}", gv)?,
}
write!(f, ", min {}", self.min_size)?;
match self.style {
HeapStyle::Dynamic { bound_gv } => write!(f, ", bound {}", bound_gv)?,
HeapStyle::Static { bound } => write!(f, ", bound {}", bound)?,
}
write!(f, ", guard {}", self.guard_size)
}
}

View File

@@ -12,7 +12,7 @@ use std::ops::{Deref, DerefMut};
use ir;
use ir::{Value, Type, Ebb, JumpTable, SigRef, FuncRef, StackSlot, MemFlags};
use ir::immediates::{Imm64, Uimm8, Ieee32, Ieee64, Offset32, Uoffset32};
use ir::immediates::{Imm64, Uimm8, Uimm32, Ieee32, Ieee64, Offset32, Uoffset32};
use ir::condcodes::*;
use ir::types;
use isa::RegUnit;
@@ -199,6 +199,12 @@ pub enum InstructionData {
args: [Value; 2],
offset: Uoffset32,
},
HeapAddr {
opcode: Opcode,
heap: ir::Heap,
arg: Value,
imm: Uimm32,
},
Load {
opcode: Opcode,
flags: MemFlags,

View File

@@ -14,18 +14,20 @@ mod builder;
mod extfunc;
mod funcname;
mod globalvar;
mod heap;
mod memflags;
mod progpoint;
mod valueloc;
pub use ir::builder::{InstBuilder, InstBuilderBase, InstInserterBase, InsertBuilder};
pub use ir::dfg::{DataFlowGraph, ValueDef};
pub use ir::entities::{Ebb, Inst, Value, StackSlot, GlobalVar, JumpTable, FuncRef, SigRef};
pub use ir::entities::{Ebb, Inst, Value, StackSlot, GlobalVar, JumpTable, FuncRef, SigRef, Heap};
pub use ir::extfunc::{Signature, CallConv, ArgumentType, ArgumentExtension, ArgumentPurpose,
ExtFuncData};
pub use ir::funcname::FunctionName;
pub use ir::function::Function;
pub use ir::globalvar::GlobalVarData;
pub use ir::heap::{HeapData, HeapStyle, HeapBase};
pub use ir::instructions::{Opcode, InstructionData, VariableArgs, ValueList, ValueListPool};
pub use ir::jumptable::JumpTableData;
pub use ir::layout::{Layout, CursorBase, Cursor};

View File

@@ -301,6 +301,9 @@ impl<'a> Verifier<'a> {
UnaryGlobalVar { global_var, .. } => {
self.verify_global_var(inst, global_var)?;
}
HeapAddr { heap, .. } => {
self.verify_heap(inst, heap)?;
}
// Exhaustive list so we can't forget to add new formats
Nullary { .. } |
@@ -367,6 +370,14 @@ impl<'a> Verifier<'a> {
}
}
fn verify_heap(&self, inst: Inst, heap: ir::Heap) -> Result {
if !self.func.heaps.is_valid(heap) {
err!(inst, "invalid heap {}", heap)
} else {
Ok(())
}
}
fn verify_value_list(&self, inst: Inst, l: &ValueList) -> Result {
if !l.is_valid(&self.func.dfg.value_lists) {
err!(inst, "invalid value list reference {:?}", l)

View File

@@ -54,6 +54,11 @@ fn write_preamble(w: &mut Write,
writeln!(w, " {} = {}", gv, func.global_vars[gv])?;
}
for heap in func.heaps.keys() {
any = true;
writeln!(w, " {} = {}", heap, func.heaps[heap])?;
}
// Write out all signatures before functions since function declarations can refer to
// signatures.
for sig in func.dfg.signatures.keys() {
@@ -325,6 +330,7 @@ pub fn write_operands(w: &mut Write,
} => write!(w, " {}, {}{}", arg, stack_slot, offset),
HeapLoad { arg, offset, .. } => write!(w, " {}{}", arg, offset),
HeapStore { args, offset, .. } => write!(w, " {}, {}{}", args[0], args[1], offset),
HeapAddr { heap, arg, imm, .. } => write!(w, " {}, {}, {}", heap, arg, imm),
Load { flags, arg, offset, .. } => write!(w, "{} {}{}", flags, arg, offset),
Store {
flags,