Skip to content

Commit b268228

Browse files
committed
Distinguish between library and lang UB in assert_unsafe_precondition
1 parent fc3800f commit b268228

File tree

46 files changed

+364
-238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+364
-238
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20012001
ConstraintCategory::SizedBound,
20022002
);
20032003
}
2004-
&Rvalue::NullaryOp(NullOp::DebugAssertions, _) => {}
2004+
&Rvalue::NullaryOp(NullOp::UbCheck(_), _) => {}
20052005

20062006
Rvalue::ShallowInitBox(operand, ty) => {
20072007
self.check_operand(operand, location);

compiler/rustc_codegen_cranelift/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ fn codegen_stmt<'tcx>(
767767
NullOp::OffsetOf(fields) => {
768768
layout.offset_of_subfield(fx, fields.iter()).bytes()
769769
}
770-
NullOp::DebugAssertions => {
770+
NullOp::UbCheck(_) => {
771771
let val = fx.tcx.sess.opts.debug_assertions;
772772
let val = CValue::by_val(
773773
fx.bcx.ins().iconst(types::I8, i64::try_from(val).unwrap()),

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
684684
let val = layout.offset_of_subfield(bx.cx(), fields.iter()).bytes();
685685
bx.cx().const_usize(val)
686686
}
687-
mir::NullOp::DebugAssertions => {
687+
mir::NullOp::UbCheck(_) => {
688+
// In codegen, we want to check for language UB and library UB
688689
let val = bx.tcx().sess.opts.debug_assertions;
689690
bx.cx().const_bool(val)
690691
}

compiler/rustc_const_eval/src/interpret/step.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
258258
let val = layout.offset_of_subfield(self, fields.iter()).bytes();
259259
Scalar::from_target_usize(val, self)
260260
}
261-
mir::NullOp::DebugAssertions => {
262-
// The checks hidden behind this are always better done by the interpreter
263-
// itself, because it knows the runtime state better.
264-
Scalar::from_bool(false)
261+
mir::NullOp::UbCheck(kind) => {
262+
// We want to enable checks for library UB, because the interpreter doesn't
263+
// know about those on its own.
264+
// But we want to disable checks for language UB, because the interpreter
265+
// has its own better checks for that.
266+
let should_check = match kind {
267+
mir::UbKind::LibraryUb => true,
268+
mir::UbKind::LanguageUb => false,
269+
};
270+
Scalar::from_bool(should_check)
265271
}
266272
};
267273
self.write_scalar(val, &dest)?;

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
558558
Rvalue::Cast(_, _, _) => {}
559559

560560
Rvalue::NullaryOp(
561-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::DebugAssertions,
561+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) | NullOp::UbCheck(_),
562562
_,
563563
) => {}
564564
Rvalue::ShallowInitBox(_, _) => {}

compiler/rustc_const_eval/src/transform/validate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11571157
Rvalue::Repeat(_, _)
11581158
| Rvalue::ThreadLocalRef(_)
11591159
| Rvalue::AddressOf(_, _)
1160-
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::DebugAssertions, _)
1160+
| Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::UbCheck(_), _)
11611161
| Rvalue::Discriminant(_) => {}
11621162
}
11631163
self.super_rvalue(rvalue, location);

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ pub fn intrinsic_operation_unsafety(tcx: TyCtxt<'_>, intrinsic_id: LocalDefId) -
123123
| sym::variant_count
124124
| sym::is_val_statically_known
125125
| sym::ptr_mask
126-
| sym::debug_assertions
126+
| sym::check_language_ub
127+
| sym::check_library_ub
127128
| sym::fadd_algebraic
128129
| sym::fsub_algebraic
129130
| sym::fmul_algebraic
@@ -508,7 +509,7 @@ pub fn check_intrinsic_type(
508509
(0, 0, vec![Ty::new_imm_ptr(tcx, Ty::new_unit(tcx))], tcx.types.usize)
509510
}
510511

511-
sym::debug_assertions => (0, 1, Vec::new(), tcx.types.bool),
512+
sym::check_language_ub | sym::check_library_ub => (0, 1, Vec::new(), tcx.types.bool),
512513

513514
other => {
514515
tcx.dcx().emit_err(UnrecognizedIntrinsicFunction { span, name: other });

compiler/rustc_middle/src/mir/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
909909
NullOp::SizeOf => write!(fmt, "SizeOf({t})"),
910910
NullOp::AlignOf => write!(fmt, "AlignOf({t})"),
911911
NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t}, {fields:?})"),
912-
NullOp::DebugAssertions => write!(fmt, "cfg!(debug_assertions)"),
912+
NullOp::UbCheck(kind) => write!(fmt, "UbCheck({kind:?})"),
913913
}
914914
}
915915
ThreadLocalRef(did) => ty::tls::with(|tcx| {

compiler/rustc_middle/src/mir/syntax.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1361,8 +1361,13 @@ pub enum NullOp<'tcx> {
13611361
AlignOf,
13621362
/// Returns the offset of a field
13631363
OffsetOf(&'tcx List<(VariantIdx, FieldIdx)>),
1364-
/// cfg!(debug_assertions), but expanded in codegen
1365-
DebugAssertions,
1364+
UbCheck(UbKind),
1365+
}
1366+
1367+
#[derive(Clone, Copy, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)]
1368+
pub enum UbKind {
1369+
LanguageUb,
1370+
LibraryUb,
13661371
}
13671372

13681373
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]

compiler/rustc_middle/src/mir/tcx.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'tcx> Rvalue<'tcx> {
194194
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
195195
tcx.types.usize
196196
}
197-
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => tcx.types.bool,
197+
Rvalue::NullaryOp(NullOp::UbCheck(_), _) => tcx.types.bool,
198198
Rvalue::Aggregate(ref ak, ref ops) => match **ak {
199199
AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
200200
AggregateKind::Tuple => {

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl<'b, 'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> Gatherer<'b, 'a, 'tcx, F> {
433433
| Rvalue::Discriminant(..)
434434
| Rvalue::Len(..)
435435
| Rvalue::NullaryOp(
436-
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::DebugAssertions,
436+
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::UbCheck(_),
437437
_,
438438
) => {}
439439
}

compiler/rustc_mir_transform/src/gvn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
488488
NullOp::OffsetOf(fields) => {
489489
layout.offset_of_subfield(&self.ecx, fields.iter()).bytes()
490490
}
491-
NullOp::DebugAssertions => return None,
491+
NullOp::UbCheck(_) => return None,
492492
};
493493
let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap();
494494
let imm = ImmTy::try_from_uint(val, usize_layout)?;

compiler/rustc_mir_transform/src/known_panics_lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
639639
NullOp::OffsetOf(fields) => {
640640
op_layout.offset_of_subfield(self, fields.iter()).bytes()
641641
}
642-
NullOp::DebugAssertions => return None,
642+
NullOp::UbCheck(_) => return None,
643643
};
644644
ImmTy::from_scalar(Scalar::from_target_usize(val, self), layout).into()
645645
}

