Skip to content
Open
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
17 changes: 1 addition & 16 deletions view/macho/machoview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1103,22 +1103,7 @@ bool MachoView::IsValidFunctionStart(uint64_t addr)
{
uint8_t opcode[BN_MAX_INSTRUCTION_LENGTH];
size_t opLen = Read(opcode, addr, m_arch->GetMaxInstructionLength());
if (!opLen)
return false;

Ref<LowLevelILFunction> ilFunc = new LowLevelILFunction(m_arch, nullptr);
ilFunc->SetCurrentAddress(m_arch, addr);
m_arch->GetInstructionLowLevelIL(opcode, addr, opLen, *ilFunc);
for (size_t i = 0; i < ilFunc->GetInstructionCount(); i++)
{
const auto& instr = ilFunc->GetInstruction(i);
if (instr.operation == LLIL_UNDEF)
return false;
if (i == 0 && instr.operation == LLIL_TRAP)
return false;
}

return true;
return ::IsValidFunctionStart(m_arch, addr, opcode, opLen);
}


Expand Down
24 changes: 24 additions & 0 deletions view/macho/machoview.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string.h>

#include "binaryninjaapi.h"
#include "lowlevelilinstruction.h"
#include "objc.h"

//These are laready defined in one of the osx headers we want to override
Expand Down Expand Up @@ -1546,3 +1547,26 @@ namespace BinaryNinja

void InitMachoViewType();
}

// Check whether the instruction at `addr` looks like a valid function entry point.
// Returns false for undefined or trap instructions that typically indicate jump table entries.
// `opcode` must contain at least `opLen` bytes read from `addr`.
inline bool IsValidFunctionStart(BinaryNinja::Architecture* arch, uint64_t addr, const uint8_t* opcode, size_t opLen)
{
if (!opLen)
return false;

BinaryNinja::Ref<BinaryNinja::LowLevelILFunction> ilFunc = new BinaryNinja::LowLevelILFunction(arch, nullptr);
ilFunc->SetCurrentAddress(arch, addr);
arch->GetInstructionLowLevelIL(opcode, addr, opLen, *ilFunc);
for (size_t i = 0; i < ilFunc->GetInstructionCount(); i++)
{
const auto& instr = ilFunc->GetInstruction(i);
if (instr.operation == LLIL_UNDEF)
return false;
if (i == 0 && instr.operation == LLIL_TRAP)
return false;
}

return true;
}
9 changes: 8 additions & 1 deletion view/sharedcache/core/MachOProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,16 @@ void SharedCacheMachOProcessor::ApplyHeader(const SharedCache& cache, SharedCach
if (m_applyFunctions && header.functionStartsPresent)
{
auto targetPlatform = m_view->GetDefaultPlatform();
auto arch = targetPlatform->GetArchitecture();
auto functions = header.ReadFunctionTable(*m_vm);
for (const auto& func : functions)
m_view->AddFunctionForAnalysis(targetPlatform, func, false);
{
uint8_t opcode[BN_MAX_INSTRUCTION_LENGTH];
size_t opLen = arch->GetMaxInstructionLength();
m_vm->Read(opcode, func, opLen);
if (IsValidFunctionStart(arch, func, opcode, opLen))
m_view->AddFunctionForAnalysis(targetPlatform, func, false);
}
}

BulkSymbolModification bulkSymbolModification(m_view);
Expand Down
Loading