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 BinaryNinja::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 @@ -1545,4 +1546,27 @@ 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(Architecture* arch, uint64_t addr, const uint8_t* opcode, size_t opLen)
{
if (!opLen)
return false;

Ref<LowLevelILFunction> ilFunc = new 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;
}
}
19 changes: 18 additions & 1 deletion view/sharedcache/core/MachOProcessor.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "MachOProcessor.h"
#include "SharedCache.h"

#include <exception>

using namespace BinaryNinja;

SharedCacheMachOProcessor::SharedCacheMachOProcessor(Ref<BinaryView> view, std::shared_ptr<VirtualMemory> vm)
Expand Down Expand Up @@ -50,9 +52,24 @@ 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();
try
{
m_vm->Read(opcode, func, opLen);
}
catch (const std::exception& e)
{
m_logger->LogWarnF("Failed to read function start at {:#x}: {}", func, e.what());
continue;
}
if (IsValidFunctionStart(arch, func, opcode, opLen))
m_view->AddFunctionForAnalysis(targetPlatform, func, false);
}
}

BulkSymbolModification bulkSymbolModification(m_view);
Expand Down