Skip to content

[BOLT] Do not reject irreversible branches during disassembly #95572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2024
Merged
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
15 changes: 14 additions & 1 deletion bolt/include/bolt/Core/MCPlusBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,20 @@ class MCPlusBuilder {
}

/// Check whether this conditional branch can be reversed
virtual bool isReversibleBranch(const MCInst &Inst) const { return true; }
virtual bool isReversibleBranch(const MCInst &Inst) const {
assert(!isUnsupportedInstruction(Inst) && isConditionalBranch(Inst) &&
"Instruction is not known conditional branch");

if (isDynamicBranch(Inst))
return false;
return true;
}

/// Return true if this instruction inhibits analysis of the containing
/// function.
virtual bool isUnsupportedInstruction(const MCInst &Inst) const {
return false;
}

/// Return true of the instruction is of pseudo kind.
virtual bool isPseudo(const MCInst &Inst) const {
Expand Down
10 changes: 6 additions & 4 deletions bolt/lib/Core/BinaryFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,10 @@ Error BinaryFunction::disassemble() {
}
}

bool IsUnsupported = BC.MIB->isUnsupportedInstruction(Instruction);
if (IsUnsupported)
setIgnored();

if (MIB->isBranch(Instruction) || MIB->isCall(Instruction)) {
uint64_t TargetAddress = 0;
if (MIB->evaluateBranch(Instruction, AbsoluteInstrAddr, Size,
Expand All @@ -1288,12 +1292,10 @@ Error BinaryFunction::disassemble() {
const bool IsCondBranch = MIB->isConditionalBranch(Instruction);
MCSymbol *TargetSymbol = nullptr;

if (!BC.MIB->isReversibleBranch(Instruction)) {
setIgnored();
if (BinaryFunction *TargetFunc =
if (IsUnsupported)
if (auto *TargetFunc =
BC.getBinaryFunctionContainingAddress(TargetAddress))
TargetFunc->setIgnored();
}

if (IsCall && containsAddress(TargetAddress)) {
if (TargetAddress == getAddress()) {
Expand Down
3 changes: 1 addition & 2 deletions bolt/lib/Passes/Instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,7 @@ void Instrumentation::instrumentFunction(BinaryFunction &Function,
HasJumpTable = true;
else if (BC.MIB->isUnconditionalBranch(Inst))
HasUnconditionalBranch = true;
else if ((!BC.MIB->isCall(Inst) && !BC.MIB->isConditionalBranch(Inst)) ||
!BC.MIB->isReversibleBranch(Inst))
else if (!(BC.MIB->isCall(Inst) || BC.MIB->isConditionalBranch(Inst)))
continue;

const uint32_t FromOffset = *BC.MIB->getOffset(Inst);
Expand Down
18 changes: 8 additions & 10 deletions bolt/lib/Target/X86/X86MCPlusBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,19 +328,19 @@ class X86MCPlusBuilder : public MCPlusBuilder {
return false;
}

bool isReversibleBranch(const MCInst &Inst) const override {
if (isDynamicBranch(Inst))
return false;

bool isUnsupportedInstruction(const MCInst &Inst) const override {
switch (Inst.getOpcode()) {
default:
return true;
return false;

case X86::LOOP:
case X86::LOOPE:
case X86::LOOPNE:
case X86::JECXZ:
case X86::JRCXZ:
return false;
// These have a short displacement, and therefore (often) break after
// basic block relayout.
return true;
}
}

Expand Down Expand Up @@ -1879,11 +1879,9 @@ class X86MCPlusBuilder : public MCPlusBuilder {
continue;
}

// Handle conditional branches and ignore indirect branches
if (isReversibleBranch(*I) && getCondCode(*I) == X86::COND_INVALID) {
// Indirect branch
// Ignore indirect branches
if (getCondCode(*I) == X86::COND_INVALID)
return false;
}

if (CondBranch == nullptr) {
const MCSymbol *TargetBB = getTargetSymbol(*I);
Expand Down
Loading