-
Notifications
You must be signed in to change notification settings - Fork 13k
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
add a lint against references to packed fields #72270
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use rustc_middle::mir::visit::{PlaceContext, Visitor}; | ||
use rustc_middle::mir::*; | ||
use rustc_middle::ty::{self, TyCtxt}; | ||
use rustc_session::lint::builtin::UNALIGNED_REFERENCES; | ||
|
||
use crate::transform::{MirPass, MirSource}; | ||
use crate::util; | ||
|
||
pub struct CheckPackedRef; | ||
|
||
impl<'tcx> MirPass<'tcx> for CheckPackedRef { | ||
fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) { | ||
let param_env = tcx.param_env(src.instance.def_id()); | ||
let source_info = SourceInfo::outermost(body.span); | ||
let mut checker = PackedRefChecker { body, tcx, param_env, source_info }; | ||
checker.visit_body(&body); | ||
} | ||
} | ||
|
||
struct PackedRefChecker<'a, 'tcx> { | ||
body: &'a Body<'tcx>, | ||
tcx: TyCtxt<'tcx>, | ||
param_env: ty::ParamEnv<'tcx>, | ||
source_info: SourceInfo, | ||
} | ||
|
||
impl<'a, 'tcx> Visitor<'tcx> for PackedRefChecker<'a, 'tcx> { | ||
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { | ||
// Make sure we know where in the MIR we are. | ||
self.source_info = terminator.source_info; | ||
self.super_terminator(terminator, location); | ||
} | ||
|
||
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) { | ||
// Make sure we know where in the MIR we are. | ||
self.source_info = statement.source_info; | ||
self.super_statement(statement, location); | ||
} | ||
|
||
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) { | ||
if context.is_borrow() { | ||
if util::is_disaligned(self.tcx, self.body, self.param_env, *place) { | ||
let source_info = self.source_info; | ||
let lint_root = self.body.source_scopes[source_info.scope] | ||
.local_data | ||
.as_ref() | ||
.assert_crate_local() | ||
.lint_root; | ||
self.tcx.struct_span_lint_hir( | ||
UNALIGNED_REFERENCES, | ||
lint_root, | ||
source_info.span, | ||
|lint| { | ||
lint.build(&format!("reference to packed field is unaligned",)) | ||
.note( | ||
"fields of packed structs are not properly aligned, and creating \ | ||
a misaligned reference is undefined behavior (even if that \ | ||
reference is never dereferenced)", | ||
) | ||
.emit() | ||
}, | ||
); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ pub mod add_call_guards; | |
pub mod add_moves_for_packed_drops; | ||
pub mod add_retag; | ||
pub mod check_consts; | ||
pub mod check_packed_ref; | ||
pub mod check_unsafety; | ||
pub mod cleanup_post_borrowck; | ||
pub mod const_prop; | ||
|
@@ -211,10 +212,11 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def_id: DefId) -> ConstQualifs { | |
validator.qualifs_in_return_place() | ||
} | ||
|
||
/// Make MIR ready for const evaluation. This is run on all MIR, not just on consts! | ||
fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> Steal<Body<'_>> { | ||
let def_id = def_id.expect_local(); | ||
|
||
// Unsafety check uses the raw mir, so make sure it is run | ||
// Unsafety check uses the raw mir, so make sure it is run. | ||
let _ = tcx.unsafety_check_result(def_id); | ||
|
||
let mut body = tcx.mir_built(def_id).steal(); | ||
|
@@ -230,6 +232,8 @@ fn mir_const(tcx: TyCtxt<'_>, def_id: DefId) -> Steal<Body<'_>> { | |
None, | ||
MirPhase::Const, | ||
&[&[ | ||
// MIR-level lints. | ||
&check_packed_ref::CheckPackedRef, | ||
// What we need to do constant evaluation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't even know what this means... I think this is a leftover from the pre-miri MIR based const evaluator. I think we could just merge the entire There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, maybe we can, I'll leave that to someone else.^^ If you change this, please remember to update the rustc-dev-guide, which is where I learned that this is not just for consts. :) |
||
&simplify::SimplifyCfg::new("initial"), | ||
&rustc_peek::SanityCheck, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#![deny(unaligned_references)] | ||
|
||
#[repr(packed)] | ||
pub struct Good { | ||
data: &'static u32, | ||
data2: [&'static u32; 2], | ||
aligned: [u8; 32], | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
let good = Good { data: &0, data2: [&0, &0], aligned: [0; 32] }; | ||
|
||
let _ = &good.data; //~ ERROR reference to packed field | ||
let _ = &good.data as *const _; //~ ERROR reference to packed field | ||
let _: *const _ = &good.data; //~ ERROR reference to packed field | ||
let _ = &good.data2[0]; //~ ERROR reference to packed field | ||
let _ = &*good.data; // ok, behind a pointer | ||
let _ = &good.aligned; // ok, has align 1 | ||
let _ = &good.aligned[2]; // ok, has align 1 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
error: reference to packed field is unaligned | ||
--> $DIR/unaligned_references.rs:14:17 | ||
| | ||
LL | let _ = &good.data; | ||
| ^^^^^^^^^^ | ||
| | ||
note: the lint level is defined here | ||
--> $DIR/unaligned_references.rs:1:9 | ||
| | ||
LL | #![deny(unaligned_references)] | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) | ||
|
||
error: reference to packed field is unaligned | ||
--> $DIR/unaligned_references.rs:15:17 | ||
| | ||
LL | let _ = &good.data as *const _; | ||
| ^^^^^^^^^^ | ||
| | ||
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) | ||
|
||
error: reference to packed field is unaligned | ||
--> $DIR/unaligned_references.rs:16:27 | ||
| | ||
LL | let _: *const _ = &good.data; | ||
| ^^^^^^^^^^ | ||
| | ||
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) | ||
|
||
error: reference to packed field is unaligned | ||
--> $DIR/unaligned_references.rs:17:17 | ||
| | ||
LL | let _ = &good.data2[0]; | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced) | ||
|
||
error: aborting due to 4 previous errors | ||
|
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.
Looks like an alternative to this would be to add the lint here:
rust/src/librustc_mir_build/build/mod.rs
Line 184 in 31add7e