[llvm] r365283 - [ARM] Add support for MSVC stack cookie checking

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 7 11:57:31 PDT 2019


Author: mstorsjo
Date: Sun Jul  7 11:57:31 2019
New Revision: 365283

URL: http://llvm.org/viewvc/llvm-project?rev=365283&view=rev
Log:
[ARM] Add support for MSVC stack cookie checking

Heavily based on the same for AArch64, from SVN r346469.

Differential Revision: https://reviews.llvm.org/D64292

Added:
    llvm/trunk/test/CodeGen/ARM/Windows/stack-protector-msvc.ll
Modified:
    llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp
    llvm/trunk/lib/Target/ARM/ARMISelLowering.h

Modified: llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp?rev=365283&r1=365282&r2=365283&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/ARM/ARMISelLowering.cpp Sun Jul  7 11:57:31 2019
@@ -15237,6 +15237,36 @@ bool ARMTargetLowering::useLoadStackGuar
   return Subtarget->isTargetMachO();
 }
 
+void ARMTargetLowering::insertSSPDeclarations(Module &M) const {
+  if (!Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
+    return TargetLowering::insertSSPDeclarations(M);
+
+  // MSVC CRT has a global variable holding security cookie.
+  M.getOrInsertGlobal("__security_cookie",
+                      Type::getInt8PtrTy(M.getContext()));
+
+  // MSVC CRT has a function to validate security cookie.
+  FunctionCallee SecurityCheckCookie = M.getOrInsertFunction(
+      "__security_check_cookie", Type::getVoidTy(M.getContext()),
+      Type::getInt8PtrTy(M.getContext()));
+  if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee()))
+    F->addAttribute(1, Attribute::AttrKind::InReg);
+}
+
+Value *ARMTargetLowering::getSDagStackGuard(const Module &M) const {
+  // MSVC CRT has a global variable holding security cookie.
+  if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
+    return M.getGlobalVariable("__security_cookie");
+  return TargetLowering::getSDagStackGuard(M);
+}
+
+Function *ARMTargetLowering::getSSPStackGuardCheck(const Module &M) const {
+  // MSVC CRT has a function to validate security cookie.
+  if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
+    return M.getFunction("__security_check_cookie");
+  return TargetLowering::getSSPStackGuardCheck(M);
+}
+
 bool ARMTargetLowering::canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
                                                   unsigned &Cost) const {
   // If we do not have NEON, vector types are not natively supported.

Modified: llvm/trunk/lib/Target/ARM/ARMISelLowering.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/ARM/ARMISelLowering.h?rev=365283&r1=365282&r2=365283&view=diff
==============================================================================
--- llvm/trunk/lib/Target/ARM/ARMISelLowering.h (original)
+++ llvm/trunk/lib/Target/ARM/ARMISelLowering.h Sun Jul  7 11:57:31 2019
@@ -555,6 +555,10 @@ class VectorType;
 
     bool useLoadStackGuardNode() const override;
 
+    void insertSSPDeclarations(Module &M) const override;
+    Value *getSDagStackGuard(const Module &M) const override;
+    Function *getSSPStackGuardCheck(const Module &M) const override;
+
     bool canCombineStoreAndExtract(Type *VectorTy, Value *Idx,
                                    unsigned &Cost) const override;
 

Added: llvm/trunk/test/CodeGen/ARM/Windows/stack-protector-msvc.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/ARM/Windows/stack-protector-msvc.ll?rev=365283&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/ARM/Windows/stack-protector-msvc.ll (added)
+++ llvm/trunk/test/CodeGen/ARM/Windows/stack-protector-msvc.ll Sun Jul  7 11:57:31 2019
@@ -0,0 +1,20 @@
+; RUN: llc -mtriple=thumbv7-windows-msvc < %s -o - | FileCheck --check-prefix=MSVC %s
+; RUN: llc -mtriple=thumbv7-windows-msvc -O0 < %s -o - | FileCheck --check-prefix=MSVC %s
+
+define void @_Z1fv() sspreq {
+entry:
+  %x = alloca i32, align 4
+  %0 = bitcast i32* %x to i8*
+  call void @_Z7CapturePi(i32* nonnull %x)
+  ret void
+}
+
+declare void @_Z7CapturePi(i32*)
+
+; MSVC: movw r0, :lower16:__security_cookie
+; MSVC: movt r0, :upper16:__security_cookie
+; MSVC: ldr r0, [r0]
+; MSVC: str r0, [sp, #4]
+; MSVC: bl  _Z7CapturePi
+; MSVC: ldr r0, [sp, #4]
+; MSVC: bl  __security_check_cookie




More information about the llvm-commits mailing list