Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit c36c93b

Browse files
author
Mandeep Singh Grang
committed
[COFF, ARM64] Add support for MSVC buffer security check
Reviewers: rnk, mstorsjo, compnerd, efriedma, TomTan Reviewed By: rnk Subscribers: javed.absar, kristof.beyls, chrib, llvm-commits Differential Revision: https://reviews.llvm.org/D54248 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346469 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 1a89942 commit c36c93b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11668,6 +11668,39 @@ Value *AArch64TargetLowering::getIRStackGuard(IRBuilder<> &IRB) const {
1166811668
return TargetLowering::getIRStackGuard(IRB);
1166911669
}
1167011670

11671+
void AArch64TargetLowering::insertSSPDeclarations(Module &M) const {
11672+
// MSVC CRT provides functionalities for stack protection.
11673+
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment()) {
11674+
// MSVC CRT has a global variable holding security cookie.
11675+
M.getOrInsertGlobal("__security_cookie",
11676+
Type::getInt8PtrTy(M.getContext()));
11677+
11678+
// MSVC CRT has a function to validate security cookie.
11679+
auto *SecurityCheckCookie = cast<Function>(
11680+
M.getOrInsertFunction("__security_check_cookie",
11681+
Type::getVoidTy(M.getContext()),
11682+
Type::getInt8PtrTy(M.getContext())));
11683+
SecurityCheckCookie->setCallingConv(CallingConv::Win64);
11684+
SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg);
11685+
return;
11686+
}
11687+
TargetLowering::insertSSPDeclarations(M);
11688+
}
11689+
11690+
Value *AArch64TargetLowering::getSDagStackGuard(const Module &M) const {
11691+
// MSVC CRT has a global variable holding security cookie.
11692+
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
11693+
return M.getGlobalVariable("__security_cookie");
11694+
return TargetLowering::getSDagStackGuard(M);
11695+
}
11696+
11697+
Value *AArch64TargetLowering::getSSPStackGuardCheck(const Module &M) const {
11698+
// MSVC CRT has a function to validate security cookie.
11699+
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
11700+
return M.getFunction("__security_check_cookie");
11701+
return TargetLowering::getSSPStackGuardCheck(M);
11702+
}
11703+
1167111704
Value *AArch64TargetLowering::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
1167211705
// Android provides a fixed TLS slot for the SafeStack pointer. See the
1167311706
// definition of TLS_SLOT_SAFESTACK in

lib/Target/AArch64/AArch64ISelLowering.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ class AArch64TargetLowering : public TargetLowering {
401401
/// returns the address of that location. Otherwise, returns nullptr.
402402
Value *getIRStackGuard(IRBuilder<> &IRB) const override;
403403

404+
void insertSSPDeclarations(Module &M) const override;
405+
Value *getSDagStackGuard(const Module &M) const override;
406+
Value *getSSPStackGuardCheck(const Module &M) const override;
407+
404408
/// If the target has a standard location for the unsafe stack pointer,
405409
/// returns the address of that location. Otherwise, returns nullptr.
406410
Value *getSafeStackPointerLocation(IRBuilder<> &IRB) const override;

test/CodeGen/AArch64/stack-protector-target.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; RUN: llc -mtriple=aarch64-linux-android < %s -o - | FileCheck --check-prefix=ANDROID-AARCH64 %s
33
; RUN: llc -mtriple=aarch64-fuchsia < %s -o - | FileCheck --check-prefixes=FUCHSIA-AARCH64-COMMON,FUCHSIA-AARCH64-USER %s
44
; RUN: llc -mtriple=aarch64-fuchsia -code-model=kernel < %s -o - | FileCheck --check-prefixes=FUCHSIA-AARCH64-COMMON,FUCHSIA-AARCH64-KERNEL %s
5+
; RUN: llc -mtriple=aarch64-windows < %s -o - | FileCheck --check-prefix=WINDOWS-AARCH64 %s
56

67
define void @_Z1fv() sspreq {
78
entry:
@@ -27,3 +28,10 @@ declare void @_Z7CapturePi(i32*)
2728
; FUCHSIA-AARCH64-COMMON: ldur [[C:.*]], {{\[}}[[A]], #-16]
2829
; FUCHSIA-AARCH64-COMMON: ldr [[D:.*]], [sp,
2930
; FUCHSIA-AARCH64-COMMON: cmp [[C]], [[D]]
31+
32+
; WINDOWS-AARCH64: adrp x8, __security_cookie
33+
; WINDOWS-AARCH64: ldr x8, [x8, __security_cookie]
34+
; WINDOWS-AARCH64: str x8, [sp, #8]
35+
; WINDOWS-AARCH64: bl _Z7CapturePi
36+
; WINDOWS-AARCH64: ldr x0, [sp, #8]
37+
; WINDOWS-AARCH64: bl __security_check_cookie

0 commit comments

Comments
 (0)