[libcxx-commits] [libcxx] [libc++] Add MSVC's implementation of `exception_ptr` for Windows (PR #94977)

Petr Hosek via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jun 27 12:08:09 PDT 2026


https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/94977

>From 8b41047c72465c3d9678163a644184239728b888 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at microsoft.com>
Date: Mon, 10 Jun 2024 04:39:43 -0700
Subject: [PATCH 01/10] microsoft/STL's `excptptr.cpp`, copied verbatim.

>From our `main` branch as of 2024-06-10:

https://github.com/microsoft/STL/blob/e36ee6c2b9bc6f5b1f70776c18cf5d3a93a69798/stl/src/excptptr.cpp
---
 libcxx/src/support/win32/excptptr.cpp | 533 ++++++++++++++++++++++++++
 1 file changed, 533 insertions(+)
 create mode 100644 libcxx/src/support/win32/excptptr.cpp

diff --git a/libcxx/src/support/win32/excptptr.cpp b/libcxx/src/support/win32/excptptr.cpp
new file mode 100644
index 0000000000000..830cb65a940d5
--- /dev/null
+++ b/libcxx/src/support/win32/excptptr.cpp
@@ -0,0 +1,533 @@
+// Copyright (c) Microsoft Corporation.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+// This implementation communicates with the EH runtime though vcruntime's per-thread-data structure; see
+// _pCurrentException in <trnsctrl.h>.
+//
+// As a result, normal EH runtime services (such as noexcept functions) are safe to use in this file.
+
+#ifndef _VCRT_ALLOW_INTERNALS
+#define _VCRT_ALLOW_INTERNALS
+#endif // !defined(_VCRT_ALLOW_INTERNALS)
+
+#include <Unknwn.h>
+#include <cstdlib> // for abort
+#include <cstring> // for memcpy
+#include <eh.h>
+#include <ehdata.h>
+#include <exception>
+#include <internal_shared.h>
+#include <malloc.h>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <trnsctrl.h>
+#include <xcall_once.h>
+
+#include <Windows.h>
+
+// Pre-V4 managed exception code
+#define MANAGED_EXCEPTION_CODE 0XE0434F4D
+
+// V4 and later managed exception code
+#define MANAGED_EXCEPTION_CODE_V4 0XE0434352
+
+extern "C" _CRTIMP2 void* __cdecl __AdjustPointer(void*, const PMD&); // defined in frame.cpp
+
+using namespace std;
+
+namespace {
+#if defined(_M_CEE_PURE)
+    template <class _Ty>
+    _Ty& _Immortalize() { // return a reference to an object that will live forever
+        /* MAGIC */ static _Immortalizer_impl<_Ty> _Static;
+        return reinterpret_cast<_Ty&>(_Static._Storage);
+    }
+#elif !defined(_M_CEE)
+    template <class _Ty>
+    struct _Constexpr_excptptr_immortalize_impl {
+        union {
+            _Ty _Storage;
+        };
+
+        constexpr _Constexpr_excptptr_immortalize_impl() noexcept : _Storage{} {}
+
+        _Constexpr_excptptr_immortalize_impl(const _Constexpr_excptptr_immortalize_impl&)            = delete;
+        _Constexpr_excptptr_immortalize_impl& operator=(const _Constexpr_excptptr_immortalize_impl&) = delete;
+
+        _MSVC_NOOP_DTOR ~_Constexpr_excptptr_immortalize_impl() {
+            // do nothing, allowing _Ty to be used during shutdown
+        }
+    };
+
+    template <class _Ty>
+    _Constexpr_excptptr_immortalize_impl<_Ty> _Immortalize_impl;
+
+    template <class _Ty>
+    [[nodiscard]] _Ty& _Immortalize() noexcept {
+        return _Immortalize_impl<_Ty>._Storage;
+    }
+#else // ^^^ !defined(_M_CEE) / defined(_M_CEE), TRANSITION, VSO-1153256 vvv
+    template <class _Ty>
+    _Ty& _Immortalize() { // return a reference to an object that will live forever
+        static once_flag _Flag;
+        alignas(_Ty) static unsigned char _Storage[sizeof(_Ty)];
+        call_once(_Flag, [&_Storage] { ::new (static_cast<void*>(&_Storage)) _Ty(); });
+        return reinterpret_cast<_Ty&>(_Storage);
+    }
+#endif // ^^^ !defined(_M_CEE_PURE) && defined(_M_CEE), TRANSITION, VSO-1153256 ^^^
+
+    void _PopulateCppExceptionRecord(
+        _EXCEPTION_RECORD& _Record, const void* const _PExcept, ThrowInfo* _PThrow) noexcept {
+        _Record.ExceptionCode           = EH_EXCEPTION_NUMBER;
+        _Record.ExceptionFlags          = EXCEPTION_NONCONTINUABLE;
+        _Record.ExceptionRecord         = nullptr; // no SEH to chain
+        _Record.ExceptionAddress        = nullptr; // Address of exception. Will be overwritten by OS
+        _Record.NumberParameters        = EH_EXCEPTION_PARAMETERS;
+        _Record.ExceptionInformation[0] = EH_MAGIC_NUMBER1; // params.magicNumber
+        _Record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(_PExcept); // params.pExceptionObject
+
+        if (_PThrow && (_PThrow->attributes & TI_IsWinRT)) {
+            // The pointer to the ExceptionInfo structure is stored sizeof(void*) in front of each WinRT Exception Info.
+            const auto _PWei = (*static_cast<WINRTEXCEPTIONINFO** const*>(_PExcept))[-1];
+            _PThrow          = _PWei->throwInfo;
+        }
+
+        _Record.ExceptionInformation[2] = reinterpret_cast<ULONG_PTR>(_PThrow); // params.pThrowInfo
+
+#if _EH_RELATIVE_TYPEINFO
+        void* _ThrowImageBase =
+            _PThrow ? RtlPcToFileHeader(const_cast<void*>(static_cast<const void*>(_PThrow)), &_ThrowImageBase)
+                    : nullptr;
+        _Record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(_ThrowImageBase); // params.pThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+
+        // If the throw info indicates this throw is from a pure region,
+        // set the magic number to the Pure one, so only a pure-region
+        // catch will see it.
+        //
+        // Also use the Pure magic number on 64-bit platforms if we were unable to
+        // determine an image base, since that was the old way to determine
+        // a pure throw, before the TI_IsPure bit was added to the FuncInfo
+        // attributes field.
+        if (_PThrow
+            && ((_PThrow->attributes & TI_IsPure)
+#if _EH_RELATIVE_TYPEINFO
+                || !_ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+                )) {
+            _Record.ExceptionInformation[0] = EH_PURE_MAGIC_NUMBER1;
+        }
+    }
+
+    void _CopyExceptionRecord(_EXCEPTION_RECORD& _Dest, const _EXCEPTION_RECORD& _Src) noexcept {
+        _Dest.ExceptionCode = _Src.ExceptionCode;
+        // we force EXCEPTION_NONCONTINUABLE because rethrow_exception is [[noreturn]]
+        _Dest.ExceptionFlags   = _Src.ExceptionFlags | EXCEPTION_NONCONTINUABLE;
+        _Dest.ExceptionRecord  = nullptr; // We don't chain SEH exceptions
+        _Dest.ExceptionAddress = nullptr; // Useless field to copy. It will be overwritten by RaiseException()
+        const auto _Parameters = _Src.NumberParameters;
+        _Dest.NumberParameters = _Parameters;
+
+        // copy the number of parameters in use
+        constexpr auto _Max_parameters = static_cast<DWORD>(EXCEPTION_MAXIMUM_PARAMETERS);
+        const auto _In_use             = (_STD min)(_Parameters, _Max_parameters);
+        _CSTD memcpy(_Dest.ExceptionInformation, _Src.ExceptionInformation, _In_use * sizeof(ULONG_PTR));
+        _CSTD memset(&_Dest.ExceptionInformation[_In_use], 0, (_Max_parameters - _In_use) * sizeof(ULONG_PTR));
+    }
+
+    void _CopyExceptionObject(void* _Dest, const void* _Src, const CatchableType* const _PType
+#if _EH_RELATIVE_TYPEINFO
+        ,
+        const uintptr_t _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+    ) {
+        // copy an object of type denoted by *_PType from _Src to _Dest; throws whatever the copy ctor of the type
+        // denoted by *_PType throws
+        if ((_PType->properties & CT_IsSimpleType) || _PType->copyFunction == 0) {
+            memcpy(_Dest, _Src, _PType->sizeOrOffset);
+
+            if (_PType->properties & CT_IsWinRTHandle) {
+                const auto _PUnknown = *static_cast<IUnknown* const*>(_Src);
+                if (_PUnknown) {
+                    _PUnknown->AddRef();
+                }
+            }
+            return;
+        }
+
+#if _EH_RELATIVE_TYPEINFO
+        const auto _CopyFunc = reinterpret_cast<void*>(_ThrowImageBase + _PType->copyFunction);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _CopyFunc = _PType->copyFunction;
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        const auto _Adjusted = __AdjustPointer(const_cast<void*>(_Src), _PType->thisDisplacement);
+        if (_PType->properties & CT_HasVirtualBase) {
+#ifdef _M_CEE_PURE
+            reinterpret_cast<void(__clrcall*)(void*, void*, int)>(_CopyFunc)(_Dest, _Adjusted, 1);
+#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
+            _CallMemberFunction2(_Dest, _CopyFunc, _Adjusted, 1);
+#endif // ^^^ !defined(_M_CEE_PURE) ^^^
+        } else {
+#ifdef _M_CEE_PURE
+            reinterpret_cast<void(__clrcall*)(void*, void*)>(_CopyFunc)(_Dest, _Adjusted);
+#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
+            _CallMemberFunction1(_Dest, _CopyFunc, _Adjusted);
+#endif // ^^^ !defined(_M_CEE_PURE) ^^^
+        }
+    }
+} // unnamed namespace
+
+// All exception_ptr implementations are out-of-line because <memory> depends on <exception>,
+// which means <exception> cannot include <memory> -- and shared_ptr is defined in <memory>.
+// To workaround this, we created a dummy class exception_ptr, which is structurally identical to shared_ptr.
+
+_STD_BEGIN
+struct _Exception_ptr_access {
+    template <class _Ty, class _Ty2>
+    static void _Set_ptr_rep(_Ptr_base<_Ty>& _This, _Ty2* _Px, _Ref_count_base* _Rx) noexcept {
+        _This._Ptr = _Px;
+        _This._Rep = _Rx;
+    }
+};
+_STD_END
+
+static_assert(sizeof(exception_ptr) == sizeof(shared_ptr<const _EXCEPTION_RECORD>)
+                  && alignof(exception_ptr) == alignof(shared_ptr<const _EXCEPTION_RECORD>),
+    "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
+
+namespace {
+    template <class _StaticEx>
+    class _ExceptionPtr_static final : public _Ref_count_base {
+        // reference count control block for special "never allocates" exceptions like the bad_alloc or bad_exception
+        // exception_ptrs
+    private:
+        void _Destroy() noexcept override {
+            // intentionally does nothing
+        }
+
+        void _Delete_this() noexcept override {
+            // intentionally does nothing
+        }
+
+    public:
+        // constexpr, TRANSITION P1064
+        explicit _ExceptionPtr_static() noexcept : _Ref_count_base() {
+            _PopulateCppExceptionRecord(_ExRecord, &_Ex, static_cast<ThrowInfo*>(__GetExceptionInfo(_Ex)));
+        }
+
+        static shared_ptr<const _EXCEPTION_RECORD> _Get() noexcept {
+            auto& _Instance = _Immortalize<_ExceptionPtr_static>();
+            shared_ptr<const _EXCEPTION_RECORD> _Ret;
+            _Instance._Incref();
+            _Exception_ptr_access::_Set_ptr_rep(_Ret, &_Instance._ExRecord, &_Instance);
+            return _Ret;
+        }
+
+        _EXCEPTION_RECORD _ExRecord;
+        _StaticEx _Ex;
+    };
+
+    class _ExceptionPtr_normal final : public _Ref_count_base {
+        // reference count control block for exception_ptrs; the exception object is stored at
+        // reinterpret_cast<unsigned char*>(this) + sizeof(_ExceptionPtr_normal)
+    private:
+        void _Destroy() noexcept override {
+            // call the destructor for a stored pure or native C++ exception if necessary
+            const auto& _CppEhRecord = reinterpret_cast<EHExceptionRecord&>(_ExRecord);
+
+            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppEhRecord)) {
+                return;
+            }
+
+            const auto _PThrow = _CppEhRecord.params.pThrowInfo;
+            if (!_PThrow) {
+                // No ThrowInfo exists. If this was a C++ exception, something must have corrupted it.
+                _CSTD abort();
+            }
+
+            if (!_CppEhRecord.params.pExceptionObject) {
+                return;
+            }
+
+#if _EH_RELATIVE_TYPEINFO
+            const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_CppEhRecord.params.pThrowImageBase);
+            const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
+                static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
+            const auto _PType = reinterpret_cast<CatchableType*>(
+                static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+            const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+            if (_PThrow->pmfnUnwind) {
+                // The exception was a user defined type with a nontrivial destructor, call it
+#if defined(_M_CEE_PURE)
+                reinterpret_cast<void(__clrcall*)(void*)>(_PThrow->pmfnUnwind)(_CppEhRecord.params.pExceptionObject);
+#elif _EH_RELATIVE_TYPEINFO
+                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject,
+                    reinterpret_cast<void*>(_PThrow->pmfnUnwind + _ThrowImageBase));
+#else // ^^^ _EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) / !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) vvv
+                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject, _PThrow->pmfnUnwind);
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) ^^^
+            } else if (_PType->properties & CT_IsWinRTHandle) {
+                const auto _PUnknown = *static_cast<IUnknown* const*>(_CppEhRecord.params.pExceptionObject);
+                if (_PUnknown) {
+                    _PUnknown->Release();
+                }
+            }
+        }
+
+        void _Delete_this() noexcept override {
+            free(this);
+        }
+
+    public:
+        explicit _ExceptionPtr_normal(const _EXCEPTION_RECORD& _Record) noexcept : _Ref_count_base() {
+            _CopyExceptionRecord(_ExRecord, _Record);
+        }
+
+        _EXCEPTION_RECORD _ExRecord;
+        void* _Unused_alignment_padding{};
+    };
+
+    // We aren't using alignas because this file might be compiled with _M_CEE_PURE
+    static_assert(sizeof(_ExceptionPtr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
+        "Exception in exception_ptr would be constructed with the wrong alignment");
+
+    void _Assign_seh_exception_ptr_from_record(
+        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const _EXCEPTION_RECORD& _Record, void* const _RxRaw) noexcept {
+        // in the memory _RxRaw, constructs a reference count control block for a SEH exception denoted by _Record
+        // if _RxRaw is nullptr, assigns bad_alloc instead
+        if (!_RxRaw) {
+            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
+            return;
+        }
+
+        const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(_Record);
+        _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
+    }
+
+    void _Assign_cpp_exception_ptr_from_record(
+        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const EHExceptionRecord& _Record) noexcept {
+        // construct a reference count control block for the C++ exception recorded by _Record, and bind it to _Dest
+        // if allocating memory for the reference count control block fails, sets _Dest to bad_alloc
+        // if copying the exception object referred to _Record throws, constructs a reference count control block for
+        //      that exception instead
+        // if copying the exception object thrown by copying the original exception object throws, sets _Dest to
+        //      bad_exception
+        const auto _PThrow = _Record.params.pThrowInfo;
+#if _EH_RELATIVE_TYPEINFO
+        const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_Record.params.pThrowImageBase);
+        const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
+            static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
+        const auto _PType = reinterpret_cast<CatchableType*>(
+            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        const auto _ExceptionObjectSize = static_cast<size_t>(_PType->sizeOrOffset);
+        const auto _AllocSize           = sizeof(_ExceptionPtr_normal) + _ExceptionObjectSize;
+        _Analysis_assume_(_AllocSize >= sizeof(_ExceptionPtr_normal));
+        auto _RxRaw = malloc(_AllocSize);
+        if (!_RxRaw) {
+            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
+            return;
+        }
+
+        try {
+            _CopyExceptionObject(static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _Record.params.pExceptionObject, _PType
+#if _EH_RELATIVE_TYPEINFO
+                ,
+                _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+            );
+
+            const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_Record));
+            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
+                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
+            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
+        } catch (...) { // copying the exception object threw an exception
+            const auto& _InnerRecord = *_pCurrentException; // exception thrown by the original exception's copy ctor
+            if (_InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE
+                || _InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+                // we don't support managed exceptions and don't want to say there's no active exception, so give up and
+                // say bad_exception
+                free(_RxRaw);
+                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
+                return;
+            }
+
+            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_InnerRecord)) { // catching a non-C++ exception depends on /EHa
+                _Assign_seh_exception_ptr_from_record(
+                    _Dest, reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord), _RxRaw);
+                return;
+            }
+
+            const auto _PInnerThrow = _InnerRecord.params.pThrowInfo;
+#if _EH_RELATIVE_TYPEINFO
+            const auto _InnerThrowImageBase     = reinterpret_cast<uintptr_t>(_InnerRecord.params.pThrowImageBase);
+            const auto _InnerCatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
+                static_cast<uintptr_t>(_PInnerThrow->pCatchableTypeArray) + _InnerThrowImageBase);
+            const auto _PInnerType = reinterpret_cast<CatchableType*>(
+                static_cast<uintptr_t>(_InnerCatchableTypeArray->arrayOfCatchableTypes[0]) + _InnerThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+            const auto _PInnerType = _PInnerThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+            const auto _InnerExceptionSize = static_cast<size_t>(_PInnerType->sizeOrOffset);
+            const auto _InnerAllocSize     = sizeof(_ExceptionPtr_normal) + _InnerExceptionSize;
+            if (_InnerAllocSize > _AllocSize) {
+                free(_RxRaw);
+                _RxRaw = malloc(_InnerAllocSize);
+                if (!_RxRaw) {
+                    _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
+                    return;
+                }
+            }
+
+            try {
+                _CopyExceptionObject(
+                    static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _InnerRecord.params.pExceptionObject, _PInnerType
+#if _EH_RELATIVE_TYPEINFO
+                    ,
+                    _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+                );
+            } catch (...) { // copying the exception emitted while copying the original exception also threw, give up
+                free(_RxRaw);
+                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
+                return;
+            }
+
+            // this next block must be duplicated inside the catch (even though it looks identical to the block in the
+            // try) so that _InnerRecord is held alive; exiting the catch will destroy it
+            const auto _Rx =
+                ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord));
+            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
+                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
+            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
+        }
+    }
+} // unnamed namespace
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void* _Ptr) noexcept {
+    ::new (_Ptr) shared_ptr<const _EXCEPTION_RECORD>();
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void* _Ptr) noexcept {
+    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr)->~shared_ptr<const _EXCEPTION_RECORD>();
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void* _Dest, _In_ const void* _Src) noexcept {
+    ::new (_Dest) shared_ptr<const _EXCEPTION_RECORD>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src));
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void* _Dest, _In_ const void* _Src) noexcept {
+    *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Dest) =
+        *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src);
+}
+
+_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
+    _In_ const void* _Lhs, _In_ const void* _Rhs) noexcept {
+    return *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)
+        == *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs);
+}
+
+_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void* _Ptr) noexcept {
+    return static_cast<bool>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr));
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void* _Lhs, _Inout_ void* _Rhs) noexcept {
+    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)->swap(
+        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs));
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void* _Ptr) noexcept {
+    const auto _PRecord = _pCurrentException; // nontrivial FLS cost, pay it once
+    if (!_PRecord || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE
+        || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+        return; // no current exception, or we don't support managed exceptions
+    }
+
+    auto& _Dest = *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr);
+    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(_PRecord)) {
+        _Assign_cpp_exception_ptr_from_record(_Dest, *_PRecord);
+    } else {
+        // _Assign_seh_exception_ptr_from_record handles failed malloc
+        _Assign_seh_exception_ptr_from_record(
+            _Dest, reinterpret_cast<_EXCEPTION_RECORD&>(*_PRecord), malloc(sizeof(_ExceptionPtr_normal)));
+    }
+}
+
+[[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void* _PtrRaw) {
+    const shared_ptr<const _EXCEPTION_RECORD>* _Ptr = static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_PtrRaw);
+    // throwing a bad_exception if they give us a nullptr exception_ptr
+    if (!*_Ptr) {
+        throw bad_exception();
+    }
+
+    auto _RecordCopy = **_Ptr;
+    auto& _CppRecord = reinterpret_cast<EHExceptionRecord&>(_RecordCopy);
+    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppRecord)) {
+        // This is a C++ exception.
+        // We need to build the exception on the stack because the current exception mechanism assumes the exception
+        // object is on the stack and will call the appropriate destructor (if there's a nontrivial one).
+        const auto _PThrow = _CppRecord.params.pThrowInfo;
+        if (!_CppRecord.params.pExceptionObject || !_PThrow || !_PThrow->pCatchableTypeArray) {
+            // Missing or corrupt ThrowInfo. If this was a C++ exception, something must have corrupted it.
+            _CSTD abort();
+        }
+
+#if _EH_RELATIVE_TYPEINFO
+        const auto _ThrowImageBase = reinterpret_cast<uintptr_t>(_CppRecord.params.pThrowImageBase);
+        const auto _CatchableTypeArray =
+            reinterpret_cast<const CatchableTypeArray*>(_ThrowImageBase + _PThrow->pCatchableTypeArray);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _CatchableTypeArray = _PThrow->pCatchableTypeArray;
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        if (_CatchableTypeArray->nCatchableTypes <= 0) {
+            // Ditto corrupted.
+            _CSTD abort();
+        }
+
+        // we finally got the type info we want
+#if _EH_RELATIVE_TYPEINFO
+        const auto _PType = reinterpret_cast<CatchableType*>(
+            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        // Alloc memory on stack for exception object. This might cause a stack overflow SEH exception, or another C++
+        // exception when copying the C++ exception object. In that case, we just let that become the thrown exception.
+
+#pragma warning(suppress : 6255) //  _alloca indicates failure by raising a stack overflow exception
+        void* _PExceptionBuffer = alloca(_PType->sizeOrOffset);
+        _CopyExceptionObject(_PExceptionBuffer, _CppRecord.params.pExceptionObject, _PType
+#if _EH_RELATIVE_TYPEINFO
+            ,
+            _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+        );
+
+        _CppRecord.params.pExceptionObject = _PExceptionBuffer;
+    } else {
+        // this is a SEH exception, no special handling is required
+    }
+
+    _Analysis_assume_(_RecordCopy.NumberParameters <= EXCEPTION_MAXIMUM_PARAMETERS);
+    RaiseException(_RecordCopy.ExceptionCode, _RecordCopy.ExceptionFlags, _RecordCopy.NumberParameters,
+        _RecordCopy.ExceptionInformation);
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
+    _Inout_ void* _Ptr, _In_ const void* _PExceptRaw, _In_ const void* _PThrowRaw) noexcept {
+    _EXCEPTION_RECORD _Record;
+    _PopulateCppExceptionRecord(_Record, _PExceptRaw, static_cast<ThrowInfo*>(_PThrowRaw));
+    _Assign_cpp_exception_ptr_from_record(
+        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr), reinterpret_cast<const EHExceptionRecord&>(_Record));
+}

>From a6c26114d89c9a24d90982cf550a91ce97dac0e6 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at microsoft.com>
Date: Mon, 10 Jun 2024 05:33:51 -0700
Subject: [PATCH 02/10] microsoft/STL's `<exception>`, copied verbatim as
 `win32_exception_ptr.h`.

>From our `main` branch as of 2024-06-10:

https://github.com/microsoft/STL/blob/e36ee6c2b9bc6f5b1f70776c18cf5d3a93a69798/stl/inc/exception
---
 .../include/__exception/win32_exception_ptr.h | 422 ++++++++++++++++++
 1 file changed, 422 insertions(+)
 create mode 100644 libcxx/include/__exception/win32_exception_ptr.h

