-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Add lint againts invalid runtime symbol definitions #155521
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Urgau
wants to merge
6
commits into
rust-lang:main
Choose a base branch
from
Urgau:runtime-symbols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82f17da
Add function to extract the symbol name from the attributes
Urgau b0d413b
Add declaration-only lang items for core runtime symbols
Urgau 856305b
Add lint against invalid runtime symbol definitions
Urgau 0080494
Handle static whose type is a function pointer
Urgau 0d7a481
Fix invalid `memcpy` definition in codegen test and UI tests
Urgau 0e3b44e
Fix invalid definition of `strlen` in error codes documentation
Urgau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| use rustc_hir::def_id::{DefId, LocalDefId}; | ||
| use rustc_hir::{self as hir, FnSig, ForeignItemKind, LanguageItems}; | ||
| use rustc_infer::infer::DefineOpaqueTypes; | ||
| use rustc_middle::ty::{self, Instance, Ty}; | ||
| use rustc_session::{declare_lint, declare_lint_pass}; | ||
| use rustc_span::Span; | ||
| use rustc_trait_selection::infer::TyCtxtInferExt; | ||
|
|
||
| use crate::lints::RedefiningRuntimeSymbolsDiag; | ||
| use crate::{LateContext, LateLintPass, LintContext}; | ||
|
|
||
| declare_lint! { | ||
| /// The `invalid_runtime_symbol_definitions` lint checks the signature of items whose | ||
| /// symbol name is a runtime symbols expected by `core`. | ||
| /// | ||
| /// ### Example | ||
| /// | ||
| /// ```rust,compile_fail | ||
| /// #[unsafe(no_mangle)] | ||
| /// pub fn strlen() {} // invalid definition of the `strlen` function | ||
| /// ``` | ||
| /// | ||
| /// {{produces}} | ||
| /// | ||
| /// ### Explanation | ||
| /// | ||
| /// Up-most care is required when defining runtime symbols assumed and | ||
| /// used by the standard library. They must follow the C specification, not use any | ||
| /// standard-library facility or undefined behavior may occur. | ||
| /// | ||
| /// The symbols currently checked are `memcpy`, `memmove`, `memset`, `memcmp`, | ||
| /// `bcmp` and `strlen`. | ||
| /// | ||
| /// [^1]: https://doc.rust-lang.org/core/index.html#how-to-use-the-core-library | ||
| pub INVALID_RUNTIME_SYMBOL_DEFINITIONS, | ||
| Deny, | ||
| "invalid definition of a symbol used by the standard library" | ||
| } | ||
|
|
||
| declare_lint_pass!(RuntimeSymbols => [INVALID_RUNTIME_SYMBOL_DEFINITIONS]); | ||
|
|
||
| static EXPECTED_SYMBOLS: &[ExpectedSymbol] = &[ | ||
| ExpectedSymbol { symbol: "memcpy", lang: LanguageItems::memcpy_fn }, | ||
| ExpectedSymbol { symbol: "memmove", lang: LanguageItems::memmove_fn }, | ||
| ExpectedSymbol { symbol: "memset", lang: LanguageItems::memset_fn }, | ||
| ExpectedSymbol { symbol: "memcmp", lang: LanguageItems::memcmp_fn }, | ||
| ExpectedSymbol { symbol: "bcmp", lang: LanguageItems::bcmp_fn }, | ||
| ExpectedSymbol { symbol: "strlen", lang: LanguageItems::strlen_fn }, | ||
| ]; | ||
|
|
||
| #[derive(Copy, Clone, Debug)] | ||
| struct ExpectedSymbol { | ||
| symbol: &'static str, | ||
| lang: fn(&LanguageItems) -> Option<DefId>, | ||
| } | ||
|
|
||
| impl<'tcx> LateLintPass<'tcx> for RuntimeSymbols { | ||
| fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { | ||
| // Bail-out if the item is not a function/method or static. | ||
| match item.kind { | ||
| hir::ItemKind::Fn { sig, ident: _, generics, body: _, has_body: _ } => { | ||
| // Generic functions cannot have the same runtime symbol as we do not allow | ||
| // any symbol attributes. | ||
| if !generics.params.is_empty() { | ||
| return; | ||
| } | ||
|
|
||
| // Try to the overridden symbol name of this function (our mangling | ||
| // cannot ever conflict with runtime symbols, so no need to check for those). | ||
| let Some(symbol_name) = rustc_symbol_mangling::symbol_name_from_attrs( | ||
| cx.tcx, | ||
| rustc_middle::ty::InstanceKind::Item(item.owner_id.to_def_id()), | ||
| ) else { | ||
| return; | ||
| }; | ||
|
|
||
| check_fn(cx, &symbol_name, sig, item.owner_id.def_id); | ||
| } | ||
| hir::ItemKind::Static(..) => { | ||
| // Compute the symbol name of this static (without mangling, as our mangling | ||
| // cannot ever conflict with runtime symbols). | ||
| let Some(symbol_name) = rustc_symbol_mangling::symbol_name_from_attrs( | ||
| cx.tcx, | ||
| rustc_middle::ty::InstanceKind::Item(item.owner_id.to_def_id()), | ||
| ) else { | ||
| return; | ||
| }; | ||
|
|
||
| let def_id = item.owner_id.def_id; | ||
|
|
||
| check_static(cx, &symbol_name, def_id, item.span); | ||
| } | ||
| hir::ItemKind::ForeignMod { abi: _, items } => { | ||
| for item in items { | ||
| let item = cx.tcx.hir_foreign_item(*item); | ||
|
|
||
| let did = item.owner_id.def_id; | ||
| let instance = Instance::new_raw( | ||
| did.to_def_id(), | ||
| ty::List::identity_for_item(cx.tcx, did), | ||
| ); | ||
| let symbol_name = cx.tcx.symbol_name(instance); | ||
|
|
||
| match item.kind { | ||
| ForeignItemKind::Fn(fn_sig, _idents, _generics) => { | ||
| check_fn(cx, &symbol_name.name, fn_sig, did); | ||
| } | ||
| ForeignItemKind::Static(..) => { | ||
| check_static(cx, &symbol_name.name, did, item.span); | ||
| } | ||
| ForeignItemKind::Type => return, | ||
| } | ||
| } | ||
| } | ||
| _ => return, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn check_fn(cx: &LateContext<'_>, symbol_name: &str, sig: FnSig<'_>, did: LocalDefId) { | ||
| let Some(expected_symbol) = EXPECTED_SYMBOLS.iter().find(|es| es.symbol == symbol_name) else { | ||
| // The symbol name does not correspond to a runtime symbols, bail out | ||
| return; | ||
| }; | ||
|
|
||
| let Some(expected_def_id) = (expected_symbol.lang)(&cx.tcx.lang_items()) else { | ||
| // Can't find the corresponding language item, bail out | ||
| return; | ||
| }; | ||
|
|
||
| // Get the two function signatures | ||
| let lang_sig = cx.tcx.normalize_erasing_regions( | ||
| cx.typing_env(), | ||
| cx.tcx.fn_sig(expected_def_id).instantiate_identity(), | ||
| ); | ||
| let user_sig = cx | ||
| .tcx | ||
| .normalize_erasing_regions(cx.typing_env(), cx.tcx.fn_sig(did).instantiate_identity()); | ||
|
|
||
| // Compare the two signatures with an inference context | ||
| let infcx = cx.tcx.infer_ctxt().build(cx.typing_mode()); | ||
| let cause = rustc_middle::traits::ObligationCause::misc(sig.span, did); | ||
| let result = infcx.at(&cause, cx.param_env).eq(DefineOpaqueTypes::No, lang_sig, user_sig); | ||
|
|
||
| // If they don't match, emit our own mismatch signatures | ||
| if let Err(_terr) = result { | ||
| // Create fn pointers for diagnostics purpose | ||
| let expected = Ty::new_fn_ptr(cx.tcx, lang_sig); | ||
| let actual = Ty::new_fn_ptr(cx.tcx, user_sig); | ||
|
|
||
| cx.emit_span_lint( | ||
| INVALID_RUNTIME_SYMBOL_DEFINITIONS, | ||
| sig.span, | ||
| RedefiningRuntimeSymbolsDiag::FnDef { | ||
| symbol_name: symbol_name.to_string(), | ||
| found_fn_sig: actual, | ||
| expected_fn_sig: expected, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| fn check_static<'tcx>(cx: &LateContext<'tcx>, symbol_name: &str, did: LocalDefId, sp: Span) { | ||
| let Some(expected_symbol) = EXPECTED_SYMBOLS.iter().find(|es| es.symbol == symbol_name) else { | ||
| // The symbol name does not correspond to a runtime symbols, bail out | ||
| return; | ||
| }; | ||
|
|
||
| let Some(expected_def_id) = (expected_symbol.lang)(&cx.tcx.lang_items()) else { | ||
| // Can't find the corresponding language item, bail out | ||
| return; | ||
| }; | ||
|
|
||
| // Get the static type | ||
| let static_ty = cx.tcx.type_of(did).instantiate_identity().skip_norm_wip(); | ||
|
|
||
| // Peel Option<...> and get the inner type (see std weak! macro with #[linkage = "extern_weak"]) | ||
| let inner_static_ty: Ty<'_> = match static_ty.kind() { | ||
| ty::Adt(def, args) if Some(def.did()) == cx.tcx.lang_items().option_type() => { | ||
| args.type_at(0) | ||
| } | ||
| _ => static_ty, | ||
| }; | ||
|
|
||
| // Get the expected symbol function signature | ||
| let lang_sig = cx.tcx.normalize_erasing_regions( | ||
| cx.typing_env(), | ||
| cx.tcx.fn_sig(expected_def_id).instantiate_identity(), | ||
| ); | ||
|
|
||
| let expected = Ty::new_fn_ptr(cx.tcx, lang_sig); | ||
|
|
||
| // Compare the expected function signature with the static type, report an error if they don't match | ||
| if expected != inner_static_ty { | ||
| cx.emit_span_lint( | ||
| INVALID_RUNTIME_SYMBOL_DEFINITIONS, | ||
| sp, | ||
| RedefiningRuntimeSymbolsDiag::Static { | ||
| static_ty, | ||
| symbol_name: symbol_name.to_string(), | ||
| expected_fn_sig: expected, | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
View changes since the review