Skip to content

Commit

Permalink
fix DoS calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
kadirayk committed Oct 26, 2022
1 parent fbb46d4 commit 4cfb696
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class SparseAliasEval {
private final SparseCFGCache.SparsificationStrategy sparsificationStrategy;
private long sparseCFGBuildTime=0;
private long totalAliasQueryTime=0;
private long aliasQueryCount = 0;
private long aliasQueryCount = 0; // issued by the client
private long scfgBuildCount = 0; // client queries + internal queries that lead to SCFG construction, i.e. not retrieved from cache
private InfoflowPerformanceData performanceData;
private float initalStmtCount = 0;
private float finalStmtCount = 0;
Expand All @@ -42,8 +43,11 @@ private void handleSparsificationSpecificData() {
List<SparseCFGQueryLog> queryLogs = cache.getQueryLogs();
for (SparseCFGQueryLog queryLog : queryLogs) {
sparseCFGBuildTime += queryLog.getDuration().toMillis();
initalStmtCount += queryLog.getInitialStmtCount();
finalStmtCount += queryLog.getFinalStmtCount();
if(queryLog.getInitialStmtCount()>0 && queryLog.getFinalStmtCount()>0){
initalStmtCount += queryLog.getInitialStmtCount();
finalStmtCount += queryLog.getFinalStmtCount();
scfgBuildCount++;
}
}
}
}
Expand Down Expand Up @@ -81,9 +85,11 @@ public void generate() {
str.append(",");
str.append("qCount");
str.append(",");
str.append("degree");
str.append("DoS");
str.append(",");
str.append("src");
str.append(",");
str.append("tqCount");
str.append(System.lineSeparator());
writer.write(str.toString());
} catch (IOException e) {
Expand All @@ -109,6 +115,8 @@ public void generate() {
str.append(degreeOfSparsification());
str.append(",");
str.append(performanceData.getSourceCount());
str.append(",");
str.append(scfgBuildCount);
str.append(System.lineSeparator());
writer.write(str.toString());
} catch (IOException e) {
Expand All @@ -118,7 +126,7 @@ public void generate() {

private String degreeOfSparsification(){
if(finalStmtCount!=0){
return String.format("%.2f",initalStmtCount/finalStmtCount);
return String.format("%.2f",(initalStmtCount-finalStmtCount)/initalStmtCount);
}
return "0";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import soot.*;
import soot.jimple.Stmt;
import wpds.impl.Weight;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

public class SparseAliasManager {

Expand All @@ -38,7 +35,8 @@ public class SparseAliasManager {

private boolean disableAliasing = false;
private SparseCFGCache.SparsificationStrategy sparsificationStrategy;
private Map<String, Set<AccessPath>> queryMap = new HashMap<>(); //new ImmutableSortedMap.Builder<String, Set<AccessPath>>(Ordering.natural()).build();
private boolean useQueryCache = true;
//private Map<String, Set<AccessPath>> queryMap = new HashMap<>(); //new ImmutableSortedMap.Builder<String, Set<AccessPath>>(Ordering.natural()).build();
private Map<String, Integer> queryCount = new HashMap<>(); // new ImmutableSortedMap.Builder<String, Integer>(Ordering.natural()).build();


Expand Down Expand Up @@ -101,7 +99,9 @@ private SparseAliasManager(SparseCFGCache.SparsificationStrategy sparsificationS
totalAliasingDuration = Duration.ZERO;
sootCallGraph = new SootCallGraph();
dataFlowScope = SootDataFlowScope.make(Scene.v());
setupQueryCache();
if(this.useQueryCache){
setupQueryCache();
}
}

public Duration getTotalDuration() {
Expand All @@ -124,19 +124,7 @@ private void setupQueryCache() {
public Set<AccessPath> load(BackwardQuery query) throws Exception {
Set<AccessPath> aliases = queryCache.getIfPresent(query);
if (aliases == null) {
// TODO: stabilize null pointer exception that happens sometimes in boomerang
boomerangSolver =
new Boomerang(
sootCallGraph, dataFlowScope, new FlowDroidBoomerangOptions(INSTANCE.sparsificationStrategy));
BackwardBoomerangResults<Weight.NoWeight> results = boomerangSolver.solve(query);
aliases = results.getAllAliases();
boolean debug = false;
if (debug) {
System.out.println(query);
System.out.println("alloc:" + results.getAllocationSites());
System.out.println("aliases:" + aliases);
}//boomerangSolver.unregisterAllListeners();
//boomerangSolver.unregisterAllListeners();
aliases = doBoomerangQuery(query);
queryCache.put(query, aliases);
}
return aliases;
Expand All @@ -162,15 +150,17 @@ public synchronized Set<AccessPath> getAliases(Stmt stmt, SootMethod method, Val
Set<AccessPath> aliases = getAliases(query);
Duration elapsed = stopwatch.elapsed();
totalAliasingDuration = totalAliasingDuration.plus(elapsed);
String queryKey = value.toString() + "-" + stmt.toString() + "-" + method.getSignature().toString();
queryMap.put(queryKey, aliases);
return aliases;
}

private void countQuery(BackwardQuery query) {
String queryKey = query.toString();
if(!queryCount.containsKey(queryKey)){
queryCount.put(queryKey, 1);
}else{
Integer count = queryCount.get(queryKey);
queryCount.put(queryKey, count+1);
}
return aliases;
}

public long getQueryCount(){
Expand All @@ -192,13 +182,26 @@ private BackwardQuery createQuery(Stmt stmt, SootMethod method, Value value) {
throw new RuntimeException("No successors for: " + statement);
}

private Set<AccessPath> doBoomerangQuery(BackwardQuery query){
countQuery(query);
boomerangSolver =
new Boomerang(
sootCallGraph, dataFlowScope, new FlowDroidBoomerangOptions(INSTANCE.sparsificationStrategy));
BackwardBoomerangResults<Weight.NoWeight> results = boomerangSolver.solve(query);
return results.getAllAliases();
}

private Set<AccessPath> getAliases(BackwardQuery query) {
try {
return queryCache.get(query);
} catch (ExecutionException e) {
e.printStackTrace();
if(useQueryCache){
try {
return queryCache.get(query);
} catch (ExecutionException e) {
e.printStackTrace();
}
return Collections.emptySet();
}else{
return doBoomerangQuery(query);
}
return Collections.emptySet();
}

}

0 comments on commit 4cfb696

Please sign in to comment.