-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathL11.tex
340 lines (284 loc) · 10.5 KB
/
L11.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
\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} }
\lstdefinelanguage{JavaScript}{
keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
keywordstyle=\color{blue}\bfseries,
ndkeywords={class, export, boolean, throw, implements, import, this},
ndkeywordstyle=\color{darkgray}\bfseries,
identifierstyle=\color{black},
sensitive=false,
comment=[l]{//},
morecomment=[s]{/*}{*/},
commentstyle=\color{purple}\ttfamily,
stringstyle=\color{red}\ttfamily,
morestring=[b]',
morestring=[b]''
}
\begin{document}
\lecture{11 --- January 28, 2015}{Winter 2015}{Patrick Lam}{version 1}
Assertions are a key ingredient in writing tests, so I thought I would
explicitly describe them.
\begin{defn}
An \emph{assertion} contains an expression that is
supposed to evaluate to {\tt true}.
\end{defn}
For instance, if we have a linked list, then the doubly-linked list
property states that {\tt prev} is the inverse of {\tt next}, i.e. for
nodes {\tt n}, we have {\tt n.next.prev == n}. After inserting a node,
one might expect this property to be true of that node (possibly with
caveats, for instance about the end of the list.)
\begin{center}
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=1cm,
semithick,initial text=]
\node[circle,draw,initial] (0) {};
\node[circle,draw] (1) [right of=0] {};
\path (0) edge [bend left] node {} (1)
(1) edge [bend left] node {} (0);
\end{tikzpicture}
\end{center}
We use assertions in unit tests to say what's supposed to be true.
\paragraph{Preconditions and postconditions.}
More generally, we can express
what is supposed to be true upon entry \& exit from a method.
We saw this code in Linux:
\begin{lstlisting}
/* LOCKING: caller. */
void ata_dev_select(...) { ...}
\end{lstlisting}
This expresses an assertion (although not written as a program statement) that the lock is held upon entry.
\paragraph{Assume/Guarantee Reasoning.}
Why would you use preconditions and postconditions? Reasoning about programs is difficult,
and preconditions and postconditions simplifies reasoning.
When reasoning about the callee, you get to assume that the precondition holds upon entry.
When reasoning about the caller, you get to guarantee the precondition holds before the call.
The reverse holds about the postcondition.
\subsection*{aComment}
We talked about the aComment approach for locking-related annotations.
In particular, it:
\squishlist
\item extracts locking-related annotations from code;
\item extracts locking-related annotations from comments; and
\item propagates annotations to callers.
\squishend
\subsection*{Tools}
To provide some context, consider the OS X Mavericks goto fail bug:
{ \scriptsize
\begin{lstlisting}[language=C]
if ((err = SSLHashSHA1.update(&hashCtx, &serverRandom)) != 0)
goto fail;
if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
goto fail;
goto fail; /* MISTAKE! THIS LINE SHOULD NOT BE HERE */
if ((err = SSLHashSHA1.final(&hashCtx, &hashOut)) != 0)
goto fail;
err = sslRawVerify(...);
fail:
return err;
\end{lstlisting}
}
The bug:\\
{\scriptsize \url{opensource.apple.com/source/Security/Security-55471/libsecurity_ssl/lib/sslKeyExchange.c} }
No bug:\\
{\scriptsize \url{www.opensource.apple.com/source/Security/Security-55179.13/libsecurity_ssl/lib/sslKeyExchange.c} }
A writeup:\\ {\scriptsize \url{nakedsecurity.sophos.com/2014/02/24/anatomy-of-a-goto-fail-apples-ssl-bug-explained-plus-an-unofficial-patch/}}
\paragraph{Detecting goto fail.}
In retrospect, a number of tools could've found this bug:
\squishlist
\item compiler {\tt -Wunreachable-code} option
\item PC-Lint:warning 539: Did not expect positive indentation
\item PVS-Studio:V640: Logic does not match formatting
\squishend
The problem is that these tools also report many other issues, and live issues can be
buried among those other issues.
\subsection*{The Landscape of Testing and Static Analysis Tools}
Here's a survey of your options:
\squishlist
\item manual testing;
\item running a JUnit test suite, manually generated;
\item running automatically-generated tests;
\item running static analysis tools.
\squishend
We'll examine several points on this continuum today.
More on this later (Lecture 23), thanks to guest lecturer. Some examples:
\squishlist
\item Coverity: a static analysis tool used by 900+ companies,
including BlackBerry, Mozilla, etc.
\item Microsoft requires Windows device drivers
to pass their Static Driver Verifier for certification.
\squishend
\subsection*{Tools for Java that you can download}
\paragraph{FindBugs.} An open-source static bytecode analyzer for Java out of
the University of Maryland.
\begin{center}
\url{findbugs.sourceforge.net}
\end{center}
It finds bug patterns:
\squishlist
\item off-by-one;
\item null pointer dereference;
\item ignored {\tt read()} return value;
\item ignored return value (immutable classes);
\item uninitialized read in constructor;
\item and more\ldots
\squishend
FindBugs gives some false positives.
Here are some techniques to help avoid them:
\begin{center}
\url{patricklam.ca/papers/14.msr.saa.pdf}
\end{center}
\paragraph{Java Path Finder (JPF), NASA.}
The key idea:
Implement a Java Virtual Machine,
but explore many thread interleavings,
looking for concurrency bugs.
\begin{center}
``JPF is an explicit state software model checker for Java\texttrademark ~bytecode.''
\end{center}
JPF can also search for deadlocks and unhandled exceptions ({\tt NullPointerException}, {\tt AssertionError}); race conditions; missing heap bounds checks; and more.
\begin{center}
\url{javapathfinder.sourceforge.net}
\end{center}
\paragraph{Korat (University of Illinois).}
Key Idea: Generate Java objects from a representation invariant specification
written as a Java method.
For instance, here's a binary tree.
\begin{tikzpicture}[->,>=stealth',shorten >=1pt,auto,node distance=1cm,
semithick,initial text=]
\node[circle,draw] (0) {};
\node[circle,draw] (1) [below left of=0] {};
\node[circle,draw] (2) [below right of=0] {};
\node[circle,draw] (3) [below left of=1] {};
\node[circle,draw] (4) [below right of=1] {};
\path (0) edge node {} (1)
(0) edge node {} (2)
(1) edge node {} (3)
(1) edge node {} (4);
\end{tikzpicture} \hspace*{2em} Binary Tree!\\[1em]
One characteristic of a binary tree:
\begin{itemize}
\item left \& right pointers don't refer to same node.
\end{itemize}
\newpage We can express that characteristic in Java as follows:
\begin{lstlisting}[language=Java]
boolean repOk() {
if (root == null) return size == 0; // empty tree has size 0
Set visited = new HashSet(); visited.add(root);
List workList = new LinkedList(); workList.add(root);
while (!workList.isEmpty()) {
Node current = (Node)workList.removeFirst();
if (current.left != null) {
if (!visited.add(current.left)) return false; // acyclicity
workList.add(current.left);
}
if (current.right != null) {
if (!visited.add(current.right)) return false; // acyclicity
workList.add(current.right);
}
}
if (visited.size() != size) return false; // consistency of size
return true;
}
\end{lstlisting}
Korat then generates all distinct (``non-isomorphic'') trees,
up to a given size (say 3).
It uses these trees as inputs for testing
the {\tt add()} method of the tree (or for any other methods.)
\begin{center}
\url{korat.sourceforge.net/index.html}
\end{center}
\paragraph{Randoop (MIT).}
Key Idea:
``Writing tests is a difficult and time-consuming activity,
and yet it is a crucial part of good software engineering.
Randoop automatically generates unit tests for Java classes.''
Randoop generates random sequence of method calls,
looking for object contract violations.
To use it, simply point it at a program \& let it run.
Randoop discards bad method sequences
(e.g. illegal argument exceptions). It remembers method sequences that create complex objects,
and sequences that result in object contract violations.\
\begin{center}
\url{code.google.com/p/randoop/}
\end{center}
Here is an example generated by Randoop:
{\scriptsize
\begin{lstlisting}[language=Java]
public static void test1() {
LinkedList list = new LinkedList();
Object o1 = new Object();
list.addFirst(o1);
TreeSet t1 = new TreeSet(list);
Set s1 = Collections.synchronizedSet(t1);
// violated in the Java standard library!
Assert.assertTrue(s1.equals(s1));
}
\end{lstlisting}
}
\end{document}