Skip to content

SafeStack: Emit __safestack_pointer_address call through RuntimeLibcalls #147916

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

Open
wants to merge 1 commit into
base: users/arsenm/safestack/use-runtime-libcalls-__stack_chk_fail
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions llvm/include/llvm/IR/RuntimeLibcalls.td
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ defset list<RuntimeLibcall> LibCalls__OutOfLineAtomic = {
def STACKPROTECTOR_CHECK_FAIL : RuntimeLibcall;
def STACK_SMASH_HANDLER : RuntimeLibcall;

// Safe stack
def SAFESTACK_POINTER_ADDRESS : RuntimeLibcall;

// Deoptimization
def DEOPTIMIZE : RuntimeLibcall;

Expand Down Expand Up @@ -656,6 +659,9 @@ foreach lc = LibCalls__atomic in {
// Stack Protector Fail
def __stack_chk_fail : RuntimeLibcallImpl<STACKPROTECTOR_CHECK_FAIL>;

// Safe stack.
def __safestack_pointer_address : RuntimeLibcallImpl<SAFESTACK_POINTER_ADDRESS>;

// Deoptimization
def __llvm_deoptimize : RuntimeLibcallImpl<DEOPTIMIZE>;

Expand Down
10 changes: 9 additions & 1 deletion llvm/lib/CodeGen/SafeStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,16 @@ bool SafeStack::run() {
IRB.SetCurrentDebugLocation(
DILocation::get(SP->getContext(), SP->getScopeLine(), 0, SP));
if (SafeStackUsePointerAddress) {
const char *SafestackPointerAddressName =
TL.getLibcallName(RTLIB::SAFESTACK_POINTER_ADDRESS);
if (!SafestackPointerAddressName) {
F.getContext().emitError(
"no libcall available for safestack pointer address");
return false;
}

FunctionCallee Fn = F.getParent()->getOrInsertFunction(
"__safestack_pointer_address", IRB.getPtrTy(0));
SafestackPointerAddressName, IRB.getPtrTy(0));
UnsafeStackPtr = IRB.CreateCall(Fn);
} else {
UnsafeStackPtr = TL.getSafeStackPointerLocation(IRB);
Expand Down
5 changes: 5 additions & 0 deletions llvm/lib/IR/RuntimeLibcalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ void RuntimeLibcallsInfo::initLibcalls(const Triple &TT,
setLibcallImpl(RTLIB::POWI_F64, RTLIB::Unsupported);
}

if (TT.isAndroid()) {
setLibcallImpl(RTLIB::SAFESTACK_POINTER_ADDRESS,
RTLIB::__safestack_pointer_address);
}

// Setup Windows compiler runtime calls.
if (TT.getArch() == Triple::x86 &&
(TT.isWindowsMSVCEnvironment() || TT.isWindowsItaniumEnvironment())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; RUN: not opt -disable-output -mtriple=nvptx64-- -safestack-use-pointer-address -mcpu=sm_90 -passes=safe-stack %s 2>&1 | FileCheck %s

; CHECK: error: no libcall available for safestack pointer address
define void @foo(i32 %t) #0 {
%vla = alloca i32, i32 %t, align 4
call void @baz(ptr %vla)
ret void
}

declare void @baz(ptr)

attributes #0 = { nounwind safestack sspstrong }
Loading