[PATCH] D37405: [safestack] Experimental mode where stack pointer is accessed with a function call.

Evgenii Stepanov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 1 16:27:05 PDT 2017


eugenis created this revision.
Herald added subscribers: hiraditya, kristof.beyls, aemerson.

This adds an -mllvm flag that forces the use of a runtime function call to
get the unsafe stack pointer, the same that is currently used on non-x86, non-aarch64 android.


https://reviews.llvm.org/D37405

Files:
  compiler-rt/lib/safestack/safestack.cc
  compiler-rt/test/safestack/pthread.c
  llvm/lib/CodeGen/SafeStack.cpp
  llvm/test/CodeGen/X86/safestack.ll


Index: llvm/test/CodeGen/X86/safestack.ll
===================================================================
--- llvm/test/CodeGen/X86/safestack.ll
+++ llvm/test/CodeGen/X86/safestack.ll
@@ -4,6 +4,8 @@
 ; RUN: llc -mtriple=x86_64-linux-android < %s -o - | FileCheck --check-prefix=ANDROID-X64 %s
 ; RUN: llc -mtriple=x86_64-fuchsia < %s -o - | FileCheck --check-prefix=FUCHSIA-X64 %s
 
+; RUN: llc -mtriple=i386-linux -safestack-use-pointer-address < %s -o - | FileCheck --check-prefix=LINUX-I386-PA %s
+
 define void @_Z1fv() safestack {
 entry:
   %x = alloca i32, align 4
@@ -35,3 +37,9 @@
 ; FUCHSIA-X64: movq %fs:24, %[[A:.*]]
 ; FUCHSIA-X64: leaq -16(%[[A]]), %[[B:.*]]
 ; FUCHSIA-X64: movq %[[B]], %fs:24
+
+; LINUX-I386-PA: calll __safestack_pointer_address
+; LINUX-I386-PA: movl %eax, %[[A:.*]]
+; LINUX-I386-PA: movl (%eax), %[[B:.*]]
+; LINUX-I386-PA: leal -16(%[[B]]), %[[C:.*]]
+; LINUX-I386-PA: movl %[[C]], (%[[A]])
Index: llvm/lib/CodeGen/SafeStack.cpp
===================================================================
--- llvm/lib/CodeGen/SafeStack.cpp
+++ llvm/lib/CodeGen/SafeStack.cpp
@@ -69,6 +69,9 @@
 
 } // namespace llvm
 
+static cl::opt<bool> SafeStackUsePointerAddress("safestack-use-pointer-address",
+                                                cl::init(false), cl::Hidden);
+
 namespace {
 
 /// Rewrite an SCEV expression for a memory access address to an expression that
@@ -711,7 +714,13 @@
     ++NumUnsafeStackRestorePointsFunctions;
 
   IRBuilder<> IRB(&F.front(), F.begin()->getFirstInsertionPt());
-  UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
+  if (SafeStackUsePointerAddress) {
+    Value *Fn = F.getParent()->getOrInsertFunction(
+        "__safestack_pointer_address", StackPtrTy->getPointerTo(0));
+    UnsafeStackPtr = IRB.CreateCall(Fn);
+  } else {
+    UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
+  }
 
   // Load the current stack pointer (we'll also use it as a base pointer).
   // FIXME: use a dedicated register for it ?
Index: compiler-rt/test/safestack/pthread.c
===================================================================
--- compiler-rt/test/safestack/pthread.c
+++ compiler-rt/test/safestack/pthread.c
@@ -1,5 +1,8 @@
 // RUN: %clang_safestack %s -pthread -o %t
-// RUN: %run %t
+// RUN: not %run %t
+
+// RUN: %clang_safestack %s -pthread -mllvm -safestack-use-pointer-address -o %t
+// RUN: not %run %t
 
 // XFAIL: darwin
 
Index: compiler-rt/lib/safestack/safestack.cc
===================================================================
--- compiler-rt/lib/safestack/safestack.cc
+++ compiler-rt/lib/safestack/safestack.cc
@@ -253,3 +253,8 @@
     __attribute__((visibility("default"))) void *__get_unsafe_stack_ptr() {
   return __safestack_unsafe_stack_ptr;
 }
+
+extern "C" __attribute__((visibility("default"))) void **
+__safestack_pointer_address() {
+  return &__safestack_unsafe_stack_ptr;
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37405.113623.patch
Type: text/x-patch
Size: 2904 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170901/05aeccfe/attachment.bin>


More information about the llvm-commits mailing list