Skip to content

Commit 55b3510

Browse files
committed
[RISCV] Use software-guarded jump in the trampoline code
1 parent d02379c commit 55b3510

File tree

2 files changed

+38
-38
lines changed

2 files changed

+38
-38
lines changed

llvm/lib/Target/RISCV/RISCVISelLowering.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8366,18 +8366,17 @@ SDValue RISCVTargetLowering::lowerINIT_TRAMPOLINE(SDValue Op,
83668366
// Offset with branch control flow protection enabled:
83678367
// 0: lpad <imm20>
83688368
// 4: auipc t3, 0
8369-
// 8: ld t0, 28(t3)
8369+
// 8: ld t2, 28(t3)
83708370
// 12: ld t3, 20(t3)
8371-
// 16: lui t2, <imm20>
8372-
// 20: jalr t0
8373-
// 24: <StaticChainOffset>
8374-
// 32: <FunctionAddressOffset>
8375-
// 40:
8371+
// 16: jalr t2
8372+
// 20: <StaticChainOffset>
8373+
// 28: <FunctionAddressOffset>
8374+
// 36:
83768375

83778376
const bool HasCFBranch =
83788377
Subtarget.hasStdExtZicfilp() &&
83798378
DAG.getMMI()->getModule()->getModuleFlag("cf-protection-branch");
8380-
const unsigned StaticChainIdx = HasCFBranch ? 6 : 4;
8379+
const unsigned StaticChainIdx = HasCFBranch ? 5 : 4;
83818380
const unsigned StaticChainOffset = StaticChainIdx * 4;
83828381
const unsigned FunctionAddressOffset = StaticChainOffset + 8;
83838382

@@ -8392,7 +8391,7 @@ SDValue RISCVTargetLowering::lowerINIT_TRAMPOLINE(SDValue Op,
83928391
};
83938392

83948393
SDValue OutChains[6];
8395-
SDValue OutChainsLPAD[8];
8394+
SDValue OutChainsLPAD[7];
83968395
if (HasCFBranch)
83978396
assert(std::size(OutChainsLPAD) == StaticChainIdx + 2);
83988397
else
@@ -8431,11 +8430,11 @@ SDValue RISCVTargetLowering::lowerINIT_TRAMPOLINE(SDValue Op,
84318430
// auipc t3, 0
84328431
// Loads the current PC into t3.
84338432
GetEncoding(MCInstBuilder(RISCV::AUIPC).addReg(RISCV::X28).addImm(0)),
8434-
// ld t0, (FunctionAddressOffset - 4)(t3)
8435-
// Loads the function address into t0. Note that we are using offsets
8433+
// ld t2, (FunctionAddressOffset - 4)(t3)
8434+
// Loads the function address into t2. Note that we are using offsets
84368435
// pc-relative to the SECOND instruction of the trampoline.
84378436
GetEncoding(MCInstBuilder(RISCV::LD)
8438-
.addReg(RISCV::X5)
8437+
.addReg(RISCV::X7)
84398438
.addReg(RISCV::X28)
84408439
.addImm(FunctionAddressOffset - 4)),
84418440
// ld t3, (StaticChainOffset - 4)(t3)
@@ -8444,14 +8443,11 @@ SDValue RISCVTargetLowering::lowerINIT_TRAMPOLINE(SDValue Op,
84448443
.addReg(RISCV::X28)
84458444
.addReg(RISCV::X28)
84468445
.addImm(StaticChainOffset - 4)),
8447-
// lui t2, <imm20>
8448-
// Setup the landing pad value.
8449-
GetEncoding(MCInstBuilder(RISCV::LUI).addReg(RISCV::X7).addImm(0)),
8450-
// jalr t0
8451-
// Jump to the function.
8446+
// jalr t2
8447+
// Software-guarded jump to the function.
84528448
GetEncoding(MCInstBuilder(RISCV::JALR)
84538449
.addReg(RISCV::X0)
8454-
.addReg(RISCV::X5)
8450+
.addReg(RISCV::X7)
84558451
.addImm(0))});
84568452
}
84578453

