diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index e6498af25d4..a70f966d2e6 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -1,4 +1,5 @@ use std::cmp; +use std::ops::Range; use rustc_abi::{ Align, ArmCall, BackendRepr, CanonAbi, ExternAbi, HasDataLayout, Reg, Size, WrappingRange, @@ -560,39 +561,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { return; } - if self.fn_abi.conv == CanonAbi::Arm(ArmCall::CCmseNonSecureEntry) - && let PassMode::Cast { cast, .. } = &self.fn_abi.ret.mode - { - // The return value of an `extern "cmse-nonsecure-entry"` function crosses the secure - // boundary. Ensure that stale information in padding or otherwise uninitialized memory - // does not leak across the secure boundary. - // - // We zero all bytes that are statically know to be uninitialized, we do not inspect the - // runtime value. - let ret_layout = self.fn_abi.ret.layout; - let uninit_ranges = ret_layout.uninit_ranges(bx.cx()); - if !uninit_ranges.is_empty() { - // Materialize the return value. - let tmp = PlaceRef::alloca(bx, ret_layout); - let op = self.codegen_consume(bx, mir::Place::return_place().as_ref()); - op.val.store(bx, tmp); - - let zero = bx.const_u8(0); - for range in uninit_ranges { - let len = bx.const_usize((range.end - range.start).bytes()); - let offset = bx.const_usize(range.start.bytes()); - - let ptr = bx.inbounds_ptradd(tmp.val.llval, offset); - bx.memset(ptr, zero, len, Align::ONE, MemFlags::empty()); - } - - // Load the value back and return. - let llval = load_cast(bx, cast, tmp.val.llval, ret_layout.align.abi); - bx.ret(llval); - return; - } - } - let llval = match &self.fn_abi.ret.mode { PassMode::Ignore | PassMode::Indirect { .. } => { bx.ret_void(); @@ -634,6 +602,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } ZeroSized => bug!("ZST return value shouldn't be in PassMode::Cast"), }; + + if self.fn_abi.conv == CanonAbi::Arm(ArmCall::CCmseNonSecureEntry) { + // The return value of an `extern "cmse-nonsecure-entry"` function crosses the secure + // boundary. Zero padding and uninitialized bytes so information does not leak. + let ret_layout = self.fn_abi.ret.layout; + let uninit_ranges = ret_layout.uninit_ranges(bx.cx()); + self.zero_byte_ranges(bx, llslot, ret_layout.size, &uninit_ranges); + } + load_cast(bx, cast_ty, llslot, self.fn_abi.ret.layout.align.abi) } }; @@ -1378,6 +1355,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { self.codegen_argument( bx, + fn_abi.conv, op, by_move, &mut llargs, @@ -1388,6 +1366,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let num_untupled = untuple.map(|tup| { self.codegen_arguments_untupled( bx, + fn_abi.conv, &tup.node, &mut llargs, &fn_abi.args[first_args.len()..], @@ -1417,6 +1396,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let last_arg = fn_abi.args.last().unwrap(); self.codegen_argument( bx, + fn_abi.conv, location, /* by_move */ false, &mut llargs, @@ -1733,9 +1713,31 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { } } + fn zero_byte_ranges( + &mut self, + bx: &mut Bx, + ptr: Bx::Value, + limit: Size, + ranges: &[Range], + ) { + let zero = bx.const_u8(0); + + for range in ranges { + let end = cmp::min(range.end, limit); + if range.start >= end { + continue; + } + let offset = bx.const_usize(range.start.bytes()); + let len = bx.const_usize((end - range.start).bytes()); + let ptr = bx.inbounds_ptradd(ptr, offset); + bx.memset(ptr, zero, len, Align::ONE, MemFlags::empty()); + } + } + fn codegen_argument( &mut self, bx: &mut Bx, + conv: CanonAbi, op: OperandRef<'tcx, Bx::Value>, by_move: bool, llargs: &mut Vec, @@ -1859,6 +1861,23 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { MemFlags::empty(), None, ); + + // The arguments of an `extern "cmse-nonsecure-call"` function cross the secure + // boundary. Zero padding bytes so information does not leak. + // + // This only zeroes "guaranteed" padding. There may be more bytes that are + // padding for some but not all variants of this type; those are not zeroed. + // + // Passing an argument with value-dependent padding will instead trigger a lint. + if conv == CanonAbi::Arm(ArmCall::CCmseNonSecureCall) { + self.zero_byte_ranges( + bx, + llscratch, + Size::from_bytes(copy_bytes), + &arg.layout.uninit_ranges(bx.cx()), + ); + } + // ...and then load it with the ABI type. llval = load_cast(bx, cast, llscratch, scratch_align); bx.lifetime_end(llscratch, scratch_size); @@ -1885,6 +1904,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { fn codegen_arguments_untupled( &mut self, bx: &mut Bx, + conv: CanonAbi, operand: &mir::Operand<'tcx>, llargs: &mut Vec, args: &[ArgAbi<'tcx, Ty<'tcx>>], @@ -1904,6 +1924,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { let field = bx.load_operand(field_ptr); self.codegen_argument( bx, + conv, field, by_move, llargs, @@ -1915,7 +1936,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { // If the tuple is immediate, the elements are as well. for i in 0..tuple.layout.fields.count() { let op = tuple.extract_field(self, bx, i); - self.codegen_argument(bx, op, by_move, llargs, &args[i], lifetime_ends_after_call); + self.codegen_argument( + bx, + conv, + op, + by_move, + llargs, + &args[i], + lifetime_ends_after_call, + ); } } tuple.layout.fields.count() diff --git a/tests/assembly-llvm/cmse-clear-padding.rs b/tests/assembly-llvm/cmse-clear-padding.rs index 28be248027b..b092bf304dc 100644 --- a/tests/assembly-llvm/cmse-clear-padding.rs +++ b/tests/assembly-llvm/cmse-clear-padding.rs @@ -15,12 +15,15 @@ // - `uxtb` clears bits 8..32 // - `uxth` clears bits 16..32 // - `bic` clears bits based on a mask +// +// When passing arguments the current implementation of the C ABI already clears padding sometimes, +// but it's not something that can be relied on. extern crate minicore; use minicore::*; #[repr(C)] -pub struct InnerPadding { +struct InnerPadding { a: u8, b: u16, } @@ -29,7 +32,7 @@ pub struct InnerPadding { // CHECK: mov r7, sp // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 #[no_mangle] -pub extern "C" fn c_ret_with_inner_padding(a: u8, b: u16) -> InnerPadding { +extern "C" fn c_ret_with_inner_padding(a: u8, b: u16) -> InnerPadding { InnerPadding { a, b } } @@ -38,12 +41,35 @@ pub extern "C" fn c_ret_with_inner_padding(a: u8, b: u16) -> InnerPadding { // CHECK-NEXT: uxtb r0, r0 // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 #[no_mangle] -pub extern "cmse-nonsecure-entry" fn cmse_ret_with_inner_padding(a: u8, b: u16) -> InnerPadding { +extern "cmse-nonsecure-entry" fn cmse_ret_with_inner_padding(a: u8, b: u16) -> InnerPadding { InnerPadding { a, b } } +// CHECK-LABEL: c_call_with_inner_padding: +// CHECK: mov r7, sp +// CHECK-NEXT: mov r2, r0 +// CHECK-NEXT: bic r0, r1, #65280 +// CHECK-NEXT: pop.w {r7, lr} +#[no_mangle] +extern "C" fn c_call_with_inner_padding(f: unsafe extern "C" fn(InnerPadding), x: InnerPadding) { + unsafe { f(x) } +} + +// CHECK-LABEL: cmse_call_with_inner_padding: +// CHECK: mov r7, sp +// CHECK-NEXT: mov r2, r0 +// CHECK-NEXT: bic r0, r1, #65280 +// CHECK-NEXT: push.w {r4, r5, r6, r7, r8, r9, r10, r11} +#[no_mangle] +extern "C" fn cmse_call_with_inner_padding( + f: unsafe extern "cmse-nonsecure-call" fn(InnerPadding), + x: InnerPadding, +) { + unsafe { f(x) } +} + #[repr(C)] -pub struct TrailingPadding { +struct TrailingPadding { a: u16, b: u8, } @@ -52,7 +78,7 @@ pub struct TrailingPadding { // CHECK: mov r7, sp // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 #[no_mangle] -pub extern "C" fn c_ret_with_trailing_padding(a: u16, b: u8) -> TrailingPadding { +extern "C" fn c_ret_with_trailing_padding(a: u16, b: u8) -> TrailingPadding { TrailingPadding { a, b } } @@ -62,15 +88,38 @@ pub extern "C" fn c_ret_with_trailing_padding(a: u16, b: u8) -> TrailingPadding // CHECK-NEXT: uxth r0, r0 // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 #[no_mangle] -pub extern "cmse-nonsecure-entry" fn cmse_ret_with_trailing_padding( - a: u16, - b: u8, -) -> TrailingPadding { +extern "cmse-nonsecure-entry" fn cmse_ret_with_trailing_padding(a: u16, b: u8) -> TrailingPadding { TrailingPadding { a, b } } +// CHECK-LABEL: c_call_with_trailing_padding: +// CHECK: mov r7, sp +// CHECK-NEXT: mov r2, r0 +// CHECK-NEXT: bic r0, r1, #-16777216 +// CHECK-NEXT: pop.w {r7, lr} +#[no_mangle] +extern "C" fn c_call_with_trailing_padding( + f: unsafe extern "C" fn(TrailingPadding), + x: TrailingPadding, +) { + unsafe { f(x) } +} + +// CHECK-LABEL: cmse_call_with_trailing_padding: +// CHECK: mov r7, sp +// CHECK-NEXT: mov r2, r0 +// CHECK-NEXT: bic r0, r1, #-16777216 +// CHECK-NEXT: push.w {r4, r5, r6, r7, r8, r9, r10, r11} +#[no_mangle] +extern "C" fn cmse_call_with_trailing_padding( + f: unsafe extern "cmse-nonsecure-call" fn(TrailingPadding), + x: TrailingPadding, +) { + unsafe { f(x) } +} + #[repr(C, align(2))] -pub struct WideU8 { +struct WideU8 { a: u8, } @@ -78,7 +127,7 @@ pub struct WideU8 { // CHECK: mov r7, sp // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 #[no_mangle] -pub extern "C" fn c_ret_with_wide_u8(a: u8, b: u8) -> [WideU8; 2] { +extern "C" fn c_ret_with_wide_u8(a: u8, b: u8) -> [WideU8; 2] { [WideU8 { a }, WideU8 { a: b }] } @@ -88,17 +137,53 @@ pub extern "C" fn c_ret_with_wide_u8(a: u8, b: u8) -> [WideU8; 2] { // CHECK-NEXT: uxtb r0, r0 // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 #[no_mangle] -pub extern "cmse-nonsecure-entry" fn cmse_ret_with_wide_u8(a: u8, b: u8) -> [WideU8; 2] { +extern "cmse-nonsecure-entry" fn cmse_ret_with_wide_u8(a: u8, b: u8) -> [WideU8; 2] { [WideU8 { a }, WideU8 { a: b }] } +// CHECK-LABEL: c_call_with_inner_wide_u8: +// CHECK: push {r7, lr} +// CHECK-NEXT: .setfp r7, sp +// CHECK-NEXT: mov r7, sp +// CHECK-NEXT: mov lr, r3 +// CHECK-NEXT: mov r12, r0 +// CHECK-NEXT: ldr r3, [r7, #8] +// CHECK-NEXT: mov r0, r1 +// CHECK-NEXT: mov r1, r2 +// CHECK-NEXT: mov r2, lr +// CHECK-NEXT: pop.w {r7, lr} +// CHECK-NEXT: bx r12 +#[no_mangle] +extern "C" fn c_call_with_inner_wide_u8(f: unsafe extern "C" fn([WideU8; 8]), x: [WideU8; 8]) { + unsafe { f(x) } +} + +// CHECK-LABEL: cmse_call_with_inner_wide_u8: +// CHECK: push {r7, lr} +// CHECK-NEXT: .setfp r7, sp +// CHECK-NEXT: mov r7, sp +// CHECK-NEXT: mov r12, r0 +// CHECK-NEXT: bic r0, r1, #-16711936 +// CHECK-NEXT: bic r1, r2, #-16711936 +// CHECK-NEXT: bic r2, r3, #-16711936 +// CHECK-NEXT: ldr r3, [r7, #8] +// CHECK-NEXT: bic r3, r3, #-16711936 +// CHECK-NEXT: push.w {r4, r5, r6, r7, r8, r9, r10, r11} +#[no_mangle] +extern "C" fn cmse_call_with_inner_wide_u8( + f: unsafe extern "cmse-nonsecure-call" fn([WideU8; 8]), + x: [WideU8; 8], +) { + unsafe { f(x) } +} + // CHECK-LABEL: cmse_ret_with_wide_u8_uninit: // CHECK: mov r7, sp // CHECK-NEXT: uxtb r0, r0 // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 // CHECK-NEXT: bic r0, r0, #-16711936 #[no_mangle] -pub extern "cmse-nonsecure-entry" fn cmse_ret_with_wide_u8_uninit( +extern "cmse-nonsecure-entry" fn cmse_ret_with_wide_u8_uninit( a: u16, b: u16, ) -> [MaybeUninit; 2] { @@ -111,7 +196,7 @@ pub extern "cmse-nonsecure-entry" fn cmse_ret_with_wide_u8_uninit( // CHECK-NEXT: orr.w r0, r0, r1, lsl #16 // CHECK-NEXT: bic r0, r0, #-16711936 #[no_mangle] -pub extern "cmse-nonsecure-entry" fn cmse_ret_with_wide_u8_uninit_tuple( +extern "cmse-nonsecure-entry" fn cmse_ret_with_wide_u8_uninit_tuple( a: u16, b: u16, ) -> (MaybeUninit, MaybeUninit) {