|
| 1 | +use rustc_index::IndexVec; |
| 2 | +use rustc_middle::mir::interpret::Scalar; |
| 3 | +use rustc_middle::mir::*; |
| 4 | +use rustc_middle::ty::{Ty, TyCtxt}; |
| 5 | +use rustc_session::Session; |
| 6 | + |
| 7 | +use crate::check_pointers::check_pointers; |
| 8 | + |
| 9 | +pub(super) struct CheckNull; |
| 10 | + |
| 11 | +impl<'tcx> crate::MirPass<'tcx> for CheckNull { |
| 12 | + fn is_enabled(&self, sess: &Session) -> bool { |
| 13 | + sess.ub_checks() |
| 14 | + } |
| 15 | + |
| 16 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 17 | + check_pointers(tcx, body, &[], insert_null_check); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +fn insert_null_check<'tcx>( |
| 22 | + tcx: TyCtxt<'tcx>, |
| 23 | + local_decls: &mut IndexVec<Local, LocalDecl<'tcx>>, |
| 24 | + block_data: &mut BasicBlockData<'tcx>, |
| 25 | + pointer: Place<'tcx>, |
| 26 | + _: Ty<'tcx>, |
| 27 | + source_info: SourceInfo, |
| 28 | + new_block: BasicBlock, |
| 29 | +) { |
| 30 | + let const_raw_ptr = Ty::new_imm_ptr(tcx, tcx.types.unit); |
| 31 | + let rvalue = Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(pointer), const_raw_ptr); |
| 32 | + let thin_ptr = local_decls.push(LocalDecl::with_source_info(const_raw_ptr, source_info)).into(); |
| 33 | + block_data |
| 34 | + .statements |
| 35 | + .push(Statement { source_info, kind: StatementKind::Assign(Box::new((thin_ptr, rvalue))) }); |
| 36 | + |
| 37 | + // Transmute the pointer to a usize (equivalent to `ptr.addr()`) |
| 38 | + let rvalue = Rvalue::Cast(CastKind::Transmute, Operand::Copy(thin_ptr), tcx.types.usize); |
| 39 | + let addr = local_decls.push(LocalDecl::with_source_info(tcx.types.usize, source_info)).into(); |
| 40 | + block_data |
| 41 | + .statements |
| 42 | + .push(Statement { source_info, kind: StatementKind::Assign(Box::new((addr, rvalue))) }); |
| 43 | + |
| 44 | + // Check if the pointer is null. |
| 45 | + let is_ok = local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into(); |
| 46 | + let zero = Operand::Constant(Box::new(ConstOperand { |
| 47 | + span: source_info.span, |
| 48 | + user_ty: None, |
| 49 | + const_: Const::Val(ConstValue::Scalar(Scalar::from_target_usize(0, &tcx)), tcx.types.usize), |
| 50 | + })); |
| 51 | + block_data.statements.push(Statement { |
| 52 | + source_info, |
| 53 | + kind: StatementKind::Assign(Box::new(( |
| 54 | + is_ok, |
| 55 | + Rvalue::BinaryOp(BinOp::Ne, Box::new((Operand::Copy(addr), zero))), |
| 56 | + ))), |
| 57 | + }); |
| 58 | + |
| 59 | + // Set this block's terminator to our assert, continuing to new_block if we pass |
| 60 | + block_data.terminator = Some(Terminator { |
| 61 | + source_info, |
| 62 | + kind: TerminatorKind::Assert { |
| 63 | + cond: Operand::Copy(is_ok), |
| 64 | + expected: true, |
| 65 | + target: new_block, |
| 66 | + msg: Box::new(AssertKind::NullPointerDereference), |
| 67 | + // This calls panic_misaligned_pointer_dereference, which is #[rustc_nounwind]. |
| 68 | + // We never want to insert an unwind into unsafe code, because unwinding could |
| 69 | + // make a failing UB check turn into much worse UB when we start unwinding. |
| 70 | + unwind: UnwindAction::Unreachable, |
| 71 | + }, |
| 72 | + }); |
| 73 | +} |
0 commit comments