[llvm] [StackProtector] Introduce stack-protect-refinement pass to remove unnecessary protections. (PR #150390)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 7 23:05:31 PDT 2025
================
@@ -0,0 +1,64 @@
+//===- StackProtectRefinement.cpp - Stack Protect Refinement --------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Scalar/StackProtectRefinement.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/IR/InstIterator.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/Support/CommandLine.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "stack-protect-refinement"
+
+STATISTIC(
+ NumFuncsWithAllocaInst,
+ "Number of functions with an instruction to allocate memory on the stack");
+STATISTIC(NumFuncsWithRemovedStackProtectAttr,
+ "Number of functions with alloca and removed stack protect attr");
+
+static cl::opt<bool>
+ UseStackSafety("optimize-ssp", cl::init(true), cl::Hidden,
+ cl::desc("Use Stack Safety analysis results"));
+
+void StackProtectRefinementPass::processFunction(Function &F) const {
+
+ bool hasAlloca = false;
+
+ for (auto &I : instructions(&F)) {
+ if (auto *AI = dyn_cast<AllocaInst>(&I)) {
+ hasAlloca = true;
+ if (!SSI->isSafe(*AI)) {
----------------
arsenm wrote:
If this is all the pass is doing, should this just be done directly in the SSP lowering pass?
Stripping the attribute seems potentially dangerous. Suppose a later pass chooses to introduce a new unsafe alloca, which will now no longer be appropriately processed during lowering
https://github.com/llvm/llvm-project/pull/150390
More information about the llvm-commits
mailing list