]> git.proxmox.com Git - rustc.git/blame - src/compiler-rt/lib/asan/scripts/gen_asm_instrumentation.sh
Imported Upstream version 1.5.0+dfsg1
[rustc.git] / src / compiler-rt / lib / asan / scripts / gen_asm_instrumentation.sh
CommitLineData
1a4d82fc
JJ
1#!/bin/bash
2
3#===- lib/asan/scripts/gen_asm_instrumentation.sh -------------------------===#
4#
5# The LLVM Compiler Infrastructure
6#
7# This file is distributed under the University of Illinois Open Source
8# License. See LICENSE.TXT for details.
9#
10# Emit x86 instrumentation functions for asan.
11#
12#===-----------------------------------------------------------------------===#
13
14check() {
15 test $# -eq 2 || (echo "Incorrent number of arguments: $#" 1>&2 && exit 1)
16 case "$1" in
17 store) ;;
18 load) ;;
19 *) echo "Incorrect first argument: $1" 1>&2 && exit 1 ;;
20 esac
21 case "$2" in
22 [0-9]*) ;;
23 *) echo "Incorrect second argument: $2" 1>&2 && exit 1 ;;
24 esac
25}
26
27func_name() {
28 check $1 $2
29 echo "__sanitizer_sanitize_$1$2"
30}
31
32func_label() {
33 check $1 $2
34 echo ".sanitize_$1$2_done"
35}
36
37func_report() {
38 check $1 $2
39 echo "__asan_report_$1$2"
40}
41
42emit_call_report() {
43cat <<EOF
44 cld
45 emms
46 call $(func_report $1 $2)@PLT
47EOF
48}
49
50emit_stack_align() {
51cat <<EOF
52 subq \$8, %rsp
53 andq \$-16, %rsp
54EOF
55}
56
57cat <<EOF
58// This file was generated by $(basename $0). Please, do not edit
59// manually.
60EOF
61
62echo "#ifdef __linux__"
63echo ".section .text"
64
65echo "#if defined(__x86_64__) || defined(__i386__)"
66for as in 1 2 4 8 16
67do
68 for at in store load
69 do
70 echo ".globl $(func_report $at $as)"
71 done
72done
73echo "#endif // defined(__x86_64__) || defined(__i386__)"
74
75echo "#if defined(__i386__)"
76
77# Functions for i386 1-, 2- and 4-byte accesses.
78for as in 1 2 4
79do
80 for at in store load
81 do
82cat <<EOF
83// Sanitize $as-byte $at. Takes one 4-byte address as an argument on
84// stack, nothing is returned.
85.globl $(func_name $at $as)
86.type $(func_name $at $as), @function
87$(func_name $at $as):
88 pushl %ebp
89 movl %esp, %ebp
90 pushl %eax
91 pushl %ecx
92 pushl %edx
93 pushfl
94 movl 8(%ebp), %eax
95 movl %eax, %ecx
96 shrl \$0x3, %ecx
97 movb 0x20000000(%ecx), %cl
98 testb %cl, %cl
99 je $(func_label $at $as)
100 movl %eax, %edx
101 andl \$0x7, %edx
102EOF
103
104 case $as in
105 1) ;;
106 2) echo ' incl %edx' ;;
107 4) echo ' addl $0x3, %edx' ;;
108 *) echo "Incorrect access size: $as" 1>&2; exit 1 ;;
109 esac
110
111cat <<EOF
112 movsbl %cl, %ecx
113 cmpl %ecx, %edx
114 jl $(func_label $at $as)
115 pushl %eax
116$(emit_call_report $at $as)
117$(func_label $at $as):
118 popfl
119 popl %edx
120 popl %ecx
121 popl %eax
122 leave
123 ret
124EOF
125 done
126done
127
128# Functions for i386 8- and 16-byte accesses.
129for as in 8 16
130do
131 for at in store load
132 do
133cat <<EOF
134// Sanitize $as-byte $at. Takes one 4-byte address as an argument on
135// stack, nothing is returned.
136.globl $(func_name $at $as)
137.type $(func_name $at $as), @function
138$(func_name $at $as):
139 pushl %ebp
140 movl %esp, %ebp
141 pushl %eax
142 pushl %ecx
143 pushfl
144 movl 8(%ebp), %eax
145 movl %eax, %ecx
146 shrl \$0x3, %ecx
147EOF
148
149 case ${as} in
150 8) echo ' cmpb $0x0, 0x20000000(%ecx)' ;;
151 16) echo ' cmpw $0x0, 0x20000000(%ecx)' ;;
152 *) echo "Incorrect access size: ${as}" 1>&2; exit 1 ;;
153 esac
154
155cat <<EOF
156 je $(func_label $at $as)
157 pushl %eax
158$(emit_call_report $at $as)
159$(func_label $at $as):
160 popfl
161 popl %ecx
162 popl %eax
163 leave
164 ret
165EOF
166 done
167done
168
169echo "#endif // defined(__i386__)"
170
171echo "#if defined(__x86_64__)"
172
173# Functions for x86-64 1-, 2- and 4-byte accesses.
174for as in 1 2 4
175do
176 for at in store load
177 do
178cat <<EOF
179// Sanitize $as-byte $at. Takes one 8-byte address as an argument in %rdi,
180// nothing is returned.
181.globl $(func_name $at $as)
182.type $(func_name $at $as), @function
183$(func_name $at $as):
184 subq \$128, %rsp
185 pushq %rax
186 pushq %rcx
187 pushfq
188 movq %rdi, %rax
189 shrq \$0x3, %rax
190 movb 0x7fff8000(%rax), %al
191 test %al, %al
192 je $(func_label $at $as)
193 movl %edi, %ecx
194 andl \$0x7, %ecx
195EOF
196
197 case ${as} in
198 1) ;;
199 2) echo ' incl %ecx' ;;
200 4) echo ' addl $0x3, %ecx' ;;
201 *) echo "Incorrect access size: ${as}" 1>&2; exit 1 ;;
202 esac
203
204cat <<EOF
205 movsbl %al, %eax
206 cmpl %eax, %ecx
207 jl $(func_label $at $as)
208$(emit_stack_align)
209$(emit_call_report $at $as)
210$(func_label $at $as):
211 popfq
212 popq %rcx
213 popq %rax
214 addq \$128, %rsp
215 ret
216EOF
217 done
218done
219
220# Functions for x86-64 8- and 16-byte accesses.
221for as in 8 16
222do
223 for at in store load
224 do
225cat <<EOF
226// Sanitize $as-byte $at. Takes one 8-byte address as an argument in %rdi,
227// nothing is returned.
228.globl $(func_name $at $as)
229.type $(func_name $at $as), @function
230$(func_name $at $as):
231 subq \$128, %rsp
232 pushq %rax
233 pushfq
234 movq %rdi, %rax
235 shrq \$0x3, %rax
236EOF
237
238 case ${as} in
239 8) echo ' cmpb $0x0, 0x7fff8000(%rax)' ;;
240 16) echo ' cmpw $0x0, 0x7fff8000(%rax)' ;;
241 *) echo "Incorrect access size: ${as}" 1>&2; exit 1 ;;
242 esac
243
244cat <<EOF
245 je $(func_label $at $as)
246$(emit_stack_align)
247$(emit_call_report $at $as)
248$(func_label $at $as):
249 popfq
250 popq %rax
251 addq \$128, %rsp
252 ret
253EOF
254 done
255done
256echo "#endif // defined(__x86_64__)"
257
258cat <<EOF
259/* We do not need executable stack. */
260#if defined(__arm__)
261 .section .note.GNU-stack,"",%progbits
262#else
263 .section .note.GNU-stack,"",@progbits
264#endif // defined(__arm__)
265#endif // __linux__
266EOF