soot tutorial

94
7/23/2019 Soot Tutorial http://slidepdf.com/reader/full/soot-tutorial 1/94 Analyzing Java Programs with Soot Bruno Dufour based on material by: Eric Bodden,Laurie Hendren, Patrick Lam, Jennifer Lhot ´ ak, Ondˇ rej Lhot ´ ak and Feng Qian McGill University http://www.sable.mcgill.ca/soot/ Analyzing Java Programs with Soot – p. 1/89

Upload: panditankursharma

Post on 19-Feb-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 1/94

Analyzing Java Programs with Soot

Bruno Dufour

based on material by:

Eric Bodden,Laurie Hendren, Patrick Lam, Jennifer Lhotak, Ondrej Lhotak and Feng

Qian

McGill University

http://www.sable.mcgill.ca/soot/

Analyzing Java Programs with Soot – p. 1/89

Page 2: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 2/94

www.sable.mcgill.ca/soot/ 

What is Soot?

a free compiler infrastructure, written in Java(LGPL)

was originally designed to analyze andtransform Java bytecode

original motivation was to provide a common

infrastructure with which researchers couldcompare analyses (points-to analyses)

has been extended to include decompilationand visualization

Analyzing Java Programs with Soot – p. 2/89

Page 3: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 3/94

What is Soot? (2)

Soot has many potential applications:

used as a stand-alone tool (command line

or Eclipse plugin)extended to include new IRs, analyses,transformations and visualizations

as the basis of building newspecial-purpose tools

Analyzing Java Programs with Soot – p. 3/89

Page 4: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 4/94

www.sable.mcgill.ca/publications/ 

Soot: Past and Present

Started in 1996-97 with the development ofcoffi by Clark Verbrugge and some first

prototypes of  Jimple IR by Clark and RajaVallée-Rai.

First publicly-available versions of Soot 1.x

were associated with Raja’s M.Sc. thesisNew contributions and releases have beenadded by many graduate students at McGill

and research results have been the topics ofpapers and theses.

Analyzing Java Programs with Soot – p. 4/89

Page 5: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 5/94

Soot: Past and Present (2)

Soot 1.x has been used by many researchgroups for a wide variety of applications. Has

also been used in several compiler courses.Last version was 1.2.5.

Soot 2.0 and the first version of the Eclipse

Plugin were released - June 2003 - JIT forPLDI 2003.

Soot 2.3.0: Java 5 support

Soot 2.4.0: partial Reflection support

This tutorial is based on Soot 2.4.0.

Analyzing Java Programs with Soot – p. 5/89

Page 6: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 6/94

Soot Overview

source

Java

source

SML

source

Scheme

source

Eiffel

source

Java

class files

javac MLJ KAWA SmallEiffel

SOOT

Produce Jimple 3−address IR

Generate Bytecode

Interpreter JIT Adaptive Engine Ahead−of−Time

  Compiler

Optimized class files + attributes

Analyze, Optimize and Tag

Eclipse

Analyzing Java Programs with Soot – p. 6/89

Page 7: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 7/94

Soot IRs

Baf:   is a compact rep. of Bytecode (stack-based)

Jimple:   is Java’s simple, typed, 3-addr

(stackless) representationShimple:   is a SSA-version of Jimple

Grimp:   is like Jimple, but with expressionsagGRegated

Dava:  structured representation used for

Decompiling Java

Analyzing Java Programs with Soot – p. 7/89

Page 8: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 8/94

Raja’s Thesis, CASCON99, CC2000, SAS2000

Jimple

Jimple is:

principal Soot Intermediate Representation

3-address code in a control-flow graph 

a typed  intermediate representation

stackless special variables for  this and parameters

only simple statements, never nested

Analyzing Java Programs with Soot – p. 8/89

Page 9: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 9/94

Kinds of Jimple Stmts I

Core statements:NopStmt

DefinitionStmt: IdentityStmt,

AssignStmt

Intraprocedural control-flow:IfStmt

GotoStmt

TableSwitchStmt,LookupSwitchStmt

Interprocedural control-flow:InvokeStmt

ReturnStmt, ReturnVoidStmt

Analyzing Java Programs with Soot – p. 9/89

Page 10: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 10/94

Kinds of Jimple Stmts II

ThrowStmt

throws an exception

RetStmtnot used; returns from a JSR

