authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-23 02:15:50+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-09-19 18:20:21-07:00
log26ddfabba4611231fa486322b9ba11b1057a2053
treeb083f47f7a7f578da30d39605fece3ffaa7ce07e
parentdc14434c0aed409a9152a05e4741623e0e99f32a

libunwind: Update to LLVM 19.


10 files changed, 118 insertions(+), 23 deletions(-)

lib/libunwind/include/__libunwind_config.h+4
......@@ -180,6 +180,10 @@
180180#endif
181181#define _LIBUNWIND_HIGHEST_DWARF_REGISTER \
182182 _LIBUNWIND_HIGHEST_DWARF_REGISTER_LOONGARCH
183#elif defined(__wasm__)
184// Unused
185#define _LIBUNWIND_CONTEXT_SIZE 0
186#define _LIBUNWIND_CURSOR_SIZE 0
183187# else
184188# error "Unsupported architecture."
185189# endif
lib/libunwind/src/Registers.hpp+7
......@@ -1815,6 +1815,13 @@ inline const char *Registers_ppc64::getRegisterName(int regNum) {
18151815/// process.
18161816class _LIBUNWIND_HIDDEN Registers_arm64;
18171817extern "C" void __libunwind_Registers_arm64_jumpto(Registers_arm64 *);
1818
1819#if defined(_LIBUNWIND_USE_GCS)
1820extern "C" void *__libunwind_cet_get_jump_target() {
1821 return reinterpret_cast<void *>(&__libunwind_Registers_arm64_jumpto);
1822}
1823#endif
1824
18181825class _LIBUNWIND_HIDDEN Registers_arm64 {
18191826public:
18201827 Registers_arm64();
lib/libunwind/src/Unwind-wasm.c+2-2
......@@ -14,7 +14,7 @@
1414
1515#include "config.h"
1616
17#ifdef __USING_WASM_EXCEPTIONS__
17#ifdef __WASM_EXCEPTIONS__
1818
1919#include "unwind.h"
2020#include <threads.h>
......@@ -120,4 +120,4 @@ _Unwind_GetRegionStart(struct _Unwind_Context *context) {
120120 return 0;
121121}
122122
123#endif // defined(__USING_WASM_EXCEPTIONS__)
123#endif // defined(__WASM_EXCEPTIONS__)
lib/libunwind/src/UnwindCursor.hpp+13-5
......@@ -36,7 +36,6 @@
3636#include <errno.h>
3737#include <signal.h>
3838#include <sys/syscall.h>
39#include <sys/uio.h>
4039#include <unistd.h>
4140#define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1
4241#endif
......@@ -472,7 +471,7 @@ public:
472471 }
473472#endif
474473
475#if defined(_LIBUNWIND_USE_CET)
474#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
476475 virtual void *get_registers() {
477476 _LIBUNWIND_ABORT("get_registers not implemented");
478477 }
......@@ -955,7 +954,7 @@ public:
955954 virtual uintptr_t getDataRelBase();
956955#endif
957956
958#if defined(_LIBUNWIND_USE_CET)
957#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
959958 virtual void *get_registers() { return &_registers; }
960959#endif
961960
......@@ -2416,7 +2415,7 @@ int UnwindCursor<A, R>::stepWithTBTable(pint_t pc, tbtable *TBTable,
24162415 }
24172416
24182417 // Reset LR in the current context.
2419 newRegisters.setLR(NULL);
2418 newRegisters.setLR(static_cast<uintptr_t>(NULL));
24202419
24212420 _LIBUNWIND_TRACE_UNWINDING(
24222421 "Extract info from lastStack=%p, returnAddress=%p",
......@@ -2590,6 +2589,15 @@ void UnwindCursor<A, R>::setInfoBasedOnIPRegister(bool isReturnAddress) {
25902589 --pc;
25912590#endif
25922591
2592#if !(defined(_LIBUNWIND_SUPPORT_SEH_UNWIND) && defined(_WIN32)) && \
2593 !defined(_LIBUNWIND_SUPPORT_TBTAB_UNWIND)
2594 // In case of this is frame of signal handler, the IP saved in the signal
2595 // handler points to first non-executed instruction, while FDE/CIE expects IP
2596 // to be after the first non-executed instruction.
2597 if (_isSignalFrame)
2598 ++pc;
2599#endif
2600
25932601 // Ask address space object to find unwind sections for this pc.
25942602 UnwindInfoSections sects;
25952603 if (_addressSpace.findUnwindSections(pc, sects)) {
......@@ -2997,7 +3005,7 @@ bool UnwindCursor<A, R>::isReadableAddr(const pint_t addr) const {
29973005}
29983006#endif
29993007
3000#if defined(_LIBUNWIND_USE_CET)
3008#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
30013009extern "C" void *__libunwind_cet_get_registers(unw_cursor_t *cursor) {
30023010 AbstractUnwindCursor *co = (AbstractUnwindCursor *)cursor;
30033011 return co->get_registers();
lib/libunwind/src/UnwindLevel1.c+30-4
......@@ -31,7 +31,8 @@
3131#include "libunwind_ext.h"
3232#include "unwind.h"
3333
34#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__)
34#if !defined(_LIBUNWIND_ARM_EHABI) && !defined(__USING_SJLJ_EXCEPTIONS__) && \
35 !defined(__wasm__)
3536
3637#ifndef _LIBUNWIND_SUPPORT_SEH_UNWIND
3738
......@@ -43,7 +44,7 @@
4344// _LIBUNWIND_POP_CET_SSP is used to adjust CET shadow stack pointer and we
4445// directly jump to __libunwind_Registers_x86/x86_64_jumpto instead of using
4546// a regular function call to avoid pushing to CET shadow stack again.
46#if !defined(_LIBUNWIND_USE_CET)
47#if !defined(_LIBUNWIND_USE_CET) && !defined(_LIBUNWIND_USE_GCS)
4748#define __unw_phase2_resume(cursor, fn) \
4849 do { \
4950 (void)fn; \
......@@ -71,6 +72,19 @@
7172 __asm__ volatile("jmpq *%%rdx\n\t" :: "D"(cetRegContext), \
7273 "d"(cetJumpAddress)); \
7374 } while (0)
75#elif defined(_LIBUNWIND_TARGET_AARCH64)
76#define __cet_ss_step_size 8
77#define __unw_phase2_resume(cursor, fn) \
78 do { \
79 _LIBUNWIND_POP_CET_SSP((fn)); \
80 void *cetRegContext = __libunwind_cet_get_registers((cursor)); \
81 void *cetJumpAddress = __libunwind_cet_get_jump_target(); \
82 __asm__ volatile("mov x0, %0\n\t" \
83 "br %1\n\t" \
84 : \
85 : "r"(cetRegContext), "r"(cetJumpAddress) \
86 : "x0"); \
87 } while (0)
7488#endif
7589
7690static _Unwind_Reason_Code
......@@ -169,6 +183,10 @@ unwind_phase1(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
169183}
170184extern int __unw_step_stage2(unw_cursor_t *);
171185
186#if defined(_LIBUNWIND_USE_GCS)
187// Enable the GCS target feature to permit gcspop instructions to be used.
188__attribute__((target("gcs")))
189#endif
172190static _Unwind_Reason_Code
173191unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *exception_object) {
174192 __unw_init_local(cursor, uc);
......@@ -179,8 +197,12 @@ unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
179197 // uc is initialized by __unw_getcontext in the parent frame. The first stack
180198 // frame walked is unwind_phase2.
181199 unsigned framesWalked = 1;
182#ifdef _LIBUNWIND_USE_CET
200#if defined(_LIBUNWIND_USE_CET)
183201 unsigned long shadowStackTop = _get_ssp();
202#elif defined(_LIBUNWIND_USE_GCS)
203 unsigned long shadowStackTop = 0;
204 if (__chkfeat(_CHKFEAT_GCS))
205 shadowStackTop = (unsigned long)__gcspr();
184206#endif
185207 // Walk each frame until we reach where search phase said to stop.
186208 while (true) {
......@@ -237,7 +259,7 @@ unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
237259// against return address stored in CET shadow stack, if the 2 addresses don't
238260// match, it means return address in normal stack has been corrupted, we return
239261// _URC_FATAL_PHASE2_ERROR.
240#ifdef _LIBUNWIND_USE_CET
262#if defined(_LIBUNWIND_USE_CET) || defined(_LIBUNWIND_USE_GCS)
241263 if (shadowStackTop != 0) {
242264 unw_word_t retInNormalStack;
243265 __unw_get_reg(cursor, UNW_REG_IP, &retInNormalStack);
......@@ -305,6 +327,10 @@ unwind_phase2(unw_context_t *uc, unw_cursor_t *cursor, _Unwind_Exception *except
305327 return _URC_FATAL_PHASE2_ERROR;
306328}
307329
330#if defined(_LIBUNWIND_USE_GCS)
331// Enable the GCS target feature to permit gcspop instructions to be used.
332__attribute__((target("gcs")))
333#endif
308334static _Unwind_Reason_Code
309335unwind_phase2_forced(unw_context_t *uc, unw_cursor_t *cursor,
310336 _Unwind_Exception *exception_object,
lib/libunwind/src/UnwindRegistersRestore.S+16-2
......@@ -20,7 +20,7 @@
2020 .text
2121#endif
2222
23#if !defined(__USING_SJLJ_EXCEPTIONS__)
23#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
2424
2525#if defined(__i386__)
2626DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_x86_jumpto)
......@@ -629,6 +629,10 @@ Lnovec:
629629
630630#elif defined(__aarch64__)
631631
632#if defined(__ARM_FEATURE_GCS_DEFAULT)
633.arch_extension gcs
634#endif
635
632636//
633637// extern "C" void __libunwind_Registers_arm64_jumpto(Registers_arm64 *);
634638//
......@@ -680,6 +684,16 @@ DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_arm64_jumpto)
680684 ldr x16, [x0, #0x0F8]
681685 ldp x0, x1, [x0, #0x000] // restore x0,x1
682686 mov sp,x16 // restore sp
687#if defined(__ARM_FEATURE_GCS_DEFAULT)
688 // If GCS is enabled we need to push the address we're returning to onto the
689 // GCS stack. We can't just return using br, as there won't be a BTI landing
690 // pad instruction at the destination.
691 mov x16, #1
692 chkfeat x16
693 cbnz x16, Lnogcs
694 gcspushm x30
695Lnogcs:
696#endif
683697 ret x30 // jump to pc
684698
685699#elif defined(__arm__) && !defined(__APPLE__)
......@@ -1232,7 +1246,7 @@ DEFINE_LIBUNWIND_FUNCTION(_ZN9libunwind19Registers_loongarch6jumptoEv)
12321246
12331247#endif
12341248
1235#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
1249#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
12361250
12371251NO_EXEC_STACK_DIRECTIVE
12381252
lib/libunwind/src/UnwindRegistersSave.S+2-2
......@@ -20,7 +20,7 @@
2020 .text
2121#endif
2222
23#if !defined(__USING_SJLJ_EXCEPTIONS__)
23#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
2424
2525#if defined(__i386__)
2626
......@@ -1177,6 +1177,6 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
11771177
11781178 WEAK_ALIAS(__unw_getcontext, unw_getcontext)
11791179
1180#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) */
1180#endif /* !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__) */
11811181
11821182NO_EXEC_STACK_DIRECTIVE
lib/libunwind/src/assembly.h+20-5
......@@ -82,7 +82,22 @@
8282#define PPC64_OPD2
8383#endif
8484
85#if defined(__aarch64__) && defined(__ARM_FEATURE_BTI_DEFAULT)
85#if defined(__aarch64__)
86#if defined(__ARM_FEATURE_GCS_DEFAULT) && defined(__ARM_FEATURE_BTI_DEFAULT)
87// Set BTI, PAC, and GCS gnu property bits
88#define GNU_PROPERTY 7
89// We indirectly branch to __libunwind_Registers_arm64_jumpto from
90// __unw_phase2_resume, so we need to use bti jc.
91#define AARCH64_BTI bti jc
92#elif defined(__ARM_FEATURE_GCS_DEFAULT)
93// Set GCS gnu property bit
94#define GNU_PROPERTY 4
95#elif defined(__ARM_FEATURE_BTI_DEFAULT)
96// Set BTI and PAC gnu property bits
97#define GNU_PROPERTY 3
98#define AARCH64_BTI bti c
99#endif
100#ifdef GNU_PROPERTY
86101 .pushsection ".note.gnu.property", "a" SEPARATOR \
87102 .balign 8 SEPARATOR \
88103 .long 4 SEPARATOR \
......@@ -91,12 +106,12 @@
91106 .asciz "GNU" SEPARATOR \
92107 .long 0xc0000000 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */ \
93108 .long 4 SEPARATOR \
94 .long 3 SEPARATOR /* GNU_PROPERTY_AARCH64_FEATURE_1_BTI AND */ \
95 /* GNU_PROPERTY_AARCH64_FEATURE_1_PAC */ \
109 .long GNU_PROPERTY SEPARATOR \
96110 .long 0 SEPARATOR \
97111 .popsection SEPARATOR
98#define AARCH64_BTI bti c
99#else
112#endif
113#endif
114#if !defined(AARCH64_BTI)
100115#define AARCH64_BTI
101116#endif
102117
lib/libunwind/src/cet_unwind.h+22
......@@ -35,6 +35,28 @@
3535 } while (0)
3636#endif
3737
38// On AArch64 we use _LIBUNWIND_USE_GCS to indicate that GCS is supported. We
39// need to guard any use of GCS instructions with __chkfeat though, as GCS may
40// not be enabled.
41#if defined(_LIBUNWIND_TARGET_AARCH64) && defined(__ARM_FEATURE_GCS_DEFAULT)
42#include <arm_acle.h>
43
44// We can only use GCS if arm_acle.h defines the GCS intrinsics.
45#ifdef _CHKFEAT_GCS
46#define _LIBUNWIND_USE_GCS 1
47#endif
48
49#define _LIBUNWIND_POP_CET_SSP(x) \
50 do { \
51 if (__chkfeat(_CHKFEAT_GCS)) { \
52 unsigned tmp = (x); \
53 while (tmp--) \
54 __gcspopm(); \
55 } \
56 } while (0)
57
58#endif
59
3860extern void *__libunwind_cet_get_registers(unw_cursor_t *);
3961extern void *__libunwind_cet_get_jump_target(void);
4062
lib/libunwind/src/libunwind.cpp+2-3
......@@ -26,7 +26,7 @@
2626#include <sanitizer/asan_interface.h>
2727#endif
2828
29#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__USING_WASM_EXCEPTIONS__)
29#if !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
3030#include "AddressSpace.hpp"
3131#include "UnwindCursor.hpp"
3232
......@@ -347,8 +347,7 @@ void __unw_remove_dynamic_eh_frame_section(unw_word_t eh_frame_start) {
347347}
348348
349349#endif // defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
350#endif // !defined(__USING_SJLJ_EXCEPTIONS__) &&
351 // !defined(__USING_WASM_EXCEPTIONS__)
350#endif // !defined(__USING_SJLJ_EXCEPTIONS__) && !defined(__wasm__)
352351
353352#ifdef __APPLE__
354353