[llvm] StackProtector: use isInTailCallPosition to verify tail call position (PR #68997)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 13 08:13:25 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Liqiang TAO (taoliq)

<details>
<summary>Changes</summary>

The issue is caused by [D133860](https://reviews.llvm.org/D133860).
The guard would be inserted in wrong place in some cases, like the test case showed below.
This patch fixed the issue by using `isInTailCallPosition()` to verify whether the tail call is in right position.


---
Full diff: https://github.com/llvm/llvm-project/pull/68997.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/StackProtector.cpp (+5-9) 
- (added) llvm/test/CodeGen/X86/tailcc-ssp2.ll (+14) 


``````````diff
diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index 387b653f8815367..8204425a350914d 100644
--- a/llvm/lib/CodeGen/StackProtector.cpp
+++ b/llvm/lib/CodeGen/StackProtector.cpp
@@ -20,6 +20,7 @@
 #include "llvm/Analysis/BranchProbabilityInfo.h"
 #include "llvm/Analysis/MemoryLocation.h"
 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
+#include "llvm/CodeGen/Analysis.h"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/TargetLowering.h"
 #include "llvm/CodeGen/TargetPassConfig.h"
@@ -520,17 +521,12 @@ bool StackProtector::InsertStackProtectors() {
     HasIRCheck = true;
 
     // If we're instrumenting a block with a tail call, the check has to be
-    // inserted before the call rather than between it and the return. The
-    // verifier guarantees that a tail call is either directly before the
-    // return or with a single correct bitcast of the return value in between so
-    // we don't need to worry about many situations here.
+    // inserted before the call rather than between it and the return.
     Instruction *Prev = CheckLoc->getPrevNonDebugInstruction();
-    if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall())
+    if (Prev && isa<CallInst>(Prev) &&
+        cast<CallInst>(Prev)->isTailCall() &&
+        isInTailCallPosition(*cast<CallInst>(Prev), *TM)) {
       CheckLoc = Prev;
-    else if (Prev) {
-      Prev = Prev->getPrevNonDebugInstruction();
-      if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall())
-        CheckLoc = Prev;
     }
 
     // Generate epilogue instrumentation. The epilogue intrumentation can be
diff --git a/llvm/test/CodeGen/X86/tailcc-ssp2.ll b/llvm/test/CodeGen/X86/tailcc-ssp2.ll
new file mode 100644
index 000000000000000..af6ddaecae2032b
--- /dev/null
+++ b/llvm/test/CodeGen/X86/tailcc-ssp2.ll
@@ -0,0 +1,14 @@
+; RUN: llc -mtriple=x86_64-linux-gnu %s -o - 2>&1 | FileCheck %s
+
+declare void @callee()
+define void @caller() sspreq {
+; CHECK: callq   callee at PLT
+; CHECK: callq   callee at PLT
+; CHECK: cmpq
+; CHECK: jne
+; CHECK: callq   __stack_chk_fail at PLT
+
+  tail call void @callee()
+  call void @callee()
+  ret void
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/68997


More information about the llvm-commits mailing list