Skip to content

Commit aa2eaca

Browse files
committed
add test for insufficiently aligned vtable
1 parent e0320b5 commit aa2eaca

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/test/ui/consts/const-eval/ub-wide-ptr.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ const TRAIT_OBJ_SHORT_VTABLE_2: &dyn Trait = unsafe { mem::transmute((&92u8, &3u
104104
// bad trait object
105105
const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, 4usize)) };
106106
//~^ ERROR it is undefined behavior to use this value
107+
const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
108+
//~^ ERROR it is undefined behavior to use this value
107109

108110
// bad data *inside* the trait object
109111
const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };

src/test/ui/consts/const-eval/ub-wide-ptr.stderr

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,41 +159,49 @@ LL | const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, 4u
159159
= 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.
160160

161161
error[E0080]: it is undefined behavior to use this value
162-
--> $DIR/ub-wide-ptr.rs:109:1
162+
--> $DIR/ub-wide-ptr.rs:107:1
163+
|
164+
LL | const TRAIT_OBJ_UNALIGNED_VTABLE: &dyn Trait = unsafe { mem::transmute((&92u8, &[0u8; 128])) };
165+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
166+
|
167+
= 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.
168+
169+
error[E0080]: it is undefined behavior to use this value
170+
--> $DIR/ub-wide-ptr.rs:111:1
163171
|
164172
LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) };
165173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x03 at .<deref>.<dyn-downcast>, but expected a boolean
166174
|
167175
= 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.
168176

169177
error[E0080]: it is undefined behavior to use this value
170-
--> $DIR/ub-wide-ptr.rs:113:1
178+
--> $DIR/ub-wide-ptr.rs:115:1
171179
|
172180
LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
173181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
174182
|
175183
= 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.
176184

177185
error[E0080]: it is undefined behavior to use this value
178-
--> $DIR/ub-wide-ptr.rs:115:1
186+
--> $DIR/ub-wide-ptr.rs:117:1
179187
|
180188
LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
181189
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered dangling or unaligned vtable pointer in wide pointer or too small vtable
182190
|
183191
= 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.
184192

185193
error[E0080]: could not evaluate static initializer
186-
--> $DIR/ub-wide-ptr.rs:121:5
194+
--> $DIR/ub-wide-ptr.rs:123:5
187195
|
188196
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
189197
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ inbounds test failed: 0x0 is not a valid pointer
190198

191199
error[E0080]: could not evaluate static initializer
192-
--> $DIR/ub-wide-ptr.rs:125:5
200+
--> $DIR/ub-wide-ptr.rs:127:5
193201
|
194202
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
195203
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: pointer must be in-bounds at offset N, but is outside bounds of allocN which has size N
196204

197-
error: aborting due to 24 previous errors
205+
error: aborting due to 25 previous errors
198206

199207
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)