Skip to content

Commit

Permalink
Merge pull request #7 from TaintBench/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
linghuiluo authored Jul 16, 2021
2 parents b0bbb1d + c416369 commit fd0cb6f
Show file tree
Hide file tree
Showing 16 changed files with 801 additions and 255 deletions.
12 changes: 12 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
===============================================================================
InferIDE Release License (MIT license)
===============================================================================
MIT License

Copyright (c) 2020 - 2023 Linghui Luo

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.upb.swt</groupId>
<artifactId>TB-Viewer</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>
<packaging>jar</packaging>

<name>TB-Viewer</name>
Expand Down
232 changes: 232 additions & 0 deletions src/main/java/de/upb/swt/tbviewer/FlowDroidResultParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
package de.upb.swt.tbviewer;

import com.ibm.wala.util.collections.Pair;
import de.foellix.aql.datastructure.Reference;
import de.foellix.aql.datastructure.Statement;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class FlowDroidResultParser {

class Tags {

public static final String root = "DataFlowResults";

public static final String results = "Results";
public static final String result = "Result";

public static final String performanceData = "PerformanceData";
public static final String performanceEntry = "PerformanceEntry";

public static final String sink = "Sink";
public static final String accessPath = "AccessPath";

public static final String fields = "Fields";
public static final String field = "Field";

public static final String sources = "Sources";
public static final String source = "Source";

public static final String taintPath = "TaintPath";
public static final String pathElement = "PathElement";
}

class Attributes {

public static final String fileFormatVersion = "FileFormatVersion";
public static final String terminationState = "TerminationState";
public static final String statement = "Statement";
public static final String method = "Method";
public static final String linenumber = "LineNumber";

public static final String value = "Value";
public static final String type = "Type";
public static final String taintSubFields = "TaintSubFields";

public static final String category = "Category";

public static final String name = "Name";
public static final String id = "ID";
}

public static boolean hasFormat(String fileName)
throws XMLStreamException, FileNotFoundException, FactoryConfigurationError {

XMLStreamReader reader =
XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(fileName));
while (reader.hasNext()) {
reader.next();
if (reader.hasName() && reader.isStartElement()) {
if (reader.getLocalName().equals(Tags.root)) {
return true;
}
}
}
return false;
}

public static Set<TaintFlow> readResultsWithPath(String fileName)
throws XMLStreamException, IOException {
XMLStreamReader reader =
XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(fileName));
Set<TaintFlow> results = new HashSet<>();
boolean hasFormat = false;
Location sink = null;
Location source = null;
ArrayList<Location> pathElements = new ArrayList<>();
String sinkStatement = null;
String sinkMethod = null;
int sinkLineNo = -1;
String sourceStatement = null;
String sourceMethod = null;
int sourceLineNo = -1;
String pathStatement = null;
String pathMethod = null;
int pathLineNo = -1;
String ID = "";
while (reader.hasNext()) {
reader.next();
if (reader.hasName()) {
if (reader.getLocalName().equals(Tags.root) && reader.isStartElement()) {
hasFormat = true;
} else if (reader.getLocalName().equals(Tags.sink) && reader.isStartElement()) {
sinkStatement = getAttributeByName(reader, Attributes.statement);
String re = getAttributeByName(reader, Attributes.linenumber);
if (!re.equals("")) {
sinkLineNo = Integer.parseInt(re);
} else {
sinkLineNo = -1;
}
sinkMethod = getAttributeByName(reader, Attributes.method);
} else if (reader.getLocalName().equals(Tags.source) && reader.isStartElement()) {
sourceStatement = getAttributeByName(reader, Attributes.statement);
ID = getAttributeByName(reader, Attributes.id);
String re = getAttributeByName(reader, Attributes.linenumber);
if (!re.equals("")) {
sourceLineNo = Integer.parseInt(getAttributeByName(reader, Attributes.linenumber));
} else {
sourceLineNo = -1;
}
sourceMethod = getAttributeByName(reader, Attributes.method);
} else if (reader.getLocalName().equals(Tags.pathElement) && reader.isStartElement()) {
pathStatement = getAttributeByName(reader, Attributes.statement);
String re = getAttributeByName(reader, Attributes.linenumber);
if (!re.equals("")) {
pathLineNo = Integer.parseInt(getAttributeByName(reader, Attributes.linenumber));
} else {
pathLineNo = -1;
}
pathMethod = getAttributeByName(reader, Attributes.method);
} else if (reader.isEndElement()) {
if (reader.getLocalName().equals(Tags.sink)) {
sink = new Location(sinkMethod, sinkStatement, sinkLineNo, null);
} else if (reader.getLocalName().equals(Tags.source)) {
source = new Location(sourceMethod, sourceStatement, sourceLineNo, ID);
results.add(new TaintFlow(source, sink, pathElements));
pathElements = new ArrayList<>();
} else if (reader.getLocalName().equals(Tags.pathElement)) {
Location pathElement = new Location(pathMethod, pathStatement, pathLineNo, null);
pathElements.add(pathElement);
}
}
}
}
if (!hasFormat) return null;
return results;
}

