Skip to content

Commit 0545e43

Browse files
committed
[CHERI] Disable use of SHXADD instructions to materialize frame offset adjustments on pure cap ABIs.
These instructions aren't capability-safe, so their use results in invalid capability dereferences.
1 parent 438ce41 commit 0545e43

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,9 @@ void RISCVRegisterInfo::adjustReg(MachineBasicBlock &MBB,
404404
// path. We avoid anything which can be done with a single lui as it might
405405
// be compressible. Note that the sh1add case is fully covered by the 2x addi
406406
// case just above and is thus omitted.
407-
if (ST.hasStdExtZba() && (Val & 0xFFF) != 0) {
407+
if (ST.hasStdExtZba() && (Val & 0xFFF) != 0 &&
408+
// SHXADD does not work on capability registers
409+
!IsPureCapABI) {
408410
unsigned Opc = 0;
409411
if (isShiftedInt<12, 3>(Val)) {
410412
Opc = RISCV::SH3ADD;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: llc --filetype=asm --mcpu=cheriot --mtriple=riscv32cheriotv1-unknown-cheriotrtos -target-abi cheriot %s -mattr=+xcheri,+xcheripurecap,+xcheriot,+b -o - | FileCheck %s
2+
3+
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128-pf200:64:64:64:32-A200-P200-G200"
4+
target triple = "riscv32cheriotv1-unknown-cheriotrtos"
5+
6+
%struct.mlk_polymat = type { [3 x %struct.mlk_polyvec] }
7+
%struct.mlk_polyvec = type { [3 x %struct.mlk_poly] }
8+
%struct.mlk_poly = type { [256 x i16] }
9+
10+
; Make sure we don't accidentally try to use a sh3add to compute a capability frame offset.
11+
; CHECK-LABEL: PQCP_MLKEM_NATIVE_MLKEM768_indcpa_keypair_derand:
12+
; CHECK-NOT: sh3add
13+
define ptr addrspace(200) @PQCP_MLKEM_NATIVE_MLKEM768_indcpa_keypair_derand() addrspace(200) #0 {
14+
entry:
15+
%a = alloca %struct.mlk_polymat, align 32, addrspace(200)
16+
%e = alloca %struct.mlk_polyvec, align 32, addrspace(200)
17+
ret ptr addrspace(200) %e
18+
}
19+
20+
attributes #0 = { "target-cpu"="cheriot-ibex" }

0 commit comments

Comments
 (0)