Can someone give me a brief intro or point me to documentation that describes how lldb handles the dyld shared cache on macOS?
I’m trying to evaluate how to implement the same functionality in Valgrind. Prior to macOS 11 Big Sur Valgrind used DYLD_SHARED_REGION=avoid to force loading the libraries and then to bypass the cache and so to trigger reading the mach-o info to be able to redirect malloc/pthread functions. Without these redirs not much is working correctly.
lldb reads libraries in the system shared cache (present in process memory, not present on disk, often) out of the target process memory. Load commands, LINKEDIT section, all of it. This is slow, of course, so lldb has an optimization that checks its own process shared cache UUID and the target process shared cache UUID, and if they match, will read its OWN memory to load the binaries. There are some situations where the shared cache UUIDs won’t match, the most commonly seen is a simulator process (e.g. a watchOS app running on a Mac). e.g. see ObjectFileMachO::GetProcessSharedCacheUUID, ObjectFileMachO::GetLLDBSharedCacheUUID and added more newly, lldb/source/Host/macosx/objcxx/HostInfoMacOSX.mm HostInfoMacOSX::GetSharedCacheImageInfo which gets a list of binaries in the shared cache. This latter is how you can run lldb on a random a.out
binary and then do image list
and you’ll see lldb has also loaded all of the dependent libraries like /usr/lib/libSystem.B.dylib which don’t exist on disk, but they’re listed there as if they were.
Speed is only a secondary concern. Nobody hurries to use Valgrind because it is so fast. Not being able to detect any heap or thread errors is a bit of a problem though.
We can’t use the UUIID of the tool’s own memory. The Valgrind tools do not link with any external libraries to ensure a clean separation between the host and the guest. That is also going to be a big problem as, generally we have to either borrow code that is GPLv2 compatible or re-implement things ourselves.
I’ll try to see what I can understand from the source.