diff options
author | Pengfei Wang <pengfei.wang@intel.com> | 2019-05-22 00:50:21 +0000 |
---|---|---|
committer | Pengfei Wang <pengfei.wang@intel.com> | 2019-05-22 00:50:21 +0000 |
commit | 6a0d432e9e0f86b6659b4082b6053011d5fe5101 (patch) | |
tree | 6591a599cd9059e26d044efad39db83ad99d9b2f /llvm/lib/Target/X86/X86IndirectBranchTracking.cpp | |
parent | 587d4b06205f10bb970489a2fcf7de620e8c0ad4 (diff) | |
download | bcm5719-llvm-6a0d432e9e0f86b6659b4082b6053011d5fe5101.tar.gz bcm5719-llvm-6a0d432e9e0f86b6659b4082b6053011d5fe5101.zip |
[X86] [CET] Deal with return-twice function such as vfork, setjmp when
CET-IBT enabled
Return-twice functions will indirectly jump after the caller's position.
So when CET-IBT is enable, we should make sure these is endbr*
instructions follow these Return-twice function caller. Like GCC does.
Patch by Xiang Zhang (xiangzhangllvm)
Differential Revision: https://reviews.llvm.org/D61881
llvm-svn: 361342
Diffstat (limited to 'llvm/lib/Target/X86/X86IndirectBranchTracking.cpp')
-rw-r--r-- | llvm/lib/Target/X86/X86IndirectBranchTracking.cpp | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp b/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp index 35866d74edb..04e8b2231fe 100644 --- a/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp +++ b/llvm/lib/Target/X86/X86IndirectBranchTracking.cpp @@ -57,7 +57,7 @@ private: /// The function will not add it if already exists. /// It will add ENDBR32 or ENDBR64 opcode, depending on the target. /// \returns true if the ENDBR was added and false otherwise. - bool addENDBR(MachineBasicBlock &MBB) const; + bool addENDBR(MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const; }; } // end anonymous namespace @@ -68,20 +68,31 @@ FunctionPass *llvm::createX86IndirectBranchTrackingPass() { return new X86IndirectBranchTrackingPass(); } -bool X86IndirectBranchTrackingPass::addENDBR(MachineBasicBlock &MBB) const { +bool X86IndirectBranchTrackingPass::addENDBR( + MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const { assert(TII && "Target instruction info was not initialized"); assert((X86::ENDBR64 == EndbrOpcode || X86::ENDBR32 == EndbrOpcode) && "Unexpected Endbr opcode"); - auto MI = MBB.begin(); - // If the MBB is empty or the first instruction is not ENDBR, - // add the ENDBR instruction to the beginning of the MBB. - if (MI == MBB.end() || EndbrOpcode != MI->getOpcode()) { - BuildMI(MBB, MI, MBB.findDebugLoc(MI), TII->get(EndbrOpcode)); - NumEndBranchAdded++; + // If the MBB/I is empty or the current instruction is not ENDBR, + // insert ENDBR instruction to the location of I. + if (I == MBB.end() || I->getOpcode() != EndbrOpcode) { + BuildMI(MBB, I, MBB.findDebugLoc(I), TII->get(EndbrOpcode)); + ++NumEndBranchAdded; return true; } + return false; +} +bool IsCallReturnTwice(llvm::MachineOperand &MOp) { + if (!MOp.isGlobal()) + return false; + auto *CalleeFn = dyn_cast<Function>(MOp.getGlobal()); + if (!CalleeFn) + return false; + AttributeList Attrs = CalleeFn->getAttributes(); + if (Attrs.hasAttribute(AttributeList::FunctionIndex, Attribute::ReturnsTwice)) + return true; return false; } @@ -107,14 +118,21 @@ bool X86IndirectBranchTrackingPass::runOnMachineFunction(MachineFunction &MF) { !MF.getFunction().hasLocalLinkage()) && !MF.getFunction().doesNoCfCheck()) { auto MBB = MF.begin(); - Changed |= addENDBR(*MBB); + Changed |= addENDBR(*MBB, MBB->begin()); } - for (auto &MBB : MF) + for (auto &MBB : MF) { // Find all basic blocks that their address was taken (for example // in the case of indirect jump) and add ENDBR instruction. if (MBB.hasAddressTaken()) - Changed |= addENDBR(MBB); - + Changed |= addENDBR(MBB, MBB.begin()); + + for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) { + if (!I->isCall()) + continue; + if (IsCallReturnTwice(I->getOperand(0))) + Changed |= addENDBR(MBB, std::next(I)); + } + } return Changed; } |