From 3e254a6ece452fcaee6da4a7286245d0a9ef7c45 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Mon, 8 May 2017 01:17:50 +0000 Subject: [PATCH] [libc++] Implement exception_ptr on Windows Summary: This patch implements exception_ptr on Windows using the `__ExceptionPtrFoo` functions provided by MSVC. The `__ExceptionPtrFoo` functions are defined inside the C++ standard library, `msvcprt`, which is unfortunate because it requires libc++ to link to the MSVC STL. However this doesn't seem to cause any immediate problems. However to be safe I kept all usages within the libc++ dylib so that user programs wouldn't have to link to MSVCPRT as well. Note there are still 2 outstanding exception_ptr/nested_exception test failures. * `current_exception.pass.cpp` needs to be rewritten for the Windows exception_ptr semantics which copy the exception every time. * `rethrow_if_nested.pass.cpp` need investigation. It hits a stack overflow, likely from recursion. This patch also gets most of the `` tests passing as well. Reviewers: mclow.lists, compnerd, bcraig, rmaprath, majnemer, BillyONeal, STL_MSFT Subscribers: mgorny, cfe-commits Differential Revision: https://reviews.llvm.org/D32927 llvm-svn: 302393 --- libcxx/include/exception | 57 ++++++++++++- libcxx/lib/CMakeLists.txt | 1 + libcxx/src/exception.cpp | 2 +- .../src/support/runtime/exception_pointer_msvc.ipp | 94 ++++++++++++++++++++++ .../futures/futures.promise/set_exception.pass.cpp | 4 - .../set_exception_at_thread_exit.pass.cpp | 3 - .../except.nested/assign.pass.cpp | 3 - .../except.nested/ctor_copy.pass.cpp | 3 - .../except.nested/ctor_default.pass.cpp | 3 - .../except.nested/rethrow_if_nested.pass.cpp | 5 +- .../except.nested/rethrow_nested.pass.cpp | 3 - .../except.nested/throw_with_nested.pass.cpp | 3 - .../propagation/current_exception.pass.cpp | 3 +- .../propagation/exception_ptr.pass.cpp | 3 - .../propagation/make_exception_ptr.pass.cpp | 9 ++- .../propagation/rethrow_exception.pass.cpp | 9 ++- 16 files changed, 167 insertions(+), 38 deletions(-) create mode 100644 libcxx/src/support/runtime/exception_pointer_msvc.ipp diff --git a/libcxx/include/exception b/libcxx/include/exception index 9999ca0..ca2eaf5 100644 --- a/libcxx/include/exception +++ b/libcxx/include/exception @@ -134,23 +134,26 @@ class _LIBCPP_TYPE_VIS exception_ptr; _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT; _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr); +#ifndef _LIBCPP_ABI_MICROSOFT + class _LIBCPP_TYPE_VIS exception_ptr { void* __ptr_; public: _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {} _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {} + exception_ptr(const exception_ptr&) _NOEXCEPT; exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; ~exception_ptr() _NOEXCEPT; - _LIBCPP_INLINE_VISIBILITY - _LIBCPP_EXPLICIT - operator bool() const _NOEXCEPT {return __ptr_ != nullptr;} + _LIBCPP_INLINE_VISIBILITY _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT + {return __ptr_ != nullptr;} friend _LIBCPP_INLINE_VISIBILITY bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {return __x.__ptr_ == __y.__ptr_;} + friend _LIBCPP_INLINE_VISIBILITY bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {return !(__x == __y);} @@ -178,6 +181,54 @@ make_exception_ptr(_Ep __e) _NOEXCEPT #endif } +#else // _LIBCPP_ABI_MICROSOFT + +class _LIBCPP_TYPE_VIS exception_ptr +{ +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-private-field" +#endif + void* __ptr1_; + void* __ptr2_; +#if defined(__clang__) +#pragma clang diagnostic pop +#endif +public: + exception_ptr() _NOEXCEPT; + exception_ptr(nullptr_t) _NOEXCEPT; + exception_ptr(const exception_ptr& __other) _NOEXCEPT; + exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT; + exception_ptr& operator=(nullptr_t) _NOEXCEPT; + ~exception_ptr() _NOEXCEPT; + _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT; +}; + +_LIBCPP_FUNC_VIS +bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT; + +inline _LIBCPP_INLINE_VISIBILITY +bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT + {return !(__x == __y);} + +_LIBCPP_FUNC_VIS void swap(exception_ptr&, exception_ptr&) _NOEXCEPT; + +_LIBCPP_FUNC_VIS exception_ptr __copy_exception_ptr(void *__except, const void* __ptr); +_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT; +_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr p); + +// This is a built-in template function which automagically extracts the required +// information. +template void *__GetExceptionInfo(_E); + +template +exception_ptr +make_exception_ptr(_Ep __e) _NOEXCEPT +{ + return __copy_exception_ptr(_VSTD::addressof(__e), __GetExceptionInfo(__e)); +} + +#endif // _LIBCPP_ABI_MICROSOFT // nested_exception class _LIBCPP_EXCEPTION_ABI nested_exception diff --git a/libcxx/lib/CMakeLists.txt b/libcxx/lib/CMakeLists.txt index 15dc05c..6161838 100644 --- a/libcxx/lib/CMakeLists.txt +++ b/libcxx/lib/CMakeLists.txt @@ -121,6 +121,7 @@ if (LIBCXX_TARGETING_MSVC) add_library_flags(ucrt${LIB_SUFFIX}) # Universal C runtime add_library_flags(vcruntime${LIB_SUFFIX}) # C++ runtime add_library_flags(msvcrt${LIB_SUFFIX}) # C runtime startup files + add_library_flags(msvcprt${LIB_SUFFIX}) # C++ standard library. Required for exception_ptr internals. # Required for standards-complaint wide character formatting functions # (e.g. `printfw`/`scanfw`) add_library_flags(iso_stdio_wide_specifiers) diff --git a/libcxx/src/exception.cpp b/libcxx/src/exception.cpp index 0b502cd..4359d12 100644 --- a/libcxx/src/exception.cpp +++ b/libcxx/src/exception.cpp @@ -20,7 +20,7 @@ #if defined(_LIBCPP_ABI_MICROSOFT) #include "support/runtime/exception_msvc.ipp" -#include "support/runtime/exception_pointer_unimplemented.ipp" +#include "support/runtime/exception_pointer_msvc.ipp" #elif defined(_LIBCPPABI_VERSION) #include "support/runtime/exception_libcxxabi.ipp" #include "support/runtime/exception_pointer_cxxabi.ipp" diff --git a/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/libcxx/src/support/runtime/exception_pointer_msvc.ipp new file mode 100644 index 0000000..a8cd0e8 --- /dev/null +++ b/libcxx/src/support/runtime/exception_pointer_msvc.ipp @@ -0,0 +1,94 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include +#include + +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void*, + _In_ const void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrAssign(_Inout_ void*, _In_ const void*); +_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL +__ExceptionPtrCompare(_In_ const void*, _In_ const void*); +_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL +__ExceptionPtrToBool(_In_ const void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void*, + _Inout_ void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrCurrentException(_Out_ void*); +[[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrRethrow(_In_ const void*); +_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL +__ExceptionPtrCopyException(_Inout_ void*, _In_ const void*, _In_ const void*); + +namespace std { + +exception_ptr::exception_ptr() _NOEXCEPT { __ExceptionPtrCreate(this); } +exception_ptr::exception_ptr(nullptr_t) _NOEXCEPT { __ExceptionPtrCreate(this); } + +exception_ptr::exception_ptr(const exception_ptr& __other) _NOEXCEPT { + __ExceptionPtrCopy(this, &__other); +} +exception_ptr& exception_ptr::operator=(const exception_ptr& __other) _NOEXCEPT { + __ExceptionPtrAssign(this, &__other); + return *this; +} + +exception_ptr& exception_ptr::operator=(nullptr_t) _NOEXCEPT { + exception_ptr dummy; + __ExceptionPtrAssign(this, &dummy); + return *this; +} + +exception_ptr::~exception_ptr() _NOEXCEPT { __ExceptionPtrDestroy(this); } + +exception_ptr::operator bool() const _NOEXCEPT { + return __ExceptionPtrToBool(this); +} + +bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT { + return __ExceptionPtrCompare(&__x, &__y); +} + + +void swap(exception_ptr& lhs, exception_ptr& rhs) _NOEXCEPT { + __ExceptionPtrSwap(&rhs, &lhs); +} + +exception_ptr __copy_exception_ptr(void* __except, const void* __ptr) { + exception_ptr __ret = nullptr; + if (__ptr) + __ExceptionPtrCopyException(&__ret, __except, __ptr); + return __ret; +} + +exception_ptr current_exception() _NOEXCEPT { + exception_ptr __ret; + __ExceptionPtrCurrentException(&__ret); + return __ret; +} + +_LIBCPP_NORETURN +void rethrow_exception(exception_ptr p) { __ExceptionPtrRethrow(&p); } + +nested_exception::nested_exception() _NOEXCEPT : __ptr_(current_exception()) {} + +nested_exception::~nested_exception() _NOEXCEPT {} + +_LIBCPP_NORETURN +void nested_exception::rethrow_nested() const { + if (__ptr_ == nullptr) + terminate(); + rethrow_exception(__ptr_); +} + +} // namespace std diff --git a/libcxx/test/libcxx/thread/futures/futures.promise/set_exception.pass.cpp b/libcxx/test/libcxx/thread/futures/futures.promise/set_exception.pass.cpp index 12c0a3d..9efa597 100644 --- a/libcxx/test/libcxx/thread/futures/futures.promise/set_exception.pass.cpp +++ b/libcxx/test/libcxx/thread/futures/futures.promise/set_exception.pass.cpp @@ -6,10 +6,6 @@ // Source Licenses. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// -// - -// This test depends on std::exception_ptr which has not yet been implemented. -// XFAIL: LIBCXX-WINDOWS-FIXME // UNSUPPORTED: libcpp-no-exceptions // UNSUPPORTED: libcpp-has-no-threads diff --git a/libcxx/test/libcxx/thread/futures/futures.promise/set_exception_at_thread_exit.pass.cpp b/libcxx/test/libcxx/thread/futures/futures.promise/set_exception_at_thread_exit.pass.cpp index 713f471..dca4933 100644 --- a/libcxx/test/libcxx/thread/futures/futures.promise/set_exception_at_thread_exit.pass.cpp +++ b/libcxx/test/libcxx/thread/futures/futures.promise/set_exception_at_thread_exit.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// This test depends on std::exception_ptr which has not yet been implemented. -// XFAIL: LIBCXX-WINDOWS-FIXME - // UNSUPPORTED: libcpp-no-exceptions // UNSUPPORTED: libcpp-has-no-threads // UNSUPPORTED: c++98, c++03 diff --git a/libcxx/test/std/language.support/support.exception/except.nested/assign.pass.cpp b/libcxx/test/std/language.support/support.exception/except.nested/assign.pass.cpp index ec5575d..6338c8a 100644 --- a/libcxx/test/std/language.support/support.exception/except.nested/assign.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/except.nested/assign.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // // class nested_exception; diff --git a/libcxx/test/std/language.support/support.exception/except.nested/ctor_copy.pass.cpp b/libcxx/test/std/language.support/support.exception/except.nested/ctor_copy.pass.cpp index 92134c9..4cbdbb2 100644 --- a/libcxx/test/std/language.support/support.exception/except.nested/ctor_copy.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/except.nested/ctor_copy.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // // class nested_exception; diff --git a/libcxx/test/std/language.support/support.exception/except.nested/ctor_default.pass.cpp b/libcxx/test/std/language.support/support.exception/except.nested/ctor_default.pass.cpp index 8f8503b..18ca996 100644 --- a/libcxx/test/std/language.support/support.exception/except.nested/ctor_default.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/except.nested/ctor_default.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // // class nested_exception; diff --git a/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp b/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp index fe9cd60..88e2253 100644 --- a/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/except.nested/rethrow_if_nested.pass.cpp @@ -7,10 +7,11 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows +// UNSUPPORTED: libcpp-no-exceptions + +// This test fails due to a stack overflow // XFAIL: LIBCXX-WINDOWS-FIXME -// UNSUPPORTED: libcpp-no-exceptions // // class nested_exception; diff --git a/libcxx/test/std/language.support/support.exception/except.nested/rethrow_nested.pass.cpp b/libcxx/test/std/language.support/support.exception/except.nested/rethrow_nested.pass.cpp index 73d02da..d511a72 100644 --- a/libcxx/test/std/language.support/support.exception/except.nested/rethrow_nested.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/except.nested/rethrow_nested.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // UNSUPPORTED: libcpp-no-exceptions // diff --git a/libcxx/test/std/language.support/support.exception/except.nested/throw_with_nested.pass.cpp b/libcxx/test/std/language.support/support.exception/except.nested/throw_with_nested.pass.cpp index 6353afc..6a9f25c 100644 --- a/libcxx/test/std/language.support/support.exception/except.nested/throw_with_nested.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/except.nested/throw_with_nested.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // UNSUPPORTED: libcpp-no-exceptions // diff --git a/libcxx/test/std/language.support/support.exception/propagation/current_exception.pass.cpp b/libcxx/test/std/language.support/support.exception/propagation/current_exception.pass.cpp index 2046997..c33d64d 100644 --- a/libcxx/test/std/language.support/support.exception/propagation/current_exception.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/propagation/current_exception.pass.cpp @@ -7,7 +7,8 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows +// This test needs to be rewritten for the Windows exception_ptr semantics +// which copy the exception each time the exception_ptr is copied. // XFAIL: LIBCXX-WINDOWS-FIXME // UNSUPPORTED: libcpp-no-exceptions diff --git a/libcxx/test/std/language.support/support.exception/propagation/exception_ptr.pass.cpp b/libcxx/test/std/language.support/support.exception/propagation/exception_ptr.pass.cpp index 9621a73..3aa8dcf 100644 --- a/libcxx/test/std/language.support/support.exception/propagation/exception_ptr.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/propagation/exception_ptr.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // // typedef unspecified exception_ptr; diff --git a/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.pass.cpp b/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.pass.cpp index 4980c65..35821d9 100644 --- a/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/propagation/make_exception_ptr.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // UNSUPPORTED: libcpp-no-exceptions // @@ -41,7 +38,12 @@ int main() } catch (const A& a) { +#ifndef _LIBCPP_ABI_MICROSOFT assert(A::constructed == 1); +#else + // On Windows exception_ptr copies the exception + assert(A::constructed == 2); +#endif assert(p != nullptr); p = nullptr; assert(p == nullptr); @@ -50,4 +52,5 @@ int main() } assert(A::constructed == 0); } + assert(A::constructed == 0); } diff --git a/libcxx/test/std/language.support/support.exception/propagation/rethrow_exception.pass.cpp b/libcxx/test/std/language.support/support.exception/propagation/rethrow_exception.pass.cpp index 7d27eee..37ffb5b 100644 --- a/libcxx/test/std/language.support/support.exception/propagation/rethrow_exception.pass.cpp +++ b/libcxx/test/std/language.support/support.exception/propagation/rethrow_exception.pass.cpp @@ -7,9 +7,6 @@ // //===----------------------------------------------------------------------===// -// exception_ptr has not been implemented on Windows -// XFAIL: LIBCXX-WINDOWS-FIXME - // UNSUPPORTED: libcpp-no-exceptions // @@ -49,7 +46,12 @@ int main() } catch (const A& a) { +#ifndef _LIBCPP_ABI_MICROSOFT assert(A::constructed == 1); +#else + // On Windows the exception_ptr copies the exception + assert(A::constructed == 2); +#endif assert(p != nullptr); p = nullptr; assert(p == nullptr); @@ -58,4 +60,5 @@ int main() } assert(A::constructed == 0); } + assert(A::constructed == 0); } -- 2.7.4