From 8565b45c0887d91b3e22ea26b86f9d4d05791a35 Mon Sep 17 00:00:00 2001 From: x86byte <111459558+x86byte@users.noreply.github.com> Date: Tue, 30 Jun 2026 09:28:52 +0100 Subject: [PATCH] fix: skip empty functions, relax jump-threading to handle multi-instr jmp stubs --- cfg/builder.cpp | 12 ++++++++-- tests/example1.exe.cfg | 50 +----------------------------------------- 2 files changed, 11 insertions(+), 51 deletions(-) diff --git a/cfg/builder.cpp b/cfg/builder.cpp index 31f3e80..87c59ef 100644 --- a/cfg/builder.cpp +++ b/cfg/builder.cpp @@ -458,12 +458,19 @@ CFG CFGBuilder::build() set built; queue pending; + auto isEmpty = [](const Function& f) -> bool { + for (const auto& b : f.blocks) + if (!b.instructions.empty()) return false; + return true; + }; + auto process = [&](addr_t addr, const string& name) { if (built.count(addr)) return; built.insert(addr); auto func = buildFunction(addr, name); + if (isEmpty(func)) return; cfg.functions.push_back(func); for (const auto& b : func.blocks) @@ -513,6 +520,7 @@ CFG CFGBuilder::build() built.insert(a); auto func = buildFunction(a, name); + if (isEmpty(func)) continue; cfg.functions.push_back(func); for (const auto& b : func.blocks) @@ -616,8 +624,8 @@ static void jumpThread(CFG& cfg) if (addr_to_func[t] != &func) continue; BasicBlock* target = it->second; - if (target->instructions.size() == 1 && - target->instructions[0].mnemonic == "jmp" && + if (!target->instructions.empty() && + target->instructions.back().mnemonic == "jmp" && target->successors.size() == 1) { addr_t redirect = target->successors[0]; diff --git a/tests/example1.exe.cfg b/tests/example1.exe.cfg index f982e4a..a392fc7 100644 --- a/tests/example1.exe.cfg +++ b/tests/example1.exe.cfg @@ -1,5 +1,5 @@ { - "binary": "tests\\example1.exe", + "binary": "C:\\Dev\\Current\\BinaryHardening\\cfgrip\\tests\\example1.exe", "mode": "full", "arch": "x86-64", "format": "PE", @@ -82571,12 +82571,6 @@ } ] }, - { - "address": "0x14000c9d0", - "name": "", - "blocks": [ - ] - }, { "address": "0x140006544", "name": "", @@ -95917,18 +95911,6 @@ } ] }, - { - "address": "0x140015200", - "name": "", - "blocks": [ - ] - }, - { - "address": "0x140011b00", - "name": "", - "blocks": [ - ] - }, { "address": "0x140012358", "name": "", @@ -114576,18 +114558,6 @@ } ] }, - { - "address": "0x14000b204", - "name": "", - "blocks": [ - ] - }, - { - "address": "0x14000ca74", - "name": "", - "blocks": [ - ] - }, { "address": "0x14000a73c", "name": "", @@ -135887,12 +135857,6 @@ } ] }, - { - "address": "0x1400075e8", - "name": "", - "blocks": [ - ] - }, { "address": "0x140009dc4", "name": "", @@ -144893,12 +144857,6 @@ } ] }, - { - "address": "0x140007650", - "name": "", - "blocks": [ - ] - }, { "address": "0x140009f50", "name": "", @@ -147615,12 +147573,6 @@ } ] }, - { - "address": "0x140004538", - "name": "", - "blocks": [ - ] - }, { "address": "0x14001a95c", "name": "",