












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













int myFunc ( int (*p)(int), …) { …… return p(x); } P is a function pointer. What function could p point to (what is the value of p)? P is a function parameter, so the value of p is unknown unless inter-procedural dataflow analysis is performed But inter-procedural data-flow requires an inter-procedural control flow graph (or a call graph)
Imperative languages that allow functions as parameters Object oriented languages and functional languages
What is the domain of analysis
What could be the values for each function pointer expression? Specification of the analysis
how to accommodate the information flow from function definitions to function invocations Well-definedness of the analysis
What is the solution?
To represent the solution, label each expression within the program, compute
C(e) contains the set of function values e may have
P(x) contains the set of function values x may have
cs6463 7 Example Code and Control-flow Analysis Solution
((fun f x => x) (fun g y => y)) Labels: 1: x; 2: (fun f x => x) 3: y; 4: (fun g y => y) 5: ((fun f x => x) (fun g y => y))
g { fun g y => y} y^ ∅ f {fun f x => x} x { fun g y => y} 5 {fun g y => y} 4 {fun g y => y} (^3) ∅ (^2) {fun f x => x} 1 {fun g y => y}
Formally Abstract values: Val = Power(Term)
Abstract environment: Env = Var -> Val
Abstract cache: Cache = Label -> Val
Each solution: a pair of (P,C) ⊆(Env, Cache)
(C,P) |= ((e1)l1 (e2)l2)l3 iff (C,P) |= (e1)l1, (C,P) |= (e2)l2, and ∀ (fun f x => (e0)l0) ∈ C(l1 ): (C,P)|=(e0)l0, C(l2) ⊆ P(x) and C(l0 ) ⊆ C(l2) The solution for function parameter (x) must contain that of the invocation argument (e2); The solution of the function invocation must contain that of the function body
(C,P) |= (let x = (e1)l1 in (e2)l2)l3 iff (C,P) |= (e1)l1, (C,P) |= (e2)l2, C(l1) ⊆ P(x) and C(l2 ) ⊆ C(l3) The solution for the local variable (x) must contain that of its defined value The solution of the outer scope must contain that of the inner scope
(C,P) |= (if (e0)l0 then (e1)l1 else (e2)l2)l3 iff (C,P) |= (e0)l0, (C,P) |= (e1)l1, (C,P) |= (e2)l2, and C(l2) ⊆ C(l3) and C(l2 ) ⊆ C(l3) The solution of the outer scope must contain that of the inner scopes (both branches)
Example Code and Control-flow Analysis Solution Example code ((fun f x => x) (fun g y => y)) Labels: 1: x; 2: (fun f x => x) 3: y; 4: (fun g y => y) 5: ((fun f x => x) (fun g y => y)) Example CFA solution (guesses of the (C,P) mappings). Are the valid? (C,P) (C’,P’) y ∅^ ∅ f {fun f x => x} {fun f x => x} g {fun g y => y} {fun g y => y} { fun g y => y} {fun g y => y} {fun g y => y} ∅ {fun f x => x} {fun g y => y} x ∅ 5 {fun g y => y} 4 {fun g y => y} 3 ∅ (^2) {fun f x => x} 1 {fun g y => y} (C,P) |= ((fun f x => x) (fun g y => y)) (C’,P’) |= ((fun f x => x) (fun g y => y))
If there is a possible evaluation of the program such that the function at a call point evaluates to some function definition then this definition has to be in the set of possible definitions computed by the analysis. Existence of solutions Every expression accepts a least CFA solution
Syntax-directed analysis Reformulate the analysis specification
Compute the least solution of the set of constraints Each constraint has the form
where
l is label, x is a variable
Example: Constraint Construction Cond[((fun f x => (x) 1 ) 2 (fun g y => (y) 3 ) 4 ) 5 ] = { {fun f x => (x)} ⊆ C(2), {fun f x => (x)} ⊆ P(f), P(x) ⊆ C(1), {fun g y => (y)}⊆C(4), {fun g y => (y)}⊆P(g), P(y) ⊆ C(3), {fun f x => (x)} ⊆ C(2) => C(4) ⊆ P(x), {fun f x => (x)} ⊆ C(2) => C(1) ⊆ C(5), {fun g y => (y)} ⊆ C(2) => C(4) ⊆ P(y), {fun g y => (y)} ⊆ C(2) => C(3) ⊆ C(5) }
Straight-forward iterative algorithm has n^5 cost, where n is the size of the program (expression) A more sophisticated algorithm takes n^3 complexity
Each node n corresponds to a unique C(l) or P(x) =>val(n) Add an edge from node n1 to n2 if any change to val(n1) may require modifications to val(n2)
while worklist is not empty do q := Remove-first(W); for each cc in Edge[q] do case cc of p1 ⊆ p2: add(p2, Val[p1]); {t} ⊆ p => p1 ⊆ p2: if t ⊆ Val[p] then add(val(p1), p2); {fun g y => y} {fun f x => x} ∅ {fun g y => y} ∅ {fun g y => y} ∅ {fun f x => x} {fun g y => y} Propoage P(x) {fun g y => y} {fun f x => x} ∅ {fun g y => y} ∅ {fun g y => y} ∅ {fun f x => x} ∅ Val Iteration 0 Propogate C(2)… Propoage C(1) P(y) ∅^ ∅ P(f) {fun f x => x} {fun f x => x} P(g) {fun g y => y} {fun g y => y} ∅ ∅ {fun g y => y} ∅ {fun f x => x} ∅ P(x) {fun g y => y} C(5) {fun g y => y} C(4) {fun g y => y} C(3) ∅ C(2) {fun f x => x} C(1) {fun g y => y}
Recording the solution of CFA analysis
C(l) = Val[C(l)] (P(x) = Val[P(x)] ) Correctness and Termination