36
1 Program Testing and Analysis: Random and Fuzz Testing Prof. Dr. Michael Pradel Software Lab, TU Darmstadt

Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

1

Program Testing and Analysis:

Random and Fuzz Testing

Prof. Dr. Michael Pradel

Software Lab, TU Darmstadt

Page 2: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

2

Warm-up Quiz

function f(a,b) {var x;for (var i = 0; i < arguments.length; i++) {x += arguments[i];

}console.log(x);

}f(1,2,3);

What does the following code print?

Nothing6 NaN3

Page 3: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

2

Warm-up Quiz

function f(a,b) {var x;for (var i = 0; i < arguments.length; i++) {x += arguments[i];

}console.log(x);

}f(1,2,3);

What does the following code print?

Nothing6 NaN3

Page 4: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

2

Warm-up Quiz

function f(a,b) {var x;for (var i = 0; i < arguments.length; i++) {x += arguments[i];

}console.log(x);

}f(1,2,3);

What does the following code print?

Nothing6 NaN3

Array-like objectthat contains allthree arguments

Page 5: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

2

Warm-up Quiz

function f(a,b) {var x;for (var i = 0; i < arguments.length; i++) {x += arguments[i];

}console.log(x);

}f(1,2,3);

What does the following code print?

Nothing6 NaN3

Initialized to undefined

undefined + somenumber yields NaN

Page 6: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

3

Outline

� Feedback-directed random testgenerationBased on Feedback-Directed Random TestGeneration, Pacheco et al., ICSE 2007

� Adaptive random testingBased on ARTOO: Adaptive Random Testing forObject-oriented Software, Ciupa et al., ICSE 2008

� Fuzz testingBased on Fuzzing with Code Fragments, Holler et al.,USENIX Security 2012

Page 7: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

4

Adaptive Random Testing

Idea: Testing is more effective wheninputs are spread evenly over the inputdomain

� Generate candidate inputs randomly

� At every step, select input that is furthest awayfrom already tested inputs

Page 8: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

5

Spread Out Evenly?

� Initially proposed for numeric values� Distance between two values: Euclidean

distance

� Example: f(int x)

� Suppose to have tested withInteger.MAX VALUE andInteger.MIN VALUE

� Next test: 0

Page 9: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

5

Spread Out Evenly?

� Initially proposed for numeric values� Distance between two values: Euclidean

distance

� Example: f(int x)

� Suppose to have tested withInteger.MAX VALUE andInteger.MIN VALUE

� Next test: 0

Challenge:How to compute distance of objects?

Page 10: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

4

Page 11: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

7

Object Distance

� Measure how different two objects are

� Object: Primitive values, dynamictype, and non-primitive valuesrecursively referred to

