[libcxx-commits] [libcxx] e5291c4 - [libc++/abi] Provide an option to turn on forgiving dynamic_cast when building libc++abi

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Apr 22 13:25:29 PDT 2020


Author: Louis Dionne
Date: 2020-04-22T16:24:26-04:00
New Revision: e5291c4ae3f7272ca3901d1189d30e23fd578193

URL: https://github.com/llvm/llvm-project/commit/e5291c4ae3f7272ca3901d1189d30e23fd578193
DIFF: https://github.com/llvm/llvm-project/commit/e5291c4ae3f7272ca3901d1189d30e23fd578193.diff

LOG: [libc++/abi] Provide an option to turn on forgiving dynamic_cast when building libc++abi

Instead of the ad-hoc #define _LIBCXX_DYNAMIC_FALLBACK, provide an option
to enable the setting when building libc++abi. Also use the occasion to
rename the option to something slightly more descriptive.

Note that in the future, it would be great to simply remove this option
altogether. However, in the meantime, it seems better to have it be an
official option than something ad-hoc.

Added: 
    

Modified: 
    libcxx/cmake/caches/Apple.cmake
    libcxxabi/CMakeLists.txt
    libcxxabi/src/CMakeLists.txt
    libcxxabi/src/private_typeinfo.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/cmake/caches/Apple.cmake b/libcxx/cmake/caches/Apple.cmake
index 5ccc53aeee0d..215c5bd9dfb1 100644
--- a/libcxx/cmake/caches/Apple.cmake
+++ b/libcxx/cmake/caches/Apple.cmake
@@ -14,3 +14,4 @@ set(LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT ON CACHE BOOL "")
 set(LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS ON CACHE BOOL "")
 set(LIBCXXABI_ENABLE_PIC OFF CACHE BOOL "")
 set(LIBCXXABI_ENABLE_ASSERTIONS OFF CACHE BOOL "")
+set(LIBCXXABI_ENABLE_FORGIVING_DYNAMIC_CAST ON CACHE BOOL "")

diff  --git a/libcxxabi/CMakeLists.txt b/libcxxabi/CMakeLists.txt
index a269806891d9..bb79987f7477 100644
--- a/libcxxabi/CMakeLists.txt
+++ b/libcxxabi/CMakeLists.txt
@@ -65,6 +65,12 @@ option(LIBCXXABI_HAS_EXTERNAL_THREAD_API
 option(LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY
   "Build libc++abi with an externalized threading library.
    This option may only be set to ON when LIBCXXABI_ENABLE_THREADS=ON" OFF)
+option(LIBCXXABI_ENABLE_FORGIVING_DYNAMIC_CAST
+"Make dynamic_cast more forgiving when type_info's mistakenly have hidden \
+visibility, and thus multiple type_infos can exist for a single type. \
+When the dynamic_cast would normally fail, this option will cause the \
+library to try comparing the type_info names to see if they are equal \
+instead." OFF)
 
 # FIXME: This option should default to off. Unfortunatly GCC 4.9 fails to link
 # programs to due undefined references to new/delete in libc++abi. Once this

diff  --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt
index 8e88416279ea..7b837a4ddf24 100644
--- a/libcxxabi/src/CMakeLists.txt
+++ b/libcxxabi/src/CMakeLists.txt
@@ -60,6 +60,10 @@ if (LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
   add_definitions(-DHAVE___CXA_THREAD_ATEXIT_IMPL)
 endif()
 
+if (LIBCXXABI_ENABLE_FORGIVING_DYNAMIC_CAST)
+  add_definitions(-D_LIBCXXABI_FORGIVING_DYNAMIC_CAST)
+endif()
+
 if (APPLE)
   add_library_flags_if(LIBCXXABI_HAS_SYSTEM_LIB System)
 else()

diff  --git a/libcxxabi/src/private_typeinfo.cpp b/libcxxabi/src/private_typeinfo.cpp
index 898cfaef7527..55a90b3ae1d4 100644
--- a/libcxxabi/src/private_typeinfo.cpp
+++ b/libcxxabi/src/private_typeinfo.cpp
@@ -8,11 +8,11 @@
 
 #include "private_typeinfo.h"
 
-// The flag _LIBCXX_DYNAMIC_FALLBACK is used to make dynamic_cast more
-// forgiving when type_info's mistakenly have hidden visibility and thus
-// multiple type_infos can exist for a single type.
+// The flag _LIBCXXABI_FORGIVING_DYNAMIC_CAST is used to make dynamic_cast
+// more forgiving when type_info's mistakenly have hidden visibility and
+// thus multiple type_infos can exist for a single type.
 //
-// When _LIBCXX_DYNAMIC_FALLBACK is defined, and only in the case where
+// When _LIBCXXABI_FORGIVING_DYNAMIC_CAST is defined, and only in the case where
 // there is a detected inconsistency in the type_info hierarchy during a
 // dynamic_cast, then the equality operation will fall back to using strcmp
 // on type_info names to determine type_info equality.
@@ -23,7 +23,7 @@
 // algorithm and an inconsistency is still detected, dynamic_cast will call
 // abort with an appropriate message.
 //
-// The current implementation of _LIBCXX_DYNAMIC_FALLBACK requires a
+// The current implementation of _LIBCXXABI_FORGIVING_DYNAMIC_CAST requires a
 // printf-like function called syslog:
 //
 //     void syslog(int facility_priority, const char* format, ...);
@@ -31,19 +31,19 @@
 // If you want this functionality but your platform doesn't have syslog,
 // just implement it in terms of fprintf(stderr, ...).
 //
-// _LIBCXX_DYNAMIC_FALLBACK is currently off by default.
+// _LIBCXXABI_FORGIVING_DYNAMIC_CAST is currently off by default.
 
 // On Windows, typeids are 
diff erent between DLLs and EXEs, so comparing
 // type_info* will work for typeids from the same compiled file but fail
 // for typeids from a DLL and an executable. Among other things, exceptions
 // are not caught by handlers since can_catch() returns false.
 //
-// Defining _LIBCXX_DYNAMIC_FALLBACK does not help since can_catch() calls
+// Defining _LIBCXXABI_FORGIVING_DYNAMIC_CAST does not help since can_catch() calls
 // is_equal() with use_strcmp=false so the string names are not compared.
 
 #include <string.h>
 
-#ifdef _LIBCXX_DYNAMIC_FALLBACK
+#ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
 #include "abort_message.h"
 #include <sys/syslog.h>
 #include <atomic>
@@ -634,7 +634,7 @@ __dynamic_cast(const void *static_ptr, const __class_type_info *static_type,
         info.number_of_dst_type = 1;
         // Do the  search
         dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, false);
-#ifdef _LIBCXX_DYNAMIC_FALLBACK
+#ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
         // The following if should always be false because we should definitely
         //   find (static_ptr, static_type), either on a public or private path
         if (info.path_dst_ptr_to_static_ptr == unknown)
@@ -652,7 +652,7 @@ __dynamic_cast(const void *static_ptr, const __class_type_info *static_type,
             info.number_of_dst_type = 1;
             dynamic_type->search_above_dst(&info, dynamic_ptr, dynamic_ptr, public_path, true);
         }
-#endif  // _LIBCXX_DYNAMIC_FALLBACK
+#endif  // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
         // Query the search.
         if (info.path_dst_ptr_to_static_ptr == public_path)
             dst_ptr = dynamic_ptr;
@@ -661,7 +661,7 @@ __dynamic_cast(const void *static_ptr, const __class_type_info *static_type,
     {
         // Not using giant short cut.  Do the search
         dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, false);
- #ifdef _LIBCXX_DYNAMIC_FALLBACK
+ #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST
         // The following if should always be false because we should definitely
         //   find (static_ptr, static_type), either on a public or private path
         if (info.path_dst_ptr_to_static_ptr == unknown &&
@@ -679,7 +679,7 @@ __dynamic_cast(const void *static_ptr, const __class_type_info *static_type,
             info = {dst_type, static_ptr, static_type, src2dst_offset, 0};
             dynamic_type->search_below_dst(&info, dynamic_ptr, public_path, true);
         }
-#endif  // _LIBCXX_DYNAMIC_FALLBACK
+#endif  // _LIBCXXABI_FORGIVING_DYNAMIC_CAST
         // Query the search.
         switch (info.number_to_static_ptr)
         {


        


More information about the libcxx-commits mailing list