MonitorStmt: EnterMonitorStmt,

ExitMonitorStmtmutual exclusion

Analyzing Java Programs with Soot – p. 10/89

Page 11: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 11/94

IdentityStmt

this.m();

Where’s the definition of  this?

IdentityStmt:

Used for assigning parameter values andthis

 ref to locals.Gives each local at least one definition point.

Jimple rep of  IdentityStmts:

r0 := @this;

i1 := @parameter0;

Analyzing Java Programs with Soot – p. 11/89

Page 12: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 12/94

Context: other Jimple  Stmts

public int foo(java.lang.String) { // localsr0 := @this; // IdentityStmt

r1 := @parameter0;

if r1 != null goto label0; // IfStmt

$i0 = r1.length(); // AssignStmtr1.toUpperCase(); // InvokeStmt

return $i0; // ReturnStmt

label0: // created by Printer

return 2;

}

Analyzing Java Programs with Soot – p. 12/89

Page 13: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 13/94

Converting bytecode → Jimple → bytecode

These transformations are relatively hard to design so

that they produce correct, useful and efficient code.

Worth the price, we do want a 3-addr typed IR.raw bytecode typed 3-address code (Jimple)

◦ each inst has implicit   ◦ each stmt acts explicitly

effect on stack on named variables

◦ no types for local   ◦ types for each local

variables variable

◦   > 200 kinds of insts   ◦ only 15 kinds of stmts

Analyzing Java Programs with Soot – p. 13/89

Page 14: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 14/94

Soot Data Structure Basics

Soot builds data structures to represent:

a complete environment (Scene)

classes (SootClass)Fields and Methods (SootMethod ,SootField )

bodies of Methods (come in differentflavours, corresponding to different IRlevels, ie.  JimpleBody)

These data structures are implemented usingOO techniques, and designed to be easy touse and generic where possible.

Analyzing Java Programs with Soot – p. 14/89

Page 15: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 15/94

Soot Classes

SootMethod

SootClass

JimpleBody

Scene

SootField

getMethod()

getField()

getSootClass()

Scene.v()

getSignature()

getActiveBody()

(singleton)

getSignature()

Analyzing Java Programs with Soot – p. 15/89

Page 16: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 16/94

Body-centric View

Chain

SootMethod

ChainJimpleBody

Chain

getLocals()

getActiveBody()

getTraps()getUnits()

Analyzing Java Programs with Soot – p. 16/89

Page 17: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 17/94

Getting a  UnitGraph

SootMethod

Chain

ChainJimpleBody

Chain

UnitGraph

getUnits()

new BriefUnitGraph()

getLocals()

getActiveBody()

getTraps()getUnits()

getBody()

Analyzing Java Programs with Soot – p. 17/89

Page 18: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 18/94

What to do with a  UnitGraph

getBody()

getHeads(),  getTails()

getPredsOf(u),  getSuccsOf(u)

getExtendedBasicBlockPathBetween

(from, to)

Analyzing Java Programs with Soot – p. 18/89

Page 19: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 19/94

Control-flow units

We create an OO hierarchy of units, allowinggeneric programming using  Units.

Unit: abstract interface

Inst: Baf’s bytecode-level unit

(load x)Stmt: Jimple’s three-address code units

(z = x + y)

Stmt: also used in Grimp(z = x + y   *   2 % n ;)

Analyzing Java Programs with Soot – p. 19/89

Page 20: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 20/94

Soot Philosophy on  Units

Accesses should be abstract wheneverpossible!

Accessing data:

getUseBoxes(), getDefBoxes(),

getUseAndDefBoxes()

(also control-flow information:)fallsThrough(), branches(),

getBoxesPointingToThis(),

addBoxesPointingToThis(),removeBoxesPointingToThis(),

redirectJumpsToThisTo()

Analyzing Java Programs with Soot – p. 20/89

Page 21: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 21/94

What is a Box?

s: x = y op z

AssignStmt

VB

x

VB

OpExpr

VB

y

VB

z

AssignStmt

x OpExpr

y z

Analyzing Java Programs with Soot – p. 21/89

Page 22: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 22/94

What is a  DefBox?

List defBoxes = ut.getDefBoxes();

method  ut.getDefBoxes() returns a list of

ValueBoxes, corresponding to all  Valueswhich get defined in  ut, a  Unit.

non-empty for  IdentityStmt and

