xref: /llvm-project/libcxxabi/test/test_exception_address_alignment.pass.cpp (revision d520dfec3bc4c5cfadaecbe5c2576d4fcfd5bfff)
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. That causes these tests to
14 // fail with those SDKs. Note that we use the AppleClang version as a cheap
15 // proxy for the SDK version.
16 // XFAIL: apple-clang-11 && libcxxabi-has-system-unwinder
17 // XFAIL: apple-clang-10 && libcxxabi-has-system-unwinder
18 // XFAIL: apple-clang-9 && libcxxabi-has-system-unwinder
19 
20 // Test that the address of the exception object is properly aligned as required
21 // by the relevant ABI
22 
23 #include <cstdint>
24 #include <cassert>
25 #include <__cxxabi_config.h>
26 
27 #include <unwind.h>
28 
29 struct __attribute__((aligned)) AlignedType {};
30 
31 // EHABI  : 8-byte aligned
32 // Itanium: Largest supported alignment for the system
33 #if defined(_LIBCXXABI_ARM_EHABI)
34 #  define EXPECTED_ALIGNMENT 8
35 #else
36 #  define EXPECTED_ALIGNMENT alignof(AlignedType)
37 #endif
38 
39 static_assert(alignof(_Unwind_Exception) == EXPECTED_ALIGNMENT,
40   "_Unwind_Exception is incorrectly aligned. This test is expected to fail");
41 
42 struct MinAligned {  };
43 static_assert(alignof(MinAligned) == 1 && sizeof(MinAligned) == 1, "");
44 
45 int main() {
46   for (int i=0; i < 10; ++i) {
47     try {
48       throw MinAligned{};
49     } catch (MinAligned const& ref) {
50       assert(reinterpret_cast<uintptr_t>(&ref) % EXPECTED_ALIGNMENT == 0);
51     }
52   }
53 }
54