2015-05-05 17:44:16 +00:00
|
|
|
//===-- X86WinEHState - Insert EH state updates for win32 exceptions ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// All functions using an MSVC EH personality use an explicitly updated state
|
|
|
|
// number stored in an exception registration stack object. The registration
|
|
|
|
// object is linked into a thread-local chain of registrations stored at fs:00.
|
|
|
|
// This pass adds the registration object and EH state updates.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "X86.h"
|
|
|
|
#include "llvm/Analysis/LibCallSemantics.h"
|
2015-05-28 22:00:24 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2015-05-05 17:44:16 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/WinEHFuncInfo.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/PatternMatch.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::PatternMatch;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "winehstate"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WinEHStatePass : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid.
|
|
|
|
|
|
|
|
WinEHStatePass() : FunctionPass(ID) {}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &Fn) override;
|
|
|
|
|
|
|
|
bool doInitialization(Module &M) override;
|
|
|
|
|
|
|
|
bool doFinalization(Module &M) override;
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
|
|
|
|
const char *getPassName() const override {
|
|
|
|
return "Windows 32-bit x86 EH state insertion";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void emitExceptionRegistrationRecord(Function *F);
|
|
|
|
|
2015-06-10 01:02:30 +00:00
|
|
|
void linkExceptionRegistration(IRBuilder<> &Builder, Function *Handler);
|
2015-05-28 22:00:24 +00:00
|
|
|
void unlinkExceptionRegistration(IRBuilder<> &Builder);
|
|
|
|
void addCXXStateStores(Function &F, MachineModuleInfo &MMI);
|
2015-06-09 21:42:19 +00:00
|
|
|
void addSEHStateStores(Function &F, MachineModuleInfo &MMI);
|
2015-05-28 22:00:24 +00:00
|
|
|
void addCXXStateStoresToFunclet(Value *ParentRegNode, WinEHFuncInfo &FuncInfo,
|
|
|
|
Function &F, int BaseState);
|
|
|
|
void insertStateNumberStore(Value *ParentRegNode, Instruction *IP, int State);
|
2015-05-05 17:44:16 +00:00
|
|
|
|
2015-05-20 23:08:04 +00:00
|
|
|
Value *emitEHLSDA(IRBuilder<> &Builder, Function *F);
|
|
|
|
|
|
|
|
Function *generateLSDAInEAXThunk(Function *ParentFunc);
|
|
|
|
|
2015-05-28 22:00:24 +00:00
|
|
|
int escapeRegNode(Function &F);
|
|
|
|
|
2015-05-05 17:44:16 +00:00
|
|
|
// Module-level type getters.
|
2015-05-29 22:57:46 +00:00
|
|
|
Type *getEHLinkRegistrationType();
|
|
|
|
Type *getSEHRegistrationType();
|
|
|
|
Type *getCXXEHRegistrationType();
|
2015-05-05 17:44:16 +00:00
|
|
|
|
|
|
|
// Per-module data.
|
|
|
|
Module *TheModule = nullptr;
|
2015-05-29 22:57:46 +00:00
|
|
|
StructType *EHLinkRegistrationTy = nullptr;
|
|
|
|
StructType *CXXEHRegistrationTy = nullptr;
|
|
|
|
StructType *SEHRegistrationTy = nullptr;
|
2015-06-08 22:43:32 +00:00
|
|
|
Function *FrameRecover = nullptr;
|
|
|
|
Function *FrameAddress = nullptr;
|
|
|
|
Function *FrameEscape = nullptr;
|
2015-05-05 17:44:16 +00:00
|
|
|
|
|
|
|
// Per-function state
|
|
|
|
EHPersonality Personality = EHPersonality::Unknown;
|
|
|
|
Function *PersonalityFn = nullptr;
|
2015-05-28 22:00:24 +00:00
|
|
|
|
|
|
|
/// The stack allocation containing all EH data, including the link in the
|
|
|
|
/// fs:00 chain and the current state.
|
|
|
|
AllocaInst *RegNode = nullptr;
|
|
|
|
|
|
|
|
/// Struct type of RegNode. Used for GEPing.
|
|
|
|
Type *RegNodeTy = nullptr;
|
|
|
|
|
|
|
|
/// The index of the state field of RegNode.
|
|
|
|
int StateFieldIndex = ~0U;
|
|
|
|
|
|
|
|
/// The linked list node subobject inside of RegNode.
|
|
|
|
Value *Link = nullptr;
|
2015-05-05 17:44:16 +00:00
|
|
|
};
|
2015-06-23 09:49:53 +00:00
|
|
|
}
|
2015-05-05 17:44:16 +00:00
|
|
|
|
|
|
|
FunctionPass *llvm::createX86WinEHStatePass() { return new WinEHStatePass(); }
|
|
|
|
|
|
|
|
char WinEHStatePass::ID = 0;
|
|
|
|
|
|
|
|
bool WinEHStatePass::doInitialization(Module &M) {
|
|
|
|
TheModule = &M;
|
Rename llvm.frameescape and llvm.framerecover to localescape and localrecover
Summary:
Initially, these intrinsics seemed like part of a family of "frame"
related intrinsics, but now I think that's more confusing than helpful.
Initially, the LangRef specified that this would create a new kind of
allocation that would be allocated at a fixed offset from the frame
pointer (EBP/RBP). We ended up dropping that design, and leaving the
stack frame layout alone.
These intrinsics are really about sharing local stack allocations, not
frame pointers. I intend to go further and add an `llvm.localaddress()`
intrinsic that returns whatever register (EBP, ESI, ESP, RBX) is being
used to address locals, which should not be confused with the frame
pointer.
Naming suggestions at this point are welcome, I'm happy to re-run sed.
Reviewers: majnemer, nicholas
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D11011
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241633 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-07 22:25:32 +00:00
|
|
|
FrameEscape = Intrinsic::getDeclaration(TheModule, Intrinsic::localescape);
|
|
|
|
FrameRecover = Intrinsic::getDeclaration(TheModule, Intrinsic::localrecover);
|
2015-06-08 22:43:32 +00:00
|
|
|
FrameAddress = Intrinsic::getDeclaration(TheModule, Intrinsic::frameaddress);
|
2015-05-05 17:44:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WinEHStatePass::doFinalization(Module &M) {
|
|
|
|
assert(TheModule == &M);
|
|
|
|
TheModule = nullptr;
|
2015-05-29 22:57:46 +00:00
|
|
|
EHLinkRegistrationTy = nullptr;
|
|
|
|
CXXEHRegistrationTy = nullptr;
|
|
|
|
SEHRegistrationTy = nullptr;
|
2015-06-08 22:43:32 +00:00
|
|
|
FrameEscape = nullptr;
|
|
|
|
FrameRecover = nullptr;
|
|
|
|
FrameAddress = nullptr;
|
2015-05-05 17:44:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WinEHStatePass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
// This pass should only insert a stack allocation, memory accesses, and
|
Rename llvm.frameescape and llvm.framerecover to localescape and localrecover
Summary:
Initially, these intrinsics seemed like part of a family of "frame"
related intrinsics, but now I think that's more confusing than helpful.
Initially, the LangRef specified that this would create a new kind of
allocation that would be allocated at a fixed offset from the frame
pointer (EBP/RBP). We ended up dropping that design, and leaving the
stack frame layout alone.
These intrinsics are really about sharing local stack allocations, not
frame pointers. I intend to go further and add an `llvm.localaddress()`
intrinsic that returns whatever register (EBP, ESI, ESP, RBX) is being
used to address locals, which should not be confused with the frame
pointer.
Naming suggestions at this point are welcome, I'm happy to re-run sed.
Reviewers: majnemer, nicholas
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D11011
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241633 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-07 22:25:32 +00:00
|
|
|
// localrecovers.
|
2015-05-05 17:44:16 +00:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WinEHStatePass::runOnFunction(Function &F) {
|
2015-05-20 23:08:04 +00:00
|
|
|
// If this is an outlined handler, don't do anything. We'll do state insertion
|
|
|
|
// for it in the parent.
|
|
|
|
StringRef WinEHParentName =
|
|
|
|
F.getFnAttribute("wineh-parent").getValueAsString();
|
|
|
|
if (WinEHParentName != F.getName() && !WinEHParentName.empty())
|
|
|
|
return false;
|
|
|
|
|
2015-05-05 17:44:16 +00:00
|
|
|
// Check the personality. Do nothing if this is not an MSVC personality.
|
2015-06-17 20:52:32 +00:00
|
|
|
if (!F.hasPersonalityFn())
|
2015-05-05 17:44:16 +00:00
|
|
|
return false;
|
|
|
|
PersonalityFn =
|
2015-06-17 20:52:32 +00:00
|
|
|
dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
|
2015-05-05 17:44:16 +00:00
|
|
|
if (!PersonalityFn)
|
|
|
|
return false;
|
|
|
|
Personality = classifyEHPersonality(PersonalityFn);
|
|
|
|
if (!isMSVCEHPersonality(Personality))
|
|
|
|
return false;
|
|
|
|
|
2015-05-29 21:58:11 +00:00
|
|
|
// Disable frame pointer elimination in this function.
|
|
|
|
// FIXME: Do the nested handlers need to keep the parent ebp in ebp, or can we
|
|
|
|
// use an arbitrary register?
|
|
|
|
F.addFnAttr("no-frame-pointer-elim", "true");
|
|
|
|
|
2015-05-05 17:44:16 +00:00
|
|
|
emitExceptionRegistrationRecord(&F);
|
2015-05-28 22:00:24 +00:00
|
|
|
|
|
|
|
auto *MMIPtr = getAnalysisIfAvailable<MachineModuleInfo>();
|
|
|
|
assert(MMIPtr && "MachineModuleInfo should always be available");
|
|
|
|
MachineModuleInfo &MMI = *MMIPtr;
|
2015-06-09 21:42:19 +00:00
|
|
|
switch (Personality) {
|
|
|
|
default: llvm_unreachable("unexpected personality function");
|
|
|
|
case EHPersonality::MSVC_CXX: addCXXStateStores(F, MMI); break;
|
|
|
|
case EHPersonality::MSVC_X86SEH: addSEHStateStores(F, MMI); break;
|
2015-05-28 22:00:24 +00:00
|
|
|
}
|
2015-05-05 17:44:16 +00:00
|
|
|
|
|
|
|
// Reset per-function state.
|
|
|
|
PersonalityFn = nullptr;
|
|
|
|
Personality = EHPersonality::Unknown;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the common EH registration subobject:
|
2015-05-20 23:08:04 +00:00
|
|
|
/// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
|
|
|
|
/// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
|
2015-05-05 17:44:16 +00:00
|
|
|
/// struct EHRegistrationNode {
|
|
|
|
/// EHRegistrationNode *Next;
|
2015-05-20 23:08:04 +00:00
|
|
|
/// PEXCEPTION_ROUTINE Handler;
|
2015-05-05 17:44:16 +00:00
|
|
|
/// };
|
2015-05-29 22:57:46 +00:00
|
|
|
Type *WinEHStatePass::getEHLinkRegistrationType() {
|
|
|
|
if (EHLinkRegistrationTy)
|
|
|
|
return EHLinkRegistrationTy;
|
2015-05-05 17:44:16 +00:00
|
|
|
LLVMContext &Context = TheModule->getContext();
|
2015-05-29 22:57:46 +00:00
|
|
|
EHLinkRegistrationTy = StructType::create(Context, "EHRegistrationNode");
|
2015-05-05 17:44:16 +00:00
|
|
|
Type *FieldTys[] = {
|
2015-05-29 22:57:46 +00:00
|
|
|
EHLinkRegistrationTy->getPointerTo(0), // EHRegistrationNode *Next
|
2015-05-05 17:44:16 +00:00
|
|
|
Type::getInt8PtrTy(Context) // EXCEPTION_DISPOSITION (*Handler)(...)
|
|
|
|
};
|
2015-05-29 22:57:46 +00:00
|
|
|
EHLinkRegistrationTy->setBody(FieldTys, false);
|
|
|
|
return EHLinkRegistrationTy;
|
2015-05-05 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The __CxxFrameHandler3 registration node:
|
|
|
|
/// struct CXXExceptionRegistration {
|
|
|
|
/// void *SavedESP;
|
|
|
|
/// EHRegistrationNode SubRecord;
|
|
|
|
/// int32_t TryLevel;
|
|
|
|
/// };
|
2015-05-29 22:57:46 +00:00
|
|
|
Type *WinEHStatePass::getCXXEHRegistrationType() {
|
|
|
|
if (CXXEHRegistrationTy)
|
|
|
|
return CXXEHRegistrationTy;
|
2015-05-05 17:44:16 +00:00
|
|
|
LLVMContext &Context = TheModule->getContext();
|
|
|
|
Type *FieldTys[] = {
|
|
|
|
Type::getInt8PtrTy(Context), // void *SavedESP
|
2015-05-29 22:57:46 +00:00
|
|
|
getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
|
2015-05-05 17:44:16 +00:00
|
|
|
Type::getInt32Ty(Context) // int32_t TryLevel
|
|
|
|
};
|
2015-05-29 22:57:46 +00:00
|
|
|
CXXEHRegistrationTy =
|
2015-05-05 17:44:16 +00:00
|
|
|
StructType::create(FieldTys, "CXXExceptionRegistration");
|
2015-05-29 22:57:46 +00:00
|
|
|
return CXXEHRegistrationTy;
|
2015-05-05 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
2015-05-29 22:57:46 +00:00
|
|
|
/// The _except_handler3/4 registration node:
|
2015-05-05 17:44:16 +00:00
|
|
|
/// struct EH4ExceptionRegistration {
|
|
|
|
/// void *SavedESP;
|
|
|
|
/// _EXCEPTION_POINTERS *ExceptionPointers;
|
|
|
|
/// EHRegistrationNode SubRecord;
|
|
|
|
/// int32_t EncodedScopeTable;
|
|
|
|
/// int32_t TryLevel;
|
|
|
|
/// };
|
2015-05-29 22:57:46 +00:00
|
|
|
Type *WinEHStatePass::getSEHRegistrationType() {
|
|
|
|
if (SEHRegistrationTy)
|
|
|
|
return SEHRegistrationTy;
|
2015-05-05 17:44:16 +00:00
|
|
|
LLVMContext &Context = TheModule->getContext();
|
|
|
|
Type *FieldTys[] = {
|
|
|
|
Type::getInt8PtrTy(Context), // void *SavedESP
|
|
|
|
Type::getInt8PtrTy(Context), // void *ExceptionPointers
|
2015-05-29 22:57:46 +00:00
|
|
|
getEHLinkRegistrationType(), // EHRegistrationNode SubRecord
|
2015-05-05 17:44:16 +00:00
|
|
|
Type::getInt32Ty(Context), // int32_t EncodedScopeTable
|
|
|
|
Type::getInt32Ty(Context) // int32_t TryLevel
|
|
|
|
};
|
2015-05-29 22:57:46 +00:00
|
|
|
SEHRegistrationTy = StructType::create(FieldTys, "SEHExceptionRegistration");
|
|
|
|
return SEHRegistrationTy;
|
2015-05-05 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit an exception registration record. These are stack allocations with the
|
|
|
|
// common subobject of two pointers: the previous registration record (the old
|
|
|
|
// fs:00) and the personality function for the current frame. The data before
|
|
|
|
// and after that is personality function specific.
|
|
|
|
void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) {
|
|
|
|
assert(Personality == EHPersonality::MSVC_CXX ||
|
|
|
|
Personality == EHPersonality::MSVC_X86SEH);
|
|
|
|
|
|
|
|
StringRef PersonalityName = PersonalityFn->getName();
|
|
|
|
IRBuilder<> Builder(&F->getEntryBlock(), F->getEntryBlock().begin());
|
|
|
|
Type *Int8PtrType = Builder.getInt8PtrTy();
|
2015-05-29 22:57:46 +00:00
|
|
|
if (Personality == EHPersonality::MSVC_CXX) {
|
|
|
|
RegNodeTy = getCXXEHRegistrationType();
|
2015-05-28 22:00:24 +00:00
|
|
|
RegNode = Builder.CreateAlloca(RegNodeTy);
|
2015-05-05 17:44:16 +00:00
|
|
|
// SavedESP = llvm.stacksave()
|
|
|
|
Value *SP = Builder.CreateCall(
|
2015-05-18 22:13:54 +00:00
|
|
|
Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
|
2015-05-05 17:44:16 +00:00
|
|
|
Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
|
|
|
|
// TryLevel = -1
|
2015-05-28 22:00:24 +00:00
|
|
|
StateFieldIndex = 2;
|
|
|
|
insertStateNumberStore(RegNode, Builder.GetInsertPoint(), -1);
|
2015-05-20 23:08:04 +00:00
|
|
|
// Handler = __ehhandler$F
|
|
|
|
Function *Trampoline = generateLSDAInEAXThunk(F);
|
2015-05-28 22:00:24 +00:00
|
|
|
Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 1);
|
|
|
|
linkExceptionRegistration(Builder, Trampoline);
|
2015-05-29 22:57:46 +00:00
|
|
|
} else if (Personality == EHPersonality::MSVC_X86SEH) {
|
|
|
|
// If _except_handler4 is in use, some additional guard checks and prologue
|
|
|
|
// stuff is required.
|
|
|
|
bool UseStackGuard = (PersonalityName == "_except_handler4");
|
|
|
|
RegNodeTy = getSEHRegistrationType();
|
2015-05-28 22:00:24 +00:00
|
|
|
RegNode = Builder.CreateAlloca(RegNodeTy);
|
2015-05-05 17:44:16 +00:00
|
|
|
// SavedESP = llvm.stacksave()
|
|
|
|
Value *SP = Builder.CreateCall(
|
2015-05-18 22:13:54 +00:00
|
|
|
Intrinsic::getDeclaration(TheModule, Intrinsic::stacksave), {});
|
2015-05-05 17:44:16 +00:00
|
|
|
Builder.CreateStore(SP, Builder.CreateStructGEP(RegNodeTy, RegNode, 0));
|
2015-05-29 22:57:46 +00:00
|
|
|
// TryLevel = -2 / -1
|
2015-05-28 22:00:24 +00:00
|
|
|
StateFieldIndex = 4;
|
2015-05-29 22:57:46 +00:00
|
|
|
insertStateNumberStore(RegNode, Builder.GetInsertPoint(),
|
|
|
|
UseStackGuard ? -2 : -1);
|
2015-05-20 23:08:04 +00:00
|
|
|
// ScopeTable = llvm.x86.seh.lsda(F)
|
|
|
|
Value *FI8 = Builder.CreateBitCast(F, Int8PtrType);
|
|
|
|
Value *LSDA = Builder.CreateCall(
|
|
|
|
Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8);
|
2015-05-29 22:57:46 +00:00
|
|
|
Type *Int32Ty = Type::getInt32Ty(TheModule->getContext());
|
|
|
|
LSDA = Builder.CreatePtrToInt(LSDA, Int32Ty);
|
|
|
|
// If using _except_handler4, xor the address of the table with
|
|
|
|
// __security_cookie.
|
|
|
|
if (UseStackGuard) {
|
|
|
|
Value *Cookie =
|
|
|
|
TheModule->getOrInsertGlobal("__security_cookie", Int32Ty);
|
|
|
|
Value *Val = Builder.CreateLoad(Int32Ty, Cookie);
|
|
|
|
LSDA = Builder.CreateXor(LSDA, Val);
|
|
|
|
}
|
|
|
|
Builder.CreateStore(LSDA, Builder.CreateStructGEP(RegNodeTy, RegNode, 3));
|
2015-05-28 22:00:24 +00:00
|
|
|
Link = Builder.CreateStructGEP(RegNodeTy, RegNode, 2);
|
|
|
|
linkExceptionRegistration(Builder, PersonalityFn);
|
2015-05-05 17:44:16 +00:00
|
|
|
} else {
|
|
|
|
llvm_unreachable("unexpected personality function");
|
|
|
|
}
|
|
|
|
|
2015-05-28 22:00:24 +00:00
|
|
|
// Insert an unlink before all returns.
|
2015-05-05 17:44:16 +00:00
|
|
|
for (BasicBlock &BB : *F) {
|
|
|
|
TerminatorInst *T = BB.getTerminator();
|
|
|
|
if (!isa<ReturnInst>(T))
|
|
|
|
continue;
|
|
|
|
Builder.SetInsertPoint(T);
|
2015-05-28 22:00:24 +00:00
|
|
|
unlinkExceptionRegistration(Builder);
|
2015-05-05 17:44:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-20 23:08:04 +00:00
|
|
|
Value *WinEHStatePass::emitEHLSDA(IRBuilder<> &Builder, Function *F) {
|
|
|
|
Value *FI8 = Builder.CreateBitCast(F, Type::getInt8PtrTy(F->getContext()));
|
|
|
|
return Builder.CreateCall(
|
|
|
|
Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_lsda), FI8);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate a thunk that puts the LSDA of ParentFunc in EAX and then calls
|
|
|
|
/// PersonalityFn, forwarding the parameters passed to PEXCEPTION_ROUTINE:
|
|
|
|
/// typedef _EXCEPTION_DISPOSITION (*PEXCEPTION_ROUTINE)(
|
|
|
|
/// _EXCEPTION_RECORD *, void *, _CONTEXT *, void *);
|
|
|
|
/// We essentially want this code:
|
|
|
|
/// movl $lsda, %eax
|
|
|
|
/// jmpl ___CxxFrameHandler3
|
|
|
|
Function *WinEHStatePass::generateLSDAInEAXThunk(Function *ParentFunc) {
|
|
|
|
LLVMContext &Context = ParentFunc->getContext();
|
|
|
|
Type *Int32Ty = Type::getInt32Ty(Context);
|
|
|
|
Type *Int8PtrType = Type::getInt8PtrTy(Context);
|
|
|
|
Type *ArgTys[5] = {Int8PtrType, Int8PtrType, Int8PtrType, Int8PtrType,
|
|
|
|
Int8PtrType};
|
|
|
|
FunctionType *TrampolineTy =
|
|
|
|
FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 4),
|
|
|
|
/*isVarArg=*/false);
|
|
|
|
FunctionType *TargetFuncTy =
|
|
|
|
FunctionType::get(Int32Ty, makeArrayRef(&ArgTys[0], 5),
|
|
|
|
/*isVarArg=*/false);
|
2015-07-13 17:55:14 +00:00
|
|
|
Function *Trampoline =
|
|
|
|
Function::Create(TrampolineTy, GlobalValue::InternalLinkage,
|
|
|
|
Twine("__ehhandler$") + GlobalValue::getRealLinkageName(
|
|
|
|
ParentFunc->getName()),
|
|
|
|
TheModule);
|
2015-05-20 23:08:04 +00:00
|
|
|
BasicBlock *EntryBB = BasicBlock::Create(Context, "entry", Trampoline);
|
|
|
|
IRBuilder<> Builder(EntryBB);
|
|
|
|
Value *LSDA = emitEHLSDA(Builder, ParentFunc);
|
|
|
|
Value *CastPersonality =
|
|
|
|
Builder.CreateBitCast(PersonalityFn, TargetFuncTy->getPointerTo());
|
|
|
|
auto AI = Trampoline->arg_begin();
|
|
|
|
Value *Args[5] = {LSDA, AI++, AI++, AI++, AI++};
|
|
|
|
CallInst *Call = Builder.CreateCall(CastPersonality, Args);
|
|
|
|
// Can't use musttail due to prototype mismatch, but we can use tail.
|
|
|
|
Call->setTailCall(true);
|
|
|
|
// Set inreg so we pass it in EAX.
|
|
|
|
Call->addAttribute(1, Attribute::InReg);
|
|
|
|
Builder.CreateRet(Call);
|
|
|
|
return Trampoline;
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:44:16 +00:00
|
|
|
void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder,
|
2015-06-10 01:02:30 +00:00
|
|
|
Function *Handler) {
|
|
|
|
// Emit the .safeseh directive for this function.
|
|
|
|
Handler->addFnAttr("safeseh");
|
|
|
|
|
2015-05-29 22:57:46 +00:00
|
|
|
Type *LinkTy = getEHLinkRegistrationType();
|
2015-05-05 17:44:16 +00:00
|
|
|
// Handler = Handler
|
2015-06-10 01:02:30 +00:00
|
|
|
Value *HandlerI8 = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy());
|
|
|
|
Builder.CreateStore(HandlerI8, Builder.CreateStructGEP(LinkTy, Link, 1));
|
2015-05-05 17:44:16 +00:00
|
|
|
// Next = [fs:00]
|
|
|
|
Constant *FSZero =
|
2015-05-28 22:00:24 +00:00
|
|
|
Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257));
|
2015-05-05 17:44:16 +00:00
|
|
|
Value *Next = Builder.CreateLoad(FSZero);
|
2015-05-28 22:00:24 +00:00
|
|
|
Builder.CreateStore(Next, Builder.CreateStructGEP(LinkTy, Link, 0));
|
|
|
|
// [fs:00] = Link
|
|
|
|
Builder.CreateStore(Link, FSZero);
|
2015-05-05 17:44:16 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 22:00:24 +00:00
|
|
|
void WinEHStatePass::unlinkExceptionRegistration(IRBuilder<> &Builder) {
|
|
|
|
// Clone Link into the current BB for better address mode folding.
|
|
|
|
if (auto *GEP = dyn_cast<GetElementPtrInst>(Link)) {
|
2015-05-05 17:44:16 +00:00
|
|
|
GEP = cast<GetElementPtrInst>(GEP->clone());
|
|
|
|
Builder.Insert(GEP);
|
2015-05-28 22:00:24 +00:00
|
|
|
Link = GEP;
|
2015-05-05 17:44:16 +00:00
|
|
|
}
|
2015-05-29 22:57:46 +00:00
|
|
|
Type *LinkTy = getEHLinkRegistrationType();
|
2015-05-28 22:00:24 +00:00
|
|
|
// [fs:00] = Link->Next
|
2015-05-05 17:44:16 +00:00
|
|
|
Value *Next =
|
2015-05-28 22:00:24 +00:00
|
|
|
Builder.CreateLoad(Builder.CreateStructGEP(LinkTy, Link, 0));
|
2015-05-05 17:44:16 +00:00
|
|
|
Constant *FSZero =
|
2015-05-28 22:00:24 +00:00
|
|
|
Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257));
|
2015-05-05 17:44:16 +00:00
|
|
|
Builder.CreateStore(Next, FSZero);
|
|
|
|
}
|
2015-05-28 22:00:24 +00:00
|
|
|
|
|
|
|
void WinEHStatePass::addCXXStateStores(Function &F, MachineModuleInfo &MMI) {
|
|
|
|
WinEHFuncInfo &FuncInfo = MMI.getWinEHFuncInfo(&F);
|
|
|
|
calculateWinCXXEHStateNumbers(&F, FuncInfo);
|
|
|
|
|
|
|
|
// The base state for the parent is -1.
|
|
|
|
addCXXStateStoresToFunclet(RegNode, FuncInfo, F, -1);
|
|
|
|
|
|
|
|
// Set up RegNodeEscapeIndex
|
|
|
|
int RegNodeEscapeIndex = escapeRegNode(F);
|
2015-06-30 22:46:59 +00:00
|
|
|
FuncInfo.EHRegNodeEscapeIndex = RegNodeEscapeIndex;
|
2015-05-28 22:00:24 +00:00
|
|
|
|
|
|
|
// Only insert stores in catch handlers.
|
|
|
|
Constant *FI8 =
|
|
|
|
ConstantExpr::getBitCast(&F, Type::getInt8PtrTy(TheModule->getContext()));
|
|
|
|
for (auto P : FuncInfo.HandlerBaseState) {
|
|
|
|
Function *Handler = const_cast<Function *>(P.first);
|
|
|
|
int BaseState = P.second;
|
|
|
|
IRBuilder<> Builder(&Handler->getEntryBlock(),
|
|
|
|
Handler->getEntryBlock().begin());
|
|
|
|
// FIXME: Find and reuse such a call if present.
|
|
|
|
Value *ParentFP = Builder.CreateCall(FrameAddress, {Builder.getInt32(1)});
|
|
|
|
Value *RecoveredRegNode = Builder.CreateCall(
|
|
|
|
FrameRecover, {FI8, ParentFP, Builder.getInt32(RegNodeEscapeIndex)});
|
|
|
|
RecoveredRegNode =
|
|
|
|
Builder.CreateBitCast(RecoveredRegNode, RegNodeTy->getPointerTo(0));
|
|
|
|
addCXXStateStoresToFunclet(RecoveredRegNode, FuncInfo, *Handler, BaseState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Escape RegNode so that we can access it from child handlers. Find the call
|
Rename llvm.frameescape and llvm.framerecover to localescape and localrecover
Summary:
Initially, these intrinsics seemed like part of a family of "frame"
related intrinsics, but now I think that's more confusing than helpful.
Initially, the LangRef specified that this would create a new kind of
allocation that would be allocated at a fixed offset from the frame
pointer (EBP/RBP). We ended up dropping that design, and leaving the
stack frame layout alone.
These intrinsics are really about sharing local stack allocations, not
frame pointers. I intend to go further and add an `llvm.localaddress()`
intrinsic that returns whatever register (EBP, ESI, ESP, RBX) is being
used to address locals, which should not be confused with the frame
pointer.
Naming suggestions at this point are welcome, I'm happy to re-run sed.
Reviewers: majnemer, nicholas
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D11011
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241633 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-07 22:25:32 +00:00
|
|
|
/// to localescape, if any, in the entry block and append RegNode to the list
|
2015-05-28 22:00:24 +00:00
|
|
|
/// of arguments.
|
|
|
|
int WinEHStatePass::escapeRegNode(Function &F) {
|
Rename llvm.frameescape and llvm.framerecover to localescape and localrecover
Summary:
Initially, these intrinsics seemed like part of a family of "frame"
related intrinsics, but now I think that's more confusing than helpful.
Initially, the LangRef specified that this would create a new kind of
allocation that would be allocated at a fixed offset from the frame
pointer (EBP/RBP). We ended up dropping that design, and leaving the
stack frame layout alone.
These intrinsics are really about sharing local stack allocations, not
frame pointers. I intend to go further and add an `llvm.localaddress()`
intrinsic that returns whatever register (EBP, ESI, ESP, RBX) is being
used to address locals, which should not be confused with the frame
pointer.
Naming suggestions at this point are welcome, I'm happy to re-run sed.
Reviewers: majnemer, nicholas
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D11011
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241633 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-07 22:25:32 +00:00
|
|
|
// Find the call to localescape and extract its arguments.
|
2015-05-28 22:00:24 +00:00
|
|
|
IntrinsicInst *EscapeCall = nullptr;
|
|
|
|
for (Instruction &I : F.getEntryBlock()) {
|
|
|
|
IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I);
|
Rename llvm.frameescape and llvm.framerecover to localescape and localrecover
Summary:
Initially, these intrinsics seemed like part of a family of "frame"
related intrinsics, but now I think that's more confusing than helpful.
Initially, the LangRef specified that this would create a new kind of
allocation that would be allocated at a fixed offset from the frame
pointer (EBP/RBP). We ended up dropping that design, and leaving the
stack frame layout alone.
These intrinsics are really about sharing local stack allocations, not
frame pointers. I intend to go further and add an `llvm.localaddress()`
intrinsic that returns whatever register (EBP, ESI, ESP, RBX) is being
used to address locals, which should not be confused with the frame
pointer.
Naming suggestions at this point are welcome, I'm happy to re-run sed.
Reviewers: majnemer, nicholas
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D11011
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241633 91177308-0d34-0410-b5e6-96231b3b80d8
2015-07-07 22:25:32 +00:00
|
|
|
if (II && II->getIntrinsicID() == Intrinsic::localescape) {
|
2015-05-28 22:00:24 +00:00
|
|
|
EscapeCall = II;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SmallVector<Value *, 8> Args;
|
|
|
|
if (EscapeCall) {
|
|
|
|
auto Ops = EscapeCall->arg_operands();
|
|
|
|
Args.append(Ops.begin(), Ops.end());
|
|
|
|
}
|
|
|
|
Args.push_back(RegNode);
|
|
|
|
|
|
|
|
// Replace the call (if it exists) with new one. Otherwise, insert at the end
|
|
|
|
// of the entry block.
|
2015-07-10 00:08:49 +00:00
|
|
|
Instruction *InsertPt = EscapeCall;
|
|
|
|
if (!EscapeCall)
|
|
|
|
InsertPt = F.getEntryBlock().getTerminator();
|
|
|
|
IRBuilder<> Builder(&F.getEntryBlock(), InsertPt);
|
2015-06-08 22:43:32 +00:00
|
|
|
Builder.CreateCall(FrameEscape, Args);
|
2015-05-28 22:00:24 +00:00
|
|
|
if (EscapeCall)
|
|
|
|
EscapeCall->eraseFromParent();
|
|
|
|
return Args.size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WinEHStatePass::addCXXStateStoresToFunclet(Value *ParentRegNode,
|
|
|
|
WinEHFuncInfo &FuncInfo,
|
|
|
|
Function &F, int BaseState) {
|
|
|
|
// Iterate all the instructions and emit state number stores.
|
|
|
|
for (BasicBlock &BB : F) {
|
|
|
|
for (Instruction &I : BB) {
|
|
|
|
if (auto *CI = dyn_cast<CallInst>(&I)) {
|
|
|
|
// Possibly throwing call instructions have no actions to take after
|
|
|
|
// an unwind. Ensure they are in the -1 state.
|
|
|
|
if (CI->doesNotThrow())
|
|
|
|
continue;
|
|
|
|
insertStateNumberStore(ParentRegNode, CI, BaseState);
|
|
|
|
} else if (auto *II = dyn_cast<InvokeInst>(&I)) {
|
|
|
|
// Look up the state number of the landingpad this unwinds to.
|
|
|
|
LandingPadInst *LPI = II->getUnwindDest()->getLandingPadInst();
|
|
|
|
// FIXME: Why does this assertion fail?
|
|
|
|
//assert(FuncInfo.LandingPadStateMap.count(LPI) && "LP has no state!");
|
|
|
|
int State = FuncInfo.LandingPadStateMap[LPI];
|
|
|
|
insertStateNumberStore(ParentRegNode, II, State);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-09 21:42:19 +00:00
|
|
|
/// 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);
|
|
|
|
|
2015-06-11 22:32:23 +00:00
|
|
|
// Remember and return the index that we used. We save it in WinEHFuncInfo so
|
2015-06-30 22:46:59 +00:00
|
|
|
// that we can lower llvm.x86.seh.recoverfp later in filter functions without
|
|
|
|
// too much trouble.
|
2015-06-11 22:32:23 +00:00
|
|
|
int RegNodeEscapeIndex = escapeRegNode(F);
|
|
|
|
FuncInfo.EHRegNodeEscapeIndex = RegNodeEscapeIndex;
|
|
|
|
|
2015-06-09 21:42:19 +00:00
|
|
|
// Iterate all the instructions and emit state number stores.
|
|
|
|
int CurState = 0;
|
2015-06-10 01:34:54 +00:00
|
|
|
SmallPtrSet<BasicBlock *, 4> ExceptBlocks;
|
2015-06-09 21:42:19 +00:00
|
|
|
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 =
|
2015-06-11 23:37:18 +00:00
|
|
|
FuncInfo.LandingPadStateMap.insert(std::make_pair(LPI, CurState));
|
2015-06-09 21:42:19 +00:00
|
|
|
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;
|
2015-06-10 01:34:54 +00:00
|
|
|
|
|
|
|
// Remember all the __except block targets.
|
|
|
|
for (auto &Handler : ActionList) {
|
|
|
|
if (auto *CH = dyn_cast<CatchHandler>(Handler.get())) {
|
|
|
|
auto *BA = cast<BlockAddress>(CH->getHandlerBlockOrFunc());
|
2015-07-08 18:08:52 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
for (BasicBlock *Pred : predecessors(BA->getBasicBlock()))
|
|
|
|
assert(Pred->isLandingPad() &&
|
|
|
|
"WinEHPrepare failed to split block");
|
|
|
|
#endif
|
2015-06-10 01:34:54 +00:00
|
|
|
ExceptBlocks.insert(BA->getBasicBlock());
|
|
|
|
}
|
|
|
|
}
|
2015-06-09 21:42:19 +00:00
|
|
|
}
|
|
|
|
insertStateNumberStore(RegNode, II, State);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-10 01:34:54 +00:00
|
|
|
|
2015-06-30 22:46:59 +00:00
|
|
|
// Insert llvm.x86.seh.restoreframe() into each __except block.
|
|
|
|
Function *RestoreFrame =
|
|
|
|
Intrinsic::getDeclaration(TheModule, Intrinsic::x86_seh_restoreframe);
|
2015-06-10 01:34:54 +00:00
|
|
|
for (BasicBlock *ExceptBB : ExceptBlocks) {
|
|
|
|
IRBuilder<> Builder(ExceptBB->begin());
|
2015-06-30 22:46:59 +00:00
|
|
|
Builder.CreateCall(RestoreFrame, {});
|
2015-06-10 01:34:54 +00:00
|
|
|
}
|
2015-06-09 21:42:19 +00:00
|
|
|
}
|
|
|
|
|
2015-05-28 22:00:24 +00:00
|
|
|
void WinEHStatePass::insertStateNumberStore(Value *ParentRegNode,
|
|
|
|
Instruction *IP, int State) {
|
|
|
|
IRBuilder<> Builder(IP);
|
|
|
|
Value *StateField =
|
|
|
|
Builder.CreateStructGEP(RegNodeTy, ParentRegNode, StateFieldIndex);
|
|
|
|
Builder.CreateStore(Builder.getInt32(State), StateField);
|
|
|
|
}
|