AssignStmt.

ut:   x   =   y op z   ;

getDefBoxes(ut) =   { x }(List   containing a   ValueBox

containing a  Local)

Analyzing Java Programs with Soot – p. 22/89

Page 23: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 23/94

On  Values and  Boxes

Value value = defBox.getValue();

getValue(): Dereferencing a pointer.

x   →  x

setValue(): mutates the value in the  Box.

Analyzing Java Programs with Soot – p. 23/89

Page 24: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 24/94

On  UseBoxes

Opposite of defBoxes.List useBoxes = ut.getUseBoxes();

method  ut.getUseBoxes() returns a list ofValueBoxes, corresponding to all  Valueswhich get used in  ut, a  Unit.

non-empty for most Soot  Units.

ut:   x   =   y op z   ;

getUseBoxes(ut) =   { y ,   z ,   y op z   }

(List   containing 3   ValueBoxes, 2

containing  Locals & 1  Expr)

Analyzing Java Programs with Soot – p. 24/89

Wh B ?

Page 25: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 25/94

Why Boxes?

Change all instances of  y to  1:

AssignStmt

VB

x

VB

OpExpr

VB

y

VB

z

AssignStmt

x OpExpr

y z

setValue() ??

Analyzing Java Programs with Soot – p. 25/89

S h & R l

Page 26: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 26/94

Search & Replace

/*   Replace all uses of v1 in body with v2   */

void replace(Body body, Value v1, Value v2)

{ for (Unit ut : body.getUnits())

{ for (ValueBox vb : ut.getUseBoxes())

if( vb.getValue().equals(v1) )

vb.setValue(v2);

}

}

replace(b, y, IntConstant.v(1));

Analyzing Java Programs with Soot – p. 26/89

M Ab t t A

Page 27: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 27/94

More Abstract Accessors:   Stmt

Jimple provides the following additionalaccessors for special kinds of  Values:

containsArrayRef(),

getArrayRef(), getArrayRefBox()

containsInvokeExpr(),

getInvokeExpr(), getInvokeExprBox()

containsFieldRef(),

getFieldRef(), getFieldRefBox()

Analyzing Java Programs with Soot – p. 27/89

Intraprocedural Outline

Page 28: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 28/94

Intraprocedural Outline

About Soot’s Flow Analysis Framework

Flow Analysis Examples

Live VariablesBranched Nullness

Adding Analyses to Soot

Analyzing Java Programs with Soot – p. 28/89

Flow Analysis in Soot

Page 29: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 29/94

Flow Analysis in Soot

Flow analysis is key part of compilerframework

Soot has easy-to-use framework forintraprocedural flow analysis

Soot itself, and its flow analysis framework,are object-oriented.

Analyzing Java Programs with Soot – p. 29/89

Four Steps to Flow Analysis

Page 30: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 30/94

Four Steps to Flow Analysis

1. Forward or backward? Branched or not?

2. Decide what you are approximating.

What is the domain’s confluence operator?3. Write equation for each kind of IR statement.

4. State the starting approximation.

Analyzing Java Programs with Soot – p. 30/89

HOWTO: Soot Flow Analysis

Page 31: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 31/94

HOWTO: Soot Flow Analysis

A checklist of your obligations:

1. Subclass  *FlowAnalysis

2. Implement abstraction:   merge(),  copy()

3. Implement flow function  flowThrough()

4. Implement initial values:newInitialFlow() andentryInitialFlow()

5. Implement constructor(it must call  doAnalysis())

Analyzing Java Programs with Soot – p. 31/89

HOWTO: Soot Flow Analysis II

Page 32: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 32/94

HOWTO: Soot Flow Analysis II

Soot provides you with:

impls of abstraction domains (flow sets)

standard abstractions trivial to implement;an implemented flow analysis namely,

doAnalysis() method: executesintraprocedural analyses on a CFG using aworklist algorithm.

Analyzing Java Programs with Soot – p. 32/89

Flow Analysis Hierarchy

Page 33: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 33/94

soot.toolkits.scalar

Flow Analysis Hierarchy

AbstractFlowAnalysis

FlowAnalysis

Forward- Backward-

BranchedFlowAnalysis

Forward-

Analyzing Java Programs with Soot – p. 33/89

Soot Flow Analyses

Page 34: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 34/94

soot.toolkits.scalar

Soot Flow Analyses

