Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Add relative offset type & data renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpalmisc committed Jan 23, 2022
1 parent 4c14ccf commit f34a46f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 25 deletions.
10 changes: 7 additions & 3 deletions source/CustomTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct tptr_t {
uint64_t raw;
};
struct rptr_t {
int32_t raw;
};
typedef void* id;
typedef char* SEL;
Expand All @@ -46,9 +50,9 @@ struct CFString {
};
struct objc_small_method_t {
int32_t name;
int32_t types;
int32_t imp;
rptr_t name;
rptr_t types;
rptr_t imp;
};
struct objc_method_t {
Expand Down
1 change: 1 addition & 0 deletions source/CustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
namespace CustomTypes {

const std::string TaggedPointer = "tptr_t";
const std::string RelativePointer = "rptr_t";
const std::string ID = "id";
const std::string Selector = "SEL";
const std::string CFString = "CFString";
Expand Down
97 changes: 75 additions & 22 deletions source/DataRenderers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,39 @@

using namespace BinaryNinja;

bool TaggedPointerDataRenderer::IsValidForData(BinaryView* bv, uint64_t,
Type* type, std::vector<std::pair<Type*, size_t>>&)
/// Get the appropriate token type for a pointer to a given symbol.
BNInstructionTextTokenType tokenTypeForSymbol(Ref<Symbol> symbol)
{
auto taggedPointerType = bv->GetTypeByName(CustomTypes::TaggedPointer);
if (!taggedPointerType)
return false;
auto tokenType = CodeRelativeAddressToken;

return type->GetRegisteredName() == taggedPointerType->GetRegisteredName();
switch (symbol->GetType()) {
case DataSymbol:
tokenType = DataSymbolToken;
break;
case FunctionSymbol:
tokenType = CodeSymbolToken;
break;
default:
break;
}

return tokenType;
}

std::vector<DisassemblyTextLine> TaggedPointerDataRenderer::GetLinesForData(
BinaryView* bv, uint64_t addr, Type*,
const std::vector<InstructionTextToken>& prefix, size_t,
std::vector<std::pair<Type*, size_t>>&)
/// Get a line for a given pointer.
DisassemblyTextLine lineForPointer(BinaryView* bv, uint64_t pointer,
uint64_t addr, const std::vector<InstructionTextToken>& prefix)
{
BinaryReader reader(bv);
reader.Seek(addr);

auto pointer = (reader.Read64() & 0xFFFFFFFF) + bv->GetStart();
std::string tokenText = "???";
auto tokenType = CodeRelativeAddressToken;

std::string tokenText;
BNInstructionTextTokenType tokenType;

// If this tagged pointer points to a symbol, use its name as the token
// text, otherwise format the address it points to as a hex string.
Ref<Symbol> symbol = bv->GetSymbolByAddress(pointer);
if (pointer - bv->GetStart() == 0) {
if (pointer == 0 || pointer == bv->GetStart()) {
tokenText = "NULL";
tokenType = KeywordToken;
} else if (symbol) {
tokenText = symbol->GetFullName();
tokenType = DataSymbolToken;
tokenType = tokenTypeForSymbol(symbol);
} else {
char addressBuffer[32];
sprintf(addressBuffer, "0x%llx", pointer);
Expand All @@ -76,7 +77,6 @@ std::vector<DisassemblyTextLine> TaggedPointerDataRenderer::GetLinesForData(
tokenType = CodeRelativeAddressToken;
}

// Create a line using the prefix tokens passed in.
DisassemblyTextLine line;
line.addr = addr;
line.tokens = prefix;
Expand All @@ -85,7 +85,60 @@ std::vector<DisassemblyTextLine> TaggedPointerDataRenderer::GetLinesForData(
return { line };
}

// Tells if a type is a given type.
bool isType(BinaryView* bv, Type* type, const std::string& name)
{
auto targetType = bv->GetTypeByName(name);
if (!targetType)
return false;

return type->GetRegisteredName() == targetType->GetRegisteredName();
}

bool TaggedPointerDataRenderer::IsValidForData(BinaryView* bv, uint64_t,
Type* type, std::vector<std::pair<Type*, size_t>>&)
{
return isType(bv, type, CustomTypes::TaggedPointer);
}

std::vector<DisassemblyTextLine> TaggedPointerDataRenderer::GetLinesForData(
BinaryView* bv, uint64_t addr, Type*,
const std::vector<InstructionTextToken>& prefix, size_t,
std::vector<std::pair<Type*, size_t>>&)
{
BinaryReader reader(bv);
reader.Seek(addr);

auto pointer = (reader.Read64() & 0xFFFFFFFF) + bv->GetStart();

return { lineForPointer(bv, pointer, addr, prefix) };
}

void TaggedPointerDataRenderer::Register()
{
DataRendererContainer::RegisterTypeSpecificDataRenderer(new TaggedPointerDataRenderer());
}

bool RelativePointerDataRenderer::IsValidForData(BinaryView* bv, uint64_t,
Type* type, std::vector<std::pair<Type*, size_t>>&)
{
return isType(bv, type, CustomTypes::RelativePointer);
}

std::vector<DisassemblyTextLine> RelativePointerDataRenderer::GetLinesForData(
BinaryView* bv, uint64_t addr, Type*,
const std::vector<InstructionTextToken>& prefix, size_t,
std::vector<std::pair<Type*, size_t>>&)
{
BinaryReader reader(bv);
reader.Seek(addr);

auto pointer = (int32_t)reader.Read32() + addr;

return { lineForPointer(bv, pointer, addr, prefix) };
}

void RelativePointerDataRenderer::Register()
{
DataRendererContainer::RegisterTypeSpecificDataRenderer(new RelativePointerDataRenderer());
}
15 changes: 15 additions & 0 deletions source/DataRenderers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ class TaggedPointerDataRenderer : public BinaryNinja::DataRenderer {

static void Register();
};

class RelativePointerDataRenderer : public BinaryNinja::DataRenderer {
RelativePointerDataRenderer() = default;

public:
bool IsValidForData(BinaryViewPtr, uint64_t addr,
TypePtr, std::vector<std::pair<TypePtr, size_t>>& context) override;

std::vector<BinaryNinja::DisassemblyTextLine> GetLinesForData(
BinaryViewPtr, uint64_t addr, TypePtr,
const std::vector<BinaryNinja::InstructionTextToken>& prefix,
size_t width, std::vector<std::pair<TypePtr, size_t>>& context) override;

static void Register();
};
1 change: 1 addition & 0 deletions source/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ BN_DECLARE_CORE_ABI_VERSION
BINARYNINJAPLUGIN bool CorePluginInit()
{
TaggedPointerDataRenderer::Register();
RelativePointerDataRenderer::Register();

Workflow::registerActivities();
OneShot::registerCommands();
Expand Down

0 comments on commit f34a46f

Please sign in to comment.