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