|
| 1 | +use rustc_errors::DiagCtxtHandle; |
| 2 | +use rustc_hir as hir; |
| 3 | +use rustc_hir::HirId; |
| 4 | +use rustc_middle::ty::layout::LayoutError; |
| 5 | +use rustc_middle::ty::{self, ParamEnv, TyCtxt}; |
| 6 | +use rustc_span::Span; |
| 7 | +use rustc_target::spec::abi; |
| 8 | + |
| 9 | +use crate::errors; |
| 10 | + |
| 11 | +/// Check conditions on inputs and outputs that the cmse ABIs impose: arguments and results MUST be |
| 12 | +/// returned via registers (i.e. MUST NOT spill to the stack). LLVM will also validate these |
| 13 | +/// conditions, but by checking them here rustc can emit nicer error messages. |
| 14 | +pub fn validate_cmse_abi<'tcx>( |
| 15 | + tcx: TyCtxt<'tcx>, |
| 16 | + dcx: DiagCtxtHandle<'_>, |
| 17 | + hir_id: HirId, |
| 18 | + abi: abi::Abi, |
| 19 | + fn_sig: ty::PolyFnSig<'tcx>, |
| 20 | +) { |
| 21 | + if let abi::Abi::CCmseNonSecureCall = abi { |
| 22 | + let hir_node = tcx.hir_node(hir_id); |
| 23 | + let hir::Node::Ty(hir::Ty { |
| 24 | + span: bare_fn_span, |
| 25 | + kind: hir::TyKind::BareFn(bare_fn_ty), |
| 26 | + .. |
| 27 | + }) = hir_node |
| 28 | + else { |
| 29 | + // might happen when this ABI is used incorrectly. That will be handled elsewhere |
| 30 | + return; |
| 31 | + }; |
| 32 | + |
| 33 | + match is_valid_cmse_inputs(tcx, fn_sig) { |
| 34 | + Ok(Ok(())) => {} |
| 35 | + Ok(Err(index)) => { |
| 36 | + // fn(x: u32, u32, u32, u16, y: u16) -> u32, |
| 37 | + // ^^^^^^ |
| 38 | + let span = bare_fn_ty.param_names[index] |
| 39 | + .span |
| 40 | + .to(bare_fn_ty.decl.inputs[index].span) |
| 41 | + .to(bare_fn_ty.decl.inputs.last().unwrap().span); |
| 42 | + let plural = bare_fn_ty.param_names.len() - index != 1; |
| 43 | + dcx.emit_err(errors::CmseCallInputsStackSpill { span, plural }); |
| 44 | + } |
| 45 | + Err(layout_err) => { |
| 46 | + if let Some(err) = cmse_layout_err(layout_err, *bare_fn_span) { |
| 47 | + dcx.emit_err(err); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + match is_valid_cmse_output(tcx, fn_sig) { |
| 53 | + Ok(true) => {} |
| 54 | + Ok(false) => { |
| 55 | + let span = bare_fn_ty.decl.output.span(); |
| 56 | + dcx.emit_err(errors::CmseCallOutputStackSpill { span }); |
| 57 | + } |
| 58 | + Err(layout_err) => { |
| 59 | + if let Some(err) = cmse_layout_err(layout_err, *bare_fn_span) { |
| 60 | + dcx.emit_err(err); |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Returns whether the inputs will fit into the available registers |
| 68 | +fn is_valid_cmse_inputs<'tcx>( |
| 69 | + tcx: TyCtxt<'tcx>, |
| 70 | + fn_sig: ty::PolyFnSig<'tcx>, |
| 71 | +) -> Result<Result<(), usize>, &'tcx LayoutError<'tcx>> { |
| 72 | + let mut span = None; |
| 73 | + let mut accum = 0u64; |
| 74 | + |
| 75 | + for (index, arg_def) in fn_sig.inputs().iter().enumerate() { |
| 76 | + let layout = tcx.layout_of(ParamEnv::reveal_all().and(*arg_def.skip_binder()))?; |
| 77 | + |
| 78 | + let align = layout.layout.align().abi.bytes(); |
| 79 | + let size = layout.layout.size().bytes(); |
| 80 | + |
| 81 | + accum += size; |
| 82 | + accum = accum.next_multiple_of(Ord::max(4, align)); |
| 83 | + |
| 84 | + // i.e. exceeds 4 32-bit registers |
| 85 | + if accum > 16 { |
| 86 | + span = span.or(Some(index)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + match span { |
| 91 | + None => Ok(Ok(())), |
| 92 | + Some(span) => Ok(Err(span)), |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// Returns whether the output will fit into the available registers |
| 97 | +fn is_valid_cmse_output<'tcx>( |
| 98 | + tcx: TyCtxt<'tcx>, |
| 99 | + fn_sig: ty::PolyFnSig<'tcx>, |
| 100 | +) -> Result<bool, &'tcx LayoutError<'tcx>> { |
| 101 | + let mut ret_ty = fn_sig.output().skip_binder(); |
| 102 | + let layout = tcx.layout_of(ParamEnv::reveal_all().and(ret_ty))?; |
| 103 | + let size = layout.layout.size().bytes(); |
| 104 | + |
| 105 | + if size <= 4 { |
| 106 | + return Ok(true); |
| 107 | + } else if size > 8 { |
| 108 | + return Ok(false); |
| 109 | + } |
| 110 | + |
| 111 | + // next we need to peel any repr(transparent) layers off |
| 112 | + 'outer: loop { |
| 113 | + let ty::Adt(adt_def, args) = ret_ty.kind() else { |
| 114 | + break; |
| 115 | + }; |
| 116 | + |
| 117 | + if !adt_def.repr().transparent() { |
| 118 | + break; |
| 119 | + } |
| 120 | + |
| 121 | + // the first field with non-trivial size and alignment must be the data |
| 122 | + for variant_def in adt_def.variants() { |
| 123 | + for field_def in variant_def.fields.iter() { |
| 124 | + let ty = field_def.ty(tcx, args); |
| 125 | + let layout = tcx.layout_of(ParamEnv::reveal_all().and(ty))?; |
| 126 | + |
| 127 | + if !layout.layout.is_1zst() { |
| 128 | + ret_ty = ty; |
| 129 | + continue 'outer; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + Ok(ret_ty == tcx.types.i64 || ret_ty == tcx.types.u64 || ret_ty == tcx.types.f64) |
| 136 | +} |
| 137 | + |
| 138 | +fn cmse_layout_err<'tcx>( |
| 139 | + layout_err: &'tcx LayoutError<'tcx>, |
| 140 | + span: Span, |
| 141 | +) -> Option<crate::errors::CmseCallGeneric> { |
| 142 | + use LayoutError::*; |
| 143 | + |
| 144 | + match layout_err { |
| 145 | + Unknown(ty) => { |
| 146 | + if ty.is_impl_trait() { |
| 147 | + None // prevent double reporting of this error |
| 148 | + } else { |
| 149 | + Some(errors::CmseCallGeneric { span }) |
| 150 | + } |
| 151 | + } |
| 152 | + SizeOverflow(..) | NormalizationFailure(..) | ReferencesError(..) | Cycle(..) => { |
| 153 | + None // not our job to report these |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments