1 / 27

The Essence of JavaScript

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.

shandi
Download Presentation

The Essence of JavaScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Essence of JavaScript ArjunGuha, ClaudiuSaftoiu, and ShriramKrishnamurthi

  2. "JavaScript has much in common with Scheme […] Because of this deep similarity …" ( )

  3. 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

  4. 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?

  5. "JavaScript has much in common with Scheme […] Because of this deep similarity …" No help to researchers studying Web security, building JavaScript analyses, etc.

  6. weirdness Bad ArjunGuha, ClaudiuSaftoiu, and ShriramKrishnamurthi

  7. <script lang="javascript" src="http://ad.doubleclick.net/..."> <script lang="javascript" src="http://ad.linkstorms.com/..."> nytimes.com is a JavaScript mashup

  8. 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

  9. 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.

  10. The Essence of JavaScript: Functions, Prototype-Based Objects, State,Control Operators, and Primitives

  11. Thank You! Questions?

  12. What about the bad parts? Thanks, Emery Berger

  13. 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.

  14. 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

  15. 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

  16. (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))?

  17. 5,400 lines of the Mozilla JavaScript test suite:

  18. /* 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);

  19. $ ./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

  20. 5,400 lines of the Mozilla JavaScript test suite: equivalent under diff scalable strategy: add more tests

  21. 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?

  22. 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"]

  23. 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

More Related