From 5443d7daada137cfd27b5f39ee056cc96f0ea44e Mon Sep 17 00:00:00 2001 From: Louis Brunner Date: Sun, 16 Mar 2025 14:20:29 +0000 Subject: [PATCH 1/2] fix: make drd work again --- README.md | 1 - coregrind/m_mach/dyld_cache.c | 31 +++++++++++++++++++++++++++---- coregrind/m_main.c | 3 ++- coregrind/pub_core_mach.h | 2 +- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 55f56eff3..e7ccd57f3 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ This repository contains a version of Valgrind including a few patches to improv Note that every version from macOS 10.12 onwards currently has the following issues: - using threads and signals together is undefined (crashes, hanging, etc), note: a few tests were disabled because of that -- drd crashes on 10.15 (probably onwards) - dhat crashes (seen macOS 14 arm64) ## Usage diff --git a/coregrind/m_mach/dyld_cache.c b/coregrind/m_mach/dyld_cache.c index 5d8158473..db76d6770 100644 --- a/coregrind/m_mach/dyld_cache.c +++ b/coregrind/m_mach/dyld_cache.c @@ -62,11 +62,13 @@ static void output_debug_info(const dyld_cache_header* dyld_cache); typedef struct { const dyld_cache_header* header; Addr slide; + Bool tried; } DYLDCache; static DYLDCache dyld_cache = { .header = NULL, .slide = 0, + .tried = False, }; static Addr calculate_relative(const dyld_cache_header * header, Addr offset) { @@ -208,13 +210,23 @@ Addr VG_(dyld_cache_get_slide)(void) { return dyld_cache.slide; } -void VG_(dyld_cache_init)(void) { +int ensure_init(void) { + if (dyld_cache.header != NULL) { + return 1; + } + + // FIXME: unlikely race condition? + if (dyld_cache.tried) { + return 0; + } + dyld_cache.tried = True; + if (!try_to_init()) { VG_(dmsg)( "WARNING: could not read from dyld shared cache (DSC)\n" "Some reports (especially memory leaks) might be missing or incorrect (false-positives)\n" ); - return; + return 0; } #if defined(VGP_arm64_darwin) // We currently detect if dyld is loading/using a library by checking if stat64 fails. @@ -225,11 +237,22 @@ void VG_(dyld_cache_init)(void) { VG_(dyld_cache_load_library)("/usr/lib/system/libsystem_pthread.dylib"); VG_(dyld_cache_load_library)("/usr/lib/system/libsystem_platform.dylib"); #endif + + return 1; +} + +void VG_(dyld_cache_init)(const HChar* tool) { + // drd crashes if you map memory segments in m_main + if (VG_(strcmp)(tool, "drd") == 0) { + return; + } + + ensure_init(); } int VG_(dyld_cache_might_be_in)(const HChar* path) { // If not init'd, there is no point - if (dyld_cache.header == NULL) { + if (!ensure_init()) { return 0; } @@ -269,7 +292,7 @@ int VG_(dyld_cache_load_library)(const HChar* path) { SizeT len = 0; // If not init'd, there is no point trying - if (dyld_cache.header == NULL) { + if (!ensure_init()) { return 0; } diff --git a/coregrind/m_main.c b/coregrind/m_main.c index 432e264e4..099119db7 100644 --- a/coregrind/m_main.c +++ b/coregrind/m_main.c @@ -1949,9 +1949,10 @@ Int valgrind_main ( Int argc, HChar **argv, HChar **envp ) // Initialize the dyld cache, which is required with macOS 11 (Big Sur) and onwards // as some system libraries aren't provided on the disk anymore // p: none + // Note: some tools don't like to start mapping memory right way, so we do it lazily in those cases. //-------------------------------------------------------------- # if defined(VGO_darwin) && DARWIN_VERS >= DARWIN_11_00 - VG_(dyld_cache_init)(); + VG_(dyld_cache_init)(VG_(clo_toolname)); # endif //-------------------------------------------------------------- diff --git a/coregrind/pub_core_mach.h b/coregrind/pub_core_mach.h index 19cfff90a..08193a4a6 100644 --- a/coregrind/pub_core_mach.h +++ b/coregrind/pub_core_mach.h @@ -72,7 +72,7 @@ extern void VG_(mach_record_system_memory)(void); #if DARWIN_VERS >= DARWIN_11_00 // Dyld shared cache (DSC) parsing, which is required as system libraries are not provided on disk // starting with macOS 11.0 (Big Sur) -extern void VG_(dyld_cache_init)(void); +extern void VG_(dyld_cache_init)(const HChar*); extern int VG_(dyld_cache_might_be_in)(const HChar*); extern int VG_(dyld_cache_load_library)(const HChar*); extern Addr VG_(dyld_cache_get_slide)(void); From 5c41e4ccd52db5e23f7f6b325c889f40a1cc2c7b Mon Sep 17 00:00:00 2001 From: Louis Brunner Date: Thu, 20 Mar 2025 15:35:23 +0000 Subject: [PATCH 2/2] disable test on macOS --- drd/tests/pth_cond_destroy_busy.vgtest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drd/tests/pth_cond_destroy_busy.vgtest b/drd/tests/pth_cond_destroy_busy.vgtest index f3cf77825..e98a50c8a 100644 --- a/drd/tests/pth_cond_destroy_busy.vgtest +++ b/drd/tests/pth_cond_destroy_busy.vgtest @@ -1,2 +1,2 @@ -prereq: ./supported_libpthread && ! ../../tests/libc_test glibc 2.24.90 +prereq: ./supported_libpthread && ! ../../tests/os_test darwin && ! ../../tests/libc_test glibc 2.24.90 prog: pth_cond_destroy_busy