Skip to content

Commit cf63f15

Browse files
authored
Rollup merge of #147451 - RalfJung:extra-const-ice, r=chenyukang
fix panic with extra-const-ub-checks Fixes #147306
2 parents 8356f6b + 8171174 commit cf63f15

File tree

6 files changed

+39
-14
lines changed

6 files changed

+39
-14
lines changed

compiler/rustc_const_eval/messages.ftl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,10 @@ const_eval_validation_null_box = {$front_matter}: encountered a {$maybe ->
481481
[true] maybe-null
482482
*[false] null
483483
} box
484-
const_eval_validation_null_fn_ptr = {$front_matter}: encountered a null function pointer
484+
const_eval_validation_null_fn_ptr = {$front_matter}: encountered a {$maybe ->
485+
[true] maybe-null
486+
*[false] null
487+
} function pointer
485488
const_eval_validation_null_ref = {$front_matter}: encountered a {$maybe ->
486489
[true] maybe-null
487490
*[false] null

compiler/rustc_const_eval/src/errors.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
666666
PartialPointer => const_eval_validation_partial_pointer,
667667
MutableRefToImmutable => const_eval_validation_mutable_ref_to_immutable,
668668
MutableRefInConst => const_eval_validation_mutable_ref_in_const,
669-
NullFnPtr => const_eval_validation_null_fn_ptr,
669+
NullFnPtr { .. } => const_eval_validation_null_fn_ptr,
670670
NeverVal => const_eval_validation_never_val,
671671
NonnullPtrMaybeNull { .. } => const_eval_validation_nonnull_ptr_out_of_range,
672672
PtrOutOfRange { .. } => const_eval_validation_ptr_out_of_range,
@@ -820,12 +820,11 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
820820
err.arg("vtable_dyn_type", vtable_dyn_type.to_string());
821821
err.arg("expected_dyn_type", expected_dyn_type.to_string());
822822
}
823-
NullPtr { maybe, .. } => {
823+
NullPtr { maybe, .. } | NullFnPtr { maybe } => {
824824
err.arg("maybe", maybe);
825825
}
826826
MutableRefToImmutable
827827
| MutableRefInConst
828-
| NullFnPtr
829828
| NonnullPtrMaybeNull
830829
| NeverVal
831830
| UnsafeCellInImmutable

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,14 +757,12 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
757757
);
758758
// FIXME: Check if the signature matches
759759
} else {
760-
// Otherwise (for standalone Miri), we have to still check it to be non-null.
760+
// Otherwise (for standalone Miri and for `-Zextra-const-ub-checks`),
761+
// we have to still check it to be non-null.
761762
if self.ecx.scalar_may_be_null(scalar)? {
762763
let maybe =
763764
!M::Provenance::OFFSET_IS_ADDR && matches!(scalar, Scalar::Ptr(..));
764-
// This can't be a "maybe-null" pointer since the check for this being
765-
// a fn ptr at all already ensures that the pointer is inbounds.
766-
assert!(!maybe);
767-
throw_validation_failure!(self.path, NullFnPtr);
765+
throw_validation_failure!(self.path, NullFnPtr { maybe });
768766
}
769767
}
770768
if self.reset_provenance_and_padding {

compiler/rustc_middle/src/mir/interpret/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,10 @@ pub enum ValidationErrorKind<'tcx> {
497497
MutableRefToImmutable,
498498
UnsafeCellInImmutable,
499499
MutableRefInConst,
500-
NullFnPtr,
500+
NullFnPtr {
501+
/// Records whether this pointer is definitely null or just may be null.
502+
maybe: bool,
503+
},
501504
NeverVal,
502505
NonnullPtrMaybeNull,
503506
PtrOutOfRange {

tests/ui/consts/extra-const-ub/detect-extra-ub.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ const UNALIGNED_PTR: () = unsafe {
5252
//[with_flag]~^ ERROR: invalid value
5353
};
5454

55+
// A function pointer offset to be maybe-null.
56+
const MAYBE_NULL_FN_PTR: () = unsafe {
57+
let _x: fn() = transmute({
58+
//[with_flag]~^ ERROR: invalid value
59+
fn fun() {}
60+
let ptr = fun as fn();
61+
(ptr as *const u8).wrapping_add(10)
62+
});
63+
};
64+
5565
const UNINHABITED_VARIANT: () = unsafe {
5666
let data = [1u8];
5767
// Not using transmute, we want to hit the ImmTy code path.

tests/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,26 @@ error[E0080]: constructing invalid value: encountered an unaligned reference (re
3737
LL | let _x: &u32 = transmute(&[0u8; 4]);
3838
| ^^^^^^^^^^^^^^^^^^^^ evaluation of `UNALIGNED_PTR` failed here
3939

40+
error[E0080]: constructing invalid value: encountered a maybe-null function pointer
41+
--> $DIR/detect-extra-ub.rs:57:20
42+
|
43+
LL | let _x: fn() = transmute({
44+
| ____________________^
45+
LL | |
46+
LL | | fn fun() {}
47+
LL | | let ptr = fun as fn();
48+
LL | | (ptr as *const u8).wrapping_add(10)
49+
LL | | });
50+
| |______^ evaluation of `MAYBE_NULL_FN_PTR` failed here
51+
4052
error[E0080]: constructing invalid value at .<enum-tag>: encountered an uninhabited enum variant
41-
--> $DIR/detect-extra-ub.rs:58:13
53+
--> $DIR/detect-extra-ub.rs:68:13
4254
|
4355
LL | let v = *addr_of!(data).cast::<UninhDiscriminant>();
4456
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `UNINHABITED_VARIANT` failed here
4557

4658
error[E0080]: constructing invalid value at [0]: encountered a partial pointer or a mix of pointers
47-
--> $DIR/detect-extra-ub.rs:77:16
59+
--> $DIR/detect-extra-ub.rs:87:16
4860
|
4961
LL | let _val = *(&mem as *const Align as *const [*const u8; 2]);
5062
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `PARTIAL_POINTER` failed here
@@ -53,11 +65,11 @@ LL | let _val = *(&mem as *const Align as *const [*const u8; 2]);
5365
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
5466

5567
error[E0080]: constructing invalid value: encountered invalid reference metadata: slice is bigger than largest supported object
56-
--> $DIR/detect-extra-ub.rs:91:16
68+
--> $DIR/detect-extra-ub.rs:101:16
5769
|
5870
LL | let _val = &*slice;
5971
| ^^^^^^^ evaluation of `OVERSIZED_REF` failed here
6072

61-
error: aborting due to 8 previous errors
73+
error: aborting due to 9 previous errors
6274

6375
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)