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; +} 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: llvm/lib/CodeGen/SafeStack.cpp =================================================================== --- llvm/lib/CodeGen/SafeStack.cpp +++ llvm/lib/CodeGen/SafeStack.cpp @@ -69,6 +69,9 @@ } // namespace llvm +static cl::opt 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: 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]])