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
Provenance
- Commit
eeaf2ef- Tree
ed476ef- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
db8fa67- Parent 2
8ebf822
2 files changed+36 added−15 removed
modified:src/error.rs+16 added-15 removed
89 unmodified lines90 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 lines232 {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 lines284 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 lines296 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 lines355 356 struct 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 }363 364 unsafe fn object_drop<E>(e: Box<ErrorImpl<()>>) {10 unmodified lines375 let unerased = mem::transmute::<Box<ErrorImpl<()>>, Box<ErrorImpl<ManuallyDrop<E>>>>(e);376 drop(unerased);377 }378 379-fn object_raw<E>(e: *const ()) -> *const (dyn StdError + Send + Sync + 'static)379+unsafe fn object_raw<E>(e: &ErrorImpl<()>) -> &(dyn StdError + Send + Sync + 'static)380 where381 E: StdError + Send + Sync + 'static,382 {383- e as *const E383+ &(*(e as *const ErrorImpl<()> as *const ErrorImpl<E>))._error384 }385 386-fn object_mut_raw<E>(e: *mut ()) -> *mut (dyn StdError + Send + Sync + 'static)386+unsafe fn object_mut_raw<E>(e: &mut ErrorImpl<()>) -> &mut (dyn StdError + Send + Sync + 'static)387 where388 E: StdError + Send + Sync + 'static,389 {390- e as *mut E390+ &mut (*(e as *mut ErrorImpl<()> as *mut ErrorImpl<E>))._error391 }392 393 unsafe fn object_boxed<E>(e: Box<ErrorImpl<()>>) -> Box<dyn StdError + Send + Sync + 'static>394 where7 unmodified lines402 struct 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 }409 410 #[repr(transparent)]411 struct MessageError<M>(M);48 unmodified lines460 }461 462 impl 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 }466 467 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 }470 471 #[cfg(backtrace)]472 fn backtrace(&self) -> &Backtrace {59 unmodified lines532 Some(self.erase().backtrace())533 }534 535 fn source(&self) -> Option<&(dyn StdError + 'static)> {535- self.error.source()536+ self.erase().error().source()537 }538 }539 540 impl<E> Debug for ErrorImpl<E>9 unmodified lines550 where551 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 }557 558 impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {42 unmodified linesmodified:tests/test_downcast.rs+20 added
2 unmodified lines3 4 use self::common::*;5 use self::drop::DetectDrop;6 use anyhow::Error;7+use std::error::Error as StdError;8+use std::fmt::{self, Display};9 use std::io;10 use std::sync::atomic::AtomicBool;11 use std::sync::atomic::Ordering::SeqCst;12 use std::sync::Arc;64 unmodified lines77 let error = Error::new(DetectDrop::new(&has_dropped));78 drop(error.downcast::<DetectDrop>().unwrap());79 assert!(has_dropped.load(SeqCst));80 }81+82+#[test]83+fn test_large_alignment() {84+ #[repr(align(64))]85+ #[derive(Debug)]86+ struct LargeAlignedError(&'static str);87+88+ impl Display for LargeAlignedError {89+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {90+ f.write_str(self.0)91+ }92+ }93+94+ impl StdError for LargeAlignedError {}95+96+ let error = Error::new(LargeAlignedError("oh no!"));97+ assert_eq!("oh no!", error.downcast_ref::<LargeAlignedError>().unwrap().0);98+}