| 1 | // { dg-do run } |
| 2 | |
| 3 | #include <stdexcept> |
| 4 | #include <cxxabi.h> |
| 5 | #include <testsuite_hooks.h> |
| 6 | |
| 7 | // Test cases for PR libstdc++/105387 |
| 8 | |
| 9 | // This test is to trigger undefined behavior if the bug 105387 is present |
| 10 | // in the code. Note, however, given that the bug is present, this test runs |
| 11 | // into undefined behavior which can also mean that it passes. |
| 12 | // It has been observed to fail quite reliably on x86_64-linux-gnu but only |
| 13 | // fail sporadically on Xtensa, depending on the code placement. |
| 14 | void portable_test() |
| 15 | { |
| 16 | bool exception_thrown = false; |
| 17 | try { |
| 18 | throw std::runtime_error("test"); |
| 19 | } catch (const char *e) { |
| 20 | VERIFY(false); |
| 21 | } catch (const std::exception &e) { |
| 22 | exception_thrown = true; |
| 23 | } |
| 24 | VERIFY(exception_thrown); |
| 25 | } |
| 26 | |
| 27 | // This test relies on the types defined in the files typeinfo and cxxabi.h |
| 28 | // It is therefore less portable then the test case above but should be |
| 29 | // guaranteed to fail if the implementation has the bug 105387. |
| 30 | // |
| 31 | // This test case checks that __pbase_type_info::__do_catch() behaves |
| 32 | // correctly when called with a non-pointer type info object as argument. |
| 33 | // In particular, __pbase_type_info::__do_catch() should not cast |
| 34 | // the given type object into a pointer type and try to access the |
| 35 | // extended fields. |
| 36 | |
| 37 | void non_portable_test() |
| 38 | { |
| 39 | // Create a zero-initialized buffer for allocation of the type object |
| 40 | unsigned char buffer [sizeof(__cxxabiv1::__fundamental_type_info) * 2] = {}; |
| 41 | |
| 42 | // Use placement-new to create the fundamental type info object in the |
| 43 | // first half of the buffer. Whenever that type info object will be |
| 44 | // casted to a pointer type info object, the extended fields of the |
| 45 | // pointer type info object will be in the second half of the buffer |
| 46 | // and hence be guaranteed zero. |
| 47 | __cxxabiv1::__fundamental_type_info *p_fund_info |
| 48 | = new(buffer) __cxxabiv1::__fundamental_type_info("fund_type"); |
| 49 | |
| 50 | __cxxabiv1::__pointer_type_info ptr_info("ptr_type", 0, p_fund_info); |
| 51 | |
| 52 | // __do_catch is declared protected in __pointer_type_info, but public in |
| 53 | // type_info, so we upcast it here |
| 54 | std::type_info *abstract_ptr_info = static_cast<std::type_info*>(&ptr_info); |
| 55 | VERIFY(abstract_ptr_info->__do_catch(p_fund_info, 0, 1) == false); |
| 56 | } |
| 57 | |
| 58 | int main() |
| 59 | { |
| 60 | portable_test(); |
| 61 | non_portable_test(); |
| 62 | return 0; |
| 63 | } |