-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add a Lint for Pointer to Integer Transmutes in Consts #130540
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
77 changes: 77 additions & 0 deletions
77
compiler/rustc_mir_transform/src/check_undefined_transmutes.rs
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,77 @@ | ||
use rustc_middle::mir::visit::Visitor; | ||
use rustc_middle::mir::{Body, Location, Operand, Terminator, TerminatorKind}; | ||
use rustc_middle::ty::{AssocItem, AssocKind, TyCtxt}; | ||
use rustc_session::lint::builtin::PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS; | ||
use rustc_span::sym; | ||
|
||
use crate::errors; | ||
|
||
/// Check for transmutes that exhibit undefined behavior. | ||
/// For example, transmuting pointers to integers in a const context. | ||
pub(super) struct CheckUndefinedTransmutes; | ||
|
||
impl<'tcx> crate::MirLint<'tcx> for CheckUndefinedTransmutes { | ||
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { | ||
let mut checker = UndefinedTransmutesChecker { body, tcx }; | ||
checker.visit_body(body); | ||
} | ||
} | ||
|
||
struct UndefinedTransmutesChecker<'a, 'tcx> { | ||
body: &'a Body<'tcx>, | ||
tcx: TyCtxt<'tcx>, | ||
} | ||
|
||
impl<'a, 'tcx> UndefinedTransmutesChecker<'a, 'tcx> { | ||
// This functions checks two things: | ||
// 1. `function` takes a raw pointer as input and returns an integer as output. | ||
// 2. `function` is called from a const function or an associated constant. | ||
// | ||
// Why do we consider const functions and associated constants only? | ||
// | ||
// Generally, undefined behavior in const items are handled by the evaluator. | ||
// But, const functions and associated constants are evaluated only when referenced. | ||
// This can result in undefined behavior in a library going unnoticed until | ||
// the function or constant is actually used. | ||
// | ||
// Therefore, we only consider const functions and associated constants here and leave | ||
// other const items to be handled by the evaluator. | ||
fn is_ptr_to_int_in_const(&self, function: &Operand<'tcx>) -> bool { | ||
let def_id = self.body.source.def_id(); | ||
|
||
if self.tcx.is_const_fn(def_id) | ||
|| matches!( | ||
self.tcx.opt_associated_item(def_id), | ||
Some(AssocItem { kind: AssocKind::Const, .. }) | ||
) | ||
{ | ||
let fn_sig = function.ty(self.body, self.tcx).fn_sig(self.tcx).skip_binder(); | ||
if let [input] = fn_sig.inputs() { | ||
return input.is_unsafe_ptr() && fn_sig.output().is_integral(); | ||
} | ||
} | ||
false | ||
} | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for UndefinedTransmutesChecker<'_, 'tcx> { | ||
// Check each block's terminator for calls to pointer to integer transmutes | ||
// in const functions or associated constants and emit a lint. | ||
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { | ||
if let TerminatorKind::Call { func, .. } = &terminator.kind | ||
&& let Some((func_def_id, _)) = func.const_fn_def() | ||
&& self.tcx.is_intrinsic(func_def_id, sym::transmute) | ||
&& self.is_ptr_to_int_in_const(func) | ||
&& let Some(call_id) = self.body.source.def_id().as_local() | ||
{ | ||
let hir_id = self.tcx.local_def_id_to_hir_id(call_id); | ||
let span = self.body.source_info(location).span; | ||
self.tcx.emit_node_span_lint( | ||
PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS, | ||
hir_id, | ||
span, | ||
errors::UndefinedTransmute, | ||
); | ||
} | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs
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,70 @@ | ||
const fn foo(ptr: *const u8) -> usize { | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ WARN pointers cannot be transmuted to integers | ||
} | ||
} | ||
|
||
trait Human { | ||
const ID: usize = { | ||
let value = 10; | ||
let ptr: *const usize = &value; | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ WARN pointers cannot be transmuted to integers | ||
} | ||
}; | ||
|
||
fn id_plus_one() -> usize { | ||
Self::ID + 1 | ||
} | ||
} | ||
|
||
struct Type<T>(T); | ||
|
||
impl<T> Type<T> { | ||
const ID: usize = { | ||
let value = 10; | ||
let ptr: *const usize = &value; | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ WARN pointers cannot be transmuted to integers | ||
} | ||
}; | ||
|
||
fn id_plus_one() -> usize { | ||
Self::ID + 1 | ||
} | ||
} | ||
|
||
fn control(ptr: *const u8) -> usize { | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
} | ||
} | ||
|
||
struct ControlStruct; | ||
|
||
impl ControlStruct { | ||
fn new() -> usize { | ||
let value = 10; | ||
let ptr: *const i32 = &value; | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
} | ||
} | ||
} | ||
|
||
|
||
const fn zoom(ptr: *const u8) -> usize { | ||
unsafe { | ||
std::mem::transmute(ptr) | ||
//~^ WARN pointers cannot be transmuted to integers | ||
} | ||
} | ||
|
||
fn main() { | ||
const a: u8 = 10; | ||
const value: usize = zoom(&a); | ||
//~^ ERROR evaluation of constant value failed | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr
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,53 @@ | ||
warning: pointers cannot be transmuted to integers during const eval | ||
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:61:9 | ||
| | ||
LL | std::mem::transmute(ptr) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: at compile-time, pointers do not have an integer value | ||
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior | ||
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html | ||
= note: `#[warn(ptr_to_integer_transmute_in_consts)]` on by default | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:68:26 | ||
| | ||
LL | const value: usize = zoom(&a); | ||
| ^^^^^^^^ unable to turn pointer into integer | ||
| | ||
= help: this code performed an operation that depends on the underlying bytes representing a pointer | ||
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported | ||
|
||
warning: pointers cannot be transmuted to integers during const eval | ||
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:3:9 | ||
| | ||
LL | std::mem::transmute(ptr) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: at compile-time, pointers do not have an integer value | ||
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior | ||
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html | ||
|
||
warning: pointers cannot be transmuted to integers during const eval | ||
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:13:13 | ||
| | ||
LL | std::mem::transmute(ptr) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: at compile-time, pointers do not have an integer value | ||
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior | ||
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html | ||
|
||
warning: pointers cannot be transmuted to integers during const eval | ||
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:30:13 | ||
| | ||
LL | std::mem::transmute(ptr) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: at compile-time, pointers do not have an integer value | ||
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior | ||
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html | ||
|
||
error: aborting due to 1 previous error; 4 warnings emitted | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
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.