Program Analysis for JavaScript Challenges and Techniques



Similar documents
TypeScript for C# developers. Making JavaScript manageable

HybriDroid: Analysis Framework for Android Hybrid Applications

WEB DEVELOPMENT COURSE (PHP/ MYSQL)

Semantic Analysis: Types and Type Checking

Mobile Web Design with HTML5, CSS3, JavaScript and JQuery Mobile Training BSP-2256 Length: 5 days Price: $ 2,895.00

Scalable and Precise Static Analysis of JavaScript Applications via Loop-Sensitivity

Art of Code Front-end Web Development Training Program

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

JavaScript Patterns. Stoyan Stefanov. O'REILLY' Beijing Cambridge Farnham Koln Sebastopol Tokyo

Facebook Twitter YouTube Google Plus Website . o Zooming and Panning. Panel. 3D commands. o Working with Canvas

Static Analysis. Find the Bug! : Analysis of Software Artifacts. Jonathan Aldrich. disable interrupts. ERROR: returning with interrupts disabled

Generating Test Cases With High Branch Coverage for Web Applications

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

JavaScript Basics & HTML DOM. Sang Shin Java Technology Architect Sun Microsystems, Inc. sang.shin@sun.com

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Write Barrier Removal by Static Analysis

Tachyon: a Meta-circular Optimizing JavaScript Virtual Machine

Static Analysis of Event-Driven Node.js JavaScript Applications

Overview. In the beginning. Issues with Client Side Scripting What is JavaScript? Syntax and the Document Object Model Moving forward with JavaScript

C++ INTERVIEW QUESTIONS

Example. Represent this as XML

Semantics and Security Issues in JavaScript

JavaScript Testing. Beginner's Guide. Liang Yuxian Eugene. Test and debug JavaScript the easy way PUBLISHING MUMBAI BIRMINGHAM. k I I.

GUI and Web Programming

Performance Issues and Optimizations in JavaScript: An Empirical Study

ECMAScript 3 rd Edition Compact Profile

Curriculum Map. Discipline: Computer Science Course: C++

Advantage of Jquery: T his file is downloaded from

JavaScript static security analysis made easy with JSPrime

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

A Static Analyzer for Large Safety-Critical Software. Considered Programs and Semantics. Automatic Program Verification by Abstract Interpretation

C++FA 5.1 PRACTICE MID-TERM EXAM

Progressive Enhancement With GQuery and GWT. Ray Cromwell

Visualizing Software Projects in JavaScript

Correlation Tracking for Points-To Analysis of JavaScript

Dart a modern web language

MASTERTAG DEVELOPER GUIDE

How to Write a Checker in 24 Hours

Programming in HTML5 with JavaScript and CSS3

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Ajax Development with ASP.NET 2.0

Tutorial on C Language Programming

Towards practical reactive security audit using extended static checkers 1

The V8 JavaScript Engine

Object-Oriented JavaScript

Adaptive Context-sensitive Analysis for JavaScript

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Development Techniques for Native/Hybrid Tizen Apps. Presented by Kirill Kruchinkin

A Practical Blended Analysis for Dynamic Features in JavaScript

Client-side Web Engineering From HTML to AJAX

Java Interview Questions and Answers

CSC 551: Web Programming. Fall 2001

Web Development Recipes

MXwendler Javascript Interface Description Version 2.3

Chapter 5 Names, Bindings, Type Checking, and Scopes

TypeScript. Language Specification. Version 1.4

TreatJS: Higher-Order Contracts for JavaScript

Address Taken FIAlias, which is equivalent to Steensgaard. CS553 Lecture Alias Analysis II 2

AjaxScope: Remotely Monitoring Client-side Web-App Behavior

Javascript in Ten Minutes

Performance Testing for Ajax Applications

Java Basics: Data Types, Variables, and Loops

Integration of an open source rule engine to enhance the IHTSDO Workbench testing

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Not agree with bug 3, precision actually was. 8,5 not set in the code. Not agree with bug 3, precision actually was

Lecture 9 Chrome Extensions

風 水. Heap Feng Shui in JavaScript. Alexander Sotirov.

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

KS3 Computing Group 1 Programme of Study hours per week

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

JavaScript: Client-Side Scripting. Chapter 6

Facebook Twitter YouTube Google Plus Website

XML Programming with PHP and Ajax

Tutorial: Building a Dojo Application using IBM Rational Application Developer Loan Payment Calculator

Modern Web Development with Dart. Alan Knight Google

Static Taint-Analysis on Binary Executables

