Skip to content

Commit cea07b7

Browse files
arsenmgithub-actions[bot]
authored andcommitted
Automerge: RuntimeLibcalls: Add entries for __security_check_cookie (#151843)
Avoids hardcoding string name based on target, and gets the entry in the centralized list of emitted calls.
2 parents d06955b + 342bf58 commit cea07b7

File tree

6 files changed

+53
-22
lines changed

6 files changed

+53
-22
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,6 +3553,11 @@ class LLVM_ABI TargetLoweringBase {
35533553
return Libcalls.getLibcallName(Call);
35543554
}
35553555

3556+
/// Get the libcall routine name for the specified libcall implementation
3557+
const char *getLibcallImplName(RTLIB::LibcallImpl Call) const {
3558+
return Libcalls.getLibcallImplName(Call);
3559+
}
3560+
35563561
const char *getMemcpyName() const { return Libcalls.getMemcpyName(); }
35573562

35583563
/// Get the comparison predicate that's to be used to test the result of the

llvm/include/llvm/IR/RuntimeLibcalls.td

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def isNotOSMSVCRT : RuntimeLibcallPredicate<"!TT.isOSMSVCRT()">;
2525
def isPS : RuntimeLibcallPredicate<"TT.isPS()">;
2626
def isNotOSWindowsOrIsCygwinMinGW
2727
: RuntimeLibcallPredicate<"!TT.isOSWindows() || TT.isOSCygMing()">;
28-
28+
def isWindowsMSVCEnvironment : RuntimeLibcallPredicate<
29+
[{TT.isWindowsMSVCEnvironment()}]>;
2930

3031
def isGNUEnvironment : RuntimeLibcallPredicate<"TT.isGNUEnvironment()">;
3132
def darwinHasSinCosStret : RuntimeLibcallPredicate<"darwinHasSinCosStret(TT)">;
@@ -369,6 +370,8 @@ def STACK_SMASH_HANDLER : RuntimeLibcall;
369370
// Safe stack
370371
def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;
371372

373+
def SECURITY_CHECK_COOKIE : RuntimeLibcall;
374+
372375
// Deoptimization
373376
def DEOPTIMIZE : RuntimeLibcall;
374377

@@ -1009,6 +1012,10 @@ def __stack_smash_handler : RuntimeLibcallImpl<STACK_SMASH_HANDLER>;
10091012

10101013
def __riscv_flush_icache : RuntimeLibcallImpl<RISCV_FLUSH_ICACHE>;
10111014

1015+
def __security_check_cookie : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE>;
1016+
def __security_check_cookie_arm64ec : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE,
1017+
"#__security_check_cookie_arm64ec">;
1018+
10121019
//===----------------------------------------------------------------------===//
10131020
// F128 libm Runtime Libcalls
10141021
//===----------------------------------------------------------------------===//
@@ -1111,6 +1118,9 @@ defvar DarwinSinCosStret = LibcallImpls<(add __sincosf_stret, __sincos_stret),
11111118
darwinHasSinCosStret>;
11121119
defvar DarwinExp10 = LibcallImpls<(add __exp10f, __exp10), darwinHasExp10>;
11131120

1121+
defvar SecurityCheckCookieIfWinMSVC =
1122+
LibcallImpls<(add __security_check_cookie), isWindowsMSVCEnvironment>;
1123+
11141124
defvar LibmHasSinCosF32 = LibcallImpls<(add sincosf), hasSinCos>;
11151125
defvar LibmHasSinCosF64 = LibcallImpls<(add sincos), hasSinCos>;
11161126
defvar LibmHasSinCosF80 = LibcallImpls<(add sincos_f80), hasSinCos>;
@@ -1233,7 +1243,8 @@ def AArch64SystemLibrary : SystemRuntimeLibrary<
12331243
DarwinExp10, DarwinSinCosStret,
12341244
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
12351245
DefaultLibmExp10,
1236-
DefaultStackProtector)
1246+
DefaultStackProtector,
1247+
SecurityCheckCookieIfWinMSVC)
12371248
>;
12381249

