-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[StackProtector] Introduce stack-protect-refinement pass to remove unnecessary protections. #150390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mermen
wants to merge
1
commit into
llvm:main
Choose a base branch
from
Mermen:stack-protect-attributor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
llvm/include/llvm/Transforms/Scalar/StackProtectRefinement.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===- StackProtectRefinement.h - 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Remove ssp attributes from functions where we can prove it is safe. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TRANSFORMS_SCALAR_STACKPROTECTREFINEMENT_H | ||
#define LLVM_TRANSFORMS_SCALAR_STACKPROTECTREFINEMENT_H | ||
|
||
#include "llvm/Analysis/StackSafetyAnalysis.h" | ||
#include "llvm/IR/PassManager.h" | ||
|
||
namespace llvm { | ||
|
||
class BasicBlock; | ||
class Function; | ||
class Instruction; | ||
|
||
class StackProtectRefinementPass | ||
: public PassInfoMixin<StackProtectRefinementPass> { | ||
public: | ||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM); | ||
|
||
private: | ||
void processFunction(Function &F) const; | ||
|
||
const StackSafetyGlobalInfo *SSI; | ||
}; | ||
} // end namespace llvm | ||
|
||
#endif // LLVM_TRANSFORMS_SCALAR_STACKPROTECTREFINEMENT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
NumFuncsWithAllocaInst++; | ||
return; | ||
} | ||
} | ||
} | ||
|
||
if (hasAlloca) { | ||
NumFuncsWithAllocaInst++; | ||
NumFuncsWithRemovedStackProtectAttr++; | ||
} | ||
|
||
F.removeFnAttr(Attribute::StackProtect); | ||
F.removeFnAttr(Attribute::StackProtectStrong); | ||
} | ||
|
||
PreservedAnalyses StackProtectRefinementPass::run(Module &M, | ||
ModuleAnalysisManager &MAM) { | ||
if (!UseStackSafety) | ||
return PreservedAnalyses::all(); | ||
|
||
SSI = &MAM.getResult<StackSafetyGlobalAnalysis>(M); | ||
for (Function &F : M) | ||
if (F.hasFnAttribute(Attribute::StackProtect) || | ||
F.hasFnAttribute(Attribute::StackProtectStrong)) | ||
processFunction(F); | ||
|
||
return PreservedAnalyses::all(); | ||
} |
2 changes: 1 addition & 1 deletion
2
llvm/test/CodeGen/AArch64/GlobalISel/dynamic-alloca-lifetime.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
llvm/test/Transforms/StackProtectAttributor/AArch64/stack-protector-refinement.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// RUN: clang -O2 -fstack-protector-strong -emit-llvm -S %s -o - | FileCheck %s | ||
|
||
__attribute__((noinline)) | ||
int foo1(int *p) { | ||
return p[10]; | ||
} | ||
int bar1() { | ||
// CHECK-LABEL: define {{[^@]+}}@bar1 | ||
// CHECK-SAME: () local_unnamed_addr #[[ATTR1:[0-9]+]] { | ||
int x[128]; | ||
return foo1(x); | ||
} | ||
|
||
__attribute__((noinline)) | ||
int foo2(int *p) { | ||
return p[1000]; | ||
} | ||
int bar2() { | ||
// CHECK-LABEL: define {{[^@]+}}@bar2 | ||
// CHECK-SAME: () local_unnamed_addr #[[ATTR2:[0-9]+]] { | ||
int x[128]; | ||
return foo2(x); | ||
} | ||
|
||
int k; | ||
__attribute__((noinline)) | ||
int foo3(int *p) { | ||
return p[k]; | ||
} | ||
int bar3() { | ||
// CHECK-LABEL: define {{[^@]+}}@bar3 | ||
// CHECK-SAME: () local_unnamed_addr #[[ATTR3:[0-9]+]] { | ||
int x[128]; | ||
return foo3(x); | ||
} | ||
|
||
__attribute__((noinline)) | ||
int foo4(int *p); | ||
int bar4() { | ||
// CHECK-LABEL: define {{[^@]+}}@bar4 | ||
// CHECK-SAME: () local_unnamed_addr #[[ATTR4:[0-9]+]] { | ||
int x[128]; | ||
return foo4(x); | ||
} | ||
|
||
int bar5() { | ||
// CHECK-LABEL: define {{[^@]+}}@bar5 | ||
// CHECK-SAME: () local_unnamed_addr #[[ATTR5:[0-9]+]] { | ||
int x[128]; | ||
int i; | ||
for (i = 0; i < 128; ++i) | ||
x[i] = i; | ||
return foo1(x); | ||
} | ||
|
||
//. | ||
// CHECK: attributes #[[ATTR1]] = | ||
// CHECK-NOT: sspstrong | ||
// CHECK: attributes #[[ATTR2]] = {{.* sspstrong}} | ||
// CHECK: attributes #[[ATTR3]] = {{.* sspstrong}} | ||
// CHECK: attributes #[[ATTR4]] = {{.* sspstrong}} | ||
// CHECK: attributes #[[ATTR5]] = | ||
// CHECK-NOT: sspstrong | ||
//. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.