public static Map<String, TreeMap<Integer, Pair<Reference, Reference>>> convertToAQL(
Set<TaintFlow> flows) {
Map<String, TreeMap<Integer, Pair<Reference, Reference>>> res = new HashMap<>();
int i = 1;
for (TaintFlow flow : flows) {
ArrayList<Location> all = new ArrayList<>();
all.add(flow.getSource());
ArrayList<Location> inter = flow.getIntermediate();
if (inter.size() >= 2) {
List<Location> path = inter.subList(1, inter.size() - 1);
all.addAll(path);
}
all.add(flow.getSink());
String id = null;
if (!flow.getSource().getID().isEmpty()) {
id = flow.getSource().getID();
} else {
id = i + "";
}
if (!res.containsKey(id)) res.put(id, new TreeMap<>());
for (int j = 0; j < all.size() - 1; j++) {
Location f = all.get(j);
Statement sf = new Statement();
Reference from = new Reference();
from.setType("from");
from.setClassname(f.getClassSignature());
from.setMethod(f.getMethodSignature());
sf.setLinenumber(f.getLinenumber());
sf.setStatementfull(f.getStatement());
String s = f.getStatement();
if (s.contains("<init>")) {
s = s.replace("<init>", "[init]");
}
if (s.contains("<clinit>")) {
s = s.replace("<clinit>", "[clinit]");
}
if (s.contains("<") && s.contains(">")) {
s = s.substring(s.indexOf("<") + 1);
s = s.substring(0, s.indexOf(">"));
}
s = s.replace("[init]", "<init>");
s = s.replace("[clinit]", "<clinit>");
sf.setStatementgeneric(s);
from.setStatement(sf);

Location t = all.get(j + 1);

Statement st = new Statement();
Reference to = new Reference();
to.setType("to");
to.setClassname(t.getClassSignature());
to.setMethod(t.getMethodSignature());
st.setLinenumber(t.getLinenumber());
st.setStatementfull(t.getStatement());
s = t.getStatement();
if (s.contains("<init>")) {
s = s.replace("<init>", "[init]");
}
if (s.contains("<clinit>")) {
s = s.replace("<clinit>", "[clinit]");
}
if (s.contains("<") && s.contains(">")) {
s = s.substring(s.indexOf("<") + 1);
s = s.substring(0, s.indexOf(">"));
}
s = s.replace("[init]", "<init>");
s = s.replace("[clinit]", "<clinit>");
st.setStatementgeneric(s);
to.setStatement(st);
Pair<Reference, Reference> step = Pair.make(from, to);
res.get(id).put(j, step);
}
i++;
}
return res;
}

public static String getAttributeByName(XMLStreamReader reader, String id) {
for (int i = 0; i < reader.getAttributeCount(); i++)
if (reader.getAttributeLocalName(i).equals(id)) return reader.getAttributeValue(i);
return "";
}
}
14 changes: 12 additions & 2 deletions src/main/java/de/upb/swt/tbviewer/InputValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLStreamException;

