[llvm] [StackProtector] Introduce stack-protect-refinement pass to remove unnecessary protections. (PR #150390)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 1 13:02:58 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)) {
----------------
Mermen 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
Excellent point! Thank you for the tip, should be thought about carefully. But the problem is that SSP lowering pass (StackProtectorPass) is a functional pass, and StackSafetyGlobalAnalysis is modular analysis. If calling analysis (SSGA) from SSP lowering pass via ModuleAnalysisManagerFunctionProxy, it will cause break of the functional pass pipeline.
https://github.com/llvm/llvm-project/pull/150390
More information about the llvm-commits
mailing list