PLDITutorial
PLDITutorial
John Whaley
Stanford University and moka5 Inc.
vs.
• 2x faster
• Fewer bugs
…56 pages! • Extensible
June 11, Using Datalog and 2
Is it really that easy?
• Requires:
– A different way of thinking
– Knowledge, experience, and intuition
– Perseverance to try different techniques
– A lot of tuning and tweaking
– Luck
• Despite all this, people who use it
swear by it and could “never go back”
ancestor(x,y) :- parent(x,y).
ancestor(x,z) :- parent(x,y), ancestor(y,z).
:-
p o1 Output Relations
f
hPointsTo(o1, f, o2)
q o2 r
vPointsTo(r, o2)
June 11, Using Datalog and 26
Inference Rule in Datalog
Assignments:
:- Assign(v1, v2),
vPointsTo(v1, o) vPointsTo(v2, o).
v1 = v2;
v2 o
v1
v1.f = v2;
v1 o1
f
v2 o2
v2 = v1.f;
v1 o1
f
v2 o2
:- Store(v1, f, v2),
hPointsTo(o1, f, o2) vPointsTo(v1, o1),
vPointsTo(v2, o2).
:- Load(v1, f, v2),
vPointsTo(v2, o2) vPointsTo(v1, o1),
hPointsTo(o1, f, o2).
Calls(A,B) → 00 01 A 00
Calls(A,C) → 00 10
01 B C 10
Calls(A,D) → 00 11
Calls(B,D) → 01 11 D 11
Calls(C,D) → 10 11
x3 x3 x3 x3
x4 x4 x4 x4 x4 x4 x4 x4
0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0
June 11, Using Datalog and 38
Binary Decision Diagrams
• Collapse redundant nodes.
x1 0 edge
1 edge
x2 x2
x3 x3 x3 x3
x4 x4 x4 x4 x4 x4 x4 x4
0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0
June 11, Using Datalog and 39
Binary Decision Diagrams
• Collapse redundant nodes.
x1 0 edge
1 edge
x2 x2
x3 x3 x3 x3
x4 x4 x4 x4 x4 x4 x4 x4
0 1
June 11, Using Datalog and 40
Binary Decision Diagrams
• Collapse redundant nodes.
x1 0 edge
1 edge
x2 x2
x3 x3 x3 x3
x4 x4 x4
0 1
June 11, Using Datalog and 41
Binary Decision Diagrams
• Collapse redundant nodes.
x1 0 edge
1 edge
x2 x2
x3 x3 x3
x4 x4 x4
0 1
June 11, Using Datalog and 42
Binary Decision Diagrams
• Eliminate unnecessary nodes.
x1 0 edge
1 edge
x2 x2
x3 x3 x3
x4 x4 x4
0 1
June 11, Using Datalog and 43
Binary Decision Diagrams
• Eliminate unnecessary nodes.
x1 0 edge
1 edge
x2 x2
x3 x3
x4
0 1
June 11, Using Datalog and 44
Binary Decision Diagrams
• Size depends on amount of redundancy,
NOT size of relation.
– Identical subtrees share the same
representation.
– As set gets very large, more nodes have
identical zero and one successors, so the size
decreases.
x2 x3 x3
x3 x2 x2
x4
x4
0 1 0 1
x1<x2<x3<x4 x1<x3<x2<x4
June 11, Using Datalog and 46
Variable ordering is NP-hard
• No good general heuristic solutions
• Dynamic reordering heuristics don’t
work well on these problems
• We use:
– Trial and error
– Active learning
a a
b b
c | c c
d d d
0 1 0 1 0 1
Arguments A, B, op Result
A and B: Boolean Functions OBDD
Represented as OBDDs
representing
composite
op: Boolean Operation (e.g., ^, &, function
|)
June 11, Using Datalog and A op
48 B
Apply Execution Example
Argument A Argument B Recursive Calls
A1 a a B1 A1,B1
A2 b A2,B2
Operation
c A6 | c B5 A6,B2 A6,B5
A4 0 1 A5 B3 0 1 B4 A4,B3 A5,B4
• Optimizations
– Dynamic programming
– Early termination rules
A2,B2 b b
A6,B2 A6,B5 c c c
A4,B3 A5,B4 0 1 0 1
Base case:
Memo cache
lookup:
Recursive step:
Memo
June cache
11, insert: Using Datalog and 52
BDD Libraries
• BuDDy
– Simple, fast, memory-friendly
– Identifies BDD by index in unique table
• JavaBDD
– 100% Java, based on BuDDy
– Also native interface to BuDDY, CUDD, CAL, JDD
• CUDD
– Most popular, most feature-complete
– Not as fast as BuDDy
– Other types: ZDD, ADD
• JDD
– 100% Java, fresh implementation
– Still under development
Datalog Output
program relations
1470 0 1464
Load Assign
Store vPointsTo
Load Assign
Store vPointsTo
hPointsTo
t1 = ρvariable→source(vPointsTo);
t2 = assign ⋈ t1;
t3 = πsource(t2);
t4 = ρdest→variable(t3);
vPointsTo = vPointsTo ∪ t4;
B C D B C D
E F E F E F E F
G G G G
1016
1012
108
104
100
B C D B0 C0 D0
E E0 E1 E2
F G F0 F1 F2 G0 G1 G2
H H0 H1 H2 H3 H4 H5
0-2 3-5
H H0 H1 H2 H3 H4 H5
• Standard approach
– Write a “harness” manually
– A client exercising the interface of the open program
• Our approach
– Generate the harness automatically
• Step 2
– Consider any access pair (e1, e2)
– To be a race e1 must be:
– Reachable from a thread-spawning call site s1
• Without “switching” threads
– Where s1 is reachable from main
– (and similarly for e2)
• Step 3
– To have a race, both must access the same memory
location
– Use alias analysis
• Step 4
– To have a race, the memory location must also
be thread-shared
– Use thread-escape analysis
• Step 5
– Discard pairs where the memory location is guarded
by a common lock in both accesses
Class c = Class.forName(“java.lang.String”);
Object o = c.newInstance();
June•11,This is interprocedural
Using Datalog and 125
const+copy prop on strings
Pointer Analysis Can Help
stringClass
clazz
className
java.lang.String
Program
Program IR
IR Call
Call graph
graph Reflection
Reflection Resolved
Resolved Final
Final call
call
construction
construction resolution
resolution calls
calls graph
graph
using
using
points-to
points-to
User-provided
User-provided Cast-based
Cast-based
spec
spec approximation
approximation
Specification
Specification
June 11, Using points
Datalog and
points 134
Implementation Details
• Call graph construction algorithm in the
presence of reflection is integrated with
pointer analysis
– Pointer analysis already has to deal with virtual calls:
new methods are discovered, points-to relations for
them are created
– Reflection analysis is another level of complexity
Variable Ordering
Header 0 6 4 0 10
Parameter 6 5 0 2 13
Cookie 1 0 0 0 1
Non-Web 2 0 0 3 5
Total 9 11 4 5 29