compiler/rustc_mir_transform/src/lower_intrinsics.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,30 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
2020
sym::unreachable => {
2121
terminator.kind = TerminatorKind::Unreachable;
2222
}
23-
sym::debug_assertions => {
23+
sym::check_language_ub => {
2424
let target = target.unwrap();
2525
block.statements.push(Statement {
2626
source_info: terminator.source_info,
2727
kind: StatementKind::Assign(Box::new((
2828
*destination,
29-
Rvalue::NullaryOp(NullOp::DebugAssertions, tcx.types.bool),
29+
Rvalue::NullaryOp(
30+
NullOp::UbCheck(UbKind::LanguageUb),
31+
tcx.types.bool,
32+
),
33+
))),
34+
});
35+
terminator.kind = TerminatorKind::Goto { target };
36+
}
37+
sym::check_library_ub => {
38+
let target = target.unwrap();
39+
block.statements.push(Statement {
40+
source_info: terminator.source_info,
41+
kind: StatementKind::Assign(Box::new((
42+
*destination,
43+
Rvalue::NullaryOp(
44+
NullOp::UbCheck(UbKind::LibraryUb),
45+
tcx.types.bool,
46+
),
3047
))),
3148
});
3249
terminator.kind = TerminatorKind::Goto { target };

compiler/rustc_mir_transform/src/promote_consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl<'tcx> Validator<'_, 'tcx> {
446446
NullOp::SizeOf => {}
447447
NullOp::AlignOf => {}
448448
NullOp::OffsetOf(_) => {}
449-
NullOp::DebugAssertions => {}
449+
NullOp::UbCheck(_) => {}
450450
},
451451

452452
Rvalue::ShallowInitBox(_, _) => return Err(Unpromotable),

compiler/rustc_smir/src/rustc_smir/convert/mir.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,19 @@ impl<'tcx> Stable<'tcx> for mir::NullOp<'tcx> {
251251
type T = stable_mir::mir::NullOp;
252252
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
253253
use rustc_middle::mir::NullOp::*;
254+
use rustc_middle::mir::UbKind;
254255
match self {
255256
SizeOf => stable_mir::mir::NullOp::SizeOf,
256257
AlignOf => stable_mir::mir::NullOp::AlignOf,
257258
OffsetOf(indices) => stable_mir::mir::NullOp::OffsetOf(
258259
indices.iter().map(|idx| idx.stable(tables)).collect(),
259260
),
260-
DebugAssertions => stable_mir::mir::NullOp::DebugAssertions,
261+
UbCheck(UbKind::LanguageUb) => {
262+
stable_mir::mir::NullOp::UbCheck(stable_mir::mir::UbKind::LanguageUb)
263+
}
264+
UbCheck(UbKind::LibraryUb) => {
265+
stable_mir::mir::NullOp::UbCheck(stable_mir::mir::UbKind::LibraryUb)
266+
}
261267
}
262268
}
263269
}

