Added address-space mangling for stack related intrinsics

Modified the following 3 intrinsics:
int_addressofreturnaddress,
int_frameaddress & int_sponentry.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D64561

llvm-svn: 366679
This commit is contained in:
Christudasan Devadasan 2019-07-22 12:42:48 +00:00
parent 6e13723d10
commit 34435ad5ca
18 changed files with 37 additions and 26 deletions

View File

@ -407,9 +407,9 @@ def int_objc_arc_annotation_bottomup_bbend : Intrinsic<[],
//===--------------------- Code Generator Intrinsics ----------------------===//
//
def int_returnaddress : Intrinsic<[llvm_ptr_ty], [llvm_i32_ty], [IntrNoMem, ImmArg<0>]>;
def int_addressofreturnaddress : Intrinsic<[llvm_ptr_ty], [], [IntrNoMem]>;
def int_frameaddress : Intrinsic<[llvm_ptr_ty], [llvm_i32_ty], [IntrNoMem, ImmArg<0>]>;
def int_sponentry : Intrinsic<[llvm_ptr_ty], [], [IntrNoMem]>;
def int_addressofreturnaddress : Intrinsic<[llvm_anyptr_ty], [], [IntrNoMem]>;
def int_frameaddress : Intrinsic<[llvm_anyptr_ty], [llvm_i32_ty], [IntrNoMem, ImmArg<0>]>;
def int_sponentry : Intrinsic<[llvm_anyptr_ty], [], [IntrNoMem]>;
def int_read_register : Intrinsic<[llvm_anyint_ty], [llvm_metadata_ty],
[IntrReadMem], "llvm.read_register">;
def int_write_register : Intrinsic<[], [llvm_metadata_ty, llvm_anyint_ty],

View File

@ -5583,11 +5583,11 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
return;
case Intrinsic::sponentry:
setValue(&I, DAG.getNode(ISD::SPONENTRY, sdl,
TLI.getPointerTy(DAG.getDataLayout())));
TLI.getFrameIndexTy(DAG.getDataLayout())));
return;
case Intrinsic::frameaddress:
setValue(&I, DAG.getNode(ISD::FRAMEADDR, sdl,
TLI.getPointerTy(DAG.getDataLayout()),
TLI.getFrameIndexTy(DAG.getDataLayout()),
getValue(I.getArgOperand(0))));
return;
case Intrinsic::read_register: {

View File

@ -477,7 +477,10 @@ bool SjLjEHPrepare::runOnFunction(Function &F) {
UnregisterFn = M.getOrInsertFunction(
"_Unwind_SjLj_Unregister", Type::getVoidTy(M.getContext()),
PointerType::getUnqual(FunctionContextTy));
FrameAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::frameaddress);
FrameAddrFn = Intrinsic::getDeclaration(
&M, Intrinsic::frameaddress,
{Type::getInt8PtrTy(M.getContext(),
M.getDataLayout().getAllocaAddrSpace())});
StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave);
StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore);
BuiltinSetupDispatchFn =

View File

