Skip to content

Commit 696f3a7

Browse files
committed
[BOLT] Avoid reference updates for non-JT symbol operands
Add a check to skip updating references for operands that do not directly refer to jump table symbols but fall within a jump table's address range to prevent unintended modifications.
1 parent 37b7207 commit 696f3a7

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

bolt/lib/Passes/ValidateMemRefs.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ bool ValidateMemRefs::checkAndFixJTReference(BinaryFunction &BF, MCInst &Inst,
3434
if (!JT)
3535
return false;
3636

37+
// If the operand does not refer to the jump table symbol, then we
38+
// don't need to update the reference.
39+
if (JT->getFirstLabel() != Sym)
40+
return true;
41+
3742
const bool IsLegitAccess = llvm::is_contained(JT->Parents, &BF);
3843
if (IsLegitAccess)
3944
return true;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# If the operand references a symbol that differs from the jump table label,
2+
# no reference updating is required even if its target address resides within
3+
# the jump table's range.
4+
# In this test case, consider the second instruction within the main function,
5+
# where the address resulting from 'c + 17' corresponds to one byte beyond the
6+
# address of the .LJTI2_0 jump table label. However, this operand represents
7+
# an offset calculation related to the global variable 'c' and should remain
8+
# unaffected by the jump table.
9+
10+
# REQUIRES: system-linux
11+
12+
13+
# RUN: %clang -no-pie %s -o %t.exe -Wl,-q
14+
15+
# RUN: %t.exe
16+
# RUN: llvm-bolt -funcs=main,foo/1 %t.exe -o %t.exe.bolt -jump-tables=move
17+
# RUN: %t.exe.bolt
18+
19+
.text
20+
.globl main
21+
.type main,@function
22+
main:
23+
pushq %rbp
24+
movq %rsp, %rbp
25+
movq $-16, %rax
26+
movl c+17(%rax), %edx
27+
cmpl $255, %edx
28+
je .LCorrect
29+
movl $1, %eax
30+
popq %rbp
31+
ret
32+
.LCorrect:
33+
movl $0, %eax
34+
popq %rbp
35+
ret
36+
.p2align 4, 0x90
37+
.type foo,@function
38+
foo:
39+
movq $0, %rax
40+
jmpq *.LJTI2_0(,%rax,8)
41+
addl $-36, %eax
42+
.LBB2_2:
43+
addl $-16, %eax
44+
retq
45+
.section .rodata,"a",@progbits
46+
.type c,@object
47+
.data
48+
.globl c
49+
.p2align 4, 0x0
50+
c:
51+
.byte 1
52+
.byte 0xff
53+
.zero 14
54+
.size c, 16
55+
.LJTI2_0:
56+
.quad .LBB2_2
57+
.quad .LBB2_2
58+
.quad .LBB2_2
59+
.quad .LBB2_2
60+

0 commit comments

Comments
 (0)