-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathL10.tex
388 lines (329 loc) · 13.1 KB
/
L10.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{tikz}
\usepackage{enumerate}
\usepackage{url}
\usepackage{amssymb}
\usetikzlibrary{arrows,automata,shapes}
\lstset{basicstyle=\ttfamily \scriptsize,
basicstyle=\ttfamily,
columns=fullflexible,
breaklines=true,
numbers=left,
numberstyle=\scriptsize,
stepnumber=1,
mathescape=false,
tabsize=2,
showstringspaces=false
}
\newtheorem{defn}{Definition}
\newtheorem{crit}{Criterion}
\newcommand{\handout}[5]{
\noindent
\begin{center}
\framebox{
\vbox{
\hbox to 5.78in { {\bf Software Testing, Quality Assurance and Maintenance } \hfill #2 }
\vspace{4mm}
\hbox to 5.78in { {\Large \hfill #5 \hfill} }
\vspace{2mm}
\hbox to 5.78in { {\em #3 \hfill #4} }
}
}
\end{center}
\vspace*{4mm}
}
\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{#4}{Lecture #1}}
% 1-inch margins, from fullpage.sty by H.Partl, Version 2, Dec. 15, 1988.
\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 8.9in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in
\parindent 0in
\parskip 1.5ex
%\renewcommand{\baselinestretch}{1.25}
% http://gurmeet.net/2008/09/20/latex-tips-n-tricks-for-conference-papers/
\newcommand{\squishlist}{
\begin{list}{$\bullet$}
{ \setlength{\itemsep}{0pt}
\setlength{\parsep}{3pt}
\setlength{\topsep}{3pt}
\setlength{\partopsep}{0pt}
\setlength{\leftmargin}{1.5em}
\setlength{\labelwidth}{1em}
\setlength{\labelsep}{0.5em} } }
\newcommand{\squishlisttwo}{
\begin{list}{$\bullet$}
{ \setlength{\itemsep}{0pt}
\setlength{\parsep}{0pt}
\setlength{\topsep}{0pt}
\setlength{\partopsep}{0pt}
\setlength{\leftmargin}{2em}
\setlength{\labelwidth}{1.5em}
\setlength{\labelsep}{0.5em} } }
\newcommand{\squishend}{
\end{list} }
\begin{document}
\lecture{10 --- January 26, 2015}{Winter 2015}{Patrick Lam}{version 0}
We are going to completely switch gears and talk about testing concurrent
programs next. For those of you in 3A, SE 350 is the first real exposure
to these concepts; you'll see them more in CS 343. ECE 459 also teaches
you how to leverage parallelism.
\paragraph{Context: Multicores are everywhere today!}
For the past 10 years, chips have not been getting more GHz.
We still have more transistors though. Hardware manufacturers
have been sharing this bounty with us, through the magic of
multicore processors!
\begin{center}
\includegraphics[width=2em]{L10/centrino-duo}
\includegraphics[width=2em]{L10/core-2-duo}
\includegraphics[width=2em]{L10/core-2-quad}
\end{center}
If you want performance today, then you need parallelism.
The dark side is that concurrency bugs will bite you. \includegraphics[height=1em]{L10/ladybug}
\begin{center}
\begin{minipage}{.8\textwidth}
``More often than not, printing a page on my dual-G5 crashes the application. The funny thing is, printing almost never crashes on my (single-core) G4 PowerBook.''\\
\end{minipage}
\end{center} \vspace*{-2em}
{ \hfill \scriptsize \url{http://archive.oreilly.com/pub/post/dreaded_concurrency.html}}
The most famous kind of concurrency bug is the race condition. Let's look at this code.
\begin{tabular}{ll}
\begin{minipage}{.5\textwidth}
\lstinputlisting[basicstyle=\tiny]{live-coding/L10/race.C}
\end{minipage} &\begin{minipage}{.5\textwidth} When we run it:
{\small \begin{verbatim}
plam@polya /tmp> ./a.out
2⏎
plam@polya /tmp> ./a.out
2⏎
plam@polya /tmp> ./a.out
1⏎
plam@polya /tmp> ./a.out
1⏎
plam@polya /tmp> ./a.out
2⏎
plam@polya /tmp> ./a.out
2⏎
\end{verbatim} }
\end{minipage}
\end{tabular}
Yes, that's a race condition.
\begin{itemize}
\item A race occurs when you have two concurrent accesses to the
same memory location, at least one of which is a {\bf write}.
\end{itemize}
Race conditions arise between variables which are shared between
threads. Note that when there's a race, the final state may not be
the same as running one access to completion and then the other.
\paragraph{Tools to the rescue.} While races may be entertaining, race conditions are never good.
You have several dynamic analysis tools at your disposal to eradicate races, including:
\squishlist
\item Helgrind (part of Valgrind)
\item lockdep (Linux kernel)
\item Thread Analyzer (Oracle Solaris Studio)
\item Thread Analyzer (Coverity)
\item Intel Inspector XE 2011 (formerly Intel Thread Checker)
\squishend
We can run the race condition shown above under Helgrind:
{\scriptsize
\begin{verbatim}
plam@polya /tmp> g++ -std=c++11 race.C -g -pthread -o race
plam@polya /tmp> valgrind --tool=helgrind ./race
[...]
==6486== Possible data race during read of size 4 at 0x603E1C by thread #3
==6486== Locks held: none
==6486== at 0x400EA1: func() (race.C:8)
==6486== by 0x402254: void std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (functional:1732)
==6486== by 0x4021AE: std::_Bind_simple<void (*())()>::operator()() (functional:1720)
==6486== by 0x402147: std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (thread:115)
==6486== by 0x4EF196F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
==6486== by 0x4C2F056: mythread_wrapper (hg_intercepts.c:234)
==6486== by 0x56650A3: start_thread (pthread_create.c:309)
==6486== by 0x595FCCC: clone (clone.S:111)
==6486==
==6486== This conflicts with a previous write of size 4 by thread #2
==6486== Locks held: none
==6486== at 0x400EB1: func() (race.C:10)
==6486== by 0x402254: void std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (functional:1732)
==6486== by 0x4021AE: std::_Bind_simple<void (*())()>::operator()() (functional:1720)
==6486== by 0x402147: std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (thread:115)
==6486== by 0x4EF196F: ??? (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.20)
==6486== by 0x4C2F056: mythread_wrapper (hg_intercepts.c:234)
==6486== by 0x56650A3: start_thread (pthread_create.c:309)
==6486== by 0x595FCCC: clone (clone.S:111)
==6486== Address 0x603e1c is 0 bytes inside data symbol "counter"
\end{verbatim}
}
\paragraph{Not enough.} OK, great. Now you've eliminated all races (as required by specification).
Of course, there are still lots of bugs that your program might contain. Some of them are even concurrency bugs.
Here, there are no longer any contended accesses, but we cheated by caching the value. Atomic operations would
be safe.
\lstinputlisting{live-coding/L10/atomicity-violation.C}
We can test our code for additional concurrency bugs:
\begin{itemize}
\item run the code multiple times
\item add noise (sleep, more system load, etc)
\item Helgrind and friends
\item force scheduling (e.g. Java PathFinder)
\item static approaches: lock-set, happens-before, state-of-the-art techniques
\end{itemize}
\paragraph{Reentrant/recursive Locks}
What happens if you have two requests for a POSIX/C++11 lock?
If the requests are in different threads, the second thread waits for the first thread to unlock.
But, if the requests are in the same thread, that thread waits for itself to unlock\ldots forever!
To avoid this unhappy situation, we can use \emph{recursive} locks.
Each lock knows how many times its owner has locked it. The owner must
then unlock the same number of times to liberate.
Java locks work this way, e.g.
\begin{lstlisting}[language=java]
class SynchronizedIsRecursive {
int x;
synchronized void f() {
x--;
g(); // does not hang!
}
synchronized void g() {
x++;
}
}
\end{lstlisting}
Although every Java object is a lock, and we can {\tt synchronized()} over every lock,
{\tt ReentrantLocks} are more special:
\begin{itemize}
\item we can explicitly {\tt lock()} \& {\tt unlock()} them,
\item (or even {\tt trylock()})!
\end{itemize}
However, inexpertly-written Java programs might hog the lock. To avoid that,
use a {\tt try}/{\tt finally} construct:
\begin{lstlisting}[language=Java]
Lock lock = new ReentrantLock();
lock.lock();
try {
// you got the lock! workworkwork
} finally {
// might have thrown an exception
lock.unlock();
}
\end{lstlisting}
\section*{Tools for detecting lock usage issues}
The reference for the next example is Engler et al~\cite{EnglerETAL00CheckingSystemRulesUsingSystemspecificProgrammerwritten}.
This example falls short of excellence:
\begin{lstlisting}[language=C,commentstyle={\color{red}\bf}]
/* 2.4.0:drivers/sound/cmpci.c:cm_midi_release: */
lock_kernel(); // [PL: GRAB THE LOCK]
if (file->f_mode & FMODE_WRITE) {
add_wait_queue(&s->midi.owait, &wait);
...
if (file->f_flags & O_NONBLOCK) {
remove_wait_queue(&s->midi.owait, &wait);
set_current_state(TASK_RUNNING);
return -EBUSY; // [PL: OH NOES!!1]
}
...
}
unlock_kernel();
\end{lstlisting}
The problem: lock() and unlock() calls must be paired! They are on the ``happy path'', but not on the {\tt -EBUSY} path.
\cite{EnglerETAL00CheckingSystemRulesUsingSystemspecificProgrammerwritten} describes a tool that allows developers
to describe calls that must be paired.
Another example:
\begin{tabular}{l|l|l}
\begin{minipage}{.24\textwidth}
\begin{lstlisting}
foo(p, ...)
bar(p, ...);
\end{lstlisting}
\end{minipage} &
\begin{minipage}{.24\textwidth}
\begin{lstlisting}
foo(p, ...)
bar(p, ...);
\end{lstlisting}
\end{minipage} &
\begin{minipage}{.5\textwidth}
\begin{lstlisting}
foo(p, ...)
// ERROR: foo, no bar!
\end{lstlisting}
\end{minipage}
\end{tabular}
Our tool might then give the following results: 23 errors, 11 false positives. A false positive is something where
the tool reports an error, but for instance, the error is in an infeasible path.
The next challenge is: how do we find such rules? In particular, we want to find rules of the form ``A() must be followed by B()'',
or``{\tt a(); ... b();}'', which denotes a MAY-belief that a() follows b(). iComment by Lin Tan et al propose mining
comments to find them~\cite{TanETAL07IcommentBugsBadComments}.
Here is some OpenSolaris code that demonstrates expectations. Also different locking primitives.
\begin{lstlisting}[language=C,escapechar=|]
/* opensolaris/common/os/taskq.c: */
/* Assumes: tq->tq_lock is held. */
/* |\small $\hookrightarrow$ consistent \checkmark| */
static void taskq_ent_free(...) { ... }
static taskq_t
*taskq_create_common(...) { ...
// [different lock primitives below:]
mutex_enter(...);
taskq_ent_free(...); /* |\small $\leftarrow$ consistent \checkmark| */
...
}
\end{lstlisting}
Getting back to actual bugs, here is a bad comment automatically detected by iComment:
\begin{lstlisting}[language=C]
/* mozilla/security/nss/lib/ssl/sslsnce.c: */
/* Caller must hold cache lock when calling this. */
static sslSessionID * ConvertToSID(...) { ... }
static sslSessionID *ServerSessionIDLookup(...)
{
...
UnlockSet(cache, set); ...
sid = ConvertToSID(...);
...
}
\end{lstlisting}
We observe a specification in the comment at line 2, and then a usage error at line 9 where we unlock the cache
and then call {\tt ConvertToSID}. The badness of the comment was confirmed by Mozilla developers.
Issue: Comments are not updated alongside code. {\bf Bad comments can and do cause bugs.}
Here's another bad comment in the Linux kernel, also automatically detected.
\begin{lstlisting}[language=C]
// linux/drivers/ata/libata-core.c:
/* LOCKING: caller. */
void ata_dev_select(...) { ...}
int ata_dev_read_id(...) {
...
ata_dev_select(...);
...
}
\end{lstlisting}
Once again, the specification at line 3 states that the caller is to lock. But line 8 calls {\tt ata\_dev\_select()}
without holding the lock. The badness of this comment was confirmed by Linux developers.
\section*{Deadlocks}
Another concurrency problem is deadlocks. We focus on a particular form of deadlock here, which occurs when code
may get interrupted by interrupt handlers, and the code shares locks with the interrupt handler. This problem inspired
aComment~\cite{TanZhouPadioleau11AcommentMiningAnnotationsFromCommentsCode}.
\begin{center}
\includegraphics[width=.6\textwidth]{L10/lock-acq1}
\includegraphics[width=.8\textwidth]{L10/lock-acq2}
\end{center}
In particular, if the spinlock is taken by code that runs in interrupt context (either a hardware or software interrupt), then
code requesting the lock must use the spin\_lock form that disables interrupts.
Otherwise, sooner or later, the code will deadlock. \includegraphics[height=1em]{L10/look_of_disapproval}
Here's an example of the right way to do things:
\begin{lstlisting}[language=C]
spinlock_t mr_lock = SPIN_LOCK_UNLOCKED;
unsigned long flags;
spin_lock_irqsave(&mr_lock, flags);
/* critical section... */
spin_lock_irqrestore(&mr_lock, flags);
\end{lstlisting}
\squishlist
\item {\tt spin\_lock\_irqsave()} disables interrupts locally and provides spinlock on symmetric multiprocessors (SMPs).
\item {\tt spin\_lock\_irqrestore()} restores interrupts to state when lock acquired.
\squishend
This covers both interrupt and SMP concurrency issues.
\bibliographystyle{alpha}
\bibliography{L10}
\end{document}