diff --git a/libcxx/include/__exception/win32_exception_ptr.h b/libcxx/include/__exception/win32_exception_ptr.h
new file mode 100644
index 0000000000000..95721640b89c4
--- /dev/null
+++ b/libcxx/include/__exception/win32_exception_ptr.h
@@ -0,0 +1,422 @@
+// exception standard header
+
+// Copyright (c) Microsoft Corporation.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#ifndef _EXCEPTION_
+#define _EXCEPTION_
+#include <yvals.h>
+#if _STL_COMPILER_PREPROCESSOR
+
+#include <cstdlib>
+#include <type_traits>
+
+#pragma pack(push, _CRT_PACKING)
+#pragma warning(push, _STL_WARNING_LEVEL)
+#pragma warning(disable : _STL_DISABLED_WARNINGS)
+_STL_DISABLE_CLANG_WARNINGS
+#pragma push_macro("new")
+#undef new
+
+_STD_BEGIN
+
+#if _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
+_EXPORT_STD extern "C++" _CXX17_DEPRECATE_UNCAUGHT_EXCEPTION _NODISCARD _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL
+    uncaught_exception() noexcept;
+#endif // _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
+_EXPORT_STD extern "C++" _NODISCARD _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL uncaught_exceptions() noexcept;
+
+_STD_END
+
+#if _HAS_EXCEPTIONS
+
+#include <malloc.h> // TRANSITION, VSO-2048380: This is unnecessary, but many projects assume it as of 2024-04-29
+#include <vcruntime_exception.h>
+
+_STD_BEGIN
+
+_EXPORT_STD using ::terminate;
+
+#ifndef _M_CEE_PURE
+_EXPORT_STD using ::set_terminate;
+_EXPORT_STD using ::terminate_handler;
+
+_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
+    // get current terminate handler
+    return _get_terminate();
+}
+#endif // !defined(_M_CEE_PURE)
+
+#if _HAS_UNEXPECTED
+using ::unexpected;
+
+#ifndef _M_CEE_PURE
+using ::set_unexpected;
+using ::unexpected_handler;
+
+_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
+    // get current unexpected handler
+    return _get_unexpected();
+}
+#endif // !defined(_M_CEE_PURE)
+#endif // _HAS_UNEXPECTED
+
+_STD_END
+
+#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv
+
+#pragma push_macro("stdext")
+#undef stdext
+
+_STDEXT_BEGIN
+class exception;
+_STDEXT_END
+
+_STD_BEGIN
+
+_EXPORT_STD using _STDEXT exception;
+
+using _Prhand = void(__cdecl*)(const exception&);
+
+extern _CRTIMP2_PURE_IMPORT _Prhand _Raise_handler; // pointer to raise handler
+
+_STD_END
+
+_STDEXT_BEGIN
+class exception { // base of all library exceptions
+public:
+    static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew) { // register a handler for _Raise calls
+        const _STD _Prhand _Pold = _STD _Raise_handler;
+        _STD _Raise_handler      = _Pnew;
+        return _Pold;
+    }
+
+    // this constructor is necessary to compile
+    // successfully header new for _HAS_EXCEPTIONS==0 scenario
+    explicit __CLR_OR_THIS_CALL exception(const char* _Message = "unknown", int = 1) noexcept : _Ptr(_Message) {}
+
+    __CLR_OR_THIS_CALL exception(const exception& _Right) noexcept : _Ptr(_Right._Ptr) {}
+
+    exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) noexcept {
+        _Ptr = _Right._Ptr;
+        return *this;
+    }
+
+    virtual __CLR_OR_THIS_CALL ~exception() noexcept {}
+
+    _NODISCARD virtual const char* __CLR_OR_THIS_CALL what() const noexcept { // return pointer to message string
+        return _Ptr ? _Ptr : "unknown exception";
+    }
+
+    [[noreturn]] void __CLR_OR_THIS_CALL _Raise() const { // raise the exception
+        if (_STD _Raise_handler) {
+            (*_STD _Raise_handler)(*this); // call raise handler if present
+        }
+
+        _Doraise(); // call the protected virtual
+        _RAISE(*this); // raise this exception
+    }
+
+protected:
+    virtual void __CLR_OR_THIS_CALL _Doraise() const {} // perform class-specific exception handling
+
+    const char* _Ptr; // the message pointer
+};
+
+class bad_exception : public exception { // base of all bad exceptions
+public:
+    __CLR_OR_THIS_CALL bad_exception(const char* _Message = "bad exception") noexcept : exception(_Message) {}
+
+    __CLR_OR_THIS_CALL ~bad_exception() noexcept override {}
+
+protected:
+    void __CLR_OR_THIS_CALL _Doraise() const override { // raise this exception
+        _RAISE(*this);
+    }
+};
+
+class bad_array_new_length;
+
+class bad_alloc : public exception { // base of all bad allocation exceptions
+public:
+    __CLR_OR_THIS_CALL bad_alloc() noexcept
+        : exception("bad allocation", 1) {} // construct from message string with no memory allocation
+
+    __CLR_OR_THIS_CALL ~bad_alloc() noexcept override {}
+
+private:
+    friend bad_array_new_length;
+
+    __CLR_OR_THIS_CALL bad_alloc(const char* _Message) noexcept
+        : exception(_Message, 1) {} // construct from message string with no memory allocation
+
+protected:
+    void __CLR_OR_THIS_CALL _Doraise() const override { // perform class-specific exception handling
+        _RAISE(*this);
+    }
+};
+
+class bad_array_new_length : public bad_alloc {
+public:
+    bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
+};
+
+_STDEXT_END
+
+_STD_BEGIN
+_EXPORT_STD using terminate_handler = void(__cdecl*)();
+
+_EXPORT_STD inline terminate_handler __CRTDECL set_terminate(terminate_handler) noexcept {
+    // register a terminate handler
+    return nullptr;
+}
+
+_EXPORT_STD [[noreturn]] inline void __CRTDECL terminate() noexcept {
+    // handle exception termination
+    _CSTD abort();
+}
+
+_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
+    // get current terminate handler
+    return nullptr;
+}
+
+#if _HAS_UNEXPECTED
+using unexpected_handler = void(__cdecl*)();
+
+inline unexpected_handler __CRTDECL set_unexpected(unexpected_handler) noexcept {
+    // register an unexpected handler
+    return nullptr;
+}
+
+inline void __CRTDECL unexpected() {} // handle unexpected exception
+
+_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
+    // get current unexpected handler
+    return nullptr;
+}
+#endif // _HAS_UNEXPECTED
+
+_EXPORT_STD using _STDEXT bad_alloc;
+_EXPORT_STD using _STDEXT bad_array_new_length;
+_EXPORT_STD using _STDEXT bad_exception;
+
+_STD_END
+
+#pragma pop_macro("stdext")
+
+#endif // ^^^ !_HAS_EXCEPTIONS ^^^
+
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void*, _In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void*, _In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
+    _In_ const void*, _In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void*, _Inout_ void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void*) noexcept;
+extern "C++" [[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void*);
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
+    _Inout_ void*, _In_ const void*, _In_ const void*) noexcept;
+
+_STD_BEGIN
+
+_EXPORT_STD class exception_ptr {
+public:
+    exception_ptr() noexcept {
+        __ExceptionPtrCreate(this);
+    }
+
+    exception_ptr(nullptr_t) noexcept {
+        __ExceptionPtrCreate(this);
+    }
+
+    ~exception_ptr() noexcept {
+        __ExceptionPtrDestroy(this);
+    }
+
+    exception_ptr(const exception_ptr& _Rhs) noexcept {
+        __ExceptionPtrCopy(this, &_Rhs);
+    }
+
+    exception_ptr& operator=(const exception_ptr& _Rhs) noexcept {
+        __ExceptionPtrAssign(this, &_Rhs);
+        return *this;
+    }
+
+    exception_ptr& operator=(nullptr_t) noexcept {
+        exception_ptr _Ptr;
+        __ExceptionPtrAssign(this, &_Ptr);
+        return *this;
+    }
+
+    explicit operator bool() const noexcept {
+        return __ExceptionPtrToBool(this);
+    }
+
+    static exception_ptr _Copy_exception(_In_ void* _Except, _In_ const void* _Ptr) {
+        exception_ptr _Retval;
+        if (!_Ptr) {
+            // unsupported exceptions
+            return _Retval;
+        }
+        __ExceptionPtrCopyException(&_Retval, _Except, _Ptr);
+        return _Retval;
+    }
+
+    friend void swap(exception_ptr& _Lhs, exception_ptr& _Rhs) noexcept {
+        __ExceptionPtrSwap(&_Lhs, &_Rhs);
+    }
+
+    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
+        return __ExceptionPtrCompare(&_Lhs, &_Rhs);
+    }
+
+    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, nullptr_t) noexcept {
+        return !_Lhs;
+    }
+
+#if !_HAS_CXX20
+    _NODISCARD_FRIEND bool operator==(nullptr_t, const exception_ptr& _Rhs) noexcept {
+        return !_Rhs;
+    }
+
+    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
+        return !(_Lhs == _Rhs);
+    }
+
+    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, nullptr_t) noexcept {
+        return !(_Lhs == nullptr);
+    }
+
+    _NODISCARD_FRIEND bool operator!=(nullptr_t, const exception_ptr& _Rhs) noexcept {
+        return !(nullptr == _Rhs);
+    }
+#endif // !_HAS_CXX20
+
+private:
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunused-private-field"
+#endif // defined(__clang__)
+    void* _Data1{};
+    void* _Data2{};
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif // defined(__clang__)
+};
+
+_EXPORT_STD _NODISCARD inline exception_ptr current_exception() noexcept {
+    exception_ptr _Retval;
+    __ExceptionPtrCurrentException(&_Retval);
+    return _Retval;
+}
+
+_EXPORT_STD [[noreturn]] inline void rethrow_exception(_In_ exception_ptr _Ptr) {
+    __ExceptionPtrRethrow(&_Ptr);
+}
+
+template <class _Ex>
+void* __GetExceptionInfo(_Ex);
+
+_EXPORT_STD template <class _Ex>
+_NODISCARD_SMART_PTR_ALLOC exception_ptr make_exception_ptr(_Ex _Except) noexcept {
+    return exception_ptr::_Copy_exception(_STD addressof(_Except), __GetExceptionInfo(_Except));
+}
+
+_EXPORT_STD class nested_exception { // wrap an exception_ptr
+public:
+    nested_exception() noexcept : _Exc(_STD current_exception()) {}
+
+    nested_exception(const nested_exception&) noexcept            = default;
+    nested_exception& operator=(const nested_exception&) noexcept = default;
+    virtual ~nested_exception() noexcept {}
+
+    [[noreturn]] void rethrow_nested() const { // throw wrapped exception_ptr
+        if (_Exc) {
+            _STD rethrow_exception(_Exc);
+        } else {
+            _STD terminate(); // per N4950 [except.nested]/4
+        }
+    }
+
+    _NODISCARD exception_ptr nested_ptr() const noexcept { // return wrapped exception_ptr
+        return _Exc;
+    }
+
+private:
+    exception_ptr _Exc;
+};
+
+template <class _Uty>
+struct _With_nested_v2 : _Uty, nested_exception { // glue user exception to nested_exception
+    template <class _Ty>
+    explicit _With_nested_v2(_Ty&& _Arg)
+        : _Uty(_STD forward<_Ty>(_Arg)), nested_exception() {} // store user exception and current_exception()
+};
+
+_EXPORT_STD template <class _Ty>
+[[noreturn]] void throw_with_nested(_Ty&& _Arg) {
+    // throw user exception, glued to nested_exception if possible
+    using _Uty = decay_t<_Ty>;
+
+    if constexpr (is_class_v<_Uty> && !is_base_of_v<nested_exception, _Uty> && !is_final_v<_Uty>) {
+        // throw user exception glued to nested_exception
+        _THROW(_With_nested_v2<_Uty>(_STD forward<_Ty>(_Arg)));
+    } else {
+        // throw user exception by itself
+        _THROW(_STD forward<_Ty>(_Arg));
+    }
+}
+
+#ifdef _CPPRTTI
+_EXPORT_STD template <class _Ty>
+void rethrow_if_nested(const _Ty& _Arg) {
+    // detect nested_exception inheritance
+    constexpr bool _Can_use_dynamic_cast =
+        is_polymorphic_v<_Ty> && (!is_base_of_v<nested_exception, _Ty> || is_convertible_v<_Ty*, nested_exception*>);
+
+    if constexpr (_Can_use_dynamic_cast) {
+        const auto _Nested = dynamic_cast<const nested_exception*>(_STD addressof(_Arg));
+
+        if (_Nested) {
+            _Nested->rethrow_nested();
+        }
+    }
+}
+#else // ^^^ defined(_CPPRTTI) / !defined(_CPPRTTI) vvv
+_EXPORT_STD template <class _Ty>
+void rethrow_if_nested(const _Ty&) = delete; // requires /GR option
+#endif // ^^^ !defined(_CPPRTTI) ^^^
+
+_EXPORT_STD class bad_variant_access
+    : public exception { // exception for visit of a valueless variant or get<I> on a variant with index() != I
+public:
+    bad_variant_access() noexcept = default;
+
+    _NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
+        return "bad variant access";
+    }
+
+#if !_HAS_EXCEPTIONS
+protected:
+    void _Doraise() const override { // perform class-specific exception handling
+        _RAISE(*this);
+    }
+#endif // ^^^ !_HAS_EXCEPTIONS ^^^
+};
+
+[[noreturn]] inline void _Throw_bad_variant_access() {
+    _THROW(bad_variant_access{});
+}
+
+_STD_END
+
+#pragma pop_macro("new")
+_STL_RESTORE_CLANG_WARNINGS
+#pragma warning(pop)
+#pragma pack(pop)
+
+#endif // _STL_COMPILER_PREPROCESSOR
+#endif // _EXCEPTION_

>From 95f3871b3189272157cd596271322daa3714b845 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at microsoft.com>
Date: Mon, 10 Jun 2024 05:07:16 -0700
Subject: [PATCH 03/10] Convert files to LF line endings.

---
 .../include/__exception/win32_exception_ptr.h |  844 ++++++-------
 libcxx/src/support/win32/excptptr.cpp         | 1066 ++++++++---------
 2 files changed, 955 insertions(+), 955 deletions(-)

