refactor get filename

This commit is contained in:
van Hauser
2020-08-13 16:29:00 +02:00
parent 47faf3dd33
commit 7f435ec5f1

View File

@ -327,6 +327,61 @@ void scanForDangerousFunctions(llvm::Module *M) {
} }
static std::string getSourceName(llvm::Function *F) {
// 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();
#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();
}
}
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 */
return instFilename.str();
}
#endif
return std::string("");
}
bool isInInstrumentList(llvm::Function *F) { bool isInInstrumentList(llvm::Function *F) {
bool return_default = true; bool return_default = true;
@ -371,37 +426,9 @@ bool isInInstrumentList(llvm::Function *F) {
if (!denyListFiles.empty()) { if (!denyListFiles.empty()) {
// let's try to get the filename for the function std::string source_file = getSourceName(F);
auto bb = &F->getEntryBlock();
BasicBlock::iterator IP = bb->getFirstInsertionPt();
IRBuilder<> IRB(&(*IP));
DebugLoc Loc = IP->getDebugLoc();
#if LLVM_VERSION_MAJOR >= 4 || \ if (!source_file.empty()) {
(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()) {
for (std::list<std::string>::iterator it = denyListFiles.begin(); for (std::list<std::string>::iterator it = denyListFiles.begin();
it != denyListFiles.end(); ++it) { it != denyListFiles.end(); ++it) {
@ -412,53 +439,9 @@ bool isInInstrumentList(llvm::Function *F) {
* specified in the list. We also allow UNIX-style pattern * specified in the list. We also allow UNIX-style pattern
* matching */ * matching */
if (instFilename.str().length() >= it->length()) { if (source_file.length() >= it->length()) {
if (fnmatch(("*" + *it).c_str(), instFilename.str().c_str(), 0) == if (fnmatch(("*" + *it).c_str(), source_file.c_str(), 0) == 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());
return false;
}
}
}
}
}
#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 = denyListFiles.begin();
it != denyListFiles.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) {
return false; return false;
@ -468,12 +451,7 @@ bool isInInstrumentList(llvm::Function *F) {
} }
} } else {
}
#endif
else {
// we could not find out the location. in this case we say it is not // we could not find out the location. in this case we say it is not
// in the instrument file list // in the instrument file list
@ -528,37 +506,9 @@ bool isInInstrumentList(llvm::Function *F) {
if (!allowListFiles.empty()) { if (!allowListFiles.empty()) {
// let's try to get the filename for the function std::string source_file = getSourceName(F);
auto bb = &F->getEntryBlock();
BasicBlock::iterator IP = bb->getFirstInsertionPt();
IRBuilder<> IRB(&(*IP));
DebugLoc Loc = IP->getDebugLoc();
#if LLVM_VERSION_MAJOR >= 4 || \ if (!source_file.empty()) {
(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()) {
for (std::list<std::string>::iterator it = allowListFiles.begin(); for (std::list<std::string>::iterator it = allowListFiles.begin();
it != allowListFiles.end(); ++it) { it != allowListFiles.end(); ++it) {
@ -569,16 +519,15 @@ bool isInInstrumentList(llvm::Function *F) {
* specified in the list. We also allow UNIX-style pattern * specified in the list. We also allow UNIX-style pattern
* matching */ * matching */
if (instFilename.str().length() >= it->length()) { if (source_file.length() >= it->length()) {
if (fnmatch(("*" + *it).c_str(), instFilename.str().c_str(), 0) == if (fnmatch(("*" + *it).c_str(), source_file.c_str(), 0) == 0) {
0) {
if (debug) if (debug)
SAYF(cMGN "[D] " cRST SAYF(cMGN "[D] " cRST
"Function %s is in the allowlist (%s), " "Function %s is in the allowlist (%s), "
"instrumenting ... \n", "instrumenting ... \n",
F->getName().str().c_str(), instFilename.str().c_str()); F->getName().str().c_str(), source_file.c_str());
return true; return true;
} }
@ -587,50 +536,7 @@ bool isInInstrumentList(llvm::Function *F) {
} }
} } else {
}
#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) {
return true;
}
}
}
}
}
#endif
else {
// we could not find out the location. In this case we say it is not // we could not find out the location. In this case we say it is not
// in the instrument file list // in the instrument file list