diff --git a/com.ibm.wala.cast.js.test/examples-src/tests/prototype_contamination_bug.js b/com.ibm.wala.cast.js.test/examples-src/tests/prototype_contamination_bug.js new file mode 100644 index 000000000..3115c8f03 --- /dev/null +++ b/com.ibm.wala.cast.js.test/examples-src/tests/prototype_contamination_bug.js @@ -0,0 +1,28 @@ +function A(){ + +} +A.prototype.foo = function foo_of_A(){ + console.log("foo_of_A"); +} + +function B(){ + +} +B.prototype.foo = function foo_of_B(){ + console.log("foo_of_B"); +} + +function test1(){ + var a = new A + console.log("calling foo_of_A"); + a.foo() +} + +function test2(){ + var b = new B + console.log("calling foo_of_B"); + b.foo() +} + +test1() +test2() \ No newline at end of file diff --git a/com.ibm.wala.cast.js.test/harness-src/com/ibm/wala/cast/js/test/TestSimpleCallGraphShape.java b/com.ibm.wala.cast.js.test/harness-src/com/ibm/wala/cast/js/test/TestSimpleCallGraphShape.java index 20eb3bdb4..eb5368119 100644 --- a/com.ibm.wala.cast.js.test/harness-src/com/ibm/wala/cast/js/test/TestSimpleCallGraphShape.java +++ b/com.ibm.wala.cast.js.test/harness-src/com/ibm/wala/cast/js/test/TestSimpleCallGraphShape.java @@ -243,4 +243,21 @@ public abstract class TestSimpleCallGraphShape extends TestJSCallGraphShape { verifyGraphAssertions(CG, assertionsForMultivar); } + private static final Object[][] assertionsForPrototypeContamination = new Object[][] { + new Object[] { ROOT, new String[] { "tests/prototype_contamination_bug.js" } }, + new Object[] { "suffix:test1", + new String[] { "suffix:foo_of_A"} }, + new Object[] { "suffix:test2", + new String[] { "suffix:foo_of_B"} } + }; + + @Test public void testProtoypeContamination() throws IOException, IllegalArgumentException, CancelException { + CallGraph CG = Util.makeScriptCG("tests", "prototype_contamination_bug.js"); + verifyGraphAssertions(CG, assertionsForPrototypeContamination); + verifyNoEdges(CG, "suffix:test1", "suffix:foo_of_B"); + verifyNoEdges(CG, "suffix:test2", "suffix:foo_of_A"); + + } + + } diff --git a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/JavaScriptConstructTargetSelector.java b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/JavaScriptConstructTargetSelector.java index d69d22832..0227b2022 100644 --- a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/JavaScriptConstructTargetSelector.java +++ b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/ipa/callgraph/JavaScriptConstructTargetSelector.java @@ -349,11 +349,11 @@ public class JavaScriptConstructTargetSelector implements MethodTargetSelector { S.addStatement(insts.NewInstruction(5, NewSiteReference.make(S.getNextProgramCounter(), cls.getReference()))); - S.addStatement(insts.PutInstruction(5, 4, "prototype")); - S.getNextProgramCounter(); - S.addStatement(insts.NewInstruction(7, NewSiteReference.make(S.getNextProgramCounter(), JavaScriptTypes.Object))); + S.addStatement(insts.PutInstruction(7, 4, "prototype")); + S.getNextProgramCounter(); + S.addStatement(insts.PutInstruction(5, 7, "prototype")); S.getNextProgramCounter(); diff --git a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/translator/PropertyReadExpander.java b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/translator/PropertyReadExpander.java index 6bb9cbc7c..f32ef4100 100644 --- a/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/translator/PropertyReadExpander.java +++ b/com.ibm.wala.cast.js/source/com/ibm/wala/cast/js/translator/PropertyReadExpander.java @@ -81,17 +81,36 @@ public class PropertyReadExpander extends CAstRewriter i = CG.getSuccNodes((CGNode) source); i.hasNext();) { + if (i.next().equals(dest)) { + Assert.fail("Found a link from " + source + " to " + dest); + } + } + } + } + } + protected static final Object ROOT = new Object(); protected abstract Collection getNodes(CallGraph CG, String functionIdentifier);