static analysis - university of washington · what is practical static analysis? 5 • a static...

53
Emina Torlak [email protected] CSE 403: Software Engineering, Spring 2015 courses.cs.washington.edu/courses/cse403/15sp/ Static Analysis

Upload: others

Post on 05-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Emina Torlak [email protected]

CSE 403: Software Engineering, Spring 2015courses.cs.washington.edu/courses/cse403/15sp/

Static Analysis

Page 2: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Outline

2

• What is static analysis?

• How does it work?

• Free and commercial tools

Page 3: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

whata brief introduction to static analysis

Page 4: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is static analysis?

4

• A static analysis tool S analyzes the source code of a program P to determine whether it satisfies a property φ, such as

• “P never deferences a null pointer”

• “P does not leak file handles”

• “No cast in P will lead to a ClassCastException”

• …

Page 5: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is static analysis?

4

• A static analysis tool S analyzes the source code of a program P to determine whether it satisfies a property φ, such as

• “P never deferences a null pointer”

• “P does not leak file handles”

• “No cast in P will lead to a ClassCastException”

• …

But it is impossible to write such a tool! For any nontrivial property φ, there is no general automated method to determine whether P satisfies φ (Rice’s theorem).

Page 6: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is static analysis?

4

• A static analysis tool S analyzes the source code of a program P to determine whether it satisfies a property φ, such as

• “P never deferences a null pointer”

• “P does not leak file handles”

• “No cast in P will lead to a ClassCastException”

• …

But it is impossible to write such a tool! For any nontrivial property φ, there is no general automated method to determine whether P satisfies φ (Rice’s theorem).

So, why are we having this lecture?

Page 7: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is practical static analysis?

5

Page 8: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is practical static analysis?

5

• A static analysis tool S analyzes the source code of a program P to determine whether it satisfies a property φ, but it can be wrong in one of two ways:

Page 9: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is practical static analysis?

5

• A static analysis tool S analyzes the source code of a program P to determine whether it satisfies a property φ, but it can be wrong in one of two ways:

• If S is sound, it will never miss any violations, but it may say that P violates φ even though it doesn’t (resulting in false positives).

Page 10: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is practical static analysis?

5

• A static analysis tool S analyzes the source code of a program P to determine whether it satisfies a property φ, but it can be wrong in one of two ways:

• If S is sound, it will never miss any violations, but it may say that P violates φ even though it doesn’t (resulting in false positives).

• If S is complete, it will never report false positives, but it may miss real violations of φ (resulting in false negatives).

Page 11: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

What is practical static analysis?

5

• A static analysis tool S analyzes the source code of a program P to determine whether it satisfies a property φ, but it can be wrong in one of two ways:

• If S is sound, it will never miss any violations, but it may say that P violates φ even though it doesn’t (resulting in false positives).

• If S is complete, it will never report false positives, but it may miss real violations of φ (resulting in false negatives).

What is a trivial way to implement a sound analysis? A complete analysis?

Page 12: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Soundness vs completeness

6

sound (overapproximate) analysis

possible program behaviors

complete (underapproximate) analysis

Page 13: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Applications of static analysis

7

• Compilers (sound)

• type checking, liveness analysis, alias analysis, …

• Bug finding (usually complete)

• Verification (sound)

Page 14: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

howstatic analysis by example

Page 15: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: find a computation’s sign

9

Page 16: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: find a computation’s sign

9

• Given a program P, determine the sign (positive, negative, or zero) of all of its variables.

Page 17: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: find a computation’s sign

9

• Given a program P, determine the sign (positive, negative, or zero) of all of its variables.

• Applications:

• Check for division by 0

• Optimize by storing + variables as unsigned integers

• Check for negative array indices

• …

Page 18: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

10

concrete domain of ints abstract domain of signs

positive ints

negative ints

zero

x = 5

x = -5

x = 0

x = b ? -1 : 1

x = y / 0

Page 19: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

10

concrete domain of ints abstract domain of signs

positive ints

negative ints

zero

x = 5

x = -5

x = 0

x = b ? -1 : 1

x = y / 0

Page 20: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

10

concrete domain of ints abstract domain of signs

positive ints

negative ints

zero

x = 5

x = -5

x = 0

x = b ? -1 : 1

x = y / 0

Page 21: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

10

concrete domain of ints abstract domain of signs

positive ints

negative ints

zero

all ints (unknown)

x = 5

x = -5

x = 0

x = b ? -1 : 1

x = y / 0

Page 22: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

10

concrete domain of ints abstract domain of signs

positive ints

negative ints

zero

all ints (unknown)

x = 5

x = -5

x = 0

x = b ? -1 : 1

x = y / 0

Page 23: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

10

concrete domain of ints abstract domain of signs

positive ints

negative ints

zero

all ints (unknown)

no ints (undefined)

x = 5

x = -5

x = 0

x = b ? -1 : 1

x = y / 0

Page 24: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

11

⊕⊖ ⊚

⊥ {}

{0} {i | i > 0}{i | i < 0}

Z

