static analysis of dynamically typed languages made easy
play

Static Analysis of Dynamically Typed Languages made Easy - PowerPoint PPT Presentation

Static Analysis of Dynamically Typed Languages made Easy Yin Wang School of Informatics and Computing Indiana University Overview Work done as two internships at


  1. Static ¡Analysis ¡of ¡Dynamically ¡ Typed ¡Languages ¡made ¡Easy Yin ¡Wang School ¡of ¡Informatics ¡and ¡Computing Indiana ¡University

  2. Overview ¡ Work ¡done ¡as ¡two ¡internships ¡at ¡Google ¡(2009 ¡ summer ¡and ¡2010 ¡summer) ¡ Motivation: § The ¡Grok ¡Project: ¡static ¡analysis ¡of ¡all ¡code ¡at ¡ Google ¡(C++, ¡Java, ¡JavaScript, ¡Python, ¡Sawzall, ¡ Protobuf ¡...) § Initial ¡goal ¡was ¡not ¡ambitious: ▪ Implement ¡“IDE-­‑like” ¡code-­‑browsing ▪ Turns ¡out ¡to ¡be ¡hard ¡for ¡Python

  3. Achieved ¡Goals ¡ Build ¡high-­‑accuracy ¡semantic ¡indexes ¡ Detect ¡and ¡report ¡semantic ¡bugs § type ¡errors § missing ¡return ¡statement § unreachable ¡code § ...

  4. Demo ¡Time Go

  5. Problems ¡Faced ¡by ¡Static ¡Analysis ¡of ¡ Dynamically ¡Typed ¡Languages

  6. 1. ¡Problems ¡with ¡Dynamic ¡Typing ¡ Dynamic ¡typing ¡makes ¡it ¡ hard ¡to ¡resolve ¡some ¡ names def ¡h(x): ¡ Mostly ¡happen ¡in ¡ polymorphic ¡functions ¡ ¡return ¡x.z

  7. 1. ¡Problems ¡with ¡Dynamic ¡Typing ¡ Dynamic ¡typing ¡makes ¡it ¡ hard ¡to ¡resolve ¡some ¡ names def ¡h(x): ¡ Mostly ¡happen ¡in ¡ polymorphic ¡functions ¡ ¡return ¡x.z Q: ¡Where ¡is ¡‘z’ ¡defined? A: ¡... ¡wherever ¡we ¡defined ¡ ¡‘x’

  8. 1. ¡Problems ¡with ¡Dynamic ¡Typing ¡ Dynamic ¡typing ¡makes ¡it ¡ Q: ¡What ¡is ¡‘x’ ¡? hard ¡to ¡resolve ¡some ¡ A: ¡Uhh... names def ¡h(x): ¡ Mostly ¡happen ¡in ¡ polymorphic ¡functions ¡ ¡return ¡x.z Q: ¡Where ¡is ¡‘z’ ¡defined? A: ¡... ¡wherever ¡we ¡defined ¡ ¡‘x’

  9. 1. ¡Problems ¡with ¡Dynamic ¡Typing ¡ Dynamic ¡typing ¡makes ¡it ¡ Q: ¡What ¡is ¡‘x’ ¡? hard ¡to ¡resolve ¡some ¡ A: ¡Uhh... names def ¡h(x): ¡ Mostly ¡happen ¡in ¡ polymorphic ¡functions ¡ ¡return ¡x.z Solution: ¡ use ¡a ¡static ¡type ¡system • use ¡inter-­‑procedural ¡ • Q: ¡Where ¡is ¡‘z’ ¡defined? analysis ¡to ¡infer ¡types A: ¡... ¡wherever ¡we ¡defined ¡ ¡‘x’

  10. Static ¡Type ¡System ¡for ¡Python Mostly ¡a ¡usual ¡type ¡system, ¡with ¡two ¡extras: ¡ union ¡and ¡ dict ¡ primitive ¡types ¡ class ¡types § int, ¡str, ¡float, ¡bool § ClassA, ¡ClassB ¡ tuple ¡types ¡ union ¡types § (int,float), ¡(A, ¡B, ¡C) § {int ¡| ¡str}, ¡{A ¡| ¡B ¡| ¡C} ¡ list ¡types ¡ recursive ¡types § [int], ¡[bool], ¡[(int,bool)] § #1(int, ¡1), ¡#2(int ¡-­‑> ¡2) ¡ dict ¡types ¡ function ¡types § {int ¡=> ¡str}, ¡{A ¡=> ¡B} § int ¡-­‑> ¡bool, ¡A ¡-­‑> ¡B

  11. 2. ¡Problems ¡with ¡Control-­‑Flow ¡Graph CFGs ¡are ¡tricky ¡to ¡build ¡for ¡high-­‑ ¡ order ¡programs def ¡g( f ,x): Attempts ¡to ¡build ¡CFGs ¡have ¡led ¡to ¡ ¡ ¡ ¡ ¡ ¡return ¡ f (x) complications ¡and ¡limitations ¡in ¡ control-­‑flow ¡analysis def ¡h1(x): ¡ ¡ ¡ ¡return ¡x+1 § Shivers ¡1988, ¡1991 build ¡CFG ¡after ¡CPS def ¡h2(x): ▪ § Might ¡& ¡Shivers ¡2006,2007 ¡ ¡ ¡ ¡return ¡x+2 solve ¡problems ¡introduced ¡by ¡CFG ▪ § Vardoulakis ¡& ¡Shivers ¡2010,2011 solve ¡problems ¡introduced ¡by ¡CPS ▪

  12. 2. ¡Problems ¡with ¡Control-­‑Flow ¡Graph Where ¡is ¡the ¡ CFG ¡target? CFGs ¡are ¡tricky ¡to ¡build ¡for ¡high-­‑ ¡ order ¡programs def ¡g( f ,x): Attempts ¡to ¡build ¡CFGs ¡have ¡led ¡to ¡ ¡ ¡ ¡ ¡ ¡return ¡ f (x) complications ¡and ¡limitations ¡in ¡ control-­‑flow ¡analysis def ¡h1(x): ¡ ¡ ¡ ¡return ¡x+1 § Shivers ¡1988, ¡1991 build ¡CFG ¡after ¡CPS def ¡h2(x): ▪ § Might ¡& ¡Shivers ¡2006,2007 ¡ ¡ ¡ ¡return ¡x+2 solve ¡problems ¡introduced ¡by ¡CFG ▪ § Vardoulakis ¡& ¡Shivers ¡2010,2011 solve ¡problems ¡introduced ¡by ¡CPS ▪

  13. 2. ¡Problems ¡with ¡Control-­‑Flow ¡Graph Where ¡is ¡the ¡ CFG ¡target? CFGs ¡are ¡tricky ¡to ¡build ¡for ¡high-­‑ ¡ order ¡programs def ¡g( f ,x): Solution: ¡ Attempts ¡to ¡build ¡CFGs ¡have ¡led ¡to ¡ ¡ ¡ ¡ ¡ ¡return ¡ f (x) Don’t ¡CPS ¡the ¡input ¡program • complications ¡and ¡limitations ¡in ¡ Don’t ¡try ¡constructing ¡the ¡CFG • control-­‑flow ¡analysis def ¡h1(x): Use ¡direct-­‑style, ¡recursive ¡ • ¡ ¡ ¡ ¡return ¡x+1 abstract ¡interpreter § Shivers ¡1988, ¡1991 build ¡CFG ¡after ¡CPS def ¡h2(x): ▪ § Might ¡& ¡Shivers ¡2006,2007 ¡ ¡ ¡ ¡return ¡x+2 solve ¡problems ¡introduced ¡by ¡CFG ▪ § Vardoulakis ¡& ¡Shivers ¡2010,2011 solve ¡problems ¡introduced ¡by ¡CPS ▪

  14. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion class ¡A: ¡ ¡ ¡ ¡x ¡= ¡1 ¡ obj ¡= ¡A() ¡ obj. y ¡= ¡3 ¡ print ¡obj.x, ¡obj. y

  15. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion Solution: ¡ class ¡A: ¡ ¡ ¡ ¡x ¡= ¡1 ¡ obj ¡= ¡A() ¡ obj. y ¡= ¡3 ¡ print ¡obj.x, ¡obj. y

  16. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion Solution: ¡ class ¡A: ¡ ¡ ¡ ¡x ¡= ¡1 ¡ obj ¡= ¡A() ¡ obj. y ¡= ¡3 ¡ print ¡obj.x, ¡obj. y

  17. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion Solution: ¡ class ¡A: create ¡“abstract ¡objects” ¡at ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡1 ¡ constructor ¡calls obj ¡= ¡A() ¡ obj. y ¡= ¡3 ¡ print ¡obj.x, ¡obj. y

  18. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion Solution: ¡ class ¡A: create ¡“abstract ¡objects” ¡at ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡1 ¡ constructor ¡calls obj ¡= ¡A() ¡ obj. y ¡= ¡3 ¡ print ¡obj.x, ¡obj. y

  19. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion Solution: ¡ class ¡A: create ¡“abstract ¡objects” ¡at ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡1 ¡ constructor ¡calls obj ¡= ¡A() ¡ Actually ¡change ¡the ¡ ¡ abstract ¡objects ¡when ¡fields ¡ obj. y ¡= ¡3 ¡ are ¡created print ¡obj.x, ¡obj. y

  20. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion Solution: ¡ class ¡A: create ¡“abstract ¡objects” ¡at ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡1 ¡ constructor ¡calls obj ¡= ¡A() ¡ Actually ¡change ¡the ¡ ¡ abstract ¡objects ¡when ¡fields ¡ obj. y ¡= ¡3 ¡ are ¡created print ¡obj.x, ¡obj. y

  21. 3. ¡Problems ¡with ¡Dynamic ¡Field ¡Creation/ Deletion Solution: ¡ class ¡A: create ¡“abstract ¡objects” ¡at ¡ ¡ ¡ ¡ ¡ ¡x ¡= ¡1 ¡ constructor ¡calls obj ¡= ¡A() ¡ Actually ¡change ¡the ¡ ¡ abstract ¡objects ¡when ¡fields ¡ obj. y ¡= ¡3 ¡ are ¡created print ¡obj.x, ¡obj. y Classes ¡are ¡not ¡affect ¡by ¡the ¡ ¡ change

  22. 4. ¡Problems ¡with ¡More ¡Powerful Dynamic ¡Features ¡ direct ¡operations ¡on ¡ __dict__ ¡(e.g. ¡setattr, ¡ delattr, ¡...) ¡ dynamic ¡object ¡ reparenting ¡ import ¡hacks ¡ eval ¡ ...

  23. 4. ¡Problems ¡with ¡More ¡Powerful Dynamic ¡Features ¡ direct ¡operations ¡on ¡ __dict__ ¡(e.g. ¡setattr, ¡ delattr, ¡...) ¡ dynamic ¡object ¡ reparenting Solution: ¡ import ¡hacks ¡ ¡“ Python ¡Style ¡Guide ” ¡ eval ¡ ...

  24. Overall ¡Structure ¡of ¡Analysis Type ¡Env Stack AST ¡node (Env) (Env) (Expr) Abstract ¡ Interpreter ¡(AI) |-­‑ ¡(Expr,Env,Stk) ¡-­‑> ¡Type Type

  25. Overall ¡Structure ¡of ¡Analysis Type ¡Env Stack AST ¡node (Env) (Env) (Expr) direct-­‑style • recursive • Abstract ¡ Interpreter ¡(AI) |-­‑ ¡(Expr,Env,Stk) ¡-­‑> ¡Type Type

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend