diff options
author | van Hauser <vh@thc.org> | 2024-05-17 23:55:55 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-17 23:55:55 +0200 |
commit | e7d871c8bf64962a658e447b90a1a3b43aaddc28 (patch) | |
tree | 7aec2a095a30ed609ce96f85ec3c4e0a8b8eb74c /instrumentation/afl-llvm-common.cc | |
parent | 497f341eac230fab13d6b5c5153c36321371b180 (diff) | |
parent | 56d5aa3101945e81519a3fac8783d0d8fad82779 (diff) | |
download | afl++-e7d871c8bf64962a658e447b90a1a3b43aaddc28.tar.gz |
Merge pull request #2093 from AFLplusplus/dev
push to stable
Diffstat (limited to 'instrumentation/afl-llvm-common.cc')
-rw-r--r-- | instrumentation/afl-llvm-common.cc | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/instrumentation/afl-llvm-common.cc b/instrumentation/afl-llvm-common.cc index 8e9e7800..ed9268dc 100644 --- a/instrumentation/afl-llvm-common.cc +++ b/instrumentation/afl-llvm-common.cc @@ -26,6 +26,51 @@ static std::list<std::string> allowListFunctions; static std::list<std::string> denyListFiles; static std::list<std::string> denyListFunctions; +unsigned int calcCyclomaticComplexity(llvm::Function *F) { + + unsigned int numBlocks = 0; + unsigned int numEdges = 0; + unsigned int numCalls = 0; + + // Iterate through each basic block in the function + for (BasicBlock &BB : *F) { + + // count all nodes == basic blocks + numBlocks++; + // Count the number of successors (outgoing edges) + for (BasicBlock *Succ : successors(&BB)) { + + // count edges for CC + numEdges++; + (void)(Succ); + + } + + for (Instruction &I : BB) { + + // every call is also an edge, so we need to count the calls too + if (isa<CallInst>(&I) || isa<InvokeInst>(&I)) { numCalls++; } + + } + + } + + // Cyclomatic Complexity V(G) = E - N + 2P + // For a single function, P (number of connected components) is 1 + // Calls are considered to be an edge + unsigned int CC = 2 + numCalls + numEdges - numBlocks; + + // if (debug) { + + fprintf(stderr, "CyclomaticComplexity for %s: %u\n", + F->getName().str().c_str(), CC); + + //} + + return CC; + +} + char *getBBName(const llvm::BasicBlock *BB) { static char *name; |