From b8952cb9e2a9ecc3ddddc2b67fa0a1c47b1fb8a0 Mon Sep 17 00:00:00 2001 From: "M.Schmidt" Date: Thu, 26 Sep 2024 15:39:02 +0200 Subject: [PATCH 1/2] streamify JimpleICFG --- .../icfg/AbstractJimpleBasedICFG.java | 90 +++++++++---------- 1 file changed, 41 insertions(+), 49 deletions(-) diff --git a/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java b/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java index 753140915f7..c8e7e459a4d 100644 --- a/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java +++ b/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java @@ -28,6 +28,7 @@ import heros.SynchronizedBy; import heros.solver.IDESolver; import java.util.*; +import java.util.stream.Collectors; import javax.annotation.Nonnull; import sootup.core.graph.StmtGraph; import sootup.core.jimple.basic.Value; @@ -83,7 +84,7 @@ protected AbstractJimpleBasedICFG() { } protected Map createStmtToOwnerMap() { - return new LinkedHashMap<>(); + return new IdentityHashMap<>(); } protected AbstractJimpleBasedICFG(boolean enableExceptions) { @@ -91,14 +92,18 @@ protected AbstractJimpleBasedICFG(boolean enableExceptions) { } public Body getBodyOf(Stmt stmt) { - assert stmtToOwner.containsKey(stmt) : "Statement " + stmt + " not in Stmt-to-owner mapping"; - return stmtToOwner.get(stmt); + Body body = stmtToOwner.get(stmt); + assert body != null : "Statement " + stmt + " not in Stmt-to-owner mapping"; + return body; } @Override public SootMethod getMethodOf(Stmt stmt) { Body b = getBodyOf(stmt); - return b == null ? null : view.getMethod(b.getMethodSignature()).orElse(null); + if (b == null) { + return null; + } + return view.getMethod(b.getMethodSignature()).orElse(null); } @Override @@ -107,8 +112,8 @@ public List getSuccsOf(Stmt stmt) { if (body == null) { return Collections.emptyList(); } - StmtGraph unitGraph = getOrCreateStmtGraph(body); - return unitGraph.successors(stmt); + StmtGraph stmtGraph = getOrCreateStmtGraph(body); + return stmtGraph.successors(stmt); } @Override @@ -125,30 +130,23 @@ protected StmtGraph makeGraph(Body body) { } protected Set getCallsFromWithinMethod(SootMethod method) { - Set res = null; - for (Stmt u : method.getBody().getStmts()) { - if (isCallStmt(u)) { - if (res == null) { - res = new LinkedHashSet<>(); - } - res.add(u); - } - } - return res == null ? Collections.emptySet() : res; + return method.getBody().getStmts().stream() + .filter(this::isCallStmt) + .collect(Collectors.toSet()); } @Override public boolean isExitStmt(Stmt stmt) { Body body = getBodyOf(stmt); - StmtGraph unitGraph = getOrCreateStmtGraph(body); - return unitGraph.getTails().contains(stmt); + StmtGraph cfg = getOrCreateStmtGraph(body); + return cfg.getTails().contains(stmt); } @Override public boolean isStartPoint(Stmt stmt) { Body body = getBodyOf(stmt); - StmtGraph unitGraph = getOrCreateStmtGraph(body); - return unitGraph.getEntrypoints().contains(stmt); + StmtGraph cfg = getOrCreateStmtGraph(body); + return cfg.getEntrypoints().contains(stmt); } @Override @@ -174,12 +172,12 @@ public List getParameterRefs(SootMethod m) { @Override public Collection getStartPointsOf(SootMethod m) { - if (m.hasBody()) { - Body body = m.getBody(); - StmtGraph unitGraph = getOrCreateStmtGraph(body); - return unitGraph.getEntrypoints(); + if (!m.hasBody()) { + return Collections.emptySet(); } - return Collections.emptySet(); + Body body = m.getBody(); + StmtGraph cfg = getOrCreateStmtGraph(body); + return cfg.getEntrypoints(); } public boolean setOwnerStatement(Stmt u, Body b) { @@ -193,16 +191,16 @@ public boolean isCallStmt(Stmt stmt) { @Override public Set allNonCallStartNodes() { - Set res = new LinkedHashSet<>(stmtToOwner.keySet()); - res.removeIf(u -> isStartPoint(u) || isCallStmt(u)); - return res; + return stmtToOwner.keySet().stream() + .filter(u -> !(isStartPoint(u) || isCallStmt(u))) + .collect(Collectors.toSet()); } @Override public Set allNonCallEndNodes() { - Set res = new LinkedHashSet<>(stmtToOwner.keySet()); - res.removeIf(u -> isExitStmt(u) || isCallStmt(u)); - return res; + return stmtToOwner.keySet().stream() + .filter(u -> !(isExitStmt(u) || isCallStmt(u))) + .collect(Collectors.toSet()); } @Override @@ -216,12 +214,11 @@ public Set getCallsFromWithin(SootMethod m) { } public void initializeStmtToOwner(SootMethod m) { - if (m.hasBody()) { - Body b = m.getBody(); - for (Stmt node : b.getStmtGraph().getNodes()) { - stmtToOwner.put(node, b); - } + if (!m.hasBody()) { + return; } + Body b = m.getBody(); + b.getStmtGraph().getNodes().forEach(node -> stmtToOwner.put(node, b)); } @Override @@ -231,18 +228,18 @@ public List getPredsOf(Stmt u) { if (body == null) { return Collections.emptyList(); } - StmtGraph unitGraph = getOrCreateStmtGraph(body); - return unitGraph.predecessors(u); + StmtGraph cfg = getOrCreateStmtGraph(body); + return cfg.predecessors(u); } @Override public Collection getEndPointsOf(SootMethod m) { - if (m.hasBody()) { - Body body = m.getBody(); - StmtGraph unitGraph = getOrCreateStmtGraph(body); - return unitGraph.getTails(); + if (!m.hasBody()) { + return Collections.emptySet(); } - return Collections.emptySet(); + Body body = m.getBody(); + StmtGraph cfg = getOrCreateStmtGraph(body); + return cfg.getTails(); } @Override @@ -252,12 +249,7 @@ public List getPredsOfCallAt(Stmt u) { @Override public boolean isReturnSite(Stmt n) { - for (Stmt pred : getPredsOf(n)) { - if (isCallStmt(pred)) { - return true; - } - } - return false; + return getPredsOf(n).stream().anyMatch(this::isCallStmt); } @Override From fc4b606def19ebc84b870a98f0e672615eb7c743 Mon Sep 17 00:00:00 2001 From: "M.Schmidt" Date: Thu, 24 Oct 2024 11:44:15 +0200 Subject: [PATCH 2/2] unify naming --- .../icfg/AbstractJimpleBasedICFG.java | 20 +++++++++---------- .../ScopedAnalysisInputLocation.java | 2 ++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java b/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java index c8e7e459a4d..2ebb42eb758 100644 --- a/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java +++ b/sootup.analysis.interprocedural/src/main/java/sootup/analysis/interprocedural/icfg/AbstractJimpleBasedICFG.java @@ -138,15 +138,15 @@ protected Set getCallsFromWithinMethod(SootMethod method) { @Override public boolean isExitStmt(Stmt stmt) { Body body = getBodyOf(stmt); - StmtGraph cfg = getOrCreateStmtGraph(body); - return cfg.getTails().contains(stmt); + StmtGraph stmtGraph = getOrCreateStmtGraph(body); + return stmtGraph.getTails().contains(stmt); } @Override public boolean isStartPoint(Stmt stmt) { Body body = getBodyOf(stmt); - StmtGraph cfg = getOrCreateStmtGraph(body); - return cfg.getEntrypoints().contains(stmt); + StmtGraph stmtGraph = getOrCreateStmtGraph(body); + return stmtGraph.getEntrypoints().contains(stmt); } @Override @@ -176,8 +176,8 @@ public Collection getStartPointsOf(SootMethod m) { return Collections.emptySet(); } Body body = m.getBody(); - StmtGraph cfg = getOrCreateStmtGraph(body); - return cfg.getEntrypoints(); + StmtGraph stmtGraph = getOrCreateStmtGraph(body); + return stmtGraph.getEntrypoints(); } public boolean setOwnerStatement(Stmt u, Body b) { @@ -228,8 +228,8 @@ public List getPredsOf(Stmt u) { if (body == null) { return Collections.emptyList(); } - StmtGraph cfg = getOrCreateStmtGraph(body); - return cfg.predecessors(u); + StmtGraph stmtGraph = getOrCreateStmtGraph(body); + return stmtGraph.predecessors(u); } @Override @@ -238,8 +238,8 @@ public Collection getEndPointsOf(SootMethod m) { return Collections.emptySet(); } Body body = m.getBody(); - StmtGraph cfg = getOrCreateStmtGraph(body); - return cfg.getTails(); + StmtGraph stmtGraph = getOrCreateStmtGraph(body); + return stmtGraph.getTails(); } @Override diff --git a/sootup.core/src/main/java/sootup/core/inputlocation/ScopedAnalysisInputLocation.java b/sootup.core/src/main/java/sootup/core/inputlocation/ScopedAnalysisInputLocation.java index 0c900fca57d..ddf7cf13799 100644 --- a/sootup.core/src/main/java/sootup/core/inputlocation/ScopedAnalysisInputLocation.java +++ b/sootup.core/src/main/java/sootup/core/inputlocation/ScopedAnalysisInputLocation.java @@ -61,6 +61,8 @@ public Optional getClassSource( @Nonnull @Override public Collection getClassSources(@Nonnull View view) { + // possibility to streamify this method to apply the filter at earlier stage i.e. before + // creating the ClassSources would be a faster approach.. return inputLocation.getClassSources(view).stream() .filter(type -> filter(type.getClassType())) .collect(Collectors.toList());