[llvm] r266809 - [PPC, SSP] Support PowerPC Linux stack protection.
Marcin KoĆcielnicki via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 19 13:40:07 PDT 2016
Patch is incorrect on PPC32, see inline comments.
On 19/04/16 22:14, Tim Shen via llvm-commits wrote:
> Author: timshen
> Date: Tue Apr 19 15:14:52 2016
> New Revision: 266809
>
> URL: http://llvm.org/viewvc/llvm-project?rev=266809&view=rev
> Log:
> [PPC, SSP] Support PowerPC Linux stack protection.
>
> Modified:
> llvm/trunk/include/llvm/Target/TargetLowering.h
> llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
> llvm/trunk/lib/CodeGen/TargetLoweringBase.cpp
> llvm/trunk/lib/Target/PowerPC/PPCISelLowering.cpp
> llvm/trunk/lib/Target/PowerPC/PPCISelLowering.h
> llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.cpp
> llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.h
> llvm/trunk/lib/Target/PowerPC/PPCSubtarget.h
> llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
> llvm/trunk/lib/Target/X86/X86ISelLowering.h
> llvm/trunk/test/CodeGen/PowerPC/stack-protector.ll
>
> Modified: llvm/trunk/include/llvm/Target/TargetLowering.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetLowering.h?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/include/llvm/Target/TargetLowering.h (original)
> +++ llvm/trunk/include/llvm/Target/TargetLowering.h Tue Apr 19 15:14:52 2016
> @@ -1024,7 +1024,7 @@ public:
> /// Return the variable that's previously inserted by insertSSPDeclarations,
> /// if any, otherwise return nullptr. Should be used only when
> /// getIRStackGuard returns nullptr.
> - virtual Value *getSDStackGuard(const Module &M) const;
> + virtual Value *getSDagStackGuard(const Module &M) const;
>
> /// If the target has a standard location for the unsafe stack pointer,
> /// returns the address of that location. Otherwise, returns nullptr.
>
> Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (original)
> +++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp Tue Apr 19 15:14:52 2016
> @@ -2004,7 +2004,7 @@ static SDValue getLoadStackGuard(Selecti
> const TargetLowering &TLI = DAG.getTargetLoweringInfo();
> EVT PtrTy = TLI.getPointerTy(DAG.getDataLayout());
> MachineFunction &MF = DAG.getMachineFunction();
> - Value *Global = TLI.getSDStackGuard(*MF.getFunction()->getParent());
> + Value *Global = TLI.getSDagStackGuard(*MF.getFunction()->getParent());
> MachineSDNode *Node =
> DAG.getMachineNode(TargetOpcode::LOAD_STACK_GUARD, DL, PtrTy, Chain);
> if (Global) {
> @@ -2034,27 +2034,25 @@ void SelectionDAGBuilder::visitSPDescrip
> MachineFrameInfo *MFI = ParentBB->getParent()->getFrameInfo();
> int FI = MFI->getStackProtectorIndex();
>
> - const Module &M = *ParentBB->getParent()->getFunction()->getParent();
> - const Value *IRGuard = TLI.getSDStackGuard(M);
> - assert(IRGuard && "Currently there must be an IR guard in order to use "
> - "SelectionDAG SSP");
> - SDValue GuardPtr = getValue(IRGuard);
> - SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
> -
> - unsigned Align = DL->getPrefTypeAlignment(IRGuard->getType());
> -
> SDValue Guard;
> SDLoc dl = getCurSDLoc();
> + SDValue StackSlotPtr = DAG.getFrameIndex(FI, PtrTy);
> + const Module &M = *ParentBB->getParent()->getFunction()->getParent();
> + unsigned Align = DL->getPrefTypeAlignment(Type::getInt8PtrTy(M.getContext()));
>
> // If useLoadStackGuardNode returns true, generate LOAD_STACK_GUARD.
> // Otherwise, emit a volatile load to retrieve the stack guard value.
> SDValue Chain = DAG.getEntryNode();
> - if (TLI.useLoadStackGuardNode())
> + if (TLI.useLoadStackGuardNode()) {
> Guard = getLoadStackGuard(DAG, dl, Chain);
> - else
> + } else {
> + const Value *IRGuard = TLI.getSDagStackGuard(M);
> + SDValue GuardPtr = getValue(IRGuard);
> +
> Guard =
> DAG.getLoad(PtrTy, dl, Chain, GuardPtr, MachinePointerInfo(IRGuard, 0),
> true, false, false, Align);
> + }
>
> SDValue StackSlot = DAG.getLoad(
> PtrTy, dl, DAG.getEntryNode(), StackSlotPtr,
> @@ -5313,7 +5311,7 @@ SelectionDAGBuilder::visitIntrinsicCall(
> if (TLI.useLoadStackGuardNode()) {
> Res = getLoadStackGuard(DAG, sdl, Chain);
> } else {
> - const Value *Global = TLI.getSDStackGuard(M);
> + const Value *Global = TLI.getSDagStackGuard(M);
> unsigned Align = DL->getPrefTypeAlignment(Global->getType());
> Res =
> DAG.getLoad(PtrTy, sdl, Chain, getValue(Global),
>
> Modified: llvm/trunk/lib/CodeGen/TargetLoweringBase.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/TargetLoweringBase.cpp?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/TargetLoweringBase.cpp (original)
> +++ llvm/trunk/lib/CodeGen/TargetLoweringBase.cpp Tue Apr 19 15:14:52 2016
> @@ -1814,6 +1814,6 @@ void TargetLoweringBase::insertSSPDeclar
>
> // Currently only support "standard" __stack_chk_guard.
> // TODO: add LOAD_STACK_GUARD support.
> -Value *TargetLoweringBase::getSDStackGuard(const Module &M) const {
> +Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
> return M.getGlobalVariable("__stack_chk_guard");
> }
>
> Modified: llvm/trunk/lib/Target/PowerPC/PPCISelLowering.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCISelLowering.cpp?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/PowerPC/PPCISelLowering.cpp (original)
> +++ llvm/trunk/lib/Target/PowerPC/PPCISelLowering.cpp Tue Apr 19 15:14:52 2016
> @@ -12013,3 +12013,16 @@ void PPCTargetLowering::insertCopiesSpli
> .addReg(NewVR);
> }
> }
> +
> +// Override to enable LOAD_STACK_GUARD lowering on Linux.
> +bool PPCTargetLowering::useLoadStackGuardNode() const {
> + if (!Subtarget.isTargetLinux())
> + return TargetLowering::useLoadStackGuardNode();
> + return true;
> +}
> +
> +// Override to disable global variable loading on Linux.
> +void PPCTargetLowering::insertSSPDeclarations(Module &M) const {
> + if (!Subtarget.isTargetLinux())
> + return TargetLowering::insertSSPDeclarations(M);
> +}
>
> Modified: llvm/trunk/lib/Target/PowerPC/PPCISelLowering.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCISelLowering.h?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/PowerPC/PPCISelLowering.h (original)
> +++ llvm/trunk/lib/Target/PowerPC/PPCISelLowering.h Tue Apr 19 15:14:52 2016
> @@ -688,6 +688,10 @@ namespace llvm {
> unsigned
> getExceptionSelectorRegister(const Constant *PersonalityFn) const override;
>
> + /// Override to support customized stack guard loading.
> + bool useLoadStackGuardNode() const override;
> + void insertSSPDeclarations(Module &M) const override;
> +
> private:
> struct ReuseLoadInfo {
> SDValue Ptr;
>
> Modified: llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.cpp?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.cpp (original)
> +++ llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.cpp Tue Apr 19 15:14:52 2016
> @@ -1857,3 +1857,19 @@ PPCInstrInfo::getSerializableBitmaskMach
> return makeArrayRef(TargetFlags);
> }
>
> +bool PPCInstrInfo::expandPostRAPseudo(MachineBasicBlock::iterator MI) const {
> + switch (MI->getOpcode()) {
> + case TargetOpcode::LOAD_STACK_GUARD: {
> + assert(Subtarget.isTargetLinux() &&
> + "Only Linux target is expected to contain LOAD_STACK_GUARD");
> + const int64_t Offset = Subtarget.isPPC64() ? -0x7010 : -0x7008;
> + const unsigned Reg = Subtarget.isPPC64() ? PPC::X13 : PPC::R2;
> + MI->setDesc(get(PPC::LD));
This should be LWZ for 32-bit PPC.
> + MachineInstrBuilder(*MI->getParent()->getParent(), MI)
> + .addImm(Offset)
> + .addReg(Reg);
> + return true;
> + }
> + }
> + return false;
> +}
>
> Modified: llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.h?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.h (original)
> +++ llvm/trunk/lib/Target/PowerPC/PPCInstrInfo.h Tue Apr 19 15:14:52 2016
> @@ -272,6 +272,9 @@ public:
>
> ArrayRef<std::pair<unsigned, const char *>>
> getSerializableBitmaskMachineOperandTargetFlags() const override;
> +
> + // Lower pseudo instructions after register allocation.
> + bool expandPostRAPseudo(MachineBasicBlock::iterator MI) const override;
> };
>
> }
>
> Modified: llvm/trunk/lib/Target/PowerPC/PPCSubtarget.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/PowerPC/PPCSubtarget.h?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/PowerPC/PPCSubtarget.h (original)
> +++ llvm/trunk/lib/Target/PowerPC/PPCSubtarget.h Tue Apr 19 15:14:52 2016
> @@ -286,6 +286,7 @@ public:
>
> bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); }
> bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); }
> + bool isTargetLinux() const { return TargetTriple.isOSLinux(); }
>
> bool isDarwinABI() const { return isTargetMachO() || isDarwin(); }
> bool isSVR4ABI() const { return !isDarwinABI(); }
>
> Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.cpp?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp (original)
> +++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp Tue Apr 19 15:14:52 2016
> @@ -2018,12 +2018,6 @@ void X86TargetLowering::insertSSPDeclara
> TargetLowering::insertSSPDeclarations(M);
> }
>
> -Value *X86TargetLowering::getSDStackGuard(const Module &M) const {
> - if (!Subtarget.isTargetLinux())
> - return TargetLowering::getSDStackGuard(M);
> - return nullptr;
> -}
> -
> Value *X86TargetLowering::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
> if (!Subtarget.isTargetAndroid())
> return TargetLowering::getSafeStackPointerLocation(IRB);
>
> Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.h?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/X86/X86ISelLowering.h (original)
> +++ llvm/trunk/lib/Target/X86/X86ISelLowering.h Tue Apr 19 15:14:52 2016
> @@ -966,8 +966,6 @@ namespace llvm {
>
> void insertSSPDeclarations(Module &M) const override;
>
> - Value *getSDStackGuard(const Module &M) const override;
> -
> /// Return true if the target stores SafeStack pointer at a fixed offset in
> /// some non-standard address space, and populates the address space and
> /// offset as appropriate.
>
> Modified: llvm/trunk/test/CodeGen/PowerPC/stack-protector.ll
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/PowerPC/stack-protector.ll?rev=266809&r1=266808&r2=266809&view=diff
> ==============================================================================
> --- llvm/trunk/test/CodeGen/PowerPC/stack-protector.ll (original)
> +++ llvm/trunk/test/CodeGen/PowerPC/stack-protector.ll Tue Apr 19 15:14:52 2016
> @@ -1,6 +1,17 @@
> -; RUN: llc -march=ppc32 -mtriple=ppc32-unknown-linux < %s | FileCheck %s
> -; CHECK: __stack_chk_guard
> -; CHECK: __stack_chk_fail
> +; RUN: llc -mtriple=powerpc-apple-darwin8 < %s | FileCheck -check-prefix=DARWIN32 %s
> +; RUN: llc -mtriple=powerpc64-apple-darwin < %s | FileCheck -check-prefix=DARWIN64 %s
> +; RUN: llc -mtriple=ppc32-unknown-linux < %s | FileCheck -check-prefix=LINUX32 %s
> +; RUN: llc -mtriple=powerpc64le-unknown-linux < %s | FileCheck -check-prefix=LINUX64 %s
> +
> +; DARWIN32: __stack_chk_guard
> +; DARWIN64: __stack_chk_guard
> +; LINUX32: ld {{[0-9]+}}, -28680(2)
Likewise.
> +; LINUX64: ld {{[0-9]+}}, -28688(13)
> +
> +; DARWIN32: __stack_chk_fail
> +; DARWIN64: __stack_chk_fail
> +; LINUX32: __stack_chk_fail
> +; LINUX64: __stack_chk_fail
>
> @"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00" ; <[11 x i8]*> [#uses=1]
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
More information about the llvm-commits
mailing list