Skip to content

Commit e29f3e8

Browse files
committed
Test invalid vtable size/alignment const UB errors
1 parent cae1918 commit e29f3e8

File tree

4 files changed

+129
-27
lines changed

4 files changed

+129
-27
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/ub-incorrect-vtable.rs:19:14
3+
|
4+
LL | / const INVALID_VTABLE_ALIGNMENT: &dyn Trait =
5+
LL | | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
6+
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
7+
| |
8+
| invalid vtable: alignment `1000` is not a power of 2
9+
|
10+
= note: `#[deny(const_err)]` on by default
11+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
12+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
13+
14+
error: any use of this value will cause an error
15+
--> $DIR/ub-incorrect-vtable.rs:25:14
16+
|
17+
LL | / const INVALID_VTABLE_SIZE: &dyn Trait =
18+
LL | | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
19+
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
20+
| |
21+
| invalid vtable: size is bigger than largest supported object
22+
|
23+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
25+
26+
error[E0080]: it is undefined behavior to use this value
27+
--> $DIR/ub-incorrect-vtable.rs:36:1
28+
|
29+
LL | / const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> =
30+
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) };
31+
| |_____________________________________________________________________________________________^ type validation failed: encountered invalid vtable: alignment `1000` is not a power of 2 at .0
32+
|
33+
= 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.
34+
= note: the raw bytes of the constant (size: 8, align: 4) {
35+
╾─allocN─╼ ╾─allocN─╼ │ ╾──╼╾──╼
36+
}
37+
38+
error[E0080]: it is undefined behavior to use this value
39+
--> $DIR/ub-incorrect-vtable.rs:41:1
40+
|
41+
LL | / const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> =
42+
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) };
43+
| |______________________________________________________________________________________________^ type validation failed: encountered invalid vtable: size is bigger than largest supported object at .0
44+
|
45+
= 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.
46+
= note: the raw bytes of the constant (size: 8, align: 4) {
47+
╾─allocN─╼ ╾─allocN─╼ │ ╾──╼╾──╼
48+
}
49+
50+
error: aborting due to 4 previous errors
51+
52+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/ub-incorrect-vtable.rs:19:14
3+
|
4+
LL | / const INVALID_VTABLE_ALIGNMENT: &dyn Trait =
5+
LL | | unsafe { std::mem::transmute((&92u8, &[0usize, 1usize, 1000usize])) };
6+
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
7+
| |
8+
| invalid vtable: alignment `1000` is not a power of 2
9+
|
10+
= note: `#[deny(const_err)]` on by default
11+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
12+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
13+
14+
error: any use of this value will cause an error
15+
--> $DIR/ub-incorrect-vtable.rs:25:14
16+
|
17+
LL | / const INVALID_VTABLE_SIZE: &dyn Trait =
18+
LL | | unsafe { std::mem::transmute((&92u8, &[1usize, usize::MAX, 1usize])) };
19+
| |______________^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^__-
20+
| |
21+
| invalid vtable: size is bigger than largest supported object
22+
|
23+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
25+
26+
error[E0080]: it is undefined behavior to use this value
27+
--> $DIR/ub-incorrect-vtable.rs:36:1
28+
|
29+
LL | / const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> =
30+
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) };
31+
| |_____________________________________________________________________________________________^ type validation failed: encountered invalid vtable: alignment `1000` is not a power of 2 at .0
32+
|
33+
= 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.
34+
= note: the raw bytes of the constant (size: 16, align: 8) {
35+
╾───────allocN───────╼ ╾───────allocN───────╼ │ ╾──────╼╾──────╼
36+
}
37+
38+
error[E0080]: it is undefined behavior to use this value
39+
--> $DIR/ub-incorrect-vtable.rs:41:1
40+
|
41+
LL | / const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> =
42+
LL | | unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) };
43+
| |______________________________________________________________________________________________^ type validation failed: encountered invalid vtable: size is bigger than largest supported object at .0
44+
|
45+
= 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.
46+
= note: the raw bytes of the constant (size: 16, align: 8) {
47+
╾───────allocN───────╼ ╾───────allocN───────╼ │ ╾──────╼╾──────╼
48+
}
49+
50+
error: aborting due to 4 previous errors
51+
52+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-eval/ub-incorrect-vtable.rs

+25
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
// triggers an error
44
// - a similar test that triggers a previously-untested const UB error: emitted close to the above
55
// error, it checks the correctness of the size
6+
//
7+
// As is, this code will only hard error when the constants are used, and the errors are emitted via
8+
// the `#[allow]`-able `const_err` lint. However, if the transparent wrapper technique to prevent
9+
// reborrows is used -- from `ub-wide-ptr.rs` -- these two errors reach validation and would trigger
10+
// ICEs as tracked by #86193. So we also use the transparent wrapper to verify proper validation
11+
// errors are emitted instead of ICEs.
12+
13+
// stderr-per-bitwidth
14+
// normalize-stderr-test "alloc\d+" -> "allocN"
615

716
trait Trait {}
817

@@ -18,4 +27,20 @@ const INVALID_VTABLE_SIZE: &dyn Trait =
1827
//~| WARNING this was previously accepted by the compiler
1928
//~| invalid vtable: size is bigger than largest supported object
2029

30+
#[repr(transparent)]
31+
struct W<T>(T);
32+
33+
// The drop fn is checked before size/align are, so get ourselves a "sufficiently valid" drop fn
34+
fn drop_me(_: *mut usize) {}
35+
36+
const INVALID_VTABLE_ALIGNMENT_UB: W<&dyn Trait> =
37+
unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), 1usize, 1000usize))) };
38+
//~^^ ERROR it is undefined behavior to use this value
39+
//~| invalid vtable: alignment `1000` is not a power of 2
40+
41+
const INVALID_VTABLE_SIZE_UB: W<&dyn Trait> =
42+
unsafe { std::mem::transmute((&92u8, &(drop_me as fn(*mut usize), usize::MAX, 1usize))) };
43+
//~^^ ERROR it is undefined behavior to use this value
44+
//~| invalid vtable: size is bigger than largest supported object
45+
2146
fn main() {}

src/test/ui/consts/const-eval/ub-incorrect-vtable.stderr

-27
This file was deleted.

0 commit comments

Comments
 (0)