From a72b98ed5a357bb380d81f07f3f36946aa729bb2 Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Mon, 23 Feb 2026 23:26:27 -0800 Subject: [DSC] Simplify file mapping to fix intermittent unrelocated pointers The `FileAccessorCache`'s LRU eviction could discard a file's mapped data after slide info had already been applied to it. Future accesses to the file produced a fresh mapping, but failed to reapply the slide info. This could result in pointers not correctly being slid, such as in https://github.com/Vector35/binaryninja-api/issues/7689. The LRU cache existed to stay within OS file descriptor limits, since the old `MappedFile` held its fd open for the lifetime of the mapping. There's no real reason for it to hold the file descriptor open like this. Closing it after `mmap` is sufficient to avoid the file descriptor limits. `MappedFileRegion` replaces the combination of `FileAccessorCache`, `WeakFileAccessor`, and `MappedFileAccessor`. It closes the fd immediately after mmap, so all files can stay mapped without consuming descriptors, making the cache unnecessary. `MappedFileRegion` is owned directly by the `CacheEntry` for its full lifetime. Slide info is applied exactly once to each `MappedFileRegion`. --- view/sharedcache/core/SharedCache.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'view/sharedcache/core/SharedCache.h') diff --git a/view/sharedcache/core/SharedCache.h b/view/sharedcache/core/SharedCache.h index 010dff10..8ecd8f1d 100644 --- a/view/sharedcache/core/SharedCache.h +++ b/view/sharedcache/core/SharedCache.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -127,10 +128,12 @@ class CacheEntry // Mapping of image path to image info, used within ProcessImagesAndRegions to add them to the cache. // Also used to retrieve the image dependencies. std::vector> m_images {}; + std::shared_ptr m_file; public: CacheEntry(std::string filePath, std::string fileName, CacheEntryType type, dyld_cache_header header, - std::vector&& mappings, std::vector>&& images); + std::vector mappings, std::vector> images, + std::shared_ptr file); CacheEntry() = default; CacheEntry(const CacheEntry&) = default; @@ -141,7 +144,7 @@ public: // Construct a cache entry from the file on disk. static CacheEntry FromFile(const std::string& filePath, const std::string& fileName, CacheEntryType type); - WeakFileAccessor GetAccessor() const; + const std::shared_ptr& GetFile() const { return m_file; } // Get the headers virtual address. // This is useful if you need to read relative to the start of the entry file. -- cgit v1.3.1