Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web:...

153
Jalangi: A Dynamic Analysis Framework for JavaScript With contributions from: Christoffer Adamsen, Esben Andreasen, Tasneem Brutch, Satish Chandra, Colin S. Gordon, Simon Gibbs, Simon Jenson, Swaroop Kalasapur, Rezwana Karim, Magnus Madsen, Michael Pradel, Frank Tip Manu Sridharan Uber Koushik Sen, Liang Gong University of California, Berkeley

Transcript of Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web:...

Page 1: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Jalangi:ADynamicAnalysisFrameworkforJavaScript

Withcontributionsfrom:

ChristofferAdamsen,EsbenAndreasen,TasneemBrutch,SatishChandra,ColinS.Gordon,SimonGibbs,SimonJenson,SwaroopKalasapur,Rezwana

Karim,MagnusMadsen,MichaelPradel,FrankTip

ManuSridharanUber

KoushikSen,LiangGongUniversityofCalifornia,Berkeley

Page 2: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

WhyJavaScript?• TheRedMonkProgrammingLanguageRankings(Popularity):January2015and2016

– BasedonprojectshostedatGitHubandquestionspostedatStackOverflow

Page 4: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• Client-sideJavaScriptinRichWebApplications

• DesktopApps(Windows8andGnome),FirefoxOS,TizenOS

• Server-side(node.js)– Paypal,Ebay,Uber,NYtimes,Linkedin,andmanymore

• AssemblyLanguagefortheWeb:emscripten,coffeescript,TypeScript

• AlanguagetoimplementDSLframeworks– Angular.js,Knockout.js,React.js

WhyJavaScript?

Page 5: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• Hugeecosystemoflibrariesandframeworks

• JavaScripthaslowlearningcurve– peoplecanstartcodingandgetresultsquickly

• Nospecialinstallation/executionenvironment– Justuseamodernbrowser

• JavaScriptsupportsfunctionalprogramming– higherorderfunctions

• ModernJavaScriptVMsarefast

WhyJavaScript?

Page 6: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Atwood’sLaw

“AnyapplicationthatcanbewritteninJavaScript,willeventuallybewrittenin

JavaScript.”

Page 7: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• JavaScripthasitsquirks(many)

WhyToolsforJavaScript?

Page 8: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

varx=“1”;

++x;

console.log(x);

varx=“1”;

x+=1;

console.log(x);

WhyToolsforJavaScript?

Page 9: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

varx=“1”;

++x;

console.log(x);

//prints2

varx=“1”;

x+=1;

console.log(x);

//prints11

WhyToolsforJavaScript?

Page 10: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• Easytointroducebugs:correctness,performance,memory– Degreesofequality==vs.===

• Loosely-typed– forgiving:implicittypeconversion– trieshardtoexecutewithoutthrowingexception

• LikeHTML

• Highlyreflective– evalanydynamicallycreatedstring

• Asynchronousprogramming

WhyToolsforJavaScript?

Page 11: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• Loosely-typed– forgiving:implicittypeconversion

– trieshardtoexecutewithoutthrowingexception

• LikeHTML

Page 12: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

ToolsforBugFindingandSecurityAnalysis

• Remarkableprogressinprogram-analysisandconstraintsolving– Commercialtools:Coverity,Klocwork,Grammatech,TotalView,Parallocity,StaticDeviceVerifierfromMicrosoft,WALAatIBM

– Open-sourcetools:GDB,lint,FindBugs,Valgrind– Academictools:SLAM,BLAST,ESP,JPF,Bandera,Saturn,MAGIC,DART,CUTE,jCUTE

– MostlyfocusedonC/C++andJavaprograms

• HardlyanysoftwarequalitytoolforJavaScriptandHTML5– Staticanalysisisdifficultfordynamiclanguages

Page 13: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Jalangi

Apowerfulbrowser-independent(dynamic)analysisframeworkforJavaScripthttps://github.com/Samsung/jalangi2

• Jalangi:Aselectiverecord-replayanddynamicanalysisframeworkforJavaScript.KoushikSen,SwaroopKalasapur,TasneemBrutch,andSimonGibbs.InESEC/FSE,2013.

Page 14: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Jalangi:GoalsandRequirements

• FrameworkforDynamicandhybridStatic/Dynamicanalysis– supportssymbolicexecution,bugfinding,memoryanalysis,runtimetype

analysis,valuetracking,tainttracking,performanceanalysis• HandleALLdynamicfeatures

– notOKtoignoreeval,newFunction• Independentofbrowser

– source-to-sourcecodeinstrumentation– instrumentedprogramwhenexecutedperformsanalysis

• EasyImplementationofDynamicAnalysis– Observeanexecutionpassively:(conventionaldynamicanalysis)– Modifysemantics/values– Repeatedlyexecutearbitrarypathswithinafunction

Page 15: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

WhynotModifyaBrowser?• Hard to keep up with browser development • Harder to get people to use of customized browser

Page 16: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Jalangi1and2• Jalangi1:

– https://github.com/SRA-SiliconValley/jalangi

– recordexecutionandreplaytoperformanalysis

– Shadowvalues(wrappedobjects)

– Nolongersupported

• Jalangi2:– https://github.com/Samsung/jalangi2

– norecord/replayorshadowvalues

– optionalshadowmemory

– activedevelopment

Page 17: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

HowJalangiWorks?

JavaScript and HTML

Jalangi Runtime

User Written Analysis

Jalangi

Analysis Writer

Intermediate

Page 18: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

HowJalangiWorks?

JavaScript and HTML

Instrumented Files

Jalangi Instrumentor

Jalangi Runtime

User Written Analysis

Source Information

Jalangi

Analysis Writer

Intermediate

Page 19: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

HowJalangiWorks?

JavaScript and HTML

Instrumented Files

Jalangi Instrumentor

Jalangi Runtime

User Written Analysis

Execute in Browser/Node.js

Trace

Output Data

Source Information

Jalangi

Analysis Writer

Intermediate

Page 20: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

HowJalangiWorks?

JavaScript and HTML

Instrumented Files

Jalangi Instrumentor

Jalangi Runtime

User Written Analysis

Execute in Browser/Node.js

Trace Offline Analysis

Output Data

Visualize Output

Final Output

Source Information

Jalangi

Analysis Writer

Intermediate

Page 21: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JalangiInstrumentation(simplified)

x=y+1 => x=Write(“x”,Binary(‘+’,Read(“y”,y),Literal(1),x)

a.f=b.g => PutField(Read(“a”,a),“f”,GetField(Read(“b”,b),“g”))

if(a.f())… => if(Branch(Method(Read(“a”,a),“f”)()))…

Page 22: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JalangiRuntime

functionBinary(op,left,right,...){

result=leftopright;

returnresult;

}

Page 23: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JalangiRuntime

functionBinary(op,left,right,...){

vararet=analysis.binaryPre(op,left,write,...); result=leftopright;aret=analysis.binary(op,left,right,result,...);

returnresult;

}

Page 24: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JalangiRuntime

functionBinary(op,left,right,...){

varskip=false;vararet=analysis.binaryPre(op,left,write,...); if(aret){ op=aret.op;left=aret.left;right=aret.right;skip=aret.skip;}} if(!skip) result=leftopright;aret=analysis.binary(op,left,right,result,...);

returnresult;

}

Page 25: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JalangiRuntime

functionBinary(op,left,right,...){

varskip=false;vararet=analysis.binaryPre(op,left,write,...); if(aret){ op=aret.op;left=aret.left;right=aret.right;skip=aret.skip;}} if(!skip) result=leftopright;aret=analysis.binary(op,left,right,result,...);if(aret)returnaret.result;else

returnresult;

}

Page 26: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

DownloadandInstallJalangi2

Download:

gitclonehttps://github.com/Samsung/jalangi2.git

cdjalangi2

Install:

npminstall

Test:pythonscripts/test.traceall.py

pythonscripts/test.analysis.py

pythonscripts/test.dlint.py

Page 27: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JalangiCallbacks

functioninvokeFunPre(iid,f,base,args,isConstructor,isMethod,functionIid);

functioninvokeFun(iid,f,base,args,result,isConstructor,isMethod,functionIid);

functionliteral(iid,val,hasGetterSetter);

functionforinObject(iid,val);

functiondeclare(iid,name,val,isArgument,argumentIndex,isCatchParam);

functiongetFieldPre(iid,base,offset,isComputed,isOpAssign,isMethodCall);

functiongetField(iid,base,offset,val,isComputed,isOpAssign,isMethodCall);

functionputFieldPre(iid,base,offset,val,isComputed,isOpAssign);

functionputField(iid,base,offset,val,isComputed,isOpAssign);

functionread(iid,name,val,isGlobal,isScriptLocal);

functionwrite(iid,name,val,lhs,isGlobal,isScriptLocal);

function_return(iid,val);

function_throw(iid,val);

function_with(iid,val);

