Skip to content

Commit 465d03e

Browse files
committed
refactor external branch and simplify tests
1 parent 7cdb175 commit 465d03e

File tree

5 files changed

+37
-79
lines changed

5 files changed

+37
-79
lines changed

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,11 +2320,6 @@ class BinaryFunction {
23202320
/// zero-value bytes.
23212321
bool isZeroPaddingAt(uint64_t Offset) const;
23222322

2323-
/// Validate if the target of an external direct branch/call is a valid
2324-
/// executable instruction.
2325-
/// Return true if the target is valid, false otherwise.
2326-
bool validateExternalBranch(uint64_t TargetAddress);
2327-
23282323
/// Validate if the target of any internal direct branch/call is a valid
23292324
/// executable instruction.
23302325
/// Return true if all the targets are valid, false otherwise.

bolt/lib/Core/BinaryContext.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,16 +1431,27 @@ void BinaryContext::processInterproceduralReferences() {
14311431
<< TargetFunction->getPrintName() << '\n';
14321432
}
14331433

1434+
const uint64_t TargetOffset = Address - TargetFunction->getAddress();
1435+
if (TargetOffset && (TargetOffset <= TargetFunction->getSize())) {
1436+
if (TargetFunction->CurrentState ==
1437+
BinaryFunction::State::Disassembled &&
1438+
(!TargetFunction->getInstructionAtOffset(TargetOffset) ||
1439+
TargetFunction->getSizeOfDataInCodeAt(TargetOffset)))
1440+
this->errs()
1441+
<< "BOLT-WARNING: corrupted control flow detected in function "
1442+
<< Function
1443+
<< ", an external branch/call targets an invalid instruction "
1444+
<< "at address 0x" << Twine::utohexstr(Address) << "\n";
1445+
Function.setIgnored();
1446+
}
1447+
14341448
// Create an extra entry point if needed. Can also render the target
14351449
// function ignored if the reference is invalid.
14361450
handleExternalBranchTarget(Address, *TargetFunction);
14371451

14381452
continue;
14391453
}
14401454

1441-
if (!Function.validateExternalBranch(Address))
1442-
continue;
1443-
14441455
// Check if address falls in function padding space - this could be
14451456
// unmarked data in code. In this case adjust the padding space size.
14461457
ErrorOr<BinarySection &> Section = getSectionForAddress(Address);

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,45 +1910,8 @@ bool BinaryFunction::scanExternalRefs() {
19101910
return Success;
19111911
}
19121912

1913-
bool BinaryFunction::validateExternalBranch(uint64_t TargetAddress) {
1914-
if (!isSimple())
1915-
return true;
1916-
1917-
BinaryFunction *TargetFunction =
1918-
BC.getBinaryFunctionContainingAddress(TargetAddress);
1919-
1920-
bool IsValid = true;
1921-
1922-
if (TargetFunction) {
1923-
const uint64_t TargetOffset = TargetAddress - TargetFunction->getAddress();
1924-
// Skip empty functions and out-of-bounds offsets,
1925-
// as they may not be disassembled.
1926-
if (!TargetOffset || (TargetOffset > TargetFunction->getSize()))
1927-
return true;
1928-
1929-
if (TargetFunction->CurrentState == State::Disassembled &&
1930-
(!TargetFunction->getInstructionAtOffset(TargetOffset) ||
1931-
getSizeOfDataInCodeAt(TargetOffset)))
1932-
IsValid = false;
1933-
} else {
1934-
if (!BC.getSectionForAddress(TargetAddress))
1935-
IsValid = false;
1936-
}
1937-
1938-
if (!IsValid) {
1939-
setIgnored();
1940-
BC.errs() << "BOLT-WARNING: corrupted control flow detected in function "
1941-
<< *this
1942-
<< ", an external branch/call targets an invalid instruction "
1943-
<< "at address 0x" << Twine::utohexstr(TargetAddress) << "\n";
1944-
return false;
1945-
}
1946-
1947-
return true;
1948-
}
1949-
19501913
bool BinaryFunction::validateInternalBranches() {
1951-
if (!isSimple())
1914+
if (!isSimple() || TrapsOnEntry)
19521915
return true;
19531916

19541917
for (const auto &KV : Labels) {

bolt/test/AArch64/validate-branch-target.s

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
.globl internal_corrupt
1515
.type internal_corrupt,@function
1616
internal_corrupt:
17-
ret
18-
nop
19-
.Lfake_branch_1:
20-
.inst 0x14000001 // Opcode 0x14=b, check for internal branch: b + 0x4
21-
.Lgarbage_1:
17+
b constant_island_0 // targeting the data in code
18+
constant_island_0:
2219
.word 0xffffffff
2320
.size internal_corrupt,.-internal_corrupt
2421

2522

2623
.globl external_corrupt
2724
.type external_corrupt,@function
2825
external_corrupt:
29-
ret
30-
nop
31-
.Lfake_branch_2:
32-
.inst 0x14000004 // Opcode 0x14=b, check for external branch: b + 0xf
26+
b constant_island_1 // targeting the data in code externally
3327
.size external_corrupt,.-external_corrupt
28+
29+
.globl external_func
30+
.type external_func,@function
31+
external_func:
32+
add x0, x0, x1
33+
constant_island_1:
34+
.word 0xffffffff // data in code
35+
ret
36+
.size external_func,.-external_func

bolt/test/X86/validate-branch-target.s

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,21 @@
1313

1414
.globl internal_corrupt
1515
.type internal_corrupt,@function
16-
.align 16
1716
internal_corrupt:
18-
leaq .Lopts_1(%rip),%rax
19-
addq $25,%rax
20-
.byte 0xf3,0xc3
21-
.L8xchar_1:
22-
addq $12,%rax
23-
.Ldone_1:
24-
.byte 0xf3,0xc3
25-
.align 64
26-
.Lopts_1:
27-
.byte 114,1,52,40,56,120,44,105,110,116,41,0 # data '114' will be disassembled as 'jb', check for internal branch: jb + 0x1
28-
.align 64
17+
jb data_in_code + 1 # targeting the data in code, and jump into the middle of 'xorb' instruction
18+
data_in_code:
19+
.byte 0x34, 0x01 # data in code, will be disassembled as 'xorb 0x1, %al'
2920
.size internal_corrupt,.-internal_corrupt
3021

3122

3223
.globl external_corrupt
3324
.type external_corrupt,@function
34-
.align 16
3525
external_corrupt:
36-
leaq .Lopts_2(%rip),%rax
37-
addq $25,%rax
38-
.byte 0xf3,0xc3
39-
.L8xchar_2:
40-
addq $12,%rax
41-
.Ldone_2:
42-
.byte 0xf3,0xc3
43-
.align 64
44-
.Lopts_2:
45-
.byte 114,99,52,40,56,120,44,99,104,97,114,41,0 # data '114' will be disassembled as 'jb', check for external branch: jb + 0x63
46-
.align 64
26+
jb external_func + 1 # targeting the middle of normal instruction externally
4727
.size external_corrupt,.-external_corrupt
28+
29+
.globl external_func
30+
.type external_func,@function
31+
external_func:
32+
addq $1, %rax # normal instruction
33+
.size external_func,.-external_func

0 commit comments

Comments
 (0)