an empirical study on the rewritability of the with statement in javascript

43
An Empirical Study on the Rewritability of the with Statement in JavaScript Changhee Park (Joint work with Hongki Lee and Sukyoung Ryu) PLRG @ KAIST October 23, 2011

Upload: kory

Post on 25-Feb-2016

40 views

Category:

Documents


2 download

DESCRIPTION

An Empirical Study on the Rewritability of the with Statement in JavaScript. Changhee Park (Joint work with Hongki Lee and Sukyoung Ryu) PLRG @ KAIST October 23, 2011. Famous Word about the w ith S tatement. Overview. Introduction of the with statement - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

An Empirical Study on the Rewritability of the with Statement in JavaScript

Changhee Park(Joint work with Hongki Lee and Sukyoung Ryu)

PLRG @ KAIST

October 23, 2011

Page 2: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Famous Word about the with Statement

Page 3: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Overview

• Introduction of the with statement• Real-world usage patterns of the with

statement• Rewritability of the with statement• Future work • Conclusion

Page 4: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Introduction

• Syntax in ECMAScript

The body of the with statement

Page 5: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Introduction

• Semantics

1. Evaluate exp to a JavaScript object(with ob-ject)

2. Add the object to the front of the scope chain• All properties in the with object become local

variables in the body of the with statement3. Evaluate stmt4. Remove the with object from the scope

chain

Page 6: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Good Parts

<html >. . .<body>

<di v> . . . </ di v>. . .

</ body>. . .

</ html >

docu-ment

body

div

An HTML document A DOM(Document Object Model) tree

document . body. chi l dren[0]

Page 7: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Good Parts

document . body. chi l dren[0] . styl e. textAl i gn="center";document . body. chi l dren[0] . styl e. f ontSi ze=50;

wi th(document . body. chi l dren[0] . styl e) {textAl i gn="center";f ontSi ze=50;

}

Page 8: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Bad Parts

• Introducing a new scope at run time– Performance overhead– Infeasible static analysis

Page 9: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Bad Parts

• Infeasible static analysis

obj1, obj2, fun1Global

obj, localvar1fun1

Page 10: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Bad Parts

• Infeasible static analysis

obj1, obj2, fun1Global

obj, localvar1fun1

onewith : obj1

Page 11: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Bad Parts

• Infeasible static analysis

obj1, obj2, fun1Global

obj, localvar1fun1

twowith : obj2

Page 12: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Empirical Study

• with statements used in real-world– Amount– Usage patterns

Page 13: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Methodology

• Real-world JavaScript code – 100(98) worldwide most popular web

sites by alexa.com• Tool

– TracingSafari, Purdue University• Customized version of WebKit

– Static analyzer using the Rhino parser

Page 14: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Methodology

• Two data sets- LOADING set

• JavaScript code collected for 30s at loading time

- INTERACTION set• JavaScript code collected for 2 minutes with

user interactions(mouse click events)

Page 15: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Methodology

• Three kinds of with statement– Static with statements

• Static with keyword detected by the Rhino Parser (static)

– Executed with statements• with statements evaluated by the inter-

preter (executed)

Page 16: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Methodology

• Three kinds of with statement– Dynamic with statements

• Dynamically instrumented and executed with statements (dynamic)

• By filtering out static with statements from executed with statements

Page 17: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Amount of with Statements

Type of with Web sites with counts Unique

withsStatic 15 54 54

Executed 8 36 12Dynamic 2 13 5

• LOADING set

• INTERACTION setType of with Web sites with counts Unique

withsStatic 38 2,245 573

Executed 27 1,232 215Dynamic 9 308 56

Dupli-cates

0%66.6%61.5%

Dupli-cates74.4%82.5%81.8%

Page 18: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

• Seven usage patterns1. DOMAccess pattern2. This pattern3. Global pattern4. Empty pattern5. Template pattern6. Generator pattern7. Others pattern

Page 19: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

1. DOMAccess pattern• with object : DOM element• Example from paypal.com

wi th(document . f orms[k] ) {appendChi l d(PAYPAL. browserscr i pt . . . . ) ;appendChi l d(PAYPAL. browserscr i pt . . . . ) ;appendChi l d(PAYPAL. browserscr i pt . . . . ) ;

}

• Access or change the values of DOM element attributes

Page 20: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

2. This pattern• with object : this

Page 21: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

2. This pattern

• Use the same naming convention be-tween private and public properties

Page 22: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

3. Global pattern• with object : window• Example from ebay.com

wi th(wi ndow)try {

eval (_1f ) ;return t rue;

} catch(e) {}

