Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a6b3541

Browse files
committed
adjust for symbolic vtables
1 parent 9cab797 commit a6b3541

File tree

11 files changed

+47
-49
lines changed

11 files changed

+47
-49
lines changed

src/intptrcast.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ impl<'mir, 'tcx> GlobalStateInner {
8686
if global_state.exposed.contains(&alloc_id) {
8787
let (_size, _align, kind) = ecx.get_alloc_info(alloc_id);
8888
match kind {
89-
AllocKind::LiveData | AllocKind::Function => return Some(alloc_id),
89+
AllocKind::LiveData | AllocKind::Function | AllocKind::VTable => {
90+
return Some(alloc_id);
91+
}
9092
AllocKind::Dead => {}
9193
}
9294
}
@@ -187,8 +189,8 @@ impl<'mir, 'tcx> GlobalStateInner {
187189

188190
// Remember next base address. If this allocation is zero-sized, leave a gap
189191
// of at least 1 to avoid two allocations having the same base address.
190-
// (The logic in `alloc_id_from_addr` assumes unique addresses, and function
191-
// pointers to different functions need to be distinguishable!)
192+
// (The logic in `alloc_id_from_addr` assumes unique addresses, and different
193+
// function/vtable pointers need to be distinguishable!)
192194
global_state.next_base_addr = base_addr.checked_add(max(size.bytes(), 1)).unwrap();
193195
// Given that `next_base_addr` increases in each allocation, pushing the
194196
// corresponding tuple keeps `int_to_ptr_map` sorted

src/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
733733
if cfg!(debug_assertions) {
734734
// The machine promises to never call us on thread-local or extern statics.
735735
let alloc_id = ptr.provenance;
736-
match ecx.tcx.get_global_alloc(alloc_id) {
736+
match ecx.tcx.try_get_global_alloc(alloc_id) {
737737
Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_thread_local_static(def_id) => {
738738
panic!("adjust_alloc_base_pointer called on thread-local static")
739739
}

src/shims/backtrace.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
122122
let this = self.eval_context_mut();
123123

124124
let ptr = this.read_pointer(ptr)?;
125-
// Take apart the pointer, we need its pieces.
125+
// Take apart the pointer, we need its pieces. The offset encodes the span.
126126
let (alloc_id, offset, _prov) = this.ptr_get_alloc_id(ptr)?;
127127

128-
let fn_instance =
129-
if let Some(GlobalAlloc::Function(instance)) = this.tcx.get_global_alloc(alloc_id) {
130-
instance
131-
} else {
132-
throw_ub_format!("expected function pointer, found {:?}", ptr);
133-
};
128+
// This has to be an actual global fn ptr, not a dlsym function.
129+
let fn_instance = if let Some(GlobalAlloc::Function(instance)) =
130+
this.tcx.try_get_global_alloc(alloc_id)
131+
{
132+
instance
133+
} else {
134+
throw_ub_format!("expected static function pointer, found {:?}", ptr);
135+
};
134136

135137
let lo =
136138
this.tcx.sess.source_map().lookup_char_pos(BytePos(offset.bytes().try_into().unwrap()));

src/stacked_borrows/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ trait EvalContextPrivExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
799799
stacked_borrows.history.log_protector(orig_tag, new_tag, current_span);
800800
}
801801
}
802-
AllocKind::Function | AllocKind::Dead => {
802+
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
803803
// No stacked borrows on these allocations.
804804
}
805805
}
@@ -1143,7 +1143,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
11431143
trace!("Stacked Borrows tag {tag:?} exposed in {alloc_id:?}");
11441144
alloc_extra.stacked_borrows.as_ref().unwrap().borrow_mut().exposed_tags.insert(tag);
11451145
}
1146-
AllocKind::Function | AllocKind::Dead => {
1146+
AllocKind::Function | AllocKind::VTable | AllocKind::Dead => {
11471147
// No stacked borrows on these allocations.
11481148
}
11491149
}

tests/fail/issue-miri-1112.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl FunnyPointer {
2828
data: data as *const _ as *const (),
2929
vtable: ptr as *const _ as *const (),
3030
};
31-
let obj = std::mem::transmute::<FatPointer, *mut FunnyPointer>(obj); //~ ERROR: invalid drop function pointer in vtable
31+
let obj = std::mem::transmute::<FatPointer, *mut FunnyPointer>(obj); //~ ERROR: expected a vtable pointer
3232
&*obj
3333
}
3434
}

tests/fail/issue-miri-1112.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: constructing invalid value: encountered invalid drop function pointer in vtable (function has incompatible signature)
1+
error: Undefined Behavior: constructing invalid value: encountered $HEX[ALLOC]<TAG>, but expected a vtable pointer
22
--> $DIR/issue-miri-1112.rs:LL:CC
33
|
44
LL | let obj = std::mem::transmute::<FatPointer, *mut FunnyPointer>(obj);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered invalid drop function pointer in vtable (function has incompatible signature)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered $HEX[ALLOC]<TAG>, but expected a vtable pointer
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/stacked_borrows/vtable.stderr

Lines changed: 0 additions & 25 deletions
This file was deleted.

tests/fail/validity/invalid_wide_raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ fn main() {
77
#[allow(dead_code)]
88
x: *mut dyn T,
99
}
10-
dbg!(S { x: unsafe { std::mem::transmute((0usize, 0usize)) } }); //~ ERROR: encountered dangling vtable pointer in wide pointer
10+
dbg!(S { x: unsafe { std::mem::transmute((0usize, 0usize)) } }); //~ ERROR: encountered null pointer, but expected a vtable pointer
1111
}

tests/fail/validity/invalid_wide_raw.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: constructing invalid value: encountered dangling vtable pointer in wide pointer
1+
error: Undefined Behavior: constructing invalid value: encountered null pointer, but expected a vtable pointer
22
--> $DIR/invalid_wide_raw.rs:LL:CC
33
|
44
LL | dbg!(S { x: unsafe { std::mem::transmute((0usize, 0usize)) } });
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered dangling vtable pointer in wide pointer
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered null pointer, but expected a vtable pointer
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
//@error-pattern: vtable pointer does not have permission
2-
#![feature(ptr_metadata)]
1+
#![feature(ptr_metadata, layout_for_ptr)]
2+
3+
use std::{mem, ptr};
34

45
trait Foo {}
56

67
impl Foo for u32 {}
78

89
fn uwu(thin: *const (), meta: &'static ()) -> *const dyn Foo {
9-
core::ptr::from_raw_parts(thin, unsafe { core::mem::transmute(meta) })
10+
ptr::from_raw_parts(thin, unsafe { mem::transmute(meta) })
1011
}
1112

1213
fn main() {
1314
unsafe {
1415
let orig = 1_u32;
1516
let x = &orig as &dyn Foo;
1617
let (ptr, meta) = (x as *const dyn Foo).to_raw_parts();
17-
let _ = uwu(ptr, core::mem::transmute(meta));
18+
let ptr = uwu(ptr, mem::transmute(meta));
19+
mem::size_of_val_raw(ptr);
1820
}
1921
}

tests/pass/pointers.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::mem::transmute;
1+
#![feature(ptr_metadata)]
2+
3+
use std::mem::{self, transmute};
4+
use std::ptr;
25

36
fn one_line_ref() -> i16 {
47
*&1
@@ -71,6 +74,19 @@ fn wide_ptr_ops() {
7174
assert!(!(a > b));
7275
}
7376

77+
fn metadata_vtable() {
78+
let p = &0i32 as &dyn std::fmt::Debug;
79+
let meta: ptr::DynMetadata<_> = ptr::metadata(p as *const _);
80+
assert_eq!(meta.size_of(), mem::size_of::<i32>());
81+
assert_eq!(meta.align_of(), mem::align_of::<i32>());
82+
83+
type T = [i32; 16];
84+
let p = &T::default() as &dyn std::fmt::Debug;
85+
let meta: ptr::DynMetadata<_> = ptr::metadata(p as *const _);
86+
assert_eq!(meta.size_of(), mem::size_of::<T>());
87+
assert_eq!(meta.align_of(), mem::align_of::<T>());
88+
}
89+
7490
fn main() {
7591
assert_eq!(one_line_ref(), 1);
7692
assert_eq!(basic_ref(), 1);
@@ -116,4 +132,5 @@ fn main() {
116132
assert!(dangling >= 4);
117133

118134
wide_ptr_ops();
135+
metadata_vtable();
119136
}

0 commit comments

Comments
 (0)