diff --git a/libcxx/include/__exception/win32_exception_ptr.h b/libcxx/include/__exception/win32_exception_ptr.h
index 95721640b89c4..efadca1b733d0 100644
--- a/libcxx/include/__exception/win32_exception_ptr.h
+++ b/libcxx/include/__exception/win32_exception_ptr.h
@@ -1,422 +1,422 @@
-// exception standard header
-
-// Copyright (c) Microsoft Corporation.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-#ifndef _EXCEPTION_
-#define _EXCEPTION_
-#include <yvals.h>
-#if _STL_COMPILER_PREPROCESSOR
-
-#include <cstdlib>
-#include <type_traits>
-
-#pragma pack(push, _CRT_PACKING)
-#pragma warning(push, _STL_WARNING_LEVEL)
-#pragma warning(disable : _STL_DISABLED_WARNINGS)
-_STL_DISABLE_CLANG_WARNINGS
-#pragma push_macro("new")
-#undef new
-
-_STD_BEGIN
-
-#if _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
-_EXPORT_STD extern "C++" _CXX17_DEPRECATE_UNCAUGHT_EXCEPTION _NODISCARD _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL
-    uncaught_exception() noexcept;
-#endif // _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
-_EXPORT_STD extern "C++" _NODISCARD _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL uncaught_exceptions() noexcept;
-
-_STD_END
-
-#if _HAS_EXCEPTIONS
-
-#include <malloc.h> // TRANSITION, VSO-2048380: This is unnecessary, but many projects assume it as of 2024-04-29
-#include <vcruntime_exception.h>
-
-_STD_BEGIN
-
-_EXPORT_STD using ::terminate;
-
-#ifndef _M_CEE_PURE
-_EXPORT_STD using ::set_terminate;
-_EXPORT_STD using ::terminate_handler;
-
-_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
-    // get current terminate handler
-    return _get_terminate();
-}
-#endif // !defined(_M_CEE_PURE)
-
-#if _HAS_UNEXPECTED
-using ::unexpected;
-
-#ifndef _M_CEE_PURE
-using ::set_unexpected;
-using ::unexpected_handler;
-
-_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
-    // get current unexpected handler
-    return _get_unexpected();
-}
-#endif // !defined(_M_CEE_PURE)
-#endif // _HAS_UNEXPECTED
-
-_STD_END
-
-#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv
-
-#pragma push_macro("stdext")
-#undef stdext
-
-_STDEXT_BEGIN
-class exception;
-_STDEXT_END
-
-_STD_BEGIN
-
-_EXPORT_STD using _STDEXT exception;
-
-using _Prhand = void(__cdecl*)(const exception&);
-
-extern _CRTIMP2_PURE_IMPORT _Prhand _Raise_handler; // pointer to raise handler
-
-_STD_END
-
-_STDEXT_BEGIN
-class exception { // base of all library exceptions
-public:
-    static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew) { // register a handler for _Raise calls
-        const _STD _Prhand _Pold = _STD _Raise_handler;
-        _STD _Raise_handler      = _Pnew;
-        return _Pold;
-    }
-
-    // this constructor is necessary to compile
-    // successfully header new for _HAS_EXCEPTIONS==0 scenario
-    explicit __CLR_OR_THIS_CALL exception(const char* _Message = "unknown", int = 1) noexcept : _Ptr(_Message) {}
-
-    __CLR_OR_THIS_CALL exception(const exception& _Right) noexcept : _Ptr(_Right._Ptr) {}
-
-    exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) noexcept {
-        _Ptr = _Right._Ptr;
-        return *this;
-    }
-
-    virtual __CLR_OR_THIS_CALL ~exception() noexcept {}
-
-    _NODISCARD virtual const char* __CLR_OR_THIS_CALL what() const noexcept { // return pointer to message string
-        return _Ptr ? _Ptr : "unknown exception";
-    }
-
-    [[noreturn]] void __CLR_OR_THIS_CALL _Raise() const { // raise the exception
-        if (_STD _Raise_handler) {
-            (*_STD _Raise_handler)(*this); // call raise handler if present
-        }
-
-        _Doraise(); // call the protected virtual
-        _RAISE(*this); // raise this exception
-    }
-
-protected:
-    virtual void __CLR_OR_THIS_CALL _Doraise() const {} // perform class-specific exception handling
-
-    const char* _Ptr; // the message pointer
-};
-
-class bad_exception : public exception { // base of all bad exceptions
-public:
-    __CLR_OR_THIS_CALL bad_exception(const char* _Message = "bad exception") noexcept : exception(_Message) {}
-
-    __CLR_OR_THIS_CALL ~bad_exception() noexcept override {}
-
-protected:
-    void __CLR_OR_THIS_CALL _Doraise() const override { // raise this exception
-        _RAISE(*this);
-    }
-};
-
-class bad_array_new_length;
-
-class bad_alloc : public exception { // base of all bad allocation exceptions
-public:
-    __CLR_OR_THIS_CALL bad_alloc() noexcept
-        : exception("bad allocation", 1) {} // construct from message string with no memory allocation
-
-    __CLR_OR_THIS_CALL ~bad_alloc() noexcept override {}
-
-private:
-    friend bad_array_new_length;
-
-    __CLR_OR_THIS_CALL bad_alloc(const char* _Message) noexcept
-        : exception(_Message, 1) {} // construct from message string with no memory allocation
-
-protected:
-    void __CLR_OR_THIS_CALL _Doraise() const override { // perform class-specific exception handling
-        _RAISE(*this);
-    }
-};
-
-class bad_array_new_length : public bad_alloc {
-public:
-    bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
-};
-
-_STDEXT_END
-
-_STD_BEGIN
-_EXPORT_STD using terminate_handler = void(__cdecl*)();
-
-_EXPORT_STD inline terminate_handler __CRTDECL set_terminate(terminate_handler) noexcept {
-    // register a terminate handler
-    return nullptr;
-}
-
-_EXPORT_STD [[noreturn]] inline void __CRTDECL terminate() noexcept {
-    // handle exception termination
-    _CSTD abort();
-}
-
-_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
-    // get current terminate handler
-    return nullptr;
-}
-
-#if _HAS_UNEXPECTED
-using unexpected_handler = void(__cdecl*)();
-
-inline unexpected_handler __CRTDECL set_unexpected(unexpected_handler) noexcept {
-    // register an unexpected handler
-    return nullptr;
-}
-
-inline void __CRTDECL unexpected() {} // handle unexpected exception
-
-_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
-    // get current unexpected handler
-    return nullptr;
-}
-#endif // _HAS_UNEXPECTED
-
-_EXPORT_STD using _STDEXT bad_alloc;
-_EXPORT_STD using _STDEXT bad_array_new_length;
-_EXPORT_STD using _STDEXT bad_exception;
-
-_STD_END
-
-#pragma pop_macro("stdext")
-
-#endif // ^^^ !_HAS_EXCEPTIONS ^^^
-
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void*, _In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void*, _In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
-    _In_ const void*, _In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void*, _Inout_ void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void*) noexcept;
-extern "C++" [[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void*);
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
-    _Inout_ void*, _In_ const void*, _In_ const void*) noexcept;
-
-_STD_BEGIN
-
-_EXPORT_STD class exception_ptr {
-public:
-    exception_ptr() noexcept {
-        __ExceptionPtrCreate(this);
-    }
-
-    exception_ptr(nullptr_t) noexcept {
-        __ExceptionPtrCreate(this);
-    }
-
-    ~exception_ptr() noexcept {
-        __ExceptionPtrDestroy(this);
-    }
-
-    exception_ptr(const exception_ptr& _Rhs) noexcept {
-        __ExceptionPtrCopy(this, &_Rhs);
-    }
-
-    exception_ptr& operator=(const exception_ptr& _Rhs) noexcept {
-        __ExceptionPtrAssign(this, &_Rhs);
-        return *this;
-    }
-
-    exception_ptr& operator=(nullptr_t) noexcept {
-        exception_ptr _Ptr;
-        __ExceptionPtrAssign(this, &_Ptr);
-        return *this;
-    }
-
-    explicit operator bool() const noexcept {
-        return __ExceptionPtrToBool(this);
-    }
-
-    static exception_ptr _Copy_exception(_In_ void* _Except, _In_ const void* _Ptr) {
-        exception_ptr _Retval;
-        if (!_Ptr) {
-            // unsupported exceptions
-            return _Retval;
-        }
-        __ExceptionPtrCopyException(&_Retval, _Except, _Ptr);
-        return _Retval;
-    }
-
-    friend void swap(exception_ptr& _Lhs, exception_ptr& _Rhs) noexcept {
-        __ExceptionPtrSwap(&_Lhs, &_Rhs);
-    }
-
-    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
-        return __ExceptionPtrCompare(&_Lhs, &_Rhs);
-    }
-
-    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, nullptr_t) noexcept {
-        return !_Lhs;
-    }
-
-#if !_HAS_CXX20
-    _NODISCARD_FRIEND bool operator==(nullptr_t, const exception_ptr& _Rhs) noexcept {
-        return !_Rhs;
-    }
-
-    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
-        return !(_Lhs == _Rhs);
-    }
-
-    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, nullptr_t) noexcept {
-        return !(_Lhs == nullptr);
-    }
-
-    _NODISCARD_FRIEND bool operator!=(nullptr_t, const exception_ptr& _Rhs) noexcept {
-        return !(nullptr == _Rhs);
-    }
-#endif // !_HAS_CXX20
-
-private:
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wunused-private-field"
-#endif // defined(__clang__)
-    void* _Data1{};
-    void* _Data2{};
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif // defined(__clang__)
-};
-
-_EXPORT_STD _NODISCARD inline exception_ptr current_exception() noexcept {
-    exception_ptr _Retval;
-    __ExceptionPtrCurrentException(&_Retval);
-    return _Retval;
-}
-
-_EXPORT_STD [[noreturn]] inline void rethrow_exception(_In_ exception_ptr _Ptr) {
-    __ExceptionPtrRethrow(&_Ptr);
-}
-
-template <class _Ex>
-void* __GetExceptionInfo(_Ex);
-
-_EXPORT_STD template <class _Ex>
-_NODISCARD_SMART_PTR_ALLOC exception_ptr make_exception_ptr(_Ex _Except) noexcept {
-    return exception_ptr::_Copy_exception(_STD addressof(_Except), __GetExceptionInfo(_Except));
-}
-
-_EXPORT_STD class nested_exception { // wrap an exception_ptr
-public:
-    nested_exception() noexcept : _Exc(_STD current_exception()) {}
-
-    nested_exception(const nested_exception&) noexcept            = default;
-    nested_exception& operator=(const nested_exception&) noexcept = default;
-    virtual ~nested_exception() noexcept {}
-
-    [[noreturn]] void rethrow_nested() const { // throw wrapped exception_ptr
-        if (_Exc) {
-            _STD rethrow_exception(_Exc);
-        } else {
-            _STD terminate(); // per N4950 [except.nested]/4
-        }
-    }
-
-    _NODISCARD exception_ptr nested_ptr() const noexcept { // return wrapped exception_ptr
-        return _Exc;
-    }
-
-private:
-    exception_ptr _Exc;
-};
-
-template <class _Uty>
-struct _With_nested_v2 : _Uty, nested_exception { // glue user exception to nested_exception
-    template <class _Ty>
-    explicit _With_nested_v2(_Ty&& _Arg)
-        : _Uty(_STD forward<_Ty>(_Arg)), nested_exception() {} // store user exception and current_exception()
-};
-
-_EXPORT_STD template <class _Ty>
-[[noreturn]] void throw_with_nested(_Ty&& _Arg) {
-    // throw user exception, glued to nested_exception if possible
-    using _Uty = decay_t<_Ty>;
-
-    if constexpr (is_class_v<_Uty> && !is_base_of_v<nested_exception, _Uty> && !is_final_v<_Uty>) {
-        // throw user exception glued to nested_exception
-        _THROW(_With_nested_v2<_Uty>(_STD forward<_Ty>(_Arg)));
-    } else {
-        // throw user exception by itself
-        _THROW(_STD forward<_Ty>(_Arg));
-    }
-}
-
-#ifdef _CPPRTTI
-_EXPORT_STD template <class _Ty>
-void rethrow_if_nested(const _Ty& _Arg) {
-    // detect nested_exception inheritance
-    constexpr bool _Can_use_dynamic_cast =
-        is_polymorphic_v<_Ty> && (!is_base_of_v<nested_exception, _Ty> || is_convertible_v<_Ty*, nested_exception*>);
-
-    if constexpr (_Can_use_dynamic_cast) {
-        const auto _Nested = dynamic_cast<const nested_exception*>(_STD addressof(_Arg));
-
-        if (_Nested) {
-            _Nested->rethrow_nested();
-        }
-    }
-}
-#else // ^^^ defined(_CPPRTTI) / !defined(_CPPRTTI) vvv
-_EXPORT_STD template <class _Ty>
-void rethrow_if_nested(const _Ty&) = delete; // requires /GR option
-#endif // ^^^ !defined(_CPPRTTI) ^^^
-
-_EXPORT_STD class bad_variant_access
-    : public exception { // exception for visit of a valueless variant or get<I> on a variant with index() != I
-public:
-    bad_variant_access() noexcept = default;
-
-    _NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
-        return "bad variant access";
-    }
-
-#if !_HAS_EXCEPTIONS
-protected:
-    void _Doraise() const override { // perform class-specific exception handling
-        _RAISE(*this);
-    }
-#endif // ^^^ !_HAS_EXCEPTIONS ^^^
-};
-
-[[noreturn]] inline void _Throw_bad_variant_access() {
-    _THROW(bad_variant_access{});
-}
-
-_STD_END
-
-#pragma pop_macro("new")
-_STL_RESTORE_CLANG_WARNINGS
-#pragma warning(pop)
-#pragma pack(pop)
-
-#endif // _STL_COMPILER_PREPROCESSOR
-#endif // _EXCEPTION_
+// exception standard header
+
+// Copyright (c) Microsoft Corporation.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#ifndef _EXCEPTION_
+#define _EXCEPTION_
+#include <yvals.h>
+#if _STL_COMPILER_PREPROCESSOR
+
+#include <cstdlib>
+#include <type_traits>
+
+#pragma pack(push, _CRT_PACKING)
+#pragma warning(push, _STL_WARNING_LEVEL)
+#pragma warning(disable : _STL_DISABLED_WARNINGS)
+_STL_DISABLE_CLANG_WARNINGS
+#pragma push_macro("new")
+#undef new
+
+_STD_BEGIN
+
+#if _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
+_EXPORT_STD extern "C++" _CXX17_DEPRECATE_UNCAUGHT_EXCEPTION _NODISCARD _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL
+    uncaught_exception() noexcept;
+#endif // _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
+_EXPORT_STD extern "C++" _NODISCARD _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL uncaught_exceptions() noexcept;
+
+_STD_END
+
+#if _HAS_EXCEPTIONS
+
+#include <malloc.h> // TRANSITION, VSO-2048380: This is unnecessary, but many projects assume it as of 2024-04-29
+#include <vcruntime_exception.h>
+
+_STD_BEGIN
+
+_EXPORT_STD using ::terminate;
+
+#ifndef _M_CEE_PURE
+_EXPORT_STD using ::set_terminate;
+_EXPORT_STD using ::terminate_handler;
+
+_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
+    // get current terminate handler
+    return _get_terminate();
+}
+#endif // !defined(_M_CEE_PURE)
+
+#if _HAS_UNEXPECTED
+using ::unexpected;
+
+#ifndef _M_CEE_PURE
+using ::set_unexpected;
+using ::unexpected_handler;
+
+_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
+    // get current unexpected handler
+    return _get_unexpected();
+}
+#endif // !defined(_M_CEE_PURE)
+#endif // _HAS_UNEXPECTED
+
+_STD_END
+
+#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv
+
+#pragma push_macro("stdext")
+#undef stdext
+
+_STDEXT_BEGIN
+class exception;
+_STDEXT_END
+
+_STD_BEGIN
+
+_EXPORT_STD using _STDEXT exception;
+
+using _Prhand = void(__cdecl*)(const exception&);
+
+extern _CRTIMP2_PURE_IMPORT _Prhand _Raise_handler; // pointer to raise handler
+
+_STD_END
+
+_STDEXT_BEGIN
+class exception { // base of all library exceptions
+public:
+    static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew) { // register a handler for _Raise calls
+        const _STD _Prhand _Pold = _STD _Raise_handler;
+        _STD _Raise_handler      = _Pnew;
+        return _Pold;
+    }
+
+    // this constructor is necessary to compile
+    // successfully header new for _HAS_EXCEPTIONS==0 scenario
+    explicit __CLR_OR_THIS_CALL exception(const char* _Message = "unknown", int = 1) noexcept : _Ptr(_Message) {}
+
+    __CLR_OR_THIS_CALL exception(const exception& _Right) noexcept : _Ptr(_Right._Ptr) {}
+
+    exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) noexcept {
+        _Ptr = _Right._Ptr;
+        return *this;
+    }
+
+    virtual __CLR_OR_THIS_CALL ~exception() noexcept {}
+
+    _NODISCARD virtual const char* __CLR_OR_THIS_CALL what() const noexcept { // return pointer to message string
+        return _Ptr ? _Ptr : "unknown exception";
+    }
+
+    [[noreturn]] void __CLR_OR_THIS_CALL _Raise() const { // raise the exception
+        if (_STD _Raise_handler) {
+            (*_STD _Raise_handler)(*this); // call raise handler if present
+        }
+
+        _Doraise(); // call the protected virtual
+        _RAISE(*this); // raise this exception
+    }
+
+protected:
+    virtual void __CLR_OR_THIS_CALL _Doraise() const {} // perform class-specific exception handling
+
+    const char* _Ptr; // the message pointer
+};
+
+class bad_exception : public exception { // base of all bad exceptions
+public:
+    __CLR_OR_THIS_CALL bad_exception(const char* _Message = "bad exception") noexcept : exception(_Message) {}
+
+    __CLR_OR_THIS_CALL ~bad_exception() noexcept override {}
+
+protected:
+    void __CLR_OR_THIS_CALL _Doraise() const override { // raise this exception
+        _RAISE(*this);
+    }
+};
+
+class bad_array_new_length;
+
+class bad_alloc : public exception { // base of all bad allocation exceptions
+public:
+    __CLR_OR_THIS_CALL bad_alloc() noexcept
+        : exception("bad allocation", 1) {} // construct from message string with no memory allocation
+
+    __CLR_OR_THIS_CALL ~bad_alloc() noexcept override {}
+
+private:
+    friend bad_array_new_length;
+
+    __CLR_OR_THIS_CALL bad_alloc(const char* _Message) noexcept
+        : exception(_Message, 1) {} // construct from message string with no memory allocation
+
+protected:
+    void __CLR_OR_THIS_CALL _Doraise() const override { // perform class-specific exception handling
+        _RAISE(*this);
+    }
+};
+
+class bad_array_new_length : public bad_alloc {
+public:
+    bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
+};
+
+_STDEXT_END
+
+_STD_BEGIN
+_EXPORT_STD using terminate_handler = void(__cdecl*)();
+
+_EXPORT_STD inline terminate_handler __CRTDECL set_terminate(terminate_handler) noexcept {
+    // register a terminate handler
+    return nullptr;
+}
+
+_EXPORT_STD [[noreturn]] inline void __CRTDECL terminate() noexcept {
+    // handle exception termination
+    _CSTD abort();
+}
+
+_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
+    // get current terminate handler
+    return nullptr;
+}
+
+#if _HAS_UNEXPECTED
+using unexpected_handler = void(__cdecl*)();
+
+inline unexpected_handler __CRTDECL set_unexpected(unexpected_handler) noexcept {
+    // register an unexpected handler
+    return nullptr;
+}
+
+inline void __CRTDECL unexpected() {} // handle unexpected exception
+
+_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
+    // get current unexpected handler
+    return nullptr;
+}
+#endif // _HAS_UNEXPECTED
+
+_EXPORT_STD using _STDEXT bad_alloc;
+_EXPORT_STD using _STDEXT bad_array_new_length;
+_EXPORT_STD using _STDEXT bad_exception;
+
+_STD_END
+
+#pragma pop_macro("stdext")
+
+#endif // ^^^ !_HAS_EXCEPTIONS ^^^
+
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void*, _In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void*, _In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
+    _In_ const void*, _In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void*, _Inout_ void*) noexcept;
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void*) noexcept;
+extern "C++" [[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void*);
+extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
+    _Inout_ void*, _In_ const void*, _In_ const void*) noexcept;
+
+_STD_BEGIN
+
+_EXPORT_STD class exception_ptr {
+public:
+    exception_ptr() noexcept {
+        __ExceptionPtrCreate(this);
+    }
+
+    exception_ptr(nullptr_t) noexcept {
+        __ExceptionPtrCreate(this);
+    }
+
+    ~exception_ptr() noexcept {
+        __ExceptionPtrDestroy(this);
+    }
+
+    exception_ptr(const exception_ptr& _Rhs) noexcept {
+        __ExceptionPtrCopy(this, &_Rhs);
+    }
+
+    exception_ptr& operator=(const exception_ptr& _Rhs) noexcept {
+        __ExceptionPtrAssign(this, &_Rhs);
+        return *this;
+    }
+
+    exception_ptr& operator=(nullptr_t) noexcept {
+        exception_ptr _Ptr;
+        __ExceptionPtrAssign(this, &_Ptr);
+        return *this;
+    }
+
+    explicit operator bool() const noexcept {
+        return __ExceptionPtrToBool(this);
+    }
+
+    static exception_ptr _Copy_exception(_In_ void* _Except, _In_ const void* _Ptr) {
+        exception_ptr _Retval;
+        if (!_Ptr) {
+            // unsupported exceptions
+            return _Retval;
+        }
+        __ExceptionPtrCopyException(&_Retval, _Except, _Ptr);
+        return _Retval;
+    }
+
+    friend void swap(exception_ptr& _Lhs, exception_ptr& _Rhs) noexcept {
+        __ExceptionPtrSwap(&_Lhs, &_Rhs);
+    }
+
+    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
+        return __ExceptionPtrCompare(&_Lhs, &_Rhs);
+    }
+
+    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, nullptr_t) noexcept {
+        return !_Lhs;
+    }
+
+#if !_HAS_CXX20
+    _NODISCARD_FRIEND bool operator==(nullptr_t, const exception_ptr& _Rhs) noexcept {
+        return !_Rhs;
+    }
+
+    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
+        return !(_Lhs == _Rhs);
+    }
+
+    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, nullptr_t) noexcept {
+        return !(_Lhs == nullptr);
+    }
+
+    _NODISCARD_FRIEND bool operator!=(nullptr_t, const exception_ptr& _Rhs) noexcept {
+        return !(nullptr == _Rhs);
+    }
+#endif // !_HAS_CXX20
+
+private:
+#ifdef __clang__
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunused-private-field"
+#endif // defined(__clang__)
+    void* _Data1{};
+    void* _Data2{};
+#ifdef __clang__
+#pragma clang diagnostic pop
+#endif // defined(__clang__)
+};
+
+_EXPORT_STD _NODISCARD inline exception_ptr current_exception() noexcept {
+    exception_ptr _Retval;
+    __ExceptionPtrCurrentException(&_Retval);
+    return _Retval;
+}
+
+_EXPORT_STD [[noreturn]] inline void rethrow_exception(_In_ exception_ptr _Ptr) {
+    __ExceptionPtrRethrow(&_Ptr);
+}
+
+template <class _Ex>
+void* __GetExceptionInfo(_Ex);
+
+_EXPORT_STD template <class _Ex>
+_NODISCARD_SMART_PTR_ALLOC exception_ptr make_exception_ptr(_Ex _Except) noexcept {
+    return exception_ptr::_Copy_exception(_STD addressof(_Except), __GetExceptionInfo(_Except));
+}
+
+_EXPORT_STD class nested_exception { // wrap an exception_ptr
+public:
+    nested_exception() noexcept : _Exc(_STD current_exception()) {}
+
+    nested_exception(const nested_exception&) noexcept            = default;
+    nested_exception& operator=(const nested_exception&) noexcept = default;
+    virtual ~nested_exception() noexcept {}
+
+    [[noreturn]] void rethrow_nested() const { // throw wrapped exception_ptr
+        if (_Exc) {
+            _STD rethrow_exception(_Exc);
+        } else {
+            _STD terminate(); // per N4950 [except.nested]/4
+        }
+    }
+
+    _NODISCARD exception_ptr nested_ptr() const noexcept { // return wrapped exception_ptr
+        return _Exc;
+    }
+
+private:
+    exception_ptr _Exc;
+};
+
+template <class _Uty>
+struct _With_nested_v2 : _Uty, nested_exception { // glue user exception to nested_exception
+    template <class _Ty>
+    explicit _With_nested_v2(_Ty&& _Arg)
+        : _Uty(_STD forward<_Ty>(_Arg)), nested_exception() {} // store user exception and current_exception()
+};
+
+_EXPORT_STD template <class _Ty>
+[[noreturn]] void throw_with_nested(_Ty&& _Arg) {
+    // throw user exception, glued to nested_exception if possible
+    using _Uty = decay_t<_Ty>;
+
+    if constexpr (is_class_v<_Uty> && !is_base_of_v<nested_exception, _Uty> && !is_final_v<_Uty>) {
+        // throw user exception glued to nested_exception
+        _THROW(_With_nested_v2<_Uty>(_STD forward<_Ty>(_Arg)));
+    } else {
+        // throw user exception by itself
+        _THROW(_STD forward<_Ty>(_Arg));
+    }
+}
+
+#ifdef _CPPRTTI
+_EXPORT_STD template <class _Ty>
+void rethrow_if_nested(const _Ty& _Arg) {
+    // detect nested_exception inheritance
+    constexpr bool _Can_use_dynamic_cast =
+        is_polymorphic_v<_Ty> && (!is_base_of_v<nested_exception, _Ty> || is_convertible_v<_Ty*, nested_exception*>);
+
+    if constexpr (_Can_use_dynamic_cast) {
+        const auto _Nested = dynamic_cast<const nested_exception*>(_STD addressof(_Arg));
+
+        if (_Nested) {
+            _Nested->rethrow_nested();
+        }
+    }
+}
+#else // ^^^ defined(_CPPRTTI) / !defined(_CPPRTTI) vvv
+_EXPORT_STD template <class _Ty>
+void rethrow_if_nested(const _Ty&) = delete; // requires /GR option
+#endif // ^^^ !defined(_CPPRTTI) ^^^
+
+_EXPORT_STD class bad_variant_access
+    : public exception { // exception for visit of a valueless variant or get<I> on a variant with index() != I
+public:
+    bad_variant_access() noexcept = default;
+
+    _NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
+        return "bad variant access";
+    }
+
+#if !_HAS_EXCEPTIONS
+protected:
+    void _Doraise() const override { // perform class-specific exception handling
+        _RAISE(*this);
+    }
+#endif // ^^^ !_HAS_EXCEPTIONS ^^^
+};
+
+[[noreturn]] inline void _Throw_bad_variant_access() {
+    _THROW(bad_variant_access{});
+}
+
+_STD_END
+
+#pragma pop_macro("new")
+_STL_RESTORE_CLANG_WARNINGS
+#pragma warning(pop)
+#pragma pack(pop)
+
+#endif // _STL_COMPILER_PREPROCESSOR
+#endif // _EXCEPTION_
diff --git a/libcxx/src/support/win32/excptptr.cpp b/libcxx/src/support/win32/excptptr.cpp
index 830cb65a940d5..a78d1528b0862 100644
--- a/libcxx/src/support/win32/excptptr.cpp
+++ b/libcxx/src/support/win32/excptptr.cpp
@@ -1,533 +1,533 @@
-// Copyright (c) Microsoft Corporation.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-// This implementation communicates with the EH runtime though vcruntime's per-thread-data structure; see
-// _pCurrentException in <trnsctrl.h>.
-//
-// As a result, normal EH runtime services (such as noexcept functions) are safe to use in this file.
-
-#ifndef _VCRT_ALLOW_INTERNALS
-#define _VCRT_ALLOW_INTERNALS
-#endif // !defined(_VCRT_ALLOW_INTERNALS)
-
-#include <Unknwn.h>
-#include <cstdlib> // for abort
-#include <cstring> // for memcpy
-#include <eh.h>
-#include <ehdata.h>
-#include <exception>
-#include <internal_shared.h>
-#include <malloc.h>
-#include <memory>
-#include <new>
-#include <stdexcept>
-#include <trnsctrl.h>
-#include <xcall_once.h>
-
-#include <Windows.h>
-
-// Pre-V4 managed exception code
-#define MANAGED_EXCEPTION_CODE 0XE0434F4D
-
-// V4 and later managed exception code
-#define MANAGED_EXCEPTION_CODE_V4 0XE0434352
-
-extern "C" _CRTIMP2 void* __cdecl __AdjustPointer(void*, const PMD&); // defined in frame.cpp
-
-using namespace std;
-
-namespace {
-#if defined(_M_CEE_PURE)
-    template <class _Ty>
-    _Ty& _Immortalize() { // return a reference to an object that will live forever
-        /* MAGIC */ static _Immortalizer_impl<_Ty> _Static;
-        return reinterpret_cast<_Ty&>(_Static._Storage);
-    }
-#elif !defined(_M_CEE)
-    template <class _Ty>
-    struct _Constexpr_excptptr_immortalize_impl {
-        union {
-            _Ty _Storage;
-        };
-
-        constexpr _Constexpr_excptptr_immortalize_impl() noexcept : _Storage{} {}
-
-        _Constexpr_excptptr_immortalize_impl(const _Constexpr_excptptr_immortalize_impl&)            = delete;
-        _Constexpr_excptptr_immortalize_impl& operator=(const _Constexpr_excptptr_immortalize_impl&) = delete;
-
-        _MSVC_NOOP_DTOR ~_Constexpr_excptptr_immortalize_impl() {
-            // do nothing, allowing _Ty to be used during shutdown
-        }
-    };
-
-    template <class _Ty>
-    _Constexpr_excptptr_immortalize_impl<_Ty> _Immortalize_impl;
-
-    template <class _Ty>
-    [[nodiscard]] _Ty& _Immortalize() noexcept {
-        return _Immortalize_impl<_Ty>._Storage;
-    }
-#else // ^^^ !defined(_M_CEE) / defined(_M_CEE), TRANSITION, VSO-1153256 vvv
-    template <class _Ty>
-    _Ty& _Immortalize() { // return a reference to an object that will live forever
-        static once_flag _Flag;
-        alignas(_Ty) static unsigned char _Storage[sizeof(_Ty)];
-        call_once(_Flag, [&_Storage] { ::new (static_cast<void*>(&_Storage)) _Ty(); });
-        return reinterpret_cast<_Ty&>(_Storage);
-    }
-#endif // ^^^ !defined(_M_CEE_PURE) && defined(_M_CEE), TRANSITION, VSO-1153256 ^^^
-
-    void _PopulateCppExceptionRecord(
-        _EXCEPTION_RECORD& _Record, const void* const _PExcept, ThrowInfo* _PThrow) noexcept {
-        _Record.ExceptionCode           = EH_EXCEPTION_NUMBER;
-        _Record.ExceptionFlags          = EXCEPTION_NONCONTINUABLE;
-        _Record.ExceptionRecord         = nullptr; // no SEH to chain
-        _Record.ExceptionAddress        = nullptr; // Address of exception. Will be overwritten by OS
-        _Record.NumberParameters        = EH_EXCEPTION_PARAMETERS;
-        _Record.ExceptionInformation[0] = EH_MAGIC_NUMBER1; // params.magicNumber
-        _Record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(_PExcept); // params.pExceptionObject
-
-        if (_PThrow && (_PThrow->attributes & TI_IsWinRT)) {
-            // The pointer to the ExceptionInfo structure is stored sizeof(void*) in front of each WinRT Exception Info.
-            const auto _PWei = (*static_cast<WINRTEXCEPTIONINFO** const*>(_PExcept))[-1];
-            _PThrow          = _PWei->throwInfo;
-        }
-
-        _Record.ExceptionInformation[2] = reinterpret_cast<ULONG_PTR>(_PThrow); // params.pThrowInfo
-
-#if _EH_RELATIVE_TYPEINFO
-        void* _ThrowImageBase =
-            _PThrow ? RtlPcToFileHeader(const_cast<void*>(static_cast<const void*>(_PThrow)), &_ThrowImageBase)
-                    : nullptr;
-        _Record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(_ThrowImageBase); // params.pThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-
-        // If the throw info indicates this throw is from a pure region,
-        // set the magic number to the Pure one, so only a pure-region
-        // catch will see it.
-        //
-        // Also use the Pure magic number on 64-bit platforms if we were unable to
-        // determine an image base, since that was the old way to determine
-        // a pure throw, before the TI_IsPure bit was added to the FuncInfo
-        // attributes field.
-        if (_PThrow
-            && ((_PThrow->attributes & TI_IsPure)
-#if _EH_RELATIVE_TYPEINFO
-                || !_ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-                )) {
-            _Record.ExceptionInformation[0] = EH_PURE_MAGIC_NUMBER1;
-        }
-    }
-
-    void _CopyExceptionRecord(_EXCEPTION_RECORD& _Dest, const _EXCEPTION_RECORD& _Src) noexcept {
-        _Dest.ExceptionCode = _Src.ExceptionCode;
-        // we force EXCEPTION_NONCONTINUABLE because rethrow_exception is [[noreturn]]
-        _Dest.ExceptionFlags   = _Src.ExceptionFlags | EXCEPTION_NONCONTINUABLE;
-        _Dest.ExceptionRecord  = nullptr; // We don't chain SEH exceptions
-        _Dest.ExceptionAddress = nullptr; // Useless field to copy. It will be overwritten by RaiseException()
-        const auto _Parameters = _Src.NumberParameters;
-        _Dest.NumberParameters = _Parameters;
-
-        // copy the number of parameters in use
-        constexpr auto _Max_parameters = static_cast<DWORD>(EXCEPTION_MAXIMUM_PARAMETERS);
-        const auto _In_use             = (_STD min)(_Parameters, _Max_parameters);
-        _CSTD memcpy(_Dest.ExceptionInformation, _Src.ExceptionInformation, _In_use * sizeof(ULONG_PTR));
-        _CSTD memset(&_Dest.ExceptionInformation[_In_use], 0, (_Max_parameters - _In_use) * sizeof(ULONG_PTR));
-    }
-
-    void _CopyExceptionObject(void* _Dest, const void* _Src, const CatchableType* const _PType
-#if _EH_RELATIVE_TYPEINFO
-        ,
-        const uintptr_t _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-    ) {
-        // copy an object of type denoted by *_PType from _Src to _Dest; throws whatever the copy ctor of the type
-        // denoted by *_PType throws
-        if ((_PType->properties & CT_IsSimpleType) || _PType->copyFunction == 0) {
-            memcpy(_Dest, _Src, _PType->sizeOrOffset);
-
-            if (_PType->properties & CT_IsWinRTHandle) {
-                const auto _PUnknown = *static_cast<IUnknown* const*>(_Src);
-                if (_PUnknown) {
-                    _PUnknown->AddRef();
-                }
-            }
-            return;
-        }
-
-#if _EH_RELATIVE_TYPEINFO
-        const auto _CopyFunc = reinterpret_cast<void*>(_ThrowImageBase + _PType->copyFunction);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _CopyFunc = _PType->copyFunction;
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        const auto _Adjusted = __AdjustPointer(const_cast<void*>(_Src), _PType->thisDisplacement);
-        if (_PType->properties & CT_HasVirtualBase) {
-#ifdef _M_CEE_PURE
-            reinterpret_cast<void(__clrcall*)(void*, void*, int)>(_CopyFunc)(_Dest, _Adjusted, 1);
-#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
-            _CallMemberFunction2(_Dest, _CopyFunc, _Adjusted, 1);
-#endif // ^^^ !defined(_M_CEE_PURE) ^^^
-        } else {
-#ifdef _M_CEE_PURE
-            reinterpret_cast<void(__clrcall*)(void*, void*)>(_CopyFunc)(_Dest, _Adjusted);
-#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
-            _CallMemberFunction1(_Dest, _CopyFunc, _Adjusted);
-#endif // ^^^ !defined(_M_CEE_PURE) ^^^
-        }
-    }
-} // unnamed namespace
-
-// All exception_ptr implementations are out-of-line because <memory> depends on <exception>,
-// which means <exception> cannot include <memory> -- and shared_ptr is defined in <memory>.
-// To workaround this, we created a dummy class exception_ptr, which is structurally identical to shared_ptr.
-
-_STD_BEGIN
-struct _Exception_ptr_access {
-    template <class _Ty, class _Ty2>
-    static void _Set_ptr_rep(_Ptr_base<_Ty>& _This, _Ty2* _Px, _Ref_count_base* _Rx) noexcept {
-        _This._Ptr = _Px;
-        _This._Rep = _Rx;
-    }
-};
-_STD_END
-
-static_assert(sizeof(exception_ptr) == sizeof(shared_ptr<const _EXCEPTION_RECORD>)
-                  && alignof(exception_ptr) == alignof(shared_ptr<const _EXCEPTION_RECORD>),
-    "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
-
-namespace {
-    template <class _StaticEx>
-    class _ExceptionPtr_static final : public _Ref_count_base {
-        // reference count control block for special "never allocates" exceptions like the bad_alloc or bad_exception
-        // exception_ptrs
-    private:
-        void _Destroy() noexcept override {
-            // intentionally does nothing
-        }
-
-        void _Delete_this() noexcept override {
-            // intentionally does nothing
-        }
-
-    public:
-        // constexpr, TRANSITION P1064
-        explicit _ExceptionPtr_static() noexcept : _Ref_count_base() {
-            _PopulateCppExceptionRecord(_ExRecord, &_Ex, static_cast<ThrowInfo*>(__GetExceptionInfo(_Ex)));
-        }
-
-        static shared_ptr<const _EXCEPTION_RECORD> _Get() noexcept {
-            auto& _Instance = _Immortalize<_ExceptionPtr_static>();
-            shared_ptr<const _EXCEPTION_RECORD> _Ret;
-            _Instance._Incref();
-            _Exception_ptr_access::_Set_ptr_rep(_Ret, &_Instance._ExRecord, &_Instance);
-            return _Ret;
-        }
-
-        _EXCEPTION_RECORD _ExRecord;
-        _StaticEx _Ex;
-    };
-
-    class _ExceptionPtr_normal final : public _Ref_count_base {
-        // reference count control block for exception_ptrs; the exception object is stored at
-        // reinterpret_cast<unsigned char*>(this) + sizeof(_ExceptionPtr_normal)
-    private:
-        void _Destroy() noexcept override {
-            // call the destructor for a stored pure or native C++ exception if necessary
-            const auto& _CppEhRecord = reinterpret_cast<EHExceptionRecord&>(_ExRecord);
-
-            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppEhRecord)) {
-                return;
-            }
-
-            const auto _PThrow = _CppEhRecord.params.pThrowInfo;
-            if (!_PThrow) {
-                // No ThrowInfo exists. If this was a C++ exception, something must have corrupted it.
-                _CSTD abort();
-            }
-
-            if (!_CppEhRecord.params.pExceptionObject) {
-                return;
-            }
-
-#if _EH_RELATIVE_TYPEINFO
-            const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_CppEhRecord.params.pThrowImageBase);
-            const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
-                static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
-            const auto _PType = reinterpret_cast<CatchableType*>(
-                static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-            const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-            if (_PThrow->pmfnUnwind) {
-                // The exception was a user defined type with a nontrivial destructor, call it
-#if defined(_M_CEE_PURE)
-                reinterpret_cast<void(__clrcall*)(void*)>(_PThrow->pmfnUnwind)(_CppEhRecord.params.pExceptionObject);
-#elif _EH_RELATIVE_TYPEINFO
-                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject,
-                    reinterpret_cast<void*>(_PThrow->pmfnUnwind + _ThrowImageBase));
-#else // ^^^ _EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) / !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) vvv
-                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject, _PThrow->pmfnUnwind);
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) ^^^
-            } else if (_PType->properties & CT_IsWinRTHandle) {
-                const auto _PUnknown = *static_cast<IUnknown* const*>(_CppEhRecord.params.pExceptionObject);
-                if (_PUnknown) {
-                    _PUnknown->Release();
-                }
-            }
-        }
-
-        void _Delete_this() noexcept override {
-            free(this);
-        }
-
-    public:
-        explicit _ExceptionPtr_normal(const _EXCEPTION_RECORD& _Record) noexcept : _Ref_count_base() {
-            _CopyExceptionRecord(_ExRecord, _Record);
-        }
-
-        _EXCEPTION_RECORD _ExRecord;
-        void* _Unused_alignment_padding{};
-    };
-
-    // We aren't using alignas because this file might be compiled with _M_CEE_PURE
-    static_assert(sizeof(_ExceptionPtr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
-        "Exception in exception_ptr would be constructed with the wrong alignment");
-
-    void _Assign_seh_exception_ptr_from_record(
-        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const _EXCEPTION_RECORD& _Record, void* const _RxRaw) noexcept {
-        // in the memory _RxRaw, constructs a reference count control block for a SEH exception denoted by _Record
-        // if _RxRaw is nullptr, assigns bad_alloc instead
-        if (!_RxRaw) {
-            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
-            return;
-        }
-
-        const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(_Record);
-        _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
-    }
-
-    void _Assign_cpp_exception_ptr_from_record(
-        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const EHExceptionRecord& _Record) noexcept {
-        // construct a reference count control block for the C++ exception recorded by _Record, and bind it to _Dest
-        // if allocating memory for the reference count control block fails, sets _Dest to bad_alloc
-        // if copying the exception object referred to _Record throws, constructs a reference count control block for
-        //      that exception instead
-        // if copying the exception object thrown by copying the original exception object throws, sets _Dest to
-        //      bad_exception
-        const auto _PThrow = _Record.params.pThrowInfo;
-#if _EH_RELATIVE_TYPEINFO
-        const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_Record.params.pThrowImageBase);
-        const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
-            static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
-        const auto _PType = reinterpret_cast<CatchableType*>(
-            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        const auto _ExceptionObjectSize = static_cast<size_t>(_PType->sizeOrOffset);
-        const auto _AllocSize           = sizeof(_ExceptionPtr_normal) + _ExceptionObjectSize;
-        _Analysis_assume_(_AllocSize >= sizeof(_ExceptionPtr_normal));
-        auto _RxRaw = malloc(_AllocSize);
-        if (!_RxRaw) {
-            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
-            return;
-        }
-
-        try {
-            _CopyExceptionObject(static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _Record.params.pExceptionObject, _PType
-#if _EH_RELATIVE_TYPEINFO
-                ,
-                _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-            );
-
-            const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_Record));
-            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
-                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
-            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
-        } catch (...) { // copying the exception object threw an exception
-            const auto& _InnerRecord = *_pCurrentException; // exception thrown by the original exception's copy ctor
-            if (_InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE
-                || _InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
-                // we don't support managed exceptions and don't want to say there's no active exception, so give up and
-                // say bad_exception
-                free(_RxRaw);
-                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
-                return;
-            }
-
-            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_InnerRecord)) { // catching a non-C++ exception depends on /EHa
-                _Assign_seh_exception_ptr_from_record(
-                    _Dest, reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord), _RxRaw);
-                return;
-            }
-
-            const auto _PInnerThrow = _InnerRecord.params.pThrowInfo;
-#if _EH_RELATIVE_TYPEINFO
-            const auto _InnerThrowImageBase     = reinterpret_cast<uintptr_t>(_InnerRecord.params.pThrowImageBase);
-            const auto _InnerCatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
-                static_cast<uintptr_t>(_PInnerThrow->pCatchableTypeArray) + _InnerThrowImageBase);
-            const auto _PInnerType = reinterpret_cast<CatchableType*>(
-                static_cast<uintptr_t>(_InnerCatchableTypeArray->arrayOfCatchableTypes[0]) + _InnerThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-            const auto _PInnerType = _PInnerThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-            const auto _InnerExceptionSize = static_cast<size_t>(_PInnerType->sizeOrOffset);
-            const auto _InnerAllocSize     = sizeof(_ExceptionPtr_normal) + _InnerExceptionSize;
-            if (_InnerAllocSize > _AllocSize) {
-                free(_RxRaw);
-                _RxRaw = malloc(_InnerAllocSize);
-                if (!_RxRaw) {
-                    _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
-                    return;
-                }
-            }
-
-            try {
-                _CopyExceptionObject(
-                    static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _InnerRecord.params.pExceptionObject, _PInnerType
-#if _EH_RELATIVE_TYPEINFO
-                    ,
-                    _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-                );
-            } catch (...) { // copying the exception emitted while copying the original exception also threw, give up
-                free(_RxRaw);
-                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
-                return;
-            }
-
-            // this next block must be duplicated inside the catch (even though it looks identical to the block in the
-            // try) so that _InnerRecord is held alive; exiting the catch will destroy it
-            const auto _Rx =
-                ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord));
-            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
-                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
-            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
-        }
-    }
-} // unnamed namespace
-
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void* _Ptr) noexcept {
-    ::new (_Ptr) shared_ptr<const _EXCEPTION_RECORD>();
-}
-
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void* _Ptr) noexcept {
-    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr)->~shared_ptr<const _EXCEPTION_RECORD>();
-}
-
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void* _Dest, _In_ const void* _Src) noexcept {
-    ::new (_Dest) shared_ptr<const _EXCEPTION_RECORD>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src));
-}
-
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void* _Dest, _In_ const void* _Src) noexcept {
-    *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Dest) =
-        *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src);
-}
-
-_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
-    _In_ const void* _Lhs, _In_ const void* _Rhs) noexcept {
-    return *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)
-        == *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs);
-}
-
-_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void* _Ptr) noexcept {
-    return static_cast<bool>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr));
-}
-
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void* _Lhs, _Inout_ void* _Rhs) noexcept {
-    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)->swap(
-        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs));
-}
-
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void* _Ptr) noexcept {
-    const auto _PRecord = _pCurrentException; // nontrivial FLS cost, pay it once
-    if (!_PRecord || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE
-        || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
-        return; // no current exception, or we don't support managed exceptions
-    }
-
-    auto& _Dest = *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr);
-    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(_PRecord)) {
-        _Assign_cpp_exception_ptr_from_record(_Dest, *_PRecord);
-    } else {
-        // _Assign_seh_exception_ptr_from_record handles failed malloc
-        _Assign_seh_exception_ptr_from_record(
-            _Dest, reinterpret_cast<_EXCEPTION_RECORD&>(*_PRecord), malloc(sizeof(_ExceptionPtr_normal)));
-    }
-}
-
-[[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void* _PtrRaw) {
-    const shared_ptr<const _EXCEPTION_RECORD>* _Ptr = static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_PtrRaw);
-    // throwing a bad_exception if they give us a nullptr exception_ptr
-    if (!*_Ptr) {
-        throw bad_exception();
-    }
-
-    auto _RecordCopy = **_Ptr;
-    auto& _CppRecord = reinterpret_cast<EHExceptionRecord&>(_RecordCopy);
-    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppRecord)) {
-        // This is a C++ exception.
-        // We need to build the exception on the stack because the current exception mechanism assumes the exception
-        // object is on the stack and will call the appropriate destructor (if there's a nontrivial one).
-        const auto _PThrow = _CppRecord.params.pThrowInfo;
-        if (!_CppRecord.params.pExceptionObject || !_PThrow || !_PThrow->pCatchableTypeArray) {
-            // Missing or corrupt ThrowInfo. If this was a C++ exception, something must have corrupted it.
-            _CSTD abort();
-        }
-
-#if _EH_RELATIVE_TYPEINFO
-        const auto _ThrowImageBase = reinterpret_cast<uintptr_t>(_CppRecord.params.pThrowImageBase);
-        const auto _CatchableTypeArray =
-            reinterpret_cast<const CatchableTypeArray*>(_ThrowImageBase + _PThrow->pCatchableTypeArray);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _CatchableTypeArray = _PThrow->pCatchableTypeArray;
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        if (_CatchableTypeArray->nCatchableTypes <= 0) {
-            // Ditto corrupted.
-            _CSTD abort();
-        }
-
-        // we finally got the type info we want
-#if _EH_RELATIVE_TYPEINFO
-        const auto _PType = reinterpret_cast<CatchableType*>(
-            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        // Alloc memory on stack for exception object. This might cause a stack overflow SEH exception, or another C++
-        // exception when copying the C++ exception object. In that case, we just let that become the thrown exception.
-
-#pragma warning(suppress : 6255) //  _alloca indicates failure by raising a stack overflow exception
-        void* _PExceptionBuffer = alloca(_PType->sizeOrOffset);
-        _CopyExceptionObject(_PExceptionBuffer, _CppRecord.params.pExceptionObject, _PType
-#if _EH_RELATIVE_TYPEINFO
-            ,
-            _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-        );
-
-        _CppRecord.params.pExceptionObject = _PExceptionBuffer;
-    } else {
-        // this is a SEH exception, no special handling is required
-    }
-
-    _Analysis_assume_(_RecordCopy.NumberParameters <= EXCEPTION_MAXIMUM_PARAMETERS);
-    RaiseException(_RecordCopy.ExceptionCode, _RecordCopy.ExceptionFlags, _RecordCopy.NumberParameters,
-        _RecordCopy.ExceptionInformation);
-}
-
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
-    _Inout_ void* _Ptr, _In_ const void* _PExceptRaw, _In_ const void* _PThrowRaw) noexcept {
-    _EXCEPTION_RECORD _Record;
-    _PopulateCppExceptionRecord(_Record, _PExceptRaw, static_cast<ThrowInfo*>(_PThrowRaw));
-    _Assign_cpp_exception_ptr_from_record(
-        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr), reinterpret_cast<const EHExceptionRecord&>(_Record));
-}
+// Copyright (c) Microsoft Corporation.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+// This implementation communicates with the EH runtime though vcruntime's per-thread-data structure; see
+// _pCurrentException in <trnsctrl.h>.
+//
+// As a result, normal EH runtime services (such as noexcept functions) are safe to use in this file.
+
+#ifndef _VCRT_ALLOW_INTERNALS
+#define _VCRT_ALLOW_INTERNALS
+#endif // !defined(_VCRT_ALLOW_INTERNALS)
+
+#include <Unknwn.h>
+#include <cstdlib> // for abort
+#include <cstring> // for memcpy
+#include <eh.h>
+#include <ehdata.h>
+#include <exception>
+#include <internal_shared.h>
+#include <malloc.h>
+#include <memory>
+#include <new>
+#include <stdexcept>
+#include <trnsctrl.h>
+#include <xcall_once.h>
+
+#include <Windows.h>
+
+// Pre-V4 managed exception code
+#define MANAGED_EXCEPTION_CODE 0XE0434F4D
+
+// V4 and later managed exception code
+#define MANAGED_EXCEPTION_CODE_V4 0XE0434352
+
+extern "C" _CRTIMP2 void* __cdecl __AdjustPointer(void*, const PMD&); // defined in frame.cpp
+
+using namespace std;
+
+namespace {
+#if defined(_M_CEE_PURE)
+    template <class _Ty>
+    _Ty& _Immortalize() { // return a reference to an object that will live forever
+        /* MAGIC */ static _Immortalizer_impl<_Ty> _Static;
+        return reinterpret_cast<_Ty&>(_Static._Storage);
+    }
+#elif !defined(_M_CEE)
+    template <class _Ty>
+    struct _Constexpr_excptptr_immortalize_impl {
+        union {
+            _Ty _Storage;
+        };
+
+        constexpr _Constexpr_excptptr_immortalize_impl() noexcept : _Storage{} {}
+
+        _Constexpr_excptptr_immortalize_impl(const _Constexpr_excptptr_immortalize_impl&)            = delete;
+        _Constexpr_excptptr_immortalize_impl& operator=(const _Constexpr_excptptr_immortalize_impl&) = delete;
+
+        _MSVC_NOOP_DTOR ~_Constexpr_excptptr_immortalize_impl() {
+            // do nothing, allowing _Ty to be used during shutdown
+        }
+    };
+
+    template <class _Ty>
+    _Constexpr_excptptr_immortalize_impl<_Ty> _Immortalize_impl;
+
+    template <class _Ty>
+    [[nodiscard]] _Ty& _Immortalize() noexcept {
+        return _Immortalize_impl<_Ty>._Storage;
+    }
+#else // ^^^ !defined(_M_CEE) / defined(_M_CEE), TRANSITION, VSO-1153256 vvv
+    template <class _Ty>
+    _Ty& _Immortalize() { // return a reference to an object that will live forever
+        static once_flag _Flag;
+        alignas(_Ty) static unsigned char _Storage[sizeof(_Ty)];
+        call_once(_Flag, [&_Storage] { ::new (static_cast<void*>(&_Storage)) _Ty(); });
+        return reinterpret_cast<_Ty&>(_Storage);
+    }
+#endif // ^^^ !defined(_M_CEE_PURE) && defined(_M_CEE), TRANSITION, VSO-1153256 ^^^
+
+    void _PopulateCppExceptionRecord(
+        _EXCEPTION_RECORD& _Record, const void* const _PExcept, ThrowInfo* _PThrow) noexcept {
+        _Record.ExceptionCode           = EH_EXCEPTION_NUMBER;
+        _Record.ExceptionFlags          = EXCEPTION_NONCONTINUABLE;
+        _Record.ExceptionRecord         = nullptr; // no SEH to chain
+        _Record.ExceptionAddress        = nullptr; // Address of exception. Will be overwritten by OS
+        _Record.NumberParameters        = EH_EXCEPTION_PARAMETERS;
+        _Record.ExceptionInformation[0] = EH_MAGIC_NUMBER1; // params.magicNumber
+        _Record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(_PExcept); // params.pExceptionObject
+
+        if (_PThrow && (_PThrow->attributes & TI_IsWinRT)) {
+            // The pointer to the ExceptionInfo structure is stored sizeof(void*) in front of each WinRT Exception Info.
+            const auto _PWei = (*static_cast<WINRTEXCEPTIONINFO** const*>(_PExcept))[-1];
+            _PThrow          = _PWei->throwInfo;
+        }
+
+        _Record.ExceptionInformation[2] = reinterpret_cast<ULONG_PTR>(_PThrow); // params.pThrowInfo
+
+#if _EH_RELATIVE_TYPEINFO
+        void* _ThrowImageBase =
+            _PThrow ? RtlPcToFileHeader(const_cast<void*>(static_cast<const void*>(_PThrow)), &_ThrowImageBase)
+                    : nullptr;
+        _Record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(_ThrowImageBase); // params.pThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+
+        // If the throw info indicates this throw is from a pure region,
+        // set the magic number to the Pure one, so only a pure-region
+        // catch will see it.
+        //
+        // Also use the Pure magic number on 64-bit platforms if we were unable to
+        // determine an image base, since that was the old way to determine
+        // a pure throw, before the TI_IsPure bit was added to the FuncInfo
+        // attributes field.
+        if (_PThrow
+            && ((_PThrow->attributes & TI_IsPure)
+#if _EH_RELATIVE_TYPEINFO
+                || !_ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+                )) {
+            _Record.ExceptionInformation[0] = EH_PURE_MAGIC_NUMBER1;
+        }
+    }
+
+    void _CopyExceptionRecord(_EXCEPTION_RECORD& _Dest, const _EXCEPTION_RECORD& _Src) noexcept {
+        _Dest.ExceptionCode = _Src.ExceptionCode;
+        // we force EXCEPTION_NONCONTINUABLE because rethrow_exception is [[noreturn]]
+        _Dest.ExceptionFlags   = _Src.ExceptionFlags | EXCEPTION_NONCONTINUABLE;
+        _Dest.ExceptionRecord  = nullptr; // We don't chain SEH exceptions
+        _Dest.ExceptionAddress = nullptr; // Useless field to copy. It will be overwritten by RaiseException()
+        const auto _Parameters = _Src.NumberParameters;
+        _Dest.NumberParameters = _Parameters;
+
+        // copy the number of parameters in use
+        constexpr auto _Max_parameters = static_cast<DWORD>(EXCEPTION_MAXIMUM_PARAMETERS);
+        const auto _In_use             = (_STD min)(_Parameters, _Max_parameters);
+        _CSTD memcpy(_Dest.ExceptionInformation, _Src.ExceptionInformation, _In_use * sizeof(ULONG_PTR));
+        _CSTD memset(&_Dest.ExceptionInformation[_In_use], 0, (_Max_parameters - _In_use) * sizeof(ULONG_PTR));
+    }
+
+    void _CopyExceptionObject(void* _Dest, const void* _Src, const CatchableType* const _PType
+#if _EH_RELATIVE_TYPEINFO
+        ,
+        const uintptr_t _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+    ) {
+        // copy an object of type denoted by *_PType from _Src to _Dest; throws whatever the copy ctor of the type
+        // denoted by *_PType throws
+        if ((_PType->properties & CT_IsSimpleType) || _PType->copyFunction == 0) {
+            memcpy(_Dest, _Src, _PType->sizeOrOffset);
+
+            if (_PType->properties & CT_IsWinRTHandle) {
+                const auto _PUnknown = *static_cast<IUnknown* const*>(_Src);
+                if (_PUnknown) {
+                    _PUnknown->AddRef();
+                }
+            }
+            return;
+        }
+
+#if _EH_RELATIVE_TYPEINFO
+        const auto _CopyFunc = reinterpret_cast<void*>(_ThrowImageBase + _PType->copyFunction);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _CopyFunc = _PType->copyFunction;
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        const auto _Adjusted = __AdjustPointer(const_cast<void*>(_Src), _PType->thisDisplacement);
+        if (_PType->properties & CT_HasVirtualBase) {
+#ifdef _M_CEE_PURE
+            reinterpret_cast<void(__clrcall*)(void*, void*, int)>(_CopyFunc)(_Dest, _Adjusted, 1);
+#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
+            _CallMemberFunction2(_Dest, _CopyFunc, _Adjusted, 1);
+#endif // ^^^ !defined(_M_CEE_PURE) ^^^
+        } else {
+#ifdef _M_CEE_PURE
+            reinterpret_cast<void(__clrcall*)(void*, void*)>(_CopyFunc)(_Dest, _Adjusted);
+#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
+            _CallMemberFunction1(_Dest, _CopyFunc, _Adjusted);
+#endif // ^^^ !defined(_M_CEE_PURE) ^^^
+        }
+    }
+} // unnamed namespace
+
+// All exception_ptr implementations are out-of-line because <memory> depends on <exception>,
+// which means <exception> cannot include <memory> -- and shared_ptr is defined in <memory>.
+// To workaround this, we created a dummy class exception_ptr, which is structurally identical to shared_ptr.
+
+_STD_BEGIN
+struct _Exception_ptr_access {
+    template <class _Ty, class _Ty2>
+    static void _Set_ptr_rep(_Ptr_base<_Ty>& _This, _Ty2* _Px, _Ref_count_base* _Rx) noexcept {
+        _This._Ptr = _Px;
+        _This._Rep = _Rx;
+    }
+};
+_STD_END
+
+static_assert(sizeof(exception_ptr) == sizeof(shared_ptr<const _EXCEPTION_RECORD>)
+                  && alignof(exception_ptr) == alignof(shared_ptr<const _EXCEPTION_RECORD>),
+    "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
+
+namespace {
+    template <class _StaticEx>
+    class _ExceptionPtr_static final : public _Ref_count_base {
+        // reference count control block for special "never allocates" exceptions like the bad_alloc or bad_exception
+        // exception_ptrs
+    private:
+        void _Destroy() noexcept override {
+            // intentionally does nothing
+        }
+
+        void _Delete_this() noexcept override {
+            // intentionally does nothing
+        }
+
+    public:
+        // constexpr, TRANSITION P1064
+        explicit _ExceptionPtr_static() noexcept : _Ref_count_base() {
+            _PopulateCppExceptionRecord(_ExRecord, &_Ex, static_cast<ThrowInfo*>(__GetExceptionInfo(_Ex)));
+        }
+
+        static shared_ptr<const _EXCEPTION_RECORD> _Get() noexcept {
+            auto& _Instance = _Immortalize<_ExceptionPtr_static>();
+            shared_ptr<const _EXCEPTION_RECORD> _Ret;
+            _Instance._Incref();
+            _Exception_ptr_access::_Set_ptr_rep(_Ret, &_Instance._ExRecord, &_Instance);
+            return _Ret;
+        }
+
+        _EXCEPTION_RECORD _ExRecord;
+        _StaticEx _Ex;
+    };
+
+    class _ExceptionPtr_normal final : public _Ref_count_base {
+        // reference count control block for exception_ptrs; the exception object is stored at
+        // reinterpret_cast<unsigned char*>(this) + sizeof(_ExceptionPtr_normal)
+    private:
+        void _Destroy() noexcept override {
+            // call the destructor for a stored pure or native C++ exception if necessary
+            const auto& _CppEhRecord = reinterpret_cast<EHExceptionRecord&>(_ExRecord);
+
+            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppEhRecord)) {
+                return;
+            }
+
+            const auto _PThrow = _CppEhRecord.params.pThrowInfo;
+            if (!_PThrow) {
+                // No ThrowInfo exists. If this was a C++ exception, something must have corrupted it.
+                _CSTD abort();
+            }
+
+            if (!_CppEhRecord.params.pExceptionObject) {
+                return;
+            }
+
+#if _EH_RELATIVE_TYPEINFO
+            const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_CppEhRecord.params.pThrowImageBase);
+            const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
+                static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
+            const auto _PType = reinterpret_cast<CatchableType*>(
+                static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+            const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+            if (_PThrow->pmfnUnwind) {
+                // The exception was a user defined type with a nontrivial destructor, call it
+#if defined(_M_CEE_PURE)
+                reinterpret_cast<void(__clrcall*)(void*)>(_PThrow->pmfnUnwind)(_CppEhRecord.params.pExceptionObject);
+#elif _EH_RELATIVE_TYPEINFO
+                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject,
+                    reinterpret_cast<void*>(_PThrow->pmfnUnwind + _ThrowImageBase));
+#else // ^^^ _EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) / !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) vvv
+                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject, _PThrow->pmfnUnwind);
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) ^^^
+            } else if (_PType->properties & CT_IsWinRTHandle) {
+                const auto _PUnknown = *static_cast<IUnknown* const*>(_CppEhRecord.params.pExceptionObject);
+                if (_PUnknown) {
+                    _PUnknown->Release();
+                }
+            }
+        }
+
+        void _Delete_this() noexcept override {
+            free(this);
+        }
+
+    public:
+        explicit _ExceptionPtr_normal(const _EXCEPTION_RECORD& _Record) noexcept : _Ref_count_base() {
+            _CopyExceptionRecord(_ExRecord, _Record);
+        }
+
+        _EXCEPTION_RECORD _ExRecord;
+        void* _Unused_alignment_padding{};
+    };
+
+    // We aren't using alignas because this file might be compiled with _M_CEE_PURE
+    static_assert(sizeof(_ExceptionPtr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
+        "Exception in exception_ptr would be constructed with the wrong alignment");
+
+    void _Assign_seh_exception_ptr_from_record(
+        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const _EXCEPTION_RECORD& _Record, void* const _RxRaw) noexcept {
+        // in the memory _RxRaw, constructs a reference count control block for a SEH exception denoted by _Record
+        // if _RxRaw is nullptr, assigns bad_alloc instead
+        if (!_RxRaw) {
+            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
+            return;
+        }
+
+        const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(_Record);
+        _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
+    }
+
+    void _Assign_cpp_exception_ptr_from_record(
+        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const EHExceptionRecord& _Record) noexcept {
+        // construct a reference count control block for the C++ exception recorded by _Record, and bind it to _Dest
+        // if allocating memory for the reference count control block fails, sets _Dest to bad_alloc
+        // if copying the exception object referred to _Record throws, constructs a reference count control block for
+        //      that exception instead
+        // if copying the exception object thrown by copying the original exception object throws, sets _Dest to
+        //      bad_exception
+        const auto _PThrow = _Record.params.pThrowInfo;
+#if _EH_RELATIVE_TYPEINFO
+        const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_Record.params.pThrowImageBase);
+        const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
+            static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
+        const auto _PType = reinterpret_cast<CatchableType*>(
+            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        const auto _ExceptionObjectSize = static_cast<size_t>(_PType->sizeOrOffset);
+        const auto _AllocSize           = sizeof(_ExceptionPtr_normal) + _ExceptionObjectSize;
+        _Analysis_assume_(_AllocSize >= sizeof(_ExceptionPtr_normal));
+        auto _RxRaw = malloc(_AllocSize);
+        if (!_RxRaw) {
+            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
+            return;
+        }
+
+        try {
+            _CopyExceptionObject(static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _Record.params.pExceptionObject, _PType
+#if _EH_RELATIVE_TYPEINFO
+                ,
+                _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+            );
+
+            const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_Record));
+            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
+                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
+            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
+        } catch (...) { // copying the exception object threw an exception
+            const auto& _InnerRecord = *_pCurrentException; // exception thrown by the original exception's copy ctor
+            if (_InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE
+                || _InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+                // we don't support managed exceptions and don't want to say there's no active exception, so give up and
+                // say bad_exception
+                free(_RxRaw);
+                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
+                return;
+            }
+
+            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_InnerRecord)) { // catching a non-C++ exception depends on /EHa
+                _Assign_seh_exception_ptr_from_record(
+                    _Dest, reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord), _RxRaw);
+                return;
+            }
+
+            const auto _PInnerThrow = _InnerRecord.params.pThrowInfo;
+#if _EH_RELATIVE_TYPEINFO
+            const auto _InnerThrowImageBase     = reinterpret_cast<uintptr_t>(_InnerRecord.params.pThrowImageBase);
+            const auto _InnerCatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
+                static_cast<uintptr_t>(_PInnerThrow->pCatchableTypeArray) + _InnerThrowImageBase);
+            const auto _PInnerType = reinterpret_cast<CatchableType*>(
+                static_cast<uintptr_t>(_InnerCatchableTypeArray->arrayOfCatchableTypes[0]) + _InnerThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+            const auto _PInnerType = _PInnerThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+            const auto _InnerExceptionSize = static_cast<size_t>(_PInnerType->sizeOrOffset);
+            const auto _InnerAllocSize     = sizeof(_ExceptionPtr_normal) + _InnerExceptionSize;
+            if (_InnerAllocSize > _AllocSize) {
+                free(_RxRaw);
+                _RxRaw = malloc(_InnerAllocSize);
+                if (!_RxRaw) {
+                    _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
+                    return;
+                }
+            }
+
+            try {
+                _CopyExceptionObject(
+                    static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _InnerRecord.params.pExceptionObject, _PInnerType
+#if _EH_RELATIVE_TYPEINFO
+                    ,
+                    _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+                );
+            } catch (...) { // copying the exception emitted while copying the original exception also threw, give up
+                free(_RxRaw);
+                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
+                return;
+            }
+
+            // this next block must be duplicated inside the catch (even though it looks identical to the block in the
+            // try) so that _InnerRecord is held alive; exiting the catch will destroy it
+            const auto _Rx =
+                ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord));
+            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
+                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
+            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
+        }
+    }
+} // unnamed namespace
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void* _Ptr) noexcept {
+    ::new (_Ptr) shared_ptr<const _EXCEPTION_RECORD>();
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void* _Ptr) noexcept {
+    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr)->~shared_ptr<const _EXCEPTION_RECORD>();
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void* _Dest, _In_ const void* _Src) noexcept {
+    ::new (_Dest) shared_ptr<const _EXCEPTION_RECORD>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src));
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void* _Dest, _In_ const void* _Src) noexcept {
+    *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Dest) =
+        *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src);
+}
+
+_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
+    _In_ const void* _Lhs, _In_ const void* _Rhs) noexcept {
+    return *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)
+        == *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs);
+}
+
+_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void* _Ptr) noexcept {
+    return static_cast<bool>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr));
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void* _Lhs, _Inout_ void* _Rhs) noexcept {
+    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)->swap(
+        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs));
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void* _Ptr) noexcept {
+    const auto _PRecord = _pCurrentException; // nontrivial FLS cost, pay it once
+    if (!_PRecord || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE
+        || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+        return; // no current exception, or we don't support managed exceptions
+    }
+
+    auto& _Dest = *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr);
+    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(_PRecord)) {
+        _Assign_cpp_exception_ptr_from_record(_Dest, *_PRecord);
+    } else {
+        // _Assign_seh_exception_ptr_from_record handles failed malloc
+        _Assign_seh_exception_ptr_from_record(
+            _Dest, reinterpret_cast<_EXCEPTION_RECORD&>(*_PRecord), malloc(sizeof(_ExceptionPtr_normal)));
+    }
+}
+
+[[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void* _PtrRaw) {
+    const shared_ptr<const _EXCEPTION_RECORD>* _Ptr = static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_PtrRaw);
+    // throwing a bad_exception if they give us a nullptr exception_ptr
+    if (!*_Ptr) {
+        throw bad_exception();
+    }
+
+    auto _RecordCopy = **_Ptr;
+    auto& _CppRecord = reinterpret_cast<EHExceptionRecord&>(_RecordCopy);
+    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppRecord)) {
+        // This is a C++ exception.
+        // We need to build the exception on the stack because the current exception mechanism assumes the exception
+        // object is on the stack and will call the appropriate destructor (if there's a nontrivial one).
+        const auto _PThrow = _CppRecord.params.pThrowInfo;
+        if (!_CppRecord.params.pExceptionObject || !_PThrow || !_PThrow->pCatchableTypeArray) {
+            // Missing or corrupt ThrowInfo. If this was a C++ exception, something must have corrupted it.
+            _CSTD abort();
+        }
+
+#if _EH_RELATIVE_TYPEINFO
+        const auto _ThrowImageBase = reinterpret_cast<uintptr_t>(_CppRecord.params.pThrowImageBase);
+        const auto _CatchableTypeArray =
+            reinterpret_cast<const CatchableTypeArray*>(_ThrowImageBase + _PThrow->pCatchableTypeArray);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _CatchableTypeArray = _PThrow->pCatchableTypeArray;
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        if (_CatchableTypeArray->nCatchableTypes <= 0) {
+            // Ditto corrupted.
+            _CSTD abort();
+        }
+
+        // we finally got the type info we want
+#if _EH_RELATIVE_TYPEINFO
+        const auto _PType = reinterpret_cast<CatchableType*>(
+            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
+#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
+        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
+
+        // Alloc memory on stack for exception object. This might cause a stack overflow SEH exception, or another C++
+        // exception when copying the C++ exception object. In that case, we just let that become the thrown exception.
+
+#pragma warning(suppress : 6255) //  _alloca indicates failure by raising a stack overflow exception
+        void* _PExceptionBuffer = alloca(_PType->sizeOrOffset);
+        _CopyExceptionObject(_PExceptionBuffer, _CppRecord.params.pExceptionObject, _PType
+#if _EH_RELATIVE_TYPEINFO
+            ,
+            _ThrowImageBase
+#endif // _EH_RELATIVE_TYPEINFO
+        );
+
+        _CppRecord.params.pExceptionObject = _PExceptionBuffer;
+    } else {
+        // this is a SEH exception, no special handling is required
+    }
+
+    _Analysis_assume_(_RecordCopy.NumberParameters <= EXCEPTION_MAXIMUM_PARAMETERS);
+    RaiseException(_RecordCopy.ExceptionCode, _RecordCopy.ExceptionFlags, _RecordCopy.NumberParameters,
+        _RecordCopy.ExceptionInformation);
+}
+
+_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
+    _Inout_ void* _Ptr, _In_ const void* _PExceptRaw, _In_ const void* _PThrowRaw) noexcept {
+    _EXCEPTION_RECORD _Record;
+    _PopulateCppExceptionRecord(_Record, _PExceptRaw, static_cast<ThrowInfo*>(_PThrowRaw));
+    _Assign_cpp_exception_ptr_from_record(
+        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr), reinterpret_cast<const EHExceptionRecord&>(_Record));
+}

