36
CSE507 courses.cs.washington.edu/courses/cse507/17wi/ Computer-Aided Reasoning for Software Emina Torlak [email protected] Symbolic Execution

Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

CSE507courses.cs.washington.edu/courses/cse507/17wi/

Computer-Aided Reasoning for Software

Emina Torlak [email protected]

Symbolic Execution

Page 2: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Today

2

Last lecture• Bounded verification: forward VCG for finitized programs

Today • Symbolic execution: a path-based translation

• Concolic testing

topi

cs

Page 3: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Con

fiden

ce

Cost (programmer effort, time, expertise)

The spectrum of program validation tools

3

VerificationStatic Analysis

Extended Static Checking

Ad-hoc Testing

Concolic Testing & Whitebox Fuzzing

Bounded Verification & Symbolic Execution

Page 4: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Con

fiden

ce

Cost (programmer effort, time, expertise)

The spectrum of program validation tools

3

VerificationStatic Analysis

Extended Static Checking

Ad-hoc Testing

Concolic Testing & Whitebox Fuzzing

E.g., JPF, Klee

Bounded Verification & Symbolic Execution

E.g., SAGE, Pex, CUTE, DART

Page 5: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

A brief history of symbolic execution

4

1976: A system to generate test data and symbolically execute programs (Lori Clarke)

1976: Symbolic execution and program testing (James King)

2005-present: practical symbolic execution

• Using SMT solvers

• Heuristics to control exponential explosion

• Heap modeling and reasoning about pointers

• Environment modeling

• Dealing with solver limitations

Page 6: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

Page 7: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

Page 8: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

Page 9: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

Path condition is a quantifier-free formula over the symbolic inputs that encodes all branch decisions taken so far.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

x ↦ A y ↦ B

A ≤ B

Page 10: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

Path condition is a quantifier-free formula over the symbolic inputs that encodes all branch decisions taken so far.

All paths in the program form its execution tree, in which some paths are feasible and some are infeasible.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

x ↦ A y ↦ B

A ≤ B

feasible

Page 11: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

Path condition is a quantifier-free formula over the symbolic inputs that encodes all branch decisions taken so far.

All paths in the program form its execution tree, in which some paths are feasible and some are infeasible.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

x ↦ A + B y ↦ B

x ↦ A y ↦ B

A ≤ BA > B

feasible

Page 12: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

Path condition is a quantifier-free formula over the symbolic inputs that encodes all branch decisions taken so far.

All paths in the program form its execution tree, in which some paths are feasible and some are infeasible.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

x ↦ A + B y ↦ B

x ↦ A + B y ↦ A

x ↦ A y ↦ B

A ≤ BA > B

true feasible

Page 13: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

Path condition is a quantifier-free formula over the symbolic inputs that encodes all branch decisions taken so far.

All paths in the program form its execution tree, in which some paths are feasible and some are infeasible.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

x ↦ A + B y ↦ B

x ↦ A + B y ↦ A

x ↦ B y ↦ A

x ↦ A y ↦ B

A ≤ BA > B

true

true

feasible

Page 14: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

Path condition is a quantifier-free formula over the symbolic inputs that encodes all branch decisions taken so far.

All paths in the program form its execution tree, in which some paths are feasible and some are infeasible.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

x ↦ A + B y ↦ B

x ↦ A + B y ↦ A

x ↦ B y ↦ A

x ↦ A y ↦ B

A ≤ BA > B

true

true

x ↦ B y ↦ A

B - A > 0

feasible

infeasible

Page 15: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: basic idea

5

Execute the program on symbolic values.

Symbolic state maps variables to symbolic values.

Path condition is a quantifier-free formula over the symbolic inputs that encodes all branch decisions taken so far.

All paths in the program form its execution tree, in which some paths are feasible and some are infeasible.

def f (x, y): if (x > y):

x = x + y y = x - y x = x - y if (x - y > 0):

