Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java-frontend: Extract calculation related method #1297

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import ossf.fuzz.introspector.soot.utils.CalculationUtils;
import ossf.fuzz.introspector.soot.utils.CalltreeUtils;
import ossf.fuzz.introspector.soot.utils.MergeUtils;
import ossf.fuzz.introspector.soot.yaml.BranchProfile;
Expand Down Expand Up @@ -388,7 +389,7 @@ protected void internalTransform(String phaseName, Map<String, String> options)
}

element.setCountInformation(
blockGraph.size(), iCount, calculateCyclomaticComplexity(blockGraph));
blockGraph.size(), iCount, CalculationUtils.calculateCyclomaticComplexity(blockGraph));

this.methodList.addFunctionElement(element);
}
Expand All @@ -399,7 +400,7 @@ protected void internalTransform(String phaseName, Map<String, String> options)
"No method in analysing scope, consider relaxing the exclude constraint.");
}

this.calculateAllCallDepth();
CalculationUtils.calculateAllCallDepth(this.methodList);
if (!isAutoFuzz) {
CalltreeUtils.addSinkMethods(this.methodList, this.reachedSinkMethodList, this.isAutoFuzz);
}
Expand Down Expand Up @@ -438,89 +439,6 @@ protected void internalTransform(String phaseName, Map<String, String> options)
analyseFinished = true;
}

private void calculateAllCallDepth() {
List<FunctionElement> newMethodList = new LinkedList<FunctionElement>();

for (FunctionElement element : this.methodList.getFunctionElements()) {
if (!element.getFunctionName().contains("init>")) {
this.calculateCallDepth(element, null);
if (this.depthHandled != null) {
for (FunctionElement handledElement : this.depthHandled) {
newMethodList.add(handledElement);
}
}
} else {
newMethodList.add(element);
}
}

this.methodList.setFunctionElements(newMethodList);
}

private Integer calculateCallDepth(FunctionElement element, List<FunctionElement> handled) {
if (handled == null) {
handled = new LinkedList<FunctionElement>();
}

List<String> handledName = new LinkedList<String>();
for (FunctionElement handledElement : handled) {
handledName.add(handledElement.getFunctionName());
}

Integer depth = element.getFunctionDepth();
if (!handledName.contains(element.getFunctionName())) {
handled.add(element);
if (depth == 0) {
for (Callsite callsite : element.getCallsites()) {
String callerName = callsite.getMethodName();
FunctionElement caller = methodList.searchElement(callerName);
if (caller != null) {
Integer newDepth = this.calculateCallDepth(caller, handled) + 1;
depth = (newDepth > depth) ? newDepth : depth;
}
}
}
element.setFunctionDepth(depth);
}
depthHandled = handled;

return depth;
}

private Integer calculateCyclomaticComplexity(BlockGraph blockGraph) {
Integer nodes = blockGraph.size();
Integer edges = 0;

// Count edges of the blockGraph
for (Block block : blockGraph.getBlocks()) {
edges += blockGraph.getSuccsOf(block).size();
}

Integer complexity = edges - nodes + 2;
if (complexity < 1) {
complexity = 1;
}
return complexity;
}

private Integer calculateConditionComplexity(Value value, Integer complexity) {
List<ValueBox> boxList = value.getUseBoxes();

if (boxList.size() == 0) {
if (value instanceof AndExpr || value instanceof OrExpr) {
return 1;
} else {
return 0;
}
}

for (ValueBox box : boxList) {
complexity += this.calculateConditionComplexity(box.getValue(), complexity);
}

return complexity;
}

private Map<String, Integer> getBlockStartEndLineWithLineNumber(
List<Block> blocks, Integer lineNumber) {
Integer startLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2022 Fuzz Introspector Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
///////////////////////////////////////////////////////////////////////////

package ossf.fuzz.introspector.soot.utils;

import java.util.LinkedList;
import java.util.List;
import ossf.fuzz.introspector.soot.yaml.Callsite;
import ossf.fuzz.introspector.soot.yaml.FunctionConfig;
import ossf.fuzz.introspector.soot.yaml.FunctionElement;
import soot.toolkits.graph.Block;
import soot.toolkits.graph.BlockGraph;

public class CalculationUtils {
private static List<FunctionElement> depthHandled;

public static Integer calculateCyclomaticComplexity(BlockGraph blockGraph) {
Integer nodes = blockGraph.size();
Integer edges = 0;

// Count edges of the blockGraph
for (Block block : blockGraph.getBlocks()) {
edges += blockGraph.getSuccsOf(block).size();
}

Integer complexity = edges - nodes + 2;
if (complexity < 1) {
complexity = 1;
}
return complexity;
}

public static void calculateAllCallDepth(FunctionConfig methodList) {
List<FunctionElement> newMethodList = new LinkedList<FunctionElement>();
depthHandled = new LinkedList<FunctionElement>();

for (FunctionElement element : methodList.getFunctionElements()) {
if (!element.getFunctionName().contains("init>")) {
calculateCallDepth(methodList, element, null);
for (FunctionElement handledElement : depthHandled) {
newMethodList.add(handledElement);
}
} else {
newMethodList.add(element);
}
}

methodList.setFunctionElements(newMethodList);
}

private static Integer calculateCallDepth(
FunctionConfig methodList, FunctionElement element, List<FunctionElement> handled) {
if (handled == null) {
handled = new LinkedList<FunctionElement>();
}

List<String> handledName = new LinkedList<String>();
for (FunctionElement handledElement : handled) {
handledName.add(handledElement.getFunctionName());
}

Integer depth = element.getFunctionDepth();
if (!handledName.contains(element.getFunctionName())) {
handled.add(element);
if (depth == 0) {
for (Callsite callsite : element.getCallsites()) {
String callerName = callsite.getMethodName();
FunctionElement caller = methodList.searchElement(callerName);
if (caller != null) {
Integer newDepth = calculateCallDepth(methodList, caller, handled) + 1;
depth = (newDepth > depth) ? newDepth : depth;
}
}
}
element.setFunctionDepth(depth);
}
depthHandled = handled;

return depth;
}
}
Loading