>From 1e535c2abbb993b1723ab1e13b6720a57ddd0f2f Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at microsoft.com>
Date: Mon, 10 Jun 2024 05:07:55 -0700
Subject: [PATCH 04/10] Add LLVM banners.

---
 libcxx/include/__exception/win32_exception_ptr.h | 8 ++++++++
 libcxx/src/support/win32/excptptr.cpp            | 8 ++++++++
 2 files changed, 16 insertions(+)

diff --git a/libcxx/include/__exception/win32_exception_ptr.h b/libcxx/include/__exception/win32_exception_ptr.h
index efadca1b733d0..ec92762751822 100644
--- a/libcxx/include/__exception/win32_exception_ptr.h
+++ b/libcxx/include/__exception/win32_exception_ptr.h
@@ -1,3 +1,11 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
 // exception standard header
 
 // Copyright (c) Microsoft Corporation.
diff --git a/libcxx/src/support/win32/excptptr.cpp b/libcxx/src/support/win32/excptptr.cpp
index a78d1528b0862..a73c33cb1c7d7 100644
--- a/libcxx/src/support/win32/excptptr.cpp
+++ b/libcxx/src/support/win32/excptptr.cpp
@@ -1,3 +1,11 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
 // Copyright (c) Microsoft Corporation.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 

>From 08801a4877bbf426cc3b6026540eeb09475ea463 Mon Sep 17 00:00:00 2001
From: "Stephan T. Lavavej" <stl at microsoft.com>
Date: Mon, 10 Jun 2024 04:58:42 -0700
Subject: [PATCH 05/10] Update `libcxx/CREDITS.TXT` to mention Microsoft.

I am intentionally not crediting myself here,
I'm just delivering the pizza, I didn't cook it.
---
 libcxx/CREDITS.TXT | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/CREDITS.TXT b/libcxx/CREDITS.TXT
index aa3c8cf1a8c6e..6ed18072de9a5 100644
--- a/libcxx/CREDITS.TXT
+++ b/libcxx/CREDITS.TXT
@@ -98,7 +98,7 @@ E: lebrungrandt at ornl.gov
 D: Implementation of mdspan.
 
 N: Microsoft Corporation
-D: Contributed floating-point to_chars.
+D: Contributed floating-point to_chars and an implementation of exception_ptr for Windows.
 
 N: Bruce Mitchener, Jr.
 E: bruce.mitchener at gmail.com

>From 3d069bd93c4f02c266516ad24f031bb1e70e2233 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sun, 21 Jun 2026 05:02:51 +0000
Subject: [PATCH 06/10] Integrate Microsoft STL exception_ptr implementation
 into libc++

* Deleted libcxx/include/__exception/win32_exception_ptr.h (which was a
  verbatim copy of Microsoft STL's <exception> header). All standard
  exception classes (exception, bad_alloc, bad_exception,
  nested_exception, etc.) are already natively provided by libc++'s
  standard headers.
* Replaced MSVC STL naming conventions and macros (_STD, _CRTIMP2_PURE,
  _Ref_count_base, _Ptr_base, _EXCEPTION_RECORD, etc.) with standard
  libc++ style and identifiers (_LIBCPP_EXPORTED_FROM_ABI,
  std::__shared_weak_count, __double_leading_underscore
  variable/function names).
* Rewrote reference count control blocks (__exception_ptr_static and
  __exception_ptr_normal) to inherit from std::__shared_weak_count.
* Replaced internal MSVC CRT headers (<trnsctrl.h>, <internal_shared.h>,
  <xcall_once.h>) with portable standard C++ constructs (e.g., standard
  C++ function-local static initialization for immortal static exception
  objects) and inline pointer-to-member invocation helpers.
* Guaranteed 16-byte heap allocation alignment for
  __exception_ptr_normal using standard
  alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__).
---
 .../include/__exception/win32_exception_ptr.h | 430 ---------
 libcxx/src/CMakeLists.txt                     |   1 +
 .../runtime/exception_pointer_msvc.ipp        |  24 +-
 libcxx/src/support/win32/excptptr.cpp         | 847 ++++++++----------
 4 files changed, 400 insertions(+), 902 deletions(-)
 delete mode 100644 libcxx/include/__exception/win32_exception_ptr.h

