62
The Eval that Men Do Gregor Richard Christian Hammer Brian Burg Jan Vitek Vincent Foley-Bourgon COMP-621 - Winter 2014 McGill University February 2014

The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

The Eval that Men Do

Gregor Richard Christian Hammer Brian Burg Jan Vitek

Vincent Foley-BourgonCOMP-621 - Winter 2014

McGill University

February 2014

Page 2: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

The paperInformation

I 3 authors from Purdue University (Go Boilermakers!)

I Presented at ECOOP 2011

I Empirical study of the usage of eval

2 / 62

Page 3: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Plan

1. What is eval?

2. Methodology

3. Results

4. Conclusion

3 / 62

Page 4: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Disclaimers

1. Tables and figures are taken from the paper

2. Won’t show all the results

3. Personal bias

4 / 62

Page 5: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Disclaimers

1. Tables and figures are taken from the paper

2. Won’t show all the results

3. Personal bias

5 / 62

Page 6: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Disclaimers

1. Tables and figures are taken from the paper

2. Won’t show all the results

3. Personal bias

6 / 62

Page 7: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?

7 / 62

Page 8: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?

Question: Who can tell me what eval does?1

1Someone who is not part of the MCJS team8 / 62

Page 9: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?eval takes a string as input and executes it

var name = "bar";

eval("o." + name + "=’" + name + "!! ’");

Same as:

o.bar = ’bar!!’;

You can pass any arbitrary string to eval:

I Assignments

I Conditionals and loops

I Functions

I Other calls to eval

9 / 62

Page 10: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?eval takes a string as input and executes it

var name = "bar";

eval("o." + name + "=’" + name + "!! ’");

Same as:

o.bar = ’bar!!’;

You can pass any arbitrary string to eval:

I Assignments

I Conditionals and loops

I Functions

I Other calls to eval

10 / 62

Page 11: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?

“eval is evil. Avoid it. eval has aliases. Don’t use them.”— Douglas Crockford

11 / 62

Page 12: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?

Question: What are some problems with eval?

How does it affect static analysis?

12 / 62

Page 13: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?

Question: What are some problems with eval?

How does it affect static analysis?

13 / 62

Page 14: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the
Page 15: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?

eval is the black hole of static analysis

I It kills everything

I It generates nothing

15 / 62

Page 16: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?Example: Reaching defs

Let’s think about how eval would affect reaching defs.

“A definition d: x = ... reaches a point p if there exits apath from d to p that does not pass through another definitionof x.”

16 / 62

Page 17: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?Example: Reaching defs

eval(S)

{ d:x=0 }

?

d: x = 0

write(x)

17 / 62

Page 18: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?Example: Reaching defs

out(Si) = gen(Si) ∪ (in(Si) - kill(Si))

where:

I gen(Si) = { di } if Si is a statement that defines x

I kill(Si) = { dj | dj defines x }

18 / 62

Page 19: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?Example: Reaching defs

eval(S)

{ d:x=0 }

d: x = 0

write(x)

19 / 62

Page 20: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What is eval?

The paper explores how eval is used in practice, and, hopefully,shows that we can replace some of eval’s usages with morestructured constructs.

20 / 62

Page 21: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Methodology

21 / 62

Page 22: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

MethodologyInfrastructure

TracingSafari: “records a trace containing most ops performedby the interpreter (reads, writes, deletes, calls, defines, etc.)”

Also records properties specific to eval: in particular theprovenance of strings, since they could be used as an argumentto eval.

22 / 62

Page 23: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

MethodologyCorpus

Question: if you want to do any kind of research on the web,where do you go first?

23 / 62

Page 24: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

MethodologyCorpus

Question: if you want to do any kind of research on the web,where do you go first?

24 / 62

Page 25: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

MethodologyCorpus

Interactive PageLoad Random

Manual interac-tion with websites

First 30 secondsof execution of aweb page

PageLoad withrandomly gener-ated events

Top 100 Top 10,000 Top 10,000∼1-5 minutes 30 seconds At most 30

events, 1 ev/sec

25 / 62

Page 26: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

MethodologyThreats to validity

I Program coverage: they believe their corpus isrepresentative of typical web browsing, even if they misssome functionality.

I Diversity: web applications in JS vastly out-number anyother type of application written in JS.

26 / 62

Page 27: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Results

27 / 62

Page 28: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Are JavaScript and eval even used?

I All top 100 sites use JS and 82 of them use eval

I 90% of the top 10,000 use JS and 50% use eval

I Events trigger more calls to eval

28 / 62

Page 29: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Are JavaScript and eval even used?

I All top 100 sites use JS and 82 of them use eval

I 90% of the top 10,000 use JS and 50% use eval

I Events trigger more calls to eval

