Skip to content

Commit 297850a

Browse files
committed
Add more checks for pointers with vtable meta
The rules for casting `*mut X<dyn A>` -> `*mut Y<dyn B>` are as follows: - If `B` has a principal - `A` must have exactly the same principal (including generics) - Auto traits of `B` must be a subset of autotraits in `A` Note that `X<_>` and `Y<_>` can be identity, or arbitrary structs with last field being the dyn type. The lifetime of the trait object itself (`dyn ... + 'a`) is not checked. This prevents a few soundness issues with `#![feature(arbitrary_self_types)]` and trait upcasting. Namely, these checks make sure that vtable is always valid for the pointee.
1 parent de4f5c2 commit 297850a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

alloc/src/boxed.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ impl dyn Error + Send {
23742374
let err: Box<dyn Error> = self;
23752375
<dyn Error>::downcast(err).map_err(|s| unsafe {
23762376
// Reapply the `Send` marker.
2377-
Box::from_raw(Box::into_raw(s) as *mut (dyn Error + Send))
2377+
mem::transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
23782378
})
23792379
}
23802380
}
@@ -2387,8 +2387,8 @@ impl dyn Error + Send + Sync {
23872387
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
23882388
let err: Box<dyn Error> = self;
23892389
<dyn Error>::downcast(err).map_err(|s| unsafe {
2390-
// Reapply the `Send + Sync` marker.
2391-
Box::from_raw(Box::into_raw(s) as *mut (dyn Error + Send + Sync))
2390+
// Reapply the `Send + Sync` markers.
2391+
mem::transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
23922392
})
23932393
}
23942394
}

0 commit comments

Comments
 (0)