|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-2023 Jon Palmisciano. All rights reserved. |
| 3 | + * |
| 4 | + * Use of this source code is governed by the BSD 3-Clause license; the full |
| 5 | + * terms of the license can be found in the LICENSE.txt file. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "GlobalState.h" |
| 9 | + |
| 10 | +#include <set> |
| 11 | +#include <shared_mutex> |
| 12 | +#include <unordered_map> |
| 13 | + |
| 14 | +static std::unordered_map<BinaryViewID, MessageHandler*> g_messageHandlers; |
| 15 | +static std::shared_mutex g_messageHandlerLock; |
| 16 | +static std::unordered_map<BinaryViewID, SharedAnalysisInfo> g_viewInfos; |
| 17 | +static std::shared_mutex g_viewInfoLock; |
| 18 | +static std::set<BinaryViewID> g_ignoredViews; |
| 19 | + |
| 20 | + |
| 21 | +MessageHandler* GlobalState::messageHandler(BinaryViewRef bv) |
| 22 | +{ |
| 23 | + std::unique_lock<std::shared_mutex> lock(g_messageHandlerLock); |
| 24 | + auto messageHandler = g_messageHandlers.find(id(bv)); |
| 25 | + if (messageHandler != g_messageHandlers.end()) |
| 26 | + return messageHandler->second; |
| 27 | + auto newMessageHandler = new MessageHandler(bv); |
| 28 | + g_messageHandlers[id(bv)] = newMessageHandler; |
| 29 | + return newMessageHandler; |
| 30 | +} |
| 31 | + |
| 32 | +BinaryViewID GlobalState::id(BinaryViewRef bv) |
| 33 | +{ |
| 34 | + return bv->GetFile()->GetSessionId(); |
| 35 | +} |
| 36 | + |
| 37 | +void GlobalState::addIgnoredView(BinaryViewRef bv) |
| 38 | +{ |
| 39 | + g_ignoredViews.insert(id(std::move(bv))); |
| 40 | +} |
| 41 | + |
| 42 | +bool GlobalState::viewIsIgnored(BinaryViewRef bv) |
| 43 | +{ |
| 44 | + return g_ignoredViews.count(id(std::move(bv))) > 0; |
| 45 | +} |
| 46 | + |
| 47 | +SharedAnalysisInfo GlobalState::analysisInfo(BinaryViewRef data) |
| 48 | +{ |
| 49 | + { |
| 50 | + std::shared_lock<std::shared_mutex> lock(g_viewInfoLock); |
| 51 | + if (auto it = g_viewInfos.find(id(data)); it != g_viewInfos.end()) |
| 52 | + if (data->GetStart() == it->second->imageBase) |
| 53 | + return it->second; |
| 54 | + } |
| 55 | + |
| 56 | + std::unique_lock<std::shared_mutex> lock(g_viewInfoLock); |
| 57 | + SharedAnalysisInfo info = std::make_shared<AnalysisInfo>(); |
| 58 | + info->imageBase = data->GetStart(); |
| 59 | + |
| 60 | + if (auto objcStubs = data->GetSectionByName("__objc_stubs")) |
| 61 | + { |
| 62 | + info->objcStubsStartEnd = {objcStubs->GetStart(), objcStubs->GetEnd()}; |
| 63 | + info->hasObjcStubs = true; |
| 64 | + } |
| 65 | + |
| 66 | + auto meta = data->QueryMetadata("Objective-C"); |
| 67 | + if (!meta) |
| 68 | + { |
| 69 | + g_viewInfos[id(data)] = info; |
| 70 | + return info; |
| 71 | + } |
| 72 | + |
| 73 | + auto metaKVS = meta->GetKeyValueStore(); |
| 74 | + if (metaKVS["version"]->GetUnsignedInteger() != 1) |
| 75 | + { |
| 76 | + BinaryNinja::LogError("workflow_objc: Invalid metadata version received!"); |
| 77 | + g_viewInfos[id(data)] = info; |
| 78 | + return info; |
| 79 | + } |
| 80 | + for (const auto& selAndImps : metaKVS["selRefImplementations"]->GetArray()) |
| 81 | + info->selRefToImp[selAndImps->GetArray()[0]->GetUnsignedInteger()] = selAndImps->GetArray()[1]->GetUnsignedIntegerList(); |
| 82 | + for (const auto& selAndImps : metaKVS["selImplementations"]->GetArray()) |
| 83 | + info->selToImp[selAndImps->GetArray()[0]->GetUnsignedInteger()] = selAndImps->GetArray()[1]->GetUnsignedIntegerList(); |
| 84 | + |
| 85 | + g_viewInfos[id(data)] = info; |
| 86 | + return info; |
| 87 | +} |
| 88 | + |
| 89 | +bool GlobalState::hasAnalysisInfo(BinaryViewRef data) |
| 90 | +{ |
| 91 | + return data->QueryMetadata("Objective-C") != nullptr; |
| 92 | +} |
| 93 | + |
| 94 | +bool GlobalState::hasFlag(BinaryViewRef bv, const std::string& flag) |
| 95 | +{ |
| 96 | + return bv->QueryMetadata(flag); |
| 97 | +} |
| 98 | + |
| 99 | +void GlobalState::setFlag(BinaryViewRef bv, const std::string& flag) |
| 100 | +{ |
| 101 | + bv->StoreMetadata(flag, new BinaryNinja::Metadata("YES")); |
| 102 | +} |
0 commit comments