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

Brad Smith brad at comstyle.com
Sun Aug 19 03:14:14 PDT 2012


On Tue, Aug 14, 2012 at 05:51:38PM -0400, Rafael Esp??ndola wrote:
> > +  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");
> 
> SSH?
> 
> Not sure if it is the patch being corrupted or what, but these lines
> show up with a lot of trailing space: 205 columns!

Oops, the trailing space was introduced through copy and pasting that
section.


"LSSH" is the name that's used as an internal label for the string
constant.

-- 
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 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=amd64-unknown-openbsd < %s -o - | grep "__guard"
+; RUN: llc -mtriple=amd64-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