Skip to content

Commit

Permalink
Merge pull request #3 from barbosamaatheus/ioa
Browse files Browse the repository at this point in the history
Ioa
  • Loading branch information
barbosamaatheus authored Nov 24, 2021
2 parents 49a6fe9 + 6b4d7a3 commit 862a818
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 120 deletions.
120 changes: 61 additions & 59 deletions src/main/java/br/unb/cic/analysis/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,23 @@

public class Main {

private static CommandLine cmd;
private Options options;
private AbstractMergeConflictDefinition definition;
private Set<String> targetClasses;
private List<String> conflicts = new ArrayList<>();
private ReachDefinitionAnalysis analysis;



public static void main(String args[]) {
Main m = new Main();
try {
m.createOptions();

CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(m.options, args);
String mode = "dataflow";
cmd = parser.parse(m.options, args);

String mode = "dataflow";

if (cmd.hasOption("mode")) {
mode = cmd.getOptionValue("mode");
}
Expand All @@ -58,26 +57,24 @@ public static void main(String args[]) {
m.loadDefinition(cmd.getOptionValue("csv"));
}
m.runAnalysis(mode, m.parseClassPath(cmd.getOptionValue("cp")));

m.exportResults();

}
catch(ParseException e) {

} catch (ParseException e) {
System.out.println("Error: " + e.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "java Main", m.options );
}
catch(Exception e) {
formatter.printHelp("java Main", m.options);
} catch (Exception e) {
e.printStackTrace();
}
}

private String parseClassPath(String cp) {
File f = new File(cp);
String res = cp;
if(f.exists() && f.isDirectory()) {
for(File file : f.listFiles()) {
if(file.getName().endsWith(".jar")) {
if (f.exists() && f.isDirectory()) {
for (File file : f.listFiles()) {
if (file.getName().endsWith(".jar")) {
res += ":";
res += file.getAbsolutePath();
}
Expand All @@ -87,31 +84,30 @@ private String parseClassPath(String cp) {
}

private void exportResults() throws Exception {
System.out.println(" Analysis results");
System.out.println(" Analysis results");
System.out.println("----------------------------");
if(conflicts.size() == 0) {
System.out.println(" No conflicts detected");
System.out.println("----------------------------");
return;

if (conflicts.size() == 0) {
System.out.println(" No conflicts detected");
System.out.println("----------------------------");
return;
}

System.out.println(" Number of conflicts: " + conflicts.size());
final String out = "out.txt";
final String out = "out.txt";
final FileWriter fw = new FileWriter(out);
conflicts.forEach(c -> {
try {
try {
fw.write(c + "\n\n");
} catch (Exception e) {
System.out.println("error exporting the results " + e.getMessage());
}
catch(Exception e) {
System.out.println("error exporting the results " + e.getMessage());
}
});
});
fw.close();
System.out.println(" Results exported to " + out);
System.out.println("----------------------------");
}

private void createOptions() {
options = new Options();
Option classPathOption = Option.builder("cp").argName("class-path")
Expand All @@ -136,23 +132,29 @@ private void createOptions() {
.hasArg().desc("the commit merge to analysis")
.build();

Option verbose = Option.builder("verbose").argName("verbose").desc("run in the verbose mode").build();
Option recursive = Option.builder("recursive").argName("recursive")
Option verboseOption = Option.builder("verbose").argName("verbose").hasArg().desc("run in the verbose mode").build();

Option recursiveOption = Option.builder("recursive").argName("recursive").hasArg()
.desc("run using the recursive strategy for mapping sources and sinks")
.build();

Option oaDepthLimitOption = Option.builder("oaDepthLimit").argName("oaDepthLimit").hasArg()
.desc("sets the depth limit on accessing methods when performing Overriding Assignment " +
"Interprocedural analysis")
.build();

options.addOption(classPathOption);
options.addOption(inputFileOption);
options.addOption(analysisOption);
options.addOption(repoOption);
options.addOption(commitOption);
options.addOption(verbose);
options.addOption(recursive);
options.addOption(verboseOption);
options.addOption(recursiveOption);
options.addOption(oaDepthLimitOption);
}


private void runAnalysis(String mode, String classpath) {
switch(mode) {
switch (mode) {
case "svfa-interprocedural":
runSparseValueFlowAnalysis(classpath, true);
break;
Expand All @@ -178,7 +180,7 @@ private void runAnalysis(String mode, String classpath) {
runDataFlowAnalysis(classpath, mode);
}
}

private void runPessimisticDataFlowAnalysis(String classpath) {
PackManager.v().getPack("jtp").add(
new Transform("jtp.analysis", new BodyTransformer() {
Expand All @@ -202,17 +204,21 @@ protected void internalTransform(Body body, String s, Map<String, String> map) {
.execute();

}


private void runDataFlowAnalysis(String classpath, String mode) {
PackManager.v().getPack("jtp").add(
new Transform("jtp.analysis", new BodyTransformer() {
@Override
protected void internalTransform(Body body, String phaseName, Map<String, String> options) {
switch(mode) {
case "dataflow" : analysis = new ReachDefinitionAnalysis(body, definition); break;
case "tainted" : analysis = new TaintedAnalysis(body, definition);
case "confluence" : analysis = new ConfluentAnalysis(body, definition); break;
switch (mode) {
case "dataflow":
analysis = new ReachDefinitionAnalysis(body, definition);
break;
case "tainted":
analysis = new TaintedAnalysis(body, definition);
case "confluence":
analysis = new ConfluentAnalysis(body, definition);
break;
case "confluence-tainted":
analysis = new ConfluentTaintedAnalysis(body, definition);
break;
Expand All @@ -237,16 +243,18 @@ protected void internalTransform(Body body, String phaseName, Map<String, String
}

private void runInterproceduralOverrideAssignmentAnalysis(String classpath) {
int depthLimit = Integer.parseInt(cmd.getOptionValue("oaDepthLimit", "10"));

InterproceduralOverrideAssignment interproceduralOverrideAssignment =
new InterproceduralOverrideAssignment(definition);
new InterproceduralOverrideAssignment(definition, depthLimit);

List<String> classes = Collections.singletonList(classpath);
SootWrapper.configureSootOptionsToRunInterproceduralOverrideAssignmentAnalysis(classes);

interproceduralOverrideAssignment.configureEntryPoints();

PackManager.v().getPack("wjtp").add(new Transform("wjtp.analysis", interproceduralOverrideAssignment));
SootWrapper.configurePackagesWithCallGraph().forEach(p -> PackManager.v().getPack(p).apply());
SootWrapper.applyPackages();

conflicts.addAll(interproceduralOverrideAssignment.getConflicts().stream().map(c -> c.toString()).collect(Collectors.toList()));
}
Expand Down Expand Up @@ -289,8 +297,8 @@ private void runSparseValueFlowAnalysis(String classpath, boolean interprocedura

private void runSparseValueFlowConfluenceAnalysis(String classpath, boolean interprocedural) {
definition.setRecursiveMode(options.hasOption("recursive"));
SVFAConfluenceAnalysis analysis = new SVFAConfluenceAnalysis(classpath, this.definition, interprocedural);
SVFAConfluenceAnalysis analysis = new SVFAConfluenceAnalysis(classpath, this.definition, interprocedural);

analysis.execute();
conflicts.addAll(analysis.getConfluentConflicts()
.stream()
Expand All @@ -304,11 +312,10 @@ private void loadDefinition(String filePath) throws Exception {
Map<String, List<Integer>> sourceDefs = new HashMap<>();
Map<String, List<Integer>> sinkDefs = new HashMap<>();
targetClasses = new HashSet<>();
for(ClassChangeDefinition change : changes) {
if(change.getType().equals(Statement.Type.SOURCE)) {
for (ClassChangeDefinition change : changes) {
if (change.getType().equals(Statement.Type.SOURCE)) {
addChange(sourceDefs, change);
}
else {
} else {
addChange(sinkDefs, change);
}
targetClasses.add(change.getClassName());
Expand All @@ -327,10 +334,9 @@ protected Map<String, List<Integer>> sinkDefinitions() {
}

private void addChange(Map<String, List<Integer>> map, ClassChangeDefinition change) {
if(map.containsKey(change.getClassName())) {
if (map.containsKey(change.getClassName())) {
map.get(change.getClassName()).add(change.getLineNumber());
}
else {
} else {
List<Integer> lines = new ArrayList<>();
lines.add(change.getLineNumber());
map.put(change.getClassName(), lines);
Expand Down Expand Up @@ -366,17 +372,13 @@ protected Map<String, List<Integer>> sinkDefinitions() {
}

private void addChangeFromDiffAnalysis(Map<String, List<Integer>> map, Entry<String, Integer> change) {
if(map.containsKey(change.getKey())) {
if (map.containsKey(change.getKey())) {
map.get(change.getKey()).add(change.getValue());
}
else {
} else {
List<Integer> lines = new ArrayList<>();
lines.add(change.getValue());
map.put(change.getKey(), lines);
}
}




}
28 changes: 18 additions & 10 deletions src/main/java/br/unb/cic/analysis/SootWrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package br.unb.cic.analysis;

import soot.PackManager;
import soot.Scene;
import soot.jimple.spark.SparkTransformer;
import soot.jimple.toolkits.callgraph.CHATransformer;
Expand Down Expand Up @@ -40,7 +41,7 @@ public void execute() {
}

private static List<String> getIncludeList() {
//"java.lang.*"
//"java.lang.*, java.util.*"
List<String> stringList = new ArrayList<String>(Arrays.asList("java.util.*")); //java.util.HashMap
return stringList;
}
Expand All @@ -63,16 +64,10 @@ public static void configureSootOptionsToRunInterproceduralOverrideAssignmentAna
Scene.v().loadNecessaryClasses();

enableSparkCallGraph();
//enableCHACallGraph();
}

public static List<String> configurePackagesWithCallGraph() {
List<String> packages = new ArrayList<String>();
packages.add("cg");
packages.add("wjtp");
return packages;
}

public static void enableSparkCallGraph() {
private static void enableSparkCallGraph() {
//Enable Spark
HashMap<String, String> opt = new HashMap<String, String>();
//opt.put("propagator","worklist");
Expand All @@ -86,10 +81,23 @@ public static void enableSparkCallGraph() {
soot.options.Options.v().setPhaseOption("cg.spark", "enable:true");
}

public static void enableCHACallGraph() {
private static void enableCHACallGraph() {
CHATransformer.v().transform();
}

private static List<String> configurePackagesWithCallGraph() {
List<String> packages = new ArrayList<String>();
packages.add("cg");
packages.add("wjtp");
return packages;
}

public static void applyPackages() {
configurePackagesWithCallGraph().forEach(p -> {
PackManager.v().getPack(p).apply();
});
}

public static class Builder {
private String classPath;
private String classes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import soot.jimple.InvokeStmt;
import soot.jimple.StaticFieldRef;

import java.util.*;
import java.util.Objects;

/**
* Information wee keep while traversing
Expand Down Expand Up @@ -99,4 +99,9 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(local, stmt);
}

@Override
public String toString() {
return "DataFlowAbstraction{ " + stmt + "}";
}
}
Loading

0 comments on commit 862a818

Please sign in to comment.