Skip to content

Commit 83de3cb

Browse files
committed
Add regression test
1 parent 7740e1b commit 83de3cb

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#![feature(trait_upcasting)]
2+
// known-bug: #120222
3+
// check-pass
4+
//! This will segfault at runtime.
5+
6+
pub trait SupSupA {
7+
fn method(&self) {}
8+
}
9+
pub trait SupSupB {}
10+
impl<T> SupSupA for T {}
11+
impl<T> SupSupB for T {}
12+
13+
pub trait Super<T>: SupSupA + SupSupB {}
14+
15+
pub trait Unimplemented {}
16+
17+
pub trait Trait<T1, T2>: Super<T1> + Super<T2> {
18+
fn missing_method(&self)
19+
where
20+
T1: Unimplemented,
21+
{
22+
}
23+
}
24+
25+
impl<S, T> Super<T> for S {}
26+
27+
impl<S, T1, T2> Trait<T1, T2> for S {}
28+
29+
#[inline(never)]
30+
pub fn user1() -> &'static dyn Trait<u8, u8> {
31+
&()
32+
/* VTABLE:
33+
.L__unnamed_2:
34+
.quad core::ptr::drop_in_place<()>
35+
.asciz "\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000"
36+
.quad example::SupSupA::method
37+
.quad .L__unnamed_4 // SupSupB vtable (pointer)
38+
.zero 8 // null pointer for missing_method
39+
*/
40+
}
41+
42+
#[inline(never)]
43+
pub fn user2() -> &'static dyn Trait<u8, u16> {
44+
&()
45+
/* VTABLE:
46+
.L__unnamed_3:
47+
.quad core::ptr::drop_in_place<()>
48+
.asciz "\000\000\000\000\000\000\000\000\001\000\000\000\000\000\000"
49+
.quad example::SupSupA::method
50+
.quad .L__unnamed_4 // SupSupB vtable (pointer)
51+
.quad .L__unnamed_5 // Super<u16> vtable (pointer)
52+
.zero 8 // null pointer for missing_method
53+
*/
54+
}
55+
56+
fn main() {
57+
let p: *const dyn Trait<u8, u8> = &();
58+
let p = p as *const dyn Trait<u8, u16>; // <- this is bad!
59+
let p = p as *const dyn Super<u16>; // <- this upcast accesses improper vtable entry
60+
// accessing from L__unnamed_2 the position for the 'Super<u16> vtable (pointer)',
61+
// thus reading 'null pointer for missing_method'
62+
63+
let p = p as *const dyn SupSupB; // <- this upcast dereferences (null) pointer from that entry
64+
// to read the SupSupB vtable (pointer)
65+
66+
// SEGFAULT
67+
68+
println!("{:?}", p);
69+
}

0 commit comments

Comments
 (0)