Bug 494327

Summary: Crash when running Helgrind built with #define TRACE_PTH_FNS 1
Product: [Developer tools] valgrind Reporter: Paul Floyd <pjfloyd>
Component: helgrindAssignee: Paul Floyd <pjfloyd>
Status: RESOLVED FIXED    
Severity: crash    
Priority: NOR    
Version: 3.24 GIT   
Target Milestone: ---   
Platform: Other   
OS: FreeBSD   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Paul Floyd 2024-10-09 06:22:47 UTC
I get this segfault:

==68279== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==68279==  Bad permissions for mapped region at address 0x1FFEDFFFF8
==68279==    at 0x49B4F5C: vfprintf_l (in /lib/libc.so.7)
==68279==    by 0x49ADA3E: fprintf (in /lib/libc.so.7)
==68279==    by 0x4857A7E: mutex_lock_WRK (hg_intercepts.c:931)
==68279==    by 0x49AB6D6: flockfile (in /lib/libc.so.7)
==68279==    by 0x49B4FB1: vfprintf_l (in /lib/libc.so.7)
==68279==    by 0x49ADA3E: fprintf (in /lib/libc.so.7)
==68279==    by 0x4857A7E: mutex_lock_WRK (hg_intercepts.c:931)
==68279==    by 0x49AB6D6: flockfile (in /lib/libc.so.7)
==68279==    by 0x49B4FB1: vfprintf_l (in /lib/libc.so.7)
==68279==    by 0x49ADA3E: fprintf (in /lib/libc.so.7)
==68279==    by 0x4857A7E: mutex_lock_WRK (hg_intercepts.c:931)
==68279==    by 0x49AB6D6: flockfile (in /lib/libc.so.7)

The code

static int mutex_lock_WRK(pthread_mutex_t *mutex)
{
   int    ret;
   OrigFn fn;
   VALGRIND_GET_ORIG_FN(fn);
   if (TRACE_PTH_FNS) {
      fprintf(stderr, "<< pthread_mxlock %p", mutex); fflush(stderr);
   }

My guess here is that FreeBSD libc fprintf makes a call to pthread_mutex_lock with the result that the code gets stuck in infinite recursion.
Comment 1 Paul Floyd 2024-11-03 19:45:31 UTC
commit a2ef9690458cf472cb8a4da3793657b2c5a8cda6 (HEAD -> master, origin/master, origin/HEAD)
Author: Paul Floyd <pjfloyd@wanadoo.fr>
Date:   Sun Nov 3 20:42:43 2024 +0100

    Bug 494327 - Crash when running Helgrind built with #define TRACE_PTH_FNS 1
    
    Use write() rather than 'fprintf()' for the TRACE_PTH_FNS blocks for
    pthread_mutex_lock and pthread_mutex_lock. Mixing FILE and fd isn't
    great, but this is to stderr which gets flushed on every line, and
    it is only for developer builds that modify that TRACE_PTH_FNS macro.