diff --git a/libcxx/include/__exception/win32_exception_ptr.h b/libcxx/include/__exception/win32_exception_ptr.h
deleted file mode 100644
index ec92762751822..0000000000000
--- a/libcxx/include/__exception/win32_exception_ptr.h
+++ /dev/null
@@ -1,430 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// exception standard header
-
-// Copyright (c) Microsoft Corporation.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-#ifndef _EXCEPTION_
-#define _EXCEPTION_
-#include <yvals.h>
-#if _STL_COMPILER_PREPROCESSOR
-
-#include <cstdlib>
-#include <type_traits>
-
-#pragma pack(push, _CRT_PACKING)
-#pragma warning(push, _STL_WARNING_LEVEL)
-#pragma warning(disable : _STL_DISABLED_WARNINGS)
-_STL_DISABLE_CLANG_WARNINGS
-#pragma push_macro("new")
-#undef new
-
-_STD_BEGIN
-
-#if _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
-_EXPORT_STD extern "C++" _CXX17_DEPRECATE_UNCAUGHT_EXCEPTION _NODISCARD _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL
-    uncaught_exception() noexcept;
-#endif // _HAS_DEPRECATED_UNCAUGHT_EXCEPTION
-_EXPORT_STD extern "C++" _NODISCARD _CRTIMP2_PURE int __CLRCALL_PURE_OR_CDECL uncaught_exceptions() noexcept;
-
-_STD_END
-
-#if _HAS_EXCEPTIONS
-
-#include <malloc.h> // TRANSITION, VSO-2048380: This is unnecessary, but many projects assume it as of 2024-04-29
-#include <vcruntime_exception.h>
-
-_STD_BEGIN
-
-_EXPORT_STD using ::terminate;
-
-#ifndef _M_CEE_PURE
-_EXPORT_STD using ::set_terminate;
-_EXPORT_STD using ::terminate_handler;
-
-_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
-    // get current terminate handler
-    return _get_terminate();
-}
-#endif // !defined(_M_CEE_PURE)
-
-#if _HAS_UNEXPECTED
-using ::unexpected;
-
-#ifndef _M_CEE_PURE
-using ::set_unexpected;
-using ::unexpected_handler;
-
-_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
-    // get current unexpected handler
-    return _get_unexpected();
-}
-#endif // !defined(_M_CEE_PURE)
-#endif // _HAS_UNEXPECTED
-
-_STD_END
-
-#else // ^^^ _HAS_EXCEPTIONS / !_HAS_EXCEPTIONS vvv
-
-#pragma push_macro("stdext")
-#undef stdext
-
-_STDEXT_BEGIN
-class exception;
-_STDEXT_END
-
-_STD_BEGIN
-
-_EXPORT_STD using _STDEXT exception;
-
-using _Prhand = void(__cdecl*)(const exception&);
-
-extern _CRTIMP2_PURE_IMPORT _Prhand _Raise_handler; // pointer to raise handler
-
-_STD_END
-
-_STDEXT_BEGIN
-class exception { // base of all library exceptions
-public:
-    static _STD _Prhand _Set_raise_handler(_STD _Prhand _Pnew) { // register a handler for _Raise calls
-        const _STD _Prhand _Pold = _STD _Raise_handler;
-        _STD _Raise_handler      = _Pnew;
-        return _Pold;
-    }
-
-    // this constructor is necessary to compile
-    // successfully header new for _HAS_EXCEPTIONS==0 scenario
-    explicit __CLR_OR_THIS_CALL exception(const char* _Message = "unknown", int = 1) noexcept : _Ptr(_Message) {}
-
-    __CLR_OR_THIS_CALL exception(const exception& _Right) noexcept : _Ptr(_Right._Ptr) {}
-
-    exception& __CLR_OR_THIS_CALL operator=(const exception& _Right) noexcept {
-        _Ptr = _Right._Ptr;
-        return *this;
-    }
-
-    virtual __CLR_OR_THIS_CALL ~exception() noexcept {}
-
-    _NODISCARD virtual const char* __CLR_OR_THIS_CALL what() const noexcept { // return pointer to message string
-        return _Ptr ? _Ptr : "unknown exception";
-    }
-
-    [[noreturn]] void __CLR_OR_THIS_CALL _Raise() const { // raise the exception
-        if (_STD _Raise_handler) {
-            (*_STD _Raise_handler)(*this); // call raise handler if present
-        }
-
-        _Doraise(); // call the protected virtual
-        _RAISE(*this); // raise this exception
-    }
-
-protected:
-    virtual void __CLR_OR_THIS_CALL _Doraise() const {} // perform class-specific exception handling
-
-    const char* _Ptr; // the message pointer
-};
-
-class bad_exception : public exception { // base of all bad exceptions
-public:
-    __CLR_OR_THIS_CALL bad_exception(const char* _Message = "bad exception") noexcept : exception(_Message) {}
-
-    __CLR_OR_THIS_CALL ~bad_exception() noexcept override {}
-
-protected:
-    void __CLR_OR_THIS_CALL _Doraise() const override { // raise this exception
-        _RAISE(*this);
-    }
-};
-
-class bad_array_new_length;
-
-class bad_alloc : public exception { // base of all bad allocation exceptions
-public:
-    __CLR_OR_THIS_CALL bad_alloc() noexcept
-        : exception("bad allocation", 1) {} // construct from message string with no memory allocation
-
-    __CLR_OR_THIS_CALL ~bad_alloc() noexcept override {}
-
-private:
-    friend bad_array_new_length;
-
-    __CLR_OR_THIS_CALL bad_alloc(const char* _Message) noexcept
-        : exception(_Message, 1) {} // construct from message string with no memory allocation
-
-protected:
-    void __CLR_OR_THIS_CALL _Doraise() const override { // perform class-specific exception handling
-        _RAISE(*this);
-    }
-};
-
-class bad_array_new_length : public bad_alloc {
-public:
-    bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
-};
-
-_STDEXT_END
-
-_STD_BEGIN
-_EXPORT_STD using terminate_handler = void(__cdecl*)();
-
-_EXPORT_STD inline terminate_handler __CRTDECL set_terminate(terminate_handler) noexcept {
-    // register a terminate handler
-    return nullptr;
-}
-
-_EXPORT_STD [[noreturn]] inline void __CRTDECL terminate() noexcept {
-    // handle exception termination
-    _CSTD abort();
-}
-
-_EXPORT_STD _NODISCARD inline terminate_handler __CRTDECL get_terminate() noexcept {
-    // get current terminate handler
-    return nullptr;
-}
-
-#if _HAS_UNEXPECTED
-using unexpected_handler = void(__cdecl*)();
-
-inline unexpected_handler __CRTDECL set_unexpected(unexpected_handler) noexcept {
-    // register an unexpected handler
-    return nullptr;
-}
-
-inline void __CRTDECL unexpected() {} // handle unexpected exception
-
-_NODISCARD inline unexpected_handler __CRTDECL get_unexpected() noexcept {
-    // get current unexpected handler
-    return nullptr;
-}
-#endif // _HAS_UNEXPECTED
-
-_EXPORT_STD using _STDEXT bad_alloc;
-_EXPORT_STD using _STDEXT bad_array_new_length;
-_EXPORT_STD using _STDEXT bad_exception;
-
-_STD_END
-
-#pragma pop_macro("stdext")
-
-#endif // ^^^ !_HAS_EXCEPTIONS ^^^
-
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void*, _In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void*, _In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
-    _In_ const void*, _In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void*, _Inout_ void*) noexcept;
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void*) noexcept;
-extern "C++" [[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void*);
-extern "C++" _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
-    _Inout_ void*, _In_ const void*, _In_ const void*) noexcept;
-
-_STD_BEGIN
-
-_EXPORT_STD class exception_ptr {
-public:
-    exception_ptr() noexcept {
-        __ExceptionPtrCreate(this);
-    }
-
-    exception_ptr(nullptr_t) noexcept {
-        __ExceptionPtrCreate(this);
-    }
-
-    ~exception_ptr() noexcept {
-        __ExceptionPtrDestroy(this);
-    }
-
-    exception_ptr(const exception_ptr& _Rhs) noexcept {
-        __ExceptionPtrCopy(this, &_Rhs);
-    }
-
-    exception_ptr& operator=(const exception_ptr& _Rhs) noexcept {
-        __ExceptionPtrAssign(this, &_Rhs);
-        return *this;
-    }
-
-    exception_ptr& operator=(nullptr_t) noexcept {
-        exception_ptr _Ptr;
-        __ExceptionPtrAssign(this, &_Ptr);
-        return *this;
-    }
-
-    explicit operator bool() const noexcept {
-        return __ExceptionPtrToBool(this);
-    }
-
-    static exception_ptr _Copy_exception(_In_ void* _Except, _In_ const void* _Ptr) {
-        exception_ptr _Retval;
-        if (!_Ptr) {
-            // unsupported exceptions
-            return _Retval;
-        }
-        __ExceptionPtrCopyException(&_Retval, _Except, _Ptr);
-        return _Retval;
-    }
-
-    friend void swap(exception_ptr& _Lhs, exception_ptr& _Rhs) noexcept {
-        __ExceptionPtrSwap(&_Lhs, &_Rhs);
-    }
-
-    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
-        return __ExceptionPtrCompare(&_Lhs, &_Rhs);
-    }
-
-    _NODISCARD_FRIEND bool operator==(const exception_ptr& _Lhs, nullptr_t) noexcept {
-        return !_Lhs;
-    }
-
-#if !_HAS_CXX20
-    _NODISCARD_FRIEND bool operator==(nullptr_t, const exception_ptr& _Rhs) noexcept {
-        return !_Rhs;
-    }
-
-    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, const exception_ptr& _Rhs) noexcept {
-        return !(_Lhs == _Rhs);
-    }
-
-    _NODISCARD_FRIEND bool operator!=(const exception_ptr& _Lhs, nullptr_t) noexcept {
-        return !(_Lhs == nullptr);
-    }
-
-    _NODISCARD_FRIEND bool operator!=(nullptr_t, const exception_ptr& _Rhs) noexcept {
-        return !(nullptr == _Rhs);
-    }
-#endif // !_HAS_CXX20
-
-private:
-#ifdef __clang__
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wunused-private-field"
-#endif // defined(__clang__)
-    void* _Data1{};
-    void* _Data2{};
-#ifdef __clang__
-#pragma clang diagnostic pop
-#endif // defined(__clang__)
-};
-
-_EXPORT_STD _NODISCARD inline exception_ptr current_exception() noexcept {
-    exception_ptr _Retval;
-    __ExceptionPtrCurrentException(&_Retval);
-    return _Retval;
-}
-
-_EXPORT_STD [[noreturn]] inline void rethrow_exception(_In_ exception_ptr _Ptr) {
-    __ExceptionPtrRethrow(&_Ptr);
-}
-
-template <class _Ex>
-void* __GetExceptionInfo(_Ex);
-
-_EXPORT_STD template <class _Ex>
-_NODISCARD_SMART_PTR_ALLOC exception_ptr make_exception_ptr(_Ex _Except) noexcept {
-    return exception_ptr::_Copy_exception(_STD addressof(_Except), __GetExceptionInfo(_Except));
-}
-
-_EXPORT_STD class nested_exception { // wrap an exception_ptr
-public:
-    nested_exception() noexcept : _Exc(_STD current_exception()) {}
-
-    nested_exception(const nested_exception&) noexcept            = default;
-    nested_exception& operator=(const nested_exception&) noexcept = default;
-    virtual ~nested_exception() noexcept {}
-
-    [[noreturn]] void rethrow_nested() const { // throw wrapped exception_ptr
-        if (_Exc) {
-            _STD rethrow_exception(_Exc);
-        } else {
-            _STD terminate(); // per N4950 [except.nested]/4
-        }
-    }
-
-    _NODISCARD exception_ptr nested_ptr() const noexcept { // return wrapped exception_ptr
-        return _Exc;
-    }
-
-private:
-    exception_ptr _Exc;
-};
-
-template <class _Uty>
-struct _With_nested_v2 : _Uty, nested_exception { // glue user exception to nested_exception
-    template <class _Ty>
-    explicit _With_nested_v2(_Ty&& _Arg)
-        : _Uty(_STD forward<_Ty>(_Arg)), nested_exception() {} // store user exception and current_exception()
-};
-
-_EXPORT_STD template <class _Ty>
-[[noreturn]] void throw_with_nested(_Ty&& _Arg) {
-    // throw user exception, glued to nested_exception if possible
-    using _Uty = decay_t<_Ty>;
-
-    if constexpr (is_class_v<_Uty> && !is_base_of_v<nested_exception, _Uty> && !is_final_v<_Uty>) {
-        // throw user exception glued to nested_exception
-        _THROW(_With_nested_v2<_Uty>(_STD forward<_Ty>(_Arg)));
-    } else {
-        // throw user exception by itself
-        _THROW(_STD forward<_Ty>(_Arg));
-    }
-}
-
-#ifdef _CPPRTTI
-_EXPORT_STD template <class _Ty>
-void rethrow_if_nested(const _Ty& _Arg) {
-    // detect nested_exception inheritance
-    constexpr bool _Can_use_dynamic_cast =
-        is_polymorphic_v<_Ty> && (!is_base_of_v<nested_exception, _Ty> || is_convertible_v<_Ty*, nested_exception*>);
-
-    if constexpr (_Can_use_dynamic_cast) {
-        const auto _Nested = dynamic_cast<const nested_exception*>(_STD addressof(_Arg));
-
-        if (_Nested) {
-            _Nested->rethrow_nested();
-        }
-    }
-}
-#else // ^^^ defined(_CPPRTTI) / !defined(_CPPRTTI) vvv
-_EXPORT_STD template <class _Ty>
-void rethrow_if_nested(const _Ty&) = delete; // requires /GR option
-#endif // ^^^ !defined(_CPPRTTI) ^^^
-
-_EXPORT_STD class bad_variant_access
-    : public exception { // exception for visit of a valueless variant or get<I> on a variant with index() != I
-public:
-    bad_variant_access() noexcept = default;
-
-    _NODISCARD const char* __CLR_OR_THIS_CALL what() const noexcept override {
-        return "bad variant access";
-    }
-
-#if !_HAS_EXCEPTIONS
-protected:
-    void _Doraise() const override { // perform class-specific exception handling
-        _RAISE(*this);
-    }
-#endif // ^^^ !_HAS_EXCEPTIONS ^^^
-};
-
-[[noreturn]] inline void _Throw_bad_variant_access() {
-    _THROW(bad_variant_access{});
-}
-
-_STD_END
-
-#pragma pop_macro("new")
-_STL_RESTORE_CLANG_WARNINGS
-#pragma warning(pop)
-#pragma pack(pop)
-
-#endif // _STL_COMPILER_PREPROCESSOR
-#endif // _EXCEPTION_
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index de7817ad69f26..669816d2e54fd 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -97,6 +97,7 @@ endif()
 if(WIN32)
   list(APPEND LIBCXX_SOURCES
     support/win32/compiler_rt_shims.cpp
+    support/win32/excptptr.cpp
     support/win32/locale_win32.cpp
     support/win32/support.cpp
     )
diff --git a/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
index 4141e0312349b..b130f058fc7c0 100644
--- a/libcxx/src/support/runtime/exception_pointer_msvc.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
@@ -8,19 +8,17 @@
 //===----------------------------------------------------------------------===//
 
 #include <exception>