compiler/rustc_span/src/symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,8 @@ symbols! {
514514
cfi,
515515
cfi_encoding,
516516
char,
517+
check_language_ub,
518+
check_library_ub,
517519
client,
518520
clippy,
519521
clobber_abi,

compiler/stable_mir/src/mir/body.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl Rvalue {
639639
Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
640640
Ok(Ty::usize_ty())
641641
}
642-
Rvalue::NullaryOp(NullOp::DebugAssertions, _) => Ok(Ty::bool_ty()),
642+
Rvalue::NullaryOp(NullOp::UbCheck(_), _) => Ok(Ty::bool_ty()),
643643
Rvalue::Aggregate(ak, ops) => match *ak {
644644
AggregateKind::Array(ty) => Ty::try_new_array(ty, ops.len() as u64),
645645
AggregateKind::Tuple => Ok(Ty::new_tuple(
@@ -1007,7 +1007,13 @@ pub enum NullOp {
10071007
/// Returns the offset of a field.
10081008
OffsetOf(Vec<(VariantIdx, FieldIdx)>),
10091009
/// cfg!(debug_assertions), but at codegen time
1010-
DebugAssertions,
1010+
UbCheck(UbKind),
1011+
}
1012+
1013+
#[derive(Clone, Debug, Eq, PartialEq)]
1014+
pub enum UbKind {
1015+
LanguageUb,
1016+
LibraryUb,
10111017
}
10121018

10131019
impl Operand {

library/core/src/char/convert.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
2626
// SAFETY: the caller must guarantee that `i` is a valid char value.
2727
unsafe {
2828
assert_unsafe_precondition!(
29+
check_language_ub,
2930
"invalid value for `char`",
3031
(i: u32 = i) => char_try_from_u32(i).is_ok()
3132
);

library/core/src/hint.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ pub const unsafe fn unreachable_unchecked() -> ! {
101101
// SAFETY: the safety contract for `intrinsics::unreachable` must
102102
// be upheld by the caller.
103103
unsafe {
104-
intrinsics::assert_unsafe_precondition!("hint::unreachable_unchecked must never be reached", () => false);
104+
intrinsics::assert_unsafe_precondition!(
105+
check_language_ub,
106+
"hint::unreachable_unchecked must never be reached",
107+
() => false
108+
);
105109
intrinsics::unreachable()
106110
}
107111
}
@@ -147,6 +151,7 @@ pub const unsafe fn assert_unchecked(cond: bool) {
147151
// SAFETY: The caller promised `cond` is true.
148152
unsafe {
149153
intrinsics::assert_unsafe_precondition!(
154+
check_language_ub,
150155
"hint::assert_unchecked must never be called when the condition is false",
151156
(cond: bool = cond) => cond,
152157
);

0 commit comments

Comments
 (0)