functionfunctionEnter(iid,f,dis,args);functionfunctionExit(iid,returnVal,wrappedExceptionVal);functionscriptEnter(iid,instrumentedFileName,originalFileName);functionscriptExit(iid,wrappedExceptionVal);functionbinaryPre(iid,op,left,right,isOpAssign,isSwitchCaseComparison,isComputed);functionbinary(iid,op,left,right,result,isOpAssign,isSwitchCaseComparison,isComputed);functionunaryPre(iid,op,left);functionunary(iid,op,left,result);functionconditional(iid,result);functioninstrumentCodePre(iid,code);functioninstrumentCode(iid,newCode,newAst);functionendExpression(iid);functionendExecution();functionrunInstrumentedFunctionBody(iid,f,functionIid);functiononReady(cb);

• Eachanalysisneedstoimplementasubsetofthesecallbacks.• MultipleanalysesclassescanbechainedfunctionbinaryPre(iid,op,left,right,isOpAssign,isSwitchCaseComparison,isComputed);functionbinary(iid,op,left,right,result,isOpAssign,isSwitchCaseComparison,isComputed);

Documentation: jalangi2/docs/MyAnalysis.html

Page 28: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

TraceAll.jsanalysis:printsallcallbacks

ForNode.js• nodesrc/js/commands/jalangi.js--inlineIID--inlineSource--analysissrc/js/sample_analyses/

ChainedAnalyses.js--analysissrc/js/runtime/SMemory.js--analysissrc/js/sample_analyses/pldi16/TraceAll.jstests/pldi16/TraceAllTest.js

Forbrowser:• nodesrc/js/commands/esnstrument_cli.js--inlineIID--inlineSource--analysissrc/js/

sample_analyses/ChainedAnalyses.js--analysissrc/js/runtime/SMemory.js--analysissrc/js/sample_analyses/pldi16/TraceAll.js--out/tmp/pldi16/TraceAllTest.htmltests/pldi16/TraceAllTest.html

• nodesrc/js/commands/esnstrument_cli.js--inlineIID--inlineSource--analysissrc/js/sample_analyses/ChainedAnalyses.js--analysissrc/js/runtime/SMemory.js--analysissrc/js/sample_analyses/pldi16/TraceAll.js--out/tmp/pldi16/TraceAllTest.jstests/pldi16/TraceAllTest.js

• openfile:///tmp/pldi16/TraceAllTest.html

Page 29: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

SampleAnalyses

Examples:src/js/sample_analyses/pldi16

Tests:tests/pldi16

Page 30: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis:checkifundefinedisconcatenatedwithastring

See:src/js/sample_analyses/pldi16/CheckUndefinedConcatenatedToString.js

this.binary=function(iid,op,left,right,result){ if(op==='+'&&typeofresult==='string'&&

(left===undefined||right===undefined))

J$.log(“Concatenatedundefinedwithstringat”+

J$.iidToLocation(J$.sid,iid));

}

Page 31: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

SourceLocations• Instrumentationassociatesaniidwitheveryexpression

• Atruntime,eachloadedscriptisgivenauniquescriptID(sid)

• sidofcurrentscriptstoredinJ$.sid

• J$.getGlobalIID(iid)getsagloballyuniqueid

• J$.iidToLocation(J$.sid,iid)getssourcelocation

• filename:start_line:start_col:end_line:end_col

• Trackslocationsofenclosingevals

Page 32: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis:countbranches

vartrueBranches={};varfalseBranches={};//initialize....

this.conditional=function(iid,result){

varid=J$.getGlobalIID(iid);

if(result)

trueBranches[id]++;

else

falseBranches[id]++;

}

this.endExecution=function(){print(trueBranches,“True”);print(falseBranches,“False”);}