-#include <stdio.h>
-#include <stdlib.h>
-
-_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCreate(void*);
-_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrDestroy(void*);
-_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCopy(void*, const void*);
-_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrAssign(void*, const void*);
-_LIBCPP_CRT_FUNC bool __cdecl __ExceptionPtrCompare(const void*, const void*);
-_LIBCPP_CRT_FUNC bool __cdecl __ExceptionPtrToBool(const void*);
-_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrSwap(void*, void*);
-_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCurrentException(void*);
-[[noreturn]] _LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrRethrow(const void*);
-_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCopyException(void*, const void*, const void*);
+
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCreate(void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrDestroy(void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopy(void*, const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrAssign(void*, const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrCompare(const void*, const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrToBool(const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrSwap(void*, void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCurrentException(void*) noexcept;
+[[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrRethrow(const void*);
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopyException(void*, const void*, const void*) noexcept;
 
 namespace std {
 
diff --git a/libcxx/src/support/win32/excptptr.cpp b/libcxx/src/support/win32/excptptr.cpp
index a73c33cb1c7d7..89c9793ef8f6f 100644
--- a/libcxx/src/support/win32/excptptr.cpp
+++ b/libcxx/src/support/win32/excptptr.cpp
@@ -9,30 +9,20 @@
 // Copyright (c) Microsoft Corporation.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-// This implementation communicates with the EH runtime though vcruntime's per-thread-data structure; see
-// _pCurrentException in <trnsctrl.h>.
-//
-// As a result, normal EH runtime services (such as noexcept functions) are safe to use in this file.
+#include <__config>
+
+_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wmultichar")
 
-#ifndef _VCRT_ALLOW_INTERNALS
-#define _VCRT_ALLOW_INTERNALS
-#endif // !defined(_VCRT_ALLOW_INTERNALS)
+#include <__exception/exception_ptr.h>
+#include <__memory/shared_ptr.h>
+#include <cstdlib>
 
 #include <Unknwn.h>
-#include <cstdlib> // for abort
-#include <cstring> // for memcpy
+#include <Windows.h>
+struct _ThrowInfo;
 #include <eh.h>
 #include <ehdata.h>
-#include <exception>
-#include <internal_shared.h>
-#include <malloc.h>
-#include <memory>
-#include <new>
-#include <stdexcept>
-#include <trnsctrl.h>
-#include <xcall_once.h>
-
-#include <Windows.h>
+#include <malloc.h> // alloca
 
 // Pre-V4 managed exception code
 #define MANAGED_EXCEPTION_CODE 0XE0434F4D
@@ -40,502 +30,441 @@
 // V4 and later managed exception code
 #define MANAGED_EXCEPTION_CODE_V4 0XE0434352
 
-extern "C" _CRTIMP2 void* __cdecl __AdjustPointer(void*, const PMD&); // defined in frame.cpp
+extern "C" _LIBCPP_CRT_FUNC void* __cdecl __AdjustPointer(void*, const PMD&);
+extern "C" _LIBCPP_CRT_FUNC void** __cdecl __current_exception();
+
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCreate(void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrDestroy(void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopy(void*, const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrAssign(void*, const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrCompare(const void*, const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrToBool(const void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrSwap(void*, void*) noexcept;
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCurrentException(void*) noexcept;
+[[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrRethrow(const void*);
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopyException(void*, const void*, const void*) noexcept;
 
-using namespace std;
+static_assert(sizeof(std::exception_ptr) == sizeof(std::shared_ptr<const _EXCEPTION_RECORD>) &&
+                  alignof(std::exception_ptr) == alignof(std::shared_ptr<const _EXCEPTION_RECORD>),
+              "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
 
 namespace {
-#if defined(_M_CEE_PURE)
-    template <class _Ty>
-    _Ty& _Immortalize() { // return a reference to an object that will live forever
-        /* MAGIC */ static _Immortalizer_impl<_Ty> _Static;
-        return reinterpret_cast<_Ty&>(_Static._Storage);
-    }
-#elif !defined(_M_CEE)
-    template <class _Ty>
-    struct _Constexpr_excptptr_immortalize_impl {
-        union {
-            _Ty _Storage;
-        };
 
-        constexpr _Constexpr_excptptr_immortalize_impl() noexcept : _Storage{} {}
+typedef void(__stdcall* __prepare_for_throw_t)(void*);
+
+struct __winrt_exception_info {
+  void* __description;
+  void* __restricted_error_string;
+  void* __restricted_error_reference;
+  void* __capability_sid;
+  long __hr;
+  void* __restricted_info;
+  ThrowInfo* __throw_info;
+  unsigned int __size;
+  __prepare_for_throw_t __prepare_throw;
+};
 
-        _Constexpr_excptptr_immortalize_impl(const _Constexpr_excptptr_immortalize_impl&)            = delete;
-        _Constexpr_excptptr_immortalize_impl& operator=(const _Constexpr_excptptr_immortalize_impl&) = delete;
+inline EHExceptionRecord* __get_current_exception() noexcept {
+  return *reinterpret_cast<EHExceptionRecord**>(__current_exception());
+}
 
-        _MSVC_NOOP_DTOR ~_Constexpr_excptptr_immortalize_impl() {
-            // do nothing, allowing _Ty to be used during shutdown
-        }
-    };
+inline void __call_member_function_0(void* __this, void* __mfn) {
+  auto __fn = reinterpret_cast<void(__thiscall*)(void*)>(__mfn);
+  __fn(__this);
+}
 
-    template <class _Ty>
-    _Constexpr_excptptr_immortalize_impl<_Ty> _Immortalize_impl;
+inline void __call_member_function_1(void* __this, void* __mfn, void* __arg) {
+  auto __fn = reinterpret_cast<void(__thiscall*)(void*, void*)>(__mfn);
+  __fn(__this, __arg);
+}
 
-    template <class _Ty>
-    [[nodiscard]] _Ty& _Immortalize() noexcept {
-        return _Immortalize_impl<_Ty>._Storage;
-    }
-#else // ^^^ !defined(_M_CEE) / defined(_M_CEE), TRANSITION, VSO-1153256 vvv
-    template <class _Ty>
-    _Ty& _Immortalize() { // return a reference to an object that will live forever
-        static once_flag _Flag;
-        alignas(_Ty) static unsigned char _Storage[sizeof(_Ty)];
-        call_once(_Flag, [&_Storage] { ::new (static_cast<void*>(&_Storage)) _Ty(); });
-        return reinterpret_cast<_Ty&>(_Storage);
-    }
-#endif // ^^^ !defined(_M_CEE_PURE) && defined(_M_CEE), TRANSITION, VSO-1153256 ^^^
-
-    void _PopulateCppExceptionRecord(
-        _EXCEPTION_RECORD& _Record, const void* const _PExcept, ThrowInfo* _PThrow) noexcept {
-        _Record.ExceptionCode           = EH_EXCEPTION_NUMBER;
-        _Record.ExceptionFlags          = EXCEPTION_NONCONTINUABLE;
-        _Record.ExceptionRecord         = nullptr; // no SEH to chain
-        _Record.ExceptionAddress        = nullptr; // Address of exception. Will be overwritten by OS
-        _Record.NumberParameters        = EH_EXCEPTION_PARAMETERS;
-        _Record.ExceptionInformation[0] = EH_MAGIC_NUMBER1; // params.magicNumber
-        _Record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(_PExcept); // params.pExceptionObject
-
-        if (_PThrow && (_PThrow->attributes & TI_IsWinRT)) {
-            // The pointer to the ExceptionInfo structure is stored sizeof(void*) in front of each WinRT Exception Info.
-            const auto _PWei = (*static_cast<WINRTEXCEPTIONINFO** const*>(_PExcept))[-1];
-            _PThrow          = _PWei->throwInfo;
-        }
-
-        _Record.ExceptionInformation[2] = reinterpret_cast<ULONG_PTR>(_PThrow); // params.pThrowInfo
+inline void __call_member_function_2(void* __this, void* __mfn, void* __arg1, int __arg2) {
+  auto __fn = reinterpret_cast<void(__thiscall*)(void*, void*, int)>(__mfn);
+  __fn(__this, __arg1, __arg2);
+}
+
+void __populate_cpp_exception_record(
+    _EXCEPTION_RECORD& __record, const void* const __except_obj, ThrowInfo* __throw_info) noexcept {
+  __record.ExceptionCode           = EH_EXCEPTION_NUMBER;
+  __record.ExceptionFlags          = EXCEPTION_NONCONTINUABLE;
+  __record.ExceptionRecord         = nullptr;
+  __record.ExceptionAddress        = nullptr;
+  __record.NumberParameters        = EH_EXCEPTION_PARAMETERS;
+  __record.ExceptionInformation[0] = EH_MAGIC_NUMBER1;
+  __record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(__except_obj);
+
+  if (__throw_info && (__throw_info->attributes & TI_IsWinRT)) {
+    const auto __wei = (*static_cast<__winrt_exception_info** const*>(const_cast<void*>(__except_obj)))[-1];
+    __throw_info     = __wei->__throw_info;
+  }
+
+  __record.ExceptionInformation[2] = reinterpret_cast<ULONG_PTR>(__throw_info);
 
 #if _EH_RELATIVE_TYPEINFO
-        void* _ThrowImageBase =
-            _PThrow ? RtlPcToFileHeader(const_cast<void*>(static_cast<const void*>(_PThrow)), &_ThrowImageBase)
-                    : nullptr;
-        _Record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(_ThrowImageBase); // params.pThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-
-        // If the throw info indicates this throw is from a pure region,
-        // set the magic number to the Pure one, so only a pure-region
-        // catch will see it.
-        //
-        // Also use the Pure magic number on 64-bit platforms if we were unable to
-        // determine an image base, since that was the old way to determine
-        // a pure throw, before the TI_IsPure bit was added to the FuncInfo
-        // attributes field.
-        if (_PThrow
-            && ((_PThrow->attributes & TI_IsPure)
+  void* __throw_image_base =
+      __throw_info ? RtlPcToFileHeader(const_cast<void*>(static_cast<const void*>(__throw_info)), &__throw_image_base)
+                   : nullptr;
+  __record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(__throw_image_base);
+#endif
+
+  if (__throw_info && ((__throw_info->attributes & TI_IsPure)
 #if _EH_RELATIVE_TYPEINFO
-                || !_ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-                )) {
-            _Record.ExceptionInformation[0] = EH_PURE_MAGIC_NUMBER1;
-        }
-    }
+                       || !__throw_image_base
+#endif
+                       )) {
+    __record.ExceptionInformation[0] = EH_PURE_MAGIC_NUMBER1;
+  }
+}
 
-    void _CopyExceptionRecord(_EXCEPTION_RECORD& _Dest, const _EXCEPTION_RECORD& _Src) noexcept {
-        _Dest.ExceptionCode = _Src.ExceptionCode;
-        // we force EXCEPTION_NONCONTINUABLE because rethrow_exception is [[noreturn]]
-        _Dest.ExceptionFlags   = _Src.ExceptionFlags | EXCEPTION_NONCONTINUABLE;
-        _Dest.ExceptionRecord  = nullptr; // We don't chain SEH exceptions
-        _Dest.ExceptionAddress = nullptr; // Useless field to copy. It will be overwritten by RaiseException()
-        const auto _Parameters = _Src.NumberParameters;
-        _Dest.NumberParameters = _Parameters;
-
-        // copy the number of parameters in use
-        constexpr auto _Max_parameters = static_cast<DWORD>(EXCEPTION_MAXIMUM_PARAMETERS);
-        const auto _In_use             = (_STD min)(_Parameters, _Max_parameters);
-        _CSTD memcpy(_Dest.ExceptionInformation, _Src.ExceptionInformation, _In_use * sizeof(ULONG_PTR));
-        _CSTD memset(&_Dest.ExceptionInformation[_In_use], 0, (_Max_parameters - _In_use) * sizeof(ULONG_PTR));
-    }
+void __copy_exception_record(_EXCEPTION_RECORD& __dest, const _EXCEPTION_RECORD& __src) noexcept {
+  __dest.ExceptionCode    = __src.ExceptionCode;
+  __dest.ExceptionFlags   = __src.ExceptionFlags | EXCEPTION_NONCONTINUABLE;
+  __dest.ExceptionRecord  = nullptr;
+  __dest.ExceptionAddress = nullptr;
+  const auto __parameters = __src.NumberParameters;
+  __dest.NumberParameters = __parameters;
+
+  constexpr auto __max_parameters = static_cast<DWORD>(EXCEPTION_MAXIMUM_PARAMETERS);
+  const auto __in_use             = (__parameters < __max_parameters) ? __parameters : __max_parameters;
+  std::memcpy(__dest.ExceptionInformation, __src.ExceptionInformation, __in_use * sizeof(ULONG_PTR));
+  std::memset(&__dest.ExceptionInformation[__in_use], 0, (__max_parameters - __in_use) * sizeof(ULONG_PTR));
+}
 
-    void _CopyExceptionObject(void* _Dest, const void* _Src, const CatchableType* const _PType
+void __copy_exception_object(void* __dest, const void* __src, const CatchableType* const __type
 #if _EH_RELATIVE_TYPEINFO
-        ,
-        const uintptr_t _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-    ) {
-        // copy an object of type denoted by *_PType from _Src to _Dest; throws whatever the copy ctor of the type
-        // denoted by *_PType throws
-        if ((_PType->properties & CT_IsSimpleType) || _PType->copyFunction == 0) {
-            memcpy(_Dest, _Src, _PType->sizeOrOffset);
-
-            if (_PType->properties & CT_IsWinRTHandle) {
-                const auto _PUnknown = *static_cast<IUnknown* const*>(_Src);
-                if (_PUnknown) {
-                    _PUnknown->AddRef();
-                }
-            }
-            return;
-        }
+                             ,
+                             uintptr_t __throw_image_base
+#endif
+) {
+  if ((__type->properties & CT_IsSimpleType) || __type->copyFunction == 0) {
+    std::memcpy(__dest, __src, __type->sizeOrOffset);
+
+    if (__type->properties & CT_IsWinRTHandle) {
+      const auto __unknown = *static_cast<IUnknown* const*>(const_cast<void*>(__src));
+      if (__unknown) {
+        __unknown->AddRef();
+      }
+    }
+    return;
+  }
 
 #if _EH_RELATIVE_TYPEINFO
-        const auto _CopyFunc = reinterpret_cast<void*>(_ThrowImageBase + _PType->copyFunction);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _CopyFunc = _PType->copyFunction;
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        const auto _Adjusted = __AdjustPointer(const_cast<void*>(_Src), _PType->thisDisplacement);
-        if (_PType->properties & CT_HasVirtualBase) {
-#ifdef _M_CEE_PURE
-            reinterpret_cast<void(__clrcall*)(void*, void*, int)>(_CopyFunc)(_Dest, _Adjusted, 1);
-#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
-            _CallMemberFunction2(_Dest, _CopyFunc, _Adjusted, 1);
-#endif // ^^^ !defined(_M_CEE_PURE) ^^^
-        } else {
-#ifdef _M_CEE_PURE
-            reinterpret_cast<void(__clrcall*)(void*, void*)>(_CopyFunc)(_Dest, _Adjusted);
-#else // ^^^ defined(_M_CEE_PURE) / !defined(_M_CEE_PURE) vvv
-            _CallMemberFunction1(_Dest, _CopyFunc, _Adjusted);
-#endif // ^^^ !defined(_M_CEE_PURE) ^^^
-        }
+  const auto __copy_func = reinterpret_cast<void*>(__throw_image_base + __type->copyFunction);
+#else
+  const auto __copy_func = __type->copyFunction;
+#endif
+
+  const auto __adjusted = __AdjustPointer(const_cast<void*>(__src), __type->thisDisplacement);
+  if (__type->properties & CT_HasVirtualBase) {
+    __call_member_function_2(__dest, __copy_func, __adjusted, 1);
+  } else {
+    __call_member_function_1(__dest, __copy_func, __adjusted);
+  }
+}
+
+template <class _StaticEx>
+class __exception_ptr_static final : public std::__shared_weak_count {
+private:
+  void __on_zero_shared() noexcept override {}
+  void __on_zero_shared_weak() noexcept override {}
+
+public:
+  __exception_ptr_static() noexcept : std::__shared_weak_count(0) {
+    __populate_cpp_exception_record(__record_, &__ex_, static_cast<ThrowInfo*>(std::__GetExceptionInfo(__ex_)));
+  }
+
+  static std::shared_ptr<const _EXCEPTION_RECORD> __get() noexcept {
+    struct __container {
+      union {
+        __exception_ptr_static __instance_;
+      };
+      __container() noexcept : __instance_() {}
+      ~__container() {}
+    };
+    static __container __storage;
+    __storage.__instance_.__add_shared();
+    return std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(
+        &__storage.__instance_.__record_, &__storage.__instance_);
+  }
+
+  _EXCEPTION_RECORD __record_;
+  _StaticEx __ex_;
+};
+
+class alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) __exception_ptr_normal final : public std::__shared_weak_count {
+private:
+  void __on_zero_shared() noexcept override {
+    const auto& __cpp_eh_record = reinterpret_cast<EHExceptionRecord&>(__record_);
+
+    if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_eh_record)) {
+      return;
     }
-} // unnamed namespace
-
-// All exception_ptr implementations are out-of-line because <memory> depends on <exception>,
-// which means <exception> cannot include <memory> -- and shared_ptr is defined in <memory>.
-// To workaround this, we created a dummy class exception_ptr, which is structurally identical to shared_ptr.
-
-_STD_BEGIN
-struct _Exception_ptr_access {
-    template <class _Ty, class _Ty2>
-    static void _Set_ptr_rep(_Ptr_base<_Ty>& _This, _Ty2* _Px, _Ref_count_base* _Rx) noexcept {
-        _This._Ptr = _Px;
-        _This._Rep = _Rx;
+
+    const auto __throw_info = __cpp_eh_record.params.pThrowInfo;
+    if (!__throw_info) {
+      std::abort();
     }
-};
-_STD_END
 
-static_assert(sizeof(exception_ptr) == sizeof(shared_ptr<const _EXCEPTION_RECORD>)
-                  && alignof(exception_ptr) == alignof(shared_ptr<const _EXCEPTION_RECORD>),
-    "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
+    if (!__cpp_eh_record.params.pExceptionObject) {
+      return;
+    }
 
-namespace {
-    template <class _StaticEx>
-    class _ExceptionPtr_static final : public _Ref_count_base {
-        // reference count control block for special "never allocates" exceptions like the bad_alloc or bad_exception
-        // exception_ptrs
-    private:
-        void _Destroy() noexcept override {
-            // intentionally does nothing
-        }
-
-        void _Delete_this() noexcept override {
-            // intentionally does nothing
-        }
-
-    public:
-        // constexpr, TRANSITION P1064
-        explicit _ExceptionPtr_static() noexcept : _Ref_count_base() {
-            _PopulateCppExceptionRecord(_ExRecord, &_Ex, static_cast<ThrowInfo*>(__GetExceptionInfo(_Ex)));
-        }
-
-        static shared_ptr<const _EXCEPTION_RECORD> _Get() noexcept {
-            auto& _Instance = _Immortalize<_ExceptionPtr_static>();
-            shared_ptr<const _EXCEPTION_RECORD> _Ret;
-            _Instance._Incref();
-            _Exception_ptr_access::_Set_ptr_rep(_Ret, &_Instance._ExRecord, &_Instance);
-            return _Ret;
-        }
-
-        _EXCEPTION_RECORD _ExRecord;
-        _StaticEx _Ex;
-    };
+#if _EH_RELATIVE_TYPEINFO
+    const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_eh_record.params.pThrowImageBase);
+    const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+        static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
+    const auto __type = reinterpret_cast<CatchableType*>(
+        static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
+#else
+    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+    if (__throw_info->pmfnUnwind) {
+#if _EH_RELATIVE_TYPEINFO
+      __call_member_function_0(__cpp_eh_record.params.pExceptionObject,
+                               reinterpret_cast<void*>(__throw_info->pmfnUnwind + __throw_image_base));
+#else
+      __call_member_function_0(__cpp_eh_record.params.pExceptionObject, __throw_info->pmfnUnwind);
+#endif
+    } else if (__type->properties & CT_IsWinRTHandle) {
+      const auto __unknown = *static_cast<IUnknown* const*>(__cpp_eh_record.params.pExceptionObject);
+      if (__unknown) {
+        __unknown->Release();
+      }
+    }
+  }
 
-    class _ExceptionPtr_normal final : public _Ref_count_base {
-        // reference count control block for exception_ptrs; the exception object is stored at
-        // reinterpret_cast<unsigned char*>(this) + sizeof(_ExceptionPtr_normal)
-    private:
-        void _Destroy() noexcept override {
-            // call the destructor for a stored pure or native C++ exception if necessary
-            const auto& _CppEhRecord = reinterpret_cast<EHExceptionRecord&>(_ExRecord);
+  void __on_zero_shared_weak() noexcept override {
+    std::free(this);
+  }
 
-            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppEhRecord)) {
-                return;
-            }
+public:
+  explicit __exception_ptr_normal(const _EXCEPTION_RECORD& __record) noexcept : std::__shared_weak_count(0) {
+    __copy_exception_record(__record_, __record);
+  }
 
-            const auto _PThrow = _CppEhRecord.params.pThrowInfo;
-            if (!_PThrow) {
-                // No ThrowInfo exists. If this was a C++ exception, something must have corrupted it.
-                _CSTD abort();
-            }
+  _EXCEPTION_RECORD __record_;
+};
 
-            if (!_CppEhRecord.params.pExceptionObject) {
-                return;
-            }
+static_assert(sizeof(__exception_ptr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
+              "Exception in exception_ptr would be constructed with the wrong alignment");
 
-#if _EH_RELATIVE_TYPEINFO
-            const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_CppEhRecord.params.pThrowImageBase);
-            const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
-                static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
-            const auto _PType = reinterpret_cast<CatchableType*>(
-                static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-            const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-            if (_PThrow->pmfnUnwind) {
-                // The exception was a user defined type with a nontrivial destructor, call it
-#if defined(_M_CEE_PURE)
-                reinterpret_cast<void(__clrcall*)(void*)>(_PThrow->pmfnUnwind)(_CppEhRecord.params.pExceptionObject);
-#elif _EH_RELATIVE_TYPEINFO
-                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject,
-                    reinterpret_cast<void*>(_PThrow->pmfnUnwind + _ThrowImageBase));
-#else // ^^^ _EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) / !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) vvv
-                _CallMemberFunction0(_CppEhRecord.params.pExceptionObject, _PThrow->pmfnUnwind);
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO && !defined(_M_CEE_PURE) ^^^
-            } else if (_PType->properties & CT_IsWinRTHandle) {
-                const auto _PUnknown = *static_cast<IUnknown* const*>(_CppEhRecord.params.pExceptionObject);
-                if (_PUnknown) {
-                    _PUnknown->Release();
-                }
-            }
-        }
-
-        void _Delete_this() noexcept override {
-            free(this);
-        }
-
-    public:
-        explicit _ExceptionPtr_normal(const _EXCEPTION_RECORD& _Record) noexcept : _Ref_count_base() {
-            _CopyExceptionRecord(_ExRecord, _Record);
-        }
-
-        _EXCEPTION_RECORD _ExRecord;
-        void* _Unused_alignment_padding{};
-    };
+void __assign_seh_exception_ptr_from_record(
+    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const _EXCEPTION_RECORD& __record, void* const __rx_raw) noexcept {
+  if (!__rx_raw) {
+    __dest = __exception_ptr_static<std::bad_alloc>::__get();
+    return;
+  }
 
-    // We aren't using alignas because this file might be compiled with _M_CEE_PURE
-    static_assert(sizeof(_ExceptionPtr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
-        "Exception in exception_ptr would be constructed with the wrong alignment");
-
-    void _Assign_seh_exception_ptr_from_record(
-        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const _EXCEPTION_RECORD& _Record, void* const _RxRaw) noexcept {
-        // in the memory _RxRaw, constructs a reference count control block for a SEH exception denoted by _Record
-        // if _RxRaw is nullptr, assigns bad_alloc instead
-        if (!_RxRaw) {
-            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
-            return;
-        }
-
-        const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(_Record);
-        _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
-    }
+  const auto __rx = ::new (__rx_raw) __exception_ptr_normal(__record);
+  __dest          = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+}
 
-    void _Assign_cpp_exception_ptr_from_record(
-        shared_ptr<const _EXCEPTION_RECORD>& _Dest, const EHExceptionRecord& _Record) noexcept {
-        // construct a reference count control block for the C++ exception recorded by _Record, and bind it to _Dest
-        // if allocating memory for the reference count control block fails, sets _Dest to bad_alloc
-        // if copying the exception object referred to _Record throws, constructs a reference count control block for
-        //      that exception instead
-        // if copying the exception object thrown by copying the original exception object throws, sets _Dest to
-        //      bad_exception
-        const auto _PThrow = _Record.params.pThrowInfo;
+void __assign_cpp_exception_ptr_from_record(
+    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const EHExceptionRecord& __record) noexcept {
+  const auto __throw_info = __record.params.pThrowInfo;
 #if _EH_RELATIVE_TYPEINFO
-        const auto _ThrowImageBase     = reinterpret_cast<uintptr_t>(_Record.params.pThrowImageBase);
-        const auto _CatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
-            static_cast<uintptr_t>(_PThrow->pCatchableTypeArray) + _ThrowImageBase);
-        const auto _PType = reinterpret_cast<CatchableType*>(
-            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        const auto _ExceptionObjectSize = static_cast<size_t>(_PType->sizeOrOffset);
-        const auto _AllocSize           = sizeof(_ExceptionPtr_normal) + _ExceptionObjectSize;
-        _Analysis_assume_(_AllocSize >= sizeof(_ExceptionPtr_normal));
-        auto _RxRaw = malloc(_AllocSize);
-        if (!_RxRaw) {
-            _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
-            return;
-        }
-
-        try {
-            _CopyExceptionObject(static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _Record.params.pExceptionObject, _PType
+  const auto __throw_image_base = reinterpret_cast<uintptr_t>(__record.params.pThrowImageBase);
+  const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+      static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
+  const auto __type = reinterpret_cast<CatchableType*>(
+      static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
+#else
+  const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+  const auto __except_obj_size = static_cast<size_t>(__type->sizeOrOffset);
+  const auto __alloc_size      = sizeof(__exception_ptr_normal) + __except_obj_size;
+  auto __rx_raw                = std::malloc(__alloc_size);
+  if (!__rx_raw) {
+    __dest = __exception_ptr_static<std::bad_alloc>::__get();
+    return;
+  }
+
+  try {
+    __copy_exception_object(static_cast<__exception_ptr_normal*>(__rx_raw) + 1,
+                            __record.params.pExceptionObject,
+                            __type
 #if _EH_RELATIVE_TYPEINFO
-                ,
-                _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-            );
-
-            const auto _Rx = ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_Record));
-            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
-                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
-            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
-        } catch (...) { // copying the exception object threw an exception
-            const auto& _InnerRecord = *_pCurrentException; // exception thrown by the original exception's copy ctor
-            if (_InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE
-                || _InnerRecord.ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
-                // we don't support managed exceptions and don't want to say there's no active exception, so give up and
-                // say bad_exception
-                free(_RxRaw);
-                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
-                return;
-            }
-
-            if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&_InnerRecord)) { // catching a non-C++ exception depends on /EHa
-                _Assign_seh_exception_ptr_from_record(
-                    _Dest, reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord), _RxRaw);
-                return;
-            }
-
-            const auto _PInnerThrow = _InnerRecord.params.pThrowInfo;
+                            ,
+                            __throw_image_base
+#endif
+    );
+
+    const auto __rx = ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__record));
+    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
+        static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
+    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+  } catch (...) {
+    const auto* __inner_record_ptr = __get_current_exception();
+    if (!__inner_record_ptr) {
+      std::free(__rx_raw);
+      __dest = __exception_ptr_static<std::bad_exception>::__get();
+      return;
+    }
+    const auto& __inner_record = *__inner_record_ptr;
+    if (__inner_record.ExceptionCode == MANAGED_EXCEPTION_CODE ||
+        __inner_record.ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+      std::free(__rx_raw);
+      __dest = __exception_ptr_static<std::bad_exception>::__get();
+      return;
+    }
+
+    if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&__inner_record)) {
+      __assign_seh_exception_ptr_from_record(
+          __dest, reinterpret_cast<const _EXCEPTION_RECORD&>(__inner_record), __rx_raw);
+      return;
+    }
+
+    const auto __inner_throw = __inner_record.params.pThrowInfo;
 #if _EH_RELATIVE_TYPEINFO
-            const auto _InnerThrowImageBase     = reinterpret_cast<uintptr_t>(_InnerRecord.params.pThrowImageBase);
-            const auto _InnerCatchableTypeArray = reinterpret_cast<const CatchableTypeArray*>(
-                static_cast<uintptr_t>(_PInnerThrow->pCatchableTypeArray) + _InnerThrowImageBase);
-            const auto _PInnerType = reinterpret_cast<CatchableType*>(
-                static_cast<uintptr_t>(_InnerCatchableTypeArray->arrayOfCatchableTypes[0]) + _InnerThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-            const auto _PInnerType = _PInnerThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-            const auto _InnerExceptionSize = static_cast<size_t>(_PInnerType->sizeOrOffset);
-            const auto _InnerAllocSize     = sizeof(_ExceptionPtr_normal) + _InnerExceptionSize;
-            if (_InnerAllocSize > _AllocSize) {
-                free(_RxRaw);
-                _RxRaw = malloc(_InnerAllocSize);
-                if (!_RxRaw) {
-                    _Dest = _ExceptionPtr_static<bad_alloc>::_Get();
-                    return;
-                }
-            }
-
-            try {
-                _CopyExceptionObject(
-                    static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1, _InnerRecord.params.pExceptionObject, _PInnerType
+    const auto __inner_throw_image_base = reinterpret_cast<uintptr_t>(__inner_record.params.pThrowImageBase);
+    const auto __inner_catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+        static_cast<uintptr_t>(__inner_throw->pCatchableTypeArray) + __inner_throw_image_base);
+    const auto __inner_type = reinterpret_cast<CatchableType*>(
+        static_cast<uintptr_t>(__inner_catchable_type_array->arrayOfCatchableTypes[0]) + __inner_throw_image_base);
+#else
+    const auto __inner_type = __inner_throw->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+    const auto __inner_except_size = static_cast<size_t>(__inner_type->sizeOrOffset);
+    const auto __inner_alloc_size  = sizeof(__exception_ptr_normal) + __inner_except_size;
+    if (__inner_alloc_size > __alloc_size) {
+      std::free(__rx_raw);
+      __rx_raw = std::malloc(__inner_alloc_size);
+      if (!__rx_raw) {
+        __dest = __exception_ptr_static<std::bad_alloc>::__get();
+        return;
+      }
+    }
+
+    try {
+      __copy_exception_object(static_cast<__exception_ptr_normal*>(__rx_raw) + 1,
+                              __inner_record.params.pExceptionObject,
+                              __inner_type
 #if _EH_RELATIVE_TYPEINFO
-                    ,
-                    _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-                );
-            } catch (...) { // copying the exception emitted while copying the original exception also threw, give up
-                free(_RxRaw);
-                _Dest = _ExceptionPtr_static<bad_exception>::_Get();
-                return;
-            }
-
-            // this next block must be duplicated inside the catch (even though it looks identical to the block in the
-            // try) so that _InnerRecord is held alive; exiting the catch will destroy it
-            const auto _Rx =
-                ::new (_RxRaw) _ExceptionPtr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(_InnerRecord));
-            reinterpret_cast<EHExceptionRecord&>(_Rx->_ExRecord).params.pExceptionObject =
-                static_cast<_ExceptionPtr_normal*>(_RxRaw) + 1;
-            _Exception_ptr_access::_Set_ptr_rep(_Dest, &_Rx->_ExRecord, _Rx);
-        }
+                              ,
+                              __inner_throw_image_base
+#endif
+      );
+    } catch (...) {
+      std::free(__rx_raw);
+      __dest = __exception_ptr_static<std::bad_exception>::__get();
+      return;
     }
-} // unnamed namespace
 
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCreate(_Out_ void* _Ptr) noexcept {
-    ::new (_Ptr) shared_ptr<const _EXCEPTION_RECORD>();
+    const auto __rx =
+        ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__inner_record));
+    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
+        static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
+    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+  }
 }
 
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrDestroy(_Inout_ void* _Ptr) noexcept {
-    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr)->~shared_ptr<const _EXCEPTION_RECORD>();
+} // namespace
+
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCreate(void* __ptr) noexcept {
+  ::new (__ptr) std::shared_ptr<const _EXCEPTION_RECORD>();
 }
 
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopy(_Out_ void* _Dest, _In_ const void* _Src) noexcept {
-    ::new (_Dest) shared_ptr<const _EXCEPTION_RECORD>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src));
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrDestroy(void* __ptr) noexcept {
+  static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr)->~shared_ptr();
 }
 
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrAssign(_Inout_ void* _Dest, _In_ const void* _Src) noexcept {
-    *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Dest) =
-        *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Src);
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopy(void* __dest, const void* __src) noexcept {
+  ::new (__dest) std::shared_ptr<const _EXCEPTION_RECORD>(
+      *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__src));
 }
 
-_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrCompare(
-    _In_ const void* _Lhs, _In_ const void* _Rhs) noexcept {
-    return *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)
-        == *static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs);
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrAssign(void* __dest, const void* __src) noexcept {
+  *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__dest) =
+      *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__src);
 }
 
-_CRTIMP2_PURE bool __CLRCALL_PURE_OR_CDECL __ExceptionPtrToBool(_In_ const void* _Ptr) noexcept {
-    return static_cast<bool>(*static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr));
+_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrCompare(const void* __lhs, const void* __rhs) noexcept {
+  return *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__lhs) ==
+         *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__rhs);
 }
 
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrSwap(_Inout_ void* _Lhs, _Inout_ void* _Rhs) noexcept {
-    static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Lhs)->swap(
-        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Rhs));
+_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrToBool(const void* __ptr) noexcept {
+  return static_cast<bool>(*static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr));
 }
 
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCurrentException(void* _Ptr) noexcept {
-    const auto _PRecord = _pCurrentException; // nontrivial FLS cost, pay it once
-    if (!_PRecord || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE
-        || _PRecord->ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
-        return; // no current exception, or we don't support managed exceptions
-    }
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrSwap(void* __lhs, void* __rhs) noexcept {
+  static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__lhs)->swap(
+      *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__rhs));
+}
 
-    auto& _Dest = *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr);
-    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(_PRecord)) {
-        _Assign_cpp_exception_ptr_from_record(_Dest, *_PRecord);
-    } else {
-        // _Assign_seh_exception_ptr_from_record handles failed malloc
-        _Assign_seh_exception_ptr_from_record(
-            _Dest, reinterpret_cast<_EXCEPTION_RECORD&>(*_PRecord), malloc(sizeof(_ExceptionPtr_normal)));
-    }
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCurrentException(void* __ptr) noexcept {
+  const auto __record = __get_current_exception();
+  if (!__record || __record->ExceptionCode == MANAGED_EXCEPTION_CODE ||
+      __record->ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+    return;
+  }
+
+  auto& __dest = *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr);
+  if (PER_IS_MSVC_PURE_OR_NATIVE_EH(__record)) {
+    __assign_cpp_exception_ptr_from_record(__dest, *__record);
+  } else {
+    __assign_seh_exception_ptr_from_record(
+        __dest, reinterpret_cast<_EXCEPTION_RECORD&>(*__record), std::malloc(sizeof(__exception_ptr_normal)));
+  }
 }
 
-[[noreturn]] _CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrRethrow(_In_ const void* _PtrRaw) {
-    const shared_ptr<const _EXCEPTION_RECORD>* _Ptr = static_cast<const shared_ptr<const _EXCEPTION_RECORD>*>(_PtrRaw);
-    // throwing a bad_exception if they give us a nullptr exception_ptr
-    if (!*_Ptr) {
-        throw bad_exception();
+[[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrRethrow(const void* __ptr_raw) {
+  const auto* __ptr = static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr_raw);
+  if (!*__ptr) {
+    throw std::bad_exception();
+  }
+
+  auto __record_copy = **__ptr;
+  auto& __cpp_record = reinterpret_cast<EHExceptionRecord&>(__record_copy);
+  if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_record)) {
+    const auto __throw_info = __cpp_record.params.pThrowInfo;
+    if (!__cpp_record.params.pExceptionObject || !__throw_info || !__throw_info->pCatchableTypeArray) {
+      std::abort();
     }
 
-    auto _RecordCopy = **_Ptr;
-    auto& _CppRecord = reinterpret_cast<EHExceptionRecord&>(_RecordCopy);
-    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&_CppRecord)) {
-        // This is a C++ exception.
-        // We need to build the exception on the stack because the current exception mechanism assumes the exception
-        // object is on the stack and will call the appropriate destructor (if there's a nontrivial one).
-        const auto _PThrow = _CppRecord.params.pThrowInfo;
-        if (!_CppRecord.params.pExceptionObject || !_PThrow || !_PThrow->pCatchableTypeArray) {
-            // Missing or corrupt ThrowInfo. If this was a C++ exception, something must have corrupted it.
-            _CSTD abort();
-        }
-
 #if _EH_RELATIVE_TYPEINFO
-        const auto _ThrowImageBase = reinterpret_cast<uintptr_t>(_CppRecord.params.pThrowImageBase);
-        const auto _CatchableTypeArray =
-            reinterpret_cast<const CatchableTypeArray*>(_ThrowImageBase + _PThrow->pCatchableTypeArray);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _CatchableTypeArray = _PThrow->pCatchableTypeArray;
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        if (_CatchableTypeArray->nCatchableTypes <= 0) {
-            // Ditto corrupted.
-            _CSTD abort();
-        }
-
-        // we finally got the type info we want
+    const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_record.params.pThrowImageBase);
+    const auto __catchable_type_array =
+        reinterpret_cast<const CatchableTypeArray*>(__throw_image_base + __throw_info->pCatchableTypeArray);
+#else
+    const auto __catchable_type_array = __throw_info->pCatchableTypeArray;
+#endif
+
+    if (__catchable_type_array->nCatchableTypes <= 0) {
+      std::abort();
+    }
+
 #if _EH_RELATIVE_TYPEINFO
-        const auto _PType = reinterpret_cast<CatchableType*>(
-            static_cast<uintptr_t>(_CatchableTypeArray->arrayOfCatchableTypes[0]) + _ThrowImageBase);
-#else // ^^^ _EH_RELATIVE_TYPEINFO / !_EH_RELATIVE_TYPEINFO vvv
-        const auto _PType = _PThrow->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif // ^^^ !_EH_RELATIVE_TYPEINFO ^^^
-
-        // Alloc memory on stack for exception object. This might cause a stack overflow SEH exception, or another C++
-        // exception when copying the C++ exception object. In that case, we just let that become the thrown exception.
-
-#pragma warning(suppress : 6255) //  _alloca indicates failure by raising a stack overflow exception
-        void* _PExceptionBuffer = alloca(_PType->sizeOrOffset);
-        _CopyExceptionObject(_PExceptionBuffer, _CppRecord.params.pExceptionObject, _PType
+    const auto __type = reinterpret_cast<CatchableType*>(
+        static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
+#else
+    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+#pragma warning(suppress : 6255)
+    void* __exception_buffer = alloca(__type->sizeOrOffset);
+    __copy_exception_object(__exception_buffer, __cpp_record.params.pExceptionObject, __type
 #if _EH_RELATIVE_TYPEINFO
-            ,
-            _ThrowImageBase
-#endif // _EH_RELATIVE_TYPEINFO
-        );
-
-        _CppRecord.params.pExceptionObject = _PExceptionBuffer;
-    } else {
-        // this is a SEH exception, no special handling is required
-    }
+                            ,
+                            __throw_image_base
+#endif
+    );
+
+    __cpp_record.params.pExceptionObject = __exception_buffer;
+  }
 
-    _Analysis_assume_(_RecordCopy.NumberParameters <= EXCEPTION_MAXIMUM_PARAMETERS);
-    RaiseException(_RecordCopy.ExceptionCode, _RecordCopy.ExceptionFlags, _RecordCopy.NumberParameters,
-        _RecordCopy.ExceptionInformation);
+  RaiseException(__record_copy.ExceptionCode, __record_copy.ExceptionFlags, __record_copy.NumberParameters,
+                 __record_copy.ExceptionInformation);
+  std::abort();
 }
 
-_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL __ExceptionPtrCopyException(
-    _Inout_ void* _Ptr, _In_ const void* _PExceptRaw, _In_ const void* _PThrowRaw) noexcept {
-    _EXCEPTION_RECORD _Record;
-    _PopulateCppExceptionRecord(_Record, _PExceptRaw, static_cast<ThrowInfo*>(_PThrowRaw));
-    _Assign_cpp_exception_ptr_from_record(
-        *static_cast<shared_ptr<const _EXCEPTION_RECORD>*>(_Ptr), reinterpret_cast<const EHExceptionRecord&>(_Record));
+_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopyException(
+    void* __ptr, const void* __except_raw, const void* __throw_raw) noexcept {
+  _EXCEPTION_RECORD __record;
+  __populate_cpp_exception_record(__record, __except_raw, static_cast<ThrowInfo*>(const_cast<void*>(__throw_raw)));
+  __assign_cpp_exception_ptr_from_record(
+      *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr), reinterpret_cast<const EHExceptionRecord&>(__record));
 }

>From 5b629f74e30c3cb93d0965956fd719cf0c081882 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Mon, 22 Jun 2026 01:25:45 +0000
Subject: [PATCH 07/10] Merge the implementation into
 exception_pointer_msvc.ipp

* Removed all ten exported __ExceptionPtr... C functions. These symbols
  were originally present in Microsoft's CRT DLL exports solely to
  support inline methods in MSVC STL's <exception> header. User code
  compiled against libc++ public headers calls standard C++ functions
  (current_exception(), rethrow_exception, swap, operator==,
  constructors/destructors) and never references the __ExceptionPtr... C
  runtime symbols.
* Moved all SEH/EH exception record definitions, SEH copying helpers
  (__populate_cpp_exception_record, __copy_exception_object), and
  reference-counted control blocks (__exception_ptr_static and
  __exception_ptr_normal) directly into exception_pointer_msvc.ipp.
* Implemented std::exception_ptr constructors, destructor, assignment
  operators, and queries directly in namespace std via placement new
  (::new (this) shared_ptr<const _EXCEPTION_RECORD>(...)) and
  reinterpret_cast conversion helpers (__to_shared), taking full
  advantage of the guaranteed layout compatibility between exception_ptr
  and shared_ptr<const _EXCEPTION_RECORD>.
---
 libcxx/src/CMakeLists.txt                     |   1 -
 .../runtime/exception_pointer_msvc.ipp        | 466 ++++++++++++++++-
 libcxx/src/support/win32/excptptr.cpp         | 470 ------------------
 3 files changed, 439 insertions(+), 498 deletions(-)
 delete mode 100644 libcxx/src/support/win32/excptptr.cpp

diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index 669816d2e54fd..de7817ad69f26 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -97,7 +97,6 @@ endif()
 if(WIN32)
   list(APPEND LIBCXX_SOURCES
     support/win32/compiler_rt_shims.cpp
-    support/win32/excptptr.cpp
     support/win32/locale_win32.cpp
     support/win32/support.cpp
     )