Moving from CS 61A Scheme to CS 61B Java

Statically Checking API Protocol Conformance with Mined Multi-Object Specifications Companion Report

Modern Web Development:

Portals and Hosted Files

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Introducing Apache Pivot. Greg Brown, Todd Volkert 6/10/2010

More on Objects and Classes

OBJECT-ORIENTED JAVASCRIPT OBJECT-ORIENTED

Web Development using PHP (WD_PHP) Duration 1.5 months

Introduction: Abstract Data Types and Java Review

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

CIS 467/602-01: Data Visualization

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Static Analysis for Independent App Developers

Advanced Java Client API

µz An Efficient Engine for Fixed points with Constraints

Specialized Programme on Web Application Development using Open Source Tools

Transcription:

Program Analysis for JavaScript Challenges and Techniques Anders Møller Center for Advanced Software Analysis Aarhus University Joint work with Esben Andreasen, Simon Holm Jensen, Peter A. Jonsson, Magnus Madsen, and Peter Thiemann

JavaScript 2

JavaScript needs program analysis Testing is still the only technique programmers have for finding errors in their code Program analysis can (in principle) be used for bug detection (e.g. "x.p in line 7 always yields undefined") code completion optimization 3

JavaScript is a dynamic language Object-based, properties created on demand Prototype-based inheritance First-class functions, closures Runtime types, coercions NO STATIC TYPE CHECKING NO STATIC CLASS HIERARCHIES 4

Type Analysis for JavaScript The goal: Catch type-related errors using program analysis Support the full language Aim for soundness 5

Statically detecting type-related errors in JavaScript programs 6

Likely programming errors 1. invoking a non-function value (e.g. undefined) as a function 2. reading an absent variable 3. accessing a property of null or undefined 4. reading an absent property of an object 5. writing to variables or object properties that are never read 6. calling a function object both as a function and as a constructor, or passing function parameters with varying types 7. calling a built-in function with an invalid number of parameters, or with a parameter of an unexpected type etc. 7

Which way to go? 8

The TAJS approach [Jensen, Møller, and Thiemann, SAS 09] Dataflow analysis (abstract interpretation) using the monotone framework [Kam & Ullman 77] The recipe: 1. construct a control flow graph for each function in the program to be analyzed 2. define an appropriate dataflow lattice (abstraction of data) 3. define transfer functions (abstraction of operations) 9

The dataflow lattice (simplified!) The analysis maintains an abstract state for each program point N and call context C: N C State Each abstract state provides an abstract value for each abstract object L and property name P: State = L P Value Each abstract value describes pointers and primitive values: Value = P(L) Bool Str Num... Details refined through trial-and-error... 10

Transfer functions, example A dynamic property read: x[y] 1. Coerce x to objects 2. Coerce y to strings 3. Descend the object prototype chains to find the relevant properties 4. Join the property values 11

