-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
CheckMaybeUninit
MIR transform
This MIR transform inserts the same validity checks from `mem::{uninitialized,zeroed}` to `MaybeUninit::{uninit,zeroed}().assume_init()`. We have been panicking in `mem::uninit` on invalid values for quite some time now, and it has helped to get people off the unsound API and towards using `MaybeUninit<T>`. While correct usage of `MaybeUninit<T>` is clearly documented, some people still use it incorrectly and simply replaced their wrong `mem::uninit` usage with `MaybeUninit::uninit().assume_init()`. This is not any more correct than the old version, and we should still emit panics in these cases. As this can't be done in the library only, we need this MIR pass to insert the calls. For now, it only detects direct usages of `MaybeUninit::uninit().assume_init()` but it could be extended in the future to do more advanced dataflow analysis.
- Loading branch information
Showing
15 changed files
with
712 additions
and
342 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
//! This pass inserts the same validity checks into `MaybeUninit::{uninit,zeroed}().assert_init()` | ||
//! as in `mem::{uninitialized,zeroed}`. | ||
//! | ||
//! Note that this module uses `uninit` to mean `uninit` or `zeroed` unless `zeroed` is used explicitly. | ||
//! | ||
//! It does this by first finding a call to `MaybeUninit::uninit`, and then figuring out | ||
//! whether the successor basic block is a call to `MaybeUninit::assume_init` on the same local. | ||
use rustc_const_eval::interpret; | ||
use rustc_hir::def_id::DefId; | ||
use rustc_middle::mir::patch::MirPatch; | ||
use rustc_middle::mir::{ | ||
BasicBlock, BasicBlockData, Body, Constant, ConstantKind, Operand, Place, SourceInfo, | ||
Terminator, TerminatorKind, | ||
}; | ||
use rustc_middle::ty::{self, List, SubstsRef, TyCtxt}; | ||
use rustc_span::{sym, Span}; | ||
|
||
use crate::MirPass; | ||
|
||
pub struct CheckMaybeUninit; | ||
|
||
impl<'tcx> MirPass<'tcx> for CheckMaybeUninit { | ||
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { | ||
let mut patch = MirPatch::new(body); | ||
|
||
for (mu_uninit_bb, _) in body.basic_blocks.iter_enumerated() { | ||
let terminator = body.basic_blocks[mu_uninit_bb].terminator(); | ||
|
||
let TerminatorKind::Call { | ||
func: mu_uninit_func, | ||
target: assume_init_bb, | ||
destination: uninit_place, | ||
.. | ||
} = &terminator.kind else { | ||
continue; | ||
}; | ||
|
||
let Some((mu_method_def_id, substs)) = mu_uninit_func.const_fn_def() else { | ||
continue; | ||
}; | ||
|
||
let Some(assume_init_bb) = assume_init_bb else { | ||
continue; | ||
}; | ||
|
||
let Some((assume_init_operand, assume_init_call_span)) = is_block_just_assume_init(tcx, &body.basic_blocks[*assume_init_bb]) else { | ||
continue; | ||
}; | ||
|
||
let Some(assume_init_place) = assume_init_operand.place() else { | ||
continue; | ||
}; | ||
|
||
if assume_init_place != *uninit_place { | ||
// The calls here are a little sketchy, but the place that is assumed to be init is not the place that was just crated | ||
// as uninit, so we conservatively bail out. | ||
continue; | ||
} | ||
|
||
// Select the right assertion intrinsic to call depending on which MaybeUninit method we called | ||
let Some(init_check_def_id) = get_init_check_def_id(tcx, mu_method_def_id) else { | ||
continue; | ||
}; | ||
|
||
let assert_valid_bb = make_assert_valid_bb( | ||
&mut patch, | ||
tcx, | ||
assume_init_call_span, | ||
init_check_def_id, | ||
*assume_init_bb, | ||
substs, | ||
); | ||
|
||
let mut new_uninit_terminator = terminator.kind.clone(); | ||
match new_uninit_terminator { | ||
TerminatorKind::Call { ref mut target, .. } => { | ||
*target = Some(assert_valid_bb); | ||
} | ||
_ => unreachable!("terminator must be TerminatorKind::Call as checked above"), | ||
} | ||
|
||
patch.patch_terminator(mu_uninit_bb, new_uninit_terminator); | ||
} | ||
|
||
patch.apply(body); | ||
} | ||
} | ||
|
||
fn is_block_just_assume_init<'tcx, 'blk>( | ||
tcx: TyCtxt<'tcx>, | ||
block: &'blk BasicBlockData<'tcx>, | ||
) -> Option<(&'blk Operand<'tcx>, Span)> { | ||
if block.statements.is_empty() | ||
&& let TerminatorKind::Call { | ||
func, | ||
args, | ||
fn_span, | ||
.. | ||
} = &block.terminator().kind | ||
&& let Some((def_id, _)) = func.const_fn_def() | ||
&& tcx.is_diagnostic_item(sym::assume_init, def_id) | ||
{ | ||
args.get(0).map(|operand| (operand, *fn_span)) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn get_init_check_def_id(tcx: TyCtxt<'_>, mu_method_def_id: DefId) -> Option<DefId> { | ||
if tcx.is_diagnostic_item(sym::maybe_uninit_uninit, mu_method_def_id) { | ||
tcx.lang_items().assert_uninit_valid() | ||
} else if tcx.is_diagnostic_item(sym::maybe_uninit_zeroed, mu_method_def_id) { | ||
tcx.lang_items().assert_zero_valid() | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn make_assert_valid_bb<'tcx>( | ||
patch: &mut MirPatch<'tcx>, | ||
tcx: TyCtxt<'tcx>, | ||
fn_span: Span, | ||
init_check_def_id: DefId, | ||
target_bb: BasicBlock, | ||
substs: SubstsRef<'tcx>, | ||
) -> BasicBlock { | ||
let func = make_fn_operand_for_assert_valid(tcx, init_check_def_id, fn_span, substs); | ||
|
||
let local = patch.new_temp(tcx.types.unit, fn_span); | ||
|
||
let terminator = TerminatorKind::Call { | ||
func, | ||
args: vec![], | ||
destination: Place { local, projection: List::empty() }, | ||
target: Some(target_bb), | ||
cleanup: Some(patch.resume_block()), | ||
from_hir_call: true, | ||
fn_span, | ||
}; | ||
|
||
let terminator = Terminator { source_info: SourceInfo::outermost(fn_span), kind: terminator }; | ||
|
||
let bb_data = BasicBlockData::new(Some(terminator)); | ||
|
||
let block = patch.new_block(bb_data); | ||
block | ||
} | ||
|
||
fn make_fn_operand_for_assert_valid<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
def_id: DefId, | ||
span: Span, | ||
substs: SubstsRef<'tcx>, | ||
) -> Operand<'tcx> { | ||
let fn_ty = ty::FnDef(def_id, substs); | ||
let fn_ty = tcx.mk_ty(fn_ty); | ||
|
||
Operand::Constant(Box::new(Constant { | ||
span, | ||
literal: ConstantKind::Val(interpret::ConstValue::ZeroSized, fn_ty), | ||
user_ty: None, | ||
})) | ||
} |
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
90 changes: 90 additions & 0 deletions
90
tests/mir-opt/check_maybe_uninit.main.CheckMaybeUninit.diff
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,90 @@ | ||
- // MIR for `main` before CheckMaybeUninit | ||
+ // MIR for `main` after CheckMaybeUninit | ||
|
||
| User Type Annotations | ||
| 0: user_ty: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], value: TypeOf(DefId(2:2022 ~ core[4f75]::mem::maybe_uninit::{impl#2}::uninit), UserSubsts { substs: [^0], user_self_ty: Some(UserSelfTy { impl_def_id: DefId(2:2019 ~ core[4f75]::mem::maybe_uninit::{impl#2}), self_ty: std::mem::MaybeUninit<u8> }) }) }, span: $DIR/check-maybe-uninit.rs:6:17: 6:42, inferred_ty: fn() -> std::mem::MaybeUninit<u8> {std::mem::MaybeUninit::<u8>::uninit} | ||
| 1: user_ty: Canonical { max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], value: TypeOf(DefId(2:2022 ~ core[4f75]::mem::maybe_uninit::{impl#2}::uninit), UserSubsts { substs: [^0], user_self_ty: Some(UserSelfTy { impl_def_id: DefId(2:2019 ~ core[4f75]::mem::maybe_uninit::{impl#2}), self_ty: std::mem::MaybeUninit<std::string::String> }) }) }, span: $DIR/check-maybe-uninit.rs:7:17: 7:46, inferred_ty: fn() -> std::mem::MaybeUninit<std::string::String> {std::mem::MaybeUninit::<std::string::String>::uninit} | ||
| | ||
fn main() -> () { | ||
let mut _0: (); // return place in scope 0 at $DIR/check-maybe-uninit.rs:+0:11: +0:11 | ||
let mut _1: u8; // in scope 0 at $DIR/check-maybe-uninit.rs:+2:17: +2:58 | ||
let mut _2: std::mem::MaybeUninit<u8>; // in scope 0 at $DIR/check-maybe-uninit.rs:+2:17: +2:44 | ||
let mut _3: std::string::String; // in scope 0 at $DIR/check-maybe-uninit.rs:+3:17: +3:62 | ||
let mut _4: std::mem::MaybeUninit<std::string::String>; // in scope 0 at $DIR/check-maybe-uninit.rs:+3:17: +3:48 | ||
+ let mut _5: (); // in scope 0 at $DIR/check-maybe-uninit.rs:+2:45: +2:58 | ||
+ let mut _6: (); // in scope 0 at $DIR/check-maybe-uninit.rs:+3:49: +3:62 | ||
scope 1 { | ||
scope 2 { | ||
scope 3 { | ||
} | ||
} | ||
} | ||
|
||
bb0: { | ||
StorageLive(_1); // scope 1 at $DIR/check-maybe-uninit.rs:+2:17: +2:58 | ||
StorageLive(_2); // scope 1 at $DIR/check-maybe-uninit.rs:+2:17: +2:44 | ||
- _2 = MaybeUninit::<u8>::uninit() -> [return: bb1, unwind: bb6]; // scope 1 at $DIR/check-maybe-uninit.rs:+2:17: +2:44 | ||
+ _2 = MaybeUninit::<u8>::uninit() -> [return: bb7, unwind: bb6]; // scope 1 at $DIR/check-maybe-uninit.rs:+2:17: +2:44 | ||
// mir::Constant | ||
// + span: $DIR/check-maybe-uninit.rs:6:17: 6:42 | ||
// + user_ty: UserType(0) | ||
// + literal: Const { ty: fn() -> MaybeUninit<u8> {MaybeUninit::<u8>::uninit}, val: Value(<ZST>) } | ||
} | ||
|
||
bb1: { | ||
_1 = MaybeUninit::<u8>::assume_init(move _2) -> [return: bb2, unwind: bb6]; // scope 1 at $DIR/check-maybe-uninit.rs:+2:17: +2:58 | ||
// mir::Constant | ||
// + span: $DIR/check-maybe-uninit.rs:6:45: 6:56 | ||
// + literal: Const { ty: unsafe fn(MaybeUninit<u8>) -> u8 {MaybeUninit::<u8>::assume_init}, val: Value(<ZST>) } | ||
} | ||
|
||
bb2: { | ||
StorageDead(_2); // scope 1 at $DIR/check-maybe-uninit.rs:+2:57: +2:58 | ||
StorageDead(_1); // scope 1 at $DIR/check-maybe-uninit.rs:+2:58: +2:59 | ||
StorageLive(_3); // scope 2 at $DIR/check-maybe-uninit.rs:+3:17: +3:62 | ||
StorageLive(_4); // scope 2 at $DIR/check-maybe-uninit.rs:+3:17: +3:48 | ||
- _4 = MaybeUninit::<String>::uninit() -> [return: bb3, unwind: bb6]; // scope 2 at $DIR/check-maybe-uninit.rs:+3:17: +3:48 | ||
+ _4 = MaybeUninit::<String>::uninit() -> [return: bb8, unwind: bb6]; // scope 2 at $DIR/check-maybe-uninit.rs:+3:17: +3:48 | ||
// mir::Constant | ||
// + span: $DIR/check-maybe-uninit.rs:7:17: 7:46 | ||
// + user_ty: UserType(1) | ||
// + literal: Const { ty: fn() -> MaybeUninit<String> {MaybeUninit::<String>::uninit}, val: Value(<ZST>) } | ||
} | ||
|
||
bb3: { | ||
_3 = MaybeUninit::<String>::assume_init(move _4) -> [return: bb4, unwind: bb6]; // scope 2 at $DIR/check-maybe-uninit.rs:+3:17: +3:62 | ||
// mir::Constant | ||
// + span: $DIR/check-maybe-uninit.rs:7:49: 7:60 | ||
// + literal: Const { ty: unsafe fn(MaybeUninit<String>) -> String {MaybeUninit::<String>::assume_init}, val: Value(<ZST>) } | ||
} | ||
|
||
bb4: { | ||
StorageDead(_4); // scope 2 at $DIR/check-maybe-uninit.rs:+3:61: +3:62 | ||
drop(_3) -> [return: bb5, unwind: bb6]; // scope 2 at $DIR/check-maybe-uninit.rs:+3:62: +3:63 | ||
} | ||
|
||
bb5: { | ||
StorageDead(_3); // scope 2 at $DIR/check-maybe-uninit.rs:+3:62: +3:63 | ||
_0 = const (); // scope 1 at $DIR/check-maybe-uninit.rs:+1:5: +4:6 | ||
return; // scope 0 at $DIR/check-maybe-uninit.rs:+5:2: +5:2 | ||
} | ||
|
||
bb6 (cleanup): { | ||
resume; // scope 0 at $DIR/check-maybe-uninit.rs:+0:1: +5:2 | ||
+ } | ||
+ | ||
+ bb7: { | ||
+ _5 = <ZST>: fn() {assert_uninit_valid_wrapper::<u8>}() -> [return: bb1, unwind: bb6]; // scope 0 at $DIR/check-maybe-uninit.rs:+2:45: +2:58 | ||
+ // mir::Constant | ||
+ // + span: $DIR/check-maybe-uninit.rs:6:45: 6:58 | ||
+ // + literal: Const { ty: fn() {assert_uninit_valid_wrapper::<u8>}, val: Value(ValTree::Branch(..)) } | ||
+ } | ||
+ | ||
+ bb8: { | ||
+ _6 = <ZST>: fn() {assert_uninit_valid_wrapper::<String>}() -> [return: bb3, unwind: bb6]; // scope 0 at $DIR/check-maybe-uninit.rs:+3:49: +3:62 | ||
+ // mir::Constant | ||
+ // + span: $DIR/check-maybe-uninit.rs:7:49: 7:62 | ||
+ // + literal: Const { ty: fn() {assert_uninit_valid_wrapper::<String>}, val: Value(ValTree::Branch(..)) } | ||
} | ||
} | ||
|
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,9 @@ | ||
use std::mem::MaybeUninit; | ||
|
||
// EMIT_MIR check_maybe_uninit.main.CheckMaybeUninit.diff | ||
fn main() { | ||
unsafe { | ||
let _ = MaybeUninit::<u8>::uninit().assume_init(); | ||
let _ = MaybeUninit::<String>::uninit().assume_init(); | ||
} | ||
} |
Oops, something went wrong.