Skip to content

Commit dbce836

Browse files
Add a Lint for Pointer to Integer Transmutes in Consts
1 parent 5d9b908 commit dbce836

File tree

10 files changed

+192
-18
lines changed

10 files changed

+192
-18
lines changed

compiler/rustc_lint_defs/src/builtin.rs

+35
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ declare_lint_pass! {
8282
PRIVATE_INTERFACES,
8383
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
8484
PTR_CAST_ADD_AUTO_TO_OBJECT,
85+
PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
8586
PUB_USE_OF_PRIVATE_EXTERN_CRATE,
8687
REDUNDANT_IMPORTS,
8788
REDUNDANT_LIFETIMES,
@@ -5095,3 +5096,37 @@ declare_lint! {
50955096
reference: "issue #124535 <https://github.com/rust-lang/rust/issues/124535>",
50965097
};
50975098
}
5099+
5100+
declare_lint! {
5101+
/// The `ptr_to_integer_transmute_in_consts` lint detects pointer to integer
5102+
/// transmute in const functions and associated constants.
5103+
///
5104+
/// ### Example
5105+
///
5106+
/// ```rust,ignore (causes a warning)
5107+
/// const fn foo(ptr: *const u8) -> usize {
5108+
/// unsafe {
5109+
/// std::mem::transmute::<*const u8, usize>(ptr)
5110+
/// }
5111+
/// }
5112+
/// ```
5113+
///
5114+
/// {{produces}}
5115+
///
5116+
/// ### Explanation
5117+
///
5118+
/// Transmuting pointers to integers in a `const` context is undefined behavior.
5119+
/// Any attempt to use the resulting integer will abort const-evaluation.
5120+
///
5121+
/// But sometimes the compiler might not emit an error for pointer to integer transmutes
5122+
/// inside const functions and associated consts because they are evaluated only when referenced.
5123+
/// Therefore, this lint serves as an extra layer of defense to prevent any undefined behavior
5124+
/// from compiling without any warnings or errors.
5125+
///
5126+
/// See [std::mem::transmute] in the reference for more details.
5127+
///
5128+
/// [std::mem::transmute]: https://doc.rust-lang.org/std/mem/fn.transmute.html
5129+
pub PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
5130+
Warn,
5131+
"detects pointer to integer transmutes in const functions and associated constants",
5132+
}

compiler/rustc_mir_transform/messages.ftl

+5
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,8 @@ mir_transform_unaligned_packed_ref = reference to packed field is unaligned
2727
.note = packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
2828
.note_ub = creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
2929
.help = copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
30+
31+
mir_transform_undefined_transmute = pointers cannot be transmuted to integers during const eval
32+
.note = at compile-time, pointers do not have an integer value
33+
.note2 = avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior
34+
.help = for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use rustc_middle::mir::visit::Visitor;
2+
use rustc_middle::mir::{Body, Location, Operand, Terminator, TerminatorKind};
3+
use rustc_middle::ty::{AssocItem, AssocKind, TyCtxt};
4+
use rustc_session::lint::builtin::PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS;
5+
use rustc_span::sym;
6+
7+
use crate::errors;
8+
9+
/// Check for transmutes that exhibit undefined behavior.
10+
/// For example, transmuting pointers to integers in a const context.
11+
pub(super) struct CheckUndefinedTransmutes;
12+
13+
impl<'tcx> crate::MirLint<'tcx> for CheckUndefinedTransmutes {
14+
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
15+
let mut checker = UndefinedTransmutesChecker { body, tcx };
16+
checker.visit_body(body);
17+
}
18+
}
19+
20+
struct UndefinedTransmutesChecker<'a, 'tcx> {
21+
body: &'a Body<'tcx>,
22+
tcx: TyCtxt<'tcx>,
23+
}
24+
25+
impl<'a, 'tcx> UndefinedTransmutesChecker<'a, 'tcx> {
26+
// This functions checks two things:
27+
// 1. `function` takes a raw pointer as input and returns an integer as output.
28+
// 2. `function` is called from a const function or an associated constant.
29+
//
30+
// Why do we consider const functions and associated constants only?
31+
//
32+
// Generally, undefined behavior in const items are handled by the evaluator.
33+
// But, const functions and associated constants are evaluated only when referenced.
34+
// This can result in undefined behavior in a library going unnoticed until
35+
// the function or constant is actually used.
36+
//
37+
// Therefore, we only consider const functions and associated constants here and leave
38+
// other const items to be handled by the evaluator.
39+
fn is_ptr_to_int_in_const(&self, function: &Operand<'tcx>) -> bool {
40+
let def_id = self.body.source.def_id();
41+
42+
if self.tcx.is_const_fn(def_id)
43+
|| matches!(
44+
self.tcx.opt_associated_item(def_id),
45+
Some(AssocItem { kind: AssocKind::Const, .. })
46+
)
47+
{
48+
let fn_sig = function.ty(self.body, self.tcx).fn_sig(self.tcx).skip_binder();
49+
if let [input] = fn_sig.inputs() {
50+
return input.is_unsafe_ptr() && fn_sig.output().is_integral();
51+
}
52+
}
53+
false
54+
}
55+
}
56+
57+
impl<'tcx> Visitor<'tcx> for UndefinedTransmutesChecker<'_, 'tcx> {
58+
// Check each block's terminator for calls to pointer to integer transmutes
59+
// in const functions or associated constants and emit a lint.
60+
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
61+
if let TerminatorKind::Call { func, .. } = &terminator.kind
62+
&& let Some((func_def_id, _)) = func.const_fn_def()
63+
&& self.tcx.is_intrinsic(func_def_id, sym::transmute)
64+
&& self.is_ptr_to_int_in_const(func)
65+
&& let Some(call_id) = self.body.source.def_id().as_local()
66+
{
67+
let hir_id = self.tcx.local_def_id_to_hir_id(call_id);
68+
let span = self.body.source_info(location).span;
69+
self.tcx.emit_node_span_lint(
70+
PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS,
71+
hir_id,
72+
span,
73+
errors::UndefinedTransmute,
74+
);
75+
}
76+
}
77+
}