12391250
// Prepend a # to every name
@@ -1252,7 +1263,9 @@ def arm64ec___stack_chk_fail : DuplicateLibcallImplWithPrefix<__stack_chk_fail,
12521263
def WindowsARM64ECSystemLibrary
12531264
: SystemRuntimeLibrary<isWindowsArm64EC,
12541265
(add WinArm64ECDefaultRuntimeLibcallImpls,
1255-
arm64ec___stack_chk_fail)>;
1266+
arm64ec___stack_chk_fail,
1267+
LibcallImpls<(add __security_check_cookie_arm64ec),
1268+
isWindowsMSVCEnvironment>)>;
12561269

12571270
//===----------------------------------------------------------------------===//
12581271
// AMDGPU Runtime Libcalls
@@ -1500,6 +1513,7 @@ def ARMSystemLibrary
15001513
LibmHasFrexpF128, LibmHasLdexpF128,
15011514
WindowARMDivRemCalls,
15021515
WindowARMFPIntCasts,
1516+
SecurityCheckCookieIfWinMSVC,
15031517
AEABIDivRemCalls,
15041518
DarwinSinCosStret, DarwinExp10,
15051519
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
@@ -2158,6 +2172,7 @@ defvar X86CommonLibcalls =
21582172
DefaultRuntimeLibcallImpls_f80,
21592173
LibmHasExp10F32, LibmHasExp10F64, LibmHasExp10F80,
21602174
LibcallImpls<(add MostPowI), isNotOSMSVCRT>,
2175+
SecurityCheckCookieIfWinMSVC,
21612176
// FIXME: MSVCRT doesn't have powi. The f128 case is added as a
21622177
// hack for one test relying on it.
21632178
__powitf2_f128,

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28609,14 +28609,16 @@ Value *AArch64TargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
2860928609

