|
| 1 | +use rustc_hir as hir; |
| 2 | +use rustc_session::{declare_lint, declare_lint_pass}; |
| 3 | + |
| 4 | +use crate::lints::RedefiningRuntimeSymbolsDiag; |
| 5 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 6 | + |
| 7 | +declare_lint! { |
| 8 | + /// The `redefining_runtime_symbols` lint checks for items whose symbol name redefines |
| 9 | + /// a runtime symbols expected by `core` and/or `std`. |
| 10 | + /// |
| 11 | + /// ### Example |
| 12 | + /// |
| 13 | + /// ```rust,compile_fail |
| 14 | + /// #![deny(redefining_runtime_symbols)] |
| 15 | + /// |
| 16 | + /// #[unsafe(no_mangle)] |
| 17 | + /// pub fn strlen() {} // redefines the libc `strlen` function |
| 18 | + /// ``` |
| 19 | + /// |
| 20 | + /// {{produces}} |
| 21 | + /// |
| 22 | + /// ### Explanation |
| 23 | + /// |
| 24 | + /// Up-most care is required when redefining runtime symbols assumed and |
| 25 | + /// used by the standard library. They must follow the C specification, not use any |
| 26 | + /// standard-library facility or undefined behavior may occur. |
| 27 | + /// |
| 28 | + /// The symbols currently checked are respectively: |
| 29 | + /// - from `core`[^1]: `memcpy`, `memmove`, `memset`, `memcmp`, `bcmp`, `strlen` |
| 30 | + /// - from `std`: `read`, `write`, `open`, `close` |
| 31 | + /// |
| 32 | + /// [^1]: https://doc.rust-lang.org/core/index.html#how-to-use-the-core-library |
| 33 | + pub REDEFINING_RUNTIME_SYMBOLS, |
| 34 | + Warn, |
| 35 | + "redefining a symbol used by the standard library" |
| 36 | +} |
| 37 | + |
| 38 | +declare_lint_pass!(RedefiningRuntimeSymbols => [REDEFINING_RUNTIME_SYMBOLS]); |
| 39 | + |
| 40 | +static CORE_RUNTIME_SYMBOLS: &[&str] = &["memcpy", "memmove", "memset", "memcmp", "bcmp", "strlen"]; |
| 41 | + |
| 42 | +static STD_RUNTIME_SYMBOLS: &[&str] = &["open", "read", "write", "close"]; |
| 43 | + |
| 44 | +impl<'tcx> LateLintPass<'tcx> for RedefiningRuntimeSymbols { |
| 45 | + fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { |
| 46 | + // Bail-out if the item is not a function/method or static. |
| 47 | + match item.kind { |
| 48 | + hir::ItemKind::Fn { sig: _, ident: _, generics: _, body: _, has_body: true } |
| 49 | + | hir::ItemKind::Static(..) => {} |
| 50 | + _ => return, |
| 51 | + } |
| 52 | + |
| 53 | + // Compute the symbol name of our item (without mangling, as our mangling cannot ever |
| 54 | + // conflict with runtime symbols). |
| 55 | + let Some(symbol_name) = rustc_symbol_mangling::symbol_name_without_mangling( |
| 56 | + cx.tcx, |
| 57 | + rustc_middle::ty::InstanceKind::Item(item.owner_id.to_def_id()), |
| 58 | + ) else { |
| 59 | + return; |
| 60 | + }; |
| 61 | + |
| 62 | + if CORE_RUNTIME_SYMBOLS.contains(&&*symbol_name) |
| 63 | + || STD_RUNTIME_SYMBOLS.contains(&&*symbol_name) |
| 64 | + { |
| 65 | + cx.emit_span_lint( |
| 66 | + REDEFINING_RUNTIME_SYMBOLS, |
| 67 | + item.span, |
| 68 | + RedefiningRuntimeSymbolsDiag { symbol_name }, |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments