[libcxxabi] r233984 - [libcxxabi] Fix multi-level pointer conversions and pointer to member conversion detection.

Eric Fiselier eric at efcs.ca
Thu Apr 2 16:26:37 PDT 2015


Author: ericwf
Date: Thu Apr  2 18:26:37 2015
New Revision: 233984

URL: http://llvm.org/viewvc/llvm-project?rev=233984&view=rev
Log:
[libcxxabi] Fix multi-level pointer conversions and pointer to member conversion detection.

Summary:
Currently there are bugs in out detection of multi-level pointer conversions and pointer to member conversions. This patch fixes the following issues.

* Allow multi-level pointers with different nested qualifiers.
* Allow multi-level mixed pointers to objects and pointers to members with different nested qualifiers.
* Allow conversions from `int Base::*` to `int Derived::*` but only for non-nested pointers.

There is still some work that needs to be done to clean this patch up but I want to get some input on it.
Open questions:

* Does `__pointer_to_member_type_info::can_catch(...)` need to adjust the pointer if a base to derived conversion is performed?


Reviewers: danalbert, compnerd, mclow.lists

Reviewed By: mclow.lists

Subscribers: cfe-commits

Differential Revision: http://reviews.llvm.org/D8758

Added:
    libcxxabi/trunk/test/catch_multi_level_pointer.pass.cpp
Modified:
    libcxxabi/trunk/src/private_typeinfo.cpp
    libcxxabi/trunk/src/private_typeinfo.h
    libcxxabi/trunk/test/catch_member_data_pointer_01.pass.cpp
    libcxxabi/trunk/test/catch_pointer_nullptr.pass.cpp

Modified: libcxxabi/trunk/src/private_typeinfo.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/private_typeinfo.cpp?rev=233984&r1=233983&r2=233984&view=diff
==============================================================================
--- libcxxabi/trunk/src/private_typeinfo.cpp (original)
+++ libcxxabi/trunk/src/private_typeinfo.cpp Thu Apr  2 18:26:37 2015
@@ -389,6 +389,24 @@ __pointer_type_info::can_catch(const __s
     // bullet 3A
     if (is_equal(__pointee, &typeid(void), false))
         return true;
+
+    // Handle pointer to pointer
+    const __pointer_type_info* nested_pointer_type =
+        dynamic_cast<const __pointer_type_info*>(__pointee);
+    if (nested_pointer_type) {
+        if (~__flags & __const_mask) return false;
+        return nested_pointer_type->can_catch_nested(thrown_pointer_type->__pointee);
+    }
+
+    // Handle pointer to pointer to member
+    const __pointer_to_member_type_info* member_ptr_type =
+        dynamic_cast<const __pointer_to_member_type_info*>(__pointee);
+    if (member_ptr_type) {
+        if (~__flags & __const_mask) return false;
+        return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee);
+    }
+
+    // Handle pointer to class type
     const __class_type_info* catch_class_type =
         dynamic_cast<const __class_type_info*>(__pointee);
     if (catch_class_type == 0)
@@ -409,6 +427,83 @@ __pointer_type_info::can_catch(const __s
     return false;
 }
 