compiler/rustc_mir_transform/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,10 @@ pub(crate) struct MustNotSuspendReason {
121121
pub span: Span,
122122
pub reason: String,
123123
}
124+
125+
#[derive(LintDiagnostic)]
126+
#[diag(mir_transform_undefined_transmute)]
127+
#[note]
128+
#[note(mir_transform_note2)]
129+
#[help]
130+
pub(crate) struct UndefinedTransmute;

compiler/rustc_mir_transform/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ mod add_subtyping_projections;
5151
mod check_alignment;
5252
mod check_const_item_mutation;
5353
mod check_packed_ref;
54+
mod check_undefined_transmutes;
5455
// This pass is public to allow external drivers to perform MIR cleanup
5556
pub mod cleanup_post_borrowck;
5657
mod copy_prop;
@@ -293,6 +294,7 @@ fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
293294
&Lint(check_packed_ref::CheckPackedRef),
294295
&Lint(check_const_item_mutation::CheckConstItemMutation),
295296
&Lint(function_item_references::FunctionItemReferences),
297+
&Lint(check_undefined_transmutes::CheckUndefinedTransmutes),
296298
// What we need to do constant evaluation.
297299
&simplify::SimplifyCfg::Initial,
298300
&Lint(sanity_check::SanityCheck),

