Skip to content
Myr

Merge pull request #16 from johnschug/master

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix UB when accessing errors with large alignment

authored by David Tolnay · committed by GitHub · parents db8fa67, 8ebf822 · Browse files · 2 files+36 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+36 added−15 removed

  • tests:+20 added−0 removed
  • other:+16 added−15 removed

2 files+36 added−15 removed

modified:src/error.rs+16 added-15 removed
89 unmodified lines
90 let inner = Box::new(ErrorImpl {91 vtable,92 type_id,93 backtrace,94 error,94 _error: error,95 });96 let erased = mem::transmute::<Box<ErrorImpl<E>>, Box<ErrorImpl<()>>>(inner);97 let inner = ManuallyDrop::new(erased);98 Error { inner }
133 unmodified lines
232 {233 if self.is::<E>() {234 let outer = ManuallyDrop::new(self);235 unsafe {236 let error = ptr::read(&outer.inner.error as *const () as *const E);236 let error = ptr::read(outer.inner.error() as *const _ as *const E);237 let inner = ptr::read(&outer.inner);238 let erased = ManuallyDrop::into_inner(inner);239 (erased.vtable.object_drop_front)(erased);240 Ok(error)
43 unmodified lines
284 where285 E: Display + Debug + Send + Sync + 'static,286 {287 if self.is::<E>() {288 Some(unsafe { &*(&self.inner.error as *const () as *const E) })288 Some(unsafe { &*(self.inner.error() as *const _ as *const E) })289 } else {290 None291 }292 }
3 unmodified lines
296 where297 E: Display + Debug + Send + Sync + 'static,298 {299 if self.is::<E>() {300 Some(unsafe { &mut *(&mut self.inner.error as *mut () as *mut E) })300 Some(unsafe { &mut *(self.inner.error_mut() as *mut _ as *mut E) })301 } else {302 None303 }304 }
50 unmodified lines
355356struct ErrorVTable {357 object_drop: unsafe fn(Box<ErrorImpl<()>>),358 object_drop_front: unsafe fn(Box<ErrorImpl<()>>),359 object_raw: fn(*const ()) -> *const (dyn StdError + Send + Sync + 'static),360 object_mut_raw: fn(*mut ()) -> *mut (dyn StdError + Send + Sync + 'static),359 object_raw: unsafe fn(&ErrorImpl<()>) -> &(dyn StdError + Send + Sync + 'static),360 object_mut_raw: unsafe fn(&mut ErrorImpl<()>) -> &mut (dyn StdError + Send + Sync + 'static),361 object_boxed: unsafe fn(Box<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>,362}363364unsafe fn object_drop<E>(e: Box<ErrorImpl<()>>) {
10 unmodified lines
375 let unerased = mem::transmute::<Box<ErrorImpl<()>>, Box<ErrorImpl<ManuallyDrop<E>>>>(e);376 drop(unerased);377}378379fn object_raw<E>(e: *const ()) -> *const (dyn StdError + Send + Sync + 'static)379unsafe fn object_raw<E>(e: &ErrorImpl<()>) -> &(dyn StdError + Send + Sync + 'static)380where381 E: StdError + Send + Sync + 'static,382{383 e as *const E383 &(*(e as *const ErrorImpl<()> as *const ErrorImpl<E>))._error384}385386fn object_mut_raw<E>(e: *mut ()) -> *mut (dyn StdError + Send + Sync + 'static)386unsafe fn object_mut_raw<E>(e: &mut ErrorImpl<()>) -> &mut (dyn StdError + Send + Sync + 'static)387where388 E: StdError + Send + Sync + 'static,389{390 e as *mut E390 &mut (*(e as *mut ErrorImpl<()> as *mut ErrorImpl<E>))._error391}392393unsafe fn object_boxed<E>(e: Box<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>394where
7 unmodified lines
402struct ErrorImpl<E> {403 vtable: &'static ErrorVTable,404 type_id: TypeId,405 backtrace: Option<Backtrace>,406 // NOTE: Don't use directly. Use only through vtable. Erased type may have different alignment.406 error: E,407 _error: E,408}409410#[repr(transparent)]411struct MessageError<M>(M);
48 unmodified lines
460}461462impl ErrorImpl<()> {463 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {463 unsafe { &*(self.vtable.object_raw)(&self.error) }464 unsafe { &*(self.vtable.object_raw)(self) }465 }466467 fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {467 unsafe { &mut *(self.vtable.object_mut_raw)(&mut self.error) }468 unsafe { &mut *(self.vtable.object_mut_raw)(self) }469 }470471 #[cfg(backtrace)]472 fn backtrace(&self) -> &Backtrace {
59 unmodified lines
532 Some(self.erase().backtrace())533 }534535 fn source(&self) -> Option<&(dyn StdError + 'static)> {535 self.error.source()536 self.erase().error().source()537 }538}539540impl<E> Debug for ErrorImpl<E>
9 unmodified lines
550where551 E: Display,552{553 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {553 Display::fmt(&self.error, formatter)554 Display::fmt(&self.erase().error(), formatter)555 }556}557558impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {
42 unmodified lines
modified:tests/test_downcast.rs+20 added
2 unmodified lines
34use self::common::*;5use self::drop::DetectDrop;6use anyhow::Error;7use std::error::Error as StdError;8use std::fmt::{self, Display};9use std::io;10use std::sync::atomic::AtomicBool;11use std::sync::atomic::Ordering::SeqCst;12use std::sync::Arc;
64 unmodified lines
77 let error = Error::new(DetectDrop::new(&has_dropped));78 drop(error.downcast::<DetectDrop>().unwrap());79 assert!(has_dropped.load(SeqCst));80}8182#[test]83fn test_large_alignment() {84 #[repr(align(64))]85 #[derive(Debug)]86 struct LargeAlignedError(&'static str);8788 impl Display for LargeAlignedError {89 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {90 f.write_str(self.0)91 }92 }9394 impl StdError for LargeAlignedError {}9596 let error = Error::new(LargeAlignedError("oh no!"));97 assert_eq!("oh no!", error.downcast_ref::<LargeAlignedError>().unwrap().0);98}