llvm/test/CodeGen/RISCV/rv64-trampoline-cfi.ll

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,27 @@ define i64 @test0(i64 %n, ptr %p) nounwind {
1717
; RV64-NEXT: sd a0, 0(sp) # 8-byte Folded Spill
1818
; RV64-NEXT: lui a0, %hi(f)
1919
; RV64-NEXT: addi a0, a0, %lo(f)
20-
; RV64-NEXT: sd a0, 48(sp)
21-
; RV64-NEXT: sd a1, 40(sp)
22-
; RV64-NEXT: li a0, 951
23-
; RV64-NEXT: sw a0, 32(sp)
20+
; RV64-NEXT: sw a0, 44(sp)
21+
; RV64-NEXT: srli a0, a0, 32
22+
; RV64-NEXT: sw a0, 48(sp)
23+
; RV64-NEXT: sw a1, 36(sp)
24+
; RV64-NEXT: srli a0, a1, 32
25+
; RV64-NEXT: sw a0, 40(sp)
2426
; RV64-NEXT: li a0, 23
2527
; RV64-NEXT: sw a0, 16(sp)
26-
; RV64-NEXT: lui a0, 40
28+
; RV64-NEXT: lui a0, 56
2729
; RV64-NEXT: addiw a0, a0, 103
28-
; RV64-NEXT: sw a0, 36(sp)
29-
; RV64-NEXT: lui a0, 5348
30+
; RV64-NEXT: sw a0, 32(sp)
31+
; RV64-NEXT: lui a0, 4324
3032
; RV64-NEXT: addiw a0, a0, -509
3133
; RV64-NEXT: sw a0, 28(sp)
32-
; RV64-NEXT: lui a0, 7395
33-
; RV64-NEXT: addiw a0, a0, 643
34+
; RV64-NEXT: lui a0, 6371
35+
; RV64-NEXT: addiw a0, a0, 899
3436
; RV64-NEXT: sw a0, 24(sp)
3537
; RV64-NEXT: lui a0, 1
3638
; RV64-NEXT: addiw a0, a0, -489
3739
; RV64-NEXT: sw a0, 20(sp)
38-
; RV64-NEXT: addi a1, sp, 40
40+
; RV64-NEXT: addi a1, sp, 36
3941
; RV64-NEXT: addi a0, sp, 16
4042
; RV64-NEXT: sd a0, 8(sp) # 8-byte Folded Spill
4143
; RV64-NEXT: call __clear_cache
@@ -54,25 +56,27 @@ define i64 @test0(i64 %n, ptr %p) nounwind {
5456
; RV64-LINUX-NEXT: sd a0, 0(sp) # 8-byte Folded Spill
5557
; RV64-LINUX-NEXT: lui a0, %hi(f)
5658
; RV64-LINUX-NEXT: addi a0, a0, %lo(f)
57-
; RV64-LINUX-NEXT: sd a0, 48(sp)
58-
; RV64-LINUX-NEXT: sd a1, 40(sp)
59-
; RV64-LINUX-NEXT: li a0, 951
60-
; RV64-LINUX-NEXT: sw a0, 32(sp)
59+
; RV64-LINUX-NEXT: sw a0, 44(sp)
60+
; RV64-LINUX-NEXT: srli a0, a0, 32
61+
; RV64-LINUX-NEXT: sw a0, 48(sp)
62+
; RV64-LINUX-NEXT: sw a1, 36(sp)
63+
; RV64-LINUX-NEXT: srli a0, a1, 32
64+
; RV64-LINUX-NEXT: sw a0, 40(sp)
6165
; RV64-LINUX-NEXT: li a0, 23
6266
; RV64-LINUX-NEXT: sw a0, 16(sp)
63-
; RV64-LINUX-NEXT: lui a0, 40
67+
; RV64-LINUX-NEXT: lui a0, 56
6468
; RV64-LINUX-NEXT: addiw a0, a0, 103
65-
; RV64-LINUX-NEXT: sw a0, 36(sp)
66-
; RV64-LINUX-NEXT: lui a0, 5348
69+
; RV64-LINUX-NEXT: sw a0, 32(sp)
70+
; RV64-LINUX-NEXT: lui a0, 4324
6771
; RV64-LINUX-NEXT: addiw a0, a0, -509
6872
; RV64-LINUX-NEXT: sw a0, 28(sp)
69-
; RV64-LINUX-NEXT: lui a0, 7395
70-
; RV64-LINUX-NEXT: addiw a0, a0, 643
73+
; RV64-LINUX-NEXT: lui a0, 6371
74+
; RV64-LINUX-NEXT: addiw a0, a0, 899
7175
; RV64-LINUX-NEXT: sw a0, 24(sp)
7276
; RV64-LINUX-NEXT: lui a0, 1
7377
; RV64-LINUX-NEXT: addiw a0, a0, -489
7478
; RV64-LINUX-NEXT: sw a0, 20(sp)
75-
; RV64-LINUX-NEXT: addi a1, sp, 40
79+
; RV64-LINUX-NEXT: addi a1, sp, 36
7680
; RV64-LINUX-NEXT: addi a0, sp, 16
7781
; RV64-LINUX-NEXT: sd a0, 8(sp) # 8-byte Folded Spill
7882
; RV64-LINUX-NEXT: li a2, 0
@@ -83,7 +87,7 @@ define i64 @test0(i64 %n, ptr %p) nounwind {
8387
; RV64-LINUX-NEXT: ld ra, 56(sp) # 8-byte Folded Reload
8488
; RV64-LINUX-NEXT: addi sp, sp, 64
8589
; RV64-LINUX-NEXT: ret
86-
%alloca = alloca [40 x i8], align 8
90+
%alloca = alloca [36 x i8], align 8
8791
call void @llvm.init.trampoline(ptr %alloca, ptr @f, ptr %p)
8892
%tramp = call ptr @llvm.adjust.trampoline(ptr %alloca)
8993
%ret = call i64 %tramp(i64 %n)

0 commit comments

Comments
 (0)