refactor get filename
MODIFY
llvm_mode/afl-llvm-common.cc
llvm_mode/afl-llvm-common.cc
@@ -327,49 +327,7 @@ void scanForDangerousFunctions(llvm::Module *M) {
}
-bool isInInstrumentList(llvm::Function *F) {
-
- bool return_default = true;
-
- // is this a function with code? If it is external we don't instrument it
- // anyway and it can't be in the instrument file list. Or if it is it is ignored.
- if (!F->size() || isIgnoreFunction(F)) return false;
-
- if (!denyListFiles.empty() || !denyListFunctions.empty()) {
-
- if (!denyListFunctions.empty()) {
-
- std::string instFunction = F->getName().str();
-
- for (std::list<std::string>::iterator it = denyListFunctions.begin();
- it != denyListFunctions.end(); ++it) {
-
- /* We don't check for filename equality here because
- * filenames might actually be full paths. Instead we
- * check that the actual filename ends in the filename
- * specified in the list. We also allow UNIX-style pattern
- * matching */
-
- if (instFunction.length() >= it->length()) {
-
- if (fnmatch(("*" + *it).c_str(), instFunction.c_str(), 0) == 0) {
-
- if (debug)
- SAYF(cMGN "[D] " cRST
- "Function %s is in the deny function list, "
- "not instrumenting ... \n",
- instFunction.c_str());
- return false;
-
- }
-
- }
-
- }
-
- }
-
- if (!denyListFiles.empty()) {
+static std::string getSourceName(llvm::Function *F) {
// let's try to get the filename for the function
auto bb = &F->getEntryBlock();
@@ -400,11 +358,46 @@ bool isInInstrumentList(llvm::Function *F) {
}
+ return instFilename.str();
+
+ }
+
+#else
+ if (!Loc.isUnknown()) {
+
+ DILocation cDILoc(Loc.getAsMDNode(F->getContext()));
+
+ unsigned int instLine = cDILoc.getLineNumber();
+ StringRef instFilename = cDILoc.getFilename();
+
+ (void)instLine;
/* Continue only if we know where we actually are */
- if (!instFilename.str().empty()) {
+ return instFilename.str();
- for (std::list<std::string>::iterator it = denyListFiles.begin();
- it != denyListFiles.end(); ++it) {
+ }
+
+#endif
+
+ return std::string("");
+
+}
+
+bool isInInstrumentList(llvm::Function *F) {
+
+ bool return_default = true;
+
+ // is this a function with code? If it is external we don't instrument it
+ // anyway and it can't be in the instrument file list. Or if it is it is ignored.
+ if (!F->size() || isIgnoreFunction(F)) return false;
+
+ if (!denyListFiles.empty() || !denyListFunctions.empty()) {
+
+ if (!denyListFunctions.empty()) {
+
+ std::string instFunction = F->getName().str();
+
+ for (std::list<std::string>::iterator it = denyListFunctions.begin();
+ it != denyListFunctions.end(); ++it) {
/* We don't check for filename equality here because
* filenames might actually be full paths. Instead we
@@ -412,16 +405,15 @@ bool isInInstrumentList(llvm::Function *F) {
* specified in the list. We also allow UNIX-style pattern
* matching */
- if (instFilename.str().length() >= it->length()) {
+ if (instFunction.length() >= it->length()) {
- if (fnmatch(("*" + *it).c_str(), instFilename.str().c_str(), 0) ==
- 0) {
+ if (fnmatch(("*" + *it).c_str(), instFunction.c_str(), 0) == 0) {
if (debug)
SAYF(cMGN "[D] " cRST
- "Function %s is in the denylist (%s), not "
- "instrumenting ... \n",
- F->getName().str().c_str(), instFilename.str().c_str());
+ "Function %s is in the deny function list, "
+ "not instrumenting ... \n",
+ instFunction.c_str());
return false;
}
@@ -432,19 +424,11 @@ bool isInInstrumentList(llvm::Function *F) {
}
- }
-
-#else
- if (!Loc.isUnknown()) {
-
- DILocation cDILoc(Loc.getAsMDNode(F->getContext()));
+ if (!denyListFiles.empty()) {
- unsigned int instLine = cDILoc.getLineNumber();
- StringRef instFilename = cDILoc.getFilename();
+ std::string source_file = getSourceName(F);
- (void)instLine;
- /* Continue only if we know where we actually are */
- if (!instFilename.str().empty()) {
+ if (!source_file.empty()) {
for (std::list<std::string>::iterator it = denyListFiles.begin();
it != denyListFiles.end(); ++it) {
@@ -455,10 +439,9 @@ bool isInInstrumentList(llvm::Function *F) {
* specified in the list. We also allow UNIX-style pattern
* matching */
- if (instFilename.str().length() >= it->length()) {
+ if (source_file.length() >= it->length()) {
- if (fnmatch(("*" + *it).c_str(), instFilename.str().c_str(), 0) ==
- 0) {
+ if (fnmatch(("*" + *it).c_str(), source_file.c_str(), 0) == 0) {
return false;
@@ -468,12 +451,7 @@ bool isInInstrumentList(llvm::Function *F) {
}
- }
-
- }
-
-#endif
- else {
+ } else {
// we could not find out the location. in this case we say it is not
// in the instrument file list
@@ -528,37 +506,9 @@ bool isInInstrumentList(llvm::Function *F) {
if (!allowListFiles.empty()) {
- // let's try to get the filename for the function
- auto bb = &F->getEntryBlock();
- BasicBlock::iterator IP = bb->getFirstInsertionPt();
- IRBuilder<> IRB(&(*IP));
- DebugLoc Loc = IP->getDebugLoc();
+ std::string source_file = getSourceName(F);
-#if LLVM_VERSION_MAJOR >= 4 || \
- (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 7)
- if (Loc) {
-
- DILocation *cDILoc = dyn_cast<DILocation>(Loc.getAsMDNode());
-
- unsigned int instLine = cDILoc->getLine();
- StringRef instFilename = cDILoc->getFilename();
-
- if (instFilename.str().empty()) {
-
- /* If the original location is empty, try using the inlined location
- */
- DILocation *oDILoc = cDILoc->getInlinedAt();
- if (oDILoc) {
-
- instFilename = oDILoc->getFilename();
- instLine = oDILoc->getLine();
-
- }
-
- }
-
- /* Continue only if we know where we actually are */
- if (!instFilename.str().empty()) {
+ if (!source_file.empty()) {
for (std::list<std::string>::iterator it = allowListFiles.begin();
it != allowListFiles.end(); ++it) {
@@ -569,54 +519,15 @@ bool isInInstrumentList(llvm::Function *F) {
* specified in the list. We also allow UNIX-style pattern
* matching */
- if (instFilename.str().length() >= it->length()) {
+ if (source_file.length() >= it->length()) {
- if (fnmatch(("*" + *it).c_str(), instFilename.str().c_str(), 0) ==
- 0) {
+ if (fnmatch(("*" + *it).c_str(), source_file.c_str(), 0) == 0) {
if (debug)
SAYF(cMGN "[D] " cRST
"Function %s is in the allowlist (%s), "
"instrumenting ... \n",
- F->getName().str().c_str(), instFilename.str().c_str());
- return true;
-
- }
-
- }
-
- }
-
- }
-
- }
-
-#else
- if (!Loc.isUnknown()) {
-
- DILocation cDILoc(Loc.getAsMDNode(F->getContext()));
-
- unsigned int instLine = cDILoc.getLineNumber();
- StringRef instFilename = cDILoc.getFilename();
-
- (void)instLine;
- /* Continue only if we know where we actually are */
- if (!instFilename.str().empty()) {
-
- for (std::list<std::string>::iterator it = allowListFiles.begin();
- it != allowListFiles.end(); ++it) {
-
- /* We don't check for filename equality here because
- * filenames might actually be full paths. Instead we
- * check that the actual filename ends in the filename
- * specified in the list. We also allow UNIX-style pattern
- * matching */
-
- if (instFilename.str().length() >= it->length()) {
-
- if (fnmatch(("*" + *it).c_str(), instFilename.str().c_str(), 0) ==
- 0) {
-
+ F->getName().str().c_str(), source_file.c_str());
return true;
}
@@ -625,12 +536,7 @@ bool isInInstrumentList(llvm::Function *F) {
}
- }
-
- }
-
-#endif
- else {
+ } else {
// we could not find out the location. In this case we say it is not
// in the instrument file list

refactor get filename