xref: /llvm-project/libcxxabi/test/test_exception_address_alignment.pass.cpp (revision db8c7e004a8acf74f40e0f7bc60066f26d43ccd9)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: no-exceptions
10 // UNSUPPORTED: c++03
11 
12 // The <unwind.h> header provided in the SDK of older Xcodes used to provide
13 // an incorrectly aligned _Unwind_Exception type on non-ARM. That causes these
14 // tests to fail when running against a system libc++abi and libunwind that was
15 // compiled with an incorrect definition of _Unwind_Exception.
16 // XFAIL: stdlib=system && target={{.+}}-apple-macosx10.{{9|10|11|12}}
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 
43 int main(int, char**) {
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   return 0;
53 }
54