/** @author Linghui Luo */
public class InputValidation {
Expand Down Expand Up @@ -44,8 +46,7 @@ public static boolean isTAFformat(File file) {
if (!sink.has("methodName")) return false;
if (!sink.has("className")) return false;
}
if (!finding.has("intermediateFlows")) return false;
else {
if (finding.has("intermediateFlows")) {
JsonArray flows = finding.getAsJsonArray("intermediateFlows");
for (int j = 0; j < flows.size(); j++) {
JsonObject inter = flows.get(j).getAsJsonObject();
Expand All @@ -68,4 +69,13 @@ public static boolean isAQLformat(File file) {
if (answer == null) return false;
return true;
}

public static boolean isFlowDroidFormat(File file) {
try {
return FlowDroidResultParser.hasFormat(file.getAbsolutePath());
} catch (FileNotFoundException | XMLStreamException | FactoryConfigurationError e) {
e.printStackTrace();
return false;
}
}
}
41 changes: 32 additions & 9 deletions src/main/java/de/upb/swt/tbviewer/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public int getLinenumber() {
private String methodSignature;
private String statement;
private int linenumber;
private String id;

private static Pattern javaMethodPattern = createJavaMethodPattern();
private static Pattern jimpleMethodPattern = createJimpleMethodPattern();

public static Pattern createJavaMethodPattern() {
// non capture modifier
String group0 =
"(?:public|private|protected|static|final|native|synchronized|abstract|transient)";
"(?:public|private|protected|static|final|native|synchronized|abstract|default)";
String group1 = "(.+)"; // return type
String group2 = "(.+)"; // method name
String group3 = "(.*?)"; // parameters
Expand Down Expand Up @@ -89,19 +90,20 @@ public static boolean compareMethod(String javaMethod, String jimpleMethod) {
String javaMethodName = javaMatcher.group(2);
String javaParameterString = javaMatcher.group(3);
// replace all types between < > for generic types
javaParameterString = javaParameterString.replaceAll("\\<.+\\>", "");
javaParameterString = javaParameterString.replaceAll("\\<[^\\>]+\\>", "");
String[] javaParameters = javaParameterString.split(",");

if (jimpleMatcher.find()) {
String jimpleReturenType = jimpleMatcher.group(1);
String jimpleMethodName = jimpleMatcher.group(2);
String[] jimpleParameterTypes = jimpleMatcher.group(3).split(",");
if (jimpleReturenType.endsWith(javaReturnType)) {
if (jimpleReturenType.endsWith(javaReturnType)
|| jimpleReturenType.equals("java.lang.Object")) {
if (jimpleMethodName.equals(javaMethodName)) {
if (javaParameters.length == jimpleParameterTypes.length) {
boolean paraMatch = true;
for (int i = 0; i < javaParameters.length; i++) {
String javaPara = javaParameters[i].split("\\s")[0];
String javaPara = javaParameters[i].trim().split("\\s")[0];
String jimplePara = jimpleParameterTypes[i];
if (javaPara.endsWith("...")) // take care of varargs
{
Expand Down Expand Up @@ -132,7 +134,7 @@ public static boolean compareMethod(String javaMethod, String jimpleMethod) {
}

public static boolean compareStatements(String javaStatement, String jimpleStatement) {
return false;
return true;
}

public Location(
Expand All @@ -149,7 +151,18 @@ public Location(
this.linenumber = linenumber;
}

public static boolean maybeEqual(Location a, Location b, boolean compareStatements) {
public Location(String method, String statement, int linenumber, String ID) {
this.kind = LocationKind.Jimple;
this.statement = statement;
String[] splits = method.split(":");
this.classSignature = splits[0].replace("<", "").trim();
this.methodSignature = method.replace("<", "").replace(">", "");
this.linenumber = linenumber;
this.id = ID;
}

public static boolean maybeEqual(
Location a, Location b, boolean compareStatements, int threshold) {
if (a.kind == b.kind) {
return a.classSignature.equals(b.classSignature)
&& a.methodSignature.equals(b.methodSignature)
Expand All @@ -166,20 +179,30 @@ public static boolean maybeEqual(Location a, Location b, boolean compareStatemen

if (aClass.equals(bClass)) {
if (compareMethod(a.methodSignature, b.methodSignature))
if (a.linenumber == b.linenumber && a.linenumber != -1) {
return true;
// sometimes soot returns wrong line numbers, so we set a threshold
if (Math.abs(a.linenumber - b.linenumber) < threshold && a.linenumber != -1) {
if (!compareStatements) {
return true;
} else {
return compareStatements(a.statement, b.statement);
}
} else {
if (a.linenumber != -1 && b.linenumber != -1) return false;
if (!compareStatements) return true;
else return compareStatements(a.statement, b.statement);
}
}
} else if (a.kind.equals(LocationKind.Jimple) && b.kind.equals(LocationKind.Java)) {
return maybeEqual(b, a, compareStatements);
return maybeEqual(b, a, compareStatements, threshold);
}
}
return false;
}

public String getID() {
return this.id;
}

@Override
public String toString() {
return "Location [kind="
Expand Down
Loading

0 comments on commit fd0cb6f

Please sign in to comment.