Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const-eval: error when encountering references to functions / vtables #121199

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
const-eval: error when encountering references to functions / vtables
  • Loading branch information
RalfJung committed Feb 16, 2024
commit e2c01caf9ccfdd18eb972da9a997caf2159514c7
2 changes: 2 additions & 0 deletions compiler/rustc_const_eval/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,10 @@ const_eval_validation_out_of_range = {$front_matter}: encountered {$value}, but
const_eval_validation_partial_pointer = {$front_matter}: encountered a partial pointer or a mix of pointers
const_eval_validation_pointer_as_int = {$front_matter}: encountered a pointer, but {$expected}
const_eval_validation_ptr_out_of_range = {$front_matter}: encountered a pointer, but expected something that cannot possibly fail to be {$in_range}
const_eval_validation_ref_to_function = {$front_matter}: encountered a reference pointing to a function
const_eval_validation_ref_to_static = {$front_matter}: encountered a reference pointing to a static variable in a constant
const_eval_validation_ref_to_uninhabited = {$front_matter}: encountered a reference pointing to uninhabited type {$ty}
const_eval_validation_ref_to_vtable = {$front_matter}: encountered a reference pointing to a vtable
const_eval_validation_unaligned_box = {$front_matter}: encountered an unaligned box (required {$required_bytes} byte alignment but found {$found_bytes})
const_eval_validation_unaligned_ref = {$front_matter}: encountered an unaligned reference (required {$required_bytes} byte alignment but found {$found_bytes})
const_eval_validation_uninhabited_enum_variant = {$front_matter}: encountered an uninhabited enum variant
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_const_eval/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
PartialPointer => const_eval_validation_partial_pointer,
ConstRefToMutable => const_eval_validation_const_ref_to_mutable,
ConstRefToExtern => const_eval_validation_const_ref_to_extern,
RefToFunction => const_eval_validation_ref_to_function,
RefToVtable => const_eval_validation_ref_to_vtable,
MutableRefInConstOrStatic => const_eval_validation_mutable_ref_in_const_or_static,
MutableRefToImmutable => const_eval_validation_mutable_ref_to_immutable,
NullFnPtr => const_eval_validation_null_fn_ptr,
Expand Down Expand Up @@ -771,6 +773,8 @@ impl<'tcx> ReportErrorExt for ValidationErrorInfo<'tcx> {
| MutableRefInConstOrStatic
| ConstRefToMutable
| ConstRefToExtern
| RefToFunction
| RefToVtable
| MutableRefToImmutable
| NullFnPtr
| NeverVal
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,11 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
if is_mut { Mutability::Mut } else { Mutability::Not }
}
GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
GlobalAlloc::Function(..) | GlobalAlloc::VTable(..) => {
// These are immutable, we better don't allow mutable pointers here.
Mutability::Not
GlobalAlloc::Function(..) => {
throw_validation_failure!(self.path, RefToFunction);
}
GlobalAlloc::VTable(..) => {
throw_validation_failure!(self.path, RefToVtable);
}
};
// Mutability check.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/mir/interpret/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ pub enum ValidationErrorKind<'tcx> {
ConstRefToExtern,
MutableRefToImmutable,
UnsafeCellInImmutable,
RefToFunction,
RefToVtable,
NullFnPtr,
NeverVal,
NullablePtrOutOfRange { range: WrappingRange, max_value: u128 },
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/consts/const-eval/ub-ref-ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,16 @@ const UNALIGNED_READ: () = unsafe {
ptr.read(); //~ inside `UNALIGNED_READ`
};

const POINTS_TO_FUNCTION: &() = unsafe {
//~^ ERROR it is undefined behavior to use this value
//~| function
mem::transmute(main as fn())
};
const POINTS_TO_VTABLE: (&(), &()) = unsafe {
//~^ ERROR it is undefined behavior to use this value
//~| vtable
mem::transmute(&() as &dyn Send)
};


fn main() {}
24 changes: 23 additions & 1 deletion tests/ui/consts/const-eval/ub-ref-ptr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,28 @@ note: inside `UNALIGNED_READ`
LL | ptr.read();
| ^^^^^^^^^^

error: aborting due to 15 previous errors
error[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:69:1
|
LL | const POINTS_TO_FUNCTION: &() = unsafe {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered a reference pointing to a function
|
= 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[E0080]: it is undefined behavior to use this value
--> $DIR/ub-ref-ptr.rs:74:1
|
LL | const POINTS_TO_VTABLE: (&(), &()) = unsafe {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .1: encountered a reference pointing to a vtable
|
= 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: aborting due to 17 previous errors

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