diff --git a/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
index b130f058fc7c0..6b5cc3506b3fe 100644
--- a/libcxx/src/support/runtime/exception_pointer_msvc.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
@@ -7,68 +7,480 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include <exception>
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCreate(void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrDestroy(void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopy(void*, const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrAssign(void*, const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrCompare(const void*, const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrToBool(const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrSwap(void*, void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCurrentException(void*) noexcept;
-[[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrRethrow(const void*);
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopyException(void*, const void*, const void*) noexcept;
+_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wmultichar")
+
+#include <__exception/exception_ptr.h>
+#include <__memory/shared_ptr.h>
+#include <cstdlib>
+
+#include <Unknwn.h>
+#include <Windows.h>
+struct _ThrowInfo;
+#include <eh.h>
+#include <ehdata.h>
+#include <malloc.h> // alloca
+
+// Pre-V4 managed exception code
+#define MANAGED_EXCEPTION_CODE 0XE0434F4D
+
+// V4 and later managed exception code
+#define MANAGED_EXCEPTION_CODE_V4 0XE0434352
+
+extern "C" _LIBCPP_CRT_FUNC void* __cdecl __AdjustPointer(void*, const PMD&);
+extern "C" _LIBCPP_CRT_FUNC void** __cdecl __current_exception();
+
+static_assert(sizeof(std::exception_ptr) == sizeof(std::shared_ptr<const _EXCEPTION_RECORD>) &&
+                  alignof(std::exception_ptr) == alignof(std::shared_ptr<const _EXCEPTION_RECORD>),
+              "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
+
+namespace {
+
+typedef void(__stdcall* __prepare_for_throw_t)(void*);
+
+struct __winrt_exception_info {
+  void* __description;
+  void* __restricted_error_string;
+  void* __restricted_error_reference;
+  void* __capability_sid;
+  long __hr;
+  void* __restricted_info;
+  ThrowInfo* __throw_info;
+  unsigned int __size;
+  __prepare_for_throw_t __prepare_throw;
+};
+
+inline EHExceptionRecord* __get_current_exception() noexcept {
+  return *reinterpret_cast<EHExceptionRecord**>(__current_exception());
+}
+
+inline void __call_member_function_0(void* __this, void* __mfn) {
+  auto __fn = reinterpret_cast<void(__thiscall*)(void*)>(__mfn);
+  __fn(__this);
+}
+
+inline void __call_member_function_1(void* __this, void* __mfn, void* __arg) {
+  auto __fn = reinterpret_cast<void(__thiscall*)(void*, void*)>(__mfn);
+  __fn(__this, __arg);
+}
+
+inline void __call_member_function_2(void* __this, void* __mfn, void* __arg1, int __arg2) {
+  auto __fn = reinterpret_cast<void(__thiscall*)(void*, void*, int)>(__mfn);
+  __fn(__this, __arg1, __arg2);
+}
+
+void __populate_cpp_exception_record(
+    _EXCEPTION_RECORD& __record, const void* const __except_obj, ThrowInfo* __throw_info) noexcept {
+  __record.ExceptionCode           = EH_EXCEPTION_NUMBER;
+  __record.ExceptionFlags          = EXCEPTION_NONCONTINUABLE;
+  __record.ExceptionRecord         = nullptr;
+  __record.ExceptionAddress        = nullptr;
+  __record.NumberParameters        = EH_EXCEPTION_PARAMETERS;
+  __record.ExceptionInformation[0] = EH_MAGIC_NUMBER1;
+  __record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(__except_obj);
+
+  if (__throw_info && (__throw_info->attributes & TI_IsWinRT)) {
+    const auto __wei = (*static_cast<__winrt_exception_info** const*>(const_cast<void*>(__except_obj)))[-1];
+    __throw_info     = __wei->__throw_info;
+  }
+
+  __record.ExceptionInformation[2] = reinterpret_cast<ULONG_PTR>(__throw_info);
+
+#if _EH_RELATIVE_TYPEINFO
+  void* __throw_image_base =
+      __throw_info ? RtlPcToFileHeader(const_cast<void*>(static_cast<const void*>(__throw_info)), &__throw_image_base)
+                   : nullptr;
+  __record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(__throw_image_base);
+#endif
+
+  if (__throw_info && ((__throw_info->attributes & TI_IsPure)
+#if _EH_RELATIVE_TYPEINFO
+                       || !__throw_image_base
+#endif
+                       )) {
+    __record.ExceptionInformation[0] = EH_PURE_MAGIC_NUMBER1;
+  }
+}
+
+void __copy_exception_record(_EXCEPTION_RECORD& __dest, const _EXCEPTION_RECORD& __src) noexcept {
+  __dest.ExceptionCode    = __src.ExceptionCode;
+  __dest.ExceptionFlags   = __src.ExceptionFlags | EXCEPTION_NONCONTINUABLE;
+  __dest.ExceptionRecord  = nullptr;
+  __dest.ExceptionAddress = nullptr;
+  const auto __parameters = __src.NumberParameters;
+  __dest.NumberParameters = __parameters;
+
+  constexpr auto __max_parameters = static_cast<DWORD>(EXCEPTION_MAXIMUM_PARAMETERS);
+  const auto __in_use             = (__parameters < __max_parameters) ? __parameters : __max_parameters;
+  std::memcpy(__dest.ExceptionInformation, __src.ExceptionInformation, __in_use * sizeof(ULONG_PTR));
+  std::memset(&__dest.ExceptionInformation[__in_use], 0, (__max_parameters - __in_use) * sizeof(ULONG_PTR));
+}
+
+void __copy_exception_object(void* __dest, const void* __src, const CatchableType* const __type
+#if _EH_RELATIVE_TYPEINFO
+                             ,
+                             uintptr_t __throw_image_base
+#endif
+) {
+  if ((__type->properties & CT_IsSimpleType) || __type->copyFunction == 0) {
+    std::memcpy(__dest, __src, __type->sizeOrOffset);
+
+    if (__type->properties & CT_IsWinRTHandle) {
+      const auto __unknown = *static_cast<IUnknown* const*>(const_cast<void*>(__src));
+      if (__unknown) {
+        __unknown->AddRef();
+      }
+    }
+    return;
+  }
+
+#if _EH_RELATIVE_TYPEINFO
+  const auto __copy_func = reinterpret_cast<void*>(__throw_image_base + __type->copyFunction);
+#else
+  const auto __copy_func = __type->copyFunction;
+#endif
+
+  const auto __adjusted = __AdjustPointer(const_cast<void*>(__src), __type->thisDisplacement);
+  if (__type->properties & CT_HasVirtualBase) {
+    __call_member_function_2(__dest, __copy_func, __adjusted, 1);
+  } else {
+    __call_member_function_1(__dest, __copy_func, __adjusted);
+  }
+}
+
+template <class _StaticEx>
+class __exception_ptr_static final : public std::__shared_weak_count {
+private:
+  void __on_zero_shared() noexcept override {}
+  void __on_zero_shared_weak() noexcept override {}
+
+public:
+  __exception_ptr_static() noexcept : std::__shared_weak_count(0) {
+    __populate_cpp_exception_record(__record_, &__ex_, static_cast<ThrowInfo*>(std::__GetExceptionInfo(__ex_)));
+  }
+
+  static std::shared_ptr<const _EXCEPTION_RECORD> __get() noexcept {
+    struct __container {
+      union {
+        __exception_ptr_static __instance_;
+      };
+      __container() noexcept : __instance_() {}
+      ~__container() {}
+    };
+    static __container __storage;
+    __storage.__instance_.__add_shared();
+    return std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(
+        &__storage.__instance_.__record_, &__storage.__instance_);
+  }
+
+  _EXCEPTION_RECORD __record_;
+  _StaticEx __ex_;
+};
+
+class alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) __exception_ptr_normal final : public std::__shared_weak_count {
+private:
+  void __on_zero_shared() noexcept override {
+    const auto& __cpp_eh_record = reinterpret_cast<EHExceptionRecord&>(__record_);
+
+    if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_eh_record)) {
+      return;
+    }
+
+    const auto __throw_info = __cpp_eh_record.params.pThrowInfo;
+    if (!__throw_info) {
+      std::abort();
+    }
+
+    if (!__cpp_eh_record.params.pExceptionObject) {
+      return;
+    }
+
+#if _EH_RELATIVE_TYPEINFO
+    const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_eh_record.params.pThrowImageBase);
+    const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+        static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
+    const auto __type = reinterpret_cast<CatchableType*>(
+        static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
+#else
+    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+    if (__throw_info->pmfnUnwind) {
+#if _EH_RELATIVE_TYPEINFO
+      __call_member_function_0(__cpp_eh_record.params.pExceptionObject,
+                               reinterpret_cast<void*>(__throw_info->pmfnUnwind + __throw_image_base));
+#else
+      __call_member_function_0(__cpp_eh_record.params.pExceptionObject, __throw_info->pmfnUnwind);
+#endif
+    } else if (__type->properties & CT_IsWinRTHandle) {
+      const auto __unknown = *static_cast<IUnknown* const*>(__cpp_eh_record.params.pExceptionObject);
+      if (__unknown) {
+        __unknown->Release();
+      }
+    }
+  }
+
+  void __on_zero_shared_weak() noexcept override {
+    std::free(this);
+  }
+
+public:
+  explicit __exception_ptr_normal(const _EXCEPTION_RECORD& __record) noexcept : std::__shared_weak_count(0) {
+    __copy_exception_record(__record_, __record);
+  }
+
+  _EXCEPTION_RECORD __record_;
+};
+
+static_assert(sizeof(__exception_ptr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
+              "Exception in exception_ptr would be constructed with the wrong alignment");
+
+void __assign_seh_exception_ptr_from_record(
+    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const _EXCEPTION_RECORD& __record, void* const __rx_raw) noexcept {
+  if (!__rx_raw) {
+    __dest = __exception_ptr_static<std::bad_alloc>::__get();
+    return;
+  }
+
+  const auto __rx = ::new (__rx_raw) __exception_ptr_normal(__record);
+  __dest          = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+}
+
+void __assign_cpp_exception_ptr_from_record(
+    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const EHExceptionRecord& __record) noexcept {
+  const auto __throw_info = __record.params.pThrowInfo;
+#if _EH_RELATIVE_TYPEINFO
+  const auto __throw_image_base = reinterpret_cast<uintptr_t>(__record.params.pThrowImageBase);
+  const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+      static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
+  const auto __type = reinterpret_cast<CatchableType*>(
+      static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
+#else
+  const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+  const auto __except_obj_size = static_cast<size_t>(__type->sizeOrOffset);
+  const auto __alloc_size      = sizeof(__exception_ptr_normal) + __except_obj_size;
+  auto __rx_raw                = std::malloc(__alloc_size);
+  if (!__rx_raw) {
+    __dest = __exception_ptr_static<std::bad_alloc>::__get();
+    return;
+  }
+
+  try {
+    __copy_exception_object(static_cast<__exception_ptr_normal*>(__rx_raw) + 1,
+                            __record.params.pExceptionObject,
+                            __type
+#if _EH_RELATIVE_TYPEINFO
+                            ,
+                            __throw_image_base
+#endif
+    );
+
+    const auto __rx = ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__record));
+    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
+        static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
+    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+  } catch (...) {
+    const auto* __inner_record_ptr = __get_current_exception();
+    if (!__inner_record_ptr) {
+      std::free(__rx_raw);
+      __dest = __exception_ptr_static<std::bad_exception>::__get();
+      return;
+    }
+    const auto& __inner_record = *__inner_record_ptr;
+    if (__inner_record.ExceptionCode == MANAGED_EXCEPTION_CODE ||
+        __inner_record.ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+      std::free(__rx_raw);
+      __dest = __exception_ptr_static<std::bad_exception>::__get();
+      return;
+    }
+
+    if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&__inner_record)) {
+      __assign_seh_exception_ptr_from_record(
+          __dest, reinterpret_cast<const _EXCEPTION_RECORD&>(__inner_record), __rx_raw);
+      return;
+    }
+
+    const auto __inner_throw = __inner_record.params.pThrowInfo;
+#if _EH_RELATIVE_TYPEINFO
+    const auto __inner_throw_image_base = reinterpret_cast<uintptr_t>(__inner_record.params.pThrowImageBase);
+    const auto __inner_catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+        static_cast<uintptr_t>(__inner_throw->pCatchableTypeArray) + __inner_throw_image_base);
+    const auto __inner_type = reinterpret_cast<CatchableType*>(
+        static_cast<uintptr_t>(__inner_catchable_type_array->arrayOfCatchableTypes[0]) + __inner_throw_image_base);
+#else
+    const auto __inner_type = __inner_throw->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+    const auto __inner_except_size = static_cast<size_t>(__inner_type->sizeOrOffset);
+    const auto __inner_alloc_size  = sizeof(__exception_ptr_normal) + __inner_except_size;
+    if (__inner_alloc_size > __alloc_size) {
+      std::free(__rx_raw);
+      __rx_raw = std::malloc(__inner_alloc_size);
+      if (!__rx_raw) {
+        __dest = __exception_ptr_static<std::bad_alloc>::__get();
+        return;
+      }
+    }
+
+    try {
+      __copy_exception_object(static_cast<__exception_ptr_normal*>(__rx_raw) + 1,
+                              __inner_record.params.pExceptionObject,
+                              __inner_type
+#if _EH_RELATIVE_TYPEINFO
+                              ,
+                              __inner_throw_image_base
+#endif
+      );
+    } catch (...) {
+      std::free(__rx_raw);
+      __dest = __exception_ptr_static<std::bad_exception>::__get();
+      return;
+    }
+
+    const auto __rx =
+        ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__inner_record));
+    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
+        static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
+    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+  }
+}
+
+inline std::shared_ptr<const _EXCEPTION_RECORD>& __to_shared(std::exception_ptr& __ptr) noexcept {
+  return *reinterpret_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(&__ptr);
+}
+
+inline const std::shared_ptr<const _EXCEPTION_RECORD>& __to_shared(const std::exception_ptr& __ptr) noexcept {
+  return *reinterpret_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(&__ptr);
+}
+
+} // namespace
 
 namespace std {
 
-exception_ptr::exception_ptr() noexcept { __ExceptionPtrCreate(this); }
-exception_ptr::exception_ptr(nullptr_t) noexcept { __ExceptionPtrCreate(this); }
+exception_ptr::exception_ptr() noexcept {
+  ::new (this) shared_ptr<const _EXCEPTION_RECORD>();
+}
+exception_ptr::exception_ptr(nullptr_t) noexcept {
+  ::new (this) shared_ptr<const _EXCEPTION_RECORD>();
+}
 
-exception_ptr::exception_ptr(const exception_ptr& __other) noexcept { __ExceptionPtrCopy(this, &__other); }
+exception_ptr::exception_ptr(const exception_ptr& __other) noexcept {
+  ::new (this) shared_ptr<const _EXCEPTION_RECORD>(__to_shared(__other));
+}
 exception_ptr& exception_ptr::operator=(const exception_ptr& __other) noexcept {
-  __ExceptionPtrAssign(this, &__other);
+  __to_shared(*this) = __to_shared(__other);
   return *this;
 }
 
 exception_ptr& exception_ptr::operator=(nullptr_t) noexcept {
-  exception_ptr dummy;
-  __ExceptionPtrAssign(this, &dummy);
+  __to_shared(*this) = nullptr;
   return *this;
 }
 
-exception_ptr::~exception_ptr() noexcept { __ExceptionPtrDestroy(this); }
+exception_ptr::~exception_ptr() noexcept {
+  __to_shared(*this).~shared_ptr();
+}
 
-exception_ptr::operator bool() const noexcept { return __ExceptionPtrToBool(this); }
+exception_ptr::operator bool() const noexcept {
+  return static_cast<bool>(__to_shared(*this));
+}
 
 bool operator==(const exception_ptr& __x, const exception_ptr& __y) noexcept {
-  return __ExceptionPtrCompare(&__x, &__y);
+  return __to_shared(__x) == __to_shared(__y);
 }
 
-void swap(exception_ptr& lhs, exception_ptr& rhs) noexcept { __ExceptionPtrSwap(&rhs, &lhs); }
+void swap(exception_ptr& __lhs, exception_ptr& __rhs) noexcept {
+  __to_shared(__lhs).swap(__to_shared(__rhs));
+}
 
 exception_ptr __copy_exception_ptr(void* __except, const void* __ptr) {
   exception_ptr __ret = nullptr;
-  if (__ptr)
-    __ExceptionPtrCopyException(&__ret, __except, __ptr);
+  if (!__ptr) {
+    return __ret;
+  }
+
+  _EXCEPTION_RECORD __record;
+  __populate_cpp_exception_record(__record, __except, static_cast<ThrowInfo*>(const_cast<void*>(__ptr)));
+  __assign_cpp_exception_ptr_from_record(__to_shared(__ret), reinterpret_cast<const EHExceptionRecord&>(__record));
   return __ret;
 }
 
 exception_ptr current_exception() noexcept {
   exception_ptr __ret;
-  __ExceptionPtrCurrentException(&__ret);
+  const auto* __record = __get_current_exception();
+  if (!__record || __record->ExceptionCode == MANAGED_EXCEPTION_CODE ||
+      __record->ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
+    return __ret;
+  }
+
+  auto& __dest = __to_shared(__ret);
+  if (PER_IS_MSVC_PURE_OR_NATIVE_EH(__record)) {
+    __assign_cpp_exception_ptr_from_record(__dest, *__record);
+  } else {
+    __assign_seh_exception_ptr_from_record(
+        __dest, reinterpret_cast<const _EXCEPTION_RECORD&>(*__record), std::malloc(sizeof(__exception_ptr_normal)));
+  }
   return __ret;
 }
 
-[[noreturn]] void rethrow_exception(exception_ptr p) { __ExceptionPtrRethrow(&p); }
+[[noreturn]] void rethrow_exception(exception_ptr __p) {
+  if (!__p) {
+    throw bad_exception();
+  }
+
+  auto __record_copy = *__to_shared(__p);
+  auto& __cpp_record = reinterpret_cast<EHExceptionRecord&>(__record_copy);
+  if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_record)) {
+    const auto __throw_info = __cpp_record.params.pThrowInfo;
+    if (!__cpp_record.params.pExceptionObject || !__throw_info || !__throw_info->pCatchableTypeArray) {
+      std::abort();
+    }
+
+#if _EH_RELATIVE_TYPEINFO
+    const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_record.params.pThrowImageBase);
+    const auto __catchable_type_array =
+        reinterpret_cast<const CatchableTypeArray*>(__throw_image_base + __throw_info->pCatchableTypeArray);
+#else
+    const auto __catchable_type_array = __throw_info->pCatchableTypeArray;
+#endif
+
+    if (__catchable_type_array->nCatchableTypes <= 0) {
+      std::abort();
+    }
+
+#if _EH_RELATIVE_TYPEINFO
+    const auto __type = reinterpret_cast<CatchableType*>(
+        static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
+#else
+    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+#endif
+
+#pragma warning(suppress : 6255)
+    void* __exception_buffer = alloca(__type->sizeOrOffset);
+    __copy_exception_object(__exception_buffer, __cpp_record.params.pExceptionObject, __type
+#if _EH_RELATIVE_TYPEINFO
+                            ,
+                            __throw_image_base
+#endif
+    );
+
+    __cpp_record.params.pExceptionObject = __exception_buffer;
+  }
+
+  RaiseException(__record_copy.ExceptionCode, __record_copy.ExceptionFlags, __record_copy.NumberParameters,
+                 __record_copy.ExceptionInformation);
+  std::abort();
+}
 
 nested_exception::nested_exception() noexcept : __ptr_(current_exception()) {}
 
 nested_exception::~nested_exception() noexcept {}
 
 [[noreturn]] void nested_exception::rethrow_nested() const {
-  if (__ptr_ == nullptr)
+  if (__ptr_ == nullptr) {
     terminate();
+  }
   rethrow_exception(__ptr_);
 }
 
diff --git a/libcxx/src/support/win32/excptptr.cpp b/libcxx/src/support/win32/excptptr.cpp
deleted file mode 100644
index 89c9793ef8f6f..0000000000000
--- a/libcxx/src/support/win32/excptptr.cpp
+++ /dev/null
@@ -1,470 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// Copyright (c) Microsoft Corporation.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-
-#include <__config>
-
-_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wmultichar")
-
-#include <__exception/exception_ptr.h>
-#include <__memory/shared_ptr.h>
-#include <cstdlib>
-
-#include <Unknwn.h>
-#include <Windows.h>
-struct _ThrowInfo;
-#include <eh.h>
-#include <ehdata.h>
-#include <malloc.h> // alloca
-
-// Pre-V4 managed exception code
-#define MANAGED_EXCEPTION_CODE 0XE0434F4D
-
-// V4 and later managed exception code
-#define MANAGED_EXCEPTION_CODE_V4 0XE0434352
-
-extern "C" _LIBCPP_CRT_FUNC void* __cdecl __AdjustPointer(void*, const PMD&);
-extern "C" _LIBCPP_CRT_FUNC void** __cdecl __current_exception();
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCreate(void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrDestroy(void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopy(void*, const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrAssign(void*, const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrCompare(const void*, const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrToBool(const void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrSwap(void*, void*) noexcept;
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCurrentException(void*) noexcept;
-[[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrRethrow(const void*);
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopyException(void*, const void*, const void*) noexcept;
-
-static_assert(sizeof(std::exception_ptr) == sizeof(std::shared_ptr<const _EXCEPTION_RECORD>) &&
-                  alignof(std::exception_ptr) == alignof(std::shared_ptr<const _EXCEPTION_RECORD>),
-              "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
-
-namespace {
-
-typedef void(__stdcall* __prepare_for_throw_t)(void*);
-
-struct __winrt_exception_info {
-  void* __description;
-  void* __restricted_error_string;
-  void* __restricted_error_reference;
-  void* __capability_sid;
-  long __hr;
-  void* __restricted_info;
-  ThrowInfo* __throw_info;
-  unsigned int __size;
-  __prepare_for_throw_t __prepare_throw;
-};
-
-inline EHExceptionRecord* __get_current_exception() noexcept {
-  return *reinterpret_cast<EHExceptionRecord**>(__current_exception());
-}
-
-inline void __call_member_function_0(void* __this, void* __mfn) {
-  auto __fn = reinterpret_cast<void(__thiscall*)(void*)>(__mfn);
-  __fn(__this);
-}
-
-inline void __call_member_function_1(void* __this, void* __mfn, void* __arg) {
-  auto __fn = reinterpret_cast<void(__thiscall*)(void*, void*)>(__mfn);
-  __fn(__this, __arg);
-}
-
-inline void __call_member_function_2(void* __this, void* __mfn, void* __arg1, int __arg2) {
-  auto __fn = reinterpret_cast<void(__thiscall*)(void*, void*, int)>(__mfn);
-  __fn(__this, __arg1, __arg2);
-}
-
-void __populate_cpp_exception_record(
-    _EXCEPTION_RECORD& __record, const void* const __except_obj, ThrowInfo* __throw_info) noexcept {
-  __record.ExceptionCode           = EH_EXCEPTION_NUMBER;
-  __record.ExceptionFlags          = EXCEPTION_NONCONTINUABLE;
-  __record.ExceptionRecord         = nullptr;
-  __record.ExceptionAddress        = nullptr;
-  __record.NumberParameters        = EH_EXCEPTION_PARAMETERS;
-  __record.ExceptionInformation[0] = EH_MAGIC_NUMBER1;
-  __record.ExceptionInformation[1] = reinterpret_cast<ULONG_PTR>(__except_obj);
-
-  if (__throw_info && (__throw_info->attributes & TI_IsWinRT)) {
-    const auto __wei = (*static_cast<__winrt_exception_info** const*>(const_cast<void*>(__except_obj)))[-1];
-    __throw_info     = __wei->__throw_info;
-  }
-
-  __record.ExceptionInformation[2] = reinterpret_cast<ULONG_PTR>(__throw_info);
-
-#if _EH_RELATIVE_TYPEINFO
-  void* __throw_image_base =
-      __throw_info ? RtlPcToFileHeader(const_cast<void*>(static_cast<const void*>(__throw_info)), &__throw_image_base)
-                   : nullptr;
-  __record.ExceptionInformation[3] = reinterpret_cast<ULONG_PTR>(__throw_image_base);
-#endif
-
-  if (__throw_info && ((__throw_info->attributes & TI_IsPure)
-#if _EH_RELATIVE_TYPEINFO
-                       || !__throw_image_base
-#endif
-                       )) {
-    __record.ExceptionInformation[0] = EH_PURE_MAGIC_NUMBER1;
-  }
-}
-
-void __copy_exception_record(_EXCEPTION_RECORD& __dest, const _EXCEPTION_RECORD& __src) noexcept {
-  __dest.ExceptionCode    = __src.ExceptionCode;
-  __dest.ExceptionFlags   = __src.ExceptionFlags | EXCEPTION_NONCONTINUABLE;
-  __dest.ExceptionRecord  = nullptr;
-  __dest.ExceptionAddress = nullptr;
-  const auto __parameters = __src.NumberParameters;
-  __dest.NumberParameters = __parameters;
-
-  constexpr auto __max_parameters = static_cast<DWORD>(EXCEPTION_MAXIMUM_PARAMETERS);
-  const auto __in_use             = (__parameters < __max_parameters) ? __parameters : __max_parameters;
-  std::memcpy(__dest.ExceptionInformation, __src.ExceptionInformation, __in_use * sizeof(ULONG_PTR));
-  std::memset(&__dest.ExceptionInformation[__in_use], 0, (__max_parameters - __in_use) * sizeof(ULONG_PTR));
-}
-
-void __copy_exception_object(void* __dest, const void* __src, const CatchableType* const __type
-#if _EH_RELATIVE_TYPEINFO
-                             ,
-                             uintptr_t __throw_image_base
-#endif
-) {
-  if ((__type->properties & CT_IsSimpleType) || __type->copyFunction == 0) {
-    std::memcpy(__dest, __src, __type->sizeOrOffset);
-
-    if (__type->properties & CT_IsWinRTHandle) {
-      const auto __unknown = *static_cast<IUnknown* const*>(const_cast<void*>(__src));
-      if (__unknown) {
-        __unknown->AddRef();
-      }
-    }
-    return;
-  }
-
-#if _EH_RELATIVE_TYPEINFO
-  const auto __copy_func = reinterpret_cast<void*>(__throw_image_base + __type->copyFunction);
-#else
-  const auto __copy_func = __type->copyFunction;
-#endif
-
-  const auto __adjusted = __AdjustPointer(const_cast<void*>(__src), __type->thisDisplacement);
-  if (__type->properties & CT_HasVirtualBase) {
-    __call_member_function_2(__dest, __copy_func, __adjusted, 1);
-  } else {
-    __call_member_function_1(__dest, __copy_func, __adjusted);
-  }
-}
-
-template <class _StaticEx>
-class __exception_ptr_static final : public std::__shared_weak_count {
-private:
-  void __on_zero_shared() noexcept override {}
-  void __on_zero_shared_weak() noexcept override {}
-
-public:
-  __exception_ptr_static() noexcept : std::__shared_weak_count(0) {
-    __populate_cpp_exception_record(__record_, &__ex_, static_cast<ThrowInfo*>(std::__GetExceptionInfo(__ex_)));
-  }
-
-  static std::shared_ptr<const _EXCEPTION_RECORD> __get() noexcept {
-    struct __container {
-      union {
-        __exception_ptr_static __instance_;
-      };
-      __container() noexcept : __instance_() {}
-      ~__container() {}
-    };
-    static __container __storage;
-    __storage.__instance_.__add_shared();
-    return std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(
-        &__storage.__instance_.__record_, &__storage.__instance_);
-  }
-
-  _EXCEPTION_RECORD __record_;
-  _StaticEx __ex_;
-};
-
-class alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) __exception_ptr_normal final : public std::__shared_weak_count {
-private:
-  void __on_zero_shared() noexcept override {
-    const auto& __cpp_eh_record = reinterpret_cast<EHExceptionRecord&>(__record_);
-
-    if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_eh_record)) {
-      return;
-    }
-
-    const auto __throw_info = __cpp_eh_record.params.pThrowInfo;
-    if (!__throw_info) {
-      std::abort();
-    }
-
-    if (!__cpp_eh_record.params.pExceptionObject) {
-      return;
-    }
-
-#if _EH_RELATIVE_TYPEINFO
-    const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_eh_record.params.pThrowImageBase);
-    const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
-        static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
-    const auto __type = reinterpret_cast<CatchableType*>(
-        static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
-#else
-    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif
-
-    if (__throw_info->pmfnUnwind) {
-#if _EH_RELATIVE_TYPEINFO
-      __call_member_function_0(__cpp_eh_record.params.pExceptionObject,
-                               reinterpret_cast<void*>(__throw_info->pmfnUnwind + __throw_image_base));
-#else
-      __call_member_function_0(__cpp_eh_record.params.pExceptionObject, __throw_info->pmfnUnwind);
-#endif
-    } else if (__type->properties & CT_IsWinRTHandle) {
-      const auto __unknown = *static_cast<IUnknown* const*>(__cpp_eh_record.params.pExceptionObject);
-      if (__unknown) {
-        __unknown->Release();
-      }
-    }
-  }
-
-  void __on_zero_shared_weak() noexcept override {
-    std::free(this);
-  }
-
-public:
-  explicit __exception_ptr_normal(const _EXCEPTION_RECORD& __record) noexcept : std::__shared_weak_count(0) {
-    __copy_exception_record(__record_, __record);
-  }
-
-  _EXCEPTION_RECORD __record_;
-};
-
-static_assert(sizeof(__exception_ptr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
-              "Exception in exception_ptr would be constructed with the wrong alignment");
-
-void __assign_seh_exception_ptr_from_record(
-    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const _EXCEPTION_RECORD& __record, void* const __rx_raw) noexcept {
-  if (!__rx_raw) {
-    __dest = __exception_ptr_static<std::bad_alloc>::__get();
-    return;
-  }
-
-  const auto __rx = ::new (__rx_raw) __exception_ptr_normal(__record);
-  __dest          = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
-}
-
-void __assign_cpp_exception_ptr_from_record(
-    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const EHExceptionRecord& __record) noexcept {
-  const auto __throw_info = __record.params.pThrowInfo;
-#if _EH_RELATIVE_TYPEINFO
-  const auto __throw_image_base = reinterpret_cast<uintptr_t>(__record.params.pThrowImageBase);
-  const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
-      static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
-  const auto __type = reinterpret_cast<CatchableType*>(
-      static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
-#else
-  const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif
-
-  const auto __except_obj_size = static_cast<size_t>(__type->sizeOrOffset);
-  const auto __alloc_size      = sizeof(__exception_ptr_normal) + __except_obj_size;
-  auto __rx_raw                = std::malloc(__alloc_size);
-  if (!__rx_raw) {
-    __dest = __exception_ptr_static<std::bad_alloc>::__get();
-    return;
-  }
-
-  try {
-    __copy_exception_object(static_cast<__exception_ptr_normal*>(__rx_raw) + 1,
-                            __record.params.pExceptionObject,
-                            __type
-#if _EH_RELATIVE_TYPEINFO
-                            ,
-                            __throw_image_base
-#endif
-    );
-
-    const auto __rx = ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__record));
-    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
-        static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
-    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
-  } catch (...) {
-    const auto* __inner_record_ptr = __get_current_exception();
-    if (!__inner_record_ptr) {
-      std::free(__rx_raw);
-      __dest = __exception_ptr_static<std::bad_exception>::__get();
-      return;
-    }
-    const auto& __inner_record = *__inner_record_ptr;
-    if (__inner_record.ExceptionCode == MANAGED_EXCEPTION_CODE ||
-        __inner_record.ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
-      std::free(__rx_raw);
-      __dest = __exception_ptr_static<std::bad_exception>::__get();
-      return;
-    }
-
-    if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&__inner_record)) {
-      __assign_seh_exception_ptr_from_record(
-          __dest, reinterpret_cast<const _EXCEPTION_RECORD&>(__inner_record), __rx_raw);
-      return;
-    }
-
-    const auto __inner_throw = __inner_record.params.pThrowInfo;
-#if _EH_RELATIVE_TYPEINFO
-    const auto __inner_throw_image_base = reinterpret_cast<uintptr_t>(__inner_record.params.pThrowImageBase);
-    const auto __inner_catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
-        static_cast<uintptr_t>(__inner_throw->pCatchableTypeArray) + __inner_throw_image_base);
-    const auto __inner_type = reinterpret_cast<CatchableType*>(
-        static_cast<uintptr_t>(__inner_catchable_type_array->arrayOfCatchableTypes[0]) + __inner_throw_image_base);
-#else
-    const auto __inner_type = __inner_throw->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif
-
-    const auto __inner_except_size = static_cast<size_t>(__inner_type->sizeOrOffset);
-    const auto __inner_alloc_size  = sizeof(__exception_ptr_normal) + __inner_except_size;
-    if (__inner_alloc_size > __alloc_size) {
-      std::free(__rx_raw);
-      __rx_raw = std::malloc(__inner_alloc_size);
-      if (!__rx_raw) {
-        __dest = __exception_ptr_static<std::bad_alloc>::__get();
-        return;
-      }
-    }
-
-    try {
-      __copy_exception_object(static_cast<__exception_ptr_normal*>(__rx_raw) + 1,
-                              __inner_record.params.pExceptionObject,
-                              __inner_type
-#if _EH_RELATIVE_TYPEINFO
-                              ,
-                              __inner_throw_image_base
-#endif
-      );
-    } catch (...) {
-      std::free(__rx_raw);
-      __dest = __exception_ptr_static<std::bad_exception>::__get();
-      return;
-    }
-
-    const auto __rx =
-        ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__inner_record));
-    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
-        static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
-    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
-  }
-}
-
-} // namespace
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCreate(void* __ptr) noexcept {
-  ::new (__ptr) std::shared_ptr<const _EXCEPTION_RECORD>();
-}
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrDestroy(void* __ptr) noexcept {
-  static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr)->~shared_ptr();
-}
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopy(void* __dest, const void* __src) noexcept {
-  ::new (__dest) std::shared_ptr<const _EXCEPTION_RECORD>(
-      *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__src));
-}
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrAssign(void* __dest, const void* __src) noexcept {
-  *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__dest) =
-      *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__src);
-}
-
-_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrCompare(const void* __lhs, const void* __rhs) noexcept {
-  return *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__lhs) ==
-         *static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__rhs);
-}
-
-_LIBCPP_EXPORTED_FROM_ABI bool __cdecl __ExceptionPtrToBool(const void* __ptr) noexcept {
-  return static_cast<bool>(*static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr));
-}
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrSwap(void* __lhs, void* __rhs) noexcept {
-  static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__lhs)->swap(
-      *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__rhs));
-}
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCurrentException(void* __ptr) noexcept {
-  const auto __record = __get_current_exception();
-  if (!__record || __record->ExceptionCode == MANAGED_EXCEPTION_CODE ||
-      __record->ExceptionCode == MANAGED_EXCEPTION_CODE_V4) {
-    return;
-  }
-
-  auto& __dest = *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr);
-  if (PER_IS_MSVC_PURE_OR_NATIVE_EH(__record)) {
-    __assign_cpp_exception_ptr_from_record(__dest, *__record);
-  } else {
-    __assign_seh_exception_ptr_from_record(
-        __dest, reinterpret_cast<_EXCEPTION_RECORD&>(*__record), std::malloc(sizeof(__exception_ptr_normal)));
-  }
-}
-
-[[noreturn]] _LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrRethrow(const void* __ptr_raw) {
-  const auto* __ptr = static_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr_raw);
-  if (!*__ptr) {
-    throw std::bad_exception();
-  }
-
-  auto __record_copy = **__ptr;
-  auto& __cpp_record = reinterpret_cast<EHExceptionRecord&>(__record_copy);
-  if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_record)) {
-    const auto __throw_info = __cpp_record.params.pThrowInfo;
-    if (!__cpp_record.params.pExceptionObject || !__throw_info || !__throw_info->pCatchableTypeArray) {
-      std::abort();
-    }
-
-#if _EH_RELATIVE_TYPEINFO
-    const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_record.params.pThrowImageBase);
-    const auto __catchable_type_array =
-        reinterpret_cast<const CatchableTypeArray*>(__throw_image_base + __throw_info->pCatchableTypeArray);
-#else
-    const auto __catchable_type_array = __throw_info->pCatchableTypeArray;
-#endif
-
-    if (__catchable_type_array->nCatchableTypes <= 0) {
-      std::abort();
-    }
-
-#if _EH_RELATIVE_TYPEINFO
-    const auto __type = reinterpret_cast<CatchableType*>(
-        static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
-#else
-    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
-#endif
-
-#pragma warning(suppress : 6255)
-    void* __exception_buffer = alloca(__type->sizeOrOffset);
-    __copy_exception_object(__exception_buffer, __cpp_record.params.pExceptionObject, __type
-#if _EH_RELATIVE_TYPEINFO
-                            ,
-                            __throw_image_base
-#endif
-    );
-
-    __cpp_record.params.pExceptionObject = __exception_buffer;
-  }
-
-  RaiseException(__record_copy.ExceptionCode, __record_copy.ExceptionFlags, __record_copy.NumberParameters,
-                 __record_copy.ExceptionInformation);
-  std::abort();
-}
-
-_LIBCPP_EXPORTED_FROM_ABI void __cdecl __ExceptionPtrCopyException(
-    void* __ptr, const void* __except_raw, const void* __throw_raw) noexcept {
-  _EXCEPTION_RECORD __record;
-  __populate_cpp_exception_record(__record, __except_raw, static_cast<ThrowInfo*>(const_cast<void*>(__throw_raw)));
-  __assign_cpp_exception_ptr_from_record(
-      *static_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(__ptr), reinterpret_cast<const EHExceptionRecord&>(__record));
-}

