private:
void emitExceptionRegistrationRecord(Function *F);
- void linkExceptionRegistration(IRBuilder<> &Builder, Value *Handler);
+ void linkExceptionRegistration(IRBuilder<> &Builder, Function *Handler);
void unlinkExceptionRegistration(IRBuilder<> &Builder);
void addCXXStateStores(Function &F, MachineModuleInfo &MMI);
+ void addSEHStateStores(Function &F, MachineModuleInfo &MMI);
void addCXXStateStoresToFunclet(Value *ParentRegNode, WinEHFuncInfo &FuncInfo,
Function &F, int BaseState);
void insertStateNumberStore(Value *ParentRegNode, Instruction *IP, int State);
StructType *EHLinkRegistrationTy = nullptr;
StructType *CXXEHRegistrationTy = nullptr;
StructType *SEHRegistrationTy = nullptr;
+ Function *FrameRecover = nullptr;
+ Function *FrameAddress = nullptr;
+ Function *FrameEscape = nullptr;
// Per-function state
EHPersonality Personality = EHPersonality::Unknown;
bool WinEHStatePass::doInitialization(Module &M) {
TheModule = &M;
+ FrameEscape = Intrinsic::getDeclaration(TheModule, Intrinsic::frameescape);
+ FrameRecover = Intrinsic::getDeclaration(TheModule, Intrinsic::framerecover);
+ FrameAddress = Intrinsic::getDeclaration(TheModule, Intrinsic::frameaddress);
return false;
}
EHLinkRegistrationTy = nullptr;
CXXEHRegistrationTy = nullptr;
SEHRegistrationTy = nullptr;
+ FrameEscape = nullptr;
+ FrameRecover = nullptr;
+ FrameAddress = nullptr;
return false;
}
return false;
// Check the personality. Do nothing if this is not an MSVC personality.
- LandingPadInst *LP = nullptr;
- for (BasicBlock &BB : F) {
- LP = BB.getLandingPadInst();
- if (LP)
- break;
- }
- if (!LP)
+ if (!F.hasPersonalityFn())
return false;
PersonalityFn =
- dyn_cast<Function>(LP->getPersonalityFn()->stripPointerCasts());
+ dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
if (!PersonalityFn)
return false;
Personality = classifyEHPersonality(PersonalityFn);
auto *MMIPtr = getAnalysisIfAvailable<MachineModuleInfo>();
assert(MMIPtr && "MachineModuleInfo should always be available");
MachineModuleInfo &MMI = *MMIPtr;
- if (Personality == EHPersonality::MSVC_CXX) {
- addCXXStateStores(F, MMI);
+ switch (Personality) {
+ default: llvm_unreachable("unexpected personality function");
+ case EHPersonality::MSVC_CXX: addCXXStateStores(F, MMI); break;
+ case EHPersonality::MSVC_X86SEH: addSEHStateStores(F, MMI); break;
}
// Reset per-function state.
if (Personality == EHPersonality::MSVC_CXX) {
RegNodeTy = getCXXEHRegistrationType();
RegNode = Builder.CreateAlloca(RegNodeTy);
- // FIXME: We can skip this in -GS- mode, when we figure that out.
// SavedESP = llvm.stacksave()
Value *SP = Builder.CreateCall(
Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
}
void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder,
- Value *Handler) {
+ Function *Handler) {
+ // Emit the .safeseh directive for this function.
+ Handler->addFnAttr("safeseh");
+
Type *LinkTy = getEHLinkRegistrationType();
// Handler = Handler
- Handler = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy());
- Builder.CreateStore(Handler, Builder.CreateStructGEP(LinkTy, Link, 1));
+ Value *HandlerI8 = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy());
+ Builder.CreateStore(HandlerI8, Builder.CreateStructGEP(LinkTy, Link, 1));
// Next = [fs:00]
Constant *FSZero =
Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257));
int RegNodeEscapeIndex = escapeRegNode(F);
// Only insert stores in catch handlers.
- Function *FrameRecover =
- Intrinsic::getDeclaration(TheModule, Intrinsic::framerecover);
- Function *FrameAddress =
- Intrinsic::getDeclaration(TheModule, Intrinsic::frameaddress);
Constant *FI8 =
ConstantExpr::getBitCast(&F, Type::getInt8PtrTy(TheModule->getContext()));
for (auto P : FuncInfo.HandlerBaseState) {
// of the entry block.
IRBuilder<> Builder(&F.getEntryBlock(),
EscapeCall ? EscapeCall : F.getEntryBlock().end());
- Builder.CreateCall(
- Intrinsic::getDeclaration(TheModule, Intrinsic::frameescape), Args);
+ Builder.CreateCall(FrameEscape, Args);
if (EscapeCall)
EscapeCall->eraseFromParent();
return Args.size() - 1;
}
}
+/// Assign every distinct landingpad a unique state number for SEH. Unlike C++
+/// EH, we can use this very simple algorithm while C++ EH cannot because catch
+/// handlers aren't outlined and the runtime doesn't have to figure out which
+/// catch handler frame to unwind to.
+/// FIXME: __finally blocks are outlined, so this approach may break down there.
+void WinEHStatePass::addSEHStateStores(Function &F, MachineModuleInfo &MMI) {
+ WinEHFuncInfo &FuncInfo = MMI.getWinEHFuncInfo(&F);
+
+ // Remember and return the index that we used. We save it in WinEHFuncInfo so
+ // that we can lower llvm.x86.seh.exceptioninfo later in filter functions
+ // without too much trouble.
+ int RegNodeEscapeIndex = escapeRegNode(F);
+ FuncInfo.EHRegNodeEscapeIndex = RegNodeEscapeIndex;
+
+ // Iterate all the instructions and emit state number stores.
+ int CurState = 0;
+ SmallPtrSet<BasicBlock *, 4> ExceptBlocks;
+ for (BasicBlock &BB : F) {
+ for (auto I = BB.begin(), E = BB.end(); I != E; ++I) {
+ if (auto *CI = dyn_cast<CallInst>(I)) {
+ auto *Intrin = dyn_cast<IntrinsicInst>(CI);
+ if (Intrin) {
+ // Calls that "don't throw" are considered to be able to throw asynch
+ // exceptions, but intrinsics cannot.
+ continue;
+ }
+ insertStateNumberStore(RegNode, CI, -1);
+ } else if (auto *II = dyn_cast<InvokeInst>(I)) {
+ // Look up the state number of the landingpad this unwinds to.
+ LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst();
+ auto InsertionPair =
+ FuncInfo.LandingPadStateMap.insert(std::make_pair(LPI, CurState));
+ auto Iter = InsertionPair.first;
+ int &State = Iter->second;
+ bool Inserted = InsertionPair.second;
+ if (Inserted) {
+ // Each action consumes a state number.
+ auto *EHActions = cast<IntrinsicInst>(LPI->getNextNode());
+ SmallVector<std::unique_ptr<ActionHandler>, 4> ActionList;
+ parseEHActions(EHActions, ActionList);
+ assert(!ActionList.empty());
+ CurState += ActionList.size();
+ State += ActionList.size() - 1;
+
+ // Remember all the __except block targets.
+ for (auto &Handler : ActionList) {
+ if (auto *CH = dyn_cast<CatchHandler>(Handler.get())) {
+ auto *BA = cast<BlockAddress>(CH->getHandlerBlockOrFunc());
+ ExceptBlocks.insert(BA->getBasicBlock());
+ }
+ }
+ }
+ insertStateNumberStore(RegNode, II, State);
+ }
+ }
+ }
+
+ // Insert llvm.stackrestore into each __except block.
+ Function *StackRestore =
+ Intrinsic::getDeclaration(TheModule, Intrinsic::stackrestore);
+ for (BasicBlock *ExceptBB : ExceptBlocks) {
+ IRBuilder<> Builder(ExceptBB->begin());
+ Value *SP =
+ Builder.CreateLoad(Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
+ Builder.CreateCall(StackRestore, {SP});
+ }
+}
+
void WinEHStatePass::insertStateNumberStore(Value *ParentRegNode,
Instruction *IP, int State) {
IRBuilder<> Builder(IP);