Disable TLS for stack protector on Android API<17.

The TLS slot did not exist back then.

llvm-svn: 296014
This commit is contained in:
Evgeniy Stepanov 2017-02-23 21:06:35 +00:00
parent ae9dadecf3
commit ee2d77f6d6
5 changed files with 64 additions and 28 deletions

View File

@ -560,7 +560,8 @@ public:
/// Tests whether the OS uses glibc.
bool isOSGlibc() const {
return getOS() == Triple::Linux || getOS() == Triple::KFreeBSD;
return (getOS() == Triple::Linux || getOS() == Triple::KFreeBSD) &&
!isAndroid();
}
/// Tests whether the OS uses the ELF binary format.
@ -599,6 +600,19 @@ public:
/// Tests whether the target is Android
bool isAndroid() const { return getEnvironment() == Triple::Android; }
bool isAndroidVersionLT(unsigned Major) const {
assert(isAndroid() && "Not an Android triple!");
unsigned Env[3];
getEnvironmentVersion(Env[0], Env[1], Env[2]);
// 64-bit targets did not exist before API level 21 (Lollipop).
if (isArch64Bit() && Env[0] < 21)
Env[0] = 21;
return Env[0] < Major;
}
/// Tests whether the environment is musl-libc
bool isMusl() const {
return getEnvironment() == Triple::Musl ||

View File

@ -719,7 +719,7 @@ bool X86DAGToDAGISel::matchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){
// For more information see http://people.redhat.com/drepper/tls.pdf
if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address))
if (C->getSExtValue() == 0 && AM.Segment.getNode() == nullptr &&
Subtarget->isTargetGlibc())
(Subtarget->isTargetGlibc() || Subtarget->isTargetAndroid()))
switch (N->getPointerInfo().getAddrSpace()) {
case 256:
AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);

View File

@ -2005,10 +2005,16 @@ unsigned X86TargetLowering::getAddressSpace() const {
return 256;
}
static bool hasStackGuardSlotTLS(const Triple &TargetTriple) {
return TargetTriple.isOSGlibc() ||
(TargetTriple.isAndroid() && !TargetTriple.isAndroidVersionLT(17));
}
Value *X86TargetLowering::getIRStackGuard(IRBuilder<> &IRB) const {
// glibc has a special slot for the stack guard in tcbhead_t, use it instead
// of the usual global variable (see sysdeps/{i386,x86_64}/nptl/tls.h)
if (!Subtarget.isTargetGlibc())
// glibc and bionic have a special slot for the stack guard in tcbhead_t, use
// it instead of the usual global variable (see
// sysdeps/{i386,x86_64}/nptl/tls.h)
if (!hasStackGuardSlotTLS(Subtarget.getTargetTriple()))
return TargetLowering::getIRStackGuard(IRB);
// %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
@ -2036,8 +2042,8 @@ void X86TargetLowering::insertSSPDeclarations(Module &M) const {
SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg);
return;
}
// glibc has a special slot for the stack guard.
if (Subtarget.isTargetGlibc())
// glibc and bionic have a special slot for the stack guard.
if (hasStackGuardSlotTLS(Subtarget.getTargetTriple()))
return;
TargetLowering::insertSSPDeclarations(M);
}

View File

