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