-
Notifications
You must be signed in to change notification settings - Fork 0
/
ub_by_example.tex
192 lines (157 loc) · 4.1 KB
/
ub_by_example.tex
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
\documentclass{beamer}
\usetheme{Hannover}
\usepackage[utf8x]{inputenc}
\usepackage{minted}
\title{Undefined Behavior}
\subtitle{Through real world examples}
\author{Elazar Leibovich}
\institute{Lightbits Labs}
\date{\today}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\section Integer Overflow
\defverbatim[colored]\lstI{
\begin{minted}{C}
int foo (int x) {
return (x + 1) > x;
}
int main () {
printf("%d\n", (INT_MAX + 1) > INT_MAX);
printf("%d\n", foo(INT_MAX));
return 0;
}
\end{minted}
}
\begin{frame}{What does it print?}
\lstI
\end{frame}
\begin{frame}{UB is unstable}{UB can display incoherent behavior}
\lstI
\begin{block}{Output}
gcc -O2 intmax-overflow.c ; ./a.out\\
0\\
1
\end{block}
\end{frame}
\begin{frame}{UB is unstable}{UB can display incoherent behavior}
\end{frame}
\section Division by Zero
\defverbatim[colored]\lstII{
\begin{minted}{C}
void f(int msize) {
if (!msize)
msize = 1 / msize; /* Provoke a signal. */
}
\end{minted}
}
\defverbatim[colored]\lstIIAsm{
\begin{minted}{asm}
; gcc -S -fno-asynchronous-unwind-tables -o- x.c
f:
ret
\end{minted}
}
\begin{frame}{UB can delete your code}{happened to a real project}
\lstII
Source: \hyperlink{https://lists.gnupg.org/pipermail/gcrypt-devel/2012-July/001958.html}{gcrypt-devel}
\end{frame}
\begin{frame}{UB can delete your code}{happened in gcrypt}
\lstII
\lstIIAsm
\end{frame}
\begin{frame}{UB can delete your code}{happened in gcrypt}
\begin{itemize}
\item Compiler see division by 0
\item It's UB, hence can't happen
\item Optimize away to no-op
\end{itemize}
\end{frame}
\section Division Overflow
\defverbatim[colored]\lstIII{
\begin{minted}{C}
void f(int arg1, int arg2) {
int result = arg1 / arg2;
// Overflow check...
if (arg2 == -1 && arg1 < 0 && result <= 0)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
}
\end{minted}
}
\begin{frame}{Primary school math is tricky}{happened in Postgres}
\lstIII
Where is the problem?
\end{frame}
\defverbatim[colored]\lstIIIrev{
\begin{minted}{C}
void f(int arg1, int arg2) {
assert(arg2 != 0);
int result = arg1 / arg2;
// Overflow check...
if (arg2 == -1 && arg1 < 0 && result <= 0)
ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("bigint out of range")));
}
\end{minted}
}
\begin{frame}{Primary school math is tricky}{happened in Postgres}
\lstIIIrev
Better now?
\end{frame}
\defverbatim[colored]\lstIIIasm{
\begin{minted}{C}
int result = arg1 / arg2;
\end{minted}
\begin{minted}{asm}
mov %edi,%eax
cltd ; sign extend eax to edx:eax
idiv %esi ; eax = edx:eax / esi
\end{minted}
What does idiv do?
}
\begin{frame}{Division is tricky}{happened in Postgres}
\lstIIIasm
\end{frame}
\begin{frame}{Division is tricky}{happened in Postgres}
\begin{itemize}
\item Signed 8-bit integer range: $-128-127$
\item What's the result of $-128/-1$
\item UB
\item Postgres assumed it'll be rounded below 0
\end{itemize}
\end{frame}
\defverbatim\listIIIdivalg{
\begin{minted}{C}
int result = arg1 / arg2;
temp ← AX / SRC; (* Signed division *)
IF (temp > 7FH) or (temp < 80H)
(* If a positive result is greater than 7FH or a negative result is less than 80H *)
THEN #DE; (* Divide error *)
\end{minted}
}
\begin{frame}{Division is tricky}{happened in Postgres}
IDIV — Signed Divide\\
temp ← AX / SRC; (* Signed division *) \\
IF ($temp > 7FH$) or ($temp < 80H$) \\
(* If a positive result is greater than 7FH or a negative result is less than 80H *) \\
THEN \#DE; (* Divide error *)\\\\
Source: \href{https://www.felixcloutier.com/x86/idiv}{Intel SDM}
\end{frame}
\section Condition null and void
\begin{frame}{UB affects unrelated code}{happened in kernel}
Source: \hyperlink{LWN}{https://lwn.net/Articles/342330/}
\end{frame}
\section NaCI
\defverbatim\listVI{
\begin{minted}{C}
return addr & ~(uintptr_t)((1 << nap->align_boundary) - 1);
\end{minted}
}
\begin{frame}{Even competent programmers does UB}{NacI case}
Source: \hyperlink{NaCI \#245}{http://code.google.com/p/nativeclient/issues/detail?id=245}
\end{frame}
\end{document}