@ -339,7 +339,10 @@ void WinEHStatePass::emitExceptionRegistrationRecord(Function *F) {
if (UseStackGuard) {
Value *Val = Builder.CreateLoad(Int32Ty, Cookie);
Value *FrameAddr = Builder.CreateCall(
Intrinsic::getDeclaration(TheModule, Intrinsic::frameaddress),
Intrinsic::getDeclaration(
TheModule, Intrinsic::frameaddress,
Builder.getInt8PtrTy(
TheModule->getDataLayout().getAllocaAddrSpace())),
Builder.getInt32(0), "frameaddr");
Value *FrameAddrI32 = Builder.CreatePtrToInt(FrameAddr, Int32Ty);
FrameAddrI32 = Builder.CreateXor(FrameAddrI32, Val);

View File

@ -778,8 +778,9 @@ Value *HWAddressSanitizer::getStackBaseTag(IRBuilder<> &IRB) {
// FIXME: use addressofreturnaddress (but implement it in aarch64 backend
// first).
Module *M = IRB.GetInsertBlock()->getParent()->getParent();
auto GetStackPointerFn =
Intrinsic::getDeclaration(M, Intrinsic::frameaddress);
auto GetStackPointerFn = Intrinsic::getDeclaration(
M, Intrinsic::frameaddress,
IRB.getInt8PtrTy(M->getDataLayout().getAllocaAddrSpace()));
Value *StackPointer = IRB.CreateCall(
GetStackPointerFn, {Constant::getNullValue(IRB.getInt32Ty())});
@ -912,8 +913,10 @@ void HWAddressSanitizer::emitPrologue(IRBuilder<> &IRB, bool WithFrameRecord) {
PC = readRegister(IRB, "pc");
else
PC = IRB.CreatePtrToInt(F, IntptrTy);
auto GetStackPointerFn =
Intrinsic::getDeclaration(F->getParent(), Intrinsic::frameaddress);
Module *M = F->getParent();
auto GetStackPointerFn = Intrinsic::getDeclaration(
M, Intrinsic::frameaddress,
IRB.getInt8PtrTy(M->getDataLayout().getAllocaAddrSpace()));
Value *SP = IRB.CreatePtrToInt(
IRB.CreateCall(GetStackPointerFn,
{Constant::getNullValue(IRB.getInt32Ty())}),

View File

@ -842,8 +842,10 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F, BasicBlock &BB,
}
if (Options.StackDepth && IsEntryBB && !IsLeafFunc) {
// Check stack depth. If it's the deepest so far, record it.
Function *GetFrameAddr =
Intrinsic::getDeclaration(F.getParent(), Intrinsic::frameaddress);
Module *M = F.getParent();
Function *GetFrameAddr = Intrinsic::getDeclaration(
M, Intrinsic::frameaddress,
IRB.getInt8PtrTy(M->getDataLayout().getAllocaAddrSpace()));
auto FrameAddrPtr =
IRB.CreateCall(GetFrameAddr, {Constant::getNullValue(Int32Ty)});
auto FrameAddrInt = IRB.CreatePtrToInt(FrameAddrPtr, IntptrTy);

View File

@ -1116,7 +1116,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -1147,7 +1147,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -1302,7 +1302,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -1373,7 +1373,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -1373,7 +1373,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -1383,7 +1383,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -1394,7 +1394,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -1486,7 +1486,7 @@ define void @intrinsics.codegen() {
call i8* @llvm.returnaddress(i32 1)
; CHECK: call i8* @llvm.returnaddress(i32 1)
call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress(i32 1)
; CHECK: call i8* @llvm.frameaddress.p0i8(i32 1)
call i32 @llvm.read_register.i32(metadata !10)
; CHECK: call i32 @llvm.read_register.i32(metadata !10)

View File

@ -11,7 +11,7 @@ declare void @use32(i32*)
define void @test_alloca() sanitize_hwaddress {
; CHECK-LABEL: @test_alloca(
; CHECK: %[[FP:[^ ]*]] = call i8* @llvm.frameaddress(i32 0)
; CHECK: %[[FP:[^ ]*]] = call i8* @llvm.frameaddress.p0i8(i32 0)
; CHECK: %[[A:[^ ]*]] = ptrtoint i8* %[[FP]] to i64
; CHECK: %[[B:[^ ]*]] = lshr i64 %[[A]], 20
; CHECK: %[[BASE_TAG:[^ ]*]] = xor i64 %[[A]], %[[B]]

View File

@ -9,7 +9,7 @@ declare void @use32(i32*)
define void @test_alloca() sanitize_hwaddress {
; CHECK-LABEL: @test_alloca(
; CHECK: %[[FP:[^ ]*]] = call i8* @llvm.frameaddress(i32 0)
; CHECK: %[[FP:[^ ]*]] = call i8* @llvm.frameaddress.p0i8(i32 0)
; CHECK: %[[A:[^ ]*]] = ptrtoint i8* %[[FP]] to i64
; CHECK: %[[B:[^ ]*]] = lshr i64 %[[A]], 20
; CHECK: %[[BASE_TAG:[^ ]*]] = xor i64 %[[A]], %[[B]]

View File

@ -14,7 +14,7 @@ target triple = "x86_64-unknown-linux-gnu"
define i32 @foo() {
entry:
; CHECK-LABEL: define i32 @foo
; CHECK-NOT: call i8* @llvm.frameaddress(i32 0)
; CHECK-NOT: call i8* @llvm.frameaddress.p0i8(i32 0)
; CHECK-NOT: @__sancov_lowest_stack
; CHECK: ret i32 7
@ -24,7 +24,7 @@ entry:
define i32 @bar() {
entry:
; CHECK-LABEL: define i32 @bar
; CHECK: [[framePtr:%[^ \t]+]] = call i8* @llvm.frameaddress(i32 0)
; CHECK: [[framePtr:%[^ \t]+]] = call i8* @llvm.frameaddress.p0i8(i32 0)
; CHECK: [[frameInt:%[^ \t]+]] = ptrtoint i8* [[framePtr]] to [[intType:i[0-9]+]]
; CHECK: [[lowest:%[^ \t]+]] = load [[intType]], [[intType]]* @__sancov_lowest_stack
; CHECK: [[cmp:%[^ \t]+]] = icmp ult [[intType]] [[frameInt]], [[lowest]]

View File

@ -13,7 +13,7 @@ declare i8* @llvm.frameaddress(i32)
define void @frame_address(i32 %var) {
; CHECK: immarg operand has non-immediate parameter
; CHECK-NEXT: i32 %var
; CHECK-NEXT: %result = call i8* @llvm.frameaddress(i32 %var)
; CHECK-NEXT: %result = call i8* @llvm.frameaddress.p0i8(i32 %var)
%result = call i8* @llvm.frameaddress(i32 %var)
ret void
}