-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathL14-slides.tex
623 lines (540 loc) · 13.9 KB
/
L14-slides.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
\documentclass{beamer}
\usetheme{Boadilla}
%\includeonlyframes{current}
\usepackage{times}
\usefonttheme{structurebold}
\usepackage{listings}
\usepackage{ragged2e}
\usepackage{pgf}
\usepackage{tikz}
\usepackage{alltt}
\usepackage[normalem]{ulem}
\usetikzlibrary{arrows}
\usetikzlibrary{automata}
\usetikzlibrary{shapes}
\usepackage{amsmath,amssymb}
\usepackage{rotating}
\usepackage{ulem}
\usepackage{listings}
\usepackage{enumerate}
\usepackage{tikz}
\tikzset{
every overlay node/.style={
draw=black,fill=white,rounded corners,anchor=north west,
},
}
\def\tikzoverlay{%
\tikz[baseline,overlay]\node[every overlay node]
}%
%\setbeamercovered{dynamic}
\setbeamertemplate{footline}[page number]{}
\setbeamertemplate{navigation symbols}{}
\usefonttheme{structurebold}
\title{Software Testing, Quality Assurance \& Maintenance---Lecture 14}
\author{Patrick Lam}
\date{February 4, 2015}
\colorlet{redshaded}{red!25!bg}
\colorlet{shaded}{black!25!bg}
\colorlet{shadedshaded}{black!10!bg}
\colorlet{blackshaded}{black!40!bg}
\colorlet{darkred}{red!80!black}
\colorlet{darkblue}{blue!80!black}
\colorlet{darkgreen}{green!80!black}
\newcommand{\rot}[1]{\rotatebox{90}{\mbox{#1}}}
\newcommand{\gray}[1]{\mbox{#1}}
\newenvironment{changemargin}[1]{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\setlength{\leftmargin}{#1}%
\setlength{\rightmargin}{1em}
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}
\lstset{ %
language=C++,
basicstyle=\ttfamily,commentstyle=\scriptsize\itshape,showstringspaces=false,breaklines=true}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{Last Time}
\begin{changemargin}{2cm}
MAY-belifes versus MUST-beliefs\\[1em]
Cross-checking beliefs
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Today}
\begin{changemargin}{2cm}
Inferring beliefs via statistics\\
Linters\\
\end{changemargin}
\end{frame}
\part{Inferring beliefs}
\frame{\partpage}
\begin{frame}[fragile]
\frametitle{Redundancy Checking}
\begin{changemargin}{2cm}
Assumption: code ought to do something\\[1em]
Look for identity operations, e.g.\\
\hspace*{4em} {\tt x = x}, ~ {\tt 1 * y}, ~ {\tt x \& x}, ~ {\tt x | x}.\\
\end{changemargin}
{\small
\begin{lstlisting}[language=C]
/* 2.4.5-ac8/net/appletalk/aarp.c */
da.s_node = sa.s_node;
da.s_net = da.s_net;
\end{lstlisting}
}
\begin{changemargin}{2cm}
Also look for unread writes:\\
\end{changemargin}
{\small \begin{lstlisting}[language=C]
for (entry=priv->lec_arp_tables[i];
entry != NULL; entry=next) {
next = entry->next; // never read!
...
}
\end{lstlisting}
}
\begin{changemargin}{2cm}
Redundancy suggests conceptual confusion.\\
\small \hfill (examples courtesy Dawson Engler)
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{From MUST to MAY}
\begin{changemargin}{2cm}
Preceding examples were about MUST beliefs:\\
\hspace*{2em} violations were clearly wrong.\\[1em]
Let's examine MAY beliefs next:\\
\begin{itemize}
\item need more evidence of wrongdoing.
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Verifying MAY beliefs}
\begin{changemargin}{1.5cm}
\begin{enumerate}
\item Record every successful MAY-belief check as ``check''.\\[1em]
\item Record every unsucessful belief check as ``error''.\\[1em]
\item Rank errors based on ``check'' : ``error'' ratio.
\end{enumerate}~\\[1.5em]
Most likely errors: ``check'' is large, ``error'' small.
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{Let's find some MAY beliefs}
\begin{changemargin}{2cm}
use-after-free:
\begin{lstlisting}[language=C]
free(p);
print(*p);
\end{lstlisting}
~\\[1em]
That is a MUST-belief. \\[1em]
However, other resources are freed by custom (undocumented) free functions.\\[1em]
Let's derive them behaviourally.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Finding custom free functions}
\begin{changemargin}{2cm}
Key idea:\\
If pointer {\tt p} not used after calling {\tt foo(p)},\\
then derive a MAY belief that {\tt foo(p)} frees {\tt p}.\\[2em]
Just assume all functions free all arguments.
\begin{itemize}
\item emit ``check'' at every call site;
\item emit ``error'' at every use.
\end{itemize}
(in reality, filter functions with suggestive names).
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Example: finding free functions}
\begin{changemargin}{2cm}
Putting that into practice, \\
we might observe:\\[1em]
\begin{tabular}{l|l|l|l|l|l}
\begin{minipage}{3em}
foo(p)
*p = x;
\end{minipage} &
\begin{minipage}{3em}
foo(p)
*p = x;
\end{minipage} &
\begin{minipage}{3em}
foo(p)
*p = x;
\end{minipage} &
\begin{minipage}{3em}
bar(p)
p = 0;
\end{minipage} &
\begin{minipage}{3em}
bar(p)
p=0;
\end{minipage} &
\begin{minipage}{3em}
bar(p)
*p = x;
\end{minipage}
\end{tabular}
~\\[1em]
Rank {\tt bar}'s error first.\\
Sample results: 23 free errors, 11 false positives.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{More statistical techniques: nullness checks}
\begin{changemargin}{2cm}
Situation: \\[1em]
\hspace*{1em} Want to know which routines may return {\tt NULL}.\\[1em]
Possible solution: static analysis to find out.\\[1em]
Problems:
\begin{itemize}
\item difficult to know statically (``{\tt return p->next;}''?)
\item get false positives: \\ functions return {\tt NULL} under special cases only.
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Applying a statistical technique to nullness checks}
\begin{changemargin}{2cm}
Instead: let's observe what the programmer does.\\
Again, rank errors based on checks vs non-checks.\\[2em]
Just assume {\bf all} functions can return {\tt NULL}.
\begin{itemize}
\item pointer checked before use: emit ``check'';
\item pointer used before check: emit ``error''.
\end{itemize}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Example: finding NULL-returning functions}
\begin{changemargin}{2cm}
This time,
we might observe:\\[1em]
\end{changemargin}
\begin{tabular}{l|l|l|l}
\begin{minipage}{5em}
p = bar(...);\\
*p = x;
\end{minipage} &
\begin{minipage}{7em}
p = bar(...);\\
if (!p) return;\\
*p = x;
\end{minipage} &\begin{minipage}{7em}
p = bar(...);\\
if (!p) return;\\
*p = x;
\end{minipage} &\begin{minipage}{7em}
p = bar(...);\\
if (!p) return;\\
*p = x;
\end{minipage}
\end{tabular}\\[1em]
\begin{changemargin}{2cm}
Sort errors based on ``check'':``error'' ratio.\\
Sample results: 152 free errors, 16 false positives.
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{General statistical technique}
\begin{changemargin}{2cm}
``a(); \ldots b();'' implies MAY-belief that a() followed by b().\\
(is it real or fantasy? we don't know!)\\[2em]
Algorithm:
\begin{itemize}
\item assume every {\tt a}--{\tt b} is a valid pair;
\item emit ``check'' for each path with ``a()'' and then ``b()'';
\item emit ``error'' for each path with ``a()'' and no ``b()''.
\end{itemize}
(actually, prefilter functions that look paired).
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Example: general technique}
\begin{changemargin}{2cm}
Consider:\\[1em]
\end{changemargin}
\begin{tabular}{l|l|l}
\begin{minipage}{10em}
foo(p, \ldots);\\
bar(p, \ldots); // check
\end{minipage} &
\begin{minipage}{10em}
foo(p, \ldots);\\
bar(p, \ldots); // check
\end{minipage} &
\begin{minipage}{12em}
foo(p, \ldots);\\
// error: foo, no bar!
\end{minipage}
\end{tabular}
\end{frame}
\begin{frame}[fragile]
\frametitle{Application: course project}
\begin{tabular}{ll}
\begin{minipage}{10em}
\scriptsize
\begin{lstlisting}
void scope1() {
A(); B(); C(); D();
}
void scope2() {
A(); C(); D();
}
void scope3() {
A(); B();
}
void scope4() {
B(); D(); scope1();
}
void scope5() {
B(); D(); A();
}
void scope6() {
B(); D();
}
\end{lstlisting}
\end{minipage} &\begin{minipage}{18em}
``A() and B() must be paired'':\\
either A() then B() or B() then A().\\[2em]
\begin{tabbing}
{\bf Support} = \= \# times a pair of functions \\
\> appears together.\\
\> \hspace*{2em} support(\{A,B\})=3
\end{tabbing}
~\\[1em]
{\bf Confidence(\{A,B\},\{A\}}) = \\ \hspace*{2em} support(\{A,B\})/support(\{A\}) = 3/4
\end{minipage}
\end{tabular}
\end{frame}
\begin{frame}[fragile]
\frametitle{Application: course project}
\begin{tabular}{ll}
\begin{minipage}{10em}
\scriptsize
\begin{lstlisting}
void scope1() {
A(); B(); C(); D();
}
void scope2() {
A(); C(); D();
}
void scope3() {
A(); B();
}
void scope4() {
B(); D(); scope1();
}
void scope5() {
B(); D(); A();
}
void scope6() {
B(); D();
}
\end{lstlisting}
\end{minipage} &\begin{minipage}{18em}
Sample output for support threshold~3, confidence threshold 65\% (intra-procedural analysis):
\small
\begin{itemize}
\item bug:A in scope2, pair: (A B), support:~3, confidence: 75.00\%
\item bug:A in scope3, pair: (A D), support:~3, confidence: 75.00\%
\item bug:B in scope3, pair: (B D), support:~4, confidence: 80.00\%
\item bug:D in scope2, pair: (B D), support:~4, confidence: 80.00\%
\end{itemize}
\end{minipage}
\end{tabular}
\end{frame}
\begin{frame}[fragile]
\frametitle{Why are we doing this again?}
\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}
\begin{changemargin}{1cm}
\Large
Problem: lock() and unlock() must be paired!
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Summary: Belief Analysis}
\begin{changemargin}{2cm}
We don't know what the right spec is.\\
Instead, look for contradictions.\\[2em]
MUST-beliefs: contradictions = errors!\\[1em]
MAY-beliefs: pretend they're MUST, rank by confidence.\\[2em]
(Key assumption: most of the code is correct.)
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Further references}
Dawson~R. Engler, David Yu Chen, Seth Hallem, Andy Chou and Benjamin Chelf.\\
``Bugs as Deviant Behaviors: A general approach to inferring errors in systems code''.\\
In SOSP '01.\\[1em]
Dawson~R. Engler, Benjamin Chelf, Andy Chou, and Seth Hallem.\\
``Checking system rules using system-specific, programmer-written
compiler extensions''.\\
In OSDI '00 (best paper).\\
\url{www.stanford.edu/~engler/mc-osdi.pdf}\\[1em]
Junfeng Yang, Can Sar and Dawson Engler.\\
``eXplode: a Lightweight, General system for Finding Serious Storage System Errors''.\\
In OSDI'06.\\
\url{www.stanford.edu/~engler/explode-osdi06.pdf}
\end{frame}
\part{Using Linters}
\begin{frame}
\partpage
\small
source: \url{jamie-wong.com/2015/02/02/linters-as-invariants/}
\end{frame}
\begin{frame}[fragile]
\frametitle{First there was C}
\begin{changemargin}{2cm}
Statically-typed languages like C:
\begin{lstlisting}[language=C]
#include <stdio.h>
int main() {
printf("%d\n", num);
return 0;
}
\end{lstlisting}
Compiler saves you from yourself.\\[1em]
Guaranteed invariant:\\
\hspace*{2em} ``if code compiles, all symbols resolve''.
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{Less-nice languages}
\begin{changemargin}{2cm}
OK, so you try to run that in JavaScript and it crashes right away.\\[1em]
Invariant:\\
\hspace*{2em} ``if code runs, all symbols resolve''?
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{Counterexample}
\begin{changemargin}{2cm}
But what about this:
\begin{lstlisting}
function main(x) {
if (x) {
console.log("Yay");
} else {
console.log(num);
}
}
main(true);
\end{lstlisting}
Nope!
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{Still no invariants}
\begin{changemargin}{2cm}
Invariant:\\
\hspace*{2em} ``if code runs without crashing, all symbols referenced in the code path executed resolve''?
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{Nope}
\begin{changemargin}{2cm}
\begin{lstlisting}
function main() {
try {
console.log(num);
} catch (err) {
console.log("nothing to see here");
}
}
main();
\end{lstlisting}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{JavaScriptWorld Problems}
\begin{changemargin}{2cm}
When maintaining old code:
\begin{itemize}
\item is this variable defined?
\item is this variable always defined?
\item do I need to load a script to define that variable?
\end{itemize}
~\\[1em]
Why is this the developer's problem?
\end{changemargin}
\end{frame}
\begin{frame}[fragile]
\frametitle{Solution: Linters}
\begin{changemargin}{0cm}
\begin{lstlisting}
plam@banach ~> cat foo.js
function main(x) {
if (x) {
console.log("Yay");
} else {
console.log(num);
}
}
main(true);
plam@banach ~> nodejs /usr/local/lib/node_modules/jshint/bin/jshint --config jshintrc foo.js
foo.js: line 5, col 17, 'num' is not defined.
1 error
\end{lstlisting}
\end{changemargin}
\end{frame}
\begin{frame}
\frametitle{Invariant}
\begin{center}
``If code passes JSHint, all top-level symbols resolve.''
\end{center}
\end{frame}
\begin{frame}
\frametitle{Strengthening the Invariant}
\begin{changemargin}{2cm}
Add a pre-commit hook.
\end{changemargin}
\begin{center}
``If code is checked-in and commit hook ran,\\ all top-level symbols resolve.''
\end{center}
\end{frame}
\begin{frame}
\frametitle{Better yet\ldots}
\begin{changemargin}{2cm}
Block deploys on test failures.
\end{changemargin}
\begin{center}
``If code is deployed,\\ all top-level symbols resolve.''
\end{center}
\end{frame}
\begin{frame}
\frametitle{Even better yet\ldots}
\begin{changemargin}{2cm}
Hard to tell if code is deployed or not.\\
Use git feature branches, merge when deployed.
\end{changemargin}
\begin{center}
``If code is in master,\\ all top-level symbols resolve.''
\end{center}
\end{frame}
\end{document}