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

Liqiang TAO via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 13 08:12:17 PDT 2023


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

The issue is caused by [](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.


>From 8ffcb7fe0043337c14bf5e4dafb08db5dd4bfdb8 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, 19 insertions(+), 9 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 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
+}



More information about the llvm-commits mailing list