From 4b668dda41611cfe1ffd896641ce25b3a5d6209f Mon Sep 17 00:00:00 2001 From: Marc Miltenberger Date: Wed, 10 Jan 2024 13:31:05 +0100 Subject: [PATCH 1/2] Support parameter mappings in virtual edges --- .../callgraph/VirtualEdgesSummaries.java | 89 ++++++++++++++++++- src/main/resources/virtualedges.xml | 18 +++- 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java b/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java index 2185efc66c8..072a76fcc04 100644 --- a/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java +++ b/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java @@ -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; @@ -335,18 +339,30 @@ private static List 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": { @@ -384,6 +400,25 @@ private static List 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 { } @@ -611,7 +646,9 @@ public boolean equals(Object obj) { } public static class DirectTarget extends VirtualEdgeTarget { - + private List parameterMappings = new ArrayList<>(); + + DirectTarget() { // internal use only } @@ -673,6 +710,52 @@ public boolean equals(Object obj) { } return true; } + + public List 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 { diff --git a/src/main/resources/virtualedges.xml b/src/main/resources/virtualedges.xml index c2b71e1ea01..596531779c8 100644 --- a/src/main/resources/virtualedges.xml +++ b/src/main/resources/virtualedges.xml @@ -167,7 +167,11 @@ + target-position="base"> + + + + @@ -178,9 +182,17 @@ target-position="base" /> + target-position="base"> + + + + + target-position="base"> + + + + From ac33e96b005c782b32a222205bb9d0f90aaabf39 Mon Sep 17 00:00:00 2001 From: Marc Miltenberger Date: Wed, 10 Jan 2024 13:34:34 +0100 Subject: [PATCH 2/2] Formatting --- .../callgraph/VirtualEdgesSummaries.java | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java b/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java index 072a76fcc04..3e5d90da39b 100644 --- a/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java +++ b/src/main/java/soot/jimple/toolkits/callgraph/VirtualEdgesSummaries.java @@ -593,7 +593,7 @@ public boolean isBase() { public int getArgIndex() { return argIndex; } - + public void setArgIndex(int value) { this.argIndex = value; } @@ -647,8 +647,7 @@ public boolean equals(Object obj) { public static class DirectTarget extends VirtualEdgeTarget { private List parameterMappings = new ArrayList<>(); - - + DirectTarget() { // internal use only } @@ -715,45 +714,46 @@ public List 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; - } + private int sourceIndex, targetIndex; - @Override - public Value getMappedSourceArgumentArg(InvokeExpr expr) { - return getValueByIndex(expr, sourceIndex); - } + public DirectParameterMapping(int src, int tgt) { + this.sourceIndex = src; + this.targetIndex = tgt; + } - @Override - public Value getMappedTargetArgumentArg(Body body) { - if (targetIndex == -1) { - return body.getThisLocal(); - } - return body.getParameterLocal(targetIndex); + 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 ((InstanceInvokeExpr) expr).getBase(); } return expr.getArg(idx); }