[HWASan] Properly handle musttail calls.
authorMatt Morehouse <mascasa@google.com>
Tue, 1 Feb 2022 19:23:36 +0000 (11:23 -0800)
committerMatt Morehouse <mascasa@google.com>
Tue, 1 Feb 2022 19:23:43 +0000 (11:23 -0800)
Fixes a compile error when the `clang::musttail` attribute is used.

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D118712

compiler-rt/test/hwasan/TestCases/musttail.cpp [new file with mode: 0644]
llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp

diff --git a/compiler-rt/test/hwasan/TestCases/musttail.cpp b/compiler-rt/test/hwasan/TestCases/musttail.cpp
new file mode 100644 (file)
index 0000000..89b58e6
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clangxx_hwasan -mllvm -hwasan-use-stack-safety=0 %s -o %t
+// RUN: %run %t
+//
+// REQUIRES: pointer-tagging
+
+__attribute__((noinline)) int bar(int X) { return X; }
+
+__attribute__((noinline)) int foo(int X) {
+  volatile int A = 5;
+  [[clang::musttail]] return bar(X + A);
+}
+
+int main(int Argc, char *Argv[]) { return foo(Argc) != 6; }
index fb10a99..70b3944 100644 (file)
@@ -1531,9 +1531,14 @@ bool HWAddressSanitizer::sanitizeFunction(
         }
       }
 
-      if (isa<ReturnInst>(Inst) || isa<ResumeInst>(Inst) ||
-          isa<CleanupReturnInst>(Inst))
+      if (isa<ReturnInst>(Inst)) {
+        if (CallInst *CI = Inst.getParent()->getTerminatingMustTailCall())
+          RetVec.push_back(CI);
+        else
+          RetVec.push_back(&Inst);
+      } else if (isa<ResumeInst, CleanupReturnInst>(Inst)) {
         RetVec.push_back(&Inst);
+      }
 
       if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&Inst)) {
         for (Value *V : DVI->location_ops()) {