A tiny example... function Person(n) { this.setname(n); Person.prototype.count++; Person.prototype.count = 0; Person.prototype.setName = function(n) { this.name = n; function Student(n,s) { this.b = Person; this.b(n); delete this.b; this.studentid = s.tostring(); Student.prototype = new Person; var t = 100026; var x = new Student("Joe Average", t++); var y = new Student("John Doe", t); y.setname("john Q. Doe"); does y have a setname method at this program point? 12

An abstract state (as produced by TAJS) 13

JavaScript web applications Modeling JavaScript code is not enough The environment of the JavaScript code: the ECMAScript standard library the browser API the HTML DOM the event mechanism around 250 abstract objects with 500 properties and 200 functions [Jensen, Madsen, and Møller, ESEC/FSE 11] 14

eval(s) Eval in JavaScript parse the string S as JavaScript code, then execute it Challenging for JavaScript static analysis the string is dynamically generated the generated code may have side-effects and JavaScript has poor encapsulation mechanisms 15

Eval is evil... but most uses of eval are not very complex So let s transform eval calls into other code! How can we soundly make such transformations if we cannot analyze code with eval? Which came first? Analysis or transformation 16

Whenever TAJS detects new dataflow to eval, the eval transformer is triggered [Jensen, Jonsson, and Møller, ISSTA 12] 17

An example var y = "foo" for (i = 0; i < 10; i++) { eval(y + "(" + i + ")") The dataflow analysis propagates dataflow until the fixpoint is reached iteration 1: y is "foo", i is 0 eval(y + "(" + i + ")") foo(0) (the dataflow analysis can now proceed into foo) iteration 2: y is "foo", i is AnyNumber eval(y + "(" + i + ")") foo(i) (would not work if i could be any string) 18

Ingredients in a static analyzer for JavaScript applications We need to model the language semantics the standard library (incl. eval) the browser API (the HTML DOM, the event system, etc.) 19

Mission complete? 20

21

22

Why use jquery (or other libraries)? Patches browser incompatibilities CSS3-based DOM navigation Event handling AJAX (client-server communication) UI widgets and animations 1000s of plugins available 23

An appetizer Which code fragment do you prefer? var checkedvalue; var elements = document.getelementsbytagname('input'); for (var n = 0; n < elements.length; n++) { if (elements[n].name == 'someradiogroup' && elements[n].checked) { checkedvalue = elements[n].value; var checkedvalue = $('[name="someradiogroup"]:checked').val(); 24

Investigating the beast lines executed when the library initializes itself after loading 25

[Schäfer, Sridharan, Dolby, Tip. Dynamic Determinacy Analysis, PLDI'13] Experimental results for jquery with WALA: can analyze a JavaScript program that loads jquery and does nothing else no success on jquery 1.3 and beyond The WALA approach: 1) dynamic analysis to infer determinate expressions that always have the same value in any execution (but for a specific calling context) 2) exploit this information in context-sensitive pointer analysis 26

Example of imprecision that explodes A dynamic property read: x[y] if x may evaluate to the global object and y may evaluate to a unknown string then x[y] may yield eval, document, Array, Math,... consequence 27

jquery: sweet on the outside, bitter on the inside A representative example from the library initialization code: jquery.each("ajaxstart ajaxstop... ajaxsend".split(" "), function(i, o) { jquery.fn[o] = function(f) { return this.on(o, f); ; ); which could have been written like this: jquery.fn.ajaxstart = function(f) { return this.on("ajaxstart", f); ; jquery.fn.ajaxstop = function(f) { return this.on("ajaxstop", f); ;... jquery.fn.ajaxsend = function(f) { return this.on("ajaxsend", f); ; 28

each: function (obj, callback, args) { var name, i = 0, length = obj.length, isobj = length === undefined jquery.isfunction(obj); if (args) {... // (some lines omitted to make the example fit on one slide) else { if (isobj) { for (name in obj) { if (callback.call(obj[name], name, obj[name]) === false) { else { break; for (; i < length ;) { if (callback.call(obj[i], i, obj[i++]) === false) { break; return obj; Lots of overloading reflection callbacks 29

Our recent results, by improving TAJS TAJS can now analyze (in reasonable time) the load-only program for 11 of 12 versions of jquery 27 of 71 small examples from a jquery tutorial Very good precision for type analysis and call graphs Analysis time: 1-24 seconds (average: 6.5 seconds) [Andreasen and Møller, OOPSLA 14] 30

TAJS analysis design Whole-program, flow-sensitive dataflow analysis Constant propagation Heap modeling using allocation site abstraction Object sensitivity (a kind of context sensitivity) Branch pruning (eliminate dataflow along infeasible branches) Parameter sensitivity Loop specialization Context-sensitive heap abstraction [Andreasen and Møller, OOPSLA 14] 31

each: function (obj, callback, args) { var name, i = 0, length = obj.length, isobj = length === undefined jquery.isfunction(obj); if (args) {... else { if (isobj) { for (name in obj) { if (callback.call(obj[name], name, obj[name]) === false) { else { break; for (; i < length ;) { if (callback.call(obj[i], i, obj[i++]) === false) { break; return obj; constant propagation... branch pruning logically eliminates several branches specializing on i effectively unrolls the loop with parameter sensitivity, these become constants context-sensitive heap abstraction keeps the ajaxstart, ajaxstop, etc. functions separate 32

The technical side... The analysis maintains an abstract state for each program point N and call context C: N C State Old TAJS: C = P(L) (object sensitivity) L = N (L: abstract memory locations) New TAJS: parameter sensitivity (A: selected parameters) C = P(L) (A Value) (B Value) L = N C context-sensitive heap abstraction loop specialization (B: selected local variables) 33

Conclusion JavaScript programmers need better tools! Static program analysis can detect type-related errors, find dead code, build call graphs, etc. dataflow analysis to model the ECMAScript standard model of the standard library, browser API, and HTML DOM rewrite calls to eval during analysis handle complex libraries by boosting analysis precision Progress, but far from a full solution Π CENTER FOR ADVANCED SOFTWARE ANALYSIS http://cs.au.dk/casa 34