1
Fork 0

always check alignment during CTFE

This commit is contained in:
Ralf Jung 2022-11-19 23:36:20 +01:00 committed by Oli Scherer
parent 397b66e77b
commit fa83763491
8 changed files with 75 additions and 41 deletions

View file

@ -311,7 +311,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
CompileTimeInterpreter::new( CompileTimeInterpreter::new(
tcx.const_eval_limit(), tcx.const_eval_limit(),
/*can_access_statics:*/ is_static, /*can_access_statics:*/ is_static,
/*check_alignment:*/ tcx.sess.opts.unstable_opts.extra_const_ub_checks, /*check_alignment:*/ true,
), ),
); );

View file

@ -71,16 +71,18 @@ LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4)
╾───────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ ╾───────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
} }
error[E0080]: it is undefined behavior to use this value error[E0080]: could not evaluate static initializer
--> $DIR/forbidden_slices.rs:32:1 --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
| |
LL | pub static S7: &[u16] = unsafe { = note: accessing memory with alignment 1, but alignment 2 is required
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
| |
= 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: inside `std::slice::from_raw_parts::<'_, u16>`
= note: the raw bytes of the constant (size: 16, align: 8) { --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
╾─────ALLOC_ID+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ note: inside `S7`
} --> $DIR/forbidden_slices.rs:35:5
|
LL | from_raw_parts(ptr, 4)
| ^^^^^^^^^^^^^^^^^^^^^^
error[E0080]: could not evaluate static initializer error[E0080]: could not evaluate static initializer
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
@ -90,7 +92,7 @@ error[E0080]: could not evaluate static initializer
note: inside `std::slice::from_raw_parts::<'_, u64>` note: inside `std::slice::from_raw_parts::<'_, u64>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
note: inside `S8` note: inside `S8`
--> $DIR/forbidden_slices.rs:43:5 --> $DIR/forbidden_slices.rs:42:5
| |
LL | from_raw_parts(ptr, 1) LL | from_raw_parts(ptr, 1)
| ^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^
@ -105,7 +107,7 @@ note: inside `ptr::const_ptr::<impl *const u32>::sub_ptr`
note: inside `from_ptr_range::<'_, u32>` note: inside `from_ptr_range::<'_, u32>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
note: inside `R0` note: inside `R0`
--> $DIR/forbidden_slices.rs:46:34 --> $DIR/forbidden_slices.rs:45:34
| |
LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -120,7 +122,7 @@ note: inside `ptr::const_ptr::<impl *const ()>::sub_ptr`
note: inside `from_ptr_range::<'_, ()>` note: inside `from_ptr_range::<'_, ()>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
note: inside `R1` note: inside `R1`
--> $DIR/forbidden_slices.rs:47:33 --> $DIR/forbidden_slices.rs:46:33
| |
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) }; LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -136,13 +138,13 @@ note: inside `ptr::const_ptr::<impl *const u32>::offset`
note: inside `ptr::const_ptr::<impl *const u32>::add` note: inside `ptr::const_ptr::<impl *const u32>::add`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `R2` note: inside `R2`
--> $DIR/forbidden_slices.rs:50:25 --> $DIR/forbidden_slices.rs:49:25
| |
LL | from_ptr_range(ptr..ptr.add(2)) LL | from_ptr_range(ptr..ptr.add(2))
| ^^^^^^^^^^ | ^^^^^^^^^^
error[E0080]: it is undefined behavior to use this value error[E0080]: it is undefined behavior to use this value
--> $DIR/forbidden_slices.rs:52:1 --> $DIR/forbidden_slices.rs:51:1
| |
LL | pub static R4: &[u8] = unsafe { LL | pub static R4: &[u8] = unsafe {
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes | ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
@ -153,7 +155,7 @@ LL | pub static R4: &[u8] = unsafe {
} }
error[E0080]: it is undefined behavior to use this value error[E0080]: it is undefined behavior to use this value
--> $DIR/forbidden_slices.rs:57:1 --> $DIR/forbidden_slices.rs:56:1
| |
LL | pub static R5: &[u8] = unsafe { LL | pub static R5: &[u8] = unsafe {
| ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@ -165,7 +167,7 @@ LL | pub static R5: &[u8] = unsafe {
} }
error[E0080]: it is undefined behavior to use this value error[E0080]: it is undefined behavior to use this value
--> $DIR/forbidden_slices.rs:62:1 --> $DIR/forbidden_slices.rs:61:1
| |
LL | pub static R6: &[bool] = unsafe { LL | pub static R6: &[bool] = unsafe {
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean | ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
@ -175,16 +177,20 @@ LL | pub static R6: &[bool] = unsafe {
╾──────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ ╾──────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
} }
error[E0080]: it is undefined behavior to use this value error[E0080]: could not evaluate static initializer
--> $DIR/forbidden_slices.rs:67:1 --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
| |
LL | pub static R7: &[u16] = unsafe { = note: accessing memory with alignment 1, but alignment 2 is required
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
| |
= 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: inside `std::slice::from_raw_parts::<'_, u16>`
= note: the raw bytes of the constant (size: 16, align: 8) { --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
╾────ALLOC_ID+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........ note: inside `from_ptr_range::<'_, u16>`
} --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
note: inside `R7`
--> $DIR/forbidden_slices.rs:68:5
|
LL | from_ptr_range(ptr..ptr.add(4))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0080]: could not evaluate static initializer error[E0080]: could not evaluate static initializer
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@ -196,7 +202,7 @@ note: inside `ptr::const_ptr::<impl *const u64>::offset`
note: inside `ptr::const_ptr::<impl *const u64>::add` note: inside `ptr::const_ptr::<impl *const u64>::add`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `R8` note: inside `R8`
--> $DIR/forbidden_slices.rs:74:25 --> $DIR/forbidden_slices.rs:72:25
| |
LL | from_ptr_range(ptr..ptr.add(1)) LL | from_ptr_range(ptr..ptr.add(1))
| ^^^^^^^^^^ | ^^^^^^^^^^
@ -211,7 +217,7 @@ note: inside `ptr::const_ptr::<impl *const u32>::sub_ptr`
note: inside `from_ptr_range::<'_, u32>` note: inside `from_ptr_range::<'_, u32>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
note: inside `R9` note: inside `R9`
--> $DIR/forbidden_slices.rs:79:34 --> $DIR/forbidden_slices.rs:77:34
| |
LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) }; LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -226,7 +232,7 @@ note: inside `ptr::const_ptr::<impl *const u32>::sub_ptr`
note: inside `from_ptr_range::<'_, u32>` note: inside `from_ptr_range::<'_, u32>`
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL --> $SRC_DIR/core/src/slice/raw.rs:LL:COL
note: inside `R10` note: inside `R10`
--> $DIR/forbidden_slices.rs:80:35 --> $DIR/forbidden_slices.rs:78:35
| |
LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) }; LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) };
| ^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^

View file

@ -30,7 +30,6 @@ pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; /
// Reading padding is not ok // Reading padding is not ok
pub static S7: &[u16] = unsafe { pub static S7: &[u16] = unsafe {
//~^ ERROR: it is undefined behavior to use this value
let ptr = (&D2 as *const Struct as *const u16).byte_add(1); let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
from_raw_parts(ptr, 4) from_raw_parts(ptr, 4)
@ -65,7 +64,6 @@ pub static R6: &[bool] = unsafe {
from_ptr_range(ptr..ptr.add(4)) from_ptr_range(ptr..ptr.add(4))
}; };
pub static R7: &[u16] = unsafe { pub static R7: &[u16] = unsafe {
//~^ ERROR: it is undefined behavior to use this value
let ptr = (&D2 as *const Struct as *const u16).byte_add(1); let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
from_ptr_range(ptr..ptr.add(4)) from_ptr_range(ptr..ptr.add(4))
}; };

View file

@ -18,6 +18,7 @@ const COPY_ZERO: () = unsafe {
let src = (); let src = ();
let mut dst = (); let mut dst = ();
copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0); copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
//~^ ERROR: evaluation of constant value failed
}; };
const COPY_OOB_1: () = unsafe { const COPY_OOB_1: () = unsafe {

View file

@ -1,27 +1,33 @@
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/copy-intrinsic.rs:27:5 --> $DIR/copy-intrinsic.rs:20:5
|
LL | copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory with alignment 1, but alignment 4 is required
error[E0080]: evaluation of constant value failed
--> $DIR/copy-intrinsic.rs:28:5
| |
LL | copy_nonoverlapping(0x100 as *const i32, dangle, 0); LL | copy_nonoverlapping(0x100 as *const i32, dangle, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc5 has size 4, so pointer at offset 40 is out-of-bounds | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc5 has size 4, so pointer at offset 40 is out-of-bounds
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/copy-intrinsic.rs:34:5 --> $DIR/copy-intrinsic.rs:35:5
| |
LL | copy_nonoverlapping(dangle, 0x100 as *mut i32, 0); LL | copy_nonoverlapping(dangle, 0x100 as *mut i32, 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc7 has size 4, so pointer at offset 40 is out-of-bounds | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc7 has size 4, so pointer at offset 40 is out-of-bounds
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/copy-intrinsic.rs:41:5 --> $DIR/copy-intrinsic.rs:42:5
| |
LL | copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); LL | copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy`
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/copy-intrinsic.rs:47:5 --> $DIR/copy-intrinsic.rs:48:5
| |
LL | copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); LL | copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy_nonoverlapping` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy_nonoverlapping`
error: aborting due to 4 previous errors error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0080`. For more information about this error, try `rustc --explain E0080`.

View file

@ -0,0 +1,24 @@
error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
|
= note: accessing memory with alignment 1, but alignment 4 is required
|
note: inside `std::ptr::read::<u32>`
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
note: inside `ptr::const_ptr::<impl *const u32>::read`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `INNER`
--> $DIR/detect-extra-ub.rs:37:9
|
LL | ptr.read();
| ^^^^^^^^^^
note: erroneous constant used
--> $DIR/detect-extra-ub.rs:31:5
|
LL | INNER;
| ^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.

View file

@ -1,5 +1,4 @@
// revisions: no_flag with_flag // revisions: no_flag with_flag
// [no_flag] check-pass
// [with_flag] compile-flags: -Zextra-const-ub-checks // [with_flag] compile-flags: -Zextra-const-ub-checks
#![feature(const_ptr_read)] #![feature(const_ptr_read)]

View file

@ -1,11 +1,11 @@
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/detect-extra-ub.rs:9:20 --> $DIR/detect-extra-ub.rs:8:20
| |
LL | let _x: bool = transmute(3u8); LL | let _x: bool = transmute(3u8);
| ^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean | ^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/detect-extra-ub.rs:15:21 --> $DIR/detect-extra-ub.rs:14:21
| |
LL | let _x: usize = transmute(&3u8); LL | let _x: usize = transmute(&3u8);
| ^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes | ^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@ -14,7 +14,7 @@ LL | let _x: usize = transmute(&3u8);
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/detect-extra-ub.rs:21:30 --> $DIR/detect-extra-ub.rs:20:30
| |
LL | let _x: (usize, usize) = transmute(x); LL | let _x: (usize, usize) = transmute(x);
| ^^^^^^^^^^^^ unable to turn pointer into raw bytes | ^^^^^^^^^^^^ unable to turn pointer into raw bytes
@ -23,7 +23,7 @@ LL | let _x: (usize, usize) = transmute(x);
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/detect-extra-ub.rs:26:20 --> $DIR/detect-extra-ub.rs:25:20
| |
LL | let _x: &u32 = transmute(&[0u8; 4]); LL | let _x: &u32 = transmute(&[0u8; 4]);
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1) | ^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)
@ -38,13 +38,13 @@ note: inside `std::ptr::read::<u32>`
note: inside `ptr::const_ptr::<impl *const u32>::read` note: inside `ptr::const_ptr::<impl *const u32>::read`
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
note: inside `INNER` note: inside `INNER`
--> $DIR/detect-extra-ub.rs:38:9 --> $DIR/detect-extra-ub.rs:37:9
| |
LL | ptr.read(); LL | ptr.read();
| ^^^^^^^^^^ | ^^^^^^^^^^
note: erroneous constant used note: erroneous constant used
--> $DIR/detect-extra-ub.rs:32:5 --> $DIR/detect-extra-ub.rs:31:5
| |
LL | INNER; LL | INNER;
| ^^^^^ | ^^^^^