1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: libcxxabi-no-exceptions
11 // UNSUPPORTED: c++98, c++03
12 
13 // The system unwind.h on OS X provides an incorrectly aligned _Unwind_Exception
14 // type. That causes these tests to fail. This XFAIL is my best attempt at
15 // working around this failure.
16 // XFAIL: darwin && libcxxabi-has-system-unwinder
17 
18 // Test that the address of the exception object is properly aligned as required
19 // by the relevant ABI
20 
21 #include <cstdint>
22 #include <cassert>
23 #include <__cxxabi_config.h>
24 
25 #include <unwind.h>
26 
27 struct __attribute__((aligned)) AlignedType {};
28 
29 // EHABI  : 8-byte aligned
30 // Itanium: Largest supported alignment for the system
31 #if defined(_LIBCXXABI_ARM_EHABI)
32 #  define EXPECTED_ALIGNMENT 8
33 #else
34 #  define EXPECTED_ALIGNMENT alignof(AlignedType)
35 #endif
36 
37 static_assert(alignof(_Unwind_Exception) == EXPECTED_ALIGNMENT,
38   "_Unwind_Exception is incorrectly aligned. This test is expected to fail");
39 
40 struct MinAligned {  };
41 static_assert(alignof(MinAligned) == 1 && sizeof(MinAligned) == 1, "");
42 
main()43 int main() {
44   for (int i=0; i < 10; ++i) {
45     try {
46       throw MinAligned{};
47     } catch (MinAligned const& ref) {
48       assert(reinterpret_cast<uintptr_t>(&ref) % EXPECTED_ALIGNMENT == 0);
49     }
50   }
51 }
52