assert false return (x, y)

x ↦ A y ↦ B

x ↦ A + B y ↦ B

x ↦ A + B y ↦ A

x ↦ B y ↦ A

x ↦ A y ↦ B

A ≤ BA > B

true

true

x ↦ B y ↦ A

x ↦ B y ↦ A

B - A ≤ 0B - A > 0

feasible

feasibleinfeasible

Page 16: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Symbolic execution: practical issues

6

Loops and recursion: infinite execution trees

Path explosion: exponentially many paths

Heap modeling: symbolic data structures and pointers

Solver limitations: dealing with complex PCs

Environment modeling: dealing with native / system / library calls

Page 17: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Loops and recursion

7

Dealing with infinite execution trees:

• Finitize paths by unrolling loops and recursion (bounded verification)

• Finitize paths by limiting the size of PCs (bounded verification)

• Use loop invariants (verification)

Page 18: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Loops and recursion

7

Dealing with infinite execution trees:

• Finitize paths by unrolling loops and recursion (bounded verification)

• Finitize paths by limiting the size of PCs (bounded verification)

• Use loop invariants (verification)

init; while (C) {

B; } assert P;

I

Page 19: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

init; assert I; makeSymbolic(targets(B)); assume I; if (C) {

B; assert I;

} else { assert P; }

Loops and recursion

7

Dealing with infinite execution trees:

• Finitize paths by unrolling loops and recursion (bounded verification)

• Finitize paths by limiting the size of PCs (bounded verification)

• Use loop invariants (verification)

init; while (C) {

B; } assert P;

I

Page 20: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

init; assert I; makeSymbolic(targets(B)); assume I; if (C) {

B; assert I;

} else { assert P; }

init; assert I; havoc targets(B); assume I; if (C) {

B; assert I;

} else { assert P; }

Loops and recursion

7

Dealing with infinite execution trees:

• Finitize paths by unrolling loops and recursion (bounded verification)

• Finitize paths by limiting the size of PCs (bounded verification)

• Use loop invariants (verification)

init; while (C) {

B; } assert P;

I

Page 21: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Path explosion

8

Achieving good coverage in the presence of exponentially many paths:

• Select next branch at random

• Select next branch based on coverage

• Interleave symbolic execution with random testing

Page 22: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Path explosion

8

Achieving good coverage in the presence of exponentially many paths:

• Select next branch at random

• Select next branch based on coverage

• Interleave symbolic execution with random testing

symbolic execution random testing interleaved execution

Page 23: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling

9

Modeling symbolic heap values and pointers

• Bit-precise memory modeling with the theory of arrays (EXE, Klee, SAGE)

• Lazy concretization (JPF)

• Concolic lazy concretization (CUTE)

Page 24: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: lazy concretization

10

class Node { int elem; Node next;

}

n = symbolic(Node); x = n.next;

Page 25: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: lazy concretization

10

class Node { int elem; Node next;

}

n = symbolic(Node); x = n.next;

n ↦ A0

elem: ?

next: ?

A0

Page 26: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: lazy concretization

10

class Node { int elem; Node next;

}

n = symbolic(Node); x = n.next;

n ↦ A0

elem: ?

next: ?

n ↦ A0x ↦ null

A0

A0.next = null

elem: ?

next: null

A0

Page 27: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: lazy concretization

10

class Node { int elem; Node next;

}

n = symbolic(Node); x = n.next;

n ↦ A0

elem: ?

next: ?

n ↦ A0x ↦ null

A0

n ↦ A0x ↦ A0

A0.next = null

elem: ?

next: null

A0elem: ?

next: A0

A0

A0.next = A0

Page 28: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: lazy concretization

10

class Node { int elem; Node next;

}

n = symbolic(Node); x = n.next;

n ↦ A0

elem: ?

next: ?

n ↦ A0x ↦ null

A0

n ↦ A0x ↦ A0

