[libcxx-commits] [PATCH] D91359: [libc++] Only check for GCC's empty string storage on macOS and iOS

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 12 08:08:44 PST 2020


ldionne created this revision.
Herald added subscribers: libcxx-commits, jkorous.
Herald added a project: libc++.
Herald added a reviewer: libc++.
ldionne requested review of this revision.

We don't need to do that on other Apple platforms, since they never
shipped libstdc++. I also added a comment extracted from the original
commit by Howard Hinnant (e115af2777f6).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91359

Files:
  libcxx/src/include/refstring.h


Index: libcxx/src/include/refstring.h
===================================================================
--- libcxx/src/include/refstring.h
+++ libcxx/src/include/refstring.h
@@ -13,12 +13,25 @@
 #include <stdexcept>
 #include <cstddef>
 #include <cstring>
-#ifdef __APPLE__
-#include <dlfcn.h>
-#include <mach-o/dyld.h>
-#endif
 #include "atomic_support.h"
 
+// MacOS and iOS used to ship with libstdc++, and still support old applications
+// linking against libstdc++. The libc++ and libstdc++ exceptions are supposed
+// to be ABI compatible, such that they can be thrown from one library and caught
+// in the other.
+//
+// For that reason, we must look for libstdc++ in the same process and if found,
+// check the string stored in the exception object to see if it is the GCC empty
+// string singleton before manipulating the reference count. This is done so that
+// if an exception is created with a zero-length string in libstdc++, libc++abi
+// won't try to delete the memory.
+#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) || \
+    defined(__ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__)
+#   define _LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE
+#   include <dlfcn.h>
+#   include <mach-o/dyld.h>
+#endif
+
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 namespace __refstring_imp { namespace {
@@ -40,7 +53,7 @@
     return data + sizeof(*rep);
 }
 
-#if defined(__APPLE__)
+#if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE)
 inline
 const char* compute_gcc_empty_string_storage() _NOEXCEPT
 {
@@ -115,7 +128,7 @@
 
 inline
 bool __libcpp_refstring::__uses_refcount() const {
-#ifdef __APPLE__
+#if defined(_LIBCPP_CHECK_FOR_GCC_EMPTY_STRING_STORAGE)
     return __imp_ != get_gcc_empty_string_storage();
 #else
     return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91359.304846.patch
Type: text/x-patch
Size: 1749 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201112/93e71fd3/attachment.bin>


More information about the libcxx-commits mailing list