library/core/src/ptr/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,7 @@ pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
19161916
/// than trying to adapt this to accommodate that change.
19171917
///
19181918
/// Any questions go to @nagisa.
1919+
#[cfg_attr(not(bootstrap), allow(ptr_to_integer_transmute_in_consts))]
19191920
#[lang = "align_offset"]
19201921
pub(crate) const unsafe fn align_offset<T: Sized>(p: *const T, a: usize) -> usize {
19211922
// FIXME(#75598): Direct use of these intrinsics improves codegen significantly at opt-level <=

src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.fixed

+6-3
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ fn issue_10449() {
8484
}
8585

8686
// Pointers cannot be cast to integers in const contexts
87+
#[allow(ptr_to_integer_transmute_in_consts, reason = "This is tested in the compiler test suite")]
8788
const fn issue_12402<P>(ptr: *const P) {
88-
unsafe { transmute::<*const i32, usize>(&42i32) };
89-
unsafe { transmute::<fn(*const P), usize>(issue_12402) };
90-
let _ = unsafe { transmute::<_, usize>(ptr) };
89+
// This test exists even though the compiler lints against it
90+
// to test that clippy's transmute lints do not trigger on this.
91+
unsafe { std::mem::transmute::<*const i32, usize>(&42i32) };
92+
unsafe { std::mem::transmute::<fn(*const P), usize>(issue_12402) };
93+
let _ = unsafe { std::mem::transmute::<_, usize>(ptr) };
9194
}

src/tools/clippy/tests/ui/transmutes_expressible_as_ptr_casts.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,11 @@ fn issue_10449() {
8484
}
8585

8686
// Pointers cannot be cast to integers in const contexts
87+
#[allow(ptr_to_integer_transmute_in_consts, reason = "This is tested in the compiler test suite")]
8788
const fn issue_12402<P>(ptr: *const P) {
88-
unsafe { transmute::<*const i32, usize>(&42i32) };
89-
unsafe { transmute::<fn(*const P), usize>(issue_12402) };
90-
let _ = unsafe { transmute::<_, usize>(ptr) };
89+
// This test exists even though the compiler lints against it
90+
// to test that clippy's transmute lints do not trigger on this.
91+
unsafe { std::mem::transmute::<*const i32, usize>(&42i32) };
92+
unsafe { std::mem::transmute::<fn(*const P), usize>(issue_12402) };
93+
let _ = unsafe { std::mem::transmute::<_, usize>(ptr) };
9194
}

tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
11
const fn foo(ptr: *const u8) -> usize {
22
unsafe {
33
std::mem::transmute(ptr)
4-
//~^ ERROR pointers cannot be transmuted to integers
4+
//~^ WARN pointers cannot be transmuted to integers
55
}
66
}
77

88
trait Human {
9-
const ID: u64 = {
9+
const ID: usize = {
1010
let value = 10;
11-
let ptr: *const i32 = &value;
11+
let ptr: *const usize = &value;
1212
unsafe {
1313
std::mem::transmute(ptr)
14-
//~^ ERROR pointers cannot be transmuted to integers
14+
//~^ WARN pointers cannot be transmuted to integers
1515
}
1616
};
1717

18-
fn id_plus_one() -> u64 {
18+
fn id_plus_one() -> usize {
1919
Self::ID + 1
2020
}
2121
}
2222

2323
struct Type<T>(T);
2424

2525
impl<T> Type<T> {
26-
const ID: u64 = {
26+
const ID: usize = {
2727
let value = 10;
28-
let ptr: *const i32 = &value;
28+
let ptr: *const usize = &value;
2929
unsafe {
3030
std::mem::transmute(ptr)
31-
//~^ ERROR pointers cannot be transmuted to integers
31+
//~^ WARN pointers cannot be transmuted to integers
3232
}
3333
};
3434

35-
fn id_plus_one() -> u64 {
35+
fn id_plus_one() -> usize {
3636
Self::ID + 1
3737
}
3838
}
@@ -59,10 +59,10 @@ impl ControlStruct {
5959
const fn zoom(ptr: *const u8) -> usize {
6060
unsafe {
6161
std::mem::transmute(ptr)
62-
//~^ ERROR pointers cannot be transmuted to integers
62+
//~^ WARN pointers cannot be transmuted to integers
6363
}
6464
}
65-
65+
6666
fn main() {
6767
const a: u8 = 10;
6868
const value: usize = zoom(&a);

tests/ui/consts/const-eval/ptr-to-int-transmute-in-consts-issue-87525.stderr

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
warning: pointers cannot be transmuted to integers during const eval
2+
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:61:9
3+
|
4+
LL | std::mem::transmute(ptr)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: at compile-time, pointers do not have an integer value
8+
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior
9+
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html
10+
= note: `#[warn(ptr_to_integer_transmute_in_consts)]` on by default
11+
112
error[E0080]: evaluation of constant value failed
213
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:68:26
314
|
@@ -7,6 +18,36 @@ LL | const value: usize = zoom(&a);
718
= help: this code performed an operation that depends on the underlying bytes representing a pointer
819
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
920

10-
error: aborting due to 1 previous error
21+
warning: pointers cannot be transmuted to integers during const eval
22+
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:3:9
23+
|
24+
LL | std::mem::transmute(ptr)
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
= note: at compile-time, pointers do not have an integer value
28+
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior
29+
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html
30+
31+
warning: pointers cannot be transmuted to integers during const eval
32+
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:13:13
33+
|
34+
LL | std::mem::transmute(ptr)
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^
36+
|
37+
= note: at compile-time, pointers do not have an integer value
38+
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior
39+
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html
40+
41+
warning: pointers cannot be transmuted to integers during const eval
42+
--> $DIR/ptr-to-int-transmute-in-consts-issue-87525.rs:30:13
43+
|
44+
LL | std::mem::transmute(ptr)
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^
46+
|
47+
= note: at compile-time, pointers do not have an integer value
48+
= note: avoiding this restriction via `union` or raw pointers leads to compile-time undefined behavior
49+
= help: for more information, see https://doc.rust-lang.org/std/mem/fn.transmute.html
50+
51+
error: aborting due to 1 previous error; 4 warnings emitted
1152

1253
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)