From 057515c7081113ce6c1abcc33bd63e5736ee94c2 Mon Sep 17 00:00:00 2001 From: Arthur Chan Date: Mon, 6 Nov 2023 21:06:12 +0000 Subject: [PATCH] Java-frontend: Extract calculation methods to utils package Signed-off-by: Arthur Chan --- .../soot/SootSceneTransformer.java | 88 +----------------- .../soot/utils/CalculationUtils.java | 93 +++++++++++++++++++ 2 files changed, 96 insertions(+), 85 deletions(-) create mode 100644 frontends/java/src/main/java/ossf/fuzz/introspector/soot/utils/CalculationUtils.java diff --git a/frontends/java/src/main/java/ossf/fuzz/introspector/soot/SootSceneTransformer.java b/frontends/java/src/main/java/ossf/fuzz/introspector/soot/SootSceneTransformer.java index a400b20c0..2df29eb1c 100644 --- a/frontends/java/src/main/java/ossf/fuzz/introspector/soot/SootSceneTransformer.java +++ b/frontends/java/src/main/java/ossf/fuzz/introspector/soot/SootSceneTransformer.java @@ -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; @@ -388,7 +389,7 @@ protected void internalTransform(String phaseName, Map options) } element.setCountInformation( - blockGraph.size(), iCount, calculateCyclomaticComplexity(blockGraph)); + blockGraph.size(), iCount, CalculationUtils.calculateCyclomaticComplexity(blockGraph)); this.methodList.addFunctionElement(element); } @@ -399,7 +400,7 @@ protected void internalTransform(String phaseName, Map 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); } @@ -438,89 +439,6 @@ protected void internalTransform(String phaseName, Map options) analyseFinished = true; } - private void calculateAllCallDepth() { - List newMethodList = new LinkedList(); - - 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 handled) { - if (handled == null) { - handled = new LinkedList(); - } - - List handledName = new LinkedList(); - 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 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 getBlockStartEndLineWithLineNumber( List blocks, Integer lineNumber) { Integer startLine; diff --git a/frontends/java/src/main/java/ossf/fuzz/introspector/soot/utils/CalculationUtils.java b/frontends/java/src/main/java/ossf/fuzz/introspector/soot/utils/CalculationUtils.java new file mode 100644 index 000000000..5e09e8beb --- /dev/null +++ b/frontends/java/src/main/java/ossf/fuzz/introspector/soot/utils/CalculationUtils.java @@ -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 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 newMethodList = new LinkedList(); + depthHandled = new LinkedList(); + + 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 handled) { + if (handled == null) { + handled = new LinkedList(); + } + + List handledName = new LinkedList(); + 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; + } +}