Page 25: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: abstraction

11

⊕⊖ ⊚

⊥ {}

{0} {i | i > 0}{i | i < 0}

Z

⊆ ⊆ ⊆

⊆⊆⊆

Page 26: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: transfer functions

12

• Transfer functions specify how to evaluate program expressions on abstract values.

• ⊕ + ⊕ = ⊕

• ⊖ + ⊖ = ⊖

• ⊚ + ⊚ = ⊚

• ⊕ + ⊖ = ⊤

• ⊤ / ⊚ = ⊥

• …

Page 27: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: transfer functions

12

• Transfer functions specify how to evaluate program expressions on abstract values.

• ⊕ + ⊕ = ⊕

• ⊖ + ⊖ = ⊖

• ⊚ + ⊚ = ⊚

• ⊕ + ⊖ = ⊤

• ⊤ / ⊚ = ⊥

• …

Page 28: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: transfer functions

12

• Transfer functions specify how to evaluate program expressions on abstract values.

• ⊕ + ⊕ = ⊕

• ⊖ + ⊖ = ⊖

• ⊚ + ⊚ = ⊚

• ⊕ + ⊖ = ⊤

• ⊤ / ⊚ = ⊥

• …

Page 29: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: transfer functions

12

• Transfer functions specify how to evaluate program expressions on abstract values.

• ⊕ + ⊕ = ⊕

• ⊖ + ⊖ = ⊖

• ⊚ + ⊚ = ⊚

• ⊕ + ⊖ = ⊤

• ⊤ / ⊚ = ⊥

• …

Page 30: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: transfer functions

12

• Transfer functions specify how to evaluate program expressions on abstract values.

• ⊕ + ⊕ = ⊕

• ⊖ + ⊖ = ⊖

• ⊚ + ⊚ = ⊚

• ⊕ + ⊖ = ⊤

• ⊤ / ⊚ = ⊥

• …

Page 31: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: transfer functions

12

• Transfer functions specify how to evaluate program expressions on abstract values.

• ⊕ + ⊕ = ⊕

• ⊖ + ⊖ = ⊖

• ⊚ + ⊚ = ⊚

• ⊕ + ⊖ = ⊤

• ⊤ / ⊚ = ⊥

• …

Page 32: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

Page 33: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

a = ⊕;

Page 34: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

a = ⊕;b = ⊖;

Page 35: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊖;

Page 36: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊖;d = ⊚;

Page 37: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊖;d = ⊚;e = ⊚;

Page 38: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊖;d = ⊚;e = ⊚;f = ⊥;

Page 39: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

A toy static analysis: an example

13

a = 5; b = -3; c = a * b; d = 0; e = c * d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊖;d = ⊚;e = ⊚;f = ⊥;

Detected division by zero! Just look for variables that the analysis maps to ⊥.

Page 40: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

A toy static analysis: another example

14

Page 41: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

a = ⊕;

A toy static analysis: another example

14

Page 42: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

a = ⊕;b = ⊖;

A toy static analysis: another example

14

Page 43: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊤;

A toy static analysis: another example

14

Page 44: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊤;d = ⊚;

A toy static analysis: another example

14

Page 45: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊤;d = ⊚;e = ⊤;

A toy static analysis: another example

14

Page 46: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊤;d = ⊚;e = ⊤;f = ⊤;

A toy static analysis: another example

14

Page 47: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

a = 5; b = -3; c = a + b; d = 0; e = c - d; f = 10 / e;

a = ⊕;b = ⊖;c = ⊤;d = ⊚;e = ⊤;f = ⊤;

A toy static analysis: another example

14

False positive! This program can never throw an error, but the analysis reports that f may contain any value (including undefined).

Page 48: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

toolsstate-of-the-art static analysis tools

Page 49: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Some state-of-the-art static analysis tools

16

• Astree

• Coverity

• Java PathFinder

• …

Page 50: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Astree (sound)

17

• Proves the absence of runtime errors and undefined behavior in C programs.

• Used to prove absence of runtime errors in

• Airbus flight control software

• Docking software for the International Space Station

• Many man-years of effort (since 2001) to develop.

• See www.astree.ens.fr/

Page 51: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Coverity (neither sound nor complete)

18

• Looks for bugs in C, C++, Java, and C#.

• Used by

• >1100 companies.

• NASA JPL (in addition to many other tools).

• Offered as a free, cloud-based service for open-source projects.

• See www.coverity.com

Page 52: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Java PathFinder (sound but can be imprecise)

19

• Finds bugs in mission-critical Java code.

• Developed by NASA.

• Focuses on concurrency errors (race conditions), uncaught exceptions.

• Free and open source!

• See babelfish.arc.nasa.gov/trac/jpf

Page 53: Static Analysis - University of Washington · What is practical static analysis? 5 • A static analysis tool S analyzes the source code of a program P to determine whether it satisfies

Summary

20

• Static analysis tools check if a program P satisfies a property φ by

• (sound) overapproximation of P

• (complete) underapproximation of P

• Many uses from compilers to bug finding to verification.

• Many high-quality tools available.