AbstractFlowAnalysis

FlowAnalysis

Forward-

PRE analy’s

Avail. Expr.

Array Bds

Backward-

PRE analy’s

Liveness

BranchedFlowAnalysis

Forward-

Casts Nullness

Analyzing Java Programs with Soot – p. 34/89

Backward vs Forward Analyses

Page 35: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 35/94

Backward vs. Forward Analyses

A forward analysis computes  OUT from   IN:

f s(i)

s

t

flow dir

i

f s(i)

f t(f s(i))

A backward analysis computes   IN from  OUT:

f t(i)

s

flow dir

t

i

f t(i)

f s(f t(i))

Analyzing Java Programs with Soot – p. 35/89

Outline: Soot Flow Analysis Examples

Page 36: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 36/94

Outline: Soot Flow Analysis Examples

Will describe how to implement a flow analysis inSoot and present examples:

live localsbranched nullness testing

Analyzing Java Programs with Soot – p. 36/89

Example 1: Live Variables

Page 37: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 37/94

Example 1: Live Variables

A local variable  v is live at s if there exists somestatement s′ using  v and a control-flow path froms to s′ free of definitions of  v.

{}

y=x

x=y   z=2

{z, y}

{z, x}

a=z

b=y

{z, y}{y}

{z, y}{z, y}

{z, y}

{z}

{z}

Analyzing Java Programs with Soot – p. 37/89

Steps to a Flow Analysis

Page 38: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 38/94

Steps to a Flow Analysis

As we’ve seen before:

1. Subclass  *FlowAnalysis

2. Implement abstraction:   merge(),  copy()

3. Implement flow function  flowThrough()

4. Implement initial values:newInitialFlow() andentryInitialFlow()

5. Implement constructor(it must call  doAnalysis())

Analyzing Java Programs with Soot – p. 38/89

Step 1: Forward or Backward?

Page 39: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 39/94

soot.toolkits.scalar.BackwardFlowAnalysis

p

Live variables is a backward flow analysis, sinceflow fn computes   IN sets from  OUT sets.

In Soot, we subclass  BackwardFlowAnalysis.

class LiveVariablesAnalysis

extends

BackwardFlowAnalysis<Unit,Set>

Analyzing Java Programs with Soot – p. 39/89

Step 2: Abstraction domain

Page 40: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 40/94

soot.toolkits.scalar.ArraySparseSet

p

Domain for Live Variables: sets of Local

se.g.  {x , y , z}

Partial order is subset inclusion

Merge operator is union

In Soot, we use the provided  ArraySparseSet

implementation of  FlowSet.

Analyzing Java Programs with Soot – p. 40/89

Implementing an Abstraction

Page 41: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 41/94

p g

Need to implement copy()

, merge()

 methods:

copy dir

src

CFGdest

copy() brings IN set to predecessor’s OUT set.

src1   src2

merge dir  dest = src1 ⊲⊳ src2

merge() joins two IN sets to make an OUT set.

Analyzing Java Programs with Soot – p. 41/89

More on Implementing an Abstraction

Page 42: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 42/94

p g

Signatures:

void merge(Set src1, Set src2,

Set dest);

void copy(Set src, Set dest);

Analyzing Java Programs with Soot – p. 42/89

Flow Sets and Soot

Page 43: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 43/94

soot.toolkits.scalar.FlowSet

Soot provides special sets called  FlowSets,which are often helpful.

Impls:   ToppedSet,  ArraySparseSet,ArrayPackedSet

//   c = a ∩ b

a.intersection(b, c);

//   d = c

c.complement(d);

//   c = a ∪ b

a.union(b,c);

//   d = d ∪ {v}

d.add(v);

Analyzing Java Programs with Soot – p. 43/89

Digression: types of  FlowSets

Page 44: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 44/94

soot.toolkits.scalar.*Set

Which  FlowSet do you want?ArraySparseSet: simple list

foo bar z

(simplest possible)

ArrayPackedSet: bitvector w/ map

00100101 10101111 10000000(can complement, need universe)

ToppedSet:FlowSet & isTop()

(adjoins a ⊤ to another  FlowSet)

Analyzing Java Programs with Soot – p. 44/89

Step 2:   copy() for live variables

Page 45: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 45/94

protected void copy(Set src,

Set dest) {

dest.clear(); dest.addAll(src);

}