+bool __pointer_type_info::can_catch_nested(
+    const __shim_type_info* thrown_type) const
+{
+  const __pointer_type_info* thrown_pointer_type =
+        dynamic_cast<const __pointer_type_info*>(thrown_type);
+    if (thrown_pointer_type == 0)
+        return false;
+    // bullet 3B
+    if (thrown_pointer_type->__flags & ~__flags)
+        return false;
+    if (is_equal(__pointee, thrown_pointer_type->__pointee, false))
+        return true;
+    // If the pointed to types differ then the catch type must be const
+    // qualified.
+    if (~__flags & __const_mask)
+        return false;
+
+    // Handle pointer to pointer
+    const __pointer_type_info* nested_pointer_type =
+        dynamic_cast<const __pointer_type_info*>(__pointee);
+    if (nested_pointer_type) {
+        return nested_pointer_type->can_catch_nested(
+            thrown_pointer_type->__pointee);
+    }
+
+    // Handle pointer to pointer to member
+    const __pointer_to_member_type_info* member_ptr_type =
+        dynamic_cast<const __pointer_to_member_type_info*>(__pointee);
+    if (member_ptr_type) {
+        return member_ptr_type->can_catch_nested(thrown_pointer_type->__pointee);
+    }
+
+    return false;
+}
+
+bool __pointer_to_member_type_info::can_catch(
+    const __shim_type_info* thrown_type, void*& adjustedPtr) const {
+    // bullets 1 and 4
+    if (__pbase_type_info::can_catch(thrown_type, adjustedPtr))
+        return true;
+
+    const __pointer_to_member_type_info* thrown_pointer_type =
+        dynamic_cast<const __pointer_to_member_type_info*>(thrown_type);
+    if (thrown_pointer_type == 0)
+        return false;
+    if (thrown_pointer_type->__flags & ~__flags)
+        return false;
+    if (!is_equal(__pointee, thrown_pointer_type->__pointee, false))
+        return false;
+    if (is_equal(__context, thrown_pointer_type->__context, false))
+        return true;
+
+    __dynamic_cast_info info = {__context, 0, thrown_pointer_type->__context, -1, 0};
+    info.number_of_dst_type = 1;
+    __context->has_unambiguous_public_base(&info, adjustedPtr, public_path);
+    if (info.path_dst_ptr_to_static_ptr == public_path)
+        return true;
+
+    return false;
+}
+
+bool __pointer_to_member_type_info::can_catch_nested(
+    const __shim_type_info* thrown_type) const
+{
+    const __pointer_to_member_type_info* thrown_member_ptr_type =
+        dynamic_cast<const __pointer_to_member_type_info*>(thrown_type);
+    if (thrown_member_ptr_type == 0)
+        return false;
+    if (~__flags & thrown_member_ptr_type->__flags)
+        return false;
+    if (!is_equal(__pointee, thrown_member_ptr_type->__pointee, false))
+        return false;
+    if (!is_equal(__context, thrown_member_ptr_type->__context, false))
+        return false;
+    return true;
+}
+
 #ifdef __clang__
 #pragma clang diagnostic pop
 #endif

Modified: libcxxabi/trunk/src/private_typeinfo.h
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/src/private_typeinfo.h?rev=233984&r1=233983&r2=233984&view=diff
==============================================================================
--- libcxxabi/trunk/src/private_typeinfo.h (original)
+++ libcxxabi/trunk/src/private_typeinfo.h Thu Apr  2 18:26:37 2015
@@ -230,6 +230,7 @@ class __attribute__ ((__visibility__("de
 public:
     __attribute__ ((__visibility__("hidden"))) virtual ~__pointer_type_info();
     __attribute__ ((__visibility__("hidden"))) virtual bool can_catch(const __shim_type_info*, void*&) const;
+    __attribute__ ((__visibility__("hidden"))) bool can_catch_nested(const __shim_type_info*) const;
 };
 
 class __attribute__ ((__visibility__("default"))) __pointer_to_member_type_info
@@ -239,6 +240,8 @@ public:
     const __class_type_info* __context;
 
     __attribute__ ((__visibility__("hidden"))) virtual ~__pointer_to_member_type_info();
+    __attribute__ ((__visibility__("hidden"))) virtual bool can_catch(const __shim_type_info*, void*&) const;
+    __attribute__ ((__visibility__("hidden"))) bool can_catch_nested(const __shim_type_info*) const;
 };
 
 #pragma GCC visibility pop

Modified: libcxxabi/trunk/test/catch_member_data_pointer_01.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/test/catch_member_data_pointer_01.pass.cpp?rev=233984&r1=233983&r2=233984&view=diff
==============================================================================
--- libcxxabi/trunk/test/catch_member_data_pointer_01.pass.cpp (original)
+++ libcxxabi/trunk/test/catch_member_data_pointer_01.pass.cpp Thu Apr  2 18:26:37 2015
@@ -18,6 +18,15 @@ struct A
 typedef const int A::*md1;
 typedef       int A::*md2;
 
+struct B : public A
+{
+    const int k;
+    int l;
+};
+
+typedef const int B::*der1;
+typedef       int B::*der2;
+
 void test1()
 {
     try
@@ -34,11 +43,114 @@ void test1()
     }
 }
 
+// Check that cv qualified conversions are allowed.
 void test2()
 {
     try
     {
         throw &A::j;
+    }
+    catch (md2)
+    {
+    }
+    catch (...)
+    {
+        assert(false);
+    }
+
+    try
+    {
+        throw &A::j;
+        assert(false);
+    }
+    catch (md1)
+    {
+    }
+    catch (...)
+    {
+        assert(false);
+    }
+}
+
+// Check that Base -> Derived conversions are allowed.
+void test3()
+{
+    try
+    {
+        throw &A::i;
+        assert(false);
+    }
+    catch (md2)
+    {
+        assert(false);
+    }
+    catch (der2)
+    {
+        assert(false);
+    }
+    catch (der1)
+    {
+    }
+    catch (md1)
+    {
+        assert(false);
+    }
+}
+
+// Check that Base -> Derived conversions are allowed with different cv
+// qualifiers.
+void test4()
+{
+    try
+    {
+        throw &A::j;
+        assert(false);
+    }
+    catch (der2)
+    {
+    }
+    catch (...)
+    {
+        assert(false);
+    }
+
+    try
+    {
+        throw &A::j;
+        assert(false);
+    }
+    catch (der1)
+    {
+    }
+    catch (...)
+    {
+        assert(false);
+    }
+}
+
+// Check that no Derived -> Base conversions are allowed.
+void test5()
+{
+    try
+    {
+        throw &B::k;
+        assert(false);
+    }
+    catch (md1)
+    {
+        assert(false);
+    }
+    catch (md2)
+    {
+        assert(false);
+    }
+    catch (der1)
+    {
+    }
+
+    try
+    {
+        throw &B::l;
         assert(false);
     }
     catch (md1)
@@ -47,6 +159,10 @@ void test2()
     }
     catch (md2)
     {
+        assert(false);
+    }
+    catch (der2)
+    {
     }
 }
 
@@ -54,4 +170,7 @@ int main()
 {
     test1();
     test2();
+    test3();
+    test4();
+    test5();
 }

Added: libcxxabi/trunk/test/catch_multi_level_pointer.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/test/catch_multi_level_pointer.pass.cpp?rev=233984&view=auto
==============================================================================
--- libcxxabi/trunk/test/catch_multi_level_pointer.pass.cpp (added)
+++ libcxxabi/trunk/test/catch_multi_level_pointer.pass.cpp Thu Apr  2 18:26:37 2015
@@ -0,0 +1,145 @@
+//===--------------------- catch_pointer_nullptr.cpp ----------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include <cassert>
+#include <cstdlib>
+#include <iostream>
+
+// Roll our own assertion macro to get better error messages out of the tests.
+// In particular on systems that don't use __PRETTY_FUNCTION__ in assertions.
+#define my_assert(pred, msg) do_assert(pred, msg, __LINE__, __PRETTY_FUNCTION__)
+
+void do_assert(bool assert_passed, const char* msg, int line, const char* func) {
+  if (assert_passed) return;
+  std::cerr << __FILE__ << ":" << line << " " << func
+            << ": Assertion Failed `" << msg << "'\n\n";
+  std::abort();
+}
+
+struct A {};
+struct Base {};
+struct Derived : public Base {};
+
+template <class To>
+bool test_conversion(To) { return true; }
+
+template <class To>
+bool test_conversion(...) { return false; }
+
+template <class Pointer>
+struct CreatePointer {
+  Pointer operator()() const {
+      return (Pointer)0;
+  }
+};
+
+template <class Tp>
+struct CreatePointer<Tp*> {
+  Tp* operator()() const {
+      return (Tp*)42;
+  }
+};
+
+template <class Throw, class Catch>
+void catch_pointer_test() {
+  Throw throw_ptr = CreatePointer<Throw>()();
+  // Use the compiler to determine if the exception of type Throw can be
+  // implicitly converted to type Catch.
+  const bool can_convert = test_conversion<Catch>(throw_ptr);
+  try {
+    throw throw_ptr;
+    assert(false);
+  } catch (Catch catch_ptr) {
+    Catch catch2 = CreatePointer<Catch>()();
+    my_assert(can_convert, "non-convertible type incorrectly caught");
+    my_assert(catch_ptr == catch2,
+              "Thrown pointer does not match caught ptr");
+  } catch (...) {
+    my_assert(!can_convert, "convertible type incorrectly not caught");
+  }
+}
+
+// Generate CV qualified pointer typedefs.
+template <class Tp, bool First = false>
+struct TestTypes {
+  typedef Tp* Type;
+  typedef Tp const* CType;
+  typedef Tp volatile* VType;
+  typedef Tp const volatile* CVType;
+};
+
+// Special case for cv-qualifying a pointer-to-member without adding an extra
+// pointer to it.
+template <class Member, class Class>
+struct TestTypes<Member Class::*, true> {
+  typedef Member (Class::*Type);
+  typedef const Member (Class::*CType);
+  typedef volatile Member (Class::*VType);
+  typedef const volatile Member (Class::*CVType);
+};
+
+template <class Throw, class Catch, int level, bool first = false>
+struct generate_tests_imp {
+  typedef TestTypes<Throw, first> ThrowTypes;
+  typedef TestTypes<Catch, first> CatchTypes;
+  void operator()() {
+      typedef typename ThrowTypes::Type Type;
+      typedef typename ThrowTypes::CType CType;
+      typedef typename ThrowTypes::VType VType;
+      typedef typename ThrowTypes::CVType CVType;
+
+      run_catch_tests<Type>();
+      run_catch_tests<CType>();
+      run_catch_tests<VType>();
+      run_catch_tests<CVType>();
+  }
+
+  template <class ThrowTp>
+  void run_catch_tests() {
+      typedef typename CatchTypes::Type Type;
+      typedef typename CatchTypes::CType CType;
+      typedef typename CatchTypes::VType VType;
+      typedef typename CatchTypes::CVType CVType;
+
+      catch_pointer_test<ThrowTp, Type>();
+      catch_pointer_test<ThrowTp, CType>();
+      catch_pointer_test<ThrowTp, VType>();
+      catch_pointer_test<ThrowTp, CVType>();
+
+      generate_tests_imp<ThrowTp, Type, level-1>()();
+      generate_tests_imp<ThrowTp, CType, level-1>()();
+      generate_tests_imp<ThrowTp, VType, level-1>()();
+      generate_tests_imp<ThrowTp, CVType, level-1>()();
+  }
+};
+
+template <class Throw, class Catch, bool first>
+struct generate_tests_imp<Throw, Catch, 0, first> {
+  void operator()() {
+      catch_pointer_test<Throw, Catch>();
+  }
+};
+
+template <class Throw, class Catch, int level>
+struct generate_tests : generate_tests_imp<Throw, Catch, level, true> {};
+
+int main()
+{
+  generate_tests<int, int, 3>()();
+  generate_tests<Base, Derived, 2>()();
+  generate_tests<Derived, Base, 2>()();
+  generate_tests<int, void, 2>()();
+  generate_tests<void, int, 2>()();
+
+  generate_tests<int A::*, int A::*, 3>()();
+  generate_tests<int A::*, void, 2>()();
+  generate_tests<void, int A::*, 2>()();
+  generate_tests<int Base::*, int Derived::*, 2>()();
+  generate_tests<int Derived::*, int Base::*, 2>()();
+}

