[llvm-commits] [PATCH] Make use of OpenBSD's libc routines for stack protector

Brad Smith brad at comstyle.com
Mon Aug 13 15:10:19 PDT 2012


On Sat, Aug 11, 2012 at 03:49:15AM -0400, Brad Smith wrote:
> On Thu, Aug 09, 2012 at 01:40:14AM -0700, Eli Friedman wrote:
> > On Thu, Aug 9, 2012 at 12:52 AM, Brad Smith <brad at comstyle.com> wrote:
> > > This makes LLVM use OpenBSD's libc routines for stack protector.
> > 
> > +#if defined(__OpenBSD__)
> > 
> > This is wrong; you want to check if the target is OpenBSD, not if the
> > host is OpenBSD.
> 
> You're right. How about this?

Ok, here is the diff with a test addition.

-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

-------------- next part --------------
Index: lib/CodeGen/StackProtector.cpp
===================================================================
--- lib/CodeGen/StackProtector.cpp	(revision 161795)
+++ lib/CodeGen/StackProtector.cpp	(working copy)
@@ -16,6 +16,7 @@
 
 #define DEBUG_TYPE "stack-protector"
 #include "llvm/CodeGen/Passes.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Attributes.h"
 #include "llvm/Constants.h"
@@ -178,7 +179,10 @@
         StackGuardVar = ConstantExpr::getIntToPtr(OffsetVal,
                                       PointerType::get(PtrTy, AddressSpace));
       } else {
-        StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
+        if (Triple(TLI->getTargetMachine().getTargetTriple()).getOS() == llvm::Triple::OpenBSD)
+          StackGuardVar = M->getOrInsertGlobal("__guard", PtrTy);
+        else
+          StackGuardVar = M->getOrInsertGlobal("__stack_chk_guard", PtrTy);
       }
 
       BasicBlock &Entry = F->getEntryBlock();
@@ -255,10 +259,24 @@
 BasicBlock *StackProtector::CreateFailBB() {
   BasicBlock *FailBB = BasicBlock::Create(F->getContext(),
                                           "CallStackCheckFailBlk", F);
-  Constant *StackChkFail =
-    M->getOrInsertFunction("__stack_chk_fail",
-                           Type::getVoidTy(F->getContext()), NULL);
-  CallInst::Create(StackChkFail, "", FailBB);
+  if (Triple(TLI->getTargetMachine().getTargetTriple()).getOS() == llvm::Triple::OpenBSD) {
+    Constant *StackChkFail =
+      M->getOrInsertFunction("__stack_smash_handler",
+                             Type::getVoidTy(F->getContext()), NULL);
+    Constant *G = new GlobalVariable(*M,
+                          ArrayType::get(Type::getInt1Ty(F->getContext()),
+                          F->getName().size() + 1),
+                          true, GlobalVariable::PrivateLinkage, 
+                          ConstantDataArray::getString(F->getContext(),
+                                  F->getName(), true),
+                           "SSH", false, 0);
+    CallInst::Create(StackChkFail, G, "", FailBB);
+  } else {
+    Constant *StackChkFail =
+      M->getOrInsertFunction("__stack_chk_fail",
+                             Type::getVoidTy(F->getContext()), NULL);
+    CallInst::Create(StackChkFail, "", FailBB);
+  }
   new UnreachableInst(F->getContext(), FailBB);
   return FailBB;
 }
Index: test/CodeGen/X86/stack-protector.ll
===================================================================
--- test/CodeGen/X86/stack-protector.ll	(revision 161795)
+++ test/CodeGen/X86/stack-protector.ll	(working copy)
@@ -3,6 +3,8 @@
 ; RUN: llc -code-model=kernel -mtriple=x86_64-pc-linux-gnu < %s -o - | grep %gs:
 ; RUN: llc -mtriple=x86_64-apple-darwin < %s -o - | grep "__stack_chk_guard"
 ; RUN: llc -mtriple=x86_64-apple-darwin < %s -o - | grep "__stack_chk_fail"
+; RUN: llc -mtriple=x86_64-unknown-openbsd < %s -o - | grep "__guard"
+; RUN: llc -mtriple=x86_64-unknown-openbsd < %s -o - | grep "__stack_smash_handler"
 
 @"\01LC" = internal constant [11 x i8] c"buf == %s\0A\00"		; <[11 x i8]*> [#uses=1]
 


More information about the llvm-commits mailing list