summaryrefslogtreecommitdiff
path: root/view/sharedcache/core/SharedCache.cpp
AgeCommit message (Collapse)Author
2025-01-15[SharedCache] Avoid copying strings on each call to VM::MappingAtAddressMark Rowe
`PageMapping` was storing the path to the file it points within. This was causing unnecessary work within `VM::MappingAtAddress` as the `PageMapping`, and thus the path, is copied into the return value. This copying was more expensive than the map lookup. The file path is now stored within a `LazyMappedFileAccessor` class that wraps the `SelfAllocatingWeakPtr`. This means the path is still available via the `PageMapping`, but it does not need to be copied as often. This includes two additional improvements / optimizations while I was touching the code in question: 1. `MMappedFileAccessor::Open` no longer performs two hash lookups when a file accessor already exists. 2. `VM::MapPages` takes the path by const reference to avoid an unnecessary copy.
2025-01-10[SharedCache] Avoid crashing the product whenever bugs occur in ser/deser, ↵kat
fix compilation issue on linux
2025-01-10[SharedCache] Fix handling of relative selectors in macOS shared cachesMark Rowe
Find the relative selector base address in the Objective-C optimization data pointed to by the shared cache header, rather than via `__objc_scoffs`. This is only present on iOS, and not for every iOS version that encodes selectors via direct offsets. This also includes some related improvements: 1. Direct selectors get their own pointer type so they're rendered correctly in the view. 2. Method lists encoded as lists of lists are now handled. 3. The `dyld_cache_header` type added to the view is truncated to the length in the loaded cache. This ensures it is applied to the view. 4. A couple of methods that process method IMPs and selectors are updated to check whether the address is valid before attempting to process them. They would otherwise fail by throwing an exception if they proceed, but checking for validity is quicker and makes exception breakpoints usable.
2025-01-10[SharedCache] Track whether non-image regions are data vs codeMark Rowe
`BackingCache` now tracks the `dyld_cache_mapping_info` for its mappings so it has access to the memory protections for the region. This means it can avoid marking some regions as containing code when they don't, reducing the amount of analysis work that has to be done. Using `dyld_cache_mapping_info` also makes references to mappings easier to understand due to its named fields vs the nested `std::pair`s that were previously in use.
2025-01-10[SharedCache] Cache type libraries in the view-specific stateMark Rowe
They're surprisingly expensive to look up.
2025-01-10[SharedCache] Split view-specific state into a separate structMark Rowe
The existing view-specific state was stored in several global unordered maps. Many of these were accessed without locking, including `viewSpecificMutexes`, which is racy in the face of multiple threads. View-specific state is stored in a new heap-allocated `ViewSpecificState` struct that is reference counted via `std::shared_ptr`. A static map holds a `std::weak_ptr` to each view-specific state, keyed by session id. `SharedCache` retrieves its view-specific state during its constructor. Since `ViewSpecificState` is reference counted it will naturally be deallocated when the last `SharedCache` instance that references it goes away. Its corresponding entry will remain in the static map, though since it only holds a `std::weak_ptr` rather than any state it will not use much memory. The next time view-specific state is retrieved any expired entries will be removed from the map.
2025-01-08[SharedCache] Fix uninitialized `loaded` field for mappings returned by ↵WeiN76LQh
`BNDSCViewGetAllImages`
2024-12-26[SharedCache] Add the ability to manually trigger Objective-C processingWeiN76LQh
A problem with processing Objective-C sections at the time a library is loaded is that some of the references within those sections may refer to unload sections. This results in things like selectors not being correctly typed and named. This commit provides a way for users to manually trigger Objective-C parsing against sections for a specific library or all libraries, via the API. Combined with the previous commit a user can use the API to batch load a number of libraries and skip Objective-C processing for each one and then run it across the entire of the DSC once they are all loaded. Further improvement would be to provide a way to trigger Objective-C processing through the UI.
2024-12-26[SharedCache] Add the ability to skip Objective-C processing when loading a ↵WeiN76LQh
library This is useful when batch loading libraries to avoid extra processing (once the next commit has landed).
2024-12-25[SharedCache] Remove unnecessary lock in `SharedCache` destructorWeiN76LQh
From what I can tell the lock being taken in `SharedCache::~SharedCache` was purely for the decrement of `sharedCacheReferences`, however its an atomic so a lock isn't necessary. The lock being taken is extremely contentious and therefore often slow to be acquired. This resulted in a surprising amount of execution time spent in the `SharedCache` destructor. Nothing hugely significant but a quick and easy win to remove this single line of code.
2024-12-23[SharedCache] Serialize `SharedCache::m_symbolInfos`WeiN76LQh
`SharedCache::m_symbolInfos` isn't being serialized but there is an attempt to deserialize it. This commit adds in the code to serialize it. I slightly modified the format because I didn't really understand how it was expected to be serialized based on the deserialization code. The deserialization code looked wrong to me but its likely a misunderstanding on my part. I kept it similar to how `m_exportInfos` is serialized.
2024-12-10[SharedCache] Fix slide info parsing and complete v2 supportWeiN76LQh
There seem to be a number of issues with slide info parsing. I decided the simplest fix was to largely mimick what `dyld` is doing. Part of this process was to remove creating a vector of page starts and processing them in another loop below the one where they were read out. It wasn't clear to me the original design decision to separate it into 2 loops like that. I think this was part of the problem that was causing issues. By adding rewrites in the same loop where page starts are being read out, it was much easier to mimick the code in `dyld` which I assume has to be correct. So as long as my copying was correct then I believe this should work as intended. Not everything has been thoroughly tested but I'm pretty confident v3 and v5 are now working as intended. v2 should be but less testing of it has been done.
2024-12-10Fix a compilation error on Windowskat
2024-12-10[SharedCache] Use basic copy-on-write for viewStateCacheMark Rowe
Copying the state from the cache into a new `SharedCache` object is done with a global lock held and is so expensive that it results in much of the shared cache analysis running on a single thread, with others blocked waiting to acquire the lock. The cache now holds a `std::shared_ptr` to the state. New `SharedCache` objects take a reference to the cached state and only create their own copy of it the first time they perform an operation that would mutate it. The cached copy is never mutated, only replaced, so there is no danger of modifying the state out from under a `SharedCache` object. Since the copy happens at first mutation, it is performed without any global locks held. This avoids blocking other threads. This cuts the initial load time of a macOS shared cache from 3 minutes to 70 seconds, and cuts the time taken to load and analyze AppKit from multiple hours to around 14 minutes.
2024-12-10[SharedCache] Preserve flags when splitting a memory regionMark Rowe
The flags field was being left uninitialized which could result in the region being mishandled during later analysis.
2024-12-10[SharedCache] Switch to SAX-based writer API for JSON serializationAuthor: Mark Rowe
2024-12-10[SharedCache] Rework metadata serialization to reduce memory overheadMark Rowe
api/MetadataSerializable.hpp is removed in favor of including core/MetadataSerializable.hpp. Both headers defined types with the same name leading to One Definition Rule violations and surprising behavior. The serialization and deserialization context are now created on-demand during serialization rather than being a member of `MetadataSerializable`. This reduces the size of every serializable object by ~220 bytes. The context is passed explicitly as an argument to `Serialize` / `Deserialize`. As a result, `Serialize` / `Deserialize` can now be free functions rather than member functions. Since `MetadataSerializable` is not used for dynamic dispatch, the virtual methods are removed and the class is updated to be a class template using CRTP. This allows delegating to the derived class's `Load` and `Store` methods without the additional size overhead of the vtable pointer in every serializable object. These changes reduce the memory footprint of Binary Ninja after loading the macOS shared cache and loading a single dylib from it from 8.3GB to 4.6GB.
2024-11-13Don't leak DataBuffersMark Rowe
`MMappedFileAccessor::ReadBuffer` was returning a heap-allocated `DataBuffer`, but no callers were ever deleting it. There does not appear to be any reason to heap allocate the `DataBuffer` as the type is effectively a smart pointer wrapper around `BNDataBuffer`. Switch to returning it by value instead. Additionally, `MMappedFileAccessor::ReadBuffer` was allocating a buffer, copying data into it, and then handing that allocation to the `DataBuffer` constructor. The constructor copies data into a new allocation it owns so this allocation is unnecessary and was being leaked.
2024-11-06normalize Shared Cache nameJordan Wiens
2024-11-06[SharedCache] Fix bndb save/load within projectskat
2024-11-05[SharedCache] very Hotfix for 'SharedCache.load_image_with_install_name'kat
2024-11-05[SharedCache] Hotfix flags on dyldData sectionskat
2024-11-05[SharedCache] Changes to the alpha popup/info tabkat
2024-11-05[SharedCache] Add load option to disable automatic loading of libsystem_ckat
2024-11-05[SharedCache] Fix for section names in caches opened within a projectkat
2024-11-05[SharedCache] Add project support for split dscJosh Ferrell
2024-11-05[SharedCache] Implement LoadedImage API, Fix serialized image names, more ↵kat
robust system for out-of-date databases
2024-11-05[SharedCache] Fix a crash whenever user uses DSC apis on non-dsc.kat
2024-10-28[SharedCache] Warnings Cleanupkat
2024-10-23Initial commit of the alpha dyld_shared_cache view API Plugin.kat
This is an early release of our DSC processing plugin. We're still hard at work improving this feature. You should be able to just drop in a dyld_shared_cache and use the 'Shared Cache Triage' view to load and analyze images.