Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions view/sharedcache/api/sharedcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ namespace SharedCacheAPI {
}

std::vector<DSCSymbol> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
{
DSCSymbol sym;
Expand Down
72 changes: 7 additions & 65 deletions view/sharedcache/core/DSCView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,79 +603,21 @@ bool DSCView::Init()
Ref<Type> filesetEntryCommandType = Type::StructureType(filesetEntryCommandStruct);
DefineType(filesetEntryCommandTypeId, filesetEntryCommandName, filesetEntryCommandType);

std::vector<SharedCacheCore::MemoryRegion> regionsMappedIntoMemory;
if (auto meta = GetParentView()->QueryMetadata(SharedCacheCore::SharedCacheMetadataTag))
if (auto metadata = SharedCacheCore::SharedCacheMetadata::LoadFromView(GetParentView()))
{
std::string data = GetParentView()->GetStringMetadata(SharedCacheCore::SharedCacheMetadataTag);
std::stringstream ss;
ss.str(data);
rapidjson::Document result(rapidjson::kObjectType);

result.Parse(data.c_str());

if (result.HasMember("metadataVersion"))
{
if (result["metadataVersion"].GetInt() != METADATA_VERSION)
{
LogError("Shared cache metadata version mismatch: expected %d, got %d", METADATA_VERSION,
result["metadataVersion"].GetInt());
return false;
}
}
else
{
LogError("Shared cache metadata version not found");
return false;
}
for (auto& imgV : result["regionsMappedIntoMemory"].GetArray())
{
SharedCacheCore::MemoryRegion region;
region.LoadFromValue(imgV);
regionsMappedIntoMemory.push_back(region);
}

std::unordered_map<uint64_t, std::string> imageStartToInstallName;
// key "m_imageStarts"
for (auto& imgV : result["m_imageStarts"].GetArray())
{
std::string name = imgV.GetArray()[0].GetString();
uint64_t addr = imgV.GetArray()[1].GetUint64();
imageStartToInstallName[addr] = name;
}

std::vector<std::pair<uint64_t, std::vector<std::pair<uint64_t, std::pair<BNSymbolType, std::string>>>>> exportInfos;

for (const auto& obj1 : result["exportInfos"].GetArray())
{
std::vector<std::pair<uint64_t, std::pair<BNSymbolType, std::string>>> innerVec;
for (const auto& obj2 : obj1["value"].GetArray())
{
std::pair<BNSymbolType, std::string> innerPair = { (BNSymbolType)obj2["val1"].GetUint64(), obj2["val2"].GetString() };
innerVec.push_back({ obj2["key"].GetUint64(), innerPair });
}

exportInfos.push_back({obj1["key"].GetUint64(), innerVec});
}

BeginBulkModifySymbols();
for (const auto & [imageBaseAddr, exportList] : exportInfos)
for (const auto& [imageBaseAddr, exportMap] : metadata->ExportInfos())
{
std::vector<Ref<Symbol>> symbolsList;
for (const auto & [exportAddr, exportTypeAndName] : exportList)
{
symbolsList.push_back(new Symbol(exportTypeAndName.first, exportTypeAndName.second, exportAddr));
}

auto typelib = GetTypeLibrary(imageStartToInstallName[imageBaseAddr]);
auto typelib = GetTypeLibrary(metadata->InstallNameForImageBaseAddress(imageBaseAddr));

for (const auto& symbol : symbolsList)
for (const auto& [address, symbol] : *exportMap)
{
if (!IsValidOffset(symbol->GetAddress()))
if (!IsValidOffset(address))
continue;

if (typelib)
{
auto type = typelib->GetNamedObject(symbol->GetRawName());
if (type)
if (auto type = typelib->GetNamedObject(symbol->GetFullName()))
{
DefineAutoSymbolAndVariableOrFunction(GetDefaultPlatform(), symbol, type);
continue;
Expand Down
9 changes: 7 additions & 2 deletions view/sharedcache/core/MetadataSerializable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ void Serialize(SerializationContext& context, uint64_t value) {
context.writer.Uint64(value);
}

void Serialize(SerializationContext& context, unsigned long value)
{
context.writer.Uint64(value);
}

void Deserialize(DeserializationContext& context, std::string_view name, bool& b) {
b = context.doc[name.data()].GetBool();
}
Expand Down Expand Up @@ -360,8 +365,8 @@ void Deserialize(DeserializationContext& context, std::string_view name, routine
return;
b.cmd = bArr[0].GetUint();
b.cmdsize = bArr[1].GetUint();
b.init_address = bArr[2].GetUint();
b.init_module = bArr[3].GetUint();
b.init_address = bArr[2].GetUint64();
b.init_module = bArr[3].GetUint64();
}

void Serialize(SerializationContext& context, const function_starts_command& value)
Expand Down
28 changes: 20 additions & 8 deletions view/sharedcache/core/MetadataSerializable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ struct DeserializationContext {
template <typename Derived, typename LoadResult = Derived>
class MetadataSerializable {
public:
std::string AsString() const {
template <typename... Args>
std::string AsString(Args&&... args) const {
SerializationContext context;
Store(context);
Store(context, std::forward<Args>(args)...);

return context.buffer.GetString();
}
Expand All @@ -106,14 +107,15 @@ class MetadataSerializable {
return Derived::Load(context);
}

Ref<Metadata> AsMetadata() const {
return new Metadata(AsString());
template <typename... Args>
Ref<Metadata> AsMetadata(Args&&... args) const {
return new Metadata(AsString(std::forward<Args>(args)...));
}

template <typename... Args>
void Store(SerializationContext& context) const {
void Store(SerializationContext& context, Args&&... args) const {
context.writer.StartObject();
AsDerived().Store(context);
AsDerived().Store(context, std::forward<Args>(args)...);
context.writer.EndObject();
}

Expand Down Expand Up @@ -148,8 +150,8 @@ void Serialize(SerializationContext& context, const std::pair<First, Second>& va
context.writer.EndArray();
}

template <typename K, typename V>
void Serialize(SerializationContext& context, const std::map<K, V>& value)
template <typename K, typename V, typename L>
void Serialize(SerializationContext& context, const std::map<K, V, L>& value)
{
context.writer.StartArray();
for (auto& pair : value)
Expand Down Expand Up @@ -181,6 +183,15 @@ void Serialize(SerializationContext& context, const std::vector<T>& values)
context.writer.EndArray();
}

template <typename T>
void Serialize(SerializationContext& context, const std::optional<T>& value)
{
if (value.has_value())
Serialize(context, *value);
else
context.writer.Null();
}

SHAREDCACHE_FFI_API void Serialize(SerializationContext& context, const char*);
SHAREDCACHE_FFI_API void Serialize(SerializationContext& context, bool b);
SHAREDCACHE_FFI_API void Deserialize(DeserializationContext& context, std::string_view name, bool& b);
Expand All @@ -200,6 +211,7 @@ SHAREDCACHE_FFI_API void Serialize(SerializationContext& context, int32_t b);
SHAREDCACHE_FFI_API void Deserialize(DeserializationContext& context, std::string_view name, int32_t& b);
SHAREDCACHE_FFI_API void Serialize(SerializationContext& context, int64_t b);
SHAREDCACHE_FFI_API void Deserialize(DeserializationContext& context, std::string_view name, int64_t& b);
SHAREDCACHE_FFI_API void Serialize(SerializationContext& context, unsigned long b);
SHAREDCACHE_FFI_API void Serialize(SerializationContext& context, std::string_view b);
SHAREDCACHE_FFI_API void Serialize(SerializationContext& context, const std::pair<uint64_t, std::pair<uint64_t, uint64_t>>& value);
SHAREDCACHE_FFI_API void Deserialize(DeserializationContext& context, std::string_view name, std::string& b);
Expand Down
Loading