Skip to content

Commit

Permalink
Prune writes to constant instances in the Python ModRef analysis (#104)
Browse files Browse the repository at this point in the history
Fixes #103.
  • Loading branch information
khatchad authored Nov 7, 2023
1 parent 3c81c94 commit f7bf0f4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
9 changes: 9 additions & 0 deletions com.ibm.wala.cast.python.test/data/kw_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def g(p1, p2):
assert p1 == 5 and p2 == 2


def f():
g(5, p2=2)


f()
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@

public class TestPythonModRefAnalysis extends TestPythonCallGraphShape {

@Test
public void testComputeModCallGraphPointerAnalysisOfT()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
PythonAnalysisEngine<?> engine = makeEngine("globals.py");
private OrdinalSet<PointerKey> getModSet(String filename)
throws ClassHierarchyException, CancelException, IOException {
PythonAnalysisEngine<?> engine = makeEngine(filename);
SSAPropagationCallGraphBuilder builder =
(SSAPropagationCallGraphBuilder) engine.defaultCallGraphBuilder();
CallGraph CG = builder.makeCallGraph(builder.getOptions());

Collection<CGNode> nodes = getNodes(CG, "script globals.py/f");
Collection<CGNode> nodes = getNodes(CG, "script " + filename + "/f");
assertEquals(1, nodes.size());

assertTrue(nodes.iterator().hasNext());
Expand All @@ -43,6 +42,13 @@ public void testComputeModCallGraphPointerAnalysisOfT()

// what heap locations does f() (transitively) modify?
OrdinalSet<PointerKey> modSet = mod.get(fNode);
return modSet;
}

@Test
public void testGlobalMods()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
OrdinalSet<PointerKey> modSet = getModSet("globals.py");

// should only modify the global.
assertEquals(1, modSet.size());
Expand All @@ -57,4 +63,11 @@ public void testComputeModCallGraphPointerAnalysisOfT()
Atom name = field.getName();
assertEquals(Atom.findOrCreateAsciiAtom("global a"), name);
}

@Test
public void testKeywordArgs()
throws ClassHierarchyException, IllegalArgumentException, CancelException, IOException {
OrdinalSet<PointerKey> modSet = getModSet("kw_args.py");
assertEquals("Should be no modifications to heap locations in this file.", 0, modSet.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import com.ibm.wala.cast.ipa.callgraph.AstHeapModel;
import com.ibm.wala.cast.ipa.modref.AstModRef;
import com.ibm.wala.cast.ir.ssa.AstGlobalWrite;
import com.ibm.wala.cast.ir.ssa.AstPropertyWrite;
import com.ibm.wala.cast.python.ssa.PythonInstructionVisitor;
import com.ibm.wala.classLoader.IField;
import com.ibm.wala.core.util.strings.Atom;
import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.ipa.callgraph.propagation.ConstantKey;
import com.ibm.wala.ipa.callgraph.propagation.InstanceFieldPointerKey;
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
import com.ibm.wala.ipa.callgraph.propagation.PointerAnalysis;
import com.ibm.wala.ipa.callgraph.propagation.PointerKey;
import com.ibm.wala.ipa.callgraph.propagation.StaticFieldKey;
import com.ibm.wala.ipa.modref.ExtendedHeapModel;
import java.util.Collection;
import java.util.Iterator;

public class PythonModRef extends AstModRef<InstanceKey> {

Expand Down Expand Up @@ -62,6 +66,23 @@ public void visitAstGlobalWrite(AstGlobalWrite instruction) {
}
}
}

@Override
public void visitPropertyWrite(AstPropertyWrite instruction) {
super.visitPropertyWrite(instruction);

// prune "writes" to ConstantKeys. See https://github.com/wala/ML/issues/103.
for (Iterator<PointerKey> it = this.result.iterator(); it.hasNext(); ) {
PointerKey pointerKey = it.next();

if (pointerKey instanceof InstanceFieldPointerKey) {
InstanceFieldPointerKey instanceFieldPointerKey = (InstanceFieldPointerKey) pointerKey;
InstanceKey instanceKey = instanceFieldPointerKey.getInstanceKey();

if (instanceKey instanceof ConstantKey) it.remove();
}
}
}
}

@Override
Expand Down

0 comments on commit f7bf0f4

Please sign in to comment.