]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Handle top __syscall_cancel frames when getting stack traces
authorMark Wielaard <mark@klomp.org>
Sun, 30 Mar 2025 15:38:21 +0000 (17:38 +0200)
committerMark Wielaard <mark@klomp.org>
Sun, 30 Mar 2025 17:20:56 +0000 (19:20 +0200)
Since glibc 2.41 there are extra frames inserted before doing a
syscall to support proper thread cancellation.  This breaks various
suppressions and regtests involving checking syscall arguments.

Solve this by removing those extra frames from the top of the call
stack when we are processing a linux system call.

https://bugs.kde.org/show_bug.cgi?id=502126

NEWS
coregrind/m_stacktrace.c

diff --git a/NEWS b/NEWS
index 40b9539596bc84f2642caaebc2a026facfffca7a..7145157f2112d3b7d2b041a98ca58a57b2f5a463 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -70,6 +70,7 @@ are not entered into bugzilla tend to get forgotten about or ignored.
 501846  Add x86 Linux shm wrappers
 501850  FreeBSD syscall arguments 7 and 8 incorrect.
 501893  Missing suppression for __wcscat_avx2 (strcat-strlen-avx2.h.S:68)?
+502126  glibc 2.41 extra syscall_cancel frames
 
 To see details of a given bug, visit
   https://bugs.kde.org/show_bug.cgi?id=XXXXXX
index cbeaab9db1ded09c7330e054d6f15013b99768d5..21c0f47942071d8e9e7f6e3d750d29fbf07d491d 100644 (file)
@@ -9,6 +9,8 @@
 
    Copyright (C) 2000-2017 Julian Seward 
       jseward@acm.org
+   Copyright (C) 2025 Mark J. Wielaard
+      mark@klomp.org
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -1693,10 +1695,59 @@ UInt VG_(get_StackTrace_with_deltas)(
                   tid, stack_highest_byte,
                   startRegs.r_pc, startRegs.r_sp);
 
-   return VG_(get_StackTrace_wrk)(tid, ips, n_ips, 
+   Int found = VG_(get_StackTrace_wrk)(tid, ips, n_ips,
                                        sps, fps,
                                        &startRegs,
                                        stack_highest_byte);
+
+#if defined(VGO_linux)
+   /* glibc might insert some extra frames before doing a syscall to support
+      thread cancellation.  This breaks various suppressions and regtests
+      involving checking syscall arguments. So when processing a syscall just
+      remove those extra frames from the top of the call stack.  */
+   if (VG_(is_in_syscall)(tid)) {
+      Int i;
+      Int start = 0;
+      DiEpoch ep = VG_(current_DiEpoch)();
+      for (i = 0; i < found; i++) {
+         /* This could be made a little more efficient by doing the lookups
+            for the symbols at glibc load time and check the address falls
+            inside the function symbol address range here. But given this
+            is only called during syscall processing, this is probably fine
+            for now.  */
+         const HChar *buf;
+         if (VG_(get_fnname_raw)(ep, ips[i], &buf)) { // raw, don't demangle
+            if (VG_STREQ("__syscall_cancel_arch", buf) ||
+                VG_STREQ("__internal_syscall_cancel", buf) ||
+#if defined(VGP_x86_linux) || defined(VGP_arm_linux)
+                VG_STREQ("__libc_do_syscall", buf) ||
+#endif
+#if defined(VGP_x86_linux)
+                VG_STREQ("_dl_sysinfo_int80", buf) ||
+#endif
+                VG_STREQ("__syscall_cancel", buf)) {
+               start++;
+               continue; // Maybe the next one is special too?
+            } else {
+               break; // Not special, only skip top stack names.
+            }
+         } else {
+            break; // No name, not special, don't skip.
+         }
+      }
+
+      if (start > 0) {
+         for (i = 0; i < (found - start); i++) {
+            ips[i] = ips[i + start];
+            if (sps) sps[i] = sps[i + start];
+            if (fps) fps[i] = fps[i + start];
+         }
+         return found - start;
+      }
+   }
+#endif
+
+   return found;
 }
 
 UInt VG_(get_StackTrace) ( ThreadId tid,