From c8cecd9f466145350c902f3913cefafb631d5f74 Mon Sep 17 00:00:00 2001 From: Billy Laws Date: Mon, 28 Jul 2025 17:42:45 +0100 Subject: [PATCH] Frontend: Adjust forward branch distance limit Now executable page tracking is implemented, this limit is technically unnecessary and effectively never hit in normal code. Keep a reasonable limit however to avoid accidentally inlining tail calls and exploring dead branches in obfuscated code. --- FEXCore/Source/Interface/Core/Frontend.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/FEXCore/Source/Interface/Core/Frontend.cpp b/FEXCore/Source/Interface/Core/Frontend.cpp index cd6f599db..775d38df7 100644 --- a/FEXCore/Source/Interface/Core/Frontend.cpp +++ b/FEXCore/Source/Interface/Core/Frontend.cpp @@ -1066,9 +1066,10 @@ void Decoder::BranchTargetInMultiblockRange() { } // If the target RIP is x86 code within the symbol ranges then we are golden - // Forbid cross-page branches to both avoid massive (range-wise) code blocks in highly fragmented code and trying to decode unmapped branch targets - bool ValidMultiblockMember = - TargetRIP >= SymbolMinAddress && TargetRIP < std::min(FEXCore::AlignUp(InstEnd, FEXCore::Utils::FEX_PAGE_SIZE), SymbolMaxAddress); + // Forbid distant branches to have the cost code better match the guest code layout, avoiding massive (range-wise) code + // blocks in highly fragmented guest code. Such branches are often not-taken branches to garbage in obfuscated code. + constexpr uint64_t MAX_FORWARD_BRANCH_DIST = FEXCore::Utils::FEX_PAGE_SIZE * 4; + bool ValidMultiblockMember = TargetRIP >= SymbolMinAddress && TargetRIP < std::min(InstEnd + MAX_FORWARD_BRANCH_DIST, SymbolMaxAddress); #ifdef _M_ARM_64EC ValidMultiblockMember = ValidMultiblockMember && !RtlIsEcCode(TargetRIP);