msan: Handle musttail calls
authorReid Kleckner <reid@kleckner.net>
Tue, 12 Aug 2014 00:12:43 +0000 (00:12 +0000)
committerReid Kleckner <reid@kleckner.net>
Tue, 12 Aug 2014 00:12:43 +0000 (00:12 +0000)
First, avoid calling setTailCall(false) on musttail calls.  The funciton
prototypes should be "congruent", so the shadow layout should be exactly
the same.

Second, avoid inserting instrumentation after a musttail call to
propagate the return value shadow.  We don't need to propagate the
result of a tail call, it should already be in the right place.

Reviewed By: eugenis

Differential Revision: http://reviews.llvm.org/D4331

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215415 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Instrumentation/MemorySanitizer.cpp
test/Instrumentation/MemorySanitizer/msan_basic.ll

index 3df1d15741203f62d22105a34df2946768942ec5..643faacc70053a22eb3c9a4daee3750def12be8b 100644 (file)
@@ -2356,6 +2356,12 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
       VAHelper->visitCallSite(CS, IRB);
     }
 
+    // If this is a musttail call site, we can't insert propagation code here.
+    // The return type of the caller must match the callee, so the shadow should
+    // already be set up for an immediate return.
+    if (CS.isMustTailCall())
+      return;
+
     // Now, get the shadow for the RetVal.
     if (!I.getType()->isSized()) return;
     IRBuilder<> IRBBefore(&I);
@@ -2389,6 +2395,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
   }
 
   void visitReturnInst(ReturnInst &I) {
+    // Don't propagate shadow between musttail calls and the return.
+    if (I.getParent()->getTerminatingMustTailCall())
+      return;
+
     IRBuilder<> IRB(&I);
     Value *RetVal = I.getReturnValue();
     if (!RetVal) return;
index 0faf45d70c5930e8a26a73e18a8d5bbff844c288..27c23145e6103a33f6f5e04e355a08183c6a14ab 100644 (file)
@@ -878,3 +878,16 @@ define void @MismatchedReturnTypeTailCall(i32 %a) sanitize_memory {
 ; CHECK-LABEL: define void @MismatchedReturnTypeTailCall
 ; CHECK: tail call i32 @InnerTailCall
 ; CHECK: ret void
+
+declare i32 @InnerMustTailCall(i32 %a)
+
+define i32 @MustTailCall(i32 %a) {
+  %b = musttail call i32 @InnerMustTailCall(i32 %a)
+  ret i32 %b
+}
+
+; Test that 'musttail' is preserved.  The ABI should make this work.
+
+; CHECK-LABEL: define i32 @MustTailCall
+; CHECK: musttail call i32 @InnerMustTailCall
+; CHECK-NEXT: ret i32