780 likes | 866 Views
Explore the theory and practice of aliasing in software engineering, including alias analysis and its importance in program optimization and concurrency. The text presents a comprehensive discussion on alias relations, expressions, and operations in programming languages.
E N D
The alias calculusBertrand Meyer ITMO Software Engineering SeminarJune 2011
Claims • Theory: • Theory of aliasing • Loss of precision is small • New concepts, in particular inverse variables • Abstract, does not mention stack & heap • Simple, implementable • Insights into the essence of object-oriented programming • Practice: • Alias calculus • Almost entirely automatic • Implemented
Reference Steps Towards a Theory and Calculus of Aliasing International Journal of Software and Informatics July 2011 http://se.ethz.ch/~meyer/publications/aliasing/alias-revised.pdf
The question under study e f • (If so, we say that e and f are aliased to each other, meaning potentially aliased.) Given expressions e and f (of reference types) and a program location p: Atp, can eandfever be attached to the same object?
“Given” expressions only x a a • y may become aliased to: • x, x a, x a a, x a a aetc. • (infinite set of expressions!) a a Consider from y := x loop y := y a end
An example of alias analysis Consider two linked list structures known through x and y: x right y item • Computing the alias relation shows that: • If x ≠ y, then no cell reachable from x ( or ) can be reached from y ( or ), and conversely • Without this assumption, such aliasing is possible
Why alias analysis is important b -- c = c, i.e. True a ? set_a (c) x.set_a (c) Understand as x.a := c -- y.a = b -- y.a = b x -- x.a = c -- x.a = c y c 1. Without it, cannot apply standard proof techniques to programs involving pointers 2. Concurrent program analysis, in particular deadlock 3. Program optimization
Basic notion A binary relation is an alias relationif it is symmetric andirreflexive Can alias x to y and y to z but not x to z Definition: Not necessarily transitive: ifcthen x := y else y := z end
Formulae of interest The calculus defines, for any instruction p and any alias relation a,the value of a » p denoting: The aliasing relation resulting from executing pfrom an initial state in which the aliasing relation is a For an entire program: compute » p
The programming language p, q, …: instructions x, y, …: variables • z := x y • x r … • Current • r do p end • call r • pn -- for integer n • loop p end E4: O-O E3: procedures • cut x, y E2:loops E1:cut E0:basic constructs Eiffel: x := Void Java etc.: x = null; • skip • create x • x := y • forget x • (p ; q) • then p else q end
Describing an alias relation • Set of binary relations on E; formally: P(E x E) D Identity on E = Set difference {[x, y], [y, x], [y, z], [z, y]} {[x, y], [y, x], [x, z], [z, x], [y, z], [z, y]} “Complete” alias relation • If r is a relation in E E, the following is an alias relation: r (r r-1) ― Id [E] • Example: {[x, x], [x, y], [y, z]}= • Generalized to sets: • {x, y, z} =
Canonical form & alias diagrams x, y, y, z, x, u, v {x, y} { y, z} {x, u, v} x , y y y y, z • An alias diagram: • (not canonical) y y x, u, v x x • Make it canonical: Canonical form of an alias relation: union of complete alias relations, e.g. , meaning None of the sets of expressionsis a subset of another
Alias calculus for basic operations (E0) a deprived of all pairs involving x e.g.x, y, y, z, x, u, v \- {x, u} = y, z, u, v Override, see next a » skip = a a » (then p else q end )= (a » p) (a » q) a » (p ; q) = (a » p) » q a » (forget x) = a \- {x} a » (create x) = a \- {x} a » (x := y) = a [x: y]
The forgetrule y y x, x, a deprived of allpairs involving x y, z u, v x, x, a » (forget x)= a \- {x}
The assignment rule (E0) a deprived of all pairs involving x Symmetrize and de-reflect All pairs [x, u] where u is either aliased to y in b or y itself a » (x := y) = a [x: y] with: a [x: y] =given b = a \- {x} then b ({x} (b / y)) end
Operations on alias relations “Minus” Set of all expressions “Quotient”, similar to equivalence class in equivalence relation For an alias relation a in E E, an expression x, and a set of expressions A E, the following are alias relations: r \– A = r — E x A a / y = {z: E | (z = y) [y, z] a}
The assignment rule (E0) Value of a » (x := y) a deprived of all pairs involving x Symmetrize and de-reflect All u aliased to yin b, plusy itself All pairs [x, u] where u is either aliased to y in b or y itself a [x: y] = given b = a \- {x} then b ({x} ( b / y )) end
Assignment example 1 x , y , z x, u, v , z Before After z := x
Assignment example 2 x x , y , y x, u, v Before After x := u
Assignment example 3 x x , y , y x, z x, u, v x, Before After x := z
The assignment rule (E0) Value of a » (x := y) a [x: y] = given b = a \- {x} then b ({x} (b / y)) end
The cut instruction E1:cut E0:basic constructs • E1 is E0 plus the instruction cut x, y • Semantics: remove aliasing, if any, between x and y
Cut example 1 x x , y , y x, u, v Before After cut x, y
Cut example 2 x , y x , v x, u, v x, Before After cut x, u
Cut rule Set difference a » cut x, y = a ― x, y
The role of cut Alias relation: x, u, x, y x, u, z, x, y, z cut x, y; cut x, y informs the alias calculus with non-alias properties coming from other sources Example: ifm < n thenx := u elsex := y end m := m + 1 ifm < n thenz := x end But here x cannot be aliased to y (only to u). The alias theory does not know this property! To take advantage of it, add the instruction This expressionrepresents checkx /= y end (Eiffel) assertx != y ; (JML, Spec#)
Introducing repetitions E2:loops E1:cut E0:basic constructs • E2 is E1 plus: • pn(for integer n): n executions of p (auxiliary notion) • loop p end : any sequence (incl. empty) of executions of p
E2 alias calculus nN a » p0 = a a » pn+1 = (a » pn) » p -- For n 0 -- Also equal to (a » p) » pn a » (loop p end) = (a » pn)
Loop aliasing theorem (1) • a » (loop p end) = (a » pn) nN n:0 N k:0 n For any a and p, there exists a constant NN such that a » (loop p end) = (a » pn) Proof : the sequence sn = (a » pk) is non-decreasing (with respect to inclusion) on a finite set More generally, for every construct p of E2, the function l a | (a » p) is non-decreasing
Loop aliasing theorem (2) k:0 n • a » (looppend) is also the fixpoint of the sequence t0 = a tn+1 = tn(tn » p) • Gives a practical way to compute a » (looppend) Proof: by induction. If sn is original sequence (a » pn), prove separately sn tnand tn sn
Introducing procedures: E3 E3: procedures • Alias calculus notations: • rdenotes body of r (i.e. ri = pi) • rdenotes formals of r (here f) E2:loops E1:cut E0:basic constructs • A program is now a sequence of procedure definitions (one designated as main): ri (f) do piend • Instructions: as before, plus call ri(a) • -- Procedure call
Handling arguments i.e. formal1 :=actual1;… ; formaln:=actualn Generalize notation a [x: y]to lists: use • a [a: b] as abbreviation for • (…((a [a1:b1])[a2:b2]) …[an:bn] • For example: a [r : a] • The calculus will treat • callr (a) as • r := a ; call r • (With recursion, possible loss of precision)
Call rule Body of r Formal arguments of r With arguments: • a » call r (v) = a[r: a] » r Without arguments: a » call r = a » r
Using the call rule • a » call r (a) = a [r: a] » r • Because of recursion, no longer just definition but equation • For entire set of procedures P, this gives a vector equation a » P = AL (a » P) • Interpret as fixpoint equation and solve iteratively • (Fixpoint exists: increasing sequence on finite set)
Object-oriented mechanisms: E4 E4: O-O E3: procedures E2:loops E1:cut E0:basic constructs • “General relativity”: • 1. Qualified expressions: x yCan be used as source (not target!) of assignments x := y z • 2. Qualified calls: callx r (v) • 3. Current
Assignment (original rule) Value of a » (x := y) a deprived of all pairs involving x All u aliased to yin b, plusy itself Example: x := y z All pairs [x, u] where u is either aliased to y in b or y itself This includes[x, y] ! a [x: y] = given b = a \- {x} then b ({x} (b / y) ) end
Assigning a qualified expression • x := x y x y x x z x does not get aliased tox y! (only to any z that was aliased tox y) := x y
Assignment rule revisited a deprived of all pairs involving x Value of a » (x := y) or an expression starting with x Example: x := y z a [x: y] = given b = a \–{x} then b ({x} (b / y)) end
Alias diagrams (E0 to E3) Value nodes Value nodes Value nodes Source node Single source node(represents stack) Each value node represents a set of possible run-time values Links: only from source tovalue nodes (will becomemore interesting with E4!) Edge label: set ofvariables; indicates theycan all be aliased to each other x , y y, z x, u, v
Alias diagrams (E4) Value nodes Value nodes Object nodes Source node x y x x z Links may now exist between value nodes(now called object nodes) Cycles possible (see next) := x y
New laws, inverse variables x Current= x Current x = x x’ x = Current x x’ = Current Current’ = Current
New form of call: qualified In E4: call x r (a, b, …)
Distribution operator: For a list a=<u, v, w, …>: x a = <xu, x v, x v, …> For a relation r in E E : x r = {[xu, x v] | [u, v] r} Example: x ( u, v, w, u, y ) = xu, x v, xw, xu, x y
Handling arguments (unqualified call) a » call r (a) = a [r: a] » r
Handling arguments: unqualified call Was written r a » callr (a) = a [ ` r: a] » callr )
Handling arguments: qualified call Current x’ x target a » call x r (a) = a [ xr: a] » call x r) • Treat • call x r (v) as • x formals := a ; callx r
Handling arguments: an example Current x’ x target Eiffel: x r (a, b) With, in a class C: r (t: T ; u: U) Handled as: x t := a x u := b callx r
Without arguments: unqualified call rule Body of r a » call r = a » r
Qualified call rule • Example: • d := c • x r (d) • with • r (u: U) • do • v := u • end • Handled as: • d := c • call • with • r • do • v := u • end Current , d c x r x’ x u, v u := x’ d target Inverse variable a » call x r= x ((x’ a) » r)
The rule in action • d := c • call • with • r • do • v := u • end • Alias relation: Current c , d , d c, d x r Current • Prefix with x’ : x’ x’ x u, x’c, x’ d x v, x u := x’ c x’c, x’d u, x’ c, x’ d c, x d x v, u, x’ c, x’d x’c, x’d target • Prefix with x : u, c x x’c, d x v, c, x xv, xu, c, d x a » call x r= x ((x’ a) » r)