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

Brad Smith brad at comstyle.com
Tue Aug 14 03:58:54 PDT 2012


On Mon, Aug 13, 2012 at 03:28:03PM -0700, Eli Friedman wrote:
> On Mon, Aug 13, 2012 at 3:10 PM, Brad Smith <brad at comstyle.com> wrote:
> > 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.
> 
> +        if (Triple(TLI->getTargetMachine().getTargetTriple()).getOS()
> == llvm::Triple::OpenBSD)
> +          StackGuardVar = M->getOrInsertGlobal("__guard", PtrTy);
> 
> 80 columns.  (Also elsewhere.)
> 
> +  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);
> 
> You're declaring a global of type [N x i1], and initializing it with
> an initializer of type [N x i8].  Also this doesn't compile on trunk.

Sorry I had tested the diff against 3.1. I have tested this against trunk
and see what you're talking about.

How about this?


Index: lib/CodeGen/StackProtector.cpp
===================================================================
--- lib/CodeGen/StackProtector.cpp	(revision 161828)
+++ 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,11 @@
         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 +260,25 @@
 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::getInt8Ty(F->getContext()),                                                                                                                                      
+                       F->getName().size() + 1),                                                                                                                                                             
+                       true, GlobalVariable::PrivateLinkage,                                                                                                                                                 
+                       ConstantDataArray::getString(F->getContext(),                                                                                                                                         
+                               F->getName(), true),                                                                                                                                                          
+                       "SSH");
+    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 161828)
+++ 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]
 

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




More information about the llvm-commits mailing list