[compiler-rt] Dump regs fbsd fix (PR #99676)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 19 12:55:20 PDT 2024


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/99676

>From 8d2f079a5a6f6a2ada047733a118773aa19801e4 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 19 Jul 2024 19:57:32 +0100
Subject: [PATCH 1/3] implement dump registers for freebsd amd64/fix build

---
 .../lib/sanitizer_common/sanitizer_linux.cpp  | 37 +++++++++++++++++--
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 47f9f0c4590fb..4e8452d459885 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -2121,7 +2121,8 @@ bool SignalContext::IsTrueFaultingAddress() const {
 UNUSED
 static const char *RegNumToRegName(int reg) {
   switch (reg) {
-#  if defined(__x86_64__)
+#  if SANITIZER_LINUX
+#    if defined(__x86_64__)
     case REG_RAX:
       return "rax";
     case REG_RBX:
@@ -2154,7 +2155,7 @@ static const char *RegNumToRegName(int reg) {
       return "r14";
     case REG_R15:
       return "r15";
-#  elif defined(__i386__)
+#    elif defined(__i386__)
     case REG_EAX:
       return "eax";
     case REG_EBX:
@@ -2171,6 +2172,7 @@ static const char *RegNumToRegName(int reg) {
       return "ebp";
     case REG_ESP:
       return "esp";
+#    endif
 #  endif
     default:
       return NULL;
@@ -2178,6 +2180,7 @@ static const char *RegNumToRegName(int reg) {
   return NULL;
 }
 
+#if SANITIZER_LINUX
 UNUSED
 static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
   const char *RegName = RegNumToRegName(RegNum);
@@ -2190,10 +2193,11 @@ static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
   (void)RegName;
 #  endif
 }
+#endif
 
 void SignalContext::DumpAllRegisters(void *context) {
-#  if SANITIZER_LINUX
   ucontext_t *ucontext = (ucontext_t *)context;
+#  if SANITIZER_LINUX
 #    if defined(__x86_64__)
   Report("Register values:\n");
   DumpSingleReg(ucontext, REG_RAX);
@@ -2232,8 +2236,35 @@ void SignalContext::DumpAllRegisters(void *context) {
   DumpSingleReg(ucontext, REG_EBP);
   DumpSingleReg(ucontext, REG_ESP);
   Printf("\n");
+#    else
+  (void)ucontext;
 #    endif
+#  elif SANITIZER_FREEBSD
+#    if defined(__x86_64__)
+  Report("Register values:\n");
+  Printf("RAX = 0x%016llx  ", ucontext->uc_mcontext.mc_rax);
+  Printf("RBX = 0x%016llx  ", ucontext->uc_mcontext.mc_rbx);
+  Printf("RCX = 0x%016llx  ", ucontext->uc_mcontext.mc_rcx);
+  Printf("RDX = 0x%016llx  ", ucontext->uc_mcontext.mc_rdx);
+  Printf("\n");
+  Printf("RDI = 0x%016llx  ", ucontext->uc_mcontext.mc_rdi);
+  Printf("RSI = 0x%016llx  ", ucontext->uc_mcontext.mc_rsi);
+  Printf("RBP = 0x%016llx  ", ucontext->uc_mcontext.mc_rbp);
+  Printf("RSP = 0x%016llx  ", ucontext->uc_mcontext.mc_rsp);
+  Printf("\n");
+  Printf(" R8 = 0x%016llx  ", ucontext->uc_mcontext.mc_r8);
+  Printf(" R9 = 0x%016llx  ", ucontext->uc_mcontext.mc_r9);
+  Printf("R10 = 0x%016llx  ", ucontext->uc_mcontext.mc_r10);
+  Printf("R11 = 0x%016llx  ", ucontext->uc_mcontext.mc_r11);
+  Printf("\n");
+  Printf("R12 = 0x%016llx  ", ucontext->uc_mcontext.mc_r12);
+  Printf("R13 = 0x%016llx  ", ucontext->uc_mcontext.mc_r13);
+  Printf("R14 = 0x%016llx  ", ucontext->uc_mcontext.mc_r14);
+  Printf("R15 = 0x%016llx  ", ucontext->uc_mcontext.mc_r15);
+  Printf("\n");
+#    else
   (void)ucontext;
+#    endif
 #  endif
   // FIXME: Implement this for other OSes and architectures.
 }

>From 06ad6b291305fea9e58ef0dd48febcf723aba23d Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 19 Jul 2024 20:02:10 +0100
Subject: [PATCH 2/3] safestack build fix for freebsd i386

---
 .../lib/safestack/safestack_platform.h        |  2 +
 .../lib/sanitizer_common/sanitizer_linux.cpp  | 44 +++++++++----------
 2 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/compiler-rt/lib/safestack/safestack_platform.h b/compiler-rt/lib/safestack/safestack_platform.h
index 68d26813bef4a..41c7c25fdaf4d 100644
--- a/compiler-rt/lib/safestack/safestack_platform.h
+++ b/compiler-rt/lib/safestack/safestack_platform.h
@@ -143,6 +143,8 @@ inline void *Mmap(void *addr, size_t length, int prot, int flags, int fd,
   return __mmap(addr, length, prot, flags, fd, 0, offset);
 #elif SANITIZER_FREEBSD && (defined(__aarch64__) || defined(__x86_64__))
   return (void *)__syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
+#elif SANITIZER_FREEBSD && (defined(__i386__))
+  return (void *)syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
 #elif SANITIZER_SOLARIS
   return _REAL64(mmap)(addr, length, prot, flags, fd, offset);
 #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 4e8452d459885..40c46a8949636 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -2180,20 +2180,20 @@ static const char *RegNumToRegName(int reg) {
   return NULL;
 }
 
-#if SANITIZER_LINUX
+#  if SANITIZER_LINUX
 UNUSED
 static void DumpSingleReg(ucontext_t *ctx, int RegNum) {
   const char *RegName = RegNumToRegName(RegNum);
-#  if defined(__x86_64__)
+#    if defined(__x86_64__)
   Printf("%s%s = 0x%016llx  ", internal_strlen(RegName) == 2 ? " " : "",
          RegName, ctx->uc_mcontext.gregs[RegNum]);
-#  elif defined(__i386__)
+#    elif defined(__i386__)
   Printf("%s = 0x%08x  ", RegName, ctx->uc_mcontext.gregs[RegNum]);
-#  else
+#    else
   (void)RegName;
-#  endif
+#    endif
 }
-#endif
+#  endif
 
 void SignalContext::DumpAllRegisters(void *context) {
   ucontext_t *ucontext = (ucontext_t *)context;
@@ -2242,25 +2242,25 @@ void SignalContext::DumpAllRegisters(void *context) {
 #  elif SANITIZER_FREEBSD
 #    if defined(__x86_64__)
   Report("Register values:\n");
-  Printf("RAX = 0x%016llx  ", ucontext->uc_mcontext.mc_rax);
-  Printf("RBX = 0x%016llx  ", ucontext->uc_mcontext.mc_rbx);
-  Printf("RCX = 0x%016llx  ", ucontext->uc_mcontext.mc_rcx);
-  Printf("RDX = 0x%016llx  ", ucontext->uc_mcontext.mc_rdx);
+  Printf("rax = 0x%016llx  ", ucontext->uc_mcontext.mc_rax);
+  Printf("rbx = 0x%016llx  ", ucontext->uc_mcontext.mc_rbx);
+  Printf("rcx = 0x%016llx  ", ucontext->uc_mcontext.mc_rcx);
+  Printf("rdx = 0x%016llx  ", ucontext->uc_mcontext.mc_rdx);
   Printf("\n");
-  Printf("RDI = 0x%016llx  ", ucontext->uc_mcontext.mc_rdi);
-  Printf("RSI = 0x%016llx  ", ucontext->uc_mcontext.mc_rsi);
-  Printf("RBP = 0x%016llx  ", ucontext->uc_mcontext.mc_rbp);
-  Printf("RSP = 0x%016llx  ", ucontext->uc_mcontext.mc_rsp);
+  Printf("rdi = 0x%016llx  ", ucontext->uc_mcontext.mc_rdi);
+  Printf("rsi = 0x%016llx  ", ucontext->uc_mcontext.mc_rsi);
+  Printf("rbp = 0x%016llx  ", ucontext->uc_mcontext.mc_rbp);
+  Printf("rsp = 0x%016llx  ", ucontext->uc_mcontext.mc_rsp);
   Printf("\n");
-  Printf(" R8 = 0x%016llx  ", ucontext->uc_mcontext.mc_r8);
-  Printf(" R9 = 0x%016llx  ", ucontext->uc_mcontext.mc_r9);
-  Printf("R10 = 0x%016llx  ", ucontext->uc_mcontext.mc_r10);
-  Printf("R11 = 0x%016llx  ", ucontext->uc_mcontext.mc_r11);
+  Printf(" r8 = 0x%016llx  ", ucontext->uc_mcontext.mc_r8);
+  Printf(" r9 = 0x%016llx  ", ucontext->uc_mcontext.mc_r9);
+  Printf("r10 = 0x%016llx  ", ucontext->uc_mcontext.mc_r10);
+  Printf("r11 = 0x%016llx  ", ucontext->uc_mcontext.mc_r11);
   Printf("\n");
-  Printf("R12 = 0x%016llx  ", ucontext->uc_mcontext.mc_r12);
-  Printf("R13 = 0x%016llx  ", ucontext->uc_mcontext.mc_r13);
-  Printf("R14 = 0x%016llx  ", ucontext->uc_mcontext.mc_r14);
-  Printf("R15 = 0x%016llx  ", ucontext->uc_mcontext.mc_r15);
+  Printf("r12 = 0x%016llx  ", ucontext->uc_mcontext.mc_r12);
+  Printf("r13 = 0x%016llx  ", ucontext->uc_mcontext.mc_r13);
+  Printf("r14 = 0x%016llx  ", ucontext->uc_mcontext.mc_r14);
+  Printf("r15 = 0x%016llx  ", ucontext->uc_mcontext.mc_r15);
   Printf("\n");
 #    else
   (void)ucontext;

>From 388f97b635dd6418577c68b186cacfe23bc363a0 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 19 Jul 2024 21:55:05 +0100
Subject: [PATCH 3/3] add test

---
 .../FreeBSD/dump_registers_x86_64.cpp         | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/FreeBSD/dump_registers_x86_64.cpp

diff --git a/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/dump_registers_x86_64.cpp b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/dump_registers_x86_64.cpp
new file mode 100644
index 0000000000000..3d11ef0e098f1
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/dump_registers_x86_64.cpp
@@ -0,0 +1,19 @@
+// Check that sanitizer prints registers dump_registers on dump_registers=1
+// RUN: %clangxx  %s -o %t
+// RUN: %env_tool_opts=dump_registers=0 not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-NODUMP --strict-whitespace
+// RUN: not %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK-DUMP --strict-whitespace
+//
+// REQUIRES: x86_64-target-arch
+
+#include <signal.h>
+
+int main() {
+  raise(SIGSEGV);
+  // CHECK-DUMP: Register values
+  // CHECK-DUMP-NEXT: rax = {{0x[0-9a-f]+}}  rbx = {{0x[0-9a-f]+}}  rcx = {{0x[0-9a-f]+}}  rdx = {{0x[0-9a-f]+}}
+  // CHECK-DUMP-NEXT: rdi = {{0x[0-9a-f]+}}  rsi = {{0x[0-9a-f]+}}  rbp = {{0x[0-9a-f]+}}  rsp = {{0x[0-9a-f]+}}
+  // CHECK-DUMP-NEXT:  r8 = {{0x[0-9a-f]+}}   r9 = {{0x[0-9a-f]+}}  r10 = {{0x[0-9a-f]+}}  r11 = {{0x[0-9a-f]+}}
+  // CHECK-DUMP-NEXT: r12 = {{0x[0-9a-f]+}}  r13 = {{0x[0-9a-f]+}}  r14 = {{0x[0-9a-f]+}}  r15 = {{0x[0-9a-f]+}}
+  // CHECK-NODUMP-NOT: Register values
+  return 0;
+}



More information about the llvm-commits mailing list