Skip to content

Pr43308 r373216 #15

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion llvm/include/llvm/CodeGen/StackProtector.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ class StackProtector : public FunctionPass {
bool InStruct = false) const;

/// Check whether a stack allocation has its address taken.
bool HasAddressTaken(const Instruction *AI);
bool HasAddressTaken(const Instruction *AI,
SmallPtrSetImpl<const PHINode *> &VisitedPHIs);

/// RequiresStackProtector - Check whether or not this function needs a
/// stack protector based upon the stack protector level.
Expand Down
46 changes: 42 additions & 4 deletions llvm/lib/CodeGen/StackProtector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/BranchProbabilityInfo.h"
#include "llvm/Analysis/CaptureTracking.h"
#include "llvm/Analysis/EHPersonalities.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/CodeGen/Passes.h"
Expand Down Expand Up @@ -157,6 +156,41 @@ bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge,
return NeedsProtector;
}

bool StackProtector::HasAddressTaken(const Instruction *AI,
SmallPtrSetImpl<const PHINode *> &VisitedPHIs) {
for (const User *U : AI->users()) {
if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
if (AI == SI->getValueOperand())
return true;
} else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) {
if (AI == SI->getOperand(0))
return true;
} else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
// Ignore intrinsics that are not calls. TODO: Use isLoweredToCall().
if (!isa<DbgInfoIntrinsic>(CI) && !CI->isLifetimeStartOrEnd())
return true;
} else if (isa<InvokeInst>(U)) {
return true;
} else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) {
if (HasAddressTaken(SI, VisitedPHIs))
return true;
} else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
// Keep track of what PHI nodes we have already visited to ensure
// they are only visited once.
if (VisitedPHIs.insert(PN).second)
if (HasAddressTaken(PN, VisitedPHIs))
return true;
} else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
if (HasAddressTaken(GEP, VisitedPHIs))
return true;
} else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
if (HasAddressTaken(BI, VisitedPHIs))
return true;
}
}
return false;
}

/// Search for the first call to the llvm.stackprotector intrinsic and return it
/// if present.
static const CallInst *findStackProtectorIntrinsic(Function &F) {
Expand Down Expand Up @@ -211,6 +245,12 @@ bool StackProtector::RequiresStackProtector() {
else if (!F->hasFnAttribute(Attribute::StackProtect))
return false;

/// VisitedPHIs - The set of PHI nodes visited when determining
/// if a variable's reference has been taken. This set
/// is maintained to ensure we don't visit the same PHI node multiple
/// times.
SmallPtrSet<const PHINode *, 16> VisitedPHIs;

for (const BasicBlock &BB : *F) {
for (const Instruction &I : BB) {
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
Expand Down Expand Up @@ -264,9 +304,7 @@ bool StackProtector::RequiresStackProtector() {
continue;
}

if (Strong && PointerMayBeCaptured(AI,
/* ReturnCaptures */ false,
/* StoreCaptures */ true)) {
if (Strong && HasAddressTaken(AI, VisitedPHIs)) {
++NumAddrTaken;
Layout.insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf));
ORE.emit([&]() {
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/X86/stack-protector.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4087,8 +4087,8 @@ define i32 @IgnoreIntrinsicTest() #1 {
%1 = alloca i32, align 4
%2 = bitcast i32* %1 to i8*
call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %2)
store i32 1, i32* %1, align 4
%3 = load i32, i32* %1, align 4
store volatile i32 1, i32* %1, align 4
%3 = load volatile i32, i32* %1, align 4
%4 = mul nsw i32 %3, 42
call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %2)
ret i32 %4
Expand Down
139 changes: 0 additions & 139 deletions llvm/test/Transforms/StackProtector/X86/captures.ll

This file was deleted.

2 changes: 0 additions & 2 deletions llvm/test/Transforms/StackProtector/X86/lit.local.cfg

This file was deleted.

1 change: 0 additions & 1 deletion llvm/tools/opt/opt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ int main(int argc, char **argv) {
initializeDwarfEHPreparePass(Registry);
initializeSafeStackLegacyPassPass(Registry);
initializeSjLjEHPreparePass(Registry);
initializeStackProtectorPass(Registry);
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
initializeGlobalMergePass(Registry);
initializeIndirectBrExpandPassPass(Registry);
Expand Down