diff options
author | vanhauser-thc <vh@thc.org> | 2024-05-15 14:16:44 +0200 |
---|---|---|
committer | vanhauser-thc <vh@thc.org> | 2024-05-15 14:16:44 +0200 |
commit | 1db3b81d2eb855167dcf65734f8833a2329609da (patch) | |
tree | f0b32a3dc543d76791bfcbc350a41af0e11221f2 /instrumentation/afl-llvm-common.cc | |
parent | 0a16ea74879a935bbeb33e488cd451d6d9a7e19a (diff) | |
download | afl++-1db3b81d2eb855167dcf65734f8833a2329609da.tar.gz |
dump cc
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; |