1use cranelift_codegen::cursor::FuncCursor;
2use cranelift_codegen::ir::{self, condcodes::IntCC, immediates::Imm64, InstBuilder};
3use cranelift_frontend::FunctionBuilder;
4
5#[derive(Clone)]
7pub enum TableSize {
8 Static {
10 bound: u32,
12 },
13 Dynamic {
15 bound_gv: ir::GlobalValue,
18 },
19}
20
21impl TableSize {
22 pub fn bound(&self, mut pos: FuncCursor, index_ty: ir::Type) -> ir::Value {
24 match *self {
25 TableSize::Static { bound } => pos.ins().iconst(index_ty, Imm64::new(i64::from(bound))),
26 TableSize::Dynamic { bound_gv } => pos.ins().global_value(index_ty, bound_gv),
27 }
28 }
29}
30
31#[derive(Clone)]
33pub struct TableData {
34 pub base_gv: ir::GlobalValue,
36
37 pub bound: TableSize,
39
40 pub element_size: u32,
42}
43
44impl TableData {
45 pub fn prepare_table_addr(
48 &self,
49 pos: &mut FunctionBuilder,
50 mut index: ir::Value,
51 addr_ty: ir::Type,
52 enable_table_access_spectre_mitigation: bool,
53 ) -> (ir::Value, ir::MemFlags) {
54 let index_ty = pos.func.dfg.value_type(index);
55
56 let bound = self.bound.bound(pos.cursor(), index_ty);
58
59 let oob = pos
61 .ins()
62 .icmp(IntCC::UnsignedGreaterThanOrEqual, index, bound);
63
64 if !enable_table_access_spectre_mitigation {
65 pos.ins().trapnz(oob, ir::TrapCode::TableOutOfBounds);
66 }
67
68 if index_ty != addr_ty {
70 index = pos.ins().uextend(addr_ty, index);
71 }
72
73 let base = pos.ins().global_value(addr_ty, self.base_gv);
75
76 let element_size = self.element_size;
77 let offset = if element_size == 1 {
78 index
79 } else if element_size.is_power_of_two() {
80 pos.ins()
81 .ishl_imm(index, i64::from(element_size.trailing_zeros()))
82 } else {
83 pos.ins().imul_imm(index, element_size as i64)
84 };
85
86 let element_addr = pos.ins().iadd(base, offset);
87
88 let base_flags = ir::MemFlags::new()
89 .with_aligned()
90 .with_alias_region(Some(ir::AliasRegion::Table));
91 if enable_table_access_spectre_mitigation {
92 let zero = pos.ins().iconst(addr_ty, 0);
96 (
97 pos.ins().select_spectre_guard(oob, zero, element_addr),
98 base_flags.with_trap_code(Some(ir::TrapCode::TableOutOfBounds)),
99 )
100 } else {
101 (element_addr, base_flags.with_trap_code(None))
102 }
103 }
104}