dist(p, q) =combination(

elementaryDistance(p, q),

typeDistance(type(p), type(q)),

fieldDistance({dist(p.a, q.a) |

a ∈ fields(type(p) ∩ fields(type(q)))})

Page 12: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

7

Object Distance

� Measure how different two objects are

� Object: Primitive values, dynamictype, and non-primitive valuesrecursively referred to

dist(p, q) =combination(

elementaryDistance(p, q),

typeDistance(type(p), type(q)),

fieldDistance({dist(p.a, q.a) |

a ∈ fields(type(p) ∩ fields(type(q)))})

Does not requiretraversing the object

Page 13: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

7

Object Distance

� Measure how different two objects are

� Object: Primitive values, dynamictype, and non-primitive valuesrecursively referred to

dist(p, q) =combination(

elementaryDistance(p, q),

typeDistance(type(p), type(q)),

fieldDistance({dist(p.a, q.a) |

a ∈ fields(type(p) ∩ fields(type(q)))})

Recursivelydefined

Page 14: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

8

Elementary Distance

Fixed functions for each possible type:� For numbers: F (|p− q|), where F is a

monotonically non-decreasing function withF (0) = 0

� For characters: 0 if identical, C otherwise

� For booleans: 0 if identical, B otherwise

� For strings: the Levenshtein distance

� For references: 0 if identical, R if different butnone is null, V if only one of them is null

C,B,R, V ∈ N

Page 15: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

5

Page 16: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

10

Type Distance

Distance between two types

typeDistance(t, u) =

λ ∗ pathLength(t, u)

+ ν ∗∑

a∈nonShared(t,u)

weighta

� pathLength(t, u) is the minimal distance to acommon ancestor in class hierarchy

� nonShared(t, u) is the set of non-shared fields� weighta is the weight for a specific field

λ, ν ∈ N

Page 17: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

6

Page 18: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

12

Field Distance

Recursively compute distance of allshared fields

fieldDistance(p, q)

=∑

aweighta ∗ (dist(p.a, q.a))

Arithmetic mean: Avoid giving too muchweight to objects with many fields

Page 19: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

13

Algorithm for Selecting Inputs

� Global sets usedObjects and candidateObjects� Choose object for next test:

� Initialize bestDistSum = 0 and bestObj = null

� for each c ∈ candidateObjects:∗ for each u ∈ usedObjects:· distSum += dist(c, u)

∗ if distSum > bestDistSum:· bestDistSum = distSum; bestObj = c

� Remove bestObj from candidateObjects, addto usedObjects instead, and run test withbestObj

Page 20: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

14

Example

Method under test:Account.transfer(Account dst, int amount)

Pool of candidates:

� Accounts� a1: owner=”A” and balance=6782832� a2: owner=”B” and balance=10� a3: owner=”O” and balance=99� a4: null

� Integers:� i1: 100, i2: 287391, i3: 0, i4: -50

Page 21: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

15

Example (cont.)

First call:a3.transfer(a1, i2)

Second call:a1.transfer(a4, i4)

Page 22: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

16

Results

� Implemented for Eiffel

� Use randomly generated objects as candidates

� Use Eiffel’s contracts (pre- and post-conditions,class invariants) as test oracle

� Comparison with random testing:

� Find bugs with 5x fewer tests

� But: Takes 1.6x the time of random testing

Page 23: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

17

Outline

� Feedback-directed random testgenerationBased on Feedback-Directed Random TestGeneration, Pacheco et al., ICSE 2007

� Adaptive random testingBased on ARTOO: Adaptive Random Testing forObject-oriented Software, Ciupa et al., ICSE 2008

� Fuzz testingBased on Fuzzing with Code Fragments, Holler et al.,USENIX Security 2012

Page 24: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

18

Fuzz Testing

Generate random inputs

� Generative: Create new random input, possiblybased on constraints and rules

� Mutative: Derive new inputs from existing input byrandomly modifying it

Page 25: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

19

Grammar-based Language Fuzzing

Idea: Combine generative and mutativeapproach to test JavaScript interpreter

� Create random JavaScript programs based onlanguage grammar

� Use and re-combine fragments of code fromexisting corpus of programs� Corpus: Programs that have exposed bugs before

Page 26: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

20

Overview of LangFuzz

Page 27: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

21

Learning Code Fragments

� Parse existing programs into ASTs

� Extract code fragments

� Examples for non-terminals of grammar

Page 28: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

7

Page 29: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

23

Mutation of Code

� Randomly pick and parse an existingprogram

� Randomly pick some fragments andreplace with fragments from phase 1of same type

Page 30: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

8

Page 31: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

25

Generation of Code

Breadth-first application of grammarrules� Set current expansion ecur to start symbol P� Loop k iterations:

� Choose a random non-terminal n in ecur� Pick one of the rules, r, that can be applied to n� Replace occurrence of n in ecur by r(n)

After k iterations: Replace remainingnon-terminals with fragments

Page 32: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

9

Page 33: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

27

Quiz

Which of the following SIMP programscould have been generated byLangFuzz?

if B then C; C

if !x > 3 then skip; y := 1

if !x > 3 then while; while

Page 34: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

27

Quiz

Which of the following SIMP programscould have been generated byLangFuzz?

if B then C; C

if !x > 3 then skip; y := 1

if !x > 3 then while; while

(has unexpandednon-terminals)

(syntacticallyincorrect)

Page 35: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

28

Results

� Used to test Mozilla’s and Chrome’sJavaScript engines

� Found various bugsMostly crashes of engine due to memory issues

� Rewarded with $50,000 bug bounties

� First author now works at Mozilla

Page 36: Random and Fuzz Testing Program Testing and ... - Software Lab · Object-oriented Software, Ciupa et al., ICSE 2008 Fuzz testing Based on Fuzzing with Code Fragments, Holler et al.,

29

Summary

Random and fuzz testing

� Fully automated and unbiased

� Non-naive approaches can be very effective

� Trade-off: Cost of generating inputs vs.effectiveness in exposing bugs

� Quickly generated, less effective tests may bebetter than slowly generated, more effectivetests