270 likes | 458 Views
The Essence of JavaScript. Arjun Guha , Claudiu Saftoiu , and Shriram Krishnamurthi. "JavaScript has much in common with Scheme […] Because of this deep similarity …". (. ). function bar(x) { return function() { var x = x; return x; }; } var f = bar(200); f() undefined.
E N D
The Essence of JavaScript ArjunGuha, ClaudiuSaftoiu, and ShriramKrishnamurthi
"JavaScript has much in common with Scheme […] Because of this deep similarity …" ( )
function bar(x) { return function() { var x = x; return x; }; } var f = bar(200); f() undefined function bar(x) { return function() { var x = x; return x; }; } var f = bar(200); f() 200
var x = 0; var y = 900; function baz(obj) { with (obj) { x = y; } } baz({ y: 100 }); x 100 varmyObj = { x : 0 }; baz(myObj); x 100 myObj.x 900 Is JavaScript Even Lexically Scoped?
"JavaScript has much in common with Scheme […] Because of this deep similarity …" No help to researchers studying Web security, building JavaScript analyses, etc.
weirdness Bad ArjunGuha, ClaudiuSaftoiu, and ShriramKrishnamurthi
<script lang="javascript" src="http://ad.doubleclick.net/..."> <script lang="javascript" src="http://ad.linkstorms.com/..."> nytimes.com is a JavaScript mashup
Malicious 3rdparty code Runtime Safety Check • window["ev" + "al"] • window["eval"] • vulnerability function safeLookup(obj, fd) { if (fd === "eval") { throw "cannot access eval"; } else { return obj[fd]; } } BUGGY ADsafe / Caja / Facebook JavaScript Syntactic Checks + Inserted Runtime Checks “Sanitized” 3rd party code safeLookup(window, "ev" + "al") safeLookup(window, "eval") * exception
How can we reason about JavaScript? We need a tractable semantics The JavaScript standard (ECMA-262). 200 pages of prose and pseudocode. Maffeis, Mitchell, and Taly. An Operational Semantics for JavaScript. 70 pages of semantics.
The Essence of JavaScript: Functions, Prototype-Based Objects, State,Control Operators, and Primitives
Thank You! Questions?
What about the bad parts? Thanks, Emery Berger
In practice most development effort goes into the “noise” that researchers abstract away […]. [M]inimalistic subsets give rise to a nice and simple formalization, whereas language implementers actually need help formalizing the rough edges of the language, not the beautiful and clean subset. Erik Meijer. Confessions of a Used Programming Language Salesman. OOPSLA 2007.
What about the bad parts? scope objects, with, switch, return, var, continue, for, do-while, for-in, implicit type conversions, function statements, named function expressions, function objects, "constructors", new-expressions, sparse "arrays", this keyword, toString(), valueOf(), variable-arity, Function.caller, Function.callee, the standard library, etc. We implement desugaring (1,000 LOC) syntactic sugar Thanks, Emery Berger
Desugaringis Compositional* desugar(e1 + e2) = C [ desugar(e1), desugar(e2) ] desugar(obj[field]) = C [ desugar(obj), desugar(field) ] etc. program context, inserted by desugaring *except for with statements
(Desugaring is Total) For all JavaScript programs e, is desugar(e) defined? desugar JavaScript program λJS program 100LOCinterpreter Chrome,Firefox,Rhino theiranswer ouranswer (Desugar Commutes with Eval) For all JavaScript programs e, does desugar(JS-eval(e)) = λJS-eval(desugar(e))?
/* if F, G are inverse functions and x==y, this should return 1 */ function match(x, y, F, G) { switch (x) { case F(G(y)): return 1; default: return 0; } } test_case("A", match(17, f(fInverse(17)), f, fInverse)), 1); test_case("B", match(17, 2000, f, fInverse), 0); test_case("C", match(1, 1, Math.exp, Math.log), 1); test_case("D", match(1, 200, Math.exp, Math.log), 0); test_case("E", match(1, 1, Math.sin, Math.cos), 1);
$ ./test_firefox.sh tests/Statements/switch-001.js BUGNUMBER: (none) STATUS: Testing the switch statement PASSED! Section A of test PASSED! Section B of test PASSED! Section C of test PASSED! Section D of test PASSED! Section Eof test Our semantics produces exactly the same result $ ./test_lambdajs.sh tests/Statements/switch-001.js BUGNUMBER: (none) STATUS: Testing the switch statement PASSED! Section A of test PASSED! Section B of test PASSED! Section C of test PASSED! Section D of test PASSED! Section Eof test
5,400 lines of the Mozilla JavaScript test suite: equivalent under diff scalable strategy: add more tests
Recent JavaScript Research • Staged Information Flow for JavaScript. PLDI’09. • GateKeeper.USENIX’09. • Static Analysis for Ajax Intrusion Detection. WWW’09. • Type Analysis for JavaScript. SAS’09. • Object Views: Fine-Grained Sharing in Browsers. WWW’10. • … desugar to λJS do proofs for λJS build tools for λJS Proofs?
function safeLookup(obj, fd) { if (fd === "eval") { throw "cannot access eval"; } else { return obj[fd]; } } function safeLookup(obj, fd) { if (fd === "eval") { throw "cannot access eval"; } else { return obj[fd.toString()]; } } Implicit call in JavaScript Explicit call in λJS • badObj = • {toString: • function () {return "eval"}} • window[badObj] • safeLookup(window, badObj) • window[badObj.toString()] • window[(function () return "eval")()] • window["eval"]
Conclusion • λJS is tractable and good for soundness proofs • desugar is executable, so semantics-based tools can handle real source • Used in Typed JavaScript, flow analyses, security type systems (JS source lang. too big, too implicit) • λJS sets a new semantics standard: testing