Skip to content

Commit 6fedcd7

Browse files
committed
RuntimeLibcalls: Add entries for __security_check_cookie
Avoids hardcoding string name based on target, and gets the entry in the centralized list of emitted calls.
1 parent dc7c3c2 commit 6fedcd7

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
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: 17 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,8 @@ 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+
__security_check_cookie_arm64ec)>;
12561268

12571269
//===----------------------------------------------------------------------===//
12581270
// AMDGPU Runtime Libcalls
@@ -1500,6 +1512,7 @@ def ARMSystemLibrary
15001512
LibmHasFrexpF128, LibmHasLdexpF128,
15011513
WindowARMDivRemCalls,
15021514
WindowARMFPIntCasts,
1515+
SecurityCheckCookieIfWinMSVC,
15031516
AEABIDivRemCalls,
15041517
DarwinSinCosStret, DarwinExp10,
15051518
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
@@ -2159,6 +2172,7 @@ defvar X86CommonLibcalls =
21592172
DefaultRuntimeLibcallImpls_f80,
21602173
LibmHasExp10F32, LibmHasExp10F64, LibmHasExp10F80,
21612174
LibcallImpls<(add MostPowI), isNotOSMSVCRT>,
2175+
SecurityCheckCookieIfWinMSVC,
21622176
// FIXME: MSVCRT doesn't have powi. The f128 case is added as a
21632177
// hack for one test relying on it.
21642178
__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
@@ -21359,7 +21359,9 @@ bool ARMTargetLowering::useLoadStackGuardNode(const Module &M) const {
2135921359
}
2136021360

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

2136521367
// MSVC CRT has a global variable holding security cookie.
@@ -21368,23 +21370,32 @@ void ARMTargetLowering::insertSSPDeclarations(Module &M) const {
2136821370

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

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

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

0 commit comments

Comments
 (0)