Skip to content

Commit 4ef2904

Browse files
author
Vasily Leonenko
committed
[BOLT][test] Add hook-init AArch64 test for checking instrumentation initialization
1 parent cf2bb1d commit 4ef2904

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed

bolt/test/AArch64/hook-init.s

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
## Test the different ways of hooking the init function for instrumentation (via
2+
## entry point, DT_INIT and via DT_INIT_ARRAY). We test the latter for both PIE
3+
## and non-PIE binaries because of the different ways of handling relocations
4+
## (static or dynamic), executable and shared library.
5+
## All tests perform the following steps:
6+
## - Compile and link for the case to be tested
7+
## - Some sanity-checks on the dynamic section and relocations in the binary to
8+
## verify it has the shape we want for testing:
9+
## - INTERP in Program Headers
10+
## - DT_INIT or DT_INIT_ARRAY in dynamic section
11+
## - No relative relocations for non-PIE
12+
## - Instrument (with extra instrumentation-no-use-entry-point option in some cases)
13+
## - Verify generated binary
14+
# REQUIRES: system-linux,bolt-runtime,target=aarch64{{.*}}
15+
16+
# RUN: %clang %cflags -pie %s -Wl,-q -o %t.exe
17+
# RUN: llvm-readelf -d %t.exe | FileCheck --check-prefix=DYN-INIT %s
18+
# RUN: llvm-readelf -l %t.exe | FileCheck --check-prefix=PH-INTERP %s
19+
# RUN: llvm-readelf -r %t.exe | FileCheck --check-prefix=RELOC-PIE %s
20+
# RUN: llvm-bolt %t.exe -o %t --instrument
21+
# RUN: llvm-readelf -hdrs %t | FileCheck --check-prefix=CHECK-INIT-EP %s
22+
# RUN: llvm-bolt %t.exe -o %t-no-ep --instrument --instrumentation-no-use-entry-point
23+
# RUN: llvm-readelf -hdrs %t-no-ep | FileCheck --check-prefix=CHECK-INIT-NO-EP %s
24+
25+
# RUN: %clang -shared %cflags -pie %s -Wl,-q -o %t-shared.exe
26+
# RUN: llvm-readelf -d %t-shared.exe | FileCheck --check-prefix=DYN-INIT %s
27+
# RUN: llvm-readelf -l %t-shared.exe | FileCheck --check-prefix=PH-INTERP-SHARED %s
28+
# RUN: llvm-readelf -r %t-shared.exe | FileCheck --check-prefix=RELOC-SHARED-PIE %s
29+
# RUN: llvm-bolt %t-shared.exe -o %t-shared --instrument
30+
# RUN: llvm-readelf -hdrs %t-shared | FileCheck --check-prefix=CHECK-SHARED-INIT %s
31+
32+
# RUN: %clang %cflags -pie %s -Wl,-q,-init=0 -o %t-no-init.exe
33+
# RUN: llvm-readelf -d %t-no-init.exe | FileCheck --check-prefix=DYN-NO-INIT %s
34+
# RUN: llvm-readelf -l %t-no-init.exe | FileCheck --check-prefix=PH-INTERP %s
35+
# RUN: llvm-readelf -r %t-no-init.exe | FileCheck --check-prefix=RELOC-PIE %s
36+
# RUN: llvm-bolt %t-no-init.exe -o %t-no-init --instrument
37+
# RUN: llvm-readelf -hdrs %t-no-init | FileCheck --check-prefix=CHECK-NO-INIT-EP %s
38+
# RUN: llvm-bolt %t-no-init.exe -o %t-no-init-no-ep --instrument --instrumentation-no-use-entry-point
39+
# TODO: CHECK DT_INIT_ARRAY with instrumentation-no-use-entry-point
40+
# RUN: llvm-readelf -hdrs %t-no-init-no-ep | FileCheck --check-prefix=CHECK-NO-INIT-NO-EP %s
41+
42+
# RUN: %clang -shared %cflags -pie %s -Wl,-q,-init=0 -o %t-shared-no-init.exe
43+
# RUN: llvm-readelf -d %t-shared-no-init.exe | FileCheck --check-prefix=DYN-NO-INIT %s
44+
# RUN: llvm-readelf -l %t-shared-no-init.exe | FileCheck --check-prefix=PH-INTERP-SHARED %s
45+
# RUN: llvm-readelf -r %t-shared-no-init.exe | FileCheck --check-prefix=RELOC-SHARED-PIE %s
46+
# RUN: llvm-bolt %t-shared-no-init.exe -o %t-shared-no-init --instrument
47+
# RUN: llvm-readelf -drs %t-shared-no-init | FileCheck --check-prefix=CHECK-SHARED-NO-INIT %s
48+
49+
## Create a dummy shared library to link against to force creation of the dynamic section.
50+
# RUN: %clang %cflags %p/../Inputs/stub.c -fPIC -shared -o %t-stub.so
51+
# RUN: %clang %cflags %s -no-pie -Wl,-q,-init=0 %t-stub.so -o %t-no-pie-no-init.exe
52+
# RUN: llvm-readelf -r %t-no-pie-no-init.exe | FileCheck --check-prefix=RELOC-NO-PIE %s
53+
# RUN: llvm-bolt %t-no-pie-no-init.exe -o %t-no-pie-no-init --instrument
54+
# RUN: llvm-readelf -hds %t-no-pie-no-init | FileCheck --check-prefix=CHECK-NO-PIE-NO-INIT-EP %s
55+
56+
## With init: dynamic section should contain DT_INIT
57+
# DYN-INIT: (INIT)
58+
59+
## Without init: dynamic section should only contain DT_INIT_ARRAY
60+
# DYN-NO-INIT-NOT: (INIT)
61+
# DYN-NO-INIT: (INIT_ARRAY)
62+
# DYN-NO-INIT: (INIT_ARRAYSZ)
63+
64+
## With interp program header (executable)
65+
# PH-INTERP: Program Headers:
66+
# PH-INTERP: INTERP
67+
68+
## Without interp program header (shared library)
69+
# PH-INTERP-SHARED: Program Headers:
70+
# PH-INTERP-SHARED-NOT: INTERP
71+
72+
## With PIE: binary should have relative relocations
73+
# RELOC-PIE: R_AARCH64_RELATIVE
74+
75+
## With PIE: binary should have relative relocations
76+
# RELOC-SHARED-PIE: R_AARCH64_ABS64
77+
78+
## Without PIE: binary should not have relative relocations
79+
# RELOC-NO-PIE-NOT: R_AARCH64_RELATIVE
80+
81+
## Check that entry point address is set to __bolt_runtime_start for PIE executable with DT_INIT
82+
# CHECK-INIT-EP: ELF Header:
83+
# CHECK-INIT-EP: Entry point address: 0x[[#%X,EP_ADDR:]]
84+
## Check that the dynamic relocation at .init and .init_array were not patched
85+
# CHECK-INIT-EP: Dynamic section at offset {{.*}} contains {{.*}} entries:
86+
# CHECK-INIT-EP-NOT: (INIT) 0x[[#%x, EP_ADDR]]
87+
# CHECK-INIT-EP-NOT: (INIT_ARRAY) 0x[[#%x, EP_ADDR]]
88+
## Check that the new entry point address points to __bolt_runtime_start
89+
# CHECK-INIT-EP: Symbol table '.symtab' contains {{.*}} entries:
90+
# CHECK-INIT-EP: {{0+}}[[#%x, EP_ADDR]] {{.*}} __bolt_runtime_start
91+
92+
## Check that DT_INIT address is set to __bolt_runtime_start for PIE executable with DT_INIT
93+
# CHECK-INIT-NO-EP: ELF Header:
94+
# CHECK-INIT-NO-EP: Entry point address: 0x[[#%X,EP_ADDR:]]
95+
## Read Dynamic section DT_INIT and DT_INIT_ARRAY entries
96+
# CHECK-INIT-NO-EP: Dynamic section at offset {{.*}} contains {{.*}} entries:
97+
# CHECK-INIT-NO-EP-DAG: (INIT) 0x[[#%x,INIT:]]
98+
# CHECK-INIT-NO-EP-DAG: (INIT_ARRAY) 0x[[#%x,INIT_ARRAY:]]
99+
## Check if ELF entry point address points to _start symbol and new DT_INIT entry points to __bolt_runtime_start
100+
# CHECK-INIT-NO-EP: Symbol table '.symtab' contains {{.*}} entries:
101+
# CHECK-INIT-NO-EP-DAG: {{0+}}[[#%x, EP_ADDR]] {{.*}} _start
102+
# CHECK-INIT-NO-EP-DAG: {{0+}}[[#%x, INIT]] {{.*}} __bolt_runtime_start
103+
104+
## Check that entry point address is set to __bolt_runtime_start for PIE executable without DT_INIT
105+
# CHECK-NO-INIT-EP: ELF Header:
106+
# CHECK-NO-INIT-EP: Entry point address: 0x[[#%X,EP_ADDR:]]
107+
## Check that the dynamic relocation at .init and .init_array were not patched
108+
# CHECK-NO-INIT-EP: Dynamic section at offset {{.*}} contains {{.*}} entries:
109+
# CHECK-NO-INIT-EP-NOT: (INIT) 0x[[#%x, EP_ADDR]]
110+
# CHECK-NO-INIT-EP-NOT: (INIT_ARRAY) 0x[[#%x, EP_ADDR]]
111+
## Check that the new entry point address points to __bolt_runtime_start
112+
# CHECK-NO-INIT-EP: Symbol table '.symtab' contains {{.*}} entries:
113+
# CHECK-NO-INIT-EP: {{0+}}[[#%x, EP_ADDR]] {{.*}} __bolt_runtime_start
114+
115+
## Check that DT_INIT is set to __bolt_runtime_start for shared library with DT_INIT
116+
# CHECK-SHARED-INIT: Dynamic section at offset {{.*}} contains {{.*}} entries:
117+
# CHECK-SHARED-INIT-DAG: (INIT) 0x[[#%x, INIT:]]
118+
# CHECK-SHARED-INIT-DAG: (INIT_ARRAY) 0x[[#%x, INIT_ARRAY:]]
119+
## Check that the dynamic relocation at .init_array was not patched
120+
# CHECK-SHARED-INIT: Relocation section '.rela.dyn' at offset {{.*}} contains {{.*}} entries
121+
# CHECK-SHARED-INIT-NOT: {{0+}}[[#%x, INIT_ARRAY]] {{.*}} R_AARCH64_ABS64 {{0+}}[[#%x, INIT]]
122+
## Check that dynamic section DT_INIT points to __bolt_runtime_start
123+
# CHECK-SHARED-INIT: Symbol table '.symtab' contains {{.*}} entries:
124+
# CHECK-SHARED-INIT: {{0+}}[[#%x, INIT]] {{.*}} __bolt_runtime_start
125+
126+
## Check that 1st entry of DT_INIT_ARRAY is set to __bolt_runtime_start for shared library without DT_INIT and
127+
## ELF header entry point not changed
128+
129+
## Check that entry point address is set to __bolt_runtime_start for PIE executable without DT_INIT
130+
# CHECK-NO-INIT-NO-EP: ELF Header:
131+
# CHECK-NO-INIT-NO-EP: Entry point address: 0x[[#%X,EP_ADDR:]]
132+
# CHECK-NO-INIT-NO-EP: Dynamic section at offset {{.*}} contains {{.*}} entries:
133+
# CHECK-NO-INIT-NO-EP-NOT: (INIT)
134+
# CHECK-NO-INIT-NO-EP: (INIT_ARRAY) 0x[[#%x,INIT_ARRAY:]]
135+
## Read the dynamic relocation from 1st entry of .init_array
136+
# CHECK-NO-INIT-NO-EP: Relocation section '.rela.dyn' at offset {{.*}} contains {{.*}} entries
137+
# CHECK-NO-INIT-NO-EP: {{0+}}[[#%x,INIT_ARRAY]] {{.*}} R_AARCH64_RELATIVE [[#%x,INIT_ADDR:]]
138+
## Check that 1st entry of .init_array points to __bolt_runtime_start
139+
# CHECK-NO-INIT-NO-EP: Symbol table '.symtab' contains {{.*}} entries:
140+
# CHECK-NO-INIT-NO-EP-DAG: {{0+}}[[#%x, EP_ADDR]] {{.*}} _start
141+
# CHECK-NO-INIT-NO-EP-DAG: {{[0-9]]*}}: {{0+}}[[#%x, INIT_ADDR]] {{.*}} __bolt_runtime_start
142+
143+
## Check that entry point address is set to __bolt_runtime_start for shared library without DT_INIT
144+
# CHECK-SHARED-NO-INIT: Dynamic section at offset {{.*}} contains {{.*}} entries:
145+
# CHECK-SHARED-NO-INIT-NOT: (INIT)
146+
# CHECK-SHARED-NO-INIT: (INIT_ARRAY) 0x[[#%x,INIT_ARRAY:]]
147+
## Read the dynamic relocation from 1st entry of .init_array
148+
# CHECK-SHARED-NO-INIT: Relocation section '.rela.dyn' at offset {{.*}} contains {{.*}} entries
149+
# CHECK-SHARED-NO-INIT: {{0+}}[[#%x, INIT_ARRAY]] {{.*}} R_AARCH64_ABS64 [[#%x,INIT_ADDR:]]
150+
## Check that 1st entry of .init_array points to __bolt_runtime_start
151+
# CHECK-SHARED-NO-INIT: Symbol table '.symtab' contains {{.*}} entries:
152+
# CHECK-SHARED-NO-INIT: {{[0-9]]*}}: {{0+}}[[#%x, INIT_ADDR]] {{.*}} __bolt_runtime_start
153+
154+
## Check that entry point address is set to __bolt_runtime_start for non-PIE executable with DT_INIT
155+
# CHECK-NO-PIE-NO-INIT-EP: ELF Header:
156+
# CHECK-NO-PIE-NO-INIT-EP: Entry point address: 0x[[#%X,EP_ADDR:]]
157+
## Check that the dynamic relocation at .init and .init_array were not patched
158+
# CHECK-NO-PIE-NO-INIT-EP: Dynamic section at offset {{.*}} contains {{.*}} entries:
159+
# CHECK-NO-PIE-NO-INIT-EP-NOT: (INIT) 0x[[#%x, EP_ADDR]]
160+
# CHECK-NO-PIE-NO-INIT-EP-NOT: (INIT_ARRAY) 0x[[#%x, EP_ADDR]]
161+
## Check that the new entry point address points to __bolt_runtime_start
162+
# CHECK-NO-PIE-NO-INIT-EP: Symbol table '.symtab' contains {{.*}} entries:
163+
# CHECK-NO-PIE-NO-INIT-EP: {{0+}}[[#%x, EP_ADDR]] {{.*}} __bolt_runtime_start
164+
165+
.globl _start
166+
.type _start, %function
167+
_start:
168+
# Dummy relocation to force relocation mode.
169+
.reloc 0, R_AARCH64_NONE
170+
ret
171+
.size _start, .-_start
172+
173+
.globl _init
174+
.type _init, %function
175+
_init:
176+
ret
177+
.size _init, .-_init
178+
179+
.globl _fini
180+
.type _fini, %function
181+
_fini:
182+
ret
183+
.size _fini, .-_fini
184+
185+
.section .init_array,"aw"
186+
.align 3
187+
.dword _init
188+
189+
.section .fini_array,"aw"
190+
.align 3
191+
.dword _fini

0 commit comments

Comments
 (0)