Skip to content

Commit efbfb42

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 779868d commit efbfb42

File tree

5 files changed

+47
-19
lines changed

5 files changed

+47
-19
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

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

3563+
/// Get the libcall routine name for the specified libcall implementation
3564+
const char *getLibcallImplName(RTLIB::LibcallImpl Call) const {
3565+
return Libcalls.getLibcallImplName(Call);
3566+
}
3567+
35633568
const char *getMemcpyName() const { return Libcalls.getMemcpyName(); }
35643569

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

llvm/include/llvm/IR/RuntimeLibcalls.td

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ def isNotOSMSVCRT : RuntimeLibcallPredicate<"!TT.isOSMSVCRT()">;
2424
def isPS : RuntimeLibcallPredicate<"TT.isPS()">;
2525
def isNotOSWindowsOrIsCygwinMinGW
2626
: RuntimeLibcallPredicate<"!TT.isOSWindows() || TT.isOSCygMing()">;
27-
27+
def isWindowsMSVCEnvironment : RuntimeLibcallPredicate<
28+
[{TT.isWindowsMSVCEnvironment()}]>;
2829

2930
def isGNUEnvironment : RuntimeLibcallPredicate<"TT.isGNUEnvironment()">;
3031
def darwinHasSinCosStret : RuntimeLibcallPredicate<"darwinHasSinCosStret(TT)">;
@@ -365,6 +366,8 @@ def STACK_SMASH_HANDLER : RuntimeLibcall;
365366
// Safe stack
366367
def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;
367368

369+
def SECURITY_CHECK_COOKIE : RuntimeLibcall;
370+
368371
// Deoptimization
369372
def DEOPTIMIZE : RuntimeLibcall;
370373

@@ -1005,6 +1008,10 @@ def __stack_smash_handler : RuntimeLibcallImpl<STACK_SMASH_HANDLER>;
10051008

10061009
def __riscv_flush_icache : RuntimeLibcallImpl<RISCV_FLUSH_ICACHE>;
10071010

1011+
def __security_check_cookie : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE>;
1012+
def __security_check_cookie_arm64ec : RuntimeLibcallImpl<SECURITY_CHECK_COOKIE,
1013+
"#__security_check_cookie_arm64ec">;
1014+
10081015
//===----------------------------------------------------------------------===//
10091016
// F128 libm Runtime Libcalls
10101017
//===----------------------------------------------------------------------===//
@@ -1107,6 +1114,9 @@ defvar DarwinSinCosStret = LibcallImpls<(add __sincosf_stret, __sincos_stret),
11071114
darwinHasSinCosStret>;
11081115
defvar DarwinExp10 = LibcallImpls<(add __exp10f, __exp10), darwinHasExp10>;
11091116

1117+
defvar SecurityCheckCookieIfWinMSVC =
1118+
LibcallImpls<(add __security_check_cookie), isWindowsMSVCEnvironment>;
1119+
11101120
defvar LibmHasSinCosF32 = LibcallImpls<(add sincosf), hasSinCos>;
11111121
defvar LibmHasSinCosF64 = LibcallImpls<(add sincos), hasSinCos>;
11121122
defvar LibmHasSinCosF80 = LibcallImpls<(add sincos_f80), hasSinCos>;
@@ -1211,6 +1221,7 @@ def AArch64SystemLibrary : SystemRuntimeLibrary<
12111221
LibcallImpls<(add Int128RTLibcalls), isAArch64_ILP64>,
12121222
LibcallImpls<(add bzero), isOSDarwin>,
12131223
DarwinExp10, DarwinSinCosStret,
1224+
SecurityCheckCookieIfWinMSVC,
12141225
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128)
12151226
>;
12161227

