diff options
Diffstat (limited to 'view/sharedcache/api/sharedcache.cpp')
| -rw-r--r-- | view/sharedcache/api/sharedcache.cpp | 485 |
1 files changed, 289 insertions, 196 deletions
diff --git a/view/sharedcache/api/sharedcache.cpp b/view/sharedcache/api/sharedcache.cpp index d9838057..6d552f0d 100644 --- a/view/sharedcache/api/sharedcache.cpp +++ b/view/sharedcache/api/sharedcache.cpp @@ -4,231 +4,324 @@ #include "sharedcacheapi.h" -namespace SharedCacheAPI { +using namespace BinaryNinja; +using namespace SharedCacheAPI; - SharedCache::SharedCache(Ref<BinaryView> view) { - m_object = BNGetSharedCache(view->GetObject()); - } +BNSharedCacheImage ImageToApi(CacheImage image) +{ + BNSharedCacheImage apiImage; + apiImage.name = BNAllocString(image.name.c_str()); + apiImage.headerAddress = image.headerAddress; + apiImage.regionStartCount = image.regionStarts.size(); + // TODO: If we alloc then core cannot delete + uint64_t *regionStarts = new uint64_t[image.regionStarts.size()]; + for (size_t i = 0; i < image.regionStarts.size(); i++) + regionStarts[i] = image.regionStarts[i]; + apiImage.regionStarts = regionStarts; + return apiImage; +} - BNDSCViewLoadProgress SharedCache::GetLoadProgress(Ref<BinaryView> view) - { - return BNDSCViewGetLoadProgress(view->GetFile()->GetSessionId()); - } +CacheImage ImageFromApi(BNSharedCacheImage image) +{ + CacheImage apiImage; + apiImage.name = image.name; + apiImage.headerAddress = image.headerAddress; + apiImage.regionStarts.reserve(image.regionStartCount); + for (size_t i = 0; i < image.regionStartCount; i++) + apiImage.regionStarts.push_back(image.regionStarts[i]); + return apiImage; +} - uint64_t SharedCache::FastGetBackingCacheCount(Ref<BinaryView> view) - { - return BNDSCViewFastGetBackingCacheCount(view->GetObject()); - } +BNSharedCacheRegion RegionToApi(const CacheRegion ®ion) +{ + BNSharedCacheRegion apiRegion; + apiRegion.vmAddress = region.start; + apiRegion.name = BNAllocString(region.name.c_str()); + apiRegion.size = region.size; + apiRegion.flags = region.flags; + apiRegion.regionType = region.type; + // If not associated with image this will be zeroed. + apiRegion.imageStart = region.imageStart.value_or(0); + return apiRegion; +} - bool SharedCache::LoadImageWithInstallName(std::string installName, bool skipObjC) - { - char* str = BNAllocString(installName.c_str()); - return BNDSCViewLoadImageWithInstallName(m_object, str, skipObjC); - } +CacheRegion RegionFromApi(BNSharedCacheRegion apiRegion) +{ + CacheRegion region; + region.start = apiRegion.vmAddress; + region.name = apiRegion.name; + region.size = apiRegion.size; + region.flags = apiRegion.flags; + region.type = apiRegion.regionType; + return region; +} - bool SharedCache::LoadSectionAtAddress(uint64_t addr) - { - return BNDSCViewLoadSectionAtAddress(m_object, addr); - } +BNSharedCacheMappingInfo MappingToApi(const CacheMappingInfo &mapping) +{ + BNSharedCacheMappingInfo apiMapping; + apiMapping.vmAddress = mapping.vmAddress; + apiMapping.size = mapping.size; + apiMapping.fileOffset = mapping.fileOffset; + return apiMapping; +} - bool SharedCache::LoadImageContainingAddress(uint64_t addr, bool skipObjC) - { - return BNDSCViewLoadImageContainingAddress(m_object, addr, skipObjC); - } +CacheMappingInfo MappingFromApi(BNSharedCacheMappingInfo apiMapping) +{ + CacheMappingInfo mapping; + mapping.vmAddress = apiMapping.vmAddress; + mapping.size = apiMapping.size; + mapping.fileOffset = apiMapping.fileOffset; + return mapping; +} - std::vector<std::string> SharedCache::GetAvailableImages() - { - size_t count; - char** value = BNDSCViewGetInstallNames(m_object, &count); - if (value == nullptr) - { - return {}; - } +BNSharedCacheEntry EntryToApi(const CacheEntry &entry) +{ + BNSharedCacheEntry apiEntry; + apiEntry.path = BNAllocString(entry.path.c_str()); + apiEntry.entryType = entry.entryType; + const auto &mappings = entry.mappings; + apiEntry.mappingCount = mappings.size(); + // TODO: If we alloc then the core cannot delete. + apiEntry.mappings = new BNSharedCacheMappingInfo[mappings.size()]; + for (size_t i = 0; i < mappings.size(); i++) + apiEntry.mappings[i] = MappingToApi(mappings[i]); + return apiEntry; +} - std::vector<std::string> result; - for (size_t i = 0; i < count; i++) - { - result.push_back(value[i]); - } +CacheEntry EntryFromApi(BNSharedCacheEntry apiEntry) +{ + CacheEntry entry; + entry.path = apiEntry.path; + entry.entryType = apiEntry.entryType; + entry.mappings.reserve(apiEntry.mappingCount); + for (size_t i = 0; i < apiEntry.mappingCount; i++) + entry.mappings.push_back(MappingFromApi(apiEntry.mappings[i])); + return entry; +} - BNFreeStringList(value, count); - return result; - } +CacheSymbol SymbolFromApi(BNSharedCacheSymbol apiSymbol) +{ + CacheSymbol symbol; + symbol.name = apiSymbol.name; + symbol.address = apiSymbol.address; + symbol.type = apiSymbol.symbolType; + return symbol; +} - void SharedCache::ProcessObjCSectionsForImageWithInstallName(std::string installName) +std::string SharedCacheAPI::GetRegionTypeAsString(const BNSharedCacheRegionType &type) +{ + switch (type) { - char* str = BNAllocString(installName.c_str()); - BNDSCViewProcessObjCSectionsForImageWithInstallName(m_object, str, true); + case SharedCacheRegionTypeImage: + return "Image"; + case SharedCacheRegionTypeStubIsland: + return "StubIsland"; + case SharedCacheRegionTypeDyldData: + return "DyldData"; + case SharedCacheRegionTypeNonImage: + return "NonImage"; + default: + return "Unknown"; } +} - void SharedCache::ProcessAllObjCSections() - { - BNDSCViewProcessAllObjCSections(m_object); - } +SharedCacheController::SharedCacheController(BNSharedCacheController *controller) +{ + m_object = controller; +} - std::vector<DSCMemoryRegion> SharedCache::GetLoadedMemoryRegions() - { - size_t count; - BNDSCMappedMemoryRegion* value = BNDSCViewGetLoadedRegions(m_object, &count); - if (value == nullptr) - { - return {}; - } +DSCRef<SharedCacheController> SharedCacheController::GetController(BinaryView &view) +{ + BNSharedCacheController *controller = BNGetSharedCacheController(view.GetObject()); + if (controller == nullptr) + return nullptr; + return new SharedCacheController(controller); +} - std::vector<DSCMemoryRegion> result; - for (size_t i = 0; i < count; i++) - { - DSCMemoryRegion region; - region.vmAddress = value[i].vmAddress; - region.size = value[i].size; - region.prettyName = value[i].name; - result.push_back(region); - } +bool SharedCacheController::ApplyRegion(BinaryView &view, const CacheRegion ®ion) +{ + auto apiRegion = RegionToApi(region); + bool result = BNSharedCacheControllerApplyRegion(m_object, view.GetObject(), &apiRegion); + BNSharedCacheFreeRegion(apiRegion); + return result; +} - BNDSCViewFreeLoadedRegions(value, count); - return result; - } - std::vector<BackingCache> SharedCache::GetBackingCaches() - { - size_t count; - BNDSCBackingCache* value = BNDSCViewGetBackingCaches(m_object, &count); - if (value == nullptr) - { - return {}; - } +bool SharedCacheController::ApplyImage(BinaryView &view, const CacheImage &image) +{ + auto apiImage = ImageToApi(image); + bool result = BNSharedCacheControllerApplyImage(m_object, view.GetObject(), &apiImage); + BNSharedCacheFreeImage(apiImage); + return result; +} - std::vector<BackingCache> result; - for (size_t i = 0; i < count; i++) - { - BackingCache cache; - cache.path = value[i].path; - cache.cacheType = value[i].cacheType; - for (size_t j = 0; j < value[i].mappingCount; j++) - { - BackingCacheMapping mapping; - mapping.vmAddress = value[i].mappings[j].vmAddress; - mapping.size = value[i].mappings[j].size; - mapping.fileOffset = value[i].mappings[j].fileOffset; - cache.mappings.push_back(mapping); - } - result.push_back(cache); - } +bool SharedCacheController::IsRegionLoaded(const CacheRegion ®ion) const +{ + auto apiRegion = RegionToApi(region); + bool result = BNSharedCacheControllerIsRegionLoaded(m_object, &apiRegion); + BNSharedCacheFreeRegion(apiRegion); + return result; +} - BNDSCViewFreeBackingCaches(value, count); - return result; - } +bool SharedCacheController::IsImageLoaded(const CacheImage &image) const +{ + auto apiImage = ImageToApi(image); + bool result = BNSharedCacheControllerIsImageLoaded(m_object, &apiImage); + BNSharedCacheFreeImage(apiImage); + return result; +} - std::vector<DSCImage> SharedCache::GetImages() - { - size_t count; - BNDSCImage* value = BNDSCViewGetAllImages(m_object, &count); - if (value == nullptr) - { - return {}; - } +std::optional<CacheRegion> SharedCacheController::GetRegionAt(uint64_t address) const +{ + BNSharedCacheRegion apiRegion; + if (!BNSharedCacheControllerGetRegionAt(m_object, address, &apiRegion)) + return std::nullopt; + CacheRegion region = RegionFromApi(apiRegion); + BNSharedCacheFreeRegion(apiRegion); + return region; +} - std::vector<DSCImage> result; - for (size_t i = 0; i < count; i++) - { - DSCImage img; - img.name = value[i].name; - img.headerAddress = value[i].headerAddress; - for (size_t j = 0; j < value[i].mappingCount; j++) - { - DSCImageMemoryMapping mapping; - mapping.filePath = value[i].mappings[j].filePath; - mapping.name = value[i].mappings[j].name; - mapping.vmAddress = value[i].mappings[j].vmAddress; - mapping.rawViewOffset = value[i].mappings[j].rawViewOffset; - mapping.size = value[i].mappings[j].size; - mapping.loaded = value[i].mappings[j].loaded; - img.mappings.push_back(mapping); - } - result.push_back(img); - } +std::optional<CacheRegion> SharedCacheController::GetRegionContaining(uint64_t address) const +{ + BNSharedCacheRegion apiRegion; + if (!BNSharedCacheControllerGetRegionContaining(m_object, address, &apiRegion)) + return std::nullopt; + CacheRegion region = RegionFromApi(apiRegion); + BNSharedCacheFreeRegion(apiRegion); + return region; +} - BNDSCViewFreeAllImages(value, count); - return result; - } +std::optional<CacheImage> SharedCacheController::GetImageAt(uint64_t address) const +{ + BNSharedCacheImage apiImage; + if (!BNSharedCacheControllerGetImageAt(m_object, address, &apiImage)) + return std::nullopt; + CacheImage image = ImageFromApi(apiImage); + BNSharedCacheFreeImage(apiImage); + return image; +} - std::vector<DSCSymbol> SharedCache::LoadAllSymbolsAndWait() - { - size_t count; - BNDSCSymbolRep* value = BNDSCViewLoadAllSymbolsAndWait(m_object, &count); - if (value == nullptr) - { - return {}; - } +std::optional<CacheImage> SharedCacheController::GetImageContaining(uint64_t address) const +{ + BNSharedCacheImage apiImage; + if (!BNSharedCacheControllerGetImageContaining(m_object, address, &apiImage)) + return std::nullopt; + CacheImage image = ImageFromApi(apiImage); + BNSharedCacheFreeImage(apiImage); + return image; +} - std::vector<DSCSymbol> result; - result.reserve(count); - for (size_t i = 0; i < count; i++) - { - DSCSymbol sym; - sym.address = value[i].address; - sym.name = StringRef(BNDuplicateStringRef(value[i].name)); - sym.image = value[i].image; - result.push_back(sym); - } +std::optional<CacheImage> SharedCacheController::GetImageWithName(const std::string &name) const +{ + BNSharedCacheImage apiImage; + if (!BNSharedCacheControllerGetImageWithName(m_object, name.c_str(), &apiImage)) + return std::nullopt; + CacheImage image = ImageFromApi(apiImage); + BNSharedCacheFreeImage(apiImage); + return image; +} - BNDSCViewFreeSymbols(value, count); - return result; - } +std::vector<std::string> SharedCacheController::GetImageDependencies(const CacheImage &image) const +{ + size_t count; + BNSharedCacheImage apiImage = ImageToApi(image); + char **dependencies = BNSharedCacheControllerGetImageDependencies(m_object, &apiImage, &count); + BNSharedCacheFreeImage(apiImage); + std::vector<std::string> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.emplace_back(dependencies[i]); + BNFreeStringList(dependencies, count); + return result; +} - std::string SharedCache::GetNameForAddress(uint64_t address) - { - char* name = BNDSCViewGetNameForAddress(m_object, address); - if (name == nullptr) - return {}; - std::string result = name; - BNFreeString(name); - return result; - } +std::optional<CacheSymbol> SharedCacheController::GetSymbolAt(uint64_t address) const +{ + BNSharedCacheSymbol apiSymbol; + if (!BNSharedCacheControllerGetSymbolAt(m_object, address, &apiSymbol)) + return std::nullopt; + CacheSymbol symbol = SymbolFromApi(apiSymbol); + BNSharedCacheFreeSymbol(apiSymbol); + return symbol; +} - std::string SharedCache::GetImageNameForAddress(uint64_t address) - { - char* name = BNDSCViewGetImageNameForAddress(m_object, address); - if (name == nullptr) - return {}; - std::string result = name; - BNFreeString(name); - return result; - } +std::optional<CacheSymbol> SharedCacheController::GetSymbolWithName(const std::string &name) const +{ + BNSharedCacheSymbol apiSymbol; + if (!BNSharedCacheControllerGetSymbolWithName(m_object, name.c_str(), &apiSymbol)) + return std::nullopt; + CacheSymbol symbol = SymbolFromApi(apiSymbol); + BNSharedCacheFreeSymbol(apiSymbol); + return symbol; +} - std::optional<SharedCacheMachOHeader> SharedCache::GetMachOHeaderForImage(std::string name) - { - char* str = BNAllocString(name.c_str()); - char* outputStr = BNDSCViewGetImageHeaderForName(m_object, str); - if (outputStr == nullptr) - return {}; - std::string output = outputStr; - BNFreeString(outputStr); - if (output.empty()) - return {}; +std::vector<CacheEntry> SharedCacheController::GetEntries() const +{ + size_t count; + BNSharedCacheEntry *entries = BNSharedCacheControllerGetEntries(m_object, &count); + std::vector<CacheEntry> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.emplace_back(EntryFromApi(entries[i])); + BNSharedCacheFreeEntryList(entries, count); + return result; +} - return SharedCacheMachOHeader::LoadFromString(output); - } +std::vector<CacheRegion> SharedCacheController::GetLoadedRegions() const +{ + size_t count; + BNSharedCacheRegion *regions = BNSharedCacheControllerGetLoadedRegions(m_object, &count); + std::vector<CacheRegion> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.emplace_back(RegionFromApi(regions[i])); + BNSharedCacheFreeRegionList(regions, count); + return result; +} - std::optional<SharedCacheMachOHeader> SharedCache::GetMachOHeaderForAddress(uint64_t address) - { - char* outputStr = BNDSCViewGetImageHeaderForAddress(m_object, address); - if (outputStr == nullptr) - return {}; - std::string output = outputStr; - BNFreeString(outputStr); - if (output.empty()) - return {}; - return SharedCacheMachOHeader::LoadFromString(output); - } +std::vector<CacheRegion> SharedCacheController::GetRegions() const +{ + size_t count; + BNSharedCacheRegion *regions = BNSharedCacheControllerGetRegions(m_object, &count); + std::vector<CacheRegion> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.emplace_back(RegionFromApi(regions[i])); + BNSharedCacheFreeRegionList(regions, count); + return result; +} - BNDSCViewState SharedCache::GetState() - { - return BNDSCViewGetState(m_object); - } +std::vector<CacheImage> SharedCacheController::GetImages() const +{ + size_t count; + BNSharedCacheImage *images = BNSharedCacheControllerGetImages(m_object, &count); + std::vector<CacheImage> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.emplace_back(ImageFromApi(images[i])); + BNSharedCacheFreeImageList(images, count); + return result; +} - void SharedCache::FindSymbolAtAddrAndApplyToAddr(uint64_t symbolLocation, uint64_t targetLocation, bool triggerReanalysis) const - { - BNDSCFindSymbolAtAddressAndApplyToAddress(m_object, symbolLocation, targetLocation, triggerReanalysis); - } +std::vector<CacheImage> SharedCacheController::GetLoadedImages() const +{ + size_t count; + BNSharedCacheImage *images = BNSharedCacheControllerGetLoadedImages(m_object, &count); + std::vector<CacheImage> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.emplace_back(ImageFromApi(images[i])); + BNSharedCacheFreeImageList(images, count); + return result; +} -} // namespace SharedCacheAPI +std::vector<CacheSymbol> SharedCacheController::GetSymbols() const +{ + size_t count; + BNSharedCacheSymbol *symbols = BNSharedCacheControllerGetSymbols(m_object, &count); + std::vector<CacheSymbol> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.emplace_back(SymbolFromApi(symbols[i])); + BNSharedCacheFreeSymbolList(symbols, count); + return result; +} |
