[PATCH] D21101: [exceptions] Upgrade exception handlers when stack protector is used

Etienne Bergeron via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 10 14:13:25 PDT 2016


etienneb updated this revision to Diff 60403.
etienneb marked 4 inline comments as done.
etienneb added a comment.

comments


http://reviews.llvm.org/D21101

Files:
  lib/CodeGen/WinEHPrepare.cpp
  test/CodeGen/WinEH/wineh-promote-eh.ll

Index: test/CodeGen/WinEH/wineh-promote-eh.ll
===================================================================
--- /dev/null
+++ test/CodeGen/WinEH/wineh-promote-eh.ll
@@ -0,0 +1,16 @@
+; RUN: opt -mtriple=i686-windows-msvc -S -winehprepare %s | FileCheck %s
+
+declare i32 @_except_handler3(...)
+
+define void @test1a() personality i32 (...)* @_except_handler3 {
+; CHECK: define void @test1a() personality i32 (...)* @_except_handler3
+entry:
+  ret void
+}
+
+define void @test1b() ssp personality i32 (...)* @_except_handler3 {
+; CHECK: define void @test1b() [[attr:.*]] personality i32 (...)* @_except_handler4
+entry:
+  ret void
+}
+
Index: lib/CodeGen/WinEHPrepare.cpp
===================================================================
--- lib/CodeGen/WinEHPrepare.cpp
+++ lib/CodeGen/WinEHPrepare.cpp
@@ -67,6 +67,7 @@
   }
 
 private:
+  void promoteEHPersonality(Function &F);
   void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot);
   void
   insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot,
@@ -104,6 +105,10 @@
   if (!Fn.hasPersonalityFn())
     return false;
 
+  // When stack-protector is present, some exception handlers need to be
+  // promoted to a compatible handlers.
+  promoteEHPersonality(Fn);
+
   // Classify the personality to see what kind of preparation we need.
   Personality = classifyEHPersonality(Fn.getPersonalityFn());
 
@@ -464,6 +469,39 @@
   return FuncInfo.ClrEHUnwindMap.size() - 1;
 }
 
+static Value *getStackGuardEHPersonality(Value *Pers) {
+  Function *F =
+      Pers ? dyn_cast<Function>(Pers->stripPointerCasts()) : nullptr;
+  if (!F)
+    return nullptr;
+
+  // TODO(etienneb): Upgrade exception handlers when they are working.
+  StringRef NewName = llvm::StringSwitch<StringRef>(F->getName())  
+      .Case("_except_handler3", "_except_handler4")
+      .Default("");
+  if (NewName.empty())
+    return nullptr;
+
+  Module *M = F->getParent();
+  return M->getOrInsertFunction("_except_handler4", F->getFunctionType(),
+                                F->getAttributes());
+}
+
+void WinEHPrepare::promoteEHPersonality(Function &F) {
+  // Promote the exception handler when stack protection is activated.
+  if (!F.hasFnAttribute(Attribute::StackProtect) &&
+      !F.hasFnAttribute(Attribute::StackProtectReq) &&
+      !F.hasFnAttribute(Attribute::StackProtectStrong))
+    return;
+
+  if (Value *PersonalityFn = F.getPersonalityFn()) {
+    if (Value *Personality = getStackGuardEHPersonality(PersonalityFn)) {
+      Function* PromotedFn = cast<Function>(Personality);
+      F.setPersonalityFn(PromotedFn);
+    }
+  }
+}    
+
 void llvm::calculateClrEHStateNumbers(const Function *Fn,
                                       WinEHFuncInfo &FuncInfo) {
   // Return if it's already been done.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21101.60403.patch
Type: text/x-patch
Size: 2808 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160610/cc514bed/attachment.bin>


More information about the llvm-commits mailing list