Skip to content

Commit 671095b

Browse files
authored
[BOLT][AArch64] Check Last Element Instead of Returning nullptr in lookupStubFromGroup (#114015)
The current implementation of `lookupStubFromGroup` is incorrect. The function is intended to find and return the closest stub using `lower_bound`, which identifies the first element in a sorted range that is not less than a specified value. However, if such an element is not found within `Candidates` and the list is not empty, the function returns `nullptr`. Instead, it should check whether the last element satisfies the condition.
1 parent 03847f1 commit 671095b

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

bolt/lib/Passes/LongJmp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ BinaryBasicBlock *LongJmpPass::lookupStubFromGroup(
138138
const std::pair<uint64_t, BinaryBasicBlock *> &RHS) {
139139
return LHS.first < RHS.first;
140140
});
141-
if (Cand == Candidates.end())
142-
return nullptr;
143-
if (Cand != Candidates.begin()) {
141+
if (Cand == Candidates.end()) {
142+
Cand = std::prev(Cand);
143+
} else if (Cand != Candidates.begin()) {
144144
const StubTy *LeftCand = std::prev(Cand);
145145
if (Cand->first - DotAddress > DotAddress - LeftCand->first)
146146
Cand = LeftCand;

bolt/test/AArch64/long-jmp-one-stub.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## This test verifies that no unnecessary stubs are inserted when each DotAddress increases during a lookup.
2+
3+
# REQUIRES: system-linux, asserts
4+
5+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
6+
# RUN: %clang %cflags -O0 %t.o -o %t.exe -Wl,-q
7+
# RUN: link_fdata %s %t.o %t.fdata
8+
# RUN: llvm-bolt %t.exe -o %t.bolt \
9+
# RUN: --data %t.fdata | FileCheck %s
10+
11+
# CHECK: BOLT-INFO: Inserted 1 stubs in the hot area and 0 stubs in the cold area.
12+
13+
.section .text
14+
.global _start
15+
.global far_away_func
16+
17+
.align 4
18+
.global _start
19+
.type _start, %function
20+
_start:
21+
# FDATA: 0 [unknown] 0 1 _start 0 0 100
22+
bl far_away_func
23+
bl far_away_func
24+
ret
25+
.space 0x8000000
26+
.global far_away_func
27+
.type far_away_func, %function
28+
far_away_func:
29+
add x0, x0, #1
30+
ret
31+
32+
.reloc 0, R_AARCH64_NONE

0 commit comments

Comments
 (0)