Skip to content

Commit e6b0b5d

Browse files
Auto merge of #144030 - oli-obk:validate-transmute, r=<try>
Validate transmute in CTFE fixes #142230 let's see what perf says, maybe we need to restrict it to literal transmutes, and not all the implicit ones happening in mir interpreter internal situations r? `@ghost`
2 parents 1c6de21 + d032919 commit e6b0b5d

Some content is hidden

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

47 files changed

+591
-1258
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn eval_body_using_ecx<'tcx, R: InterpretationResult<'tcx>>(
9393
// Since evaluation had no errors, validate the resulting constant.
9494
const_validate_mplace(ecx, &ret, cid)?;
9595

96-
// Only report this after validation, as validaiton produces much better diagnostics.
96+
// Only report this after validation, as validation produces much better diagnostics.
9797
// FIXME: ensure validation always reports this and stop making interning care about it.
9898

9999
match intern_result {

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ where
813813
// Do the actual copy.
814814
self.copy_op_no_validate(src, dest, allow_transmute)?;
815815

816-
if M::enforce_validity(self, dest.layout()) {
816+
if M::enforce_validity(self, dest.layout()) || allow_transmute {
817817
let dest = dest.to_place();
818818
// Given that there were two typed copies, we have to ensure this is valid at both types,
819819
// and we have to ensure this loses provenance and padding according to both types.

tests/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,13 @@ error[E0080]: constructing invalid value: encountered 0x42, but expected a boole
88
--> $DIR/invalid-patterns.rs:42:14
99
|
1010
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
12-
|
13-
= 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.
14-
= note: the raw bytes of the constant (size: 1, align: 1) {
15-
42 │ B
16-
}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `main::{constant#8}` failed here
1712

1813
error[E0080]: constructing invalid value: encountered 0x42, but expected a boolean
1914
--> $DIR/invalid-patterns.rs:44:14
2015
|
2116
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
23-
|
24-
= 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.
25-
= note: the raw bytes of the constant (size: 1, align: 1) {
26-
42 │ B
27-
}
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `main::{constant#10}` failed here
2818

2919
error[E0080]: using uninitialized data, but this operation requires initialized memory
3020
--> $DIR/invalid-patterns.rs:44:58

tests/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,13 @@ error[E0080]: constructing invalid value: encountered 0x42, but expected a boole
88
--> $DIR/invalid-patterns.rs:42:14
99
|
1010
LL | get_flag::<{ unsafe { bool_raw.boolean } }, 'z'>();
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
12-
|
13-
= 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.
14-
= note: the raw bytes of the constant (size: 1, align: 1) {
15-
42 │ B
16-
}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `main::{constant#8}` failed here
1712

1813
error[E0080]: constructing invalid value: encountered 0x42, but expected a boolean
1914
--> $DIR/invalid-patterns.rs:44:14
2015
|
2116
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
23-
|
24-
= 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.
25-
= note: the raw bytes of the constant (size: 1, align: 1) {
26-
42 │ B
27-
}
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `main::{constant#10}` failed here
2818

2919
error[E0080]: using uninitialized data, but this operation requires initialized memory
3020
--> $DIR/invalid-patterns.rs:44:58

tests/ui/const-ptr/forbidden_slices.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ pub static S7: &[u16] = unsafe {
3939

4040
// Unaligned read
4141
pub static S8: &[u64] = unsafe {
42-
//~^ ERROR: dangling reference (going beyond the bounds of its allocation)
4342
let ptr = (&D4 as *const [u32; 2] as *const u32).byte_add(1).cast::<u64>();
4443

4544
from_raw_parts(ptr, 1)
45+
//~^ ERROR: dangling reference (going beyond the bounds of its allocation)
4646
};
4747

4848
pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
@@ -70,9 +70,9 @@ pub static R6: &[bool] = unsafe {
7070
from_ptr_range(ptr..ptr.add(4))
7171
};
7272
pub static R7: &[u16] = unsafe {
73-
//~^ ERROR: unaligned reference (required 2 byte alignment but found 1)
7473
let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
7574
from_ptr_range(ptr..ptr.add(4))
75+
//~^ ERROR: unaligned reference (required 2 byte alignment but found 1)
7676
};
7777
pub static R8: &[u64] = unsafe {
7878
let ptr = (&D4 as *const [u32; 2] as *const u32).byte_add(1).cast::<u64>();

tests/ui/const-ptr/forbidden_slices.stderr

Lines changed: 16 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
error[E0080]: constructing invalid value: encountered a null reference
2-
--> $DIR/forbidden_slices.rs:16:1
2+
--> $DIR/forbidden_slices.rs:16:34
33
|
44
LL | pub static S0: &[u32] = unsafe { from_raw_parts(ptr::null(), 0) };
5-
| ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
6-
|
7-
= 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.
8-
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
9-
HEX_DUMP
10-
}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `S0` failed here
116

127
error[E0080]: constructing invalid value: encountered a null reference
13-
--> $DIR/forbidden_slices.rs:18:1
8+
--> $DIR/forbidden_slices.rs:18:33
149
|
1510
LL | pub static S1: &[()] = unsafe { from_raw_parts(ptr::null(), 0) };
16-
| ^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
17-
|
18-
= 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.
19-
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
20-
HEX_DUMP
21-
}
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `S1` failed here
2212

2313
error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation)
24-
--> $DIR/forbidden_slices.rs:22:1
14+
--> $DIR/forbidden_slices.rs:22:34
2515
|
2616
LL | pub static S2: &[u32] = unsafe { from_raw_parts(&D0, 2) };
27-
| ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
28-
|
29-
= 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.
30-
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
31-
HEX_DUMP
32-
}
17+
| ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `S2` failed here
3318

3419
error[E0080]: constructing invalid value at .<deref>[0]: encountered uninitialized memory, but expected an integer
3520
--> $DIR/forbidden_slices.rs:26:1
@@ -78,34 +63,24 @@ LL | pub static S7: &[u16] = unsafe {
7863
}
7964

8065
error[E0080]: constructing invalid value: encountered a dangling reference (going beyond the bounds of its allocation)
81-
--> $DIR/forbidden_slices.rs:41:1
66+
--> $DIR/forbidden_slices.rs:44:5
8267
|
83-
LL | pub static S8: &[u64] = unsafe {
84-
| ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
85-
|
86-
= 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.
87-
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
88-
HEX_DUMP
89-
}
68+
LL | from_raw_parts(ptr, 1)
69+
| ^^^^^^^^^^^^^^^^^^^^^^ evaluation of `S8` failed here
9070

9171
error[E0080]: constructing invalid value: encountered a null reference
92-
--> $DIR/forbidden_slices.rs:48:1
72+
--> $DIR/forbidden_slices.rs:48:34
9373
|
9474
LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
95-
| ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
96-
|
97-
= 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.
98-
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
99-
HEX_DUMP
100-
}
75+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `R0` failed here
10176

10277
error[E0080]: evaluation panicked: assertion failed: 0 < pointee_size && pointee_size <= isize::MAX as usize
10378
--> $DIR/forbidden_slices.rs:50:33
10479
|
10580
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; // errors inside libcore
10681
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `R1` failed here
10782

108-
error[E0080]: in-bounds pointer arithmetic failed: attempting to offset pointer by 8 bytes, but got ALLOC10 which is only 4 bytes from the end of the allocation
83+
error[E0080]: in-bounds pointer arithmetic failed: attempting to offset pointer by 8 bytes, but got ALLOC7 which is only 4 bytes from the end of the allocation
10984
--> $DIR/forbidden_slices.rs:54:25
11085
|
11186
LL | from_ptr_range(ptr..ptr.add(2)) // errors inside libcore
@@ -147,17 +122,12 @@ LL | pub static R6: &[bool] = unsafe {
147122
}
148123

149124
error[E0080]: constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
150-
--> $DIR/forbidden_slices.rs:72:1
151-
|
152-
LL | pub static R7: &[u16] = unsafe {
153-
| ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
125+
--> $DIR/forbidden_slices.rs:74:5
154126
|
155-
= 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.
156-
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
157-
HEX_DUMP
158-
}
127+
LL | from_ptr_range(ptr..ptr.add(4))
128+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `R7` failed here
159129

160-
error[E0080]: in-bounds pointer arithmetic failed: attempting to offset pointer by 8 bytes, but got ALLOC11+0x1 which is only 7 bytes from the end of the allocation
130+
error[E0080]: in-bounds pointer arithmetic failed: attempting to offset pointer by 8 bytes, but got ALLOC8+0x1 which is only 7 bytes from the end of the allocation
161131
--> $DIR/forbidden_slices.rs:79:25
162132
|
163133
LL | from_ptr_range(ptr..ptr.add(1))

tests/ui/consts/const-eval/heap/dealloc_intrinsic_dangling.stderr

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@ error[E0080]: constructing invalid value: encountered a dangling reference (use-
22
--> $DIR/dealloc_intrinsic_dangling.rs:11:1
33
|
44
LL | const _X: &'static u8 = unsafe {
5-
| ^^^^^^^^^^^^^^^^^^^^^ it is undefined behavior to use this value
6-
|
7-
= 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.
8-
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
9-
HEX_DUMP
10-
}
5+
| ^^^^^^^^^^^^^^^^^^^^^ evaluation of `_X` failed here
116

12-
error[E0080]: memory access failed: ALLOC1 has been freed, so this pointer is dangling
7+
error[E0080]: memory access failed: ALLOC0 has been freed, so this pointer is dangling
138
--> $DIR/dealloc_intrinsic_dangling.rs:22:5
149
|
1510
LL | *reference
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ptr::NonNull;
22

33
const NON_NULL: NonNull<u8> = unsafe { NonNull::dangling() };
4-
const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() }); //~ERROR: dangling pointer (it has no provenance)
4+
const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() }); //~ERROR: dangling reference
55

66
fn main() {}

tests/ui/consts/const-eval/nonnull_as_ref_ub.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0080]: memory access failed: attempting to access 1 byte, but got 0x1[noalloc] which is a dangling pointer (it has no provenance)
2-
--> $DIR/nonnull_as_ref_ub.rs:4:29
1+
error[E0080]: constructing invalid value: encountered a dangling reference (0x1[noalloc] has no provenance)
2+
--> $DIR/nonnull_as_ref_ub.rs:4:39
33
|
44
LL | const _: () = assert!(42 == *unsafe { NON_NULL.as_ref() });
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `_` failed here
5+
| ^^^^^^^^^^^^^^^^^ evaluation of `_` failed here
66

77
error: aborting due to 1 previous error
88

0 commit comments

Comments
 (0)