Fundamentals of Programming Languages V Abstract Interpretation
Total Page:16
File Type:pdf, Size:1020Kb
Fundamentals of Programming Languages V Abstract Interpretation Guoqiang Li School of Software, Shanghai Jiao Tong University Assignment Assignment 2 is announced! Deadline Nov. 15 Reference Cousot P., Cousot R., Abstract Interpretation: A Unified Lattice Mode for Static Analysis of Programs by Construction or Approximation of Fixpoints, POPL’77 Steffen, B., Data Flow Analysis as Model Checking, TACS’91 Schmidt, D.A., Data Flow Analysis is Model Checking of Abstract Interpretations POPL’98 Lacey,D., Proving Correctness of Compiler Optimizations by Temporal Logic POPL’02 Turing Award 1986, Tony Hoare For his fundamental contributions to the definition and design of programming languages... 1991, Robin Milner For three distinct and complete achievements: 1) LCF, the mechanization of Scott’s Logic of Computable Functions, probably the first theoretically based yet practical tool for machine assisted proof construction; 2) ML, the first language to include polymorphic type inference together with a type-safe exception-handling mechanism; 3) CCS, a general theory of concurrency. In addition, he formulated and strongly advanced full abstraction, the study of the relationship between operational and denotational semantics... 1996, Amir Pnueli For seminal work introducing temporal logic into computing science and for outstanding contributions to program and systems verification... 2007, Edmund M. Clarke, E. Allen Emerson and Joseph Sifakis For their roles in developing model checking into a highly effective verification technology, widely adopted in the hardware and software industries... Abstract Interpretation in Practice Hardware Model Checking • Classical Model Checking (in Clarke’s book)(hardware model checking): Implementer and verifier are same. • During designing a system, each model is checked and refined. • A correct system implementation is finally obtained. • e.g. a modulo 8 counter 0 • v0 = :v0 0 • v1 = v0 ⊕ v1 0 • v2 = (v0 ^ v1) ⊕ v2 Software Model Checking • Modern Model Checking(software model checking): Implementer and verifier may be different. • Given a system implementation (e.g. a source code), construct a model and verify it. • Construction of models: manually/automatically. • e.g. • security protocols • real-time embedded systems • program analysis Classical Program Analysis • From POPA • From Dragon book • available expression analysis • dead code detection • reaching definition analysis • constant propagation • very busy expression analysis • common expression detection • live variable analysis • code motion • Concurrent program analysis • Object-oriented program analysis • deadlock, livelock • point-to analysis • data race • escape analysis • • inference of class invariants Aspect-oriented program analysis • shape analysis • ????? Brief Idea of Abstract Interpretation • In order to know properties of a program, execute it for all possible inputs. • It will not terminate since domains of variables are often infinite. • Abstract input data to a suitable finite domain. Then finiteness guarantees termination. • Functions needs to be reinterpreted. • Examples: • Security protocols: a principal may receive infinitely many messages from environments. (some affects the principal, some are not) • Embedded systems: a scheduler may have to schedule infinitely many released tasks. (when time exceeds, all tasks have the same results) Abstract Interpretation on Program Analysis: Examples • Even/Odd analysis: x × (x + 1) is even. • Abstraction: • Even • Odd • Interpretation: • Even + Even = Even, Even + Odd = Odd, • Even × Odd = Even,… • Positive/nagative analysis • Abstraction: • Pos • Neg • Zero • Interpretation: • Pos + Pos = Pos, Pos + Neg = fPos; Zero; Negg,… • Neg × Neg = Pos, Pos × Zero = Zero,… • The result will be a (sound) approximation. Sound Abstraction The result is an approximation Completeness: sufficient condition • if no errors detected there are really no errors. Soundness: necessary condition • if errors detected they are really errors. f • Data Abstraction : abs : D ! Abs DD • Typical concretization : con = abs−1 con abs abs • abs · con id ^ con · abs ⊇ id ( = Abs) ( D) Int(f) guarantee soundness. Abs Abs 1 Abstract Interpretation Define abstraction and finite abstract domain abs : D ! Abs Interpret primitive functions +; ×; if ; =; ≥; ≤;::: Execute looping structures via program semantics while, function call Program Semantics Semantics: interpret a program (syntax) as an input-output relation (semantics). Intuitively, the input-output relation is a relation between input environment and output environment of variables in a scopes. Looping structure is solved as least fixed point. Theorem Tarski’s least fixed point theorem: Assume τ : P(D) ! P(D) is a monotonic, then there exists the S i unique least fixed point, which is computed as i τ (;). We will come back to this theorem again! This can be read as ThisThis cancan bebe readread asas • UnfoldExample: while sentence Computing as repeated applications Sum of if. •• UnfoldUnfold whilewhile sentencesentence asas repeatedrepeated applicationsapplications ofof ifif.. env = [n,i,s,c] env = [n,i,s,c] envτ: env = [n,i,s,c]×envenv =→ [n; i;envs; c] ×env /* sum */ if ¬c τ: env×env → env×env /* sum */ τ: τenv: P×(envenv× env→) !envP(env×env× env) /*1: sumread */ n; if ¬c 1: read n; ifthen ¬ cskip 1:2: readi:=0; n; then skip step 0: φ 2: i:=0; thenelse skip τ step 0: φ 2:3: i:=0;S:=0; else step 0: φ 3: S:=0; elseS:=S+i; τ step 1: 3:4: S:=0;c:=True; S:=S+i; τ step 1: 4: c:=True; S:=S+i;i:=i+1; step{([ 01:,0, 0,True], 4:5: c:=True;while c i:=i+1; {([0,0,0,True], 5: while c i:=i+1;c:=i<=n; {([[00,0,,1,00,True],,False])} 5: whiledo c c:=i<=n; τ2 [0,1,0,False])} do c:=i<=n;if ¬c τ2 [0,1,0,False])} 6: doS=S+i; if ¬c τ2 step 2: 6: S=S+i; ifthen ¬ cskip step 2: 6:7: S=S+i;i:=i+1; then skip step{([ 02:,0, 0,True], 7:8: i:=i+1;c:=i<=n; thenelse skip {([0,0,0,True], 7: i:=i+1; else {([[00,0,,1,00,True],,False]), 8: c:=i<=n;od; elseS:=S+i; [0,1,0,False]), 8: c:=i<=n; S:=S+i; ([01,1,,0,0,False]),,True], 9: od;write S S:=S+i;i:=i+1; ([1,0,0,True], od; i:=i+1; ([[11,0,,2,01,True],,False])} 9: write S i:=i+1;c:=i<=n; [1,2,1,False])} 9: write S c:=i<=n; [1,2,1,False])} Unfoldc:=i<=n; while-loop Unfold while-loop Unfold while-loop ThisThis cancan bebe readread asas Example:Example: positive/negative Positive/Negative analysis •• UnfoldUnfold whilewhile sentencesentence asas repeatedrepeated applicationsapplications ofof ifif.. • Abstraction: NatAnalysis→ {Pos, Zeroenv =}, [n,i,s,c] Bool → Bool env = [n,i,s,c] τ: env×env → env×env /*Abstraction: sum */ Nat ! fPos; Zerog τ: env×env → env×env /* sum */ if ¬c env = [n,i,s,c] 1: read n; if ¬c env = [n; i; s; c] 1: read n; then skip τ: env×env → env×env 2:/* i:=0;sum */ ifthen ¬c skip τ : P(stepenv × 0:env )φ! P(env × env) 2: i:=0; else step 0: φ 3:1: S:=0;read n; thenelse skip τ 3: S:=0; S:=S+i; τ step 0: φstep 1: 4:2: c:=True;i:=0; elseS:=S+i; step 1: 4: c:=True; i:=i+1; τ’ {([0,0,0,True], 5:3: whileS:=0; c S:=S+i;i:=i+1; step 1: {([0,0,0,True], 5: while c c:=i<=n; [0,1,0,False])} 4: doc:=True; i:=i+1;c:=i<=n; {([τ2Zero,Zero,[0,1,0Zero,False])},True], do if ¬c τ2 6:5: S=S+i;while c c:=i<=n;¬ [Zero,Pos,Zero,False])} 6: S=S+i; if c step 2: 7: i:=i+1;do then skip τ’2 step 2: 7: i:=i+1; ifthen ¬c skip {([0,0,0,True], 8:6: c:=i<=n;S=S+i; else step 2: {([0,0,0,True], 8: c:=i<=n; thenelse skip [0,1,0,False]), 7: od;i:=i+1; S:=S+i; {([Zero,Zero,[0,1,0Zero,False]),,True], od; elseS:=S+i; ([1,0,0,True], 9:8: writec:=i<=n; S i:=i+1; [Zero(,Pos,[1,0,Zero0,True],,False]), S:=S+i;i:=i+1; [1,2,1,False])} 9: writeod; S c:=i<=n; [Pos,Zero,[1,2,Zero1,False])},True], i:=i+1;c:=i<=n; 9: write S [Pos,Pos,Pos, c:=i<=n;Unfold while-loop Unfold while-loop True/False])} Converged!Least Fixed (analyzed!) Point! ThisThis cancan bebe readread asas VariationExample: of Variationabstraction •• UnfoldUnfold whilewhile sentencesentence asas repeatedrepeated applicationsapplications ofof ifif.. • Abstraction: Nat → {Pos, Zero}, Bool → Bool env = [n,i,s,c] env = [n,i,s,c] Abstraction: Nat ! fMany; One; Zerog τ: env×env → env×env • Abstraction:/* sum */ Nat → {Many,Oneτ:,Zero env×},env Bool→ env→×envBool /* sum */ if ¬c env = [n; i; s; c] 1: read n; if ¬c 1:/* readsum */n; then skip τ : (env × env) ! (env × env) 2: i:=0; ifthen ¬c skip P step 0: φ P 2:1: i:=0;read n; else step 0: φ 3: S:=0; thenelse skip τ step 0: φ 3:2: S:=0;i:=0; S:=S+i; τ step 1: 4: c:=True; elseS:=S+i; τ step 1: 4:3: c:=True;S:=0; i:=i+1; ’ step 1: {([0,0,0,True], 5: while c S:=S+i;i:=i+1; {([0,0,0,True], 5:4: whilec:=True; c c:=i<=n; {([Zero,Zero,[0,1,Zero0,False])},True], do i:=i+1;c:=i<=n; τ2 [0,1,0,False])} 5: dowhile c c:=i<=n;if ¬c τ[2Zero,One,Zero,False])} 6: S=S+i; if ¬c τ’2 step 2: 6: S=S+i;do if then¬c skip step 2: 6:7: S=S+i;i:=i+1; then skip {([0,0,0,True], 7: i:=i+1; thenelse skip step 2: {([0,0,0,True], 7:8: i:=i+1;c:=i<=n; else [0,1,0,False]), 8: c:=i<=n; elseS:=S+i; {([Zero,Zero,[0,1,0Zero,False]),,True], 8: c:=i<=n;od; S:=S+i; ([1,0,0,True], od; S:=S+i;i:=i+1; [Zero(,One,[1,0,Zero0,True],,False]), 9: od;write S i:=i+1; [1,2,1,False])} 9: write S i:=i+1;c:=i<=n; [One,Zero,[1,2,Zero1,False])},True], 9: write S c:=i<=n; [One,One,One,True])} c:=i<=n;Unfold while-loop Unfold while-loop Finer abstraction will find more; but this is trade-off Program Analysis = Abstract Interpretation + Model Checking Model Checking Based Program Analysis • Each line of a program is regarded as nondeterministic transition.