-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add lint about redefining runtime symbols #146505
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
3
commits into
rust-lang:master
Choose a base branch
from
Urgau:clash-fn-names-with-fundamental-fns
base: master
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
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use rustc_hir as hir; | ||
use rustc_session::{declare_lint, declare_lint_pass}; | ||
|
||
use crate::lints::RedefiningRuntimeSymbolsDiag; | ||
use crate::{LateContext, LateLintPass, LintContext}; | ||
|
||
declare_lint! { | ||
/// The `redefining_runtime_symbols` lint checks for items whose symbol name redefines | ||
/// a runtime symbols expected by `core` and/or `std`. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// #![deny(redefining_runtime_symbols)] | ||
/// | ||
/// #[unsafe(no_mangle)] | ||
/// pub fn strlen() {} // redefines the libc `strlen` function | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// Up-most care is required when redefining 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 respectively: | ||
/// - from `core`[^1]: `memcpy`, `memmove`, `memset`, `memcmp`, `bcmp`, `strlen` | ||
/// - from `std`: `open`/`open64`, `read`, `write`, `close` | ||
/// | ||
/// [^1]: https://doc.rust-lang.org/core/index.html#how-to-use-the-core-library | ||
pub REDEFINING_RUNTIME_SYMBOLS, | ||
Warn, | ||
"redefining a symbol used by the standard library" | ||
} | ||
|
||
declare_lint_pass!(RedefiningRuntimeSymbols => [REDEFINING_RUNTIME_SYMBOLS]); | ||
|
||
static CORE_RUNTIME_SYMBOLS: &[&str] = &["memcpy", "memmove", "memset", "memcmp", "bcmp", "strlen"]; | ||
|
||
static STD_RUNTIME_SYMBOLS: &[&str] = &["open", "open64", "read", "write", "close"]; | ||
|
||
impl<'tcx> LateLintPass<'tcx> for RedefiningRuntimeSymbols { | ||
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: true } | ||
| hir::ItemKind::Static(..) => {} | ||
_ => return, | ||
} | ||
|
||
// Compute the symbol name of our item (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; | ||
}; | ||
|
||
if CORE_RUNTIME_SYMBOLS.contains(&&*symbol_name) | ||
|| STD_RUNTIME_SYMBOLS.contains(&&*symbol_name) | ||
{ | ||
cx.emit_span_lint( | ||
REDEFINING_RUNTIME_SYMBOLS, | ||
item.span, | ||
RedefiningRuntimeSymbolsDiag { symbol_name }, | ||
); | ||
} | ||
} | ||
} |
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,63 @@ | ||
//@ check-pass | ||
//@ edition: 2021 | ||
|
||
use std::ffi::c_void; | ||
|
||
// From core | ||
|
||
#[no_mangle] | ||
pub extern "C" fn memcpy( | ||
dest: *mut c_void, | ||
src: *const c_void, | ||
n: i64, | ||
) -> *mut c_void { std::ptr::null_mut() } | ||
//~^^^^^ WARN redefinition of the runtime `memcpy` symbol | ||
|
||
#[no_mangle] | ||
pub fn memmove() {} | ||
//~^ WARN redefinition of the runtime `memmove` symbol | ||
|
||
#[no_mangle] | ||
pub fn memset() {} | ||
//~^ WARN redefinition of the runtime `memset` symbol | ||
|
||
#[no_mangle] | ||
pub fn memcmp() {} | ||
//~^ WARN redefinition of the runtime `memcmp` symbol | ||
|
||
#[export_name = "bcmp"] | ||
pub fn bcmp_() {} | ||
//~^ WARN redefinition of the runtime `bcmp` symbol | ||
|
||
#[no_mangle] | ||
pub static strlen: () = (); | ||
//~^ WARN redefinition of the runtime `strlen` symbol | ||
|
||
// From std | ||
|
||
#[no_mangle] | ||
pub fn open() {} | ||
//~^ WARN redefinition of the runtime `open` symbol | ||
|
||
#[no_mangle] | ||
pub fn open64() {} | ||
//~^ WARN redefinition of the runtime `open64` symbol | ||
|
||
#[export_name = "read"] | ||
pub async fn read1() {} | ||
//~^ WARN redefinition of the runtime `read` symbol | ||
|
||
#[export_name = "write"] | ||
pub fn write1() {} | ||
//~^ WARN redefinition of the runtime `write` symbol | ||
|
||
#[export_name = "close"] | ||
pub fn close_() {} | ||
//~^ WARN redefinition of the runtime `close` symbol | ||
|
||
extern "C" { | ||
// No warning, not a body. | ||
pub fn close(a: i32) -> i32; | ||
} | ||
|
||
fn main() {} |
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.
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.
What is the significance of documenting a split between
core
andstd
? It looks like they get checked the same.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.
It's not very important; it's mainly a way to distinguish between explicitly documented symbols (those in core) and those that are more implicit (those in std).
Regarding the checking, it could be asked to not check the std runtime symbols in
#![no_std]
, which I don't think we should do,#![no_std]
crates may still end-up in std-full crates.