Skip to content

Commit

Permalink
Revert "DwarfEHPrepare: insert extra unwind paths for stack protector…
Browse files Browse the repository at this point in the history
… to instrument"

It's caused more failures than are trivially fixable.

This reverts commit 203b6f3.
  • Loading branch information
TNorthover committed Mar 16, 2023
1 parent 203b6f3 commit e4b352a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 261 deletions.
121 changes: 0 additions & 121 deletions llvm/lib/CodeGen/DwarfEHPrepare.cpp
Expand Up @@ -18,7 +18,6 @@
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/CodeGen/RuntimeLibcalls.h"
#include "llvm/CodeGen/StackProtector.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
Expand All @@ -29,7 +28,6 @@
#include "llvm/IR/Dominators.h"
#include "llvm/IR/EHPersonalities.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
Expand All @@ -39,7 +37,6 @@
#include "llvm/Target/TargetMachine.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <cstddef>

using namespace llvm;
Expand Down Expand Up @@ -169,125 +166,7 @@ size_t DwarfEHPrepare::pruneUnreachableResumes(
return ResumesLeft;
}

/// If a landingpad block doesn't already have a cleanup case, add one
/// that feeds directly into a resume instruction.
static void addCleanupResumeToLandingPad(BasicBlock &BB, DomTreeUpdater *DTU) {
LandingPadInst *LP = BB.getLandingPadInst();
if (LP->isCleanup())
return;

// There will usually be code testing for the other kinds of exception
// immediately after the landingpad. Working out the far end of that chain is
// tricky, so put our test for the new cleanup case (i.e. selector == 0) at
// the beginning.
BasicBlock *ContBB = SplitBlock(&BB, LP->getNextNode(), DTU);
BB.getTerminator()->eraseFromParent();

LP->setCleanup(true);
IRBuilder<> B(&BB);
Value *Selector = B.CreateExtractValue(LP, 1);
Value *Cmp = B.CreateICmpEQ(Selector, ConstantInt::get(Selector->getType(), 0));

Function *F = BB.getParent();
LLVMContext &Ctx = F->getContext();
BasicBlock *ResumeBB = BasicBlock::Create(Ctx, "resume", F);
ResumeInst::Create(LP, ResumeBB);

B.CreateCondBr(Cmp, ResumeBB, ContBB);
}

/// Create a basic block that has a `landingpad` instruction feeding
/// directly into a `resume`. Will be set to the unwind destination of a new
/// invoke.
static BasicBlock *createCleanupResumeBB(Function &F, Type *LandingPadTy) {
LLVMContext &Ctx = F.getContext();
BasicBlock *BB = BasicBlock::Create(Ctx, "cleanup_resume", &F);
IRBuilder<> B(BB);

// If this is going to be the only landingpad in the function, synthesize the
// standard type all ABIs use, which is essentially `{ ptr, i32 }`.
if (!LandingPadTy)
LandingPadTy =
StructType::get(Type::getInt8PtrTy(Ctx), IntegerType::get(Ctx, 32));

LandingPadInst *Except = B.CreateLandingPad(LandingPadTy, 0);
Except->setCleanup(true);
B.CreateResume(Except);
return BB;
}

/// Convert a call that might throw into an invoke that unwinds to the specified
/// simple landingpad/resume block.
static void changeCallToInvokeResume(CallInst &CI, BasicBlock *CleanupResumeBB,
DomTreeUpdater *DTU) {
BasicBlock *BB = CI.getParent();
BasicBlock *ContBB = SplitBlock(BB, &CI, DTU);
BB->getTerminator()->eraseFromParent();

IRBuilder<> B(BB);
SmallVector<Value *> Args(CI.args());
SmallVector<OperandBundleDef> Bundles;
CI.getOperandBundlesAsDefs(Bundles);
InvokeInst *NewCall =
B.CreateInvoke(CI.getFunctionType(), CI.getCalledOperand(), ContBB,
CleanupResumeBB, Args, Bundles, CI.getName());
NewCall->setAttributes(CI.getAttributes());
NewCall->setCallingConv(CI.getCallingConv());
NewCall->copyMetadata(CI);

CI.replaceAllUsesWith(NewCall);
CI.eraseFromParent();
}

/// Ensure that any call in this function that might throw has an associated
/// cleanup/resume that the stack protector can instrument later. Existing
/// invokes will get an added `cleanup` clause if needed, calls will be
/// converted to an invoke with trivial unwind followup.
static void addCleanupPathsForStackProtector(Function &F, DomTreeUpdater *DTU) {
// First add cleanup -> resume paths to all existing landingpads, noting what
// type landingpads in this function actually have along the way.
Type *LandingPadTy = nullptr;
for (Function::iterator FI = F.begin(); FI != F.end(); ++FI) {
BasicBlock &BB = *FI;
if (LandingPadInst *LP = BB.getLandingPadInst()) {
// We can assume the type is broadly compatible with { ptr, i32 } since
// other parts of this pass already try to extract values from it.
LandingPadTy = LP->getType();
addCleanupResumeToLandingPad(BB, DTU);
}
}

// Next convert any call that might throw into an invoke to a resume
// instruction for later instrumentation.
BasicBlock *CleanupResumeBB = nullptr;
for (Function::iterator FI = F.begin(); FI != F.end(); ++FI) {
BasicBlock &BB = *FI;
for (Instruction &I : BB) {
CallInst *CI = dyn_cast<CallInst>(&I);
if (!CI || CI->doesNotThrow())
continue;

// Tail calls cannot use our stack so no need to check whether it was
// corrupted.
if (CI->isTailCall())
continue;

if (!CleanupResumeBB)
CleanupResumeBB = createCleanupResumeBB(F, LandingPadTy);

changeCallToInvokeResume(*CI, CleanupResumeBB, DTU);

// This block has been split, start again on its continuation.
break;
}
}
}

bool DwarfEHPrepare::InsertUnwindResumeCalls() {
if (F.hasPersonalityFn() &&
StackProtector::requiresStackProtector(&F, nullptr))
addCleanupPathsForStackProtector(F, DTU);

SmallVector<ResumeInst *, 16> Resumes;
SmallVector<LandingPadInst *, 16> CleanupLPads;
if (F.doesNotThrow())
Expand Down
140 changes: 0 additions & 140 deletions llvm/test/CodeGen/Generic/safestack-unwind.ll

This file was deleted.

0 comments on commit e4b352a

Please sign in to comment.