@ -1,10 +1,17 @@
; Test target-specific stack cookie location.
; RUN: llc -mtriple=i386-linux < %s -o - | FileCheck --check-prefix=LINUX-I386 %s
; RUN: llc -mtriple=x86_64-linux < %s -o - | FileCheck --check-prefix=LINUX-X64 %s
; RUN: llc -mtriple=i386-linux-android < %s -o - | FileCheck --check-prefix=LINUX-I386 %s
; RUN: llc -mtriple=x86_64-linux-android < %s -o - | FileCheck --check-prefix=LINUX-X64 %s
; RUN: llc -mtriple=i386-kfreebsd < %s -o - | FileCheck --check-prefix=LINUX-I386 %s
; RUN: llc -mtriple=x86_64-kfreebsd < %s -o - | FileCheck --check-prefix=LINUX-X64 %s
; RUN: llc -mtriple=i386-linux < %s -o - | FileCheck --check-prefix=I386-TLS %s
; RUN: llc -mtriple=x86_64-linux < %s -o - | FileCheck --check-prefix=X64-TLS %s
; RUN: llc -mtriple=i386-linux-android < %s -o - | FileCheck --check-prefix=I386 %s
; RUN: llc -mtriple=i386-linux-android16 < %s -o - | FileCheck --check-prefix=I386 %s
; RUN: llc -mtriple=i386-linux-android17 < %s -o - | FileCheck --check-prefix=I386-TLS %s
; RUN: llc -mtriple=i386-linux-android24 < %s -o - | FileCheck --check-prefix=I386-TLS %s
; RUN: llc -mtriple=x86_64-linux-android < %s -o - | FileCheck --check-prefix=X64-TLS %s
; RUN: llc -mtriple=x86_64-linux-android17 < %s -o - | FileCheck --check-prefix=X64-TLS %s
; RUN: llc -mtriple=x86_64-linux-android24 < %s -o - | FileCheck --check-prefix=X64-TLS %s
; RUN: llc -mtriple=i386-kfreebsd < %s -o - | FileCheck --check-prefix=I386-TLS %s
; RUN: llc -mtriple=x86_64-kfreebsd < %s -o - | FileCheck --check-prefix=X64-TLS %s
define void @_Z1fv() sspreq {
entry:
@ -16,12 +23,17 @@ entry:
declare void @_Z7CapturePi(i32*)
; LINUX-X64: movq %fs:40, %[[B:.*]]
; LINUX-X64: movq %[[B]], 16(%rsp)
; LINUX-X64: movq %fs:40, %[[C:.*]]
; LINUX-X64: cmpq 16(%rsp), %[[C]]
; X64-TLS: movq %fs:40, %[[B:.*]]
; X64-TLS: movq %[[B]], 16(%rsp)
; X64-TLS: movq %fs:40, %[[C:.*]]
; X64-TLS: cmpq 16(%rsp), %[[C]]
; LINUX-I386: movl %gs:20, %[[B:.*]]
; LINUX-I386: movl %[[B]], 8(%esp)
; LINUX-I386: movl %gs:20, %[[C:.*]]
; LINUX-I386: cmpl 8(%esp), %[[C]]
; I386: movl __stack_chk_guard, %[[B:.*]]
; I386: movl %[[B]], 8(%esp)
; I386: movl __stack_chk_guard, %[[C:.*]]
; I386: cmpl 8(%esp), %[[C]]
; I386-TLS: movl %gs:20, %[[B:.*]]
; I386-TLS: movl %[[B]], 8(%esp)
; I386-TLS: movl %gs:20, %[[C:.*]]
; I386-TLS: cmpl 8(%esp), %[[C]]

View File

@ -1,18 +1,22 @@
; RUN: opt -safe-stack -S -mtriple=i686-pc-linux-gnu < %s -o - | FileCheck --check-prefix=TLS --check-prefix=TLS32 %s
; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck --check-prefix=TLS --check-prefix=TLS64 %s
; RUN: opt -safe-stack -S -mtriple=i686-linux-android < %s -o - | FileCheck --check-prefix=TLS --check-prefix=TLS32 %s
; RUN: opt -safe-stack -S -mtriple=x86_64-linux-android < %s -o - | FileCheck --check-prefix=TLS --check-prefix=TLS64 %s
; RUN: opt -safe-stack -S -mtriple=i686-pc-linux-gnu < %s -o - | FileCheck --check-prefixes=COMMON,TLS32 %s
; RUN: opt -safe-stack -S -mtriple=x86_64-pc-linux-gnu < %s -o - | FileCheck --check-prefixes=COMMON,TLS64 %s
; RUN: opt -safe-stack -S -mtriple=i686-linux-android < %s -o - | FileCheck --check-prefixes=COMMON,GLOBAL32 %s
; RUN: opt -safe-stack -S -mtriple=i686-linux-android24 < %s -o - | FileCheck --check-prefixes=COMMON,TLS32 %s
; RUN: opt -safe-stack -S -mtriple=x86_64-linux-android < %s -o - | FileCheck --check-prefixes=COMMON,TLS64 %s
define void @foo() safestack sspreq {
entry:
; TLS32: %[[StackGuard:.*]] = load i8*, i8* addrspace(256)* inttoptr (i32 20 to i8* addrspace(256)*)
; TLS64: %[[StackGuard:.*]] = load i8*, i8* addrspace(257)* inttoptr (i32 40 to i8* addrspace(257)*)
; TLS: store i8* %[[StackGuard]], i8** %[[StackGuardSlot:.*]]
; GLOBAL32: %[[StackGuard:.*]] = load i8*, i8** @__stack_chk_guard
; COMMON: store i8* %[[StackGuard]], i8** %[[StackGuardSlot:.*]]
%a = alloca i8, align 1
call void @Capture(i8* %a)
; TLS: %[[A:.*]] = load i8*, i8** %[[StackGuardSlot]]
; TLS: icmp ne i8* %[[StackGuard]], %[[A]]
; COMMON: %[[A:.*]] = load i8*, i8** %[[StackGuardSlot]]
; COMMON: icmp ne i8* %[[StackGuard]], %[[A]]
ret void
}