From 71ebcd3348061de2fde3a13f1adb56059aca5997 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Wed, 3 Aug 2022 15:45:18 -0700 Subject: [PATCH] [lldb] Make LLDB resilient against failing dyld introspection SPIs Make LLDB resilient against failing dyld introspection SPIs: - dyld_process_create_for_current_task - dyld_process_snapshot_create_for_process - dyld_process_snapshot_get_shared_cache These can all fail and return a nullptr. Instead of having an assert, which doesn't really make sense, as we have no control over whether these calls succeed or not, bail out gracefully and use the fallback logic. rdar://98070414 Differential revision: https://reviews.llvm.org/D131110 --- lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm | 33 +++++++++++++++++++----- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm index 74a4fb2..4cb9f5d 100644 --- a/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm +++ b/lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm @@ -16,6 +16,7 @@ #include "lldb/Utility/Timer.h" #include "Utility/UuidCompatibility.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/FileSystem.h" @@ -520,20 +521,33 @@ public: SharedCacheInfo(); private: + bool CreateSharedCacheInfoWithInstrospectionSPIs(); + llvm::StringMap m_images; UUID m_uuid; }; } -SharedCacheInfo::SharedCacheInfo() { +bool SharedCacheInfo::CreateSharedCacheInfoWithInstrospectionSPIs() { #if defined(SDK_HAS_NEW_DYLD_INTROSPECTION_SPIS) if (__builtin_available(macOS 12, *)) { if (dyld_process_create_for_current_task) { - auto dyld_process = dyld_process_create_for_current_task(); - auto snapshot = + dyld_process_t dyld_process = dyld_process_create_for_current_task(); + if (!dyld_process) + return false; + + dyld_process_snapshot_t snapshot = dyld_process_snapshot_create_for_process(dyld_process, nullptr); - auto shared_cache = dyld_process_snapshot_get_shared_cache(snapshot); - assert(dyld_process && snapshot && shared_cache); + if (!snapshot) + return false; + + auto on_exit = llvm::make_scope_exit( + [&]() { dyld_process_snapshot_dispose(snapshot); }); + + dyld_shared_cache_t shared_cache = + dyld_process_snapshot_get_shared_cache(snapshot); + if (!shared_cache) + return false; dyld_shared_cache_for_each_image(shared_cache, ^(dyld_image_t image) { __block uint64_t minVmAddr = UINT64_MAX; @@ -555,11 +569,16 @@ SharedCacheInfo::SharedCacheInfo() { std::make_shared((uint8_t *)minVmAddr, maxVmAddr - minVmAddr)}; }); - dyld_process_snapshot_dispose(snapshot); - return; + return true; } } #endif + return false; +} + +SharedCacheInfo::SharedCacheInfo() { + if (CreateSharedCacheInfoWithInstrospectionSPIs()) + return; size_t shared_cache_size; uint8_t *shared_cache_start = -- 2.7.4