Top Banner
Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee- Rai, Patrick Lam, Etienne Gagnon and Charles Godin Sable Research Group School of Computer Science McGill University Montreal
22

Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Dec 17, 2015

Download

Documents

Lesley Peters
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Practical Virtual Method Call Resolution for Java

Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ee-Rai, Patrick Lam, Etienne Gagnon and Charles ́�

GodinSable Research Group

School of Computer Science McGill University Montreal

Page 2: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

The Problem

• Polymorphism:MouseMouse

Point getXY();

USBMouseUSBMouse

Point getXY(){...

}

PS2MousePS2Mouse

Point getXY(){...

}

BluetoothMouseBluetoothMouse

Point getXY(){...

}

LogitechTrackballLogitechTrackball

Point getXY(){...

}

MightyMouseMightyMouse

Point getXY(){...

}

What happens atpos = mouse.getXY()

?

Page 3: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Motivation

Compact ExecutableRemove functions which are never called

11

Faster Method CallsIdentify monomorphic call sites

22

Predictable FlowReduce number of flows, for other analyses

33

Page 4: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

hierarchy − types(d2)⊆ hierarchy − types(d1)

Class Hierarchy Analysis

For every class or instance d, we have

hierarchy − types(d)⊆ types

If d2 extends d1 or if d2 implements d1

For every class or interface d

d ∈ hierarchy − types(d)

Defined by the following recursion:

Page 5: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Class Hierarchy AnalysisMouseMouse

Point getXY();

USBMouseUSBMouse

Point getXY(){...

}

PS2MousePS2Mouse

Point getXY(){...

}

BluetoothMouseBluetoothMouse

Point getXY(){...

}

LogitechTrackballLogitechTrackball

Point getXY(){...

}

MightyMouseMightyMouse

Point getXY(){...

}

Mouse, PS2Mouse, USBMouse, BluetoothMouse, LogitechTrackball, MightyMouse

hierarchy-types

hierarchy-types

USBMouse, LogitechTrackball, MightyMouse

Page 6: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Class Hierarchy AnalysisMouseMouse

Point getXY();

USBMouseUSBMouse

Point getXY(){...

}

PS2MousePS2Mouse

Point getXY(){...

}

BluetoothMouseBluetoothMouse

Point getXY(){...

}

LogitechTrackballLogitechTrackball MightyMouseMightyMouse

Point getXY(){...

}

look-up(LogitechTrackball.getXY) = USBMouse.getXY

look-up(LogitechTrackball.equals) = object.equals

look-up(PS2Mouse.getXY) = PS2Mouse.getXY

Sometimes you may need to look up the hierarchy

!!

Page 7: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Call Graph

C.m1C.m1

o.m2()o.m2()

D.m2D.m2

For a receiver o of type d,

D.m2 = look-up(d’.m2)for all d’in hierarchy-types(d)

where

(pessimistic)

Page 8: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Call Graphpublic class C extends A

{

public static void main(String[] p)

{

A b = new B();

A c = new C();

b.m();

c.m();

System.err.println(c.toString());

}

}

C.mainC.main

b.m()b.m() c.m()c.m()

c.toString()c.toString()

err.println()err.println()

Page 9: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Call Graph

C.mainC.main

b.m()b.m() c.m()c.m()

c.toString()c.toString()

err.println()err.println()

A.mA.m

B.mB.m

C.mC.m

Object.toStringObject

.toString

PrintStream.println

PrintStream.println

Page 10: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Rapid Type AnalysisImprovement over Class Hierarchy

For a program P –

instantiated − types(P)=Δ

{d |∃a stmt "new d" in P}

hierarchy − types(d) ∩ instantiated − types(P)

•Get a more accurate result by only considering

•Build call graph (and P) iteratively

Page 11: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Variable-Type Analysis

a receiver o may be of type d at run-time,(where d is a subclass of the declared type of o)

IfIf

there must be a chain of assignments of the formo = varn = varn–1 = … = var1 = new d()

thenthen

Page 12: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Representative Nodes

public class C extends A

{

public String m(Object p)

{

f = new C();

Object v = p;

return v.toString();

}

private A f;

}

C.m.pC.m.p

C.fC.f

C.m.thisC.m.this

C.m.retC.m.ret

C.m.vC.m.v

parameter

receiver

return value

local

field

Page 13: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Anatomy of AssignmentsReferencesReferences

v[i]

vplain

v.ffield

array

ExpressionsExpressions

(C)vtype cast

w = v.m(u1,……,un)method call

lhs = rhs

Can assume w.l.g. that at least one of {lhs, rhs} is plain local

!!

Page 14: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

}}

public class C extends A{

public String m(Object p){

A u, v, w;

Representative Edges

v = p; C.m.vC.m.vC.m.pC.m.p

this.f = v; C.fC.fC.m.vC.m.v

w = v.m(u);A.m.pA.m.pC.m.uC.m.u

A.m.thisA.m.thisC.m.vC.m.v

A.m.retA.m.retC.m.wC.m.w

For array or Object references:instead of

!!

Page 15: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Representative Graph

public class C extends A

{

public String m(Object p)

{

f = new C();

Object v = p;

return v.toString();

}

private A f;

}

C.m.pC.m.p C.fC.fC.m.thisC.m.this

C.m.retC.m.ret

C.m.t0C.m.t0

Object.toString

.this

Object.toString

.this

Object.toString

.ret

Object.toString

.ret

C.m.vC.m.v

Page 16: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Instances

instances(n)=Δ

{d |∃assignment n = new d}

Page 17: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Variable-Type Analysis

a receiver o may be of type d at run-time,(where d is a subclass of the declared type of o)

IfIf

there must be a path in the representative graph from a node n to representative(o) s.t. d in instances(n)

thenthen

Page 18: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Variable Type AnalysisImproving Performance

C.m.pC.m.p C.fC.fC.m.thisC.m.this

C.m.retC.m.ret

C.m.t0C.m.t0

Object.toString

.this

Object.toString

.this

Object.toString

.ret

Object.toString

.ret

C.m.vC.m.v

{B} {C}

{S}

Page 19: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Variable Type AnalysisImproving Performance

C.m.pC.m.p C.fC.fC.m.thisC.m.this

C.m.retC.m.ret

C.m.t0C.m.t0

Object.toString

.this

Object.toString

.this

Object.toString

.ret

Object.toString

.ret

C.m.vC.m.v

Find Strongly Connected Componentsusing Tarjan’s algorithm

11 {B}{C}

{S}

Page 20: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Variable Type AnalysisImproving Performance

C.m.pC.m.p C.fC.fC.m.thisC.m.this

C.m.retC.m.ret

C.m.t0C.m.t0

Object.toString

.this

Object.toString

.this

Object.toString

.ret

Object.toString

.ret

C.m.vC.m.v

Propagate Types Along Edges(graph is now a DAG)

22 {B}{B,C}

{S}

{B,C}

{S}

Page 21: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Declared-Type Analysis

• All variables of the same declared type are summarized as a single node

• Coarser-grain– Not as accurate as variable-type, but still more

accurate than class hierarchy and rapid type– Faster and requires much less space

Page 22: Practical Virtual Method Call Resolution for Java Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vall ́ee-Rai, Patrick Lam, Etienne Gagnon.

Experimental Results• Detection of monomorphic call sites