Skip to content

StackProtector: Use RuntimeLibcalls to query libcall names #147913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions llvm/include/llvm/IR/RuntimeLibcalls.td
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ defset list<RuntimeLibcall> LibCalls__OutOfLineAtomic = {

// Stack Protector Fail
def STACKPROTECTOR_CHECK_FAIL : RuntimeLibcall;
def STACK_SMASH_HANDLER : RuntimeLibcall;

// Deoptimization
def DEOPTIMIZE : RuntimeLibcall;
Expand Down Expand Up @@ -987,6 +988,9 @@ def bzero : RuntimeLibcallImpl<BZERO>;
def __bzero : RuntimeLibcallImpl<BZERO>;
def _Unwind_SjLj_Resume : RuntimeLibcallImpl<UNWIND_RESUME>;

// Used on OpenBSD
def __stack_smash_handler : RuntimeLibcallImpl<STACK_SMASH_HANDLER>;

def __riscv_flush_icache : RuntimeLibcallImpl<RISCV_FLUSH_ICACHE>;

//===----------------------------------------------------------------------===//
Expand Down
28 changes: 18 additions & 10 deletions llvm/lib/CodeGen/StackProtector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static bool InsertStackProtectors(const TargetMachine *TM, Function *F,

/// CreateFailBB - Create a basic block to jump to when the stack protector
/// check fails.
static BasicBlock *CreateFailBB(Function *F, const Triple &Trip);
static BasicBlock *CreateFailBB(Function *F, const TargetLowering &TLI);

bool SSPLayoutInfo::shouldEmitSDCheck(const BasicBlock &BB) const {
return HasPrologue && !HasIRCheck && isa<ReturnInst>(BB.getTerminator());
Expand Down Expand Up @@ -673,7 +673,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
// merge pass will merge together all of the various BB into one including
// fail BB generated by the stack protector pseudo instruction.
if (!FailBB)
FailBB = CreateFailBB(F, TM->getTargetTriple());
FailBB = CreateFailBB(F, *TLI);

IRBuilder<> B(CheckLoc);
Value *Guard = getStackGuard(TLI, M, B);
Expand Down Expand Up @@ -706,7 +706,7 @@ bool InsertStackProtectors(const TargetMachine *TM, Function *F,
return HasPrologue;
}

BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
BasicBlock *CreateFailBB(Function *F, const TargetLowering &TLI) {
auto *M = F->getParent();
LLVMContext &Context = F->getContext();
BasicBlock *FailBB = BasicBlock::Create(Context, "CallStackCheckFailBlk", F);
Expand All @@ -716,17 +716,25 @@ BasicBlock *CreateFailBB(Function *F, const Triple &Trip) {
DILocation::get(Context, 0, 0, F->getSubprogram()));
FunctionCallee StackChkFail;
SmallVector<Value *, 1> Args;
if (Trip.isOSOpenBSD()) {
StackChkFail = M->getOrInsertFunction("__stack_smash_handler",
Type::getVoidTy(Context),

if (const char *ChkFailName =
TLI.getLibcallName(RTLIB::STACKPROTECTOR_CHECK_FAIL)) {
StackChkFail =
M->getOrInsertFunction(ChkFailName, Type::getVoidTy(Context));
} else if (const char *SSHName =
TLI.getLibcallName(RTLIB::STACK_SMASH_HANDLER)) {
StackChkFail = M->getOrInsertFunction(SSHName, Type::getVoidTy(Context),
PointerType::getUnqual(Context));
Args.push_back(B.CreateGlobalString(F->getName(), "SSH"));
} else {
StackChkFail =
M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
Context.emitError("no libcall available for stack protector");
}
cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
B.CreateCall(StackChkFail, Args);

if (StackChkFail) {
cast<Function>(StackChkFail.getCallee())->addFnAttr(Attribute::NoReturn);
B.CreateCall(StackChkFail, Args);
}

B.CreateUnreachable();
return FailBB;
}
1 change: 1 addition & 0 deletions llvm/lib/IR/RuntimeLibcalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,

if (TT.isOSOpenBSD()) {
setLibcallImpl(RTLIB::STACKPROTECTOR_CHECK_FAIL, RTLIB::Unsupported);
setLibcallImpl(RTLIB::STACK_SMASH_HANDLER, RTLIB::__stack_smash_handler);
}

// Skip default manual processing for targets that have been fully ported to
Expand Down
10 changes: 10 additions & 0 deletions llvm/test/CodeGen/NVPTX/no-stack-protector-libcall-error.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; RUN: not opt -disable-output -mtriple=nvptx64-- -enable-selectiondag-sp=0 -passes=stack-protector %s 2>&1 | FileCheck %s

; CHECK: error: no libcall available for stack protector
define void @func() sspreq nounwind {
%alloca = alloca i32, align 4
call void @capture(ptr %alloca)
ret void
}

declare void @capture(ptr)
Loading