Skip to content

Commit

Permalink
Merge pull request #2038 from MarcMil/nop-fix
Browse files Browse the repository at this point in the history
Support parameter mappings in virtual edges
  • Loading branch information
StevenArzt authored Jan 10, 2024
2 parents fa42b89 + ac33e96 commit e088394
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import soot.Body;
import soot.Kind;
import soot.MethodSubSignature;
import soot.ModuleUtil;
import soot.RefType;
import soot.Scene;
import soot.Value;
import soot.jimple.InstanceInvokeExpr;
import soot.jimple.InvokeExpr;
import soot.jimple.Stmt;
import soot.options.Options;
import soot.util.StringNumberer;
Expand Down Expand Up @@ -335,18 +339,30 @@ private static List<VirtualEdgeTarget> parseEdgeTargets(Element targetsElement)

String tpos = targetElement.getAttribute("target-position");
RefType type = getDeclaringClassType(targetElement);
DirectTarget dt;
switch (tpos) {
case "argument":
int argIdx = Integer.valueOf(targetElement.getAttribute("index"));
targets.add(new DirectTarget(type, subsignature, argIdx));
dt = new DirectTarget(type, subsignature, argIdx);
break;
case "base":
targets.add(new DirectTarget(type, subsignature));
dt = new DirectTarget(type, subsignature);
break;
default:
throw new IllegalArgumentException("Unsupported target position " + tpos);

}
targets.add(dt);
NodeList cd = targetElement.getChildNodes();
for (int x = 0; x < cd.getLength(); x++) {
Node ce = cd.item(x);
if (ce instanceof Element) {
Element cee = (Element) ce;
if (cee.getTagName().equals("parameterMappings")) {
parseParameterMappings(dt, cee);
}
}
}
break;
}
case "indirect": {
Expand Down Expand Up @@ -384,6 +400,25 @@ private static List<VirtualEdgeTarget> parseEdgeTargets(Element targetsElement)
return targets;
}

private static void parseParameterMappings(DirectTarget dt, Element cee) {
NodeList cn = cee.getChildNodes();
for (int i = 0; i < cn.getLength(); i++) {
Node d = cn.item(i);
if (d instanceof Element) {
Element e = (Element) d;
switch (e.getTagName()) {
case "direct":
int sourceIdx = Integer.parseInt(e.getAttribute("sourceIdx"));
int targetIdx = Integer.parseInt(e.getAttribute("targetIdx"));
dt.parameterMappings.add(new DirectParameterMapping(sourceIdx, targetIdx));
break;
default:
throw new RuntimeException("Not supported: " + e.getTagName());
}
}
}
}

public static abstract class VirtualEdgeSource {
}

Expand Down Expand Up @@ -558,7 +593,7 @@ public boolean isBase() {
public int getArgIndex() {
return argIndex;
}

public void setArgIndex(int value) {
this.argIndex = value;
}
Expand Down Expand Up @@ -611,6 +646,7 @@ public boolean equals(Object obj) {
}

public static class DirectTarget extends VirtualEdgeTarget {
private List<AbstractParameterMapping> parameterMappings = new ArrayList<>();

DirectTarget() {
// internal use only
Expand Down Expand Up @@ -673,6 +709,53 @@ public boolean equals(Object obj) {
}
return true;
}

public List<AbstractParameterMapping> getParameterMappings() {
return parameterMappings;
}
}

public static abstract class AbstractParameterMapping {
public abstract Value getMappedSourceArgumentArg(InvokeExpr expr);

public abstract Value getMappedTargetArgumentArg(Body body);
}

public static class DirectParameterMapping extends AbstractParameterMapping {
private int sourceIndex, targetIndex;

public DirectParameterMapping(int src, int tgt) {
this.sourceIndex = src;
this.targetIndex = tgt;
}

public int getSourceIndex() {
return sourceIndex;
}

public int getTargetIndex() {
return targetIndex;
}

@Override
public Value getMappedSourceArgumentArg(InvokeExpr expr) {
return getValueByIndex(expr, sourceIndex);
}

@Override
public Value getMappedTargetArgumentArg(Body body) {
if (targetIndex == -1) {
return body.getThisLocal();
}
return body.getParameterLocal(targetIndex);
}
}

private static Value getValueByIndex(InvokeExpr expr, int idx) {
if (idx == BASE_INDEX) {
return ((InstanceInvokeExpr) expr).getBase();
}
return expr.getArg(idx);
}

public static class IndirectTarget extends VirtualEdgeTarget {
Expand Down
18 changes: 15 additions & 3 deletions src/main/resources/virtualedges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@
<targets>
<direct
subsignature="java.lang.Object doInBackground(java.lang.Object[])"
target-position="base" />
target-position="base">
<parameterMappings>
<direct sourceIdx="0" targetIdx="0" />
</parameterMappings>
</direct>
</targets>
</edge>
<edge>
Expand All @@ -178,9 +182,17 @@
target-position="base" />
<direct
subsignature="void onProgressUpdate(java.lang.Object[])"
target-position="base" />
target-position="base">
<parameterMappings>
<direct sourceIdx="0" targetIdx="0" />
</parameterMappings>
</direct>
<direct subsignature="void onPostExecute(java.lang.Object)"
target-position="base" />
target-position="base">
<parameterMappings>
<direct sourceIdx="0" targetIdx="0" />
</parameterMappings>
</direct>
</targets>
</edge>
<!-- doPrivileged -->
Expand Down

0 comments on commit e088394

Please sign in to comment.