From 77f8c3caea901df51ef723251a7a58b27f96bb3a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 6 Jan 2024 15:06:22 +0100 Subject: [PATCH] detect consts that reference extern statics --- compiler/rustc_const_eval/messages.ftl | 1 + .../src/const_eval/eval_queries.rs | 7 ++- compiler/rustc_const_eval/src/errors.rs | 2 + .../src/interpret/validity.rs | 14 +++++- .../rustc_middle/src/mir/interpret/error.rs | 1 + .../const_refs_to_static_fail_invalid.rs | 43 ++++++++++++++++--- .../const_refs_to_static_fail_invalid.stderr | 42 ++++++++++++++++-- .../const_refs_to_static_fail_pattern.rs | 18 -------- .../const_refs_to_static_fail_pattern.stderr | 20 --------- 9 files changed, 97 insertions(+), 51 deletions(-) delete mode 100644 tests/ui/consts/const_refs_to_static_fail_pattern.rs delete mode 100644 tests/ui/consts/const_refs_to_static_fail_pattern.stderr diff --git a/compiler/rustc_const_eval/messages.ftl b/compiler/rustc_const_eval/messages.ftl index f8bb122c52c..546001a25b2 100644 --- a/compiler/rustc_const_eval/messages.ftl +++ b/compiler/rustc_const_eval/messages.ftl @@ -412,6 +412,7 @@ const_eval_upcast_mismatch = const_eval_validation_box_to_static = {$front_matter}: encountered a box pointing to a static variable in a constant const_eval_validation_box_to_uninhabited = {$front_matter}: encountered a box pointing to uninhabited type {$ty} +const_eval_validation_const_ref_to_extern = {$front_matter}: encountered reference to `extern` static in `const` const_eval_validation_const_ref_to_mutable = {$front_matter}: encountered reference to mutable memory in `const` const_eval_validation_dangling_box_no_provenance = {$front_matter}: encountered a dangling box ({$pointer} has no provenance) diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index 52060a8693f..0844cdbe99b 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -360,7 +360,7 @@ pub fn const_validate_mplace<'mir, 'tcx>( // Promoteds in statics are consts that re allowed to point to statics. CtfeValidationMode::Const { allow_immutable_unsafe_cell: false, - allow_static_ptrs: true, + allow_extern_static_ptrs: true, } } Some(mutbl) => CtfeValidationMode::Static { mutbl }, // a `static` @@ -368,7 +368,10 @@ pub fn const_validate_mplace<'mir, 'tcx>( // In normal `const` (not promoted), the outermost allocation is always only copied, // so having `UnsafeCell` in there is okay despite them being in immutable memory. let allow_immutable_unsafe_cell = cid.promoted.is_none() && !inner; - CtfeValidationMode::Const { allow_immutable_unsafe_cell, allow_static_ptrs: false } + CtfeValidationMode::Const { + allow_immutable_unsafe_cell, + allow_extern_static_ptrs: false, + } } }; ecx.const_validate_operand(&mplace.into(), path, &mut ref_tracking, mode)?; diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index fe72941bbab..fb89b49fade 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -612,6 +612,7 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { PointerAsInt { .. } => const_eval_validation_pointer_as_int, PartialPointer => const_eval_validation_partial_pointer, ConstRefToMutable => const_eval_validation_const_ref_to_mutable, + ConstRefToExtern => const_eval_validation_const_ref_to_extern, MutableRefInConst => const_eval_validation_mutable_ref_in_const, MutableRefToImmutable => const_eval_validation_mutable_ref_to_immutable, NullFnPtr => const_eval_validation_null_fn_ptr, @@ -767,6 +768,7 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> { | PtrToStatic { .. } | MutableRefInConst | ConstRefToMutable + | ConstRefToExtern | MutableRefToImmutable | NullFnPtr | NeverVal diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 90623a1aa9d..38aeace02ba 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -133,7 +133,7 @@ pub enum CtfeValidationMode { /// `allow_immutable_unsafe_cell` says whether we allow `UnsafeCell` in immutable memory (which is the /// case for the top-level allocation of a `const`, where this is fine because the allocation will be /// copied at each use site). - Const { allow_immutable_unsafe_cell: bool, allow_static_ptrs: bool }, + Const { allow_immutable_unsafe_cell: bool, allow_extern_static_ptrs: bool }, } impl CtfeValidationMode { @@ -488,13 +488,23 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // This could miss some UB, but that's fine. return Ok(()); } - Some(CtfeValidationMode::Const { .. }) => { + Some(CtfeValidationMode::Const { + allow_extern_static_ptrs, .. + }) => { // For consts on the other hand we have to recursively check; // pattern matching assumes a valid value. However we better make // sure this is not mutable. if is_mut { throw_validation_failure!(self.path, ConstRefToMutable); } + if self.ecx.tcx.is_foreign_item(did) { + if !allow_extern_static_ptrs { + throw_validation_failure!(self.path, ConstRefToExtern); + } else { + // We can't validate this... + return Ok(()); + } + } } None => {} } diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 9a4ce48ebce..66f448a451e 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -418,6 +418,7 @@ pub enum ValidationErrorKind<'tcx> { PtrToStatic { ptr_kind: PointerKind }, MutableRefInConst, ConstRefToMutable, + ConstRefToExtern, MutableRefToImmutable, UnsafeCellInImmutable, NullFnPtr, diff --git a/tests/ui/consts/const_refs_to_static_fail_invalid.rs b/tests/ui/consts/const_refs_to_static_fail_invalid.rs index de4ec6b1e2a..ee20db6c6c0 100644 --- a/tests/ui/consts/const_refs_to_static_fail_invalid.rs +++ b/tests/ui/consts/const_refs_to_static_fail_invalid.rs @@ -2,16 +2,49 @@ // normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?()?─*╼ )+ *│.*" -> "HEX_DUMP" #![feature(const_refs_to_static)] -static S: i8 = 10; +fn invalid() { + static S: i8 = 10; -const C: &bool = unsafe { std::mem::transmute(&S) }; -//~^ERROR: undefined behavior -//~| expected a boolean + const C: &bool = unsafe { std::mem::transmute(&S) }; + //~^ERROR: undefined behavior + //~| expected a boolean -fn main() { // This must be rejected here (or earlier), since it's not a valid `&bool`. match &true { + C => {} //~ERROR: could not evaluate constant pattern + _ => {} + } +} + +fn extern_() { + extern "C" { + static S: i8; + } + + const C: &i8 = unsafe { &S }; + //~^ERROR: undefined behavior + //~| `extern` static + + // This must be rejected here (or earlier), since the pattern cannot be read. + match &0 { + C => {} //~ERROR: could not evaluate constant pattern + _ => {} + } +} + +fn mutable() { + static mut S_MUT: i32 = 0; + + const C: &i32 = unsafe { &S_MUT }; + //~^ERROR: undefined behavior + //~| encountered reference to mutable memory + + // This *must not build*, the constant we are matching against + // could change its value! + match &42 { C => {}, //~ERROR: could not evaluate constant pattern _ => {}, } } + +fn main() {} diff --git a/tests/ui/consts/const_refs_to_static_fail_invalid.stderr b/tests/ui/consts/const_refs_to_static_fail_invalid.stderr index cf8238063bc..56006f7ae25 100644 --- a/tests/ui/consts/const_refs_to_static_fail_invalid.stderr +++ b/tests/ui/consts/const_refs_to_static_fail_invalid.stderr @@ -1,8 +1,8 @@ error[E0080]: it is undefined behavior to use this value - --> $DIR/const_refs_to_static_fail_invalid.rs:7:1 + --> $DIR/const_refs_to_static_fail_invalid.rs:8:5 | -LL | const C: &bool = unsafe { std::mem::transmute(&S) }; - | ^^^^^^^^^^^^^^ constructing invalid value at .: encountered 0x0a, but expected a boolean +LL | const C: &bool = unsafe { std::mem::transmute(&S) }; + | ^^^^^^^^^^^^^^ constructing invalid value at .: encountered 0x0a, but expected a boolean | = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { @@ -12,9 +12,43 @@ LL | const C: &bool = unsafe { std::mem::transmute(&S) }; error: could not evaluate constant pattern --> $DIR/const_refs_to_static_fail_invalid.rs:14:9 | +LL | C => {} + | ^ + +error[E0080]: it is undefined behavior to use this value + --> $DIR/const_refs_to_static_fail_invalid.rs:24:5 + | +LL | const C: &i8 = unsafe { &S }; + | ^^^^^^^^^^^^ constructing invalid value: encountered reference to `extern` static in `const` + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error: could not evaluate constant pattern + --> $DIR/const_refs_to_static_fail_invalid.rs:30:9 + | +LL | C => {} + | ^ + +error[E0080]: it is undefined behavior to use this value + --> $DIR/const_refs_to_static_fail_invalid.rs:38:5 + | +LL | const C: &i32 = unsafe { &S_MUT }; + | ^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const` + | + = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. + = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { + HEX_DUMP + } + +error: could not evaluate constant pattern + --> $DIR/const_refs_to_static_fail_invalid.rs:45:9 + | LL | C => {}, | ^ -error: aborting due to 2 previous errors +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/consts/const_refs_to_static_fail_pattern.rs b/tests/ui/consts/const_refs_to_static_fail_pattern.rs deleted file mode 100644 index 27a77378d0e..00000000000 --- a/tests/ui/consts/const_refs_to_static_fail_pattern.rs +++ /dev/null @@ -1,18 +0,0 @@ -// normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)" -// normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?()?─*╼ )+ *│.*" -> "HEX_DUMP" -#![feature(const_refs_to_static)] - -static mut S_MUT: i32 = 0; - -const C: &i32 = unsafe { &S_MUT }; -//~^ERROR: undefined behavior -//~| encountered reference to mutable memory - -fn main() { - // This *must not build*, the constant we are matching against - // could change its value! - match &42 { - C => {}, //~ERROR: could not evaluate constant pattern - _ => {}, - } -} diff --git a/tests/ui/consts/const_refs_to_static_fail_pattern.stderr b/tests/ui/consts/const_refs_to_static_fail_pattern.stderr deleted file mode 100644 index a229654a89b..00000000000 --- a/tests/ui/consts/const_refs_to_static_fail_pattern.stderr +++ /dev/null @@ -1,20 +0,0 @@ -error[E0080]: it is undefined behavior to use this value - --> $DIR/const_refs_to_static_fail_pattern.rs:7:1 - | -LL | const C: &i32 = unsafe { &S_MUT }; - | ^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const` - | - = note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior. - = note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) { - HEX_DUMP - } - -error: could not evaluate constant pattern - --> $DIR/const_refs_to_static_fail_pattern.rs:15:9 - | -LL | C => {}, - | ^ - -error: aborting due to 2 previous errors - -For more information about this error, try `rustc --explain E0080`.