2861028610
void AArch64TargetLowering::insertSSPDeclarations(Module &M) const {
2861128611
// MSVC CRT provides functionalities for stack protection.
28612-
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment()) {
28612+
RTLIB::LibcallImpl SecurityCheckCookieLibcall =
28613+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
28614+
if (SecurityCheckCookieLibcall != RTLIB::Unsupported) {
2861328615
// MSVC CRT has a global variable holding security cookie.
2861428616
M.getOrInsertGlobal("__security_cookie",
2861528617
PointerType::getUnqual(M.getContext()));
2861628618

2861728619
// MSVC CRT has a function to validate security cookie.
2861828620
FunctionCallee SecurityCheckCookie =
28619-
M.getOrInsertFunction(Subtarget->getSecurityCheckCookieName(),
28621+
M.getOrInsertFunction(getLibcallImplName(SecurityCheckCookieLibcall),
2862028622
Type::getVoidTy(M.getContext()),
2862128623
PointerType::getUnqual(M.getContext()));
2862228624
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) {
@@ -28637,8 +28639,10 @@ Value *AArch64TargetLowering::getSDagStackGuard(const Module &M) const {
2863728639

2863828640
Function *AArch64TargetLowering::getSSPStackGuardCheck(const Module &M) const {
2863928641
// MSVC CRT has a function to validate security cookie.
28640-
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
28641-
return M.getFunction(Subtarget->getSecurityCheckCookieName());
28642+
RTLIB::LibcallImpl SecurityCheckCookieLibcall =
28643+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
28644+
if (SecurityCheckCookieLibcall != RTLIB::Unsupported)
28645+
return M.getFunction(getLibcallImplName(SecurityCheckCookieLibcall));
2864228646
return TargetLowering::getSSPStackGuardCheck(M);
2864328647
}
2864428648

llvm/lib/Target/AArch64/AArch64Subtarget.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,6 @@ class AArch64Subtarget final : public AArch64GenSubtargetInfo {
451451
return "__chkstk";
452452
}
453453

454-
const char* getSecurityCheckCookieName() const {
455-
if (isWindowsArm64EC())
456-
return "#__security_check_cookie_arm64ec";
457-
return "__security_check_cookie";
458-
}
459-
460454
/// Choose a method of checking LR before performing a tail call.
461455
AArch64PAuth::AuthCheckMethod
462456
getAuthenticatedLRCheckMethod(const MachineFunction &MF) const;

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21358,7 +21358,9 @@ bool ARMTargetLowering::useLoadStackGuardNode(const Module &M) const {
2135821358
}
2135921359

2136021360
void ARMTargetLowering::insertSSPDeclarations(Module &M) const {
21361-
if (!Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
21361+
RTLIB::LibcallImpl SecurityCheckCookieLibcall =
21362+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
21363+
if (SecurityCheckCookieLibcall == RTLIB::Unsupported)
2136221364
return TargetLowering::insertSSPDeclarations(M);
2136321365

2136421366
// MSVC CRT has a global variable holding security cookie.
@@ -21367,23 +21369,32 @@ void ARMTargetLowering::insertSSPDeclarations(Module &M) const {
2136721369

2136821370
// MSVC CRT has a function to validate security cookie.
2136921371
FunctionCallee SecurityCheckCookie = M.getOrInsertFunction(
21370-
"__security_check_cookie", Type::getVoidTy(M.getContext()),
21371-
PointerType::getUnqual(M.getContext()));
21372+
getLibcallImplName(SecurityCheckCookieLibcall),
21373+
Type::getVoidTy(M.getContext()), PointerType::getUnqual(M.getContext()));
2137221374
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee()))
2137321375
F->addParamAttr(0, Attribute::AttrKind::InReg);
2137421376
}
2137521377

2137621378
Value *ARMTargetLowering::getSDagStackGuard(const Module &M) const {
21377-
// MSVC CRT has a global variable holding security cookie.
21378-
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
21379+
RTLIB::LibcallImpl SecurityCheckCookieLibcall =
21380+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
21381+
if (SecurityCheckCookieLibcall != RTLIB::Unsupported) {
21382+
// MSVC CRT has a global variable holding security cookie.
21383+
//
21384+
// FIXME: We have a libcall entry for the correlated check function, but not
21385+
// the global name.
2137921386
return M.getGlobalVariable("__security_cookie");
21387+
}
21388+
2138021389
return TargetLowering::getSDagStackGuard(M);
2138121390
}
2138221391

2138321392
Function *ARMTargetLowering::getSSPStackGuardCheck(const Module &M) const {
2138421393
// MSVC CRT has a function to validate security cookie.
21385-
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
21386-
return M.getFunction("__security_check_cookie");
21394+
RTLIB::LibcallImpl SecurityCheckCookie =
21395+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
21396+
if (SecurityCheckCookie != RTLIB::Unsupported)
21397+
return M.getFunction(getLibcallImplName(SecurityCheckCookie));
2138721398
return TargetLowering::getSSPStackGuardCheck(M);
2138821399
}
2138921400

llvm/test/CodeGen/AArch64/stacksmash-arm64ec.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
; RUN: llc -mtriple=arm64ec-unknown-windows-gnu < %s | FileCheck %s
1+
; RUN: llc -mtriple=arm64ec-unknown-windows < %s | FileCheck -check-prefixes=CHECK,NONGNU %s
2+
; RUN: llc -mtriple=arm64ec-unknown-windows-gnu < %s | FileCheck -check-prefixes=CHECK,GNU %s
23

34
; CHECK-LABEL: func = "#func"
45
; CHECK: bl "#other"
5-
; CHECK: bl "#__stack_chk_fail"
6+
; NONGNU: bl "#__security_check_cookie_arm64ec"
7+
; GNU: bl "#__stack_chk_fail"
68
define void @func() #0 {
79
entry:
810
%buf = alloca [10 x i8], align 1

0 commit comments

Comments
 (0)