Skip to content

Commit c1d76f4

Browse files
pogo59tstellar
authored andcommitted
Merging r373216:
------------------------------------------------------------------------ r373216 | probinson | 2019-09-30 08:01:35 -0700 (Mon, 30 Sep 2019) | 7 lines [SSP] [1/3] Revert "StackProtector: Use PointerMayBeCaptured" "Captured" and "relevant to Stack Protector" are not the same thing. This reverts commit f29366b. aka r363169. Differential Revision: https://reviews.llvm.org/D67842 ------------------------------------------------------------------------ To avoid changing the ABI, the VisitedPHIs member from the StackProtector class was replaced with a local variable in StackProtector::RequiresStackProtector().
1 parent 6d7bc60 commit c1d76f4

File tree

6 files changed

+46
-149
lines changed

6 files changed

+46
-149
lines changed

llvm/include/llvm/CodeGen/StackProtector.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ class StackProtector : public FunctionPass {
8989
bool InStruct = false) const;
9090

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

9495
/// RequiresStackProtector - Check whether or not this function needs a
9596
/// stack protector based upon the stack protector level.

llvm/lib/CodeGen/StackProtector.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "llvm/ADT/SmallPtrSet.h"
1818
#include "llvm/ADT/Statistic.h"
1919
#include "llvm/Analysis/BranchProbabilityInfo.h"
20-
#include "llvm/Analysis/CaptureTracking.h"
2120
#include "llvm/Analysis/EHPersonalities.h"
2221
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
2322
#include "llvm/CodeGen/Passes.h"
@@ -157,6 +156,41 @@ bool StackProtector::ContainsProtectableArray(Type *Ty, bool &IsLarge,
157156
return NeedsProtector;
158157
}
159158

159+
bool StackProtector::HasAddressTaken(const Instruction *AI,
160+
SmallPtrSetImpl<const PHINode *> &VisitedPHIs) {
161+
for (const User *U : AI->users()) {
162+
if (const StoreInst *SI = dyn_cast<StoreInst>(U)) {
163+
if (AI == SI->getValueOperand())
164+
return true;
165+
} else if (const PtrToIntInst *SI = dyn_cast<PtrToIntInst>(U)) {
166+
if (AI == SI->getOperand(0))
167+
return true;
168+
} else if (const CallInst *CI = dyn_cast<CallInst>(U)) {
169+
// Ignore intrinsics that are not calls. TODO: Use isLoweredToCall().
170+
if (!isa<DbgInfoIntrinsic>(CI) && !CI->isLifetimeStartOrEnd())
171+
return true;
172+
} else if (isa<InvokeInst>(U)) {
173+
return true;
174+
} else if (const SelectInst *SI = dyn_cast<SelectInst>(U)) {
175+
if (HasAddressTaken(SI, VisitedPHIs))
176+
return true;
177+
} else if (const PHINode *PN = dyn_cast<PHINode>(U)) {
178+
// Keep track of what PHI nodes we have already visited to ensure
179+
// they are only visited once.
180+
if (VisitedPHIs.insert(PN).second)
181+
if (HasAddressTaken(PN, VisitedPHIs))
182+
return true;
183+
} else if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(U)) {
184+
if (HasAddressTaken(GEP, VisitedPHIs))
185+
return true;
186+
} else if (const BitCastInst *BI = dyn_cast<BitCastInst>(U)) {
187+
if (HasAddressTaken(BI, VisitedPHIs))
188+
return true;
189+
}
190+
}
191+
return false;
192+
}
193+
160194
/// Search for the first call to the llvm.stackprotector intrinsic and return it
161195
/// if present.
162196
static const CallInst *findStackProtectorIntrinsic(Function &F) {
@@ -211,6 +245,12 @@ bool StackProtector::RequiresStackProtector() {
211245
else if (!F->hasFnAttribute(Attribute::StackProtect))
212246
return false;
213247

248+
/// VisitedPHIs - The set of PHI nodes visited when determining
249+
/// if a variable's reference has been taken. This set
250+
/// is maintained to ensure we don't visit the same PHI node multiple
251+
/// times.
252+
SmallPtrSet<const PHINode *, 16> VisitedPHIs;
253+
214254
for (const BasicBlock &BB : *F) {
215255
for (const Instruction &I : BB) {
216256
if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
@@ -264,9 +304,7 @@ bool StackProtector::RequiresStackProtector() {
264304
continue;
265305
}
266306

267-
if (Strong && PointerMayBeCaptured(AI,
268-
/* ReturnCaptures */ false,
269-
/* StoreCaptures */ true)) {
307+
if (Strong && HasAddressTaken(AI, VisitedPHIs)) {
270308
++NumAddrTaken;
271309
Layout.insert(std::make_pair(AI, MachineFrameInfo::SSPLK_AddrOf));
272310
ORE.emit([&]() {

llvm/test/CodeGen/X86/stack-protector.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4087,8 +4087,8 @@ define i32 @IgnoreIntrinsicTest() #1 {
40874087
%1 = alloca i32, align 4
40884088
%2 = bitcast i32* %1 to i8*
40894089
call void @llvm.lifetime.start.p0i8(i64 4, i8* nonnull %2)
4090-
store i32 1, i32* %1, align 4
4091-
%3 = load i32, i32* %1, align 4
4090+
store volatile i32 1, i32* %1, align 4
4091+
%3 = load volatile i32, i32* %1, align 4
40924092
%4 = mul nsw i32 %3, 42
40934093
call void @llvm.lifetime.end.p0i8(i64 4, i8* nonnull %2)
40944094
ret i32 %4

llvm/test/Transforms/StackProtector/X86/captures.ll

Lines changed: 0 additions & 139 deletions
This file was deleted.

llvm/test/Transforms/StackProtector/X86/lit.local.cfg

Lines changed: 0 additions & 2 deletions
This file was deleted.

llvm/tools/opt/opt.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ int main(int argc, char **argv) {
523523
initializeDwarfEHPreparePass(Registry);
524524
initializeSafeStackLegacyPassPass(Registry);
525525
initializeSjLjEHPreparePass(Registry);
526-
initializeStackProtectorPass(Registry);
527526
initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
528527
initializeGlobalMergePass(Registry);
529528
initializeIndirectBrExpandPassPass(Registry);

0 commit comments

Comments
 (0)