Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle global writes in the Python ModRef analysis #101

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions com.ibm.wala.cast.python.test/data/globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
a = 10


def f():
global a
a = a + 1


assert a == 10
f()
assert a == 11
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.ibm.wala.cast.python.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.ibm.wala.cast.python.client.PythonAnalysisEngine;
import com.ibm.wala.cast.python.modref.PythonModRef;
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.CallGraph;
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
import com.ibm.wala.ipa.callgraph.propagation.PointerKey;
import com.ibm.wala.ipa.callgraph.propagation.SSAPropagationCallGraphBuilder;
import com.ibm.wala.ipa.callgraph.propagation.StaticFieldKey;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.modref.ModRef;
import com.ibm.wala.util.CancelException;
import com.ibm.wala.util.intset.OrdinalSet;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import org.junit.Test;

public class TestPythonModRefAnalysis extends TestPythonCallGraphShape {

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

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

assertTrue(nodes.iterator().hasNext());
CGNode fNode = nodes.iterator().next();

ModRef<InstanceKey> modRef = new PythonModRef();
Map<CGNode, OrdinalSet<PointerKey>> mod = modRef.computeMod(CG, builder.getPointerAnalysis());

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

// should only modify the global.
assertEquals(1, modSet.size());

assertTrue(modSet.iterator().hasNext());
PointerKey pointerKey = modSet.iterator().next();

assertTrue(pointerKey instanceof StaticFieldKey);
StaticFieldKey staticFieldKey = (StaticFieldKey) pointerKey;

IField field = staticFieldKey.getField();
Atom name = field.getName();
assertEquals(Atom.findOrCreateAsciiAtom("global a"), name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

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.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.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;

Expand Down Expand Up @@ -41,6 +45,23 @@ public PythonModVisitor(
boolean ignoreAllocHeapDefs) {
super(n, result, (AstHeapModel) h, pa);
}

@Override
public void visitAstGlobalWrite(AstGlobalWrite instruction) {
super.visitAstGlobalWrite(instruction);
String globalName = instruction.getGlobalName();

// find the pointer key corresponding to this global.
for (PointerKey pk : this.pa.getPointerKeys()) {
if (pk instanceof StaticFieldKey) {
StaticFieldKey staticFieldKey = (StaticFieldKey) pk;
IField field = staticFieldKey.getField();
Atom fieldName = field.getName();
if (fieldName.toString().equals(globalName))
this.result.add(this.h.getPointerKeyForStaticField(field));
}
}
}
}

@Override
Expand Down