[MSan] Introduce ActualFnStart. NFC

This is a step towards the upcoming KMSAN implementation patch.
KMSAN is going to prepend a special basic block containing
tool-specific calls to each function. Because we still want to
instrument the original entry block, we'll need to store it in
ActualFnStart.

For MSan this will still be F.getEntryBlock(), whereas for KMSAN
it'll contain the second BB.

llvm-svn: 328697
This commit is contained in:
Alexander Potapenko 2018-03-28 11:35:09 +00:00
parent fd11560f6e
commit 4e7ad0805e
1 changed files with 10 additions and 8 deletions

View File

@ -715,6 +715,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
ValueMap<Value*, Value*> ShadowMap, OriginMap; ValueMap<Value*, Value*> ShadowMap, OriginMap;
std::unique_ptr<VarArgHelper> VAHelper; std::unique_ptr<VarArgHelper> VAHelper;
const TargetLibraryInfo *TLI; const TargetLibraryInfo *TLI;
BasicBlock *ActualFnStart;
// The following flags disable parts of MSan instrumentation based on // The following flags disable parts of MSan instrumentation based on
// blacklist contents and command-line options. // blacklist contents and command-line options.
@ -747,6 +748,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
CheckReturnValue = SanitizeFunction && (F.getName() == "main"); CheckReturnValue = SanitizeFunction && (F.getName() == "main");
TLI = &MS.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); TLI = &MS.getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
MS.initializeCallbacks(*F.getParent());
ActualFnStart = &F.getEntryBlock();
DEBUG(if (!InsertChecks) DEBUG(if (!InsertChecks)
dbgs() << "MemorySanitizer is not inserting checks into '" dbgs() << "MemorySanitizer is not inserting checks into '"
<< F.getName() << "'\n"); << F.getName() << "'\n");
@ -931,8 +935,6 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
/// \brief Add MemorySanitizer instrumentation to a function. /// \brief Add MemorySanitizer instrumentation to a function.
bool runOnFunction() { bool runOnFunction() {
MS.initializeCallbacks(*F.getParent());
// In the presence of unreachable blocks, we may see Phi nodes with // In the presence of unreachable blocks, we may see Phi nodes with
// incoming nodes from such blocks. Since InstVisitor skips unreachable // incoming nodes from such blocks. Since InstVisitor skips unreachable
// blocks, such nodes will not have any shadow value associated with them. // blocks, such nodes will not have any shadow value associated with them.
@ -942,7 +944,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
// Iterate all BBs in depth-first order and create shadow instructions // Iterate all BBs in depth-first order and create shadow instructions
// for all instructions (where applicable). // for all instructions (where applicable).
// For PHI nodes we create dummy shadow PHIs which will be finalized later. // For PHI nodes we create dummy shadow PHIs which will be finalized later.
for (BasicBlock *BB : depth_first(&F.getEntryBlock())) for (BasicBlock *BB : depth_first(ActualFnStart))
visit(*BB); visit(*BB);
// Finalize PHI nodes. // Finalize PHI nodes.
@ -1216,7 +1218,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
if (*ShadowPtr) if (*ShadowPtr)
return *ShadowPtr; return *ShadowPtr;
Function *F = A->getParent(); Function *F = A->getParent();
IRBuilder<> EntryIRB(F->getEntryBlock().getFirstNonPHI()); IRBuilder<> EntryIRB(ActualFnStart->getFirstNonPHI());
unsigned ArgOffset = 0; unsigned ArgOffset = 0;
const DataLayout &DL = F->getParent()->getDataLayout(); const DataLayout &DL = F->getParent()->getDataLayout();
for (auto &FArg : F->args()) { for (auto &FArg : F->args()) {
@ -3214,7 +3216,7 @@ struct VarArgAMD64Helper : public VarArgHelper {
if (!VAStartInstrumentationList.empty()) { if (!VAStartInstrumentationList.empty()) {
// If there is a va_start in this function, make a backup copy of // If there is a va_start in this function, make a backup copy of
// va_arg_tls somewhere in the function entry block. // va_arg_tls somewhere in the function entry block.
IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI()); IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS); VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
Value *CopySize = Value *CopySize =
IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset), IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset),
@ -3336,7 +3338,7 @@ struct VarArgMIPS64Helper : public VarArgHelper {
void finalizeInstrumentation() override { void finalizeInstrumentation() override {
assert(!VAArgSize && !VAArgTLSCopy && assert(!VAArgSize && !VAArgTLSCopy &&
"finalizeInstrumentation called twice"); "finalizeInstrumentation called twice");
IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI()); IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
VAArgSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS); VAArgSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0), Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
VAArgSize); VAArgSize);
@ -3521,7 +3523,7 @@ struct VarArgAArch64Helper : public VarArgHelper {
if (!VAStartInstrumentationList.empty()) { if (!VAStartInstrumentationList.empty()) {
// If there is a va_start in this function, make a backup copy of // If there is a va_start in this function, make a backup copy of
// va_arg_tls somewhere in the function entry block. // va_arg_tls somewhere in the function entry block.
IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI()); IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS); VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
Value *CopySize = Value *CopySize =
IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AArch64VAEndOffset), IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AArch64VAEndOffset),
@ -3757,7 +3759,7 @@ struct VarArgPowerPC64Helper : public VarArgHelper {
void finalizeInstrumentation() override { void finalizeInstrumentation() override {
assert(!VAArgSize && !VAArgTLSCopy && assert(!VAArgSize && !VAArgTLSCopy &&
"finalizeInstrumentation called twice"); "finalizeInstrumentation called twice");
IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI()); IRBuilder<> IRB(MSV.ActualFnStart->getFirstNonPHI());
VAArgSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS); VAArgSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0), Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
VAArgSize); VAArgSize);