a simple and extensible approach to program analysis
play

A Simple and Extensible Approach to Program Analysis David Darais - PowerPoint PPT Presentation

A Simple and Extensible Approach to Program Analysis David Darais University of Maryland University of Vermont Does my program cause a runtime error? Does my program allocate too much? Does my program sanitize all untrusted inputs? Does my


  1. A Simple and Extensible Approach to Program Analysis David Darais University of Maryland University of Vermont

  2. Does my program cause a runtime error? Does my program allocate too much? Does my program sanitize all untrusted inputs? Does my program have any data races?

  3. ☹ My PL Doesn’t Have a Program Analyzer

  4. πŸ€• Should I Write My Own Program Analyzer?

  5. πŸ˜‹ Writing Your Own Program Analyzer is Easy If you know how to write an interpreter

  6. Abstracting Definitional Interpreters Interpreter => Analyzer Sound Terminating Precise Extensible

  7. 
 
 Hypothesis: 
 It’s easier to write a precise semantics than an abstract semantics. Approach: 
 Write, maintain and debug one precise semantics. Systematically derive multiple static analyzers.

  8. Concrete Interpreter Static Analyzer

  9. Concrete 
 Interpreter

  10. if (N β‰  0){ x ≔ 100/N }

  11. if (N β‰  0){ x ≔ 100/N } N=1

  12. if (N β‰  0){ x ≔ 100/N } N=1 if (true){ x ≔ 100/N } N=1

  13. if (N β‰  0){ x ≔ 100/N } N=1 if (true){ x ≔ 100/N } N=1 x ≔ 100/N N=1

  14. if (N β‰  0){ x ≔ 100/N } N=1 if (true){ x ≔ 100/N } N=1 x ≔ 100/N N=1 100 N=1 x=100

  15. eval : exp Γ— env ⇀ val Γ— env eval(Var(x), ρ ) ≔ ( ρ (x), ρ ) eval(Assign(x,e), ρ ) ≔ (v, ρ′ ) ≔ eval(e, ρ ) (v, ρ′ [x ↦ v]) env ≔ var ⇀ val eval(Op(o,e ₁ ,e β‚‚ ), ρ ) ≔ val ≔ 𝔺 ⊎ β„€ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) (v β‚‚ , ρ″ ) ≔ eval(e β‚‚ , ρ′ ) Ξ΄ : op Γ— val Γ— val ⇀ val ( Ξ΄ (o,v ₁ ,v β‚‚ ), ρ″ ) eval(If(e ₁ ,e β‚‚ ,e ₃ ), ρ ) ≔ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) cases v ₁ = true β‡’ eval(e β‚‚ , ρ′ ) v ₁ = false β‡’ eval(e ₃ , ρ′ )

  16. eval : exp Γ— env ⇀ val Γ— env eval(Var(x), ρ ) ≔ ( ρ (x), ρ ) eval(Assign(x,e), ρ ) ≔ (v, ρ′ ) ≔ eval(e, ρ ) (v, ρ′ [x ↦ v]) env ≔ var ⇀ val eval(Op(o,e ₁ ,e β‚‚ ), ρ ) ≔ val ≔ 𝔺 ⊎ β„€ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) (v β‚‚ , ρ″ ) ≔ eval(e β‚‚ , ρ′ ) Ξ΄ : op Γ— val Γ— val ⇀ val ( Ξ΄ (o,v ₁ ,v β‚‚ ), ρ″ ) eval(If(e ₁ ,e β‚‚ ,e ₃ ), ρ ) ≔ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) cases v ₁ = true β‡’ eval(e β‚‚ , ρ′ ) v ₁ = false β‡’ eval(e ₃ , ρ′ )

  17. eval : exp Γ— env ⇀ val Γ— env eval(Var(x), ρ ) ≔ ( ρ (x), ρ ) eval(Assign(x,e), ρ ) ≔ (v, ρ′ ) ≔ eval(e, ρ ) (v, ρ′ [x ↦ v]) env ≔ var ⇀ val eval(Op(o,e ₁ ,e β‚‚ ), ρ ) ≔ val ≔ 𝔺 ⊎ β„€ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) (v β‚‚ , ρ″ ) ≔ eval(e β‚‚ , ρ′ ) Ξ΄ : op Γ— val Γ— val ⇀ val ( Ξ΄ (o,v ₁ ,v β‚‚ ), ρ″ ) eval(If(e ₁ ,e β‚‚ ,e ₃ ), ρ ) ≔ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) cases v ₁ = true β‡’ eval(e β‚‚ , ρ′ ) v ₁ = false β‡’ eval(e ₃ , ρ′ )

  18. eval : exp Γ— env ⇀ val Γ— env eval(Var(x), ρ ) ≔ ( ρ (x), ρ ) eval(Assign(x,e), ρ ) ≔ (v, ρ′ ) ≔ eval(e, ρ ) (v, ρ′ [x ↦ v]) env ≔ var ⇀ val eval(Op(o,e ₁ ,e β‚‚ ), ρ ) ≔ val ≔ 𝔺 ⊎ β„€ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) (v β‚‚ , ρ″ ) ≔ eval(e β‚‚ , ρ′ ) Ξ΄ : op Γ— val Γ— val ⇀ val ( Ξ΄ (o,v ₁ ,v β‚‚ ), ρ″ ) eval(If(e ₁ ,e β‚‚ ,e ₃ ), ρ ) ≔ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) cases v ₁ = true β‡’ eval(e β‚‚ , ρ′ ) v ₁ = false β‡’ eval(e ₃ , ρ′ )

  19. eval : exp Γ— env ⇀ val Γ— env eval(Var(x), ρ ) ≔ ( ρ (x), ρ ) eval(Assign(x,e), ρ ) ≔ (v, ρ′ ) ≔ eval(e, ρ ) (v, ρ′ [x ↦ v]) env ≔ var ⇀ val eval(Op(o,e ₁ ,e β‚‚ ), ρ ) ≔ val ≔ 𝔺 ⊎ β„€ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) (v β‚‚ , ρ″ ) ≔ eval(e β‚‚ , ρ′ ) Ξ΄ : op Γ— val Γ— val ⇀ val ( Ξ΄ (o,v ₁ ,v β‚‚ ), ρ″ ) eval(If(e ₁ ,e β‚‚ ,e ₃ ), ρ ) ≔ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) cases v ₁ = true β‡’ eval(e β‚‚ , ρ′ ) v ₁ = false β‡’ eval(e ₃ , ρ′ )

  20. eval : exp Γ— env ⇀ val Γ— env eval(Var(x), ρ ) ≔ ( ρ (x), ρ ) eval(Assign(x,e), ρ ) ≔ (v, ρ′ ) ≔ eval(e, ρ ) (v, ρ′ [x ↦ v]) env ≔ var ⇀ val eval(Op(o,e ₁ ,e β‚‚ ), ρ ) ≔ val ≔ 𝔺 ⊎ β„€ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) (v β‚‚ , ρ″ ) ≔ eval(e β‚‚ , ρ′ ) Ξ΄ : op Γ— val Γ— val ⇀ val ( Ξ΄ (o,v ₁ ,v β‚‚ ), ρ″ ) eval(If(e ₁ ,e β‚‚ ,e ₃ ), ρ ) ≔ (v ₁ , ρ′ ) ≔ eval(e ₁ , ρ ) cases v ₁ = true β‡’ eval(e β‚‚ , ρ′ ) v ₁ = false β‡’ eval(e ₃ , ρ′ )

  21. Concrete 
 Interpreter

  22. Monadic Concrete 
 Interpreter

  23. eval : exp Γ— env ⇀ val Γ— env

  24. eval : exp Γ— env ⇀ val Γ— env β‰ˆ eval : exp β†’ M(val) M(val) ≔ env ⇀ val Γ— env

  25. eval : exp β†’ M(val) eval(Var(x)) ≔ do ρ ← get-env return ρ (x) eval(Assign(x,e)) ≔ do v ← eval(e) ρ ← get-env env ≔ var ⇀ val put-env ρ [x ↦ v] val ≔ 𝔺 ⊎ β„€ return v eval(Op(o,e ₁ ,e β‚‚ )) ≔ do Ξ΄ : op Γ— val Γ— val ⇀ val 
 v ₁ ← eval(e ₁ ) v β‚‚ ← eval(e β‚‚ ) M(A) ≔ env ⇀ A Γ— env return Ξ΄ (o,v ₁ ,v β‚‚ ) eval(If(e ₁ ,e β‚‚ ,e ₃ )) ≔ do v ₁ ← eval(e ₁ ) cases v ₁ = true β‡’ eval(e β‚‚ ) v ₁ = false β‡’ eval(e ₃ )

  26. eval : exp β†’ M(val) eval(Var(x)) ≔ do ρ ← get-env return ρ (x) eval(Assign(x,e)) ≔ do v ← eval(e) ρ ← get-env env ≔ var ⇀ val put-env ρ [x ↦ v] val ≔ 𝔺 ⊎ β„€ return v eval(Op(o,e ₁ ,e β‚‚ )) ≔ do Ξ΄ : op Γ— val Γ— val ⇀ val 
 v ₁ ← eval(e ₁ ) v β‚‚ ← eval(e β‚‚ ) M(A) ≔ env ⇀ A Γ— env return Ξ΄ (o,v ₁ ,v β‚‚ ) eval(If(e ₁ ,e β‚‚ ,e ₃ )) ≔ do v ₁ ← eval(e ₁ ) cases v ₁ = true β‡’ eval(e β‚‚ ) v ₁ = false β‡’ eval(e ₃ )

  27. eval : exp β†’ M(val) eval(Var(x)) ≔ do ρ ← get-env return ρ (x) eval(Assign(x,e)) ≔ do v ← eval(e) ρ ← get-env env ≔ var ⇀ val put-env ρ [x ↦ v] val ≔ 𝔺 ⊎ β„€ return v eval(Op(o,e ₁ ,e β‚‚ )) ≔ do Ξ΄ : op Γ— val Γ— val ⇀ val 
 v ₁ ← eval(e ₁ ) v β‚‚ ← eval(e β‚‚ ) M(A) ≔ env ⇀ A Γ— env return Ξ΄ (o,v ₁ ,v β‚‚ ) eval(If(e ₁ ,e β‚‚ ,e ₃ )) ≔ do v ₁ ← eval(e ₁ ) cases v ₁ = true β‡’ eval(e β‚‚ ) v ₁ = false β‡’ eval(e ₃ )

  28. eval : exp β†’ M(val) eval(Var(x)) ≔ do ρ ← get-env return ρ (x) eval(Assign(x,e)) ≔ do v ← eval(e) ρ ← get-env env ≔ var ⇀ val put-env ρ [x ↦ v] val ≔ 𝔺 ⊎ β„€ return v eval(Op(o,e ₁ ,e β‚‚ )) ≔ do Ξ΄ : op Γ— val Γ— val ⇀ val 
 v ₁ ← eval(e ₁ ) v β‚‚ ← eval(e β‚‚ ) M(A) ≔ env ⇀ A Γ— env return Ξ΄ (o,v ₁ ,v β‚‚ ) eval(If(e ₁ ,e β‚‚ ,e ₃ )) ≔ do v ₁ ← eval(e ₁ ) cases v ₁ = true β‡’ eval(e β‚‚ ) v ₁ = false β‡’ eval(e ₃ )

  29. if (N=0){ x ≔ 100/N }

  30. if (N=0){ x ≔ 100/N } N=0 βœ—

  31. if (N=0){ x ≔ 100/N } N=1 βœ“

  32. if (N=0){ x ≔ 100/N } N=ANY ?

  33. Monadic Concrete 
 Interpreter

  34. Monadic Abstract 
 Interpreter

  35. Abstract Values Join Results Variable Refinement

  36. β„€ ⌲ {-,0,+} 2 / ( 3 - 1 ) {+} / ({+} - {+}) {+} / {-,0,+} . βœ“ {+,-} OR βœ—

  37. β„€ ⌲ {-,0,+} 2 / ( 3 - 1 ) {+} / ({+} - {+}) {+} / {-,0,+} . βœ“ {+,-} OR βœ—

  38. β„€ ⌲ {-,0,+} 2 / ( 3 - 1 ) {+} / ({+} - {+}) {+} / {-,0,+} . βœ“ {+,-} OR βœ—

  39. β„€ ⌲ {-,0,+} 2 / ( 3 - 1 ) {+} / ({+} - {+}) {+} / {-,0,+} . βœ“ {+,-} OR βœ—

  40. β„€ ⌲ {-,0,+} 2 / ( 3 - 1 ) {+} / ({+} - {+}) {+} / {-,0,+} . βœ“ {+,-} OR βœ—

  41. eval : exp β†’ M(val) eval(Var(x)) ≔ do ρ ← get-env return ρ (x) eval(Assign(x,e)) ≔ do v ← eval(e) ρ ← get-env put-env ρ [x ↦ v] return v env ≔ var β†’ val eval(Op(o,e ₁ ,e β‚‚ )) ≔ do val ≔ β„˜ ( 𝔺 ) ⊎ β„˜ ({-,0,+}) v ₁ ← eval(e ₁ ) v β‚‚ ← eval(e β‚‚ ) Ξ΄ : op Γ— val Γ— val β†’ val Γ— 𝔺 (v ₃ ,err) ≔ Ξ΄ (o,v ₁ ,v β‚‚ ) 
 ⟦ _ ⟧ : val β†’ β„˜ ( 𝔺 ) join-cases refine : exp Γ— 𝔺 β†’ M(void) 
 err = true β‡’ fail always β‡’ return v ₃ M(A) ≔ env β†’ β„˜ (A Γ— env) Γ— 𝔺 eval(If(e ₁ ,e β‚‚ ,e ₃ )) ≔ do v ₁ ← eval(e ₁ ) Could the operation fail? join-cases ⟦ v β‚βŸ§ βˆ‹ true β‡’ do refine (e ₁ ,true) eval(e β‚‚ ) ⟦ v β‚βŸ§ βˆ‹ false β‡’ do refine (e ₁ ,false) eval(e ₃ )

  42. Abstract Values Join Results Variable Refinement

  43. eval : exp β†’ M(val) eval(Var(x)) ≔ do ρ ← get-env return ρ (x) eval(Assign(x,e)) ≔ do v ← eval(e) ρ ← get-env put-env ρ [x ↦ v] return v env ≔ var β†’ val eval(Op(o,e ₁ ,e β‚‚ )) ≔ do val ≔ β„˜ ( 𝔺 ) ⊎ β„˜ ({-,0,+}) v ₁ ← eval(e ₁ ) v β‚‚ ← eval(e β‚‚ ) Ξ΄ : op Γ— val Γ— val β†’ val Γ— 𝔺 (v ₃ ,err) ≔ Ξ΄ (o,v ₁ ,v β‚‚ ) 
 ⟦ _ ⟧ : val β†’ β„˜ ( 𝔺 ) join-cases refine : exp Γ— 𝔺 β†’ M(void) 
 err = true β‡’ fail always β‡’ return v ₃ M(A) ≔ env β†’ β„˜ (A Γ— env) Γ— 𝔺 eval(If(e ₁ ,e β‚‚ ,e ₃ )) ≔ do v ₁ ← eval(e ₁ ) join-cases ⟦ v β‚βŸ§ βˆ‹ true β‡’ do refine (e ₁ ,true) eval(e β‚‚ ) ⟦ v β‚βŸ§ βˆ‹ false β‡’ do refine (e ₁ ,false) eval(e ₃ )

  44. Abstract Values Join Results Variable Refinement

  45. if (N β‰  0){ x ≔ 100/N } N=ANY

  46. if (N β‰  0){ x ≔ 100/N } N=ANY x ≔ 100/N N ∈ {-,+}

Recommend


More recommend