29 / 62

Page 30: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What about JS frameworks?

I Manual inspection reveals that eval is not required fortheir operation

I Used mostly as a fallback for browsers lacking JSON.parse

30 / 62

Page 31: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

What about JS frameworks?

I Manual inspection reveals that eval is not required fortheir operation

I Used mostly as a fallback for browsers lacking JSON.parse

31 / 62

Page 32: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of eval

32 / 62

Page 33: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of eval

I Many common patterns in the use of eval

I Some are accepted industry practices (e.g. JSON, asynccontent and library loading)

I Many result from a poor understanding of JavaScript

33 / 62

Page 34: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of eval

34 / 62

Page 35: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalJSON

m = eval(’{"a": "foo", "b": [1,2,3]}’);

Funny note: JSON was invented by Douglas Crockford, so thateval could be used to parse it.

35 / 62

Page 36: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalJSONP

eval(’m = {"a": "foo", "b": [1,2,3]}’);

eval(’f({"a": "foo", "b": [1,2,3]}) ’);

I Used for load balancing across domains (work around thesame origin policiy)

36 / 62

Page 37: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalLibrary

Libraries loaded with <script> tag are downloaded, parsedand evaluated synchronously.

Workaround: download the library with AJAX, and load itwith eval.

Detection heuristic: any eval string longer than 512 bytes anddefinining at least one function.

37 / 62

Page 38: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalRead

Field accesses and pseudo arrays.

eval("foo." + x) // foo[x]

eval("arr_" + 3)

An alias to eval can also be used to access a shadowed variable.

38 / 62

Page 39: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalAssign

Patterns similar to Read, but with assignments.

39 / 62

Page 40: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalTypeof

Strange patterns involving typeof.

eval(" typeof(x) === ’undefined ’")

// typeof(x) === ’undefined ’

// ’x’ in window

40 / 62

Page 41: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalTry

“Another case for which we have no satisfying explanation,labeled Try, is to eval try/catch blocks.”

From bbc.co.uk:

eval(’try{throw v=4} catch(e){}’) // v = 4

Authors assume it’s the result of a corner case of a codegenerator.

41 / 62

Page 42: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalCall

Method invocations (typically, global functions strings) withparameters that are not padded JSON.

eval(meth+’(x)’) // window[meth](x)

42 / 62

Page 43: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalEmpty

eval is called with empty string (or all blanks).

eval ("")

Likely the default case for eval strings in a code generator.

43 / 62

Page 44: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalOther

Patterns not captured by the previous categories.

eval("img1.src=’http ://f.ca/t.php?ip=xx ’;");

“Encodes data in a URL and sends an HTTP GET request inorder to circumvent the same origin policy imposed by theDOM.”

44 / 62

Page 45: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalPatterns by websites

45 / 62

Page 46: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalPatterns distribution

46 / 62

Page 47: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Patterns of evalImpact on analysis

“Most eval call sites in categories other than Library, Other,Call are replaceable by less dynamic features such asJSON.parse, hashmap access, and proper use of JavaScriptarrays. On Interactive, these categories account for 76% of alleval’d strings; thus, a majority of eval uses are not necessary.”

47 / 62

Page 48: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Pattern replacements

48 / 62

Page 49: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Pattern replacements

49 / 62

Page 50: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Provenance of eval strings

50 / 62

Page 51: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Provenance of strings

Where do the strings passed to eval come from? Authors usedTracingSafari to track their provenance:

51 / 62

Page 52: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Provenance of strings

52 / 62

Page 53: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Provenance of strings

Notice how many eval’ed strings are constant and composite!

53 / 62

Page 54: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Provenance of strings

54 / 62

Page 55: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Performance impact of eval

55 / 62

Page 56: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Performance impact of eval

56 / 62

Page 57: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Conclusion

57 / 62

Page 58: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Conclusion

“We started this work with the hope that it would show thateval can be replaced by other features. Unfortunately our datadoes not support this conclusion.”

58 / 62

Page 59: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Conclusion

“eval is a convenient way of providing a range of features thatweren’t planned for by the language designers. For example,JSON was created to support (de-)serialization of JavaScriptobjects.”

59 / 62

Page 60: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Conclusion

“Most accepted uses of eval have been transformed intolibraries or new language features recently, and as such nobest practices recommends usage of eval.”

60 / 62

Page 61: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

</presentation>

61 / 62

Page 62: The Eval that Men Dovfoley1/eval-that-men-do.pdf · What is eval? Question: What are some problems with eval? How does it affect static analysis? 13/62. What is eval? eval is the

Big question

How would you design an analysis to identify constant andcomposite strings, so that you could offer suggestions to aprogrammer that his usage of eval is perhaps not necessary?

62 / 62