aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/x86_64/arch_backtrace.h
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2024-05-31 12:26:43 +0200
committerFlorian Weimer <fweimer@redhat.com>2024-05-31 22:49:18 +0200
commitc5f7f4fc8bb857cbe07972ff1e29970b101e9995 (patch)
tree3ee7b5b3127b3be0bf34b291dd0537f207b5d679 /sysdeps/x86_64/arch_backtrace.h
parent90ee0d87302810f1670a1fbcf9455b883309b1de (diff)
downloadglibc-c5f7f4fc8bb857cbe07972ff1e29970b101e9995.zip
glibc-c5f7f4fc8bb857cbe07972ff1e29970b101e9995.tar.gz
glibc-c5f7f4fc8bb857cbe07972ff1e29970b101e9995.tar.bz2
x86_64: Use shadow stack for backtrace implementationfw/x86-shstk-backtrace
Test failures: FAIL: debug/tst-backtrace4 FAIL: misc/tst-sigcontext-get_pc The return address of signal handlers is not on the shadow stack.
Diffstat (limited to 'sysdeps/x86_64/arch_backtrace.h')
-rw-r--r--sysdeps/x86_64/arch_backtrace.h62
1 files changed, 62 insertions, 0 deletions
diff --git a/sysdeps/x86_64/arch_backtrace.h b/sysdeps/x86_64/arch_backtrace.h
new file mode 100644
index 0000000..64f173f
--- /dev/null
+++ b/sysdeps/x86_64/arch_backtrace.h
@@ -0,0 +1,62 @@
+/* Architecture-specific backtrace implementation. x86-64 version.
+ Copyright (C) 2024 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
+
+#include <stdbool.h>
+#include <sysdep.h>
+
+/* From libc_sigaction.c. */
+extern void restore_rt (void) asm ("__restore_rt") attribute_hidden;
+
+/* Copy addresses from the shadow stack if available. */
+static inline __always_inline int
+__arch_backtrace (void **array, int size)
+{
+#if CET_ENABLED
+ void **ssp;
+ asm ("rdsspq %0"
+ : "=r" (ssp)
+ : "0" (0));
+ if (ssp == NULL)
+ return -1;
+
+ void **ssp_base = (void **) THREAD_GETMEM (THREAD_SELF, header.ssp_base);
+ if (ssp_base < ssp)
+ /* Covers the NULL case. */
+ return 0;
+
+ long int limit = ssp_base - ssp;
+ if (limit > size)
+ limit = size;
+
+#if 1
+ __builtin_memcpy (array, ssp, limit * sizeof (*array));
+ return limit;
+#else
+ /* We cannot use memcpy because we need to filter out signal
+ frames. */
+ int count = 0;
+ for (unsigned int i = 0; i < limit; ++i)
+ if (ssp[i] != restore_rt)
+ array[count++] = ssp[i];
+ return count;
+#endif
+
+#else /* !CET_ENABLED */
+ return -1;
+#endif
+}