Skip to content

Commit 542db87

Browse files
committed
[libunwind] Ensure enough alignment for unw_cursor_t for SEH build configurations
When built in SEH mode, UnwindCursor contains a CONTEXT struct, which is aligned to 16 bytes in most configurations, causing the whole UnwindCursor object to have 16 byte alignment. This fixes backtraces using _Unwind_Backtrace on x86_64 mingw, where an unw_cursor_t allocated on the stack was misaligned before. This is an ABI break for this struct for this configuration, but very few callers call libunwind directly (and even fewer directly allocate an unw_cursor_t anyway). Differential Revision: https://reviews.llvm.org/D86102
1 parent c1dc44f commit 542db87

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

libunwind/include/libunwind.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
#define LIBUNWIND_AVAIL
4444
#endif
4545

46+
#if defined(_WIN32) && defined(__SEH__)
47+
#define LIBUNWIND_CURSOR_ALIGNMENT_ATTR __attribute__((__aligned__(16)))
48+
#else
49+
#define LIBUNWIND_CURSOR_ALIGNMENT_ATTR
50+
#endif
51+
4652
/* error codes */
4753
enum {
4854
UNW_ESUCCESS = 0, /* no error */
@@ -68,7 +74,7 @@ typedef struct unw_context_t unw_context_t;
6874

6975
struct unw_cursor_t {
7076
uint64_t data[_LIBUNWIND_CURSOR_SIZE];
71-
};
77+
} LIBUNWIND_CURSOR_ALIGNMENT_ATTR;
7278
typedef struct unw_cursor_t unw_cursor_t;
7379

7480
typedef struct unw_addr_space *unw_addr_space_t;

libunwind/src/UnwindCursor.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
530530
: _addressSpace(as), _unwindInfoMissing(false) {
531531
static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
532532
"UnwindCursor<> does not fit in unw_cursor_t");
533+
static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
534+
"UnwindCursor<> requires more alignment than unw_cursor_t");
533535
memset(&_info, 0, sizeof(_info));
534536
memset(&_histTable, 0, sizeof(_histTable));
535537
_dispContext.ContextRecord = &_msContext;
@@ -1182,6 +1184,8 @@ UnwindCursor<A, R>::UnwindCursor(unw_context_t *context, A &as)
11821184
_isSignalFrame(false) {
11831185
static_assert((check_fit<UnwindCursor<A, R>, unw_cursor_t>::does_fit),
11841186
"UnwindCursor<> does not fit in unw_cursor_t");
1187+
static_assert((alignof(UnwindCursor<A, R>) <= alignof(unw_cursor_t)),
1188+
"UnwindCursor<> requires more alignment than unw_cursor_t");
11851189
memset(&_info, 0, sizeof(_info));
11861190
}
11871191

0 commit comments

Comments
 (0)