n ↦ A0x ↦ A1

A0.next = null

elem: ?

next: null

A0elem: ?

next: A1

A0elem: ?

next: ?

A1elem: ?

next: A0

A0

A0.next = A0A0.next = A1

Page 29: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: concolic testing

11

typedef struct cell { int v; struct cell *next; } cell;

int f(int v) { return 2*v + 1; }

int testme(cell *p, int x) { if (x > 0) if (p != NULL) if (f(x) == p->v) if (p->next == p) abort(); return 0; }

Page 30: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: concolic testing

11

typedef struct cell { int v; struct cell *next; } cell;

int f(int v) { return 2*v + 1; }

int testme(cell *p, int x) { if (x > 0) if (p != NULL) if (f(x) == p->v) if (p->next == p) abort(); return 0; }

p ↦ nullx ↦ 236

x > 0 ∧ p=null

Execute concretely and symbolically.

Concrete PC

Page 31: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: concolic testing

11

typedef struct cell { int v; struct cell *next; } cell;

int f(int v) { return 2*v + 1; }

int testme(cell *p, int x) { if (x > 0) if (p != NULL) if (f(x) == p->v) if (p->next == p) abort(); return 0; }

p ↦ nullx ↦ 236

x > 0 ∧ p=null

Execute concretely and symbolically.

Negate last decision and solve for new inputs.

Concrete PC

p ↦ A0x ↦ 236

x > 0 ∧ p≠null ∧ p.v ≠ 2x + 1

next: null

v: 634

A0

Page 32: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: concolic testing

11

typedef struct cell { int v; struct cell *next; } cell;

int f(int v) { return 2*v + 1; }

int testme(cell *p, int x) { if (x > 0) if (p != NULL) if (f(x) == p->v) if (p->next == p) abort(); return 0; }

p ↦ nullx ↦ 236

x > 0 ∧ p=null

Execute concretely and symbolically.

Negate last decision and solve for new inputs.

Concrete PC

p ↦ A0x ↦ 236

x > 0 ∧ p≠null ∧ p.v ≠ 2x + 1

next: null

v: 634

A0

p ↦ A0x ↦ 1

x > 0 ∧ p≠null ∧ p.v = 2x + 1 ∧ p.next ≠ p

next: null

v: 3

A0

Page 33: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Heap modeling: concolic testing

11

typedef struct cell { int v; struct cell *next; } cell;

int f(int v) { return 2*v + 1; }

int testme(cell *p, int x) { if (x > 0) if (p != NULL) if (f(x) == p->v) if (p->next == p) abort(); return 0; }

p ↦ nullx ↦ 236

x > 0 ∧ p=null

Execute concretely and symbolically.

Negate last decision and solve for new inputs.

Concrete PC

p ↦ A0x ↦ 236

x > 0 ∧ p≠null ∧ p.v ≠ 2x + 1

next: null

v: 634

A0

p ↦ A0x ↦ 1

x > 0 ∧ p≠null ∧ p.v = 2x + 1 ∧ p.next ≠ p

next: null

v: 3

A0

p ↦ A0x ↦ 1

next: A0v: 3

A0x > 0 ∧ p≠null ∧ p.v = 2x + 1 ∧ p.next = p

Page 34: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Solver limitations

12

Reducing the demands on the solver:

• On-the-fly expression simplification

• Incremental solving

• Solution caching

• Substituting concrete values for symbolic in complex PCs (CUTE)

Page 35: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Environment modeling

13

Dealing with system / native / library calls:

• Partial state concretization

• Manual models of the environment (Klee)

Page 36: Computer-Aided Reasoning for Software CSE507€¦ · E.g., JPF, Klee Bounded ... E.g., SAGE, Pex, CUTE, DART. A brief history of symbolic execution 4 1976: A system to generate test

Summary

14

Today• Practical symbolic execution and concolic testing

Next lecture• Angelic execution