Skip to content

Commit fa83763

Browse files
RalfJungoli-obk
authored andcommitted
always check alignment during CTFE
1 parent 397b66e commit fa83763

File tree

8 files changed

+75
-41
lines changed

8 files changed

+75
-41
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
@@ -311,7 +311,7 @@ pub fn eval_to_allocation_raw_provider<'tcx>(
311311
CompileTimeInterpreter::new(
312312
tcx.const_eval_limit(),
313313
/*can_access_statics:*/ is_static,
314-
/*check_alignment:*/ tcx.sess.opts.unstable_opts.extra_const_ub_checks,
314+
/*check_alignment:*/ true,
315315
),
316316
);
317317

src/test/ui/const-ptr/forbidden_slices.64bit.stderr

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,18 @@ LL | pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4)
7171
╾───────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
7272
}
7373

74-
error[E0080]: it is undefined behavior to use this value
75-
--> $DIR/forbidden_slices.rs:32:1
74+
error[E0080]: could not evaluate static initializer
75+
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
7676
|
77-
LL | pub static S7: &[u16] = unsafe {
78-
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
77+
= note: accessing memory with alignment 1, but alignment 2 is required
7978
|
80-
= 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.
81-
= note: the raw bytes of the constant (size: 16, align: 8) {
82-
╾─────ALLOC_ID+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
83-
}
79+
note: inside `std::slice::from_raw_parts::<'_, u16>`
80+
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
81+
note: inside `S7`
82+
--> $DIR/forbidden_slices.rs:35:5
83+
|
84+
LL | from_raw_parts(ptr, 4)
85+
| ^^^^^^^^^^^^^^^^^^^^^^
8486

8587
error[E0080]: could not evaluate static initializer
8688
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
@@ -90,7 +92,7 @@ error[E0080]: could not evaluate static initializer
9092
note: inside `std::slice::from_raw_parts::<'_, u64>`
9193
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
9294
note: inside `S8`
93-
--> $DIR/forbidden_slices.rs:43:5
95+
--> $DIR/forbidden_slices.rs:42:5
9496
|
9597
LL | from_raw_parts(ptr, 1)
9698
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -105,7 +107,7 @@ note: inside `ptr::const_ptr::<impl *const u32>::sub_ptr`
105107
note: inside `from_ptr_range::<'_, u32>`
106108
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
107109
note: inside `R0`
108-
--> $DIR/forbidden_slices.rs:46:34
110+
--> $DIR/forbidden_slices.rs:45:34
109111
|
110112
LL | pub static R0: &[u32] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
111113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -120,7 +122,7 @@ note: inside `ptr::const_ptr::<impl *const ()>::sub_ptr`
120122
note: inside `from_ptr_range::<'_, ()>`
121123
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
122124
note: inside `R1`
123-
--> $DIR/forbidden_slices.rs:47:33
125+
--> $DIR/forbidden_slices.rs:46:33
124126
|
125127
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
126128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -136,13 +138,13 @@ note: inside `ptr::const_ptr::<impl *const u32>::offset`
136138
note: inside `ptr::const_ptr::<impl *const u32>::add`
137139
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
138140
note: inside `R2`
139-
--> $DIR/forbidden_slices.rs:50:25
141+
--> $DIR/forbidden_slices.rs:49:25
140142
|
141143
LL | from_ptr_range(ptr..ptr.add(2))
142144
| ^^^^^^^^^^
143145

144146
error[E0080]: it is undefined behavior to use this value
145-
--> $DIR/forbidden_slices.rs:52:1
147+
--> $DIR/forbidden_slices.rs:51:1
146148
|
147149
LL | pub static R4: &[u8] = unsafe {
148150
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered uninitialized bytes
@@ -153,7 +155,7 @@ LL | pub static R4: &[u8] = unsafe {
153155
}
154156

155157
error[E0080]: it is undefined behavior to use this value
156-
--> $DIR/forbidden_slices.rs:57:1
158+
--> $DIR/forbidden_slices.rs:56:1
157159
|
158160
LL | pub static R5: &[u8] = unsafe {
159161
| ^^^^^^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -165,7 +167,7 @@ LL | pub static R5: &[u8] = unsafe {
165167
}
166168

167169
error[E0080]: it is undefined behavior to use this value
168-
--> $DIR/forbidden_slices.rs:62:1
170+
--> $DIR/forbidden_slices.rs:61:1
169171
|
170172
LL | pub static R6: &[bool] = unsafe {
171173
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<deref>[0]: encountered 0x11, but expected a boolean
@@ -175,16 +177,20 @@ LL | pub static R6: &[bool] = unsafe {
175177
╾──────ALLOC_ID───────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
176178
}
177179

178-
error[E0080]: it is undefined behavior to use this value
179-
--> $DIR/forbidden_slices.rs:67:1
180+
error[E0080]: could not evaluate static initializer
181+
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
180182
|
181-
LL | pub static R7: &[u16] = unsafe {
182-
| ^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 2 byte alignment but found 1)
183+
= note: accessing memory with alignment 1, but alignment 2 is required
183184
|
184-
= 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.
185-
= note: the raw bytes of the constant (size: 16, align: 8) {
186-
╾────ALLOC_ID+0x1─────╼ 04 00 00 00 00 00 00 00 │ ╾──────╼........
187-
}
185+
note: inside `std::slice::from_raw_parts::<'_, u16>`
186+
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
187+
note: inside `from_ptr_range::<'_, u16>`
188+
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
189+
note: inside `R7`
190+
--> $DIR/forbidden_slices.rs:68:5
191+
|
192+
LL | from_ptr_range(ptr..ptr.add(4))
193+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188194

189195
error[E0080]: could not evaluate static initializer
190196
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -196,7 +202,7 @@ note: inside `ptr::const_ptr::<impl *const u64>::offset`
196202
note: inside `ptr::const_ptr::<impl *const u64>::add`
197203
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
198204
note: inside `R8`
199-
--> $DIR/forbidden_slices.rs:74:25
205+
--> $DIR/forbidden_slices.rs:72:25
200206
|
201207
LL | from_ptr_range(ptr..ptr.add(1))
202208
| ^^^^^^^^^^
@@ -211,7 +217,7 @@ note: inside `ptr::const_ptr::<impl *const u32>::sub_ptr`
211217
note: inside `from_ptr_range::<'_, u32>`
212218
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
213219
note: inside `R9`
214-
--> $DIR/forbidden_slices.rs:79:34
220+
--> $DIR/forbidden_slices.rs:77:34
215221
|
216222
LL | pub static R9: &[u32] = unsafe { from_ptr_range(&D0..(&D0 as *const u32).add(1)) };
217223
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -226,7 +232,7 @@ note: inside `ptr::const_ptr::<impl *const u32>::sub_ptr`
226232
note: inside `from_ptr_range::<'_, u32>`
227233
--> $SRC_DIR/core/src/slice/raw.rs:LL:COL
228234
note: inside `R10`
229-
--> $DIR/forbidden_slices.rs:80:35
235+
--> $DIR/forbidden_slices.rs:78:35
230236
|
231237
LL | pub static R10: &[u32] = unsafe { from_ptr_range(&D0..&D0) };
232238
| ^^^^^^^^^^^^^^^^^^^^^^^^

src/test/ui/const-ptr/forbidden_slices.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ pub static S6: &[bool] = unsafe { from_raw_parts((&D0) as *const _ as _, 4) }; /
3030

3131
// Reading padding is not ok
3232
pub static S7: &[u16] = unsafe {
33-
//~^ ERROR: it is undefined behavior to use this value
3433
let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
3534

3635
from_raw_parts(ptr, 4)
@@ -65,7 +64,6 @@ pub static R6: &[bool] = unsafe {
6564
from_ptr_range(ptr..ptr.add(4))
6665
};
6766
pub static R7: &[u16] = unsafe {
68-
//~^ ERROR: it is undefined behavior to use this value
6967
let ptr = (&D2 as *const Struct as *const u16).byte_add(1);
7068
from_ptr_range(ptr..ptr.add(4))
7169
};

src/test/ui/consts/copy-intrinsic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const COPY_ZERO: () = unsafe {
1818
let src = ();
1919
let mut dst = ();
2020
copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
21+
//~^ ERROR: evaluation of constant value failed
2122
};
2223

2324
const COPY_OOB_1: () = unsafe {
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/copy-intrinsic.rs:27:5
2+
--> $DIR/copy-intrinsic.rs:20:5
3+
|
4+
LL | copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory with alignment 1, but alignment 4 is required
6+
7+
error[E0080]: evaluation of constant value failed
8+
--> $DIR/copy-intrinsic.rs:28:5
39
|
410
LL | copy_nonoverlapping(0x100 as *const i32, dangle, 0);
511
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc5 has size 4, so pointer at offset 40 is out-of-bounds
612

713
error[E0080]: evaluation of constant value failed
8-
--> $DIR/copy-intrinsic.rs:34:5
14+
--> $DIR/copy-intrinsic.rs:35:5
915
|
1016
LL | copy_nonoverlapping(dangle, 0x100 as *mut i32, 0);
1117
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: alloc7 has size 4, so pointer at offset 40 is out-of-bounds
1218

1319
error[E0080]: evaluation of constant value failed
14-
--> $DIR/copy-intrinsic.rs:41:5
20+
--> $DIR/copy-intrinsic.rs:42:5
1521
|
1622
LL | copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
1723
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy`
1824

1925
error[E0080]: evaluation of constant value failed
20-
--> $DIR/copy-intrinsic.rs:47:5
26+
--> $DIR/copy-intrinsic.rs:48:5
2127
|
2228
LL | copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
2329
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy_nonoverlapping`
2430

25-
error: aborting due to 4 previous errors
31+
error: aborting due to 5 previous errors
2632

2733
For more information about this error, try `rustc --explain E0080`.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
3+
|
4+
= note: accessing memory with alignment 1, but alignment 4 is required
5+
|
6+
note: inside `std::ptr::read::<u32>`
7+
--> $SRC_DIR/core/src/ptr/mod.rs:LL:COL
8+
note: inside `ptr::const_ptr::<impl *const u32>::read`
9+
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
10+
note: inside `INNER`
11+
--> $DIR/detect-extra-ub.rs:37:9
12+
|
13+
LL | ptr.read();
14+
| ^^^^^^^^^^
15+
16+
note: erroneous constant used
17+
--> $DIR/detect-extra-ub.rs:31:5
18+
|
19+
LL | INNER;
20+
| ^^^^^
21+
22+
error: aborting due to previous error
23+
24+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/extra-const-ub/detect-extra-ub.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// revisions: no_flag with_flag
2-
// [no_flag] check-pass
32
// [with_flag] compile-flags: -Zextra-const-ub-checks
43
#![feature(const_ptr_read)]
54

src/test/ui/consts/extra-const-ub/detect-extra-ub.with_flag.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/detect-extra-ub.rs:9:20
2+
--> $DIR/detect-extra-ub.rs:8:20
33
|
44
LL | let _x: bool = transmute(3u8);
55
| ^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean
66

77
error[E0080]: evaluation of constant value failed
8-
--> $DIR/detect-extra-ub.rs:15:21
8+
--> $DIR/detect-extra-ub.rs:14:21
99
|
1010
LL | let _x: usize = transmute(&3u8);
1111
| ^^^^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -14,7 +14,7 @@ LL | let _x: usize = transmute(&3u8);
1414
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
1515

1616
error[E0080]: evaluation of constant value failed
17-
--> $DIR/detect-extra-ub.rs:21:30
17+
--> $DIR/detect-extra-ub.rs:20:30
1818
|
1919
LL | let _x: (usize, usize) = transmute(x);
2020
| ^^^^^^^^^^^^ unable to turn pointer into raw bytes
@@ -23,7 +23,7 @@ LL | let _x: (usize, usize) = transmute(x);
2323
= help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
2424

2525
error[E0080]: evaluation of constant value failed
26-
--> $DIR/detect-extra-ub.rs:26:20
26+
--> $DIR/detect-extra-ub.rs:25:20
2727
|
2828
LL | let _x: &u32 = transmute(&[0u8; 4]);
2929
| ^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered an unaligned reference (required 4 byte alignment but found 1)
@@ -38,13 +38,13 @@ note: inside `std::ptr::read::<u32>`
3838
note: inside `ptr::const_ptr::<impl *const u32>::read`
3939
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
4040
note: inside `INNER`
41-
--> $DIR/detect-extra-ub.rs:38:9
41+
--> $DIR/detect-extra-ub.rs:37:9
4242
|
4343
LL | ptr.read();
4444
| ^^^^^^^^^^
4545

4646
note: erroneous constant used
47-
--> $DIR/detect-extra-ub.rs:32:5
47+
--> $DIR/detect-extra-ub.rs:31:5
4848
|
4949
LL | INNER;
5050
| ^^^^^

0 commit comments

Comments
 (0)