@@ -1227,7 +1238,8 @@ defset list<RuntimeLibcallImpl> WinArm64ECDefaultRuntimeLibcallImpls = {
12271238

12281239
def WindowsARM64ECSystemLibrary
12291240
: SystemRuntimeLibrary<isWindowsArm64EC,
1230-
(add WinArm64ECDefaultRuntimeLibcallImpls)>;
1241+
(add WinArm64ECDefaultRuntimeLibcallImpls,
1242+
__security_check_cookie_arm64ec)>;
12311243

12321244
//===----------------------------------------------------------------------===//
12331245
// AMDGPU Runtime Libcalls
@@ -1479,6 +1491,7 @@ def ARMSystemLibrary
14791491
LibmHasFrexpF128, LibmHasLdexpF128,
14801492
WindowARMDivRemCalls,
14811493
WindowARMFPIntCasts,
1494+
SecurityCheckCookieIfWinMSVC,
14821495
AEABIDivRemCalls,
14831496
DarwinSinCosStret, DarwinExp10,
14841497
LibmHasSinCosF32, LibmHasSinCosF64, LibmHasSinCosF128,
@@ -2122,6 +2135,7 @@ defvar X86CommonLibcalls =
21222135
LibmHasFrexpF80, LibmHasLdexpF80,
21232136
LibcallImpls<(add frexp_f128, ldexp_f128), hasFrexplLdexplF128>,
21242137
DefaultRuntimeLibcallImpls_f80,
2138+
SecurityCheckCookieIfWinMSVC,
21252139
// FIXME: MSVCRT doesn't have powi. The f128 case is added as a
21262140
// hack for one test relying on it.
21272141
__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
@@ -21363,7 +21363,9 @@ bool ARMTargetLowering::useLoadStackGuardNode(const Module &M) const {
2136321363
}
2136421364

2136521365
void ARMTargetLowering::insertSSPDeclarations(Module &M) const {
21366-
if (!Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
21366+
RTLIB::LibcallImpl SecurityCheckCookieLibcall =
21367+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
21368+
if (SecurityCheckCookieLibcall == RTLIB::Unsupported)
2136721369
return TargetLowering::insertSSPDeclarations(M);
2136821370

2136921371
// MSVC CRT has a global variable holding security cookie.
@@ -21372,23 +21374,32 @@ void ARMTargetLowering::insertSSPDeclarations(Module &M) const {
2137221374

2137321375
// MSVC CRT has a function to validate security cookie.
2137421376
FunctionCallee SecurityCheckCookie = M.getOrInsertFunction(
21375-
"__security_check_cookie", Type::getVoidTy(M.getContext()),
21376-
PointerType::getUnqual(M.getContext()));
21377+
getLibcallImplName(SecurityCheckCookieLibcall),
21378+
Type::getVoidTy(M.getContext()), PointerType::getUnqual(M.getContext()));
2137721379
if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee()))
2137821380
F->addParamAttr(0, Attribute::AttrKind::InReg);
2137921381
}
2138021382

2138121383
Value *ARMTargetLowering::getSDagStackGuard(const Module &M) const {
21382-
// MSVC CRT has a global variable holding security cookie.
21383-
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
21384+
RTLIB::LibcallImpl SecurityCheckCookieLibcall =
21385+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
21386+
if (SecurityCheckCookieLibcall != RTLIB::Unsupported) {
21387+
// MSVC CRT has a global variable holding security cookie.
21388+
//
21389+
// FIXME: We have a libcall entry for the correlated check function, but not
21390+
// the global name.
2138421391
return M.getGlobalVariable("__security_cookie");
21392+
}
21393+
2138521394
return TargetLowering::getSDagStackGuard(M);
2138621395
}
2138721396

2138821397
Function *ARMTargetLowering::getSSPStackGuardCheck(const Module &M) const {
2138921398
// MSVC CRT has a function to validate security cookie.
21390-
if (Subtarget->getTargetTriple().isWindowsMSVCEnvironment())
21391-
return M.getFunction("__security_check_cookie");
21399+
RTLIB::LibcallImpl SecurityCheckCookie =
21400+
getLibcallImpl(RTLIB::SECURITY_CHECK_COOKIE);
21401+
if (SecurityCheckCookie != RTLIB::Unsupported)
21402+
return M.getFunction(getLibcallImplName(SecurityCheckCookie));
2139221403
return TargetLowering::getSSPStackGuardCheck(M);
2139321404
}
2139421405

0 commit comments

Comments
 (0)