Skip to content

Commit

Permalink
Fix function elements
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
  • Loading branch information
arthurscchan committed Nov 6, 2023
1 parent 2eae095 commit d3b68ed
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,27 +307,9 @@ protected void internalTransform(String phaseName, Map<String, String> options)
}

element.setFunctionName("[" + c.getFilePath() + "]." + m.getSubSignature().split(" ")[1]);
element.setFunctionSourceFile(c.getFilePath());
element.setFunctionLinenumber(m.getJavaSourceStartLineNumber());
element.setReturnType(m.getReturnType().toString());
element.setFunctionDepth(0);
element.setArgCount(m.getParameterCount());
for (soot.Type type : m.getParameterTypes()) {
element.addArgType(type.toString());
}
element.setBaseInformation(m);
if (isAutoFuzz) {
JavaMethodInfo methodInfo = new JavaMethodInfo();
methodInfo.setIsConcrete(m.isConcrete());
methodInfo.setIsJavaLibraryMethod(m.isJavaLibraryMethod());
methodInfo.setIsPublic(m.isPublic());
methodInfo.setIsStatic(m.isStatic());
methodInfo.setIsClassEnum(c.isEnum());
methodInfo.setIsClassPublic(c.isPublic());
methodInfo.setIsClassConcrete(c.isConcrete());
for (SootClass exception : m.getExceptions()) {
methodInfo.addException(exception.getFilePath());
}
element.setJavaMethodInfo(methodInfo);
element.setJavaMethodInfo(m);
}

// Identify in / out edges of each method.
Expand Down Expand Up @@ -383,17 +365,12 @@ protected void internalTransform(String phaseName, Map<String, String> options)
try {
methodBody = m.retrieveActiveBody();
} catch (Exception e) {
// Source code not provided for this method.
element.setBBCount(0);
element.setiCount(0);
element.setCyclomaticComplexity(0);
this.addMethodElement(element);
// System.err.println("Source code for " + m + " not found.");
continue;
}
BlockGraph blockGraph = new BriefBlockGraph(methodBody);

element.setBBCount(blockGraph.size());
int iCount = 0;
for (Block block : blockGraph.getBlocks()) {
Iterator<Unit> blockIt = block.iterator();
Expand All @@ -414,10 +391,8 @@ protected void internalTransform(String phaseName, Map<String, String> options)
iCount++;
}
}
element.setiCount(iCount);

// Calculate method cyclomatic complexity from method unit graph
element.setCyclomaticComplexity(calculateCyclomaticComplexity(blockGraph));
element.setCountInformation(blockGraph.size(), iCount, calculateCyclomaticComplexity(blockGraph));

