[libcxx-commits] [PATCH] D95827: Avoid cast<T*> before T is constructed to pacify CFI checks

Reid Kleckner via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Feb 1 15:20:52 PST 2021


rnk created this revision.
rnk added a reviewer: ldionne.
rnk requested review of this revision.
Herald added a project: libc++.
Herald added a reviewer: libc++.

Fixes usage of shared_ptr with CFI enabled, which is llvm.org/pr48993.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95827

Files:
  libcxx/include/memory


Index: libcxx/include/memory
===================================================================
--- libcxx/include/memory
+++ libcxx/include/memory
@@ -2593,7 +2593,7 @@
         _TpAlloc __tmp(*__get_alloc());
         allocator_traits<_TpAlloc>::construct(__tmp, __get_elem(), _VSTD::forward<_Args>(__args)...);
 #else
-        ::new ((void*)__get_elem()) _Tp(_VSTD::forward<_Args>(__args)...);
+        ::new (__storage_.__get_elem_for_new()) _Tp(_VSTD::forward<_Args>(__args)...);
 #endif
     }
 
@@ -2653,6 +2653,11 @@
             _Tp *__elem = reinterpret_cast<_Tp*>(__second);
             return __elem;
         }
+	void *__get_elem_for_new() _NOEXCEPT {
+            _CompressedPair *__as_pair = reinterpret_cast<_CompressedPair*>(__blob_);
+            typename _CompressedPair::_Base2* __second = _CompressedPair::__get_second_base(__as_pair);
+            return (void*)__second;
+        }
     };
 
     static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95827.320619.patch
Type: text/x-patch
Size: 1004 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20210201/59b1a32a/attachment-0001.bin>


More information about the libcxx-commits mailing list