xref: /llvm-project/bolt/test/runtime/X86/interp-overwrite-bug.s (revision 6b4eb0b94a6d5e6b65aa874a65e01d8772cf7054)
1*6b4eb0b9SAmir Ayupov# This reproduces a bug with BOLT non-reloc mode, during emission, if the user
2*6b4eb0b9SAmir Ayupov# does not use -update-debug-sections. In this bug, if a function gets too large
3*6b4eb0b9SAmir Ayupov# to occupy its original location, but it has a jump table, BOLT would skip
4*6b4eb0b9SAmir Ayupov# rewriting the function but it would still overwrite the jump table in a bogus
5*6b4eb0b9SAmir Ayupov# file offset (offset zero). This will typically corrupt the .interp section,
6*6b4eb0b9SAmir Ayupov# which is the first section in the binary, depending on the size of the jump
7*6b4eb0b9SAmir Ayupov# table that was written. If .interp is corrupted, the binary won't run.
8*6b4eb0b9SAmir Ayupov
9*6b4eb0b9SAmir Ayupov# REQUIRES: system-linux
10*6b4eb0b9SAmir Ayupov
11*6b4eb0b9SAmir Ayupov# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown %s -o %t.o
12*6b4eb0b9SAmir Ayupov# RUN: llvm-strip --strip-unneeded %t.o
13*6b4eb0b9SAmir Ayupov# RUN: %clang %cflags -nostartfiles -nostdlib -lc %t.o -o %t.exe
14*6b4eb0b9SAmir Ayupov
15*6b4eb0b9SAmir Ayupov# RUN: llvm-bolt %t.exe -o %t.exe.bolt -relocs=0 -lite=0 -reorder-blocks=reverse
16*6b4eb0b9SAmir Ayupov
17*6b4eb0b9SAmir Ayupov# RUN: %t.exe.bolt 1 2 3
18*6b4eb0b9SAmir Ayupov
19*6b4eb0b9SAmir Ayupov  .file "test.S"
20*6b4eb0b9SAmir Ayupov  .text
21*6b4eb0b9SAmir Ayupov  .globl _start
22*6b4eb0b9SAmir Ayupov  .type _start, @function
23*6b4eb0b9SAmir Ayupov_start:
24*6b4eb0b9SAmir Ayupov  .cfi_startproc
25*6b4eb0b9SAmir Ayupov  xor    %rax,%rax
26*6b4eb0b9SAmir Ayupov  movq   (%rsp), %rdi
27*6b4eb0b9SAmir Ayupov  and    $0x3,%rdi
28*6b4eb0b9SAmir Ayupov  jmpq   *.JT1(,%rdi,8)
29*6b4eb0b9SAmir Ayupov.LBB1:
30*6b4eb0b9SAmir Ayupov  movl   $0x1,%eax
31*6b4eb0b9SAmir Ayupov  jmp    .LBB5
32*6b4eb0b9SAmir Ayupov.LBB2:
33*6b4eb0b9SAmir Ayupov  movl   $0x2,%eax
34*6b4eb0b9SAmir Ayupov  jmp    .LBB5
35*6b4eb0b9SAmir Ayupov.LBB3:
36*6b4eb0b9SAmir Ayupov  movl   $0x3,%eax
37*6b4eb0b9SAmir Ayupov  jmp    .LBB5
38*6b4eb0b9SAmir Ayupov.LBB4:
39*6b4eb0b9SAmir Ayupov  movl   $0x4,%eax
40*6b4eb0b9SAmir Ayupov.LBB5:
41*6b4eb0b9SAmir Ayupov  callq exit@PLT
42*6b4eb0b9SAmir Ayupov  .cfi_endproc
43*6b4eb0b9SAmir Ayupov  .size _start, .-_start
44*6b4eb0b9SAmir Ayupov
45*6b4eb0b9SAmir Ayupov# Make the jump table large enough to force the bug to manifest as .interp
46*6b4eb0b9SAmir Ayupov# being corrupt. Typically .interp will be at offset 0x1c8, so the jump table
47*6b4eb0b9SAmir Ayupov# needs to be larger than that.
48*6b4eb0b9SAmir Ayupov  .section .rodata,"a",@progbits
49*6b4eb0b9SAmir Ayupov  .p2align 3
50*6b4eb0b9SAmir Ayupov.JT1:
51*6b4eb0b9SAmir Ayupov  .quad .LBB1
52*6b4eb0b9SAmir Ayupov  .quad .LBB2
53*6b4eb0b9SAmir Ayupov  .quad .LBB3
54*6b4eb0b9SAmir Ayupov  .quad .LBB4
55*6b4eb0b9SAmir Ayupov  .quad .LBB5
56*6b4eb0b9SAmir Ayupov  .quad .LBB5
57*6b4eb0b9SAmir Ayupov  .rept 100
58*6b4eb0b9SAmir Ayupov  .quad .LBB1
59*6b4eb0b9SAmir Ayupov  .endr
60