functionprint(map,str){for(varidinmap)if(map.hasOwnProperty(id)){J$.log(str+“branchtakenat”+J$.iidToLocation(id)+“”+map[id]+“times”;}}

See: src/js/sample_analyses/pldi16/BranchCoverage.js

Page 33: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis:countnumberofobjectsallocatedateachsite

varallocCount={};

this.literal=function(iid,val){ varid=J$.getGlobalIID(iid);

if(typeofval===‘object’)

allocCount[id]++;

};

this.invokeFunPre=function(iid,f,

base,args,isConstructor){

varid=J$.getGlobalIID(iid);

if(isConstructor)

allocCount[id]++;

};

this.endExecution=function(){print(allocCount);}

functionprint(map){for(varidinmap)if(map.hasOwnProperty(id)){J$.log(“Objectallocatedat”+J$.iidToLocation(id)+“=”+map[id]);}}

See: src/js/sample_analyses/pldi16/CountObjectsPerAllocationSite.js

Page 34: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

ShadowObjects(SMemory.js)• AssociatesashadowobjectwitheachJavaScriptobject(excludesprimitivevaluesincludingstringsandnull)

• Associatesashadowobjectwitheachactivationframe

• Shadowobjectcanstoremeta-information

• Ashadowobjectcontainsanuniqueid– canbeusedaslogicaladdressofanobject/frame

--analysissrc/js/sample_analyses/ChainedAnalyses.js--analysissrc/js/runtime/SMemory.js

Page 35: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

SMemory.jsAPIDocumentation:jalangi2/docs/SMemory.html

• getShadowObject(obj,prop,isGetField)

Thismethodshouldbecalledonabaseobjectandapropertynametoretrievetheshadowobjectassociatedwiththeobjectthatactuallyownstheproperty

• getShadowObjectOfObject(val)Thismethodreturnstheshadowobjectassociatedwiththeargument.Iftheargumentcannotbeassociatedwithashadowobject,thefunctionreturnsundefined.

• getShadowFrame(name)

Thismethodreturnstheshadowobjectassociatedwiththeactivationframethatcontainsthevariable"name".Togetthecurrentactivationframe'sshadowobject,callgetShadowFrame('this')

• getIDFromShadowObjectOrFrame(obj)

Givenashadowobjectorframe,itreturnstheuniqueidoftheshadowobjectorframe.Itreturnsundefined,ifobjisundefined,null,ornotavalidshadowobject.

• getActualObjectOrFunctionFromShadowObjectOrFrame(obj)Givenashadowobject/frame,itreturnstheactualobject/thefunctionwhoseinvocationcreatedtheframe.

Page 36: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

AssociateAllocationSite

See:src/js/sample_analyses/pldi16/LogLoadStoreAlloc.js

this.literal=function(iid,val,hasGetterSetter){ if(typeofval==="object"&&val!==null){ varsobj=sandbox.smemory.getShadowObjectOfObject(val);sobj.allocSite=J$.iidToLocation(J$.sid,iid);} };

this.getFieldPre=function(iid,base,offset,isComputed,isOpAssign,isMethodCall){ varsobj=sandbox.smemory.getShadowObject(base,offset,true).owner;varret="Load'"+offset+"'ofobjectallocatedat"+sobj.allocSite;ret+="at"+J$.iidToLocation(J$.sid,iid);log(ret);};

Page 37: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

LogAllLoadsandStoresSee:src/js/sample_analyses/pldi16/LogLoadStoreAlloc.js

this.getFieldPre=function(iid,base,offset,isComputed,isOpAssign,isMethodCall){varsobj=sandbox.smemory.getShadowObject(base,offset,true).owner;varactualObjectId=sandbox.smemory.getIDFromShadowObjectOrFrame(sobj);varret="Loadofobject(id="+actualObjectId+")."+offset;ret+="at"+J$.iidToLocation(J$.sid,iid);log(ret);};

this.write=function(iid,name,val,lhs,isGlobal,isScriptLocal){varsobj=sandbox.smemory.getShadowFrame(name);varframeId=sandbox.smemory.getIDFromShadowObjectOrFrame(sobj);varret="Storeofframe(id="+frameId+")."+name;ret+="at"+J$.iidToLocation(J$.sid,iid);log(ret);return{result:val};};

Page 38: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis(modifysemantics):interpret‘*’as‘+’

See:src/js/sample_analyses/pldi16/ChangeSematicsOfMult.js

this.binaryPre=function(iid,op,left,right){

if(op===‘*’) return{op:op,left:left,right:right,skip:true};};

this.binary=function(iid,op,left,right,result){ if(op===‘*’) return{result:left+right};};

Page 39: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis(modifysemantics):skipexecutionofanevilfunction

See:src/js/sample_analyses/pldi16/SkipFunction.js

this.invokeFunPre=function(iid,f,base,args){ if(typeofevilFunction==="function"&&f===evilFunction){

return{f:f,base:base,args:args,skip:true};};

Page 40: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis(modifysemantics):loopafunctionbody

functionloop(n){

varret=ret?ret-1:n;//dosomething

console.log(ret);

returnret;

}

loop(10);

See: src/js/sample_analyses/pldi16/BackTrackLoop.js

Page 41: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis(modifysemantics):loopafunctionbody

functionloop(n){

varret=ret?ret-1:n;//dosomething

console.log(ret);

returnret;

}

loop(10);

Prints 10

See: src/js/sample_analyses/pldi16/BackTrackLoop.js

Page 42: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis(modifysemantics):loopafunctionbody

this.functionExit=function(iid,rv,ex){ return{returnVal:rv,wrappedExceptionVal:ex,isBacktrack:rv?true:false};

};

----------------------------------Program------------------------------------

functionloop(n){

varret=ret?ret-1:n;//dosomething

console.log(ret);

returnret;

}

loop(10); Prints 10 to 0

See: src/js/sample_analyses/pldi16/BackTrackLoop.js

Page 43: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Sampleanalysis(modifysemantics):MultiSE:Multi-PathSymbolicExecutionusingValueSummaries

(ESEC/FSE2015)

• Symbolicexecution

• Exploreallpathsinafunction– butmergestatefromallpathsbeforeexitingthefunction

• Overridedefaultsemanticstoperformsymbolicevaluation

• Backtrackwithinafunctionuntilallpathsareexplored

• Customsemanticsandbacktracking– forsimpleabstractinterpretation

– forsimpledataflowanalysis

Page 44: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Jalangi2Summary

• Observeanexecutionandcollectinformation

• Changevaluesusedinanexecution

• Changesemanticsofoperators/functions

• Explorearbitrarypathinafunction

• Re-executethebodyofafunctionrepeatedly

• Maintainyourown(abstract)stateandcallstack

• 3x-100xslowdown

Page 45: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

SeriousAnalyseswithJalangi• "Feedback-DirectedInstrumentationforDeployedJavaScriptApplications,"

– MagnusMadsenandFrankTipandEsbenAndreasenandKoushikSenandAndersMoller(ICSE'16)• "TraceTyping:AnApproachforEvaluatingRetrofittedTypeSystems,"

– EsbenAndreasenandColinS.GordonandSatishChandraandManuSridharanandFrankTipandKoushikSen(ECOOP'16)

• "TypeDevil:DynamicTypeInconsistencyAnalysisforJavaScript,”– MichaelPradelandParkerSchuhandKoushikSen(ICSE'15)

• "JITProf:PinpointingJIT-unfriendlyJavaScriptCode,"– LiangGongandMichaelPradelandKoushikSen(ESEC/FSE'15)

• "MemInsight:Platform-IndependentMemoryDebuggingforJavaScript,”– SimonJensenandManuSridharanandKoushikSenandSatishChandra(ESEC/FSE'15)

• "DLint:DynamicallyCheckingBadCodingPracticesinJavaScript,"– LiangGongandMichaelPradelandManuSridharanandKoushikSen(ISSTA'15)

• "MultiSE:Multi-PathSymbolicExecutionusingValueSummaries,”– KoushikSenandGeorgeNeculaandLiangGongandWontaeChoi,(ESEC/FSE'15)

• "TheGood,theBad,andtheUgly:AnEmpiricalStudyofImplicitTypeConversionsinJavaScript,"– MichaelPradelandKoushikSen(ECOOP'15)

• "EventBreak:AnalyzingtheResponsivenessofUserInterfacesthroughPerformance-GuidedTestGeneration,"

– MichaelPradelandParkerSchuhandGeorgeNeculaandKoushikSen(OOPSLA'14)

Page 46: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

SeriousAnalyseswithJalangi• "Feedback-DirectedInstrumentationforDeployedJavaScriptApplications,"

– MagnusMadsenandFrankTipandEsbenAndreasenandKoushikSenandAnders(ICSE'16)• "TraceTyping:AnApproachforEvaluatingRetrofittedTypeSystems,"

– EsbenAndreasenandColinS.GordonandSatishChandraandManuSridharanandFrankTipandKoushikSen(ECOOP'16)

• "TypeDevil:DynamicTypeInconsistencyAnalysisforJavaScript,”– MichaelPradelandParkerSchuhandKoushikSen(ICSE'15)

• "JITProf:PinpointingJIT-unfriendlyJavaScriptCode,"– LiangGongandMichaelPradelandKoushikSen(ESEC/FSE'15)

• "MemInsight:Platform-IndependentMemoryDebuggingforJavaScript,”– SimonJensenandManuSridharanandKoushikSenandSatishChandra(ESEC/FSE'15)

• "DLint:DynamicallyCheckingBadCodingPracticesinJavaScript,"– LiangGongandMichaelPradelandManuSridharanandKoushikSen(ISSTA'15)

• "MultiSE:Multi-PathSymbolicExecutionusingValueSummaries,”– KoushikSenandGeorgeNeculaandLiangGongandWontaeChoi,(ESEC/FSE'15)

• "TheGood,theBad,andtheUgly:AnEmpiricalStudyofImplicitTypeConversionsinJavaScript,"– MichaelPradelandKoushikSen(ECOOP'15)

• "EventBreak:AnalyzingtheResponsivenessofUserInterfacesthroughPerformance-GuidedTestGeneration,"

– MichaelPradelandParkerSchuhandGeorgeNeculaandKoushikSen(OOPSLA'14)

Page 47: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

MemInsightPlatform-Independent Memory

Debugging for JavaScript

47

http://github.com/Samsung/meminsight

Page 48: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JS Apps and Memory

48

Page 49: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Leaks and Staleness

• Staleness: long gap between last use and unreachable

• Leak: never unreachable

• Many stale objects indicates a potential problem

Object allocated ! Object used ! Object is unreachable!

Staleness!

49

Page 50: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Leak Examplevar name2obj = {};var cache = [];

function add(name) { var x = new Obj(); name2obj[name] = x; cache.push(x);}

function remove(name) { name2obj[name] = null; // forgot to remove from the cache!}

More insidious in web apps, where DOM nodes are involved50

Page 51: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Churn

51

Page 52: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Bloat

52

Page 53: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Heap Snapshots

Chrome Dev Tools https://developers.google.com/chrome-developer-tools/docs/javascript-memory-profiling

53

Page 54: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Heap Snapshots

• Capture several snapshots, diff to find possible leaks

• Low overhead, but:

• No information on staleness (does not track uses)

• Can miss excessive churn

• Cannot handle fine-grained time-varying properties

54

Page 55: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

MemInsight• Platform independent: use on any modern browser or

node.js

• Fine-grained behaviors via detailed tracing

• computes exact object lifetimes

• enables a variety of client analyses

• Exposes DOM manipulation

• Reasonable overhead55

Page 56: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

56

Page 57: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Memory leak!

57

Page 58: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Memory leak - Details

58

Page 59: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

jQuery issue!

59

Page 60: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Memory leak - Details

60

Page 61: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Challenges

• Prefer not to modify a browser engine Yet handle full JavaScript Keep overhead reasonable

• Want to report staleness of DOM nodes, without modifying browser

• Figure out object lifetimes accurately without information from the garbage collector

61

Page 62: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

How does MemInsight work?

(via Jalangi)

Jalangi is a dynamic analysis framework for JavaScriptSee FSE 2013, Sen et al.

62

Page 63: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Trace generation(A)$

JavaScript$code$

(B)$Instrumented$JavaScript$

code$

Instrumentor$(C)$

Trace$(D)$

Enhanced$Trace$

Life>me$analysis$

Run$ Client$analyses$ GUI$

Figure 5: MEMINSIGHT tool chain

2. We show that the detailed information collected byMEMINSIGHT is useful for diagnosing and fixing mem-ory issues in real-world web applications.The rest of the paper is organized as follows. After outlin-

ing the different phases of MEMINSIGHT in Sections 2–4 asdescribed above, Sections 5 and 6 respectively present a quan-titative evaluation of MEMINSIGHT and case studies showingits usefulness. Finally, Section 7 discusses related work.

2. Trace GenerationIn principle, our memory analysis framework could be imple-mented in an entirely “online” fashion, with client analysesrunning while the target program is being exercised. How-ever, this approach could have very high analysis overhead,adversely affecting the usability of the target program. Hence,our framework divides the work into two phases. A tracegeneration phase runs along with the target program, record-ing relevant memory operations into a trace file. Then, clientanalyses run in an offline mode, based on the recorded trace.Here we first discuss the design of our trace format, craftedto balance detail with analysis overhead. Then, we discussour handling of uninstrumented code and the DOM in particu-lar. We defer discussion of certain challenges in handling theJavaScript language to Section 3.3.

2.1. Trace Design

To enable client analyses like leak detection, we require thattraces be sufficient to reconstruct object lifetimes, i.e., whenobjects are created and become unreachable. Hence, tracesmust include records of each object allocation and each mem-ory write, both to variables and to object fields (“properties”in JavaScript parlance). As an optimization, we avoid loggingwrites when the old and new values are both primitive, assuch writes are irrelevant to a memory analysis. A deleteoperation on an object property is modeled as a write of null.3

To handle functions, the generator logs calls and returns,and also logs declarations of local variables to enable properscope handling. For leak detection, we also log the last use ofeach object, where an object is used when it is dereferenced or,for function objects, when it is invoked. We only log the lastuse of each object since we found that logging all uses wasprohibitively expensive, and last use information is sufficientfor computing object staleness.

Figure 6 shows the generated trace for a simple example.Most entries includes a source location at the end. The allo-

3We do not yet model the effect of delete on the shape of the object, orphysical object sizes in general; see “Limitations” in Section 5.1.

1 var x = {};

2 var y = {};

3 function m(p,q)

4 {

5 p.f = q;

6 };

7 m(x,y);

8 x = null;

DECLARE x,y,m;

ALLOCOBJ 2 at 1;

WRITE x,2 at 1;

ALLOCOBJ 3 at 2;

WRITE y,3 at 2;

ALLOCFUN 4 at 3;

WRITE m,4 at 3;

CALL 4 at 7;

DECLARE p = 2,

q = 3;

PUTFIELD 2,"f",3

at 5;

LASTUSE 2 at 5;

RETURN at 7;

LASTUSE 4 at 7;

WRITE x,0 at 8;

UNREACHABLE

2 at 8;

UNREACHABLE

3 at end;

UNREACHABLE

4 at end;

Figure 6: A simple code example and the corresponding trace.Red entries are added in the enhanced trace.

1 var elem = document.createElement("div");

2 div.innerHTML = "<p><h1>Hello World!</h1></p>";

3 document.getElementById("x").appendChild(elem);

Figure 7: Example to illustrate handling of DOM-related code.

cation entries introduce a unique identifier used to name thecorresponding object throughout the trace. We use a distinctentry type to identify function object allocation, used to enableproper handling of closures (see below). In our implementa-tion, LASTUSE entries include a timestamp and all appear atthe end of the generated trace (since the last use is only knownat the end of the program); a separate post-processing phaseinserts the entries at the appropriate slots.

2.2. Uninstrumented Code

MEMINSIGHT works robustly in the presence of uninstru-mented JavaScript code or native code from the environment,e.g., DOM functions. Here, we detail our strategies for han-dling uninstrumented code and the DOM.

Uninstrumented Code In principle, uninstrumented codecould arbitrarily mutate any memory locations to which it hasaccess. Attempting to discover all such behavior via codeinstrumentation alone would be difficult or impossible, partic-ularly since invocations of uninstrumented code may not beobservable (e.g., a browser invoking an uninstrumented eventhandler). Furthermore, such conservative detection wouldrequire frequent traversals of the full heap visible to uninstru-mented code, a very costly operation.

In practice, we have found a policy of only tracking refer-ences created in instrumented code to strike a good balancebetween coverage of relevant behaviors and analysis overhead.

4

63

Page 64: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Trace generation(A)$

JavaScript$code$

(B)$Instrumented$JavaScript$

code$

Instrumentor$(C)$

Trace$(D)$

Enhanced$Trace$

Life>me$analysis$

Run$ Client$analyses$ GUI$

Figure 5: MEMINSIGHT tool chain

2. We show that the detailed information collected byMEMINSIGHT is useful for diagnosing and fixing mem-ory issues in real-world web applications.The rest of the paper is organized as follows. After outlin-

ing the different phases of MEMINSIGHT in Sections 2–4 asdescribed above, Sections 5 and 6 respectively present a quan-titative evaluation of MEMINSIGHT and case studies showingits usefulness. Finally, Section 7 discusses related work.

2. Trace GenerationIn principle, our memory analysis framework could be imple-mented in an entirely “online” fashion, with client analysesrunning while the target program is being exercised. How-ever, this approach could have very high analysis overhead,adversely affecting the usability of the target program. Hence,our framework divides the work into two phases. A tracegeneration phase runs along with the target program, record-ing relevant memory operations into a trace file. Then, clientanalyses run in an offline mode, based on the recorded trace.Here we first discuss the design of our trace format, craftedto balance detail with analysis overhead. Then, we discussour handling of uninstrumented code and the DOM in particu-lar. We defer discussion of certain challenges in handling theJavaScript language to Section 3.3.

2.1. Trace Design

To enable client analyses like leak detection, we require thattraces be sufficient to reconstruct object lifetimes, i.e., whenobjects are created and become unreachable. Hence, tracesmust include records of each object allocation and each mem-ory write, both to variables and to object fields (“properties”in JavaScript parlance). As an optimization, we avoid loggingwrites when the old and new values are both primitive, assuch writes are irrelevant to a memory analysis. A deleteoperation on an object property is modeled as a write of null.3

To handle functions, the generator logs calls and returns,and also logs declarations of local variables to enable properscope handling. For leak detection, we also log the last use ofeach object, where an object is used when it is dereferenced or,for function objects, when it is invoked. We only log the lastuse of each object since we found that logging all uses wasprohibitively expensive, and last use information is sufficientfor computing object staleness.

Figure 6 shows the generated trace for a simple example.Most entries includes a source location at the end. The allo-

3We do not yet model the effect of delete on the shape of the object, orphysical object sizes in general; see “Limitations” in Section 5.1.

1 var x = {};

2 var y = {};

3 function m(p,q)

4 {

5 p.f = q;

6 };

7 m(x,y);

8 x = null;

DECLARE x,y,m;

ALLOCOBJ 2 at 1;

WRITE x,2 at 1;

ALLOCOBJ 3 at 2;

WRITE y,3 at 2;

ALLOCFUN 4 at 3;

WRITE m,4 at 3;

CALL 4 at 7;

DECLARE p = 2,

q = 3;

PUTFIELD 2,"f",3

at 5;

LASTUSE 2 at 5;

RETURN at 7;

LASTUSE 4 at 7;

WRITE x,0 at 8;

UNREACHABLE

2 at 8;

UNREACHABLE

3 at end;

UNREACHABLE

4 at end;

Figure 6: A simple code example and the corresponding trace.Red entries are added in the enhanced trace.

1 var elem = document.createElement("div");

2 div.innerHTML = "<p><h1>Hello World!</h1></p>";

3 document.getElementById("x").appendChild(elem);

Figure 7: Example to illustrate handling of DOM-related code.

cation entries introduce a unique identifier used to name thecorresponding object throughout the trace. We use a distinctentry type to identify function object allocation, used to enableproper handling of closures (see below). In our implementa-tion, LASTUSE entries include a timestamp and all appear atthe end of the generated trace (since the last use is only knownat the end of the program); a separate post-processing phaseinserts the entries at the appropriate slots.

2.2. Uninstrumented Code

MEMINSIGHT works robustly in the presence of uninstru-mented JavaScript code or native code from the environment,e.g., DOM functions. Here, we detail our strategies for han-dling uninstrumented code and the DOM.

Uninstrumented Code In principle, uninstrumented codecould arbitrarily mutate any memory locations to which it hasaccess. Attempting to discover all such behavior via codeinstrumentation alone would be difficult or impossible, partic-ularly since invocations of uninstrumented code may not beobservable (e.g., a browser invoking an uninstrumented eventhandler). Furthermore, such conservative detection wouldrequire frequent traversals of the full heap visible to uninstru-mented code, a very costly operation.

In practice, we have found a policy of only tracking refer-ences created in instrumented code to strike a good balancebetween coverage of relevant behaviors and analysis overhead.

4

Preserve line numbers

63

Page 65: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Trace generation(A)$

JavaScript$code$

(B)$Instrumented$JavaScript$

code$

Instrumentor$(C)$

Trace$(D)$

Enhanced$Trace$

Life>me$analysis$

Run$ Client$analyses$ GUI$

Figure 5: MEMINSIGHT tool chain

2. We show that the detailed information collected byMEMINSIGHT is useful for diagnosing and fixing mem-ory issues in real-world web applications.The rest of the paper is organized as follows. After outlin-

ing the different phases of MEMINSIGHT in Sections 2–4 asdescribed above, Sections 5 and 6 respectively present a quan-titative evaluation of MEMINSIGHT and case studies showingits usefulness. Finally, Section 7 discusses related work.

2. Trace GenerationIn principle, our memory analysis framework could be imple-mented in an entirely “online” fashion, with client analysesrunning while the target program is being exercised. How-ever, this approach could have very high analysis overhead,adversely affecting the usability of the target program. Hence,our framework divides the work into two phases. A tracegeneration phase runs along with the target program, record-ing relevant memory operations into a trace file. Then, clientanalyses run in an offline mode, based on the recorded trace.Here we first discuss the design of our trace format, craftedto balance detail with analysis overhead. Then, we discussour handling of uninstrumented code and the DOM in particu-lar. We defer discussion of certain challenges in handling theJavaScript language to Section 3.3.

2.1. Trace Design

To enable client analyses like leak detection, we require thattraces be sufficient to reconstruct object lifetimes, i.e., whenobjects are created and become unreachable. Hence, tracesmust include records of each object allocation and each mem-ory write, both to variables and to object fields (“properties”in JavaScript parlance). As an optimization, we avoid loggingwrites when the old and new values are both primitive, assuch writes are irrelevant to a memory analysis. A deleteoperation on an object property is modeled as a write of null.3

To handle functions, the generator logs calls and returns,and also logs declarations of local variables to enable properscope handling. For leak detection, we also log the last use ofeach object, where an object is used when it is dereferenced or,for function objects, when it is invoked. We only log the lastuse of each object since we found that logging all uses wasprohibitively expensive, and last use information is sufficientfor computing object staleness.

Figure 6 shows the generated trace for a simple example.Most entries includes a source location at the end. The allo-

3We do not yet model the effect of delete on the shape of the object, orphysical object sizes in general; see “Limitations” in Section 5.1.

1 var x = {};

2 var y = {};

3 function m(p,q)

4 {

5 p.f = q;

6 };

7 m(x,y);

8 x = null;

DECLARE x,y,m;

ALLOCOBJ 2 at 1;

WRITE x,2 at 1;

ALLOCOBJ 3 at 2;

WRITE y,3 at 2;

ALLOCFUN 4 at 3;

WRITE m,4 at 3;

CALL 4 at 7;

DECLARE p = 2,

q = 3;

PUTFIELD 2,"f",3

at 5;

LASTUSE 2 at 5;

RETURN at 7;

LASTUSE 4 at 7;

WRITE x,0 at 8;

UNREACHABLE

2 at 8;

UNREACHABLE

3 at end;

UNREACHABLE

4 at end;

Figure 6: A simple code example and the corresponding trace.Red entries are added in the enhanced trace.

1 var elem = document.createElement("div");

2 div.innerHTML = "<p><h1>Hello World!</h1></p>";

3 document.getElementById("x").appendChild(elem);

Figure 7: Example to illustrate handling of DOM-related code.

cation entries introduce a unique identifier used to name thecorresponding object throughout the trace. We use a distinctentry type to identify function object allocation, used to enableproper handling of closures (see below). In our implementa-tion, LASTUSE entries include a timestamp and all appear atthe end of the generated trace (since the last use is only knownat the end of the program); a separate post-processing phaseinserts the entries at the appropriate slots.

2.2. Uninstrumented Code

MEMINSIGHT works robustly in the presence of uninstru-mented JavaScript code or native code from the environment,e.g., DOM functions. Here, we detail our strategies for han-dling uninstrumented code and the DOM.

Uninstrumented Code In principle, uninstrumented codecould arbitrarily mutate any memory locations to which it hasaccess. Attempting to discover all such behavior via codeinstrumentation alone would be difficult or impossible, partic-ularly since invocations of uninstrumented code may not beobservable (e.g., a browser invoking an uninstrumented eventhandler). Furthermore, such conservative detection wouldrequire frequent traversals of the full heap visible to uninstru-mented code, a very costly operation.

In practice, we have found a policy of only tracking refer-ences created in instrumented code to strike a good balancebetween coverage of relevant behaviors and analysis overhead.

4

Preserve call stack

63

Page 66: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Trace generation(A)$

JavaScript$code$

(B)$Instrumented$JavaScript$

code$

Instrumentor$(C)$

Trace$(D)$

Enhanced$Trace$

Life>me$analysis$

Run$ Client$analyses$ GUI$

Figure 5: MEMINSIGHT tool chain

2. We show that the detailed information collected byMEMINSIGHT is useful for diagnosing and fixing mem-ory issues in real-world web applications.The rest of the paper is organized as follows. After outlin-

ing the different phases of MEMINSIGHT in Sections 2–4 asdescribed above, Sections 5 and 6 respectively present a quan-titative evaluation of MEMINSIGHT and case studies showingits usefulness. Finally, Section 7 discusses related work.

2. Trace GenerationIn principle, our memory analysis framework could be imple-mented in an entirely “online” fashion, with client analysesrunning while the target program is being exercised. How-ever, this approach could have very high analysis overhead,adversely affecting the usability of the target program. Hence,our framework divides the work into two phases. A tracegeneration phase runs along with the target program, record-ing relevant memory operations into a trace file. Then, clientanalyses run in an offline mode, based on the recorded trace.Here we first discuss the design of our trace format, craftedto balance detail with analysis overhead. Then, we discussour handling of uninstrumented code and the DOM in particu-lar. We defer discussion of certain challenges in handling theJavaScript language to Section 3.3.

2.1. Trace Design

To enable client analyses like leak detection, we require thattraces be sufficient to reconstruct object lifetimes, i.e., whenobjects are created and become unreachable. Hence, tracesmust include records of each object allocation and each mem-ory write, both to variables and to object fields (“properties”in JavaScript parlance). As an optimization, we avoid loggingwrites when the old and new values are both primitive, assuch writes are irrelevant to a memory analysis. A deleteoperation on an object property is modeled as a write of null.3

To handle functions, the generator logs calls and returns,and also logs declarations of local variables to enable properscope handling. For leak detection, we also log the last use ofeach object, where an object is used when it is dereferenced or,for function objects, when it is invoked. We only log the lastuse of each object since we found that logging all uses wasprohibitively expensive, and last use information is sufficientfor computing object staleness.

Figure 6 shows the generated trace for a simple example.Most entries includes a source location at the end. The allo-

3We do not yet model the effect of delete on the shape of the object, orphysical object sizes in general; see “Limitations” in Section 5.1.

1 var x = {};

2 var y = {};

3 function m(p,q)

4 {

5 p.f = q;

6 };

7 m(x,y);

8 x = null;

DECLARE x,y,m;

ALLOCOBJ 2 at 1;

WRITE x,2 at 1;

ALLOCOBJ 3 at 2;

WRITE y,3 at 2;

ALLOCFUN 4 at 3;

WRITE m,4 at 3;

CALL 4 at 7;

DECLARE p = 2,

q = 3;

PUTFIELD 2,"f",3

at 5;

LASTUSE 2 at 5;

RETURN at 7;

LASTUSE 4 at 7;

WRITE x,0 at 8;

UNREACHABLE

2 at 8;

UNREACHABLE

3 at end;

UNREACHABLE

4 at end;

Figure 6: A simple code example and the corresponding trace.Red entries are added in the enhanced trace.

1 var elem = document.createElement("div");

2 div.innerHTML = "<p><h1>Hello World!</h1></p>";

3 document.getElementById("x").appendChild(elem);

Figure 7: Example to illustrate handling of DOM-related code.

cation entries introduce a unique identifier used to name thecorresponding object throughout the trace. We use a distinctentry type to identify function object allocation, used to enableproper handling of closures (see below). In our implementa-tion, LASTUSE entries include a timestamp and all appear atthe end of the generated trace (since the last use is only knownat the end of the program); a separate post-processing phaseinserts the entries at the appropriate slots.

2.2. Uninstrumented Code

MEMINSIGHT works robustly in the presence of uninstru-mented JavaScript code or native code from the environment,e.g., DOM functions. Here, we detail our strategies for han-dling uninstrumented code and the DOM.

Uninstrumented Code In principle, uninstrumented codecould arbitrarily mutate any memory locations to which it hasaccess. Attempting to discover all such behavior via codeinstrumentation alone would be difficult or impossible, partic-ularly since invocations of uninstrumented code may not beobservable (e.g., a browser invoking an uninstrumented eventhandler). Furthermore, such conservative detection wouldrequire frequent traversals of the full heap visible to uninstru-mented code, a very costly operation.

In practice, we have found a policy of only tracking refer-ences created in instrumented code to strike a good balancebetween coverage of relevant behaviors and analysis overhead.

4

Only last use

63

Page 67: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Trace generation(A)$

JavaScript$code$

(B)$Instrumented$JavaScript$

code$

Instrumentor$(C)$

Trace$(D)$

Enhanced$Trace$

Life>me$analysis$

Run$ Client$analyses$ GUI$

Figure 5: MEMINSIGHT tool chain

2. We show that the detailed information collected byMEMINSIGHT is useful for diagnosing and fixing mem-ory issues in real-world web applications.The rest of the paper is organized as follows. After outlin-

ing the different phases of MEMINSIGHT in Sections 2–4 asdescribed above, Sections 5 and 6 respectively present a quan-titative evaluation of MEMINSIGHT and case studies showingits usefulness. Finally, Section 7 discusses related work.

2. Trace GenerationIn principle, our memory analysis framework could be imple-mented in an entirely “online” fashion, with client analysesrunning while the target program is being exercised. How-ever, this approach could have very high analysis overhead,adversely affecting the usability of the target program. Hence,our framework divides the work into two phases. A tracegeneration phase runs along with the target program, record-ing relevant memory operations into a trace file. Then, clientanalyses run in an offline mode, based on the recorded trace.Here we first discuss the design of our trace format, craftedto balance detail with analysis overhead. Then, we discussour handling of uninstrumented code and the DOM in particu-lar. We defer discussion of certain challenges in handling theJavaScript language to Section 3.3.

2.1. Trace Design

To enable client analyses like leak detection, we require thattraces be sufficient to reconstruct object lifetimes, i.e., whenobjects are created and become unreachable. Hence, tracesmust include records of each object allocation and each mem-ory write, both to variables and to object fields (“properties”in JavaScript parlance). As an optimization, we avoid loggingwrites when the old and new values are both primitive, assuch writes are irrelevant to a memory analysis. A deleteoperation on an object property is modeled as a write of null.3

To handle functions, the generator logs calls and returns,and also logs declarations of local variables to enable properscope handling. For leak detection, we also log the last use ofeach object, where an object is used when it is dereferenced or,for function objects, when it is invoked. We only log the lastuse of each object since we found that logging all uses wasprohibitively expensive, and last use information is sufficientfor computing object staleness.

Figure 6 shows the generated trace for a simple example.Most entries includes a source location at the end. The allo-

3We do not yet model the effect of delete on the shape of the object, orphysical object sizes in general; see “Limitations” in Section 5.1.

1 var x = {};

2 var y = {};

3 function m(p,q)

4 {

5 p.f = q;

6 };

7 m(x,y);

8 x = null;

DECLARE x,y,m;

ALLOCOBJ 2 at 1;

WRITE x,2 at 1;

ALLOCOBJ 3 at 2;

WRITE y,3 at 2;

ALLOCFUN 4 at 3;

WRITE m,4 at 3;

CALL 4 at 7;

DECLARE p = 2,

q = 3;

PUTFIELD 2,"f",3

at 5;

LASTUSE 2 at 5;

RETURN at 7;

LASTUSE 4 at 7;

WRITE x,0 at 8;

UNREACHABLE

2 at 8;

UNREACHABLE

3 at end;

UNREACHABLE

4 at end;

Figure 6: A simple code example and the corresponding trace.Red entries are added in the enhanced trace.

1 var elem = document.createElement("div");

2 div.innerHTML = "<p><h1>Hello World!</h1></p>";

3 document.getElementById("x").appendChild(elem);

Figure 7: Example to illustrate handling of DOM-related code.

cation entries introduce a unique identifier used to name thecorresponding object throughout the trace. We use a distinctentry type to identify function object allocation, used to enableproper handling of closures (see below). In our implementa-tion, LASTUSE entries include a timestamp and all appear atthe end of the generated trace (since the last use is only knownat the end of the program); a separate post-processing phaseinserts the entries at the appropriate slots.

2.2. Uninstrumented Code

MEMINSIGHT works robustly in the presence of uninstru-mented JavaScript code or native code from the environment,e.g., DOM functions. Here, we detail our strategies for han-dling uninstrumented code and the DOM.

Uninstrumented Code In principle, uninstrumented codecould arbitrarily mutate any memory locations to which it hasaccess. Attempting to discover all such behavior via codeinstrumentation alone would be difficult or impossible, partic-ularly since invocations of uninstrumented code may not beobservable (e.g., a browser invoking an uninstrumented eventhandler). Furthermore, such conservative detection wouldrequire frequent traversals of the full heap visible to uninstru-mented code, a very costly operation.

In practice, we have found a policy of only tracking refer-ences created in instrumented code to strike a good balancebetween coverage of relevant behaviors and analysis overhead.

4

From lifetime analysis

63

Page 68: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Object lifetimes• From trace, model runtime heap

• Including call stack and closures

• Reference counting to compute unreachability time

• Handle cycles with Merlin algorithm[Hertz et al. ASPLOS’06]

• Insert unreachability times in the enhanced trace

64

Page 69: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

DOM Challenges• DOM: tree data structure representing rendered HTML

• Often involved in web app memory leaks

• Many manipulations not directly visible to JavaScript

// allocates new div elementvar elem = document.createElement(“div");

// allocates DOM tree from HTML string and// updates children of elemelem.innerHTML = "<p><h1>Hello World!</h1></p>”;

// inserts elem into global DOMdocument.getElementById("x").appendChild(elem);

65

Page 70: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Our DOM Handling

• elem gets reified into a fresh object ID • no special handling of createElement

• For DOM manipulations, leverage HTML5 mutation observers • Provide asynchronous notifications of DOM mutation • Handles innerHTML manipulation and appendChild

• Additional handling of innerHTML for better source locations

// allocates new div elementvar elem = document.createElement(“div");

// allocates DOM tree from HTML string and// updates children of elemelem.innerHTML = "<p><h1>Hello World!</h1></p>";

// inserts elem into global DOMdocument.getElementById("x").appendChild(elem);

66

Page 71: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Other tricky features

• Constructors: need to properly handle this, and get good source locations

• Eval: instrument on the fly

• Getters / setters: don’t treat calls as reads / writes

• Global object, prototypes, further native models, …

67

Page 72: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Clients built atop MemInsight

• Leak detection: increasing stale object count at idle points (empty call stack)

• Non-escaping: no object escapes allocating function

• Leverages execution index [Xin et al. PLDI’08]

• Inlineable: objects consistently “owned” by objects from another site

• Many more are possible!

68

Page 73: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Case Studies (see paper for details)

• Leaks

• Fixed in one Tizen app shopping_list (patch accepted)

• Confirmed existing patch fixes leak in dataTables

• Leaks found by internal users in other apps

• Churn

• Fixed in one Tizen app annex for 10% speedup (patch accepted)

• 10X speedup for escodegen (patch accepted)

• Bloat: Found object inlining opportunity in old esprima version (since fixed)

69

Page 74: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Leak in Shopping List app

Should have used $.empty()!

70

Page 75: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Run an instrumented app

71

Page 76: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Interactive staleness analysis

72

Page 77: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Interactive staleness analysis

73

Page 78: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Overhead

Low overhead for (most) interactive apps

benchmark overheadrichards 10.4Xdeltablue 15X

crypto 47.1Xraytrace 41.3X

earley-boyer 99.8Xregexp 26.7Xsplay 43.4X

navier-stokes 45.4Xpdfjs 31.8Xbox2d 35.8X

typescript 77.2X

74

Page 79: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Reducing Overhead• Only log the last use of an object (not all uses)

• Don’t log operations on primitive fields

• Enhanced Jalangi to do selective instrumentation

• Binary trace format

• Work with simulated heap as opposed to real heap

• Reflection too expensive / fragile

75

Page 80: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Advanced Jalangi Usage

76

Page 81: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Tracing• Common technique: store a trace, and do heavyweight

analysis over the trace • Supported directly in Jalangi 1 via record/replay • But, hard to debug and write analyses

• lib/analysis/Loggers.ts has all analysis tracing code • Under Node.js, dump trace to file system

(BinaryFSLogger) • From web, trace over web socket

(BinaryWebSocketLogger) • lib/server/server.ts has server code • pipes trace directly to running lifetime analysis

77

Page 82: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Integrating Static Analysis

• MemInsight needs the “free variables” of each function

• Captured by closures, relevant for lifetimes • Computed by freeVarsAstHandler.ts • Provided as an AST handler to Jalangi instrumentation • Jalangi stores result of AST handler inside

instrumented code • For eval’d code, use the instrumentCode callback

78

Page 83: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Native Methods• Built-in methods that cannot be instrumented

• Standard JS library, DOM routines • (In general, any uninstrumented code)

• Modeling is analysis-specific • For MemInsight, lib/analysis/NativeModels.ts

• Also, careful with callbacks from native methods • may see functionEnter without invokeFunPre

79

Page 84: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Analysis Configuration

• May want analysis-wide configuration options • E.g., MemInsight allows for a debug function for

dumping ref counts • Use --initParam option to instrument.js (web) or esnstrument_cli.js (node.js)

• values stored in J$.initParams

80

Page 85: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Debugging with JSDelta

81

https://github.com/WALA/jsdelta

Page 86: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JSDelta: motivation• Building a Jalangi analysis

• Works great on unit tests

• But, crashes on jQuery!

• What went wrong? Need a minimized input

• Jsdelta does automatic input minimization

• Via delta debugging [Zeller, FSE’99]

82

Page 87: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

JSDelta: Demo

83

Google “JS Delta Walkthrough”

Page 88: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Using JSDelta• Easy: write a script that prints a message when error

occurs

• Also works for JSON, entire directories

• For a Jalangi analysis:

• Check for errors in uninstrumented program first

• Always run with a timeout (e.g., with timeout command)

• For browser code, use PhantomJS, Selenium, etc.

84

Page 89: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• DLint:DynamicallyCheckingJSCodingPractice

• JITProf:FindJScodethatprohibitJIT-optimization

85

DLintandJITProf

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

[ISSTA’15] DLint: Dynamically Checking Bad Coding Practices in JavaScriptLiang Gong, Michael Pradel, Manu Sridharan, Koushik Sen

[FSE’15] JITProf: Pinpointing JIT-unfriendly JavaScript codeLiang Gong, Michael Pradel, Koushik Sen

Page 90: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

DLintandJITProfforWebPages

mitmproxyObserve requests & intercepts responses

that contain JS and webpages

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.86

Page 91: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

DLintandJITProfforWebPages

mitmproxyObserve requests & intercepts responses

that contain JS and webpages

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.86

Page 92: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• DLint:DynamicallyCheckingJSCodingPractice

• JITProf:FindJScodethatprohibitJIT-optimization

87

DLintandJITProf

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

[ISSTA’15] DLint: Dynamically Checking Bad Coding Practices in JavaScriptLiang Gong, Michael Pradel, Manu Sridharan, Koushik Sen

[FSE’15] JITProf: Pinpointing JIT-unfriendly JavaScript codeLiang Gong, Michael Pradel, Koushik Sen

Page 93: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• Goodcodingpractices• Informalrules• Improvecodequality

• Betterqualitymeans:• Fewercorrectnessissues• Betterperformance• Betterusability• Bettermaintainability• Fewersecurityloopholes• Fewersurprises• …

88

Whatarecodingpractices?

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 94: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

89

varsum=0,value;vararray=[11,22,33];for(valueinarray){sum+=value;}>sum?

Rule:avoidusingfor..inoverarrays

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 95: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

90

varsum=0,value;vararray=[11,22,33];for(valueinarray){sum+=value;}>sum?

11+22+33=>66arrayindex

(notarrayvalue)0+1+2=>3 arrayindex:string0+"0"+"1"+"2"=>"0012"

Rule:avoidusingfor..inoverarrays

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 96: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• Cross-browserissues• ResultdependsontheArrayprototypeobject

91

varsum=0,value;vararray=[11,22,33];for(valueinarray){sum+=value;}>sum?

11+22+33=>66arrayindex

(notarrayvalue)0+1+2=>3 arrayindex:string0+"0"+"1"+"2"=>"0012"

>"0012indexOftoString..."

Rule:avoidusingfor..inoverarrays

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 97: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

92

varsum=0,value;vararray=[11,22,33];for(valueinarray){sum+=value;}>sum?

for(i=0;i<array.length;i++){sum+=array[i];}

functionaddup(element,index,array){sum+=element;}array.forEach(addup);

Rule:avoidusingfor..inoverarrays

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 98: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

93

varsum=0,value;vararray=[11,22,33];for(valueinarray){sum+=value;}>sum?

for(i=0;i<array.length;i++){sum+=array[i];}

functionaddup(element,index,array){sum+=element;}array.forEach(addup);

Rule:avoidusingfor..inoverarrays

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 99: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

CodingPracticesandLintTools

• ExistingLint-likecheckers– Inspectsourcecode

– Detectcommonmistakes

• Limitations:– Approximatesbehavior

– Unknownaliases

– Linttoolsfavorprecisionoversoundness

• Difficulty:Precisestaticprogramanalysis

94LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 100: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

95

• DynamicLintercheckingcodequalityrulesforJS

• Open-source,robust,andextensibleframework• Formalizedandimplemented28rules

– Counterpartsofstaticrules– Additionalrules

• Empiricalstudy– ItisbettertouseDLintandstaticlintertogether

DLint

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 101: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

96

varsum=0,value;vararray=[11,22,33];for(valueinarray){sum+=value;}>sum?

for(i=0;i<array.length;i++){sum+=array[i];}

functionaddup(element,index,array){sum+=element;}array.forEach(addup);

Detectfor..inoverarrayswithJalangi

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 102: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

97LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

Detectfor..inoverarrayswithJalangi

Page 103: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

98LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

Have a warning whenobj in for-in is an array.

Detectfor..inoverarrayswithJalangi

Page 104: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

99LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

Have a warning whenobj in for-in is an array.

Page 105: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

100LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

functionforinObject(iid,val){

}

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

Have a warning whenobj in for-in is an array.

Page 106: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

101LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

functionforinObject(iid,val){

}

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

Have a warning whenobj in for-in is an array.

Page 107: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

102LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

functionforinObject(iid,val){if(isArray(val)){//reportwarning!}}

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

Have a warning whenobj in for-in is an array.

Page 108: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

103LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

functionforinObject(iid,val){if(isArray(val)){//reportwarning!}}

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

Have a warning whenobj in for-in is an array.

Page 109: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

104LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

functionforinObject(iid,val){if(isArray(val)){//reportwarning!}}

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

J$.iidToLocation(iid);

Have a warning whenobj in for-in is an array.

Page 110: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

105LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

functionforinObject(iid,val){if(isArray(val)){//reportwarning!}}

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

file.js:<start line>:<start col>:<end line>:<end col>

J$.iidToLocation(iid);

Have a warning whenobj in for-in is an array.

Page 111: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

106LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

for(valueinobj){sum+=value;}

instrumentation

functionforinObject(iid,val){if(isArray(val)){//reportwarning!}}

Detectfor..inoverarrayswithJalangi

JalangiInstrumentedCode

file.js:<start line>:<start col>:<end line>:<end col>

J$.iidToLocation(iid);

Have a warning whenobj in for-in is an array.

Page 112: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Checkers

107

CheckNaN.jsConcatUndefinedToString.jsNonObjectPrototype.js SetFieldToPrimitive.js OverFlowUnderFlow.js StyleMisuse.js ToStringGivesNonString.js UndefinedOffset.js NoEffectOperation.js AddEnumerablePropertyToObject.js ConstructWrappedPrimitive.jsInconsistentNewCallPrefix.jsUncountableSpaceInRegexp.jsFloatNumberEqualityComparison.js

FunctionToString.js ShadowProtoProperty.jsForInArray.jsNonNumericArrayProperty.jsOverwrittenPrototype.jsGlobalThis.js CompareFunctionWithPrimitives.js InconsistentConstructor.js FunctionCalledWithMoreArguments.js IllegalUseOfArgumentsVariable.js DoubleEvaluation.jsEmptyClassInRegexp.jsUseArrObjConstrWithoutArg.jsMissRadixArgInParseNum.js

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 113: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Chained Analysis

108

PutField(Read("a",a),"f",GetField(Read("b",b),"g"))

a.f=b.g

functions

ChainedAnalysis

PutField

Read

functions

Checker-1

PutField

Read

…functions

Checker-2

PutField

Read

functions

Checker-n

PutField

Read

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 114: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Other Resources

https://github.com/Samsung/jalangi2

Jalangi (v2) Github

https://github.com/ksen007/jalangi2analysesDLint + JITProf Github based on Jalangi (v2)

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.109

https://github.com/JacksonGL/jitprof-visualizationJITProf Visualization Github based on Jalangi (v2)

Page 115: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• DLint:DynamicallyCheckingJSCodingPractice

• JITProf:FindJScodethatprohibitJIT-optimization

110

DLintandJITProf

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

[ISSTA’15] DLint: Dynamically Checking Bad Coding Practices in JavaScriptLiang Gong, Michael Pradel, Manu Sridharan, Koushik Sen

[FSE’15] JITProf: Pinpointing JIT-unfriendly JavaScript codeLiang Gong, Michael Pradel, Koushik Sen

Page 116: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Simplifies coding• Write less, do more ! more productive• Code is less verbose ! easier to understand

Dynamic language features:

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.111

Motivation of JITProf

Page 117: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Simplifies coding• Write less, do more ! more productive• Code is less verbose ! easier to understand Slow execution• Too many runtime checks• Object property lookup -> hash table lookup ...

Dynamic language features:

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.112

Motivation of JITProf

Page 118: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

SplayTree.prototype.insert=function(key,value){ ...varnode=newSplayTree.Node(key,value);if(key>this.root_.key){node.left=this.root_;node.right=this.root_.right;...}else{node.right=this.root_;node.left=this.root_.left;...}this.root_=node;};

113

Pinpointing JIT-unfriendly JavaScript Code

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

• Code snippet from Google Octane Benchmark:

Page 119: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

SplayTree.prototype.insert=function(key,value){ ...varnode=newSplayTree.Node(key,value);if(key>this.root_.key){node.left=this.root_;node.right=this.root_.right;...}else{node.right=this.root_;node.left=this.root_.left;...}this.root_=node;};

114

Pinpointing JIT-unfriendly JavaScript Code

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

• Code snippet from Google Octane Benchmark:

Causeofpoorperformance:• nodehastwolayouts:offsetofleftinnodecanbe0or1• JITcannotreplacenode.leftwithnode[0]ornode[1]

Page 120: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Performanceboost:

15%

6.7%

115

Pinpointing JIT-unfriendly JavaScript Code

SplayTree.prototype.insert=function(key,value){ ...varnode=newSplayTree.Node(key,value);if(key>this.root_.key){node.left=this.root_;node.right=this.root_.right;...}else{node.right=this.root_;node.left=this.root_.left;...}this.root_=node;};

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

• Code snippet from Google Octane Benchmark:

Page 121: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Performanceboost:

15%

6.7%

116

Pinpointing JIT-unfriendly JavaScript Code

SplayTree.prototype.insert=function(key,value){ ...varnode=newSplayTree.Node(key,value);if(key>this.root_.key){node.left=this.root_;node.right=this.root_.right;...}else{node.right=this.root_;node.left=this.root_.left;...}this.root_=node;};

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

• Code snippet from Google Octane Benchmark:

JITProf Simulates the Hidden Classesbased on the information provided by Jalangi

Page 122: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.117

• Each object has a meta information associated with it

• The meta information keeps track of its object layout and its transition history.

Page 123: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.118

Page 124: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.119

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Hiddenclasssimulationbeforethestatement

Page 125: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.120

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Objects

Property Offset__proto__

HiddenClassesAnonymous

Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

Page 126: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.121

Objects

Property Offset

__proto__

HiddenClassesAnonymous

Offset0 4

Offset1 3

HiddenClassProperty Offset

b 0

__proto__

Property Offset

b 0

__proto__

a 1

Objects

Anonymous2

HiddenClass

Property Offset

a 0

__proto__

Offset0 2

Offset1 3

Property Offset

a 0

__proto__

b 1

Page 127: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.122

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Property Offset__proto__Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

Page 128: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.123

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Property Offset__proto__Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

functionputFieldPre(iid,base,offset,val…){//logicforupdatingthehiddenclass

}

invoke

Jalangi

Page 129: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.124

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Property Offset__proto__Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

functionputFieldPre(iid,base,offset,val…){//logicforupdatingthehiddenclass

}

invoke

Jalangithis.b=4;

Page 130: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.125

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Property Offset__proto__Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

functionputFieldPre(iid,base,offset,val…){//logicforupdatingthehiddenclass

}

invoke

Jalangithis.b=4;

Page 131: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.126

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Property Offset__proto__Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

functionputFieldPre(iid,base,offset,val…){//logicforupdatingthehiddenclass

}

invoke

Jalangithis.b=4;

'b'

Page 132: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.127

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Property Offset__proto__Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

functionputFieldPre(iid,base,offset,val…){//logicforupdatingthehiddenclass

}

invoke

Jalangithis.b=4;

'b'

Page 133: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.128

Objects

Property Offset

__proto__

HiddenClassesAnonymous

HiddenClass

Property Offset__proto__Offset0 4

HiddenClass

Property Offset

b 0

__proto__

Hiddenclasssimulationbeforethestatement

Hiddenclasssimulationafterthestatement

functionputFieldPre(iid,base,offset,val…){varsobj=J$.smemory.getShadowObject(base);sobj.hiddenClass...}

invoke

Jalangithis.b=4;

'b'

Page 134: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

varo={a:1,b:2};

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.129

InterceptputFieldtoupdatethehiddenclass

Page 135: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

varo={a:1,b:2};

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.130

InterceptputFieldtoupdatethehiddenclass

InterceptinvokeFuntorecordobjectcreationlocation

Page 136: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

varo={a:1,b:2};

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.131

InterceptputFieldtoupdatethehiddenclass

InterceptinvokeFuntorecordobjectcreationlocation

InterceptgetFieldtorecordinlinecachemisses

Page 137: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Back to the Motivating Example

functionThing(flag){if(!flag){this.b=4;this.a=3;}else{this.a=2;this.b=1;}}

for(vari=0;i<1000000;i++){varo=newThing(i%2);result+=o.a+o.b;}

varo={a:1,b:2};

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.132

InterceptputFieldtoupdatethehiddenclass

InterceptinvokeFuntorecordobjectcreationlocation

InterceptgetFieldtorecordinlinecachemisses

Interceptliteraltoupdatehiddenclass+recordobjectcreationlocation

Page 138: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.133

• Use inconsistent object layout• Access undeclared property or array element• Store non-numeric value in numeric arrays• Use in-contiguous keys for arrays• Not all properties are initialized in constructors• … and more

JIT-unfriendly Code Checked by JITProf

Page 139: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

vararray=[];for(vari=10000;i>=0;i--){array[i]=i;}

134

Rule #5: Use Contiguous Keys for Array

Page 140: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

vararray=[];for(vari=10000;i>=0;i--){array[i]=i;}

135

array[10000]=10000;array[9999]=9999;...

• non-contiguous array • To save memory, JIT-engine decides to represent

the array with slow data structures like hash table.

Rule #5: Use Contiguous Keys for Array

Page 141: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

10X+speedup!

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

vararray=[];for(vari=10000;i>=0;i--){array[i]=i;}

for(vari=0;i<=10000;i++){array[i]=i;}

136

Rule #5: Use Contiguous Keys for Array

Page 142: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

vararray=[];for(vari=10000;i>=0;i--){array[i]=i;}

137

loc1:

• InterceptputFieldoperationofarrays• Ranklocationsbynumberassignmentstonon-contiguousarrays

Rule #5: Use Contiguous Keys for Array

Page 143: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

higher ! better

138

(*)means smaller is better group average improve rate

sunspider-chrome-sha1 (*) original 1884.7588 26.3%refactored 1299.0706

octane-firefox-Splay original 11331.59 3.5%refactored 12198.65

Sunspider-String-Tagcloud (*) original 9178.76 11.7%refactored 9457.53

octane-firefox-DeltaBlue original 28473.53 1.4%refactored 31154.06

octane-chrome-Box2D original 24569.47 7.5%refactored 24915.00

octane-chrome-RayTrace original 43595.94 12.9%refactored 48140.35

©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 144: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

higher ! better

(*)means smaller is better group average improve rate

octane-chrome-Splay original 10278.59 15.1%refactored 11885.71

octane-chrome-SplayLatency original 20910.24 3.8%refactored 21994.82

sunspider-chrome-3d-Cube (*) original 597.047059 1.1%refactored 593.744118

sunspider-firefox-sha1 (*) original 680.476471 3.3%refactored 669.932353

sunspider-firefox-Xparb (*) original 364.6824 19.7%refactored 357.2235

sunspider-chrome-md5 (*) original 774.3500 24.6%refactored 665.8382

sunspider-chrome-format-tofte (*) original 212.2029 3.4%refactored 200.9000

139©LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 145: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• https://github.com/ksen007/jalangi2analyses

140

InstallDLintandJITProfwithJalangi2

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

npm install

• pip install pyOpenSSL • pip install mitmproxy==0.11.3

Installthemitmproxycertificatemanually(drag-and-drop)

(third-party framework)

Page 146: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• man-in-the-middleproxy• Interactive,SSL-capableproxyforHTTPwith

aconsoleinterface.• Intercepthttpcommunicationbetweenthe

clientandtheserverforinstrumentation.

141

mitmproxyBrowser Server

requestforwarded request

responseforwarded response

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

(third-party framework)

Page 147: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• pip install pyOpenSSL • pip install mitmproxy==0.11.3

142

Installmitmproxy

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 148: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• pip install pyOpenSSL • pip install mitmproxy==0.11.3

143

Installmitmproxy

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

Page 149: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

• Man-in-the-middleProxy• SSLandHTTPSisdesignedagainstMITM• HTTPSHandleshakeerrorduetouncertified

modificationviainstrumentation

144

TheHTTPSProblem

Browser Server

requestforwarded request

response

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

mitmproxy +Jalangi Instrumentation

Page 150: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

145

TheHTTPSProblem

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

mitmproxy +Jalangi Instrumentation

+ a CertificateAuthorityImplementation

Browser Server

requestforwarded request

response

• Man-in-the-middleProxy• SSLandHTTPSisdesignedagainstMITM• HTTPSHandleshakeerrorduetouncertified

modificationviainstrumentation

Page 151: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

146

TheHTTPSProblem

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.

mitmproxy +Jalangi Instrumentation

+ a CertificateAuthorityImplementation

Browser Server

requestforwarded request

response

• Man-in-the-middleProxy• SSLandHTTPSisdesignedagainstMITM• HTTPSHandleshakeerrorduetouncertified

modificationviainstrumentation

Page 152: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

Other Resources

https://github.com/Samsung/jalangi2

Jalangi (v2) Github

https://github.com/ksen007/jalangi2analysesDLint + JITProf Github based on Jalangi (v2)

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.147

Questions

https://github.com/JacksonGL/jitprof-visualizationJITProf Visualization Github based on Jalangi (v2)

Page 153: Jalangi: A Dynamic Analysis Framework for JavaScript · • Assembly Language for the Web: emscripten, coffeescript, TypeScript • A language to implement DSL frameworks – Angular.js,

LiangGong,ElectricEngineering&ComputerScience,UniversityofCalifornia,Berkeley.148