|
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
| 2 | +use rustc_ast::InlineAsmOptions; |
| 3 | +use rustc_hir::{Expr, ExprKind, InlineAsm, InlineAsmOperand}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::declare_lint_pass; |
| 6 | +use rustc_span::Span; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// Checks if any pointer is being passed to an asm! block with `nomem` option. |
| 11 | + /// |
| 12 | + /// ### Why is this bad? |
| 13 | + /// `nomem` forbids any reads or writes to memory and passing a pointer suggests |
| 14 | + /// that either of those will happen. |
| 15 | + /// |
| 16 | + /// ### Example |
| 17 | + /// ```no_run |
| 18 | + /// fn f(p: *mut u32) { |
| 19 | + /// unsafe { core::arch::asm!("mov [{p}], 42", p = in(reg) p, options(nomem, nostack)); } |
| 20 | + /// } |
| 21 | + /// ``` |
| 22 | + /// Use instead: |
| 23 | + /// ```no_run |
| 24 | + /// fn f(p: *mut u32) { |
| 25 | + /// unsafe { core::arch::asm!("mov [{p}], 42", p = in(reg) p, options(nostack)); } |
| 26 | + /// } |
| 27 | + /// ``` |
| 28 | + #[clippy::version = "1.81.0"] |
| 29 | + pub POINTERS_IN_NOMEM_ASM_BLOCK, |
| 30 | + suspicious, |
| 31 | + "pointers in nomem asm block" |
| 32 | +} |
| 33 | + |
| 34 | +declare_lint_pass!(PointersInNomemAsmBlock => [POINTERS_IN_NOMEM_ASM_BLOCK]); |
| 35 | + |
| 36 | +impl<'tcx> LateLintPass<'tcx> for PointersInNomemAsmBlock { |
| 37 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { |
| 38 | + if let ExprKind::InlineAsm(asm) = &expr.kind { |
| 39 | + check_asm(cx, asm); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn check_asm(cx: &LateContext<'_>, asm: &InlineAsm<'_>) { |
| 45 | + if !asm.options.contains(InlineAsmOptions::NOMEM) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + let spans = asm |
| 50 | + .operands |
| 51 | + .iter() |
| 52 | + .filter(|(op, _span)| has_in_operand_pointer(cx, op)) |
| 53 | + .map(|(_op, span)| *span) |
| 54 | + .collect::<Vec<Span>>(); |
| 55 | + |
| 56 | + if spans.is_empty() { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + span_lint_and_then( |
| 61 | + cx, |
| 62 | + POINTERS_IN_NOMEM_ASM_BLOCK, |
| 63 | + spans, |
| 64 | + "passing pointers to nomem asm block", |
| 65 | + additional_notes, |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +fn has_in_operand_pointer(cx: &LateContext<'_>, asm_op: &InlineAsmOperand<'_>) -> bool { |
| 70 | + let asm_in_expr = match asm_op { |
| 71 | + InlineAsmOperand::SymStatic { .. } |
| 72 | + | InlineAsmOperand::Out { .. } |
| 73 | + | InlineAsmOperand::Const { .. } |
| 74 | + | InlineAsmOperand::SymFn { .. } |
| 75 | + | InlineAsmOperand::Label { .. } => return false, |
| 76 | + InlineAsmOperand::SplitInOut { in_expr, .. } => in_expr, |
| 77 | + InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => expr, |
| 78 | + }; |
| 79 | + |
| 80 | + // This checks for raw ptrs, refs and function pointers - the last one |
| 81 | + // also technically counts as reading memory. |
| 82 | + cx.typeck_results().expr_ty(asm_in_expr).is_any_ptr() |
| 83 | +} |
| 84 | + |
| 85 | +fn additional_notes(diag: &mut rustc_errors::Diag<'_, ()>) { |
| 86 | + diag.note("`nomem` means that no memory write or read happens inside the asm! block"); |
| 87 | + diag.note("if this is intentional and no pointers are read or written to, consider allowing the lint"); |
| 88 | +} |
0 commit comments