Skip to content

Commit 5265b33

Browse files
committed
add aarch64 test case
1 parent de3f331 commit 5265b33

File tree

4 files changed

+57
-21
lines changed

4 files changed

+57
-21
lines changed

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,10 @@ MCSymbol *BinaryFunction::getOrCreateLocalLabel(uint64_t Address) {
10441044

10451045
// For AArch64, check if this address is part of a constant island.
10461046
if (BC.isAArch64()) {
1047-
if (MCSymbol *IslandSym = getOrCreateIslandAccess(Address))
1047+
if (MCSymbol *IslandSym = getOrCreateIslandAccess(Address)) {
1048+
Labels[Offset] = IslandSym;
10481049
return IslandSym;
1050+
}
10491051
}
10501052

10511053
if (Offset == getSize())
@@ -1925,7 +1927,8 @@ bool BinaryFunction::validateExternalBranch(uint64_t TargetAddress) {
19251927
return true;
19261928

19271929
if (TargetFunction->CurrentState == State::Disassembled &&
1928-
!TargetFunction->getInstructionAtOffset(TargetOffset))
1930+
(!TargetFunction->getInstructionAtOffset(TargetOffset) ||
1931+
getSizeOfDataInCodeAt(TargetOffset)))
19291932
IsValid = false;
19301933
} else {
19311934
if (!BC.getSectionForAddress(TargetAddress))
@@ -1959,15 +1962,14 @@ bool BinaryFunction::validateInternalBranch() {
19591962
if (!Offset || (Offset > getSize()))
19601963
continue;
19611964

1962-
if (getInstructionAtOffset(Offset))
1963-
continue;
1964-
1965-
BC.errs() << "BOLT-WARNING: corrupted control flow detected in function "
1966-
<< *this << ", an internal branch/call targets an invalid "
1967-
<< "instruction at address 0x"
1968-
<< Twine::utohexstr(getAddress() + Offset) << "\n";
1969-
setIgnored();
1970-
return false;
1965+
if (!getInstructionAtOffset(Offset) || getSizeOfDataInCodeAt(Offset)) {
1966+
BC.errs() << "BOLT-WARNING: corrupted control flow detected in function "
1967+
<< *this << ", an internal branch/call targets an invalid "
1968+
<< "instruction at address 0x"
1969+
<< Twine::utohexstr(getAddress() + Offset) << "\n";
1970+
setIgnored();
1971+
return false;
1972+
}
19711973
}
19721974

19731975
return true;

bolt/test/AArch64/constant-island-alignment.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ _start:
5353
blr x0
5454
mov x0, #1
5555
ret
56+
.size _start,.-_start
5657
nop
5758
# CHECK: {{0|8}} <$d>:
5859
.Lci:
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Test that BOLT errs when detecting the target
2+
## of a direct call/branch is a invalid instruction
3+
4+
# REQUIRES: system-linux
5+
# RUN: rm -rf %t && mkdir -p %t && cd %t
6+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-linux %s -o main.o
7+
# RUN: %clang %cflags %t/main.o -o main.exe -Wl,-q
8+
# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -lite=0 2>&1 | FileCheck %s --check-prefix=CHECK-TARGETS
9+
10+
# CHECK-TARGETS: BOLT-WARNING: corrupted control flow detected in function external_corrupt, an external branch/call targets an invalid instruction at address 0x{{[0-9a-f]+}}
11+
# CHECK-TARGETS: BOLT-WARNING: corrupted control flow detected in function internal_corrupt, an internal branch/call targets an invalid instruction at address 0x{{[0-9a-f]+}}
12+
13+
14+
.globl internal_corrupt
15+
.type internal_corrupt,@function
16+
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:
22+
.word 0xffffffff
23+
.size internal_corrupt,.-internal_corrupt
24+
25+
26+
.globl external_corrupt
27+
.type external_corrupt,@function
28+
external_corrupt:
29+
ret
30+
nop
31+
.Lfake_branch_2:
32+
.inst 0x14000004 // Opcode 0x14=b, check for external branch: b + 0xf
33+
.size external_corrupt,.-external_corrupt

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
# RUN: %clang %cflags -pie -Wl,-q %t/main.o -o main.exe
88
# RUN: llvm-bolt %t/main.exe -o %t/main.exe.bolt -lite=0 2>&1 | FileCheck %s --check-prefix=CHECK-TARGETS
99

10-
# CHECK-TARGETS: BOLT-WARNING: corrupted control flow detected in function external_corrcupt, an external branch/call targets an invalid instruction at address 0x{{[0-9a-f]+}}
11-
# CHECK-TARGETS: BOLT-WARNING: corrupted control flow detected in function internal_corrcupt, an internal branch/call targets an invalid instruction at address 0x{{[0-9a-f]+}}
10+
# CHECK-TARGETS: BOLT-WARNING: corrupted control flow detected in function external_corrupt, an external branch/call targets an invalid instruction at address 0x{{[0-9a-f]+}}
11+
# CHECK-TARGETS: BOLT-WARNING: corrupted control flow detected in function internal_corrupt, an internal branch/call targets an invalid instruction at address 0x{{[0-9a-f]+}}
1212

1313

14-
.globl internal_corrcupt
15-
.type internal_corrcupt,@function
14+
.globl internal_corrupt
15+
.type internal_corrupt,@function
1616
.align 16
17-
internal_corrcupt:
17+
internal_corrupt:
1818
leaq .Lopts_1(%rip),%rax
1919
addq $25,%rax
2020
.byte 0xf3,0xc3
@@ -26,13 +26,13 @@ internal_corrcupt:
2626
.Lopts_1:
2727
.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
2828
.align 64
29-
.size internal_corrcupt,.-internal_corrcupt
29+
.size internal_corrupt,.-internal_corrupt
3030

3131

32-
.globl external_corrcupt
33-
.type external_corrcupt,@function
32+
.globl external_corrupt
33+
.type external_corrupt,@function
3434
.align 16
35-
external_corrcupt:
35+
external_corrupt:
3636
leaq .Lopts_2(%rip),%rax
3737
addq $25,%rax
3838
.byte 0xf3,0xc3
@@ -44,4 +44,4 @@ external_corrcupt:
4444
.Lopts_2:
4545
.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
4646
.align 64
47-
.size external_corrcupt,.-external_corrcupt
47+
.size external_corrupt,.-external_corrupt

0 commit comments

Comments
 (0)