Add heap_load, heap_store, and heap_addr instructions.

These are used when lowering WebAssembly sandbox code.
This commit is contained in:
Jakob Stoklund Olesen
2017-04-10 15:03:10 -07:00
parent e78e4ea4ec
commit b474485c0d
9 changed files with 110 additions and 46 deletions

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 i8, f32, f64, b1
from base.immediates import imm64, uimm8, ieee32, ieee64, offset32
from base.immediates import imm64, uimm8, ieee32, ieee64, offset32, uoffset32
from base.immediates import intcc, floatcc
from base import entities
import base.formats # noqa
@@ -209,6 +209,7 @@ SS = Operand('SS', entities.stack_slot)
Offset = Operand('Offset', offset32, 'In-bounds offset into stack slot')
x = Operand('x', Mem, doc='Value to be stored')
a = Operand('a', Mem, doc='Value loaded')
p = Operand('p', iAddr)
addr = Operand('addr', iAddr)
stack_load = Instruction(
@@ -247,6 +248,44 @@ stack_addr = Instruction(
""",
ins=(SS, Offset), outs=addr)
#
# 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')
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)
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))
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.
Convert the heap-relative address in ``p`` to a real absolute address
and return it.
""",
ins=(p, Offset), outs=addr)
#
# Materializing constants.
#