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

Liqiang TAO via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 30 03:49:59 PDT 2023


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

>From 97602f0972e620259d85420e75a402f228c201fe Mon Sep 17 00:00:00 2001
From: Liqiang Tao <taolq at outlook.com>
Date: Fri, 13 Oct 2023 21:33:46 +0800
Subject: [PATCH] use isInTailCallPosition to check tail call position

---
 llvm/lib/CodeGen/StackProtector.cpp  | 14 ++++----------
 llvm/test/CodeGen/X86/tailcc-ssp2.ll | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 10 deletions(-)
 create mode 100644 llvm/test/CodeGen/X86/tailcc-ssp2.ll

diff --git a/llvm/lib/CodeGen/StackProtector.cpp b/llvm/lib/CodeGen/StackProtector.cpp
index 3ba85bc125a0190..85dcd340104cd9e 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"
@@ -519,18 +520,11 @@ 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())
-      CheckLoc = Prev;
-    else if (Prev) {
-      Prev = Prev->getPrevNonDebugInstruction();
-      if (Prev && isa<CallInst>(Prev) && cast<CallInst>(Prev)->isTailCall())
+    if (auto *CI = dyn_cast_if_present<CallInst>(Prev))
+      if (CI->isTailCall() && isInTailCallPosition(*CI, *TM))
         CheckLoc = Prev;
-    }
 
     // Generate epilogue instrumentation. The epilogue intrumentation can be
     // function-based or inlined depending on which mechanism the target is
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
+}



More information about the llvm-commits mailing list