A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
cranelift: aarch64: fix BTI landing pads for indirect control flow (#15072)
Nicolas Stigler81231fd0c205
diff --git a/cranelift/codegen/src/inst_predicates.rs b/cranelift/codegen/src/inst_predicates.rsindex 993b2673a6..c18a908b60 100644--- a/cranelift/codegen/src/inst_predicates.rs+++ b/cranelift/codegen/src/inst_predicates.rs@@ -205,7 +205,7 @@ pub fn inst_store_data(func: &Function, inst: Inst) -> Option<Value> {/// Visit all successors of a block with a given visitor closure. The closure/// arguments are the branch instruction that is used to reach the successor,/// the successor block itself, and a flag indicating whether the block is-/// branched to via a table entry.+/// reached via an indirect branch.pub(crate) fn visit_block_succs<F: FnMut(Inst, Block, bool)>(f: &Function,block: Block,@@ -231,17 +231,16 @@ pub(crate) fn visit_block_succs<F: FnMut(Inst, Block, bool)>(let pool = &f.dfg.value_lists;let table = &f.stencil.dfg.jump_tables[*table];- // The default block is reached via a direct conditional branch,- // so it is not part of the table. We visit the default block- // first explicitly, to mirror the traversal order of- // `JumpTableData::all_branches`, and transitively the order of- // `InstructionData::branch_destination`.+ // The default block is also an indirect branch target on+ // backends that include it in the jump table (aarch64).+ // We visit the default block first explicitly, to mirror the+ // traversal order of `JumpTableData::all_branches`, and+ // transitively the order of `InstructionData::branch_destination`.//// Additionally, this case is why we are unable to replace this// whole function with a loop over `branch_destination`: we need- // to report which branch targets come from the table vs the- // default.- visit(inst, table.default_block().block(pool), false);+ // to report which branch targets are reached indirectly.+ visit(inst, table.default_block().block(pool), true);for dest in table.as_slice() {visit(inst, dest.block(pool), true);@@ -253,9 +252,15 @@ pub(crate) fn visit_block_succs<F: FnMut(Inst, Block, bool)>(let pool = &f.dfg.value_lists;let exdata = &f.stencil.dfg.exception_tables[*exception];- for dest in exdata.all_branches() {- visit(inst, dest.block(pool), false);+ // Exception handlers are resumed into via an indirect branch+ // from the unwinder, so they are indirect branch targets too.+ // The last target is the normal-return path, which is reached+ // directly after the call returns.+ let (normal, handlers) = exdata.all_branches().split_last().unwrap();+ for dest in handlers {+ visit(inst, dest.block(pool), true);}+ visit(inst, normal.block(pool), false);}inst => debug_assert!(!inst.opcode().is_branch()),diff --git a/cranelift/codegen/src/isa/aarch64/abi.rs b/cranelift/codegen/src/isa/aarch64/abi.rsindex 32ad4a1786..afdcddb61d 100644--- a/cranelift/codegen/src/isa/aarch64/abi.rs+++ b/cranelift/codegen/src/isa/aarch64/abi.rs@@ -643,6 +643,15 @@ impl ABIMachineSpec for AArch64MachineDeps {match Self::select_api_key(isa_flags, call_conv, setup_frame) {Some(key) => {+ // `PACIASP` and `PACIBSP` are implicit `BTI C` landing pads,+ // but the zero-modifier variants used by the tail calling+ // convention are not. Adds an explicit landing pad for those.+ if isa_flags.use_bti() && matches!(key, APIKey::AZ | APIKey::BZ) {+ insts.push(Inst::Bti {+ targets: BranchTargetType::C,+ });+ }+insts.push(Inst::Paci { key });if flags.unwind_info() {insts.push(Inst::Unwind {diff --git a/cranelift/codegen/src/isa/aarch64/inst/emit.rs b/cranelift/codegen/src/isa/aarch64/inst/emit.rsindex fc6dec68dc..8060d6312d 100644--- a/cranelift/codegen/src/isa/aarch64/inst/emit.rs+++ b/cranelift/codegen/src/isa/aarch64/inst/emit.rs@@ -3522,8 +3522,31 @@ impl MachInstEmit for Inst {&Inst::ReturnCallInd { ref info } => {emit_return_call_common_sequence(sink, emit_info, state, info);+ // Indirect tail calls must use x16 or x17 when BTI is enabled+ // so that they are compatible with the callee's `BTI C`+ // landing pad. Other registers are only accepted by `BTI J`,+ // which is not a valid landing pad for a function entry.+ //+ // x16 is the spill-temp register, which by Cranelift+ // convention we are allowed to use as a scratch register+ // within one VCode inst, so this is safe.+ let dest = if emit_info.isa_flags.use_bti() {+ let dest = xreg(16);+ if info.dest != dest {+ Inst::Mov {+ size: OperandSize::Size64,+ rd: writable_xreg(16),+ rm: info.dest,+ }+ .emit(sink, emit_info, state);+ }+ dest+ } else {+ info.dest+ };+Inst::IndirectBr {- rn: info.dest,+ rn: dest,targets: vec![],}.emit(sink, emit_info, state);diff --git a/cranelift/codegen/src/machinst/blockorder.rs b/cranelift/codegen/src/machinst/blockorder.rsindex f19720e6c2..08c014ebd5 100644--- a/cranelift/codegen/src/machinst/blockorder.rs+++ b/cranelift/codegen/src/machinst/blockorder.rs@@ -166,12 +166,12 @@ impl BlockLoweringOrder {for block in f.layout.blocks() {let start = block_succs.len();- visit_block_succs(f, block, |_, succ, from_table| {+ visit_block_succs(f, block, |_, succ, is_indirect| {block_out_count[block] += 1;block_in_count[succ] += 1;block_succs.push(LoweredBlock::Orig { block: succ });- if from_table {+ if is_indirect {indirect_branch_target_clif_blocks.insert(succ);}});diff --git a/cranelift/filetests/filetests/isa/aarch64/bti-exceptions.clif b/cranelift/filetests/filetests/isa/aarch64/bti-exceptions.clifnew file mode 100644index 0000000000..fc36e148ae--- /dev/null+++ b/cranelift/filetests/filetests/isa/aarch64/bti-exceptions.clif@@ -0,0 +1,233 @@+test compile precise-output+target aarch64 use_bti++function %try_call(i32) -> i32 {+ sig0 = (i32) -> i32 tail+ fn0 = %g(i32) -> i32 tail++ block0(v0: i32):+ try_call fn0(v0), sig0, block1, [ default: block2(exn0) ]++ block1:+ return v0++ block2(v1: i64):+ v2 = ireduce.i32 v1+ return v2+}++; VCode:+; bti c+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; stp x27, x28, [sp, #-16]!+; stp x25, x26, [sp, #-16]!+; stp x23, x24, [sp, #-16]!+; stp x21, x22, [sp, #-16]!+; stp x19, x20, [sp, #-16]!+; stp d14, d15, [sp, #-16]!+; stp d12, d13, [sp, #-16]!+; stp d10, d11, [sp, #-16]!+; stp d8, d9, [sp, #-16]!+; sub sp, sp, #16+; block0:+; str x0, [sp]+; load_ext_name_far x6, TestCase(%g)+0+; ldr x2, [sp]+; blr x6; b MachLabel(1); catch [default: MachLabel(2)]+; block1:+; ldr x0, [sp]+; add sp, sp, #16+; ldp d8, d9, [sp], #16+; ldp d10, d11, [sp], #16+; ldp d12, d13, [sp], #16+; ldp d14, d15, [sp], #16+; ldp x19, x20, [sp], #16+; ldp x21, x22, [sp], #16+; ldp x23, x24, [sp], #16+; ldp x25, x26, [sp], #16+; ldp x27, x28, [sp], #16+; ldp fp, lr, [sp], #16+; ret+; block2:+; bti j+; add sp, sp, #16+; ldp d8, d9, [sp], #16+; ldp d10, d11, [sp], #16+; ldp d12, d13, [sp], #16+; ldp d14, d15, [sp], #16+; ldp x19, x20, [sp], #16+; ldp x21, x22, [sp], #16+; ldp x23, x24, [sp], #16+; ldp x25, x26, [sp], #16+; ldp x27, x28, [sp], #16+; ldp fp, lr, [sp], #16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; bti c+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; stp x27, x28, [sp, #-0x10]!+; stp x25, x26, [sp, #-0x10]!+; stp x23, x24, [sp, #-0x10]!+; stp x21, x22, [sp, #-0x10]!+; stp x19, x20, [sp, #-0x10]!+; stp d14, d15, [sp, #-0x10]!+; stp d12, d13, [sp, #-0x10]!+; stp d10, d11, [sp, #-0x10]!+; stp d8, d9, [sp, #-0x10]!+; sub sp, sp, #0x10+; block1: ; offset 0x34+; stur x0, [sp]+; ldr x6, #0x40+; b #0x48+; udf #0 ; reloc_external Abs8 %g 0+; udf #0+; ldur x2, [sp]+; blr x6+; block2: ; offset 0x50+; ldur x0, [sp]+; add sp, sp, #0x10+; ldp d8, d9, [sp], #0x10+; ldp d10, d11, [sp], #0x10+; ldp d12, d13, [sp], #0x10+; ldp d14, d15, [sp], #0x10+; ldp x19, x20, [sp], #0x10+; ldp x21, x22, [sp], #0x10+; ldp x23, x24, [sp], #0x10+; ldp x25, x26, [sp], #0x10+; ldp x27, x28, [sp], #0x10+; ldp x29, x30, [sp], #0x10+; ret+; block3: ; offset 0x84+; bti j+; add sp, sp, #0x10+; ldp d8, d9, [sp], #0x10+; ldp d10, d11, [sp], #0x10+; ldp d12, d13, [sp], #0x10+; ldp d14, d15, [sp], #0x10+; ldp x19, x20, [sp], #0x10+; ldp x21, x22, [sp], #0x10+; ldp x23, x24, [sp], #0x10+; ldp x25, x26, [sp], #0x10+; ldp x27, x28, [sp], #0x10+; ldp x29, x30, [sp], #0x10+; ret++function %try_call_indirect(i32) -> i32 {+ sig0 = (i32) -> i32 tail+ fn0 = %g(i32) -> i32 tail++ block0(v0: i32):+ v1 = func_addr.i64 fn0+ try_call_indirect v1(v0), sig0, block1, [ default: block2(exn0) ]++ block1:+ return v0++ block2(v2: i64):+ v3 = ireduce.i32 v2+ return v3+}++; VCode:+; bti c+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; stp x27, x28, [sp, #-16]!+; stp x25, x26, [sp, #-16]!+; stp x23, x24, [sp, #-16]!+; stp x21, x22, [sp, #-16]!+; stp x19, x20, [sp, #-16]!+; stp d14, d15, [sp, #-16]!+; stp d12, d13, [sp, #-16]!+; stp d10, d11, [sp, #-16]!+; stp d8, d9, [sp, #-16]!+; sub sp, sp, #16+; block0:+; str x0, [sp]+; load_ext_name_far x6, TestCase(%g)+0+; ldr x2, [sp]+; blr x6; b MachLabel(1); catch [default: MachLabel(2)]+; block1:+; ldr x0, [sp]+; add sp, sp, #16+; ldp d8, d9, [sp], #16+; ldp d10, d11, [sp], #16+; ldp d12, d13, [sp], #16+; ldp d14, d15, [sp], #16+; ldp x19, x20, [sp], #16+; ldp x21, x22, [sp], #16+; ldp x23, x24, [sp], #16+; ldp x25, x26, [sp], #16+; ldp x27, x28, [sp], #16+; ldp fp, lr, [sp], #16+; ret+; block2:+; bti j+; add sp, sp, #16+; ldp d8, d9, [sp], #16+; ldp d10, d11, [sp], #16+; ldp d12, d13, [sp], #16+; ldp d14, d15, [sp], #16+; ldp x19, x20, [sp], #16+; ldp x21, x22, [sp], #16+; ldp x23, x24, [sp], #16+; ldp x25, x26, [sp], #16+; ldp x27, x28, [sp], #16+; ldp fp, lr, [sp], #16+; ret+;+; Disassembled:+; block0: ; offset 0x0+; bti c+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; stp x27, x28, [sp, #-0x10]!+; stp x25, x26, [sp, #-0x10]!+; stp x23, x24, [sp, #-0x10]!+; stp x21, x22, [sp, #-0x10]!+; stp x19, x20, [sp, #-0x10]!+; stp d14, d15, [sp, #-0x10]!+; stp d12, d13, [sp, #-0x10]!+; stp d10, d11, [sp, #-0x10]!+; stp d8, d9, [sp, #-0x10]!+; sub sp, sp, #0x10+; block1: ; offset 0x34+; stur x0, [sp]+; ldr x6, #0x40+; b #0x48+; udf #0 ; reloc_external Abs8 %g 0+; udf #0+; ldur x2, [sp]+; blr x6+; block2: ; offset 0x50+; ldur x0, [sp]+; add sp, sp, #0x10+; ldp d8, d9, [sp], #0x10+; ldp d10, d11, [sp], #0x10+; ldp d12, d13, [sp], #0x10+; ldp d14, d15, [sp], #0x10+; ldp x19, x20, [sp], #0x10+; ldp x21, x22, [sp], #0x10+; ldp x23, x24, [sp], #0x10+; ldp x25, x26, [sp], #0x10+; ldp x27, x28, [sp], #0x10+; ldp x29, x30, [sp], #0x10+; ret+; block3: ; offset 0x84+; bti j+; add sp, sp, #0x10+; ldp d8, d9, [sp], #0x10+; ldp d10, d11, [sp], #0x10+; ldp d12, d13, [sp], #0x10+; ldp d14, d15, [sp], #0x10+; ldp x19, x20, [sp], #0x10+; ldp x21, x22, [sp], #0x10+; ldp x23, x24, [sp], #0x10+; ldp x25, x26, [sp], #0x10+; ldp x27, x28, [sp], #0x10+; ldp x29, x30, [sp], #0x10+; retdiff --git a/cranelift/filetests/filetests/isa/aarch64/bti-pauth.clif b/cranelift/filetests/filetests/isa/aarch64/bti-pauth.clifnew file mode 100644index 0000000000..7d5c2f9c65--- /dev/null+++ b/cranelift/filetests/filetests/isa/aarch64/bti-pauth.clif@@ -0,0 +1,99 @@+test compile precise-output+set unwind_info=false+target aarch64 use_bti sign_return_address++function %normal(i64) -> i64 {+ fn0 = %g(i64) -> i64++ block0(v0: i64):+ v1 = call fn0(v0)+ return v1+}++; VCode:+; paciasp+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; block0:+; load_ext_name_far x3, TestCase(%g)+0+; blr x3+; ldp fp, lr, [sp], #16+; autiasp ; ret+;+; Disassembled:+; block0: ; offset 0x0+; paciasp+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; block1: ; offset 0xc+; ldr x3, #0x14+; b #0x1c+; udf #0 ; reloc_external Abs8 %g 0+; udf #0+; blr x3+; ldp x29, x30, [sp], #0x10+; autiasp+; ret++function %tail(i64) -> i64 tail {+ fn0 = %g(i64) -> i64++ block0(v0: i64):+ v1 = call fn0(v0)+ return v1+}++; VCode:+; bti c+; paciaz+; stp fp, lr, [sp, #-16]!+; mov fp, sp+; block0:+; load_ext_name_far x3, TestCase(%g)+0+; mov x0, x2+; blr x3+; mov x2, x0+; ldp fp, lr, [sp], #16+; autiaz ; ret+;+; Disassembled:+; block0: ; offset 0x0+; bti c+; paciaz+; stp x29, x30, [sp, #-0x10]!+; mov x29, sp+; block1: ; offset 0x10+; ldr x3, #0x18+; b #0x20+; udf #0 ; reloc_external Abs8 %g 0+; udf #0+; mov x0, x2+; blr x3+; mov x2, x0+; ldp x29, x30, [sp], #0x10+; autiaz+; ret++function %tail_call(i64) -> i64 tail {+ fn0 = %g(i64) -> i64 tail++ block0(v0: i64):+ return_call fn0(v0)+}++; VCode:+; bti c+; block0:+; load_ext_name_far x1, TestCase(%g)+0+; return_call_ind x1 new_stack_arg_size:0 x2=x2+;+; Disassembled:+; block0: ; offset 0x0+; bti c+; block1: ; offset 0x4+; ldr x1, #0xc+; b #0x14+; udf #0 ; reloc_external Abs8 %g 0+; udf #0+; mov x16, x1+; br x16diff --git a/cranelift/filetests/filetests/isa/aarch64/bti-return-call-indirect.clif b/cranelift/filetests/filetests/isa/aarch64/bti-return-call-indirect.clifnew file mode 100644index 0000000000..349d45b286--- /dev/null+++ b/cranelift/filetests/filetests/isa/aarch64/bti-return-call-indirect.clif@@ -0,0 +1,54 @@+test compile precise-output+set unwind_info=false+target aarch64 use_bti++;; Indirect tail calls must branch through x16 (the spill-temp register) so+;; that BTI classifies the branch as a call and the callee's `bti c` landing+;; pad is accepted.++function %callee_i64(i64) -> i64 tail {+block0(v0: i64):+ v2 = iconst.i64 10+ v1 = iadd v0, v2+ return v1+}++; VCode:+; bti c+; block0:+; add x2, x2, #10+; ret+;+; Disassembled:+; block0: ; offset 0x0+; bti c+; block1: ; offset 0x4+; add x2, x2, #0xa+; ret++function %call_i64(i64) -> i64 tail {+ sig0 = (i64) -> i64 tail+ fn0 = %callee_i64(i64) -> i64 tail++block0(v0: i64):+ v1 = func_addr.i64 fn0+ return_call_indirect sig0, v1(v0)+}++; VCode:+; bti c+; block0:+; load_ext_name_far x1, TestCase(%callee_i64)+0+; return_call_ind x1 new_stack_arg_size:0 x2=x2+;+; Disassembled:+; block0: ; offset 0x0+; bti c+; block1: ; offset 0x4+; ldr x1, #0xc+; b #0x14+; udf #0 ; reloc_external Abs8 %callee_i64 0+; udf #0+; mov x16, x1+; br x16+diff --git a/cranelift/filetests/filetests/isa/aarch64/bti.clif b/cranelift/filetests/filetests/isa/aarch64/bti.clifindex 7dbbbfd978..6732d063c7 100644--- a/cranelift/filetests/filetests/isa/aarch64/bti.clif+++ b/cranelift/filetests/filetests/isa/aarch64/bti.clif@@ -48,6 +48,7 @@ block5(v5: i32):; movz w5, #1; b label5; block4:+; bti j; movz w5, #4; b label5; block5:@@ -72,18 +73,19 @@ block5(v5: i32):; block2: ; offset 0x30; bti j; mov w5, #3-; b #0x58+; b #0x5c; block3: ; offset 0x3c; bti j; mov w5, #2-; b #0x58+; b #0x5c; block4: ; offset 0x48; bti j; mov w5, #1-; b #0x58+; b #0x5c; block5: ; offset 0x54+; bti j; mov w5, #4-; block6: ; offset 0x58+; block6: ; offset 0x5c; add w0, w0, w5; ret@@ -118,6 +120,7 @@ block2:; add x0, x0, #42; ret; block2:+; bti j; mov x0, x8; ret;@@ -142,6 +145,7 @@ block2:; add x0, x0, #0x2a; ret; block3: ; offset 0x40+; bti j; mov x0, x8; retdiff --git a/cranelift/filetests/filetests/isa/aarch64/bti_with_csdb.clif b/cranelift/filetests/filetests/isa/aarch64/bti_with_csdb.clifindex 3a1272b94a..33c34e4e7e 100644--- a/cranelift/filetests/filetests/isa/aarch64/bti_with_csdb.clif+++ b/cranelift/filetests/filetests/isa/aarch64/bti_with_csdb.clif@@ -48,6 +48,7 @@ block5(v5: i32):; movz w5, #1; b label5; block4:+; bti j; movz w5, #4; b label5; block5:@@ -73,18 +74,19 @@ block5(v5: i32):; block2: ; offset 0x34; bti j; mov w5, #3-; b #0x5c+; b #0x60; block3: ; offset 0x40; bti j; mov w5, #2-; b #0x5c+; b #0x60; block4: ; offset 0x4c; bti j; mov w5, #1-; b #0x5c+; b #0x60; block5: ; offset 0x58+; bti j; mov w5, #4-; block6: ; offset 0x5c+; block6: ; offset 0x60; add w0, w0, w5; ret@@ -119,6 +121,7 @@ block2:; add x0, x0, #42; ret; block2:+; bti j; mov x0, x8; ret;@@ -144,6 +147,7 @@ block2:; add x0, x0, #0x2a; ret; block3: ; offset 0x44+; bti j; mov x0, x8; retdiff --git a/cranelift/filetests/filetests/runtests/return-call-indirect.clif b/cranelift/filetests/filetests/runtests/return-call-indirect.clifindex 393245583f..dcc33484e6 100644--- a/cranelift/filetests/filetests/runtests/return-call-indirect.clif+++ b/cranelift/filetests/filetests/runtests/return-call-indirect.clif@@ -7,6 +7,8 @@ target x86_64target aarch64target aarch64 sign_return_addresstarget aarch64 has_pauth sign_return_address+target aarch64 use_bti+target aarch64 use_bti sign_return_addresstarget riscv64target riscv64 has_c has_zcbtarget s390xdiff --git a/cranelift/filetests/filetests/runtests/try_call.clif b/cranelift/filetests/filetests/runtests/try_call.clifindex ffdc80d9f0..c73806fa2a 100644--- a/cranelift/filetests/filetests/runtests/try_call.clif+++ b/cranelift/filetests/filetests/runtests/try_call.clif@@ -3,6 +3,8 @@ target x86_64target aarch64target aarch64 sign_return_addresstarget aarch64 has_pauth sign_return_address+target aarch64 use_bti+target aarch64 use_bti sign_return_address;; target s390x -- not yettarget riscv64target riscv64 has_c has_zcb