Skip to content
Closed
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
9 changes: 3 additions & 6 deletions view/sharedcache/api/sharedcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,8 @@ namespace SharedCacheAPI {
BNFreeString(outputStr);
if (output.empty())
return {};
SharedCacheMachOHeader header;
header.LoadFromString(output);
return header;

return SharedCacheMachOHeader::LoadFromString(output);
}

std::optional<SharedCacheMachOHeader> SharedCache::GetMachOHeaderForAddress(uint64_t address)
Expand All @@ -218,9 +217,7 @@ namespace SharedCacheAPI {
BNFreeString(outputStr);
if (output.empty())
return {};
SharedCacheMachOHeader header;
header.LoadFromString(output);
return header;
return SharedCacheMachOHeader::LoadFromString(output);
}

BNDSCViewState SharedCache::GetState()
Expand Down
70 changes: 36 additions & 34 deletions view/sharedcache/api/sharedcacheapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,40 +211,42 @@ namespace SharedCacheAPI {
MSS(relocatable);
}

void Load(SharedCacheCore::DeserializationContext& context) {
MSL(textBase);
MSL(loadCommandOffset);
MSL_SUBCLASS(ident);
MSL(identifierPrefix);
MSL(installName);
MSL(entryPoints);
MSL(m_entryPoints);
MSL_SUBCLASS(symtab);
MSL_SUBCLASS(dysymtab);
MSL_SUBCLASS(dyldInfo);
// MSL_SUBCLASS(routines64); // FIXME CRASH but also do we even use this?
MSL_SUBCLASS(functionStarts);
MSL_SUBCLASS(moduleInitSections);
MSL_SUBCLASS(exportTrie);
MSL_SUBCLASS(chainedFixups);
MSL(relocationBase);
MSL_SUBCLASS(segments);
MSL_SUBCLASS(linkeditSegment);
MSL_SUBCLASS(sections);
MSL(sectionNames);
MSL_SUBCLASS(symbolStubSections);
MSL_SUBCLASS(symbolPointerSections);
MSL(dylibs);
MSL_SUBCLASS(buildVersion);
MSL_SUBCLASS(buildToolVersions);
MSL(exportTriePath);
MSL(dysymPresent);
MSL(dyldInfoPresent);
MSL(exportTriePresent);
MSL(chainedFixupsPresent);
// MSL(routinesPresent);
MSL(functionStartsPresent);
MSL(relocatable);
static SharedCacheMachOHeader Load(SharedCacheCore::DeserializationContext& context) {
return SharedCacheMachOHeader {
.MSL(textBase),
.MSL(loadCommandOffset),
.MSL(ident),
.MSL(identifierPrefix),
.MSL(installName),
.MSL(entryPoints),
.MSL(m_entryPoints),
.MSL(symtab),
.MSL(dysymtab),
.MSL(dyldInfo),
// .MSL(routines64), // FIXME CRASH but also do we even use this?
.MSL(functionStarts),
.MSL(moduleInitSections),
.MSL(exportTrie),
.MSL(chainedFixups),
.MSL(relocationBase),
.MSL(segments),
.MSL(linkeditSegment),
.MSL(sections),
.MSL(sectionNames),
.MSL(symbolStubSections),
.MSL(symbolPointerSections),
.MSL(dylibs),
.MSL(buildVersion),
.MSL(buildToolVersions),
.MSL(exportTriePath),
.MSL(dysymPresent),
.MSL(dyldInfoPresent),
.MSL(exportTriePresent),
.MSL(chainedFixupsPresent),
// .MSL(routinesPresent),
.MSL(functionStartsPresent),
.MSL(relocatable),
};
}
};

Expand Down
40 changes: 15 additions & 25 deletions view/sharedcache/core/MetadataSerializable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@
avoid that.
* */

#ifndef SHAREDCACHE_CORE_METADATASERIALIZABLE_HPP
#define SHAREDCACHE_CORE_METADATASERIALIZABLE_HPP

#include <cassert>
#include "binaryninjaapi.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#include "../api/sharedcachecore.h"
#include "view/macho/machoview.h"

#ifndef SHAREDCACHE_CORE_METADATASERIALIZABLE_HPP
#define SHAREDCACHE_CORE_METADATASERIALIZABLE_HPP

namespace SharedCacheCore {

#define MSS(name) context.store(#name, name)
#define MSS_CAST(name, type) context.store(#name, (type) name)
#define MSS_SUBCLASS(name) Serialize(context, #name, name)
#define MSL(name) name = context.load<decltype(name)>(#name)
#define MSL_CAST(name, storedType, type) name = (type)context.load<storedType>(#name)
#define MSL_SUBCLASS(name) Deserialize(context, #name, name)

using namespace BinaryNinja;

Expand Down Expand Up @@ -83,44 +83,34 @@ struct DeserializationContext {
}
};

template <typename Derived>
class MetadataSerializable
{
template <typename Derived, typename LoadResult = Derived>
class MetadataSerializable {
public:
std::string AsString() const
{
std::string AsString() const {
SerializationContext context;
Store(context);

return context.buffer.GetString();
}

void LoadFromString(const std::string& s)
{
static LoadResult LoadFromString(const std::string& s) {
DeserializationContext context;
context.doc.Parse(s.c_str());
AsDerived().Load(context);
rapidjson::ParseResult result = context.doc.Parse(s.c_str());
assert(result);
return Derived::Load(context);
}

void LoadFromValue(rapidjson::Value& s)
{
static LoadResult LoadFromValue(rapidjson::Value& s) {
DeserializationContext context;
context.doc.CopyFrom(s, context.doc.GetAllocator());
AsDerived().Load(context);
return Derived::Load(context);
}

Ref<Metadata> AsMetadata() {
Ref<Metadata> AsMetadata() const {
return new Metadata(AsString());
}

bool LoadFromMetadata(const Ref<Metadata>& meta)
{
if (!meta->IsString())
return false;
LoadFromString(meta->GetString());
return true;
}

template <typename... Args>
void Store(SerializationContext& context) const {
context.writer.StartObject();
AsDerived().Store(context);
Expand Down
Loading