• Run the code by eval under the global scope

Page 23: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

4. Empty pattern• with object : any object• Has the empty body

Page 24: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

5. Template pattern• with object : template data• Example from 163.com

wi th(obj ) {_. push(`<a href ="' , ur l , `">' , text , `</ a>' ) ;

}

wi th({ur l : "exampl e. com", text : "exampl e"}) {_. push(`<a href ="' , ur l , `">' , text , `</ a>' ) ;

}<a href ="exampl e. com">exampl e</ a>

• Process HTML templates

Page 25: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

6. Generator pattern• with object : any object• Contains dynamic code generating func-

tions such as eval, Function, setTime-out, and setInterval

Page 26: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

7. Others pattern• with object : any object• Avoid repeatedly accessing the with ob-

ject

Page 27: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

• LOADING set

Page 28: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Usage Patterns

• INTERACTION set

Page 29: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Template Pattern in Dynamic with

• Example from 163.com

• For an arbitrary tag?

wi th(obj ) {_. push(`<a href ="' , ur l , `">' , text , `</ a>' ) ;

}

Only process <a> tag

Generate new process code with an input tag

Page 30: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewritability of with Statements

• Many static approaches disallow the with statement

• Safe subsets of JavaScriptGoogle Facebook YAHOO!Cajita FBJS ADsafe

What if it is possible to rewrite the with statement to other statements?

Page 31: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewriting Strategy

• Main ideawi th(obj )

. . . i d . . .obj has the id prop-erty?

. . . obj . i d . . . . . . i d . . .

Yes No

Page 32: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewriting Strategy

• Main ideawi th(obj )

. . . i d . . .

var $f = toObj ect (obj ) ;. . . ( "i d" i n obj ? $f . i d : i d) . . .

Page 33: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewriting Strategy

• Rewriting the assignment expressionwi th(obj )

x=3;

var $f = toObj ect (obj ) ;( "x" i n obj ? $f . x : x) = 3;

ReferenceError exception!!

Page 34: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewriting Strategy

• Rewriting the assignment expressionwi th(obj )

x=3;

var $f = toObj ect (obj ) ;( "x" i n obj ? $f . x=3 : x=3) ;

Page 35: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewriting Strategy

• Rewriting the variable declaration

wi th(obj ) {var x=3;var y;

}

v

var x;var y;wi th(obj ){

x=3;}

var $f = toObj ect (obj ) ;var x;( "x" i n obj ? $f . x=3 : x=3) ;var y;

Page 36: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewriting Strategy

• Rewriting the function expressionwi th(obj )

x=f unct i on( ) {var l var1;l var1=l var2;

}

v f unct i on( ) {

var l var1;l var1=("l var2" i n $f ? $f . l var2 : l var2) ;

}

Page 37: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewritability Check

• Rewritability of with statements in each pattern

Pattern Rewritabil-ity

Rewriting

DOMAccess Yes By the rewriting strategyThis Yes By the rewriting strategy

Global YesEmpty Yes By the rewriting strategy

Template Yes By the rewriting strategyGenerator No

Others Yes By the rewriting strategy

Page 38: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewritability Check

• Generator patternwi th(obj )

eval (st r) ;

Not possible to rewrite in general!!

Page 39: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewritability Check

• Global pattern : example from ebay.comwi th(wi ndow)

try {eval (_1f ) ;return t rue;

} catch(e) {}

var $f =toObj ect (wi ndow) ;t ry {

("eval " i n $f ? $f . eval ( _1f ) ; $f . eval (_1f ) ) ;return true;

} catch(e) {}

Page 40: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewritability Check

• According to the ECMAScript 5th edition, rewriting is possible by aliasing

wi th(wi ndow)try {

eval (_1f ) ;return t rue;

} catch(e) {}

t ry {var al i aseval =eval ;al i aseval ( _1f ) ;return t rue;

} catch (e) {};

Page 41: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Rewritability Check

• Summary– We can rewrite all static with statements in

all patterns except for Generator pattern

– 100% of static with statements in the LOADING set and 93.8% in the INTER-ACTION set are rewritable

Page 42: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Future Work

• Extension to the top 1,000 sites• Formalization of the rewriting process• Implementation of the rewriting process

Page 43: An Empirical Study on the Rewritability  of the  with  Statement in JavaScript

Conclusion

• Usage of with statements in real world– 38% of the top 98 sites have static oc-

currences of with statements with sim-ple user interactions

• Rewritability of with statements– We can rewrite all static with state-

ments to other statements if the with statements do not have dynamic code generating functions