|
| 1 | +#include <llvm/IR/Module.h> |
| 2 | +#include <llvm/IRReader/IRReader.h> |
| 3 | +#include <llvm/IR/LLVMContext.h> |
| 4 | +#include <llvm/Support/SourceMgr.h> |
| 5 | +#include <llvm/Support/raw_ostream.h> |
| 6 | +#include <llvm/IR/InstrTypes.h> |
| 7 | +#include <llvm/IR/Instructions.h> |
| 8 | +#include <llvm/Analysis/CmpInstAnalysis.h> |
| 9 | +#include <llvm/IR/DebugLoc.h> |
| 10 | +#include "llvm/IR/DebugInfo.h" |
| 11 | +#include "llvm/IR/DIBuilder.h" |
| 12 | +#include "llvm/IR/Function.h" |
| 13 | +#include <llvm/IR/DebugInfoMetadata.h> |
| 14 | +#include <llvm/IR/CFG.h> |
| 15 | +#include <llvm/IR/Metadata.h> |
| 16 | +#include <llvm/Analysis/CallGraph.h> |
| 17 | + |
| 18 | +#include <string> |
| 19 | +#include <iostream> |
| 20 | +#include <sstream> |
| 21 | +#include <fstream> |
| 22 | +#include <sys/types.h> |
| 23 | +#include <dirent.h> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "log.h" |
| 27 | + |
| 28 | + |
| 29 | +using namespace std; |
| 30 | +using namespace llvm; |
| 31 | + |
| 32 | + |
| 33 | +std::vector<std::string> readFuncList(std::string funcListPath); |
| 34 | +std::vector<std::string> getIRList(std::string IRDirPath); |
| 35 | +std::map<std::string, int> FuncComplexity; |
| 36 | + |
| 37 | +void getCalledFunc(CallGraph *CG, const Function *f, int blockNum, int level, std::vector<std::string> *funcList, std::string rootFunc); |
| 38 | +void writeToNewFuncList(std::vector<std::string> funcList, std::string oldPath); |
| 39 | + |
| 40 | +int main(int argc, const char *argv[]) { |
| 41 | + if (argc < 2 || argv[1] == nullptr) { |
| 42 | + outs() << "./extern_func functions_list ir_dir\n"; |
| 43 | + return 1; |
| 44 | + } |
| 45 | + std::string FuncListPath = argv[1]; |
| 46 | + std::string IRDirPath = argv[2]; |
| 47 | + |
| 48 | + std::vector<std::string> IRList = getIRList(IRDirPath); |
| 49 | + std::vector<std::string> funcList = readFuncList(FuncListPath); |
| 50 | + std::vector<std::string> extFuncList; |
| 51 | + |
| 52 | + for (std::string ir : IRList) { |
| 53 | + LLVMContext ctx; |
| 54 | + SMDiagnostic err; |
| 55 | + std::unique_ptr<Module> mod = parseIRFile(ir, err, ctx); |
| 56 | + if (mod == nullptr) { |
| 57 | + outs() << FAIL << "Failed to open ir file: " << ir << "\n" << RESET; |
| 58 | + continue; |
| 59 | + } |
| 60 | + Module *constMod = mod.get(); |
| 61 | + CallGraph CG(*constMod); |
| 62 | + for (std::string fn : funcList) { |
| 63 | + const Function *func = constMod->getFunction(fn); |
| 64 | + if (func == nullptr || func->size() < 1) |
| 65 | + continue; |
| 66 | + FuncComplexity[fn] = func->size(); |
| 67 | + outs() << "Function: " << fn << " was found\n"; |
| 68 | + if (func->size() < 20) |
| 69 | + getCalledFunc(&CG, func, 7, 3, &extFuncList, fn); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /* Show the sum blocks of enable functions of a root function */ |
| 74 | + outs() << "Extended function:\n"; |
| 75 | + for (auto &i : FuncComplexity) { |
| 76 | + outs() << "Function: " << i.first << " with " << i.second << " blocks\n"; |
| 77 | + } |
| 78 | + |
| 79 | + for (std::string f : extFuncList) { |
| 80 | + bool found = false; |
| 81 | + for (std::string ff : funcList) { |
| 82 | + if (f == ff) { |
| 83 | + found = true; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + if (found) |
| 88 | + continue; |
| 89 | + funcList.push_back(f); |
| 90 | + } |
| 91 | + /* Create a FuncListPath.new file for extended functions list */ |
| 92 | + writeToNewFuncList(funcList, FuncListPath); |
| 93 | +} |
| 94 | + |
| 95 | +std::vector<std::string> readFuncList(std::string funcListPath) { |
| 96 | + fstream funcListFile(funcListPath); |
| 97 | + std::vector<std::string> funcList; |
| 98 | + std::string fn = ""; |
| 99 | + if (!funcListFile.is_open()) { |
| 100 | + outs() << FAIL << "Failed to open init function list\n" << RESET; |
| 101 | + return funcList; |
| 102 | + } |
| 103 | + while (getline(funcListFile, fn)) { |
| 104 | + if(fn != "") |
| 105 | + funcList.push_back(fn); |
| 106 | + } |
| 107 | + return funcList; |
| 108 | +} |
| 109 | + |
| 110 | +std::vector<std::string> getIRList(std::string IRDirPath) { |
| 111 | + std::vector<std::string> ret; |
| 112 | + struct dirent *entry; |
| 113 | + DIR *dir = opendir(IRDirPath.c_str()); |
| 114 | + if (dir == NULL) { |
| 115 | + return ret; |
| 116 | + } |
| 117 | + while ((entry = readdir(dir)) != NULL) { |
| 118 | + ret.push_back(IRDirPath + "/" + entry->d_name); |
| 119 | + } |
| 120 | + closedir(dir); |
| 121 | + return ret; |
| 122 | +} |
| 123 | + |
| 124 | +/* Recursively get the called functions, use blockNum and level limit functions */ |
| 125 | +void getCalledFunc(CallGraph *CG, const Function *f, int blockNum, int level, std::vector<std::string> *funcList, std::string rootFunc) { |
| 126 | + const CallGraphNode *n = (*CG)[f]; |
| 127 | + if (level < 1) |
| 128 | + return; |
| 129 | + if (n == nullptr) { |
| 130 | + outs() << FAIL << "Failed to get call graph node\n" << RESET; |
| 131 | + return; |
| 132 | + } |
| 133 | + for (auto i : *n) { |
| 134 | + Function *calledFunc = i.second->getFunction(); |
| 135 | + if (calledFunc != nullptr) { |
| 136 | + /* Skip the instument function */ |
| 137 | + if (calledFunc->getName().find("saniti") != std::string::npos) |
| 138 | + continue; |
| 139 | + if (calledFunc->getName().find("asan") != std::string::npos) |
| 140 | + continue; |
| 141 | + if (calledFunc->size() > blockNum) { |
| 142 | + funcList->push_back(calledFunc->getName()); |
| 143 | + outs() << "Add function " << calledFunc->getName() << " to root " << rootFunc << " at level " << level << "\n"; |
| 144 | + /* Calculate the sum of blocks from root function to every called functions */ |
| 145 | + FuncComplexity[rootFunc] += calledFunc->size(); |
| 146 | + } |
| 147 | + /* Recursive call with a depth level */ |
| 148 | + getCalledFunc(CG, calledFunc, blockNum, level - 1, funcList, rootFunc); |
| 149 | + } |
| 150 | + } |
| 151 | + return; |
| 152 | +} |
| 153 | + |
| 154 | +void writeToNewFuncList(std::vector<std::string> funcList, std::string oldPath) { |
| 155 | + ofstream newFuncList; |
| 156 | + newFuncList.open(oldPath + ".new"); |
| 157 | + for (std::string f : funcList) { |
| 158 | + newFuncList << f << "\n"; |
| 159 | + } |
| 160 | + newFuncList.close(); |
| 161 | + |
| 162 | +} |
0 commit comments