-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Lint on some incorrect uses of mem::zeroed / mem::uninitialized #63346
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
da6fbb1
add basic lint testing for misuse of mem::zeroed and mem::uninitialized
RalfJung ca1e94b
warn for more cases
RalfJung fbd5613
fix a comment
RalfJung 8e6fbbe
add tuple_fields convenience method and use it in a few places
RalfJung 4b062a1
note a FIXME
RalfJung 3972d05
proper doc comment for 'recovered' field of variant
RalfJung c5a6356
allow the lint if a few UB-demonstrating doc tests
RalfJung 5c77a17
note down some more future plans
RalfJung 0930747
update clippy
RalfJung 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
|
||
use rustc::hir::def::{Res, DefKind}; | ||
use rustc::hir::def_id::{DefId, LOCAL_CRATE}; | ||
use rustc::ty::{self, Ty, TyCtxt}; | ||
use rustc::ty::{self, Ty, TyCtxt, layout::VariantIdx}; | ||
use rustc::{lint, util}; | ||
use hir::Node; | ||
use util::nodemap::HirIdSet; | ||
|
@@ -1862,3 +1862,92 @@ impl EarlyLintPass for IncompleteFeatures { | |
}); | ||
} | ||
} | ||
|
||
declare_lint! { | ||
pub INVALID_VALUE, | ||
Warn, | ||
"an invalid value is being created (such as a NULL reference)" | ||
} | ||
|
||
declare_lint_pass!(InvalidValue => [INVALID_VALUE]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidValue { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &hir::Expr) { | ||
|
||
const ZEROED_PATH: &[Symbol] = &[sym::core, sym::mem, sym::zeroed]; | ||
const UININIT_PATH: &[Symbol] = &[sym::core, sym::mem, sym::uninitialized]; | ||
|
||
/// Return `false` only if we are sure this type does *not* | ||
/// allow zero initialization. | ||
fn ty_maybe_allows_zero_init<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { | ||
use rustc::ty::TyKind::*; | ||
match ty.sty { | ||
// Primitive types that don't like 0 as a value. | ||
Ref(..) | FnPtr(..) | Never => false, | ||
Adt(..) if ty.is_box() => false, | ||
// Recurse for some compound types. | ||
Adt(adt_def, substs) if !adt_def.is_union() => { | ||
match adt_def.variants.len() { | ||
0 => false, // Uninhabited enum! | ||
1 => { | ||
// Struct, or enum with exactly one variant. | ||
// Proceed recursively, check all fields. | ||
let variant = &adt_def.variants[VariantIdx::from_u32(0)]; | ||
variant.fields.iter().all(|field| { | ||
ty_maybe_allows_zero_init( | ||
tcx, | ||
field.ty(tcx, substs), | ||
) | ||
}) | ||
} | ||
_ => true, // Conservative fallback for multi-variant enum. | ||
} | ||
} | ||
Tuple(..) => { | ||
// Proceed recursively, check all fields. | ||
ty.tuple_fields().all(|field| ty_maybe_allows_zero_init(tcx, field)) | ||
} | ||
// FIXME: Would be nice to also warn for `NonNull`/`NonZero*`. | ||
// FIXME: *Only for `mem::uninitialized`*, we could also warn for `bool`, | ||
// `char`, and any multivariant enum. | ||
// Conservative fallback. | ||
_ => true, | ||
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. Can you check for types with 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. In the future this should probably check for that attribute, yes. |
||
} | ||
} | ||
|
||
if let hir::ExprKind::Call(ref path_expr, ref _args) = expr.node { | ||
if let hir::ExprKind::Path(ref qpath) = path_expr.node { | ||
if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() { | ||
if cx.match_def_path(def_id, &ZEROED_PATH) || | ||
cx.match_def_path(def_id, &UININIT_PATH) | ||
{ | ||
// This conjures an instance of a type out of nothing, | ||
// using zeroed or uninitialized memory. | ||
// We are extremely conservative with what we warn about. | ||
let conjured_ty = cx.tables.expr_ty(expr); | ||
|
||
if !ty_maybe_allows_zero_init(cx.tcx, conjured_ty) { | ||
cx.struct_span_lint( | ||
INVALID_VALUE, | ||
expr.span, | ||
&format!( | ||
"the type `{}` does not permit {}", | ||
conjured_ty, | ||
if cx.match_def_path(def_id, &ZEROED_PATH) { | ||
"zero-initialization" | ||
} else { | ||
"being left uninitialized" | ||
} | ||
), | ||
) | ||
.note("this means that this code causes undefined behavior \ | ||
when executed") | ||
.help("use `MaybeUninit` instead") | ||
.emit(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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,58 @@ | ||
// ignore-tidy-linelength | ||
// This test checks that calling `mem::{uninitialized,zeroed}` with certain types results | ||
// in a lint. | ||
|
||
#![feature(never_type)] | ||
#![allow(deprecated)] | ||
#![deny(invalid_value)] | ||
|
||
use std::mem::{self, MaybeUninit}; | ||
|
||
enum Void {} | ||
|
||
struct Ref(&'static i32); | ||
|
||
struct Wrap<T> { wrapped: T } | ||
|
||
#[allow(unused)] | ||
fn generic<T: 'static>() { | ||
unsafe { | ||
let _val: &'static T = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: &'static T = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Wrap<&'static T> = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Wrap<&'static T> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
} | ||
} | ||
|
||
fn main() { | ||
unsafe { | ||
let _val: ! = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: ! = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: (i32, !) = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: (i32, !) = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Void = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Void = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: &'static i32 = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: &'static i32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Ref = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Ref = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: fn() = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: fn() = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
let _val: Wrap<fn()> = mem::zeroed(); //~ ERROR: does not permit zero-initialization | ||
let _val: Wrap<fn()> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized | ||
|
||
// Some types that should work just fine. | ||
let _val: Option<&'static i32> = mem::zeroed(); | ||
let _val: Option<fn()> = mem::zeroed(); | ||
let _val: MaybeUninit<&'static i32> = mem::zeroed(); | ||
let _val: bool = mem::zeroed(); | ||
let _val: i32 = mem::zeroed(); | ||
} | ||
} |
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.
I used the "validity" terminology here. Does that seem okay?