1 of 19

Static and Dynamic Analysis

Christopher Parnin

2 of 19

Esprima for Static and Dynamic Analysis

https://www.youtube.com/watch?v=ACYZFkvq0Sk

Watching 5:00-14:30, 28:00-34:00

3 of 19

Bad Smells

Duplicated Code

  • bad because you modify one instance of duplicated code but not the others; not all versions fixed

Long Method

  • long methods are more difficult to understand; performance concerns with respect to lots of short methods are largely obsolete

Message Chains

  • a client asks an object for another object and then asks that object for another object etc.
  • Bad because client depends on the structure of the navigation

4 of 19

More Smells

Primitive Obsession

Long Parameter List

Data Clumps

Temporary Field

Refused Bequest

Divergent Change

Shotgun Surgery

Speculative Generality

Feature Envy

5 of 19

Static Analysis Tools

Coverity (http://cacm.acm.org/magazines/2010/2/69354-a-few-billion-lines-of-code-later/fulltext)

FindBugs,, PyLint, JsLint, CheckStyle

Parse Tools: Roslyn (.NET), Esprima, Eclipse JDT (Have headless parser)

6 of 19

Activity

Describe some other “bad smells” or properties you would detect?

7 of 19

Parsing JSON (White Board)

  • Tokenization

  • Grammar

  • Parser

8 of 19

Mini-Workshop

9 of 19

Data Flow Analysis

A framework for proving facts about a program.

Examines how information propagates through blocks and paths of a program.

Content: http://www.seas.harvard.edu/courses/cs252/2011sp/slides/Lec02-Dataflow.pdf,

https://www.cs.umd.edu/class/spring2014/cmsc430/lectures/lec19.pdf

10 of 19

Reaching Definitions

A definition of a variable x is a statement that may modify the value of x.

A use of a variable x is a statement that reads from x.

A definition at node k reaches node n if there is a definition-free path from k to n.

11 of 19

Def-Use/Use-Def Chains

DU-Chain: Links each def to uses it reaches

UD-Chain: Links each use to reaching defs

foo(b) {

x = 1;

if( b < 0 )

{

x = 2;

}

b = x;

}

12 of 19

13 of 19

Gen

Kill

Flow

RD

B1

entry(B)

B

entry(B) U ( ∅ - B )

entry(B)

B2

B2(X)

X

B2(X) U ( entry(B) - X )

B2(X),

entry(B)

B3

∅ U ({B2(X),entry(B)} - ∅ )

B2(X),

entry(B)

B4

B4(X)

X

B4(X) U ({B2(X),entry(B)} - X)

B4(X),

entry(B)

B5

B5(B)

B

B5(B) U {B2(X),B4(X),entry(B) - B}

B2(X), B4(X),

B5(B),

14 of 19

Worklist Algorithm

OUT[ENTRY] = empty_set;

for (each block B other than ENTRY)

OUT[B] = empty_set;

while (changes to any OUT occur)

for (each block B other than ENTRY)

{

IN[B] = ∪P a predecessor of B OUT[P]; OUT[B] = genB ∪ (IN[B] ─ killB);

}

15 of 19

Dynamic Analysis

Coverage… using instrumentation… probes

Important later for testing...performance...

Other Techniques:

Slicing

Performance, Memory Load

Taint Analysis

Random Testing

16 of 19

Random Testing

Activity: Why not just randomly generated input and see what happens?

17 of 19

Fuzzing

18 of 19

Applying Analysis to Deployment

Discuss how you might apply any of these techniques as a “Quality Gate” in a deployment pipeline.

19 of 19

  • Static Analysis
  • https://github.ncsu.edu/CSC510-Fall2014/Analysis
  • esprima video...
  • Common Properties
  • Cyclomatic Complexity, Coupling, Nested Depth
  • Bad Smells
  • Find Bugs, etc...