Modified: libcxxabi/trunk/test/catch_pointer_nullptr.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxxabi/trunk/test/catch_pointer_nullptr.pass.cpp?rev=233984&r1=233983&r2=233984&view=diff
==============================================================================
--- libcxxabi/trunk/test/catch_pointer_nullptr.pass.cpp (original)
+++ libcxxabi/trunk/test/catch_pointer_nullptr.pass.cpp Thu Apr  2 18:26:37 2015
@@ -8,6 +8,13 @@
 //===----------------------------------------------------------------------===//
 
 #include <cassert>
+#include <cstdlib>
+
+#ifndef __has_feature
+#define __has_feature(x) 0
+#endif
+
+struct A {};
 
 #if __has_feature(cxx_nullptr)
 
@@ -27,8 +34,6 @@ void test1()
     }
 }
 
-struct A {};
-
 void test2()
 {
     try
@@ -45,6 +50,18 @@ void test2()
     }
 }
 
+template <class Catch>
+void catch_nullptr_test() {
+  try {
+    throw nullptr;
+    assert(false);
+  } catch (Catch) {
+    // nothing todo
+  } catch (...) {
+    assert(false);
+  }
+}
+
 #else
 
 void test1()
@@ -55,10 +72,22 @@ void test2()
 {
 }
 
+template <class Catch>
+void catch_nullptr_test()
+{
+}
+
 #endif
 
 int main()
 {
-    test1();
-    test2();
+  // catch naked nullptrs
+  test1();
+  test2();
+
+  catch_nullptr_test<int*>();
+  catch_nullptr_test<int**>();
+  catch_nullptr_test<int A::*>();
+  catch_nullptr_test<const int A::*>();
+  catch_nullptr_test<int A::**>();
 }





More information about the cfe-commits mailing list