>From 632c42dc47b06d6c2858fb71dda28004fc4075cc Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Mon, 22 Jun 2026 08:03:34 +0000
Subject: [PATCH 08/10] Use reference-counted control block representation

* Removed the platform #ifdef _LIBCPP_ABI_MICROSOFT bifurcation for
  class exception_ptr in exception_ptr.h. On all ABI targets (Itanium,
  MSVC, etc.), sizeof(std::exception_ptr) == sizeof(void*) (8 bytes on
  x64).
* std::exception_ptr now shares the exact same single-pointer data
  member (void* __ptr_;), trivial relocatability attribute (using
  __trivially_relocatable), and inline operations (default constructor,
  nullptr constructor, move constructor, move assignment, operator
  bool(), operator==, operator!=, and swap) across all operating
  systems.
* __exception_ptr_storage encapsulates the _EXCEPTION_RECORD __record_;
  member immediately following the std::__shared_count base layout (16
  bytes on x64), providing guaranteed standard C++ inheritance layout
  for downcasting.
* For immortal static exception objects (std::bad_alloc,
  std::bad_exception), __exception_ptr_static::__on_zero_shared() is
  overridden as a no-op.
* For dynamically captured heap exceptions,
  __exception_ptr_normal::__on_zero_shared() invokes the SEH object
  destructors (pmfnUnwind / WinRT Release()) and frees heap memory
  (std::free(this)).
---
 libcxx/include/__exception/exception_ptr.h    |  35 +--
 .../runtime/exception_pointer_msvc.ipp        | 217 ++++++++----------
 2 files changed, 96 insertions(+), 156 deletions(-)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index f7ed00d555836..0c86313545fbf 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -62,10 +62,6 @@ _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
 _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
 _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
-#ifndef _LIBCPP_ABI_MICROSOFT
-
-inline _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT;
-
 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 
@@ -74,6 +70,8 @@ class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   template <class _Ep>
   friend _LIBCPP_HIDE_FROM_ABI exception_ptr __make_exception_ptr_explicit(_Ep&) _NOEXCEPT;
 
+  friend _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void*, const void*);
+
 public:
   // exception_ptr is basically a COW string so it is trivially relocatable.
   using __trivially_relocatable _LIBCPP_NODEBUG = exception_ptr;
@@ -113,6 +111,8 @@ inline _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _
   std::swap(__x.__ptr_, __y.__ptr_);
 }
 
+#ifndef _LIBCPP_ABI_MICROSOFT
+
 #  if _LIBCPP_HAS_EXCEPTIONS
 #    if _LIBCPP_HAS_RTTI && _LIBCPP_AVAILABILITY_HAS_INIT_PRIMARY_EXCEPTION
 template <class _Ep>
@@ -180,34 +180,7 @@ _LIBCPP_HIDE_FROM_ABI exception_ptr make_exception_ptr(_Ep) _NOEXCEPT {
 
 #else // defined(_LIBCPP_ABI_MICROSOFT)
 
-class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
-  _LIBCPP_DIAGNOSTIC_PUSH
-  _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field")
-  void* __ptr1_;
-  void* __ptr2_;
-  _LIBCPP_DIAGNOSTIC_POP
-
-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;
-  explicit operator bool() const _NOEXCEPT;
-};
-
-_LIBCPP_EXPORTED_FROM_ABI bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT;
-
-inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT {
-  return !(__x == __y);
-}
-
-_LIBCPP_EXPORTED_FROM_ABI void swap(exception_ptr&, exception_ptr&) _NOEXCEPT;
-
 _LIBCPP_EXPORTED_FROM_ABI exception_ptr __copy_exception_ptr(void* __except, const void* __ptr);
-_LIBCPP_EXPORTED_FROM_ABI exception_ptr current_exception() _NOEXCEPT;
-[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void rethrow_exception(exception_ptr);
 
 // This is a built-in template function which automagically extracts the required
 // information.
diff --git a/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
index 6b5cc3506b3fe..6a739f442f065 100644
--- a/libcxx/src/support/runtime/exception_pointer_msvc.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
@@ -10,8 +10,9 @@
 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wmultichar")
 
 #include <__exception/exception_ptr.h>
-#include <__memory/shared_ptr.h>
+#include <__memory/shared_count.h>
 #include <cstdlib>
+#include <cstring>
 
 #include <Unknwn.h>
 #include <Windows.h>
@@ -29,10 +30,6 @@ struct _ThrowInfo;
 extern "C" _LIBCPP_CRT_FUNC void* __cdecl __AdjustPointer(void*, const PMD&);
 extern "C" _LIBCPP_CRT_FUNC void** __cdecl __current_exception();
 
-static_assert(sizeof(std::exception_ptr) == sizeof(std::shared_ptr<const _EXCEPTION_RECORD>) &&
-                  alignof(std::exception_ptr) == alignof(std::shared_ptr<const _EXCEPTION_RECORD>),
-              "std::exception_ptr and std::shared_ptr<const _EXCEPTION_RECORD> must have the same layout.");
-
 namespace {
 
 typedef void(__stdcall* __prepare_for_throw_t)(void*);
@@ -147,18 +144,22 @@ void __copy_exception_object(void* __dest, const void* __src, const CatchableTyp
   }
 }
 
+struct __exception_ptr_storage : public std::__shared_count {
+  _EXCEPTION_RECORD __record_;
+  explicit __exception_ptr_storage(long __refs = 0) noexcept : std::__shared_count(__refs) {}
+};
+
 template <class _StaticEx>
-class __exception_ptr_static final : public std::__shared_weak_count {
-private:
-  void __on_zero_shared() noexcept override {}
-  void __on_zero_shared_weak() noexcept override {}
+struct __exception_ptr_static final : public __exception_ptr_storage {
+  _StaticEx __ex_;
 
-public:
-  __exception_ptr_static() noexcept : std::__shared_weak_count(0) {
-    __populate_cpp_exception_record(__record_, &__ex_, static_cast<ThrowInfo*>(std::__GetExceptionInfo(__ex_)));
+  __exception_ptr_static() noexcept : __exception_ptr_storage(0) {
+    __populate_cpp_exception_record(__record_, &__ex_, static_cast<ThrowInfo*>(__GetExceptionInfo(__ex_)));
   }
 
-  static std::shared_ptr<const _EXCEPTION_RECORD> __get() noexcept {
+  void __on_zero_shared() noexcept override {}
+
+  static __exception_ptr_storage* __get() noexcept {
     struct __container {
       union {
         __exception_ptr_static __instance_;
@@ -167,100 +168,76 @@ public:
       ~__container() {}
     };
     static __container __storage;
-    __storage.__instance_.__add_shared();
-    return std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(
-        &__storage.__instance_.__record_, &__storage.__instance_);
+    return &__storage.__instance_;
   }
-
-  _EXCEPTION_RECORD __record_;
-  _StaticEx __ex_;
 };
 
-class alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) __exception_ptr_normal final : public std::__shared_weak_count {
-private:
+struct alignas(__STDCPP_DEFAULT_NEW_ALIGNMENT__) __exception_ptr_normal final : public __exception_ptr_storage {
+  explicit __exception_ptr_normal(const _EXCEPTION_RECORD& __record) noexcept : __exception_ptr_storage(0) {
+    __copy_exception_record(__record_, __record);
+  }
+
   void __on_zero_shared() noexcept override {
     const auto& __cpp_eh_record = reinterpret_cast<EHExceptionRecord&>(__record_);
-
-    if (!PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_eh_record)) {
-      return;
-    }
-
-    const auto __throw_info = __cpp_eh_record.params.pThrowInfo;
-    if (!__throw_info) {
-      std::abort();
-    }
-
-    if (!__cpp_eh_record.params.pExceptionObject) {
-      return;
-    }
-
+    if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_eh_record)) {
+      const auto* __throw_info = __cpp_eh_record.params.pThrowInfo;
+      if (__throw_info && __cpp_eh_record.params.pExceptionObject) {
 #if _EH_RELATIVE_TYPEINFO
-    const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_eh_record.params.pThrowImageBase);
-    const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
-        static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
-    const auto __type = reinterpret_cast<CatchableType*>(
-        static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
+        const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_eh_record.params.pThrowImageBase);
+        const auto* __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+            static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
+        const auto* __type = reinterpret_cast<CatchableType*>(
+            static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
 #else
-    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+        const auto* __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
 #endif
-
-    if (__throw_info->pmfnUnwind) {
+        if (__throw_info->pmfnUnwind) {
 #if _EH_RELATIVE_TYPEINFO
-      __call_member_function_0(__cpp_eh_record.params.pExceptionObject,
-                               reinterpret_cast<void*>(__throw_info->pmfnUnwind + __throw_image_base));
+          __call_member_function_0(__cpp_eh_record.params.pExceptionObject,
+                                   reinterpret_cast<void*>(__throw_info->pmfnUnwind + __throw_image_base));
 #else
-      __call_member_function_0(__cpp_eh_record.params.pExceptionObject, __throw_info->pmfnUnwind);
+          __call_member_function_0(__cpp_eh_record.params.pExceptionObject, __throw_info->pmfnUnwind);
 #endif
-    } else if (__type->properties & CT_IsWinRTHandle) {
-      const auto __unknown = *static_cast<IUnknown* const*>(__cpp_eh_record.params.pExceptionObject);
-      if (__unknown) {
-        __unknown->Release();
+        } else if (__type->properties & CT_IsWinRTHandle) {
+          const auto* __unknown = *static_cast<IUnknown* const*>(__cpp_eh_record.params.pExceptionObject);
+          if (__unknown) {
+            const_cast<IUnknown*>(__unknown)->Release();
+          }
+        }
       }
     }
-  }
-
-  void __on_zero_shared_weak() noexcept override {
     std::free(this);
   }
-
-public:
-  explicit __exception_ptr_normal(const _EXCEPTION_RECORD& __record) noexcept : std::__shared_weak_count(0) {
-    __copy_exception_record(__record_, __record);
-  }
-
-  _EXCEPTION_RECORD __record_;
 };
 
 static_assert(sizeof(__exception_ptr_normal) % __STDCPP_DEFAULT_NEW_ALIGNMENT__ == 0,
               "Exception in exception_ptr would be constructed with the wrong alignment");
 
 void __assign_seh_exception_ptr_from_record(
-    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const _EXCEPTION_RECORD& __record, void* const __rx_raw) noexcept {
+    void*& __dest, const _EXCEPTION_RECORD& __record, void* const __rx_raw) noexcept {
   if (!__rx_raw) {
     __dest = __exception_ptr_static<std::bad_alloc>::__get();
     return;
   }
 
-  const auto __rx = ::new (__rx_raw) __exception_ptr_normal(__record);
-  __dest          = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+  __dest = ::new (__rx_raw) __exception_ptr_normal(__record);
 }
 
-void __assign_cpp_exception_ptr_from_record(
-    std::shared_ptr<const _EXCEPTION_RECORD>& __dest, const EHExceptionRecord& __record) noexcept {
-  const auto __throw_info = __record.params.pThrowInfo;
+void __assign_cpp_exception_ptr_from_record(void*& __dest, const EHExceptionRecord& __record) noexcept {
+  const auto* __throw_info = __record.params.pThrowInfo;
 #if _EH_RELATIVE_TYPEINFO
   const auto __throw_image_base = reinterpret_cast<uintptr_t>(__record.params.pThrowImageBase);
-  const auto __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+  const auto* __catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
       static_cast<uintptr_t>(__throw_info->pCatchableTypeArray) + __throw_image_base);
-  const auto __type = reinterpret_cast<CatchableType*>(
+  const auto* __type = reinterpret_cast<CatchableType*>(
       static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
 #else
-  const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+  const auto* __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
 #endif
 
   const auto __except_obj_size = static_cast<size_t>(__type->sizeOrOffset);
   const auto __alloc_size      = sizeof(__exception_ptr_normal) + __except_obj_size;
-  auto __rx_raw                = std::malloc(__alloc_size);
+  auto* __rx_raw               = std::malloc(__alloc_size);
   if (!__rx_raw) {
     __dest = __exception_ptr_static<std::bad_alloc>::__get();
     return;
@@ -276,10 +253,10 @@ void __assign_cpp_exception_ptr_from_record(
 #endif
     );
 
-    const auto __rx = ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__record));
-    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
+    const auto* __rx = ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__record));
+    reinterpret_cast<EHExceptionRecord&>(const_cast<__exception_ptr_normal*>(__rx)->__record_).params.pExceptionObject =
         static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
-    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+    __dest = const_cast<__exception_ptr_normal*>(__rx);
   } catch (...) {
     const auto* __inner_record_ptr = __get_current_exception();
     if (!__inner_record_ptr) {
@@ -301,15 +278,15 @@ void __assign_cpp_exception_ptr_from_record(
       return;
     }
 
-    const auto __inner_throw = __inner_record.params.pThrowInfo;
+    const auto* __inner_throw = __inner_record.params.pThrowInfo;
 #if _EH_RELATIVE_TYPEINFO
     const auto __inner_throw_image_base = reinterpret_cast<uintptr_t>(__inner_record.params.pThrowImageBase);
-    const auto __inner_catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
+    const auto* __inner_catchable_type_array = reinterpret_cast<const CatchableTypeArray*>(
         static_cast<uintptr_t>(__inner_throw->pCatchableTypeArray) + __inner_throw_image_base);
-    const auto __inner_type = reinterpret_cast<CatchableType*>(
+    const auto* __inner_type = reinterpret_cast<CatchableType*>(
         static_cast<uintptr_t>(__inner_catchable_type_array->arrayOfCatchableTypes[0]) + __inner_throw_image_base);
 #else
-    const auto __inner_type = __inner_throw->pCatchableTypeArray->arrayOfCatchableTypes[0];
+    const auto* __inner_type = __inner_throw->pCatchableTypeArray->arrayOfCatchableTypes[0];
 #endif
 
     const auto __inner_except_size = static_cast<size_t>(__inner_type->sizeOrOffset);
@@ -338,60 +315,50 @@ void __assign_cpp_exception_ptr_from_record(
       return;
     }
 
-    const auto __rx =
+    const auto* __rx =
         ::new (__rx_raw) __exception_ptr_normal(reinterpret_cast<const _EXCEPTION_RECORD&>(__inner_record));
-    reinterpret_cast<EHExceptionRecord&>(__rx->__record_).params.pExceptionObject =
+    reinterpret_cast<EHExceptionRecord&>(const_cast<__exception_ptr_normal*>(__rx)->__record_).params.pExceptionObject =
         static_cast<__exception_ptr_normal*>(__rx_raw) + 1;
-    __dest = std::shared_ptr<const _EXCEPTION_RECORD>::__create_with_control_block(&__rx->__record_, __rx);
+    __dest = const_cast<__exception_ptr_normal*>(__rx);
   }
 }
 
-inline std::shared_ptr<const _EXCEPTION_RECORD>& __to_shared(std::exception_ptr& __ptr) noexcept {
-  return *reinterpret_cast<std::shared_ptr<const _EXCEPTION_RECORD>*>(&__ptr);
-}
-
-inline const std::shared_ptr<const _EXCEPTION_RECORD>& __to_shared(const std::exception_ptr& __ptr) noexcept {
-  return *reinterpret_cast<const std::shared_ptr<const _EXCEPTION_RECORD>*>(&__ptr);
-}
-
 } // namespace
 
 namespace std {
 
-exception_ptr::exception_ptr() noexcept {
-  ::new (this) shared_ptr<const _EXCEPTION_RECORD>();
-}
-exception_ptr::exception_ptr(nullptr_t) noexcept {
-  ::new (this) shared_ptr<const _EXCEPTION_RECORD>();
-}
-
-exception_ptr::exception_ptr(const exception_ptr& __other) noexcept {
-  ::new (this) shared_ptr<const _EXCEPTION_RECORD>(__to_shared(__other));
-}
-exception_ptr& exception_ptr::operator=(const exception_ptr& __other) noexcept {
-  __to_shared(*this) = __to_shared(__other);
-  return *this;
-}
-
-exception_ptr& exception_ptr::operator=(nullptr_t) noexcept {
-  __to_shared(*this) = nullptr;
-  return *this;
-}
-
 exception_ptr::~exception_ptr() noexcept {
-  __to_shared(*this).~shared_ptr();
+  if (__ptr_) {
+    static_cast<__shared_count*>(__ptr_)->__release_shared();
+  }
 }
 
-exception_ptr::operator bool() const noexcept {
-  return static_cast<bool>(__to_shared(*this));
+exception_ptr::exception_ptr(const exception_ptr& __other) noexcept : __ptr_(__other.__ptr_) {
+  if (__ptr_) {
+    static_cast<__shared_count*>(__ptr_)->__add_shared();
+  }
 }
 
-bool operator==(const exception_ptr& __x, const exception_ptr& __y) noexcept {
-  return __to_shared(__x) == __to_shared(__y);
+exception_ptr& exception_ptr::operator=(const exception_ptr& __other) noexcept {
+  if (__ptr_ != __other.__ptr_) {
+    if (__other.__ptr_) {
+      static_cast<__shared_count*>(__other.__ptr_)->__add_shared();
+    }
+    if (__ptr_) {
+      static_cast<__shared_count*>(__ptr_)->__release_shared();
+    }
+    __ptr_ = __other.__ptr_;
+  }
+  return *this;
 }
 
-void swap(exception_ptr& __lhs, exception_ptr& __rhs) noexcept {
-  __to_shared(__lhs).swap(__to_shared(__rhs));
+exception_ptr exception_ptr::__from_native_exception_pointer(void* __p) noexcept {
+  exception_ptr __ret;
+  __ret.__ptr_ = __p;
+  if (__ret.__ptr_) {
+    static_cast<__shared_count*>(__ret.__ptr_)->__add_shared();
+  }
+  return __ret;
 }
 
 exception_ptr __copy_exception_ptr(void* __except, const void* __ptr) {
@@ -402,7 +369,7 @@ exception_ptr __copy_exception_ptr(void* __except, const void* __ptr) {
 
   _EXCEPTION_RECORD __record;
   __populate_cpp_exception_record(__record, __except, static_cast<ThrowInfo*>(const_cast<void*>(__ptr)));
-  __assign_cpp_exception_ptr_from_record(__to_shared(__ret), reinterpret_cast<const EHExceptionRecord&>(__record));
+  __assign_cpp_exception_ptr_from_record(__ret.__ptr_, reinterpret_cast<const EHExceptionRecord&>(__record));
   return __ret;
 }
 
@@ -414,12 +381,11 @@ exception_ptr current_exception() noexcept {
     return __ret;
   }
 
-  auto& __dest = __to_shared(__ret);
   if (PER_IS_MSVC_PURE_OR_NATIVE_EH(__record)) {
-    __assign_cpp_exception_ptr_from_record(__dest, *__record);
+    __assign_cpp_exception_ptr_from_record(__ret.__ptr_, *__record);
   } else {
     __assign_seh_exception_ptr_from_record(
-        __dest, reinterpret_cast<const _EXCEPTION_RECORD&>(*__record), std::malloc(sizeof(__exception_ptr_normal)));
+        __ret.__ptr_, reinterpret_cast<const _EXCEPTION_RECORD&>(*__record), std::malloc(sizeof(__exception_ptr_normal)));
   }
   return __ret;
 }
@@ -429,20 +395,21 @@ exception_ptr current_exception() noexcept {
     throw bad_exception();
   }
 
-  auto __record_copy = *__to_shared(__p);
+  auto* __rep = static_cast<__exception_ptr_storage*>(__p.__ptr_);
+  auto __record_copy = __rep->__record_;
   auto& __cpp_record = reinterpret_cast<EHExceptionRecord&>(__record_copy);
   if (PER_IS_MSVC_PURE_OR_NATIVE_EH(&__cpp_record)) {
-    const auto __throw_info = __cpp_record.params.pThrowInfo;
+    const auto* __throw_info = __cpp_record.params.pThrowInfo;
     if (!__cpp_record.params.pExceptionObject || !__throw_info || !__throw_info->pCatchableTypeArray) {
       std::abort();
     }
 
 #if _EH_RELATIVE_TYPEINFO
     const auto __throw_image_base = reinterpret_cast<uintptr_t>(__cpp_record.params.pThrowImageBase);
-    const auto __catchable_type_array =
+    const auto* __catchable_type_array =
         reinterpret_cast<const CatchableTypeArray*>(__throw_image_base + __throw_info->pCatchableTypeArray);
 #else
-    const auto __catchable_type_array = __throw_info->pCatchableTypeArray;
+    const auto* __catchable_type_array = __throw_info->pCatchableTypeArray;
 #endif
 
     if (__catchable_type_array->nCatchableTypes <= 0) {
@@ -450,10 +417,10 @@ exception_ptr current_exception() noexcept {
     }
 
 #if _EH_RELATIVE_TYPEINFO
-    const auto __type = reinterpret_cast<CatchableType*>(
+    const auto* __type = reinterpret_cast<CatchableType*>(
         static_cast<uintptr_t>(__catchable_type_array->arrayOfCatchableTypes[0]) + __throw_image_base);
 #else
-    const auto __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
+    const auto* __type = __throw_info->pCatchableTypeArray->arrayOfCatchableTypes[0];
 #endif
 
 #pragma warning(suppress : 6255)

>From 12f7725cddbf6acb19450905ac05d5ef6bbf74ce Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Wed, 24 Jun 2026 22:10:06 +0000
Subject: [PATCH 09/10] Fix the test failures by readding the swap forward
 declaration

---
 libcxx/include/__exception/exception_ptr.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 0c86313545fbf..4ba107accdb75 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -62,6 +62,8 @@ _LIBCPP_OVERRIDABLE_FUNC_VIS __cxa_exception* __cxa_init_primary_exception(
 _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
 _LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
 
+inline _LIBCPP_HIDE_FROM_ABI void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT;
+
 class _LIBCPP_EXPORTED_FROM_ABI exception_ptr {
   void* __ptr_;
 

>From 2f55fa2dfb61b5062c3921fadcd7ada4bc967c1c Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Sat, 27 Jun 2026 19:02:16 +0000
Subject: [PATCH 10/10] Fix reference count leak in std::rethrow_exception

---
 libcxx/src/support/runtime/exception_pointer_msvc.ipp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/libcxx/src/support/runtime/exception_pointer_msvc.ipp b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
index 6a739f442f065..57d470b5c5f73 100644
--- a/libcxx/src/support/runtime/exception_pointer_msvc.ipp
+++ b/libcxx/src/support/runtime/exception_pointer_msvc.ipp
@@ -435,6 +435,12 @@ exception_ptr current_exception() noexcept {
     __cpp_record.params.pExceptionObject = __exception_buffer;
   }
 
+  // Under MSVC C++ exception handling (/EHsc), C Win32 API functions like RaiseException
+  // are assumed not to throw synchronous C++ exceptions. As a result, stack unwinding
+  // initiated by RaiseException does not execute local destructors in this frame.
+  // We must explicitly reset __p here so that the reference count on the stored
+  // exception record is properly decremented before unwinding begins.
+  __p = nullptr;
   RaiseException(__record_copy.ExceptionCode, __record_copy.ExceptionFlags, __record_copy.NumberParameters,
                  __record_copy.ExceptionInformation);
   std::abort();



More information about the libcxx-commits mailing list