this.addMethodElement(element);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,55 +78,12 @@ public static List<FunctionElement> getConstructorList(SootClass sootClass) {
List<SootMethod> mList = new LinkedList<SootMethod>(sootClass.getMethods());
for (SootMethod method : mList) {
if (method.getName().equals("<init>")) {
FunctionElement element = new FunctionElement();
String name = "[" + sootClass.getName() + "]." + method.getSubSignature().split(" ")[1];
element.setFunctionName(name);
element.setFunctionSourceFile(sootClass.getName());
element.setFunctionLinenumber(method.getJavaSourceStartLineNumber());
element.setReturnType("");
element.setFunctionDepth(0);
element.setArgCount(method.getParameterCount());
for (soot.Type type : method.getParameterTypes()) {
element.addArgType(type.toString());
}
element.setFunctionUses(0);
element.setEdgeCount(0);
element.setBBCount(0);
element.setiCount(0);
element.setCyclomaticComplexity(0);

JavaMethodInfo methodInfo = new JavaMethodInfo();
methodInfo.setIsConcrete(method.isConcrete());
methodInfo.setIsPublic(method.isPublic());
methodInfo.setIsClassConcrete(sootClass.isConcrete());
methodInfo.setIsClassEnum(sootClass.isEnum());
methodInfo.setIsClassPublic(sootClass.isPublic());
if (sootClass.hasSuperclass()) {
methodInfo.setSuperClass(sootClass.getSuperclass().getName());
}
for (SootClass exception : method.getExceptions()) {
methodInfo.addException(exception.getFilePath());
}
Iterator<SootClass> interfaces = sootClass.getInterfaces().snapshotIterator();
while (interfaces.hasNext()) {
methodInfo.addInterface(interfaces.next().getName());
}
Iterator<SootField> fields = sootClass.getFields().snapshotIterator();
while (fields.hasNext()) {
SootField field = fields.next();
ClassField classField = new ClassField();

classField.setFieldName(field.getName());
classField.setFieldType(field.getType().toString());
classField.setIsConcrete(field.isDeclared());
classField.setIsPublic(field.isPublic());
classField.setIsStatic(field.isStatic());
classField.setIsFinal(field.isFinal());

methodInfo.addClassField(classField);
}

element.setJavaMethodInfo(methodInfo);
FunctionElement element = new FunctionElement();
element.setFunctionName(name);
element.setBaseInformation(method);
element.setJavaMethodInfo(method);

eList.add(element);
}
Expand All @@ -141,34 +98,12 @@ public static List<FunctionElement> getSinkMethodList(List<SootMethod> reachedSi

for (SootMethod method : reachedSinkMethodList) {
SootClass cl = method.getDeclaringClass();

FunctionElement element = new FunctionElement();
element.setFunctionName("[" + cl.getName() + "]." + method.getSubSignature().split(" ")[1]);
element.setFunctionSourceFile(cl.getName());
element.setFunctionLinenumber(method.getJavaSourceStartLineNumber());
element.setReturnType(method.getReturnType().toString());
element.setFunctionDepth(0);
element.setArgCount(method.getParameterCount());
for (soot.Type type : method.getParameterTypes()) {
element.addArgType(type.toString());
}
element.setFunctionUses(0);
element.setEdgeCount(0);
element.setBBCount(0);
element.setiCount(0);
element.setCyclomaticComplexity(0);

element.setBaseInformation(method);
if (isAutoFuzz) {
JavaMethodInfo methodInfo = new JavaMethodInfo();
methodInfo.setIsConcrete(method.isConcrete());
methodInfo.setIsJavaLibraryMethod(method.isJavaLibraryMethod());
methodInfo.setIsPublic(method.isPublic());
methodInfo.setIsStatic(method.isStatic());
methodInfo.setIsClassEnum(method.getDeclaringClass().isEnum());
methodInfo.setIsClassPublic(method.getDeclaringClass().isPublic());
for (SootClass exception : method.getExceptions()) {
methodInfo.addException(exception.getFilePath());
}
element.setJavaMethodInfo(methodInfo);
element.setJavaMethodInfo(method);
}

eList.add(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import soot.SootClass;
import soot.SootField;
import soot.SootMethod;
import soot.Type;

public class FunctionElement {
private String functionName;
Expand Down Expand Up @@ -47,6 +52,13 @@ public FunctionElement() {
this.functionsReached = new ArrayList<String>();
this.branchProfiles = new ArrayList<BranchProfile>();
this.callsites = new ArrayList<Callsite>();

this.functionDepth = 0;
this.functionUses = 0;
this.edgeCount = 0;
this.BBCount = 0;
this.iCount = 0;
this.CyclomaticComplexity = 0;
}

public String getFunctionName() {
Expand Down Expand Up @@ -237,7 +249,66 @@ public JavaMethodInfo getJavaMethodInfo() {
return this.javaMethodInfo;
}

public void setJavaMethodInfo(JavaMethodInfo javaMethodInfo) {
this.javaMethodInfo = javaMethodInfo;
public void setJavaMethodInfo(SootMethod m) {
JavaMethodInfo methodInfo = new JavaMethodInfo();
SootClass c = m.getDeclaringClass();

// Base java method information
methodInfo.setIsConcrete(m.isConcrete());
methodInfo.setIsJavaLibraryMethod(m.isJavaLibraryMethod());
methodInfo.setIsPublic(m.isPublic());
methodInfo.setIsStatic(m.isStatic());
methodInfo.setIsClassEnum(c.isEnum());
methodInfo.setIsClassPublic(c.isPublic());
methodInfo.setIsClassConcrete(c.isConcrete());
for (SootClass exception : m.getExceptions()) {
methodInfo.addException(exception.getFilePath());
}

// Extra class information for constructors
if (m.getName().equals("<init>")) {
if (c.hasSuperclass()) {
methodInfo.setSuperClass(c.getSuperclass().getName());
}
Iterator<SootClass> interfaces = c.getInterfaces().snapshotIterator();
while (interfaces.hasNext()) {
methodInfo.addInterface(interfaces.next().getName());
}
Iterator<SootField> fields = c.getFields().snapshotIterator();
while (fields.hasNext()) {
SootField field = fields.next();
ClassField classField = new ClassField();

classField.setFieldName(field.getName());
classField.setFieldType(field.getType().toString());
classField.setIsConcrete(field.isDeclared());
classField.setIsPublic(field.isPublic());
classField.setIsStatic(field.isStatic());
classField.setIsFinal(field.isFinal());

methodInfo.addClassField(classField);
}
}

this.javaMethodInfo = methodInfo;
}

public void setBaseInformation(SootMethod m) {
SootClass c = m.getDeclaringClass();

this.setFunctionSourceFile(c.getFilePath());
this.setFunctionLinenumber(m.getJavaSourceStartLineNumber());
this.setReturnType(m.getReturnType().toString());
this.setFunctionDepth(0);
this.setArgCount(m.getParameterCount());
for (Type type : m.getParameterTypes()) {
this.addArgType(type.toString());
}
}

public void setCountInformation(Integer bbCount, Integer iCount, Integer complexity) {
this.setBBCount(bbCount);
this.setiCount(iCount);
this.setCyclomaticComplexity(complexity);
}
}

0 comments on commit d3b68ed

Please sign in to comment.