Fix crash in exception_wrapper::get_exception<>
authorYedidya Feldblum <yfeldblum@fb.com>
Thu, 7 Dec 2017 20:38:59 +0000 (12:38 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 7 Dec 2017 20:51:13 +0000 (12:51 -0800)
commit39018ad5c77c53156cb7cd672c05a19fb75a6af7
tree00edf8a563e313db4a4400fd49dd5b1b00edcb1d
parenta335700fba3a818043d53de320711dc97b680894
Fix crash in exception_wrapper::get_exception<>

Summary:
[Folly] Fix crash in `exception_wrapper::get_exception<>`.

When the contained object is unthrown and does not inherit `std::exception`, `get_exception` templated over a type that does not inherit `std::exception` must throw and catch internally and may then return a pointer to the internally thrown object, which has since been deallocated.

Attempting to dereference that pointer is undefined behavior is correctly caught by ASAN as heap-use-after-free.

Fix it by storing objects not inheriting `std::exception` using only the `std::exception_ptr` representation.

As a downside, we no longer get the small-object optimization or the non-throwing optimization for objects which do not inherit `std::exception`. But this is not likely to be terribly concerning in practice.

Reviewed By: ericniebler

Differential Revision: D6504911

fbshipit-source-id: 0065de911733b5cab87be55e7e4e47f0a9c09140
folly/ExceptionWrapper-inl.h
folly/ExceptionWrapper.h
folly/test/ExceptionWrapperTest.cpp