Analyzing Java Programs with Soot – p. 45/89

Step 2:   merge() for live variables

Page 46: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 46/94

In live variables, a variable  v is live if there existsany path from  d to  p, so we use union.

void merge(...) {

dest.clear();

dest.addAll(src1Set);

dest.addAll(src2Set);   }

Often, you may want to implement a more exotic

merge.

Analyzing Java Programs with Soot – p. 46/89

Step 3: Flow equations

Page 47: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 47/94

Goal: At a unit like  x = y   *   z:kill   def  x;gen   uses  y,  z.

How? Implement this method:protected void flowThrough

(Set srcValue,

Unit u,

Set destValue)

Analyzing Java Programs with Soot – p. 47/89

Step 3: Copying

Page 48: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 48/94

Need to copy  src to  dest to allow manipulation.

copy dirsrc

CFGdest

dest.clear();dest.addAll(src);

Analyzing Java Programs with Soot – p. 48/89

Step 3: Implementing  flowThrough

Page 49: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 49/94

Must decide what happens at each statement (ingeneral, need to switch on unit type):

= OUT[ut] \  kills[ut] ∪  gens[ut]

ut

OUT[ut]

IN[ut]

IN[ut]

= flowThrough(OUT[ut])

flowThrough

flowThrough is the brains of a flow analysis.

Analyzing Java Programs with Soot – p. 49/89

Step 3:   flowThrough for live locals

Page 50: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 50/94

A local variable  v is live at s if there exists somestatement s′ containing a use of  v, and a

control-flow path from s to s′

free of def’ns of  v.

Don’t care about the type of unit we’re analyzing:

Soot provides abstract accessors to values usedand defined in a unit.

Analyzing Java Programs with Soot – p. 50/89

Step 3: Implementing  flowThrough: removing kills

Page 51: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 51/94

// Take out kill set:

// for each local v def’d in

// this unit, remove v from dest

for (ValueBox box : ut.getDefBoxes()){

Value value = box.getValue();

if( value instanceof Local )dest.remove( value );

}

Analyzing Java Programs with Soot – p. 51/89

Step 3: Implementing  flowThrough: adding gens

Page 52: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 52/94

// Add gen set

// for each local v used in

// this unit, add v to dest

for (ValueBox box : ut.getUseBoxes())

{

Value value = box.getValue();

if (value instanceof Local)

dest.add(value);}

N.B. our analysis is generic, not restricted toJimple.

Analyzing Java Programs with Soot – p. 52/89

Step 4: Initial values

Page 53: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 53/94

• Soundly initialize IN, OUT sets prior toanalysis.

Create initial sets

Set newInitialFlow(){return new HashSet();}

Create initial sets for exit nodesSet entryInitialFlow()

{return new HashSet();}

Want conservative initial value at exit nodes,optimistic value at all other nodes.

Analyzing Java Programs with Soot – p. 53/89

Step 5: Implement constructor

Page 54: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 54/94

LiveVariablesAnalysis(UnitGraph g)

{

super(g);

doAnalysis();

}

Causes the flow sets to be computed, usingSoot’s flow analysis engine.

In other analyses, we precompute values.

Analyzing Java Programs with Soot – p. 54/89

Enjoy: Flow Analysis Results

Page 55: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 55/94

You can instantiate an analysis and collectresults:

LiveVariablesAnalysis lv =

new LiveVariablesAnalysis(g);

// return HashSets

// of live variables:

lv.getFlowBefore(s);

lv.getFlowAfter(s);

Analyzing Java Programs with Soot – p. 55/89

Example 2: VeryB

Page 56: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 56/94

class VeryBusyExpressionAnalysis

extends BackwardFlowAnalysis {

[...]

}

Analyzing Java Programs with Soot – p. 56/89

VeryB - Constructor

Page 57: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 57/94

public VeryBusyExpressionAnalysis(

DirectedGraph g) {

super(g);

doAnalysis();

}

Analyzing Java Programs with Soot – p. 57/89

VeryB - Merge

Page 58: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 58/94

protected void merge(Object in1,

Object in2,

Object out) {

FlowSet inSet1 = (FlowSet)in1,

inSet2 = (FlowSet)in2,

outSet = (FlowSet)out;

inSet1.intersection(inSet2,outSet);

}

Analyzing Java Programs with Soot – p. 58/89

VeryB - Copy

Page 59: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 59/94

protected void copy(Object source,

Object dest) {

FlowSet srcSet = (FlowSet)source,

destSet = (FlowSet)dest;

srcSet.copy(destSet);

}

Analyzing Java Programs with Soot – p. 59/89

VeryB - Flow

Page 60: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 60/94

protected void flowThrough(Object in,

Object node,

Object out) {

FlowSet inSet = (FlowSet)source,

outSet = (FlowSet)dest;

Unit u = (Unit)node;

kill(inSet, u, outSet);

gen(outSet, u);

}

Analyzing Java Programs with Soot – p. 60/89

VeryB - Gen

Page 61: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 61/94

private void gen(FlowSet outSet,

Unit u) {

for (ValueBox useBox: u.getUseBoxes()) {

if (useBox.getValue()

instanceof BinopExpr)

outSet.add(useBox.getValue());

}}

}

Analyzing Java Programs with Soot – p. 61/89

VeryB - Kill

i t

Page 62: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 62/94

private

void kill(FlowSet in, Unit u, FlowSet out) {

FlowSet kills = (FlowSet)emptySet.clone();

for (ValueBox defBox: u.getUseBoxes()) {

if (defBox.getValue() instanceof Local) {

for (BinopExpr e: in) {

for (ValueBox useBox: e.getUseBoxes()) {

if (useBox.getValue() instanceof Local&& useBox.getValue().equivTo(

defBox.getValue()))

kills.add(e);}}}}

in.difference(kills, out);

}

Analyzing Java Programs with Soot – p. 62/89

VeryB - Initial State

Page 63: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 63/94

protected Object entryInitialFlow() {

return new ValueArraySparseSet();

}

protected Object newInitialFlow() {

return new ValueArraySparseSet();

}

Analyzing Java Programs with Soot – p. 63/89

Example 3: Branched Nullness

A local ariable is non n ll at if all

Page 64: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 64/94

A local variable  v is non-null at s if allcontrol-flow paths reaching s result in  v beingassigned a value different from  null.

F

x=new foo()

{y}{}

{y}

{x, y}

{x}

{x}

{x, b}

{}  if (y==null)

x.bar()

y=null

y=b.f

{}

{x}

T

Analyzing Java Programs with Soot – p. 64/89

HOWTO: Soot Flow Analysis

Page 65: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 65/94

Again, here’s what to do:

1. Subclass  *FlowAnalysis

2. Implement abstraction:   merge(),  copy()

3. Implement flow function  flowThrough()

4. Implement initial values:newInitialFlow() andentryInitialFlow()

5. Implement constructor(it must call  doAnalysis())

Analyzing Java Programs with Soot – p. 65/89

Step 1: Forward or Backward?

Page 66: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 66/94

soot.toolkits.scalar.ForwardBranchedFlowAnalysis

Nullness is a branched forward flow analysis,since flow fn computes  OUT sets from   IN sets,sensitive to branches

Now subclass   ForwardBranchedFlowAnalysis.

class NullnessAnalysis

extends

ForwardBranchedFlowAnalysis<Unit,FlowSet>   {

Analyzing Java Programs with Soot – p. 66/89

Step 2: Abstraction domain

Domain: sets of Locals known to be non null

Page 67: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 67/94

see soot.jimple.toolkits.annotation.nullcheck.BranchedRefVarsAnalysis

Domain: sets of  Locals known to be non-nullPartial order is subset inclusion.

(More complicated abstractions possible∗ for this

problem; e.g.  ⊥, ⊤, null, non-null per-local.)

This time, use  ArraySparseSet to implement:

void merge(FlowSet in1, FlowSet in2,

FlowSet out);

void copy(FlowSet src, FlowSet dest);

Analyzing Java Programs with Soot – p. 67/89

Implementing an Abstraction

For a forward analysis copy and merge mean:

Page 68: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 68/94

For a forward analysis,  copy and  merge mean:

dest

CFGcopy dir

src

copy() brings OUT set to predecessor’s IN set.

src1   src2

merge dir

dest = src1 ⊲⊳ src2

merge() joins two OUT sets to make an IN set.

Analyzing Java Programs with Soot – p. 68/89

Step 2:   copy() for nullness

Page 69: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 69/94

Same as for live locals.protected void copy(FlowSet src,

FlowSet dest) {

src.copy(dest);

}

Use  copy() method from  FlowSet.

Analyzing Java Programs with Soot – p. 69/89

Step 2:   merge() for nullness

Page 70: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 70/94

In branched nullness, a variable  v is non-null if itis non-null on all paths from  start to  s, so weuse intersection.

Like  copy(), use  FlowSet method – here,intersection():

void merge(...) {

srcSet1.intersection(srcSet2,

destSet);

}

Analyzing Java Programs with Soot – p. 70/89

Step 3: Branched Flow Function

Page 71: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 71/94

Need to differentiate between branch andfall-through OUT sets.

protected void

flowThrough(FlowSet srcValue,

Unit unit,

List<FlowSet> fallOut,List<FlowSet> branchOuts)

fallOut is a one-element list.

branchOuts contains a  FlowSet for eachnon-fallthrough successor.

Analyzing Java Programs with Soot – p. 71/89

Step 3: Flow equations

Page 72: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 72/94

We do the following things in our flow function:

Create copy of src set.

Analyzing Java Programs with Soot – p. 72/89

Step 3: Flow equations

Page 73: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 73/94

We do the following things in our flow function:

Create copy of src set.

Remove kill set (defined  Locals).y in  y = y.next;

Analyzing Java Programs with Soot – p. 72/89

Step 3: Flow equations

Page 74: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 74/94

We do the following things in our flow function:

Create copy of src set.

Remove kill set (defined  Locals).y in  y = y.next;

Add gen set.

x in  x.foo();

Analyzing Java Programs with Soot – p. 72/89

Step 3: Flow equations

Page 75: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 75/94

We do the following things in our flow function:

Create copy of src set.

Remove kill set (defined  Locals).y in  y = y.next;

Add gen set.

x in  x.foo();

Handle copy statements.

Analyzing Java Programs with Soot – p. 72/89

Step 3: Flow equations

Page 76: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 76/94

We do the following things in our flow function:

Create copy of src set.

Remove kill set (defined  Locals).y in  y = y.next;

Add gen set.

x in  x.foo();

Handle copy statements.

Copy to branch and fallthrough lists.

Analyzing Java Programs with Soot – p. 72/89

Step 3: Flow equations

Page 77: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 77/94

We do the following things in our flow function:

Create copy of src set.

Remove kill set (defined  Locals).y in  y = y.next;

Add gen set.

x in  x.foo();

Handle copy statements.

Copy to branch and fallthrough lists.Patch sets for  if statements.

Analyzing Java Programs with Soot – p. 72/89

Step 4: Initial values

Page 78: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 78/94

Initialize IN, OUT sets.Create initial sets (⊤ from constr.)FlowSet newInitialFlow()   {

{   return fullSet.clone();   }

Create entry sets (emptySet from constr.)

FlowSet entryInitialFlow(){   return emptySet.clone();   }

(To be created in constructor!)

Analyzing Java Programs with Soot – p. 73/89

Step 5: Constructor: Prologue

Page 79: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 79/94

Create auxiliary objects.public NullnessAnalysis(UnitGraph g)

{

super(g);

unitToGenerateSet = new HashMap();

Body b = g.getBody();

Analyzing Java Programs with Soot – p. 74/89

Step 5: Constructor: Finding All Locals

Page 80: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 80/94

Create flowsets, finding all locals in body:emptySet = new ArraySparseSet();

fullSet = new ArraySparseSet();

for (Local l : b.getLocals()) {

if (l.getType()

instanceof RefLikeType)fullSet.add(l);

}

Analyzing Java Programs with Soot – p. 75/89

Step 5: Creating gen sets

Page 81: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 81/94

Precompute, for each statement, which localsbecome non-null after execution of that stmt.

x gets non-null value:x =   *, where  * is  NewExpr,  ThisRef, etc.

successful use of  x:

x.f,  x.m(),  entermonitor x, etc.

Analyzing Java Programs with Soot – p. 76/89

Step 5: Constructor: Doing work

Page 82: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 82/94

Don’t forget to call  doAnalysis()!...

doAnalysis();}

}

Analyzing Java Programs with Soot – p. 77/89

Enjoy: Branched Flow Analysis Results

To instantiate a branched analysis & collect

Page 83: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 83/94

results:NullnessAnalysis na=new NullnessAnalysis(b);

// a SparseArraySet of non-null variables.

na.getFlowBefore(s);

// another SparseArraySet

if (s.fallsThrough()) na.getFallFlowAfter(s);

// a List of SparseArraySets

if (s.branches()) na.getBranchFlowAfter(s);

Analyzing Java Programs with Soot – p. 78/89

Adding transformations to Soot (easy way)

Page 84: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 84/94

1. Implement a  BodyTransformer ora  SceneTransformer

internalTransform method

does the transformation2. Choose a pack for your transformation

(usually  jtp)

3. Write a  main method that adds the transformto the pack, then runs Soot’s  main

Analyzing Java Programs with Soot – p. 79/89

On  Packs

Want to run a set of  Transformer objects with

Page 85: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 85/94

soot.Pack

one method call.⇒ Group them in a  Pack.

Soot defines default  Packs which are runautomatically. To add a  Transformer to thejtp Pack:

Pack jtp = G.v().PackManager().getPack("jtp");

jtp.add(new Transform("jtp.nt",

new NullTransformer()));jtp.add(new Transform("jtp.nac",

new NullnessAnalysisColorer()));

Analyzing Java Programs with Soot – p. 80/89

Running Soot more than once

Page 86: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 86/94

All Soot global variables are stored in  G.v()

G.reset() re-initializes all of Soot

Analyzing Java Programs with Soot – p. 81/89

Generating Jimple

Page 87: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 87/94

.class   coffi

 jb   Jimple

.jimple Jimpleparser

Analyzing Java Programs with Soot – p. 82/89

Intra-procedural packs

t Shi l

Page 88: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 88/94

stp sop sap   Shimple

Jimple   jtp jop jap   Jimple

bb bop tag   Baf

gb gop   Grimp

Dava Jasmin Output

Analyzing Java Programs with Soot – p. 83/89

Soot Pack Naming Scheme

?(j| |b| )(b|t| | )

Page 89: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 89/94

The p is sometimes silent.

w?( j|s|b|g)(b|t|o|a) p

w ⇒ Whole-program phase

 j, s, b, g ⇒ Jimple, Shimple, Baf, Grimp

b, t, o, a ⇒(b) Body creation

(t) User-defined transformations

(o) Optimizations with -O option

(a) Attribute generation

Analyzing Java Programs with Soot – p. 84/89

Soot Packs (Jimple Body)

jb t i Ji l t d f

Page 90: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 90/94

jb  converts naive Jimple generated frombytecode into typed Jimple with split variables

Analyzing Java Programs with Soot – p. 85/89

Soot Packs (Jimple)

jtp f m d fi d i t d l

Page 91: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 91/94

jtp  performs user-defined intra-proceduraltransformations

jop  performs intra-procedural optimizations

CSE, PRE, constant propagation, . . .

jap  generates annotations using whole-program

analysesnull-pointer check

array bounds check

side-effect analysis

Analyzing Java Programs with Soot – p. 86/89

Soot Packs (Back-end)

bb performs transformations to create Baf

Page 92: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 92/94

bb  performs transformations to create Bafbop  performs user-defined Baf optimizations

gb  performs transformations to create Grimp

gop  performs user-defined Grimp optimizations

tag  aggregates annotations intobytecode attributes

Analyzing Java Programs with Soot – p. 87/89

Conclusion

Have introduced Soot a framework for

Page 93: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 93/94

Have introduced Soot, a framework foranalyzing, optimizing, (tagging andvisualizing) Java bytecode.

Have shown the basics of using Soot as astand-alone tool and also how to add newfunctionality to Soot.

Now for some homework and reading.

Analyzing Java Programs with Soot – p. 88/89

Resources

Main Soot page: www sable mcgill ca/soot/

Page 94: Soot Tutorial

7/23/2019 Soot Tutorial

http://slidepdf.com/reader/full/soot-tutorial 94/94

Main Soot page:   www.sable.mcgill.ca/soot/

Theses and papers:

www.sable.mcgill.ca/publications/

Tutorials:   www.sable.mcgill.ca/soot/tutorial/

Javadoc:   in main Soot distribution,

www.sable.mcgill.ca/software/#soot  and alsoonline at www.sable.mcgill.ca/soot/doc/.

Mailing lists:

www.sable.mcgill.ca/soot/#mailingLists

Soot in a Course:

www.sable.mcgill.ca/˜hendren/621/

Analyzing Java Programs with Soot – p. 89/89