lua
play

Lua Jan Midtgaard Dagstuhl Seminar 16131 State of affairs, PL-wise - PowerPoint PPT Presentation

A Static Type Analysis for Lua Lua Jan Midtgaard Dagstuhl Seminar 16131 State of affairs, PL-wise Good news: Many of todays popular programming languages (JavaScript, Python, Lua, . . . ) are dynamically typed: they enable rapid


  1. A Static Type Analysis for Lua Lua Jan Midtgaard Dagstuhl Seminar 16131

  2. State of affairs, PL-wise Good news: Many of today’s popular programming languages (JavaScript, Python, Lua, . . . ) are dynamically typed: they enable rapid prototyping and � flexible software development � Bad news: In terms of software guarantees, that means we don’t get many guarantees from the compiler � bugs may surface late in the development cycle � 2 / 30

  3. Just for fun? 3 / 30

  4. Just for fun? 3 / 30

  5. This talk More good news: Static analysis to the rescue! Today: Developing a static analysis (lattice) for inferring types of Lua programs Lua Result: A tool 4 / 30

  6. Lua? 5 / 30 Lua games picture slide courtesy of Ierusalimschy, de Figueiredo, and Celes. The above company names and logos are trademarked.

  7. Lua, in brief Developed in Brazil, hence the name (’lua’ = ’moon’) � Similar to languages like Python and JavaScript � Lightweight language (few, well-chosen features): � Imperative – Dynamically typed – Builtin tables (associative arrays) – First-class functions – . . . – Multiparadigm (FP , OO, . . . ) � Lightweight, cross-platform implementation � Standalone and easily embeddable � 6 / 30

  8. Example: Sets of numbers (Ierusalimschy’13) Set = {} -- create a new set with the values of a given list function Set.new (l) local set = {} for _, v in ipairs (l) do set[v] = true end return set end function Set.union (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = Set.union(s1,s2) 7 / 30

  9. Example: Sets of numbers, desugared Set = {} -- create a new set with the values of a given list Set.new = function (l) local set = {} for _, v in ipairs (l) do set[v] = true end return set end Set.union = function (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = Set.union(s1,s2) 8 / 30

  10. Example: Sets of numbers, desugared Set = {} -- create a new set with the values of a given list Set.new = function (l) builtin tables local set = {} for _, v in ipairs (l) do set[v] = true end return set end Set.union = function (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = Set.union(s1,s2) 8 / 30

  11. Example: Sets of numbers, desugared tables as modules Set = {} -- create a new set with the values of a given list Set.new = function (l) builtin tables local set = {} for _, v in ipairs (l) do set[v] = true end return set end Set.union = function (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = Set.union(s1,s2) 8 / 30

  12. Example: Sets of numbers, desugared tables as modules Set = {} -- create a new set with the values of a given list Set.new = function (l) builtin tables local set = {} for _, v in ipairs (l) do set[v] = true end return set end Set.union = function (a, b) local res = Set.new {} lexical scope, block structure for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = Set.union(s1,s2) 8 / 30

  13. Lua in a bit more detail. . . To add PL spice it also includes: Metatables with events and metamethods : � These are used to model both OO-like – inheritance and overriding Python has similar constructions – Proper tail calls � Co-routines � . . . � 9 / 30

  14. Example: Sets w/overriding (Ierusalimschy’13) Set = {} local mt = {} -- metatable for sets -- create a new set with the values of a given list function Set.new (l) local set = {} setmetatable(set ,mt) for _, v in ipairs (l) do set[v] = true end return set end function Set.union (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end mt.__add = Set.union s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = s1 + s2 10 / 30

  15. Example: Sets w/overriding (Ierusalimschy’13) Set = {} declare a metatable local mt = {} -- metatable for sets -- create a new set with the values of a given list function Set.new (l) local set = {} setmetatable(set ,mt) for _, v in ipairs (l) do set[v] = true end return set end function Set.union (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end mt.__add = Set.union s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = s1 + s2 10 / 30

  16. Example: Sets w/overriding (Ierusalimschy’13) Set = {} declare a metatable local mt = {} -- metatable for sets -- create a new set with the values of a given list function Set.new (l) local set = {} install metatable setmetatable(set ,mt) for _, v in ipairs (l) do set[v] = true end return set end function Set.union (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end mt.__add = Set.union s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = s1 + s2 10 / 30

  17. Example: Sets w/overriding (Ierusalimschy’13) Set = {} declare a metatable local mt = {} -- metatable for sets -- create a new set with the values of a given list function Set.new (l) local set = {} install metatable setmetatable(set ,mt) for _, v in ipairs (l) do set[v] = true end return set end function Set.union (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end register metamethod for addition event mt.__add = Set.union s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} s3 = s1 + s2 10 / 30

  18. Example: Sets w/overriding (Ierusalimschy’13) Set = {} declare a metatable local mt = {} -- metatable for sets -- create a new set with the values of a given list function Set.new (l) local set = {} install metatable setmetatable(set ,mt) for _, v in ipairs (l) do set[v] = true end return set end function Set.union (a, b) local res = Set.new {} for k in pairs (a) do res[k] = true end for k in pairs (b) do res[k] = true end return res end register metamethod for addition event mt.__add = Set.union s1 = Set.new {10 ,20 ,30 ,50} s2 = Set.new {30 ,1} utilize overridden addition s3 = s1 + s2 10 / 30

  19. A program analysis for types We’ll build a forward program analysis for � approximating the run-time values of Lua programs It’s not a type system (no rejected programs) � Instead: an over-approximation over a suitable lattice � Our challenge is to design a lattice capable of expressing types for this kind of example Related work: . . . 11 / 30

  20. A program analysis for types We’ll build a forward program analysis for � approximating the run-time values of Lua programs It’s not a type system (no rejected programs) � Instead: an over-approximation over a suitable lattice � Our challenge is to design a lattice capable of expressing types for this kind of example Related work: . . . lots 11 / 30

  21. Describing Lua values, take 1 Lua has a range of basic values: nil , true , false , 42 , 3.14 , "hello" , ’world’ , . . . statelattice = Var − → valuelattice valuelattice = P ( tag ) tag = { nil , bool , number , string , userdata } We record the (over-approximate) state for each � program point 12 / 30

  22. Describing Lua values, take 1 Lua has a range of basic values: nil , true , false , 42 , 3.14 , "hello" , ’world’ , . . . statelattice = Var − → valuelattice valuelattice = P ( tag ) tag = { nil , bool , number , string , userdata } We record the (over-approximate) state for each � program point This can be understood as an outer function lattice � 12 / 30

  23. Describing Lua values, take 1 Lua has a range of basic values: nil , true , false , 42 , 3.14 , "hello" , ’world’ , . . . analysislattice = pplabel − → statelattice statelattice = Var − → valuelattice valuelattice = P ( tag ) tag = { nil , bool , number , string , userdata } We record the (over-approximate) state for each � program point This can be understood as an outer function lattice � 12 / 30

  24. In Lua hashtables are everywhere! In Lua even environments are hashtables: for name ,val in pairs ( _G ) do print (name , type (val)) end displays _G ’s content (the global environment): string table pairs function _G table type function arg table rawget function loadstring function ... Tables double as modules (e.g., string.len ), objects, and classes 13 / 30

  25. Approximating Lua’s hashtables (1/6) As a first step we will label all allocation sites : Set = ℓ 1 {} -- create a new set with the values of a given list Set.new = function (l) local set = ℓ 2 {} for _, v in ipairs (l) do set[v] = true end return set end ... An allocation site (a label) represents all table values originating from there (Jones-Muchnick:POPL82). A table’s contents is available in an approximate store. 14 / 30

Recommend


More recommend