Skip to content

Commit 3c8563a

Browse files
committed
make NOP dyn casts not require anything about the vtable
1 parent affe0d3 commit 3c8563a

File tree

6 files changed

+46
-21
lines changed

6 files changed

+46
-21
lines changed

compiler/rustc_codegen_cranelift/src/unsize.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub(crate) fn unsized_info<'tcx>(
2929
let old_info =
3030
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
3131
if data_a.principal_def_id() == data_b.principal_def_id() {
32+
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
3233
return old_info;
3334
}
3435

compiler/rustc_codegen_ssa/src/base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ pub fn unsized_info<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
151151
let old_info =
152152
old_info.expect("unsized_info: missing old info for trait upcasting coercion");
153153
if data_a.principal_def_id() == data_b.principal_def_id() {
154+
// A NOP cast that doesn't actually change anything, should be allowed even with invalid vtables.
154155
return old_info;
155156
}
156157

compiler/rustc_const_eval/src/interpret/cast.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
298298
self.write_immediate(val, dest)
299299
}
300300
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
301-
let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?;
301+
let val = self.read_immediate(src)?;
302+
if data_a.principal() == data_b.principal() {
303+
// A NOP cast that doesn't actually change anything, should be allowed even with mismatching vtables.
304+
return self.write_immediate(*val, dest);
305+
}
306+
let (old_data, old_vptr) = val.to_scalar_pair()?;
302307
let old_vptr = old_vptr.to_pointer(self)?;
303308
let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?;
304309
if old_trait != data_a.principal() {

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -278,26 +278,36 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_,
278278
╾allocN─╼ ╾allocN─╼ │ ╾──╼╾──╼
279279
}
280280

281-
error[E0080]: evaluation of constant value failed
282-
--> $DIR/ub-wide-ptr.rs:147:62
281+
error[E0080]: it is undefined behavior to use this value
282+
--> $DIR/ub-wide-ptr.rs:147:1
283283
|
284284
LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
285-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance)
285+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a vtable pointer
286+
|
287+
= 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.
288+
= note: the raw bytes of the constant (size: 8, align: 4) {
289+
╾allocN─╼ 00 00 00 00 │ ╾──╼....
290+
}
286291

287-
error[E0080]: evaluation of constant value failed
288-
--> $DIR/ub-wide-ptr.rs:150:65
292+
error[E0080]: it is undefined behavior to use this value
293+
--> $DIR/ub-wide-ptr.rs:149:1
289294
|
290295
LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
291-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable
296+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered allocN, but expected a vtable pointer
297+
|
298+
= 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.
299+
= note: the raw bytes of the constant (size: 8, align: 4) {
300+
╾allocN─╼ ╾allocN─╼ │ ╾──╼╾──╼
301+
}
292302

293303
error[E0080]: could not evaluate static initializer
294-
--> $DIR/ub-wide-ptr.rs:157:5
304+
--> $DIR/ub-wide-ptr.rs:155:5
295305
|
296306
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
297307
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance)
298308

299309
error[E0080]: could not evaluate static initializer
300-
--> $DIR/ub-wide-ptr.rs:161:5
310+
--> $DIR/ub-wide-ptr.rs:159:5
301311
|
302312
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
303313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable

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

+18-8
Original file line numberDiff line numberDiff line change
@@ -278,26 +278,36 @@ LL | const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_,
278278
╾──────allocN───────╼ ╾──────allocN───────╼ │ ╾──────╼╾──────╼
279279
}
280280

281-
error[E0080]: evaluation of constant value failed
282-
--> $DIR/ub-wide-ptr.rs:147:62
281+
error[E0080]: it is undefined behavior to use this value
282+
--> $DIR/ub-wide-ptr.rs:147:1
283283
|
284284
LL | const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
285-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance)
285+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a vtable pointer
286+
|
287+
= 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.
288+
= note: the raw bytes of the constant (size: 16, align: 8) {
289+
╾──────allocN───────╼ 00 00 00 00 00 00 00 00 │ ╾──────╼........
290+
}
286291

287-
error[E0080]: evaluation of constant value failed
288-
--> $DIR/ub-wide-ptr.rs:150:65
292+
error[E0080]: it is undefined behavior to use this value
293+
--> $DIR/ub-wide-ptr.rs:149:1
289294
|
290295
LL | const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
291-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable
296+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered allocN, but expected a vtable pointer
297+
|
298+
= 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.
299+
= note: the raw bytes of the constant (size: 16, align: 8) {
300+
╾──────allocN───────╼ ╾──────allocN───────╼ │ ╾──────╼╾──────╼
301+
}
292302

293303
error[E0080]: could not evaluate static initializer
294-
--> $DIR/ub-wide-ptr.rs:157:5
304+
--> $DIR/ub-wide-ptr.rs:155:5
295305
|
296306
LL | mem::transmute::<_, &dyn Trait>((&92u8, 0usize))
297307
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer use: null pointer is a dangling pointer (it has no provenance)
298308

299309
error[E0080]: could not evaluate static initializer
300-
--> $DIR/ub-wide-ptr.rs:161:5
310+
--> $DIR/ub-wide-ptr.rs:159:5
301311
|
302312
LL | mem::transmute::<_, &dyn Trait>((&92u8, &3u64))
303313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using allocN as vtable pointer but it does not point to a vtable

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,9 @@ const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = unsafe { mem::transmute::<_, &bool
145145

146146
// # raw trait object
147147
const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { mem::transmute((&92u8, 0usize)) };
148-
//~^ ERROR evaluation of constant value failed
149-
//~| null pointer
148+
//~^ ERROR it is undefined behavior to use this value
150149
const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { mem::transmute((&92u8, &3u64)) };
151-
//~^ ERROR evaluation of constant value failed
152-
//~| does not point to a vtable
150+
//~^ ERROR it is undefined behavior to use this value
153151
const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = unsafe { mem::transmute::<_, &bool>(&3u8) } as *const dyn Trait; // ok because raw
154152

155153
// Const eval fails for these, so they need to be statics to error.

0 commit comments

Comments
 (0)