Skip to content

Commit 925977f

Browse files
committed
add intrinsic to access vtable size and align
1 parent a0091db commit 925977f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

core/src/intrinsics.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,16 @@ extern "rust-intrinsic" {
22912291
/// [`std::hint::black_box`]: crate::hint::black_box
22922292
#[rustc_const_unstable(feature = "const_black_box", issue = "none")]
22932293
pub fn black_box<T>(dummy: T) -> T;
2294+
2295+
/// `ptr` must point to a vtable.
2296+
/// The intrinsic will return the size stored in that vtable.
2297+
#[cfg(not(bootstrap))]
2298+
pub fn vtable_size(ptr: *const ()) -> usize;
2299+
2300+
/// `ptr` must point to a vtable.
2301+
/// The intrinsic will return the alignment stored in that vtable.
2302+
#[cfg(not(bootstrap))]
2303+
pub fn vtable_align(ptr: *const ()) -> usize;
22942304
}
22952305

22962306
// Some functions are defined here because they accidentally got made

core/src/ptr/metadata.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,20 @@ pub struct DynMetadata<Dyn: ?Sized> {
180180
phantom: crate::marker::PhantomData<Dyn>,
181181
}
182182

183+
/// Opaque type for accessing vtables.
184+
///
185+
/// Private implementation detail of `DynMetadata::size_of` etc.
186+
/// Must be zero-sized since there is conceptually not actually any Abstract Machine memory behind this pointer.
187+
/// However, we can require pointer alignment.
188+
#[repr(C)]
189+
#[cfg(not(bootstrap))]
190+
struct VTable([usize; 0]);
191+
183192
/// The common prefix of all vtables. It is followed by function pointers for trait methods.
184193
///
185194
/// Private implementation detail of `DynMetadata::size_of` etc.
186195
#[repr(C)]
196+
#[cfg(bootstrap)]
187197
struct VTable {
188198
drop_in_place: fn(*mut ()),
189199
size_of: usize,
@@ -194,13 +204,25 @@ impl<Dyn: ?Sized> DynMetadata<Dyn> {
194204
/// Returns the size of the type associated with this vtable.
195205
#[inline]
196206
pub fn size_of(self) -> usize {
197-
self.vtable_ptr.size_of
207+
#[cfg(bootstrap)]
208+
return self.vtable_ptr.size_of;
209+
#[cfg(not(bootstrap))]
210+
// SAFETY: DynMetadata always contains a valid vtable pointer
211+
return unsafe {
212+
crate::intrinsics::vtable_size(self.vtable_ptr as *const VTable as *const ())
213+
};
198214
}
199215

200216
/// Returns the alignment of the type associated with this vtable.
201217
#[inline]
202218
pub fn align_of(self) -> usize {
203-
self.vtable_ptr.align_of
219+
#[cfg(bootstrap)]
220+
return self.vtable_ptr.align_of;
221+
#[cfg(not(bootstrap))]
222+
// SAFETY: DynMetadata always contains a valid vtable pointer
223+
return unsafe {
224+
crate::intrinsics::vtable_align(self.vtable_ptr as *const VTable as *const ())
225+
};
204226
}
205227

206228
/// Returns the size and alignment together as a `Layout`

0 commit comments

Comments
 (0)