Skip to content

Commit 3cc4a96

Browse files
committed
Avoid creating functions at jump table entries in shared cache function tables
Add a heuristic to skip creating functions at addresses that appear to be jump table entries rather than actual function starts. On aarch64, any entry that disassembles as a `udf` instruction is skipped, since `udf` is used as padding/data in jump tables and is not a valid function prologue. Fix #7992
1 parent 770e9c0 commit 3cc4a96

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

view/sharedcache/core/MachOProcessor.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@ SharedCacheMachOProcessor::SharedCacheMachOProcessor(Ref<BinaryView> view, std::
1717
}
1818
}
1919

20+
static bool HeuristicIsAFunction(Platform* targetPlatform, std::shared_ptr<VirtualMemory> vm, uint64_t func)
21+
{
22+
// Very dumb heuristic which prevents us from creating function at jump tables
23+
if (targetPlatform->GetArchitecture()->GetName() == "aarch64")
24+
{
25+
// disassemble then and ensure it's not a UDF instruction
26+
uint32_t instruction = 0;
27+
size_t instructionLength = 4;
28+
vm->Read(&instruction, func, sizeof(instruction));
29+
std::vector<InstructionTextToken> result;
30+
targetPlatform->GetArchitecture()->GetInstructionText((uint8_t*)&instruction, func, instructionLength, result);
31+
bool isUDF = false;
32+
for (const auto& instructionText : result)
33+
{
34+
if (instructionText.type == BNInstructionTextTokenType::InstructionToken && instructionText.text == "udf")
35+
{
36+
// This is likely a jump table entry, skip creating a function here.
37+
return false;
38+
}
39+
}
40+
}
41+
return true;
42+
}
43+
2044
void SharedCacheMachOProcessor::ApplyHeader(const SharedCache& cache, SharedCacheMachOHeader& header)
2145
{
2246
auto typeLibraryFromName = [&](const std::string& name) -> Ref<TypeLibrary> {
@@ -52,7 +76,10 @@ void SharedCacheMachOProcessor::ApplyHeader(const SharedCache& cache, SharedCach
5276
auto targetPlatform = m_view->GetDefaultPlatform();
5377
auto functions = header.ReadFunctionTable(*m_vm);
5478
for (const auto& func : functions)
55-
m_view->AddFunctionForAnalysis(targetPlatform, func, false);
79+
{
80+
if (HeuristicIsAFunction(targetPlatform, m_vm, func))
81+
m_view->AddFunctionForAnalysis(targetPlatform, func, false);
82+
}
5683
}
5784

5885
BulkSymbolModification bulkSymbolModification(m_view);

0 commit comments

Comments
 (0)