[llvm-commits] [llvm] r58791 - in /llvm/trunk: include/llvm/CodeGen/MachineFrameInfo.h include/llvm/Intrinsics.td lib/CodeGen/PrologEpilogInserter.cpp lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp lib/CodeGen/StackProtector.cpp
Bill Wendling
isanbard at gmail.com
Wed Nov 5 18:29:10 PST 2008
Author: void
Date: Wed Nov 5 20:29:10 2008
New Revision: 58791
URL: http://llvm.org/viewvc/llvm-project?rev=58791&view=rev
Log:
Implement the stack protector stack accesses via intrinsics:
- stackprotector_prologue creates a stack object and stores the guard there.
- stackprotector_epilogue reads the stack guard from the stack position created
by stackprotector_prologue.
- The PrologEpilogInserter was changed to make sure that the stack guard is
first on the stack frame.
Modified:
llvm/trunk/include/llvm/CodeGen/MachineFrameInfo.h
llvm/trunk/include/llvm/Intrinsics.td
llvm/trunk/lib/CodeGen/PrologEpilogInserter.cpp
llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
llvm/trunk/lib/CodeGen/StackProtector.cpp
Modified: llvm/trunk/include/llvm/CodeGen/MachineFrameInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/MachineFrameInfo.h?rev=58791&r1=58790&r2=58791&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/MachineFrameInfo.h (original)
+++ llvm/trunk/include/llvm/CodeGen/MachineFrameInfo.h Wed Nov 5 20:29:10 2008
@@ -150,6 +150,12 @@
/// only valid during and after prolog/epilog code insertion.
bool HasCalls;
+ /// HasStackProtector - Set to true if this function has stack protectors.
+ bool HasStackProtector;
+
+ /// StackProtectorIdx - The frame index for the stack protector.
+ int StackProtectorIdx;
+
/// MaxCallFrameSize - This contains the size of the largest call frame if the
/// target uses frame setup/destroy pseudo instructions (as defined in the
/// TargetFrameInfo class). This information is important for frame pointer
@@ -180,6 +186,8 @@
HasVarSizedObjects = false;
FrameAddressTaken = false;
HasCalls = false;
+ HasStackProtector = false;
+ StackProtectorIdx = -1;
MaxCallFrameSize = 0;
MMI = 0;
}
@@ -195,6 +203,17 @@
///
bool hasVarSizedObjects() const { return HasVarSizedObjects; }
+ /// hasStackProtector - Return true if the function has a stack protector.
+ ///
+ bool hasStackProtector() const { return HasStackProtector; }
+ void setStackProtector(bool T) { HasStackProtector = T; }
+
+ /// getStackProtectorIndex/setStackProtectorIndex - Return the index for the
+ /// stack protector object.
+ ///
+ int getStackProtectorIndex() const { return StackProtectorIdx; }
+ void setStackProtectorIndex(int I) { StackProtectorIdx = I; }
+
/// isFrameAddressTaken - This method may be called any time after instruction
/// selection is complete to determine if there is a call to
/// @llvm.frameaddress in this function.
Modified: llvm/trunk/include/llvm/Intrinsics.td
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Intrinsics.td?rev=58791&r1=58790&r2=58791&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Intrinsics.td (original)
+++ llvm/trunk/include/llvm/Intrinsics.td Wed Nov 5 20:29:10 2008
@@ -165,6 +165,7 @@
GCCBuiltin<"__builtin_stack_save">;
def int_stackrestore : Intrinsic<[llvm_void_ty, llvm_ptr_ty]>,
GCCBuiltin<"__builtin_stack_restore">;
+
// IntrWriteArgMem is more pessimistic than strictly necessary for prefetch,
// however it does conveniently prevent the prefetch from being reordered
// with respect to nearby accesses to the same memory.
@@ -175,6 +176,10 @@
def int_readcyclecounter : Intrinsic<[llvm_i64_ty]>;
+// Stack protector intrinsics.
+def int_stackprotector_prologue : Intrinsic<[llvm_void_ty, llvm_ptr_ty]>;
+def int_stackprotector_epilogue : Intrinsic<[llvm_ptr_ty]>;
+
//===------------------- Standard C Library Intrinsics --------------------===//
//
Modified: llvm/trunk/lib/CodeGen/PrologEpilogInserter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/PrologEpilogInserter.cpp?rev=58791&r1=58790&r2=58791&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/PrologEpilogInserter.cpp (original)
+++ llvm/trunk/lib/CodeGen/PrologEpilogInserter.cpp Wed Nov 5 20:29:10 2008
@@ -406,6 +406,33 @@
}
}
+ // Make sure that the stack protector comes before the local variables on the
+ // stack.
+ if (FFI->hasStackProtector()) {
+ int FI = FFI->getStackProtectorIndex();
+
+ // If stack grows down, we need to add size of find the lowest
+ // address of the object.
+ if (StackGrowsDown)
+ Offset += FFI->getObjectSize(FI);
+
+ unsigned Align = FFI->getObjectAlignment(FI);
+
+ // If the alignment of this object is greater than that of the stack, then
+ // increase the stack alignment to match.
+ MaxAlign = std::max(MaxAlign, Align);
+
+ // Adjust to alignment boundary.
+ Offset = (Offset + Align - 1) / Align * Align;
+
+ if (StackGrowsDown) {
+ FFI->setObjectOffset(FI, -Offset); // Set the computed offset
+ } else {
+ FFI->setObjectOffset(FI, Offset);
+ Offset += FFI->getObjectSize(FI);
+ }
+ }
+
// Then assign frame offsets to stack objects that are not used to spill
// callee saved registers.
for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp?rev=58791&r1=58790&r2=58791&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuild.cpp Wed Nov 5 20:29:10 2008
@@ -25,6 +25,7 @@
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/IntrinsicInst.h"
+#include "llvm/Module.h"
#include "llvm/CodeGen/FastISel.h"
#include "llvm/CodeGen/GCStrategy.h"
#include "llvm/CodeGen/GCMetadata.h"
@@ -34,6 +35,7 @@
#include "llvm/CodeGen/MachineJumpTableInfo.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/CodeGen/PseudoSourceValue.h"
#include "llvm/CodeGen/SelectionDAG.h"
#include "llvm/Target/TargetRegisterInfo.h"
#include "llvm/Target/TargetData.h"
@@ -3793,6 +3795,47 @@
DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
return 0;
}
+ case Intrinsic::stackprotector_prologue: {
+ // Emit code into the DAG to store the stack guard onto the stack.
+ MachineFunction &MF = DAG.getMachineFunction();
+ MachineFrameInfo *MFI = MF.getFrameInfo();
+ MVT PtrTy = TLI.getPointerTy();
+
+ // Retrieve the stack protector guard's value.
+ SDValue Src = getValue(I.getOperand(1));
+
+ // Create a slot on the stack for the stack protector. It should go first
+ // before local variables are allocated.
+ unsigned Align =
+ TLI.getTargetData()->getPrefTypeAlignment(PtrTy.getTypeForMVT());
+ int FI = MFI->CreateStackObject(PtrTy.getSizeInBits() / 8, Align);
+
+ MFI->setStackProtector(true);
+ MFI->setStackProtectorIndex(FI);
+
+ SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
+
+ // Store the stack protector onto the stack.
+ SDValue Result = DAG.getStore(getRoot(), Src, FIN,
+ PseudoSourceValue::getFixedStack(FI),
+ 0, true);
+ setValue(&I, Result);
+ DAG.setRoot(Result);
+ return 0;
+ }
+ case Intrinsic::stackprotector_epilogue: {
+ // Emit code into the DAG to retrieve the stack guard off of the stack.
+ MachineFunction &MF = DAG.getMachineFunction();
+ MachineFrameInfo *MFI = MF.getFrameInfo();
+ MVT PtrTy = TLI.getPointerTy();
+
+ // Load the value stored on the stack.
+ int FI = MFI->getStackProtectorIndex();
+ SDValue FIN = DAG.getFrameIndex(MFI->getStackProtectorIndex(), PtrTy);
+ setValue(&I, DAG.getLoad(PtrTy, getRoot(), FIN,
+ PseudoSourceValue::getFixedStack(FI), 0, true));
+ return 0;
+ }
case Intrinsic::var_annotation:
// Discard annotate attributes
return 0;
Modified: llvm/trunk/lib/CodeGen/StackProtector.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/StackProtector.cpp?rev=58791&r1=58790&r2=58791&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/StackProtector.cpp (original)
+++ llvm/trunk/lib/CodeGen/StackProtector.cpp Wed Nov 5 20:29:10 2008
@@ -20,6 +20,7 @@
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
+#include "llvm/Intrinsics.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/ADT/APInt.h"
@@ -110,16 +111,15 @@
// onto the stack.
BasicBlock &Entry = F->getEntryBlock();
Instruction *InsertPt = &Entry.front();
+
const PointerType *GuardTy = PointerType::getUnqual(Type::Int8Ty);
// The global variable for the stack guard.
Constant *StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", GuardTy);
-
- // The place on the stack that the stack protector guard is kept.
- AllocaInst *StackProtFrameSlot =
- new AllocaInst(GuardTy, "StackProt_Frame", InsertPt);
LoadInst *LI = new LoadInst(StackGuardVar, "StackGuard", false, InsertPt);
- new StoreInst(LI, StackProtFrameSlot, false, InsertPt);
+ CallInst::
+ Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_prologue),
+ LI, "", InsertPt);
// Create the basic block to jump to when the guard check fails.
BasicBlock *FailBB = CreateFailBB();
@@ -137,7 +137,7 @@
// %1 = load __stack_chk_guard
// %2 = load <stored stack guard>
// %3 = cmp i1 %1, %2
- // br i1 %3, label %SPRet, label %CallStackCheckFailBlk
+ // br i1 %3, label %SP_return, label %CallStackCheckFailBlk
//
// SP_return:
// ret ...
@@ -161,9 +161,11 @@
F->getBasicBlockList().insert(InsPt, NewBB);
// Generate the stack protector instructions in the old basic block.
- LoadInst *LI2 = new LoadInst(StackGuardVar, "", false, BB);
- LoadInst *LI1 = new LoadInst(StackProtFrameSlot, "", true, BB);
- ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, LI1, LI2, "", BB);
+ LoadInst *LI1 = new LoadInst(StackGuardVar, "", false, BB);
+ CallInst *CI = CallInst::
+ Create(Intrinsic::getDeclaration(M, Intrinsic::stackprotector_epilogue),
+ "", BB);
+ ICmpInst *Cmp = new ICmpInst(CmpInst::ICMP_EQ, CI, LI1, "", BB);
BranchInst::Create(NewBB, FailBB, Cmp, BB);
}
More information about the llvm-commits
mailing list