Skip to content

Commit 1084d36

Browse files
authored
Fix sysreg registers in RISC-V and modify the header patcher to not crash if a header doesn't include all tags of a generated .inc file (#2894)
* fix sysreg registers in RISC-V and modify the header patcher to not crash if a header doesn't include all tags of a generated .inc file * add tests
1 parent 6a9c06c commit 1084d36

7 files changed

Lines changed: 572 additions & 7 deletions

File tree

arch/RISCV/RISCVInstPrinter.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,4 @@ const char *getSysRegName(unsigned reg);
6161

6262
bool isCompressed(MCInst *MI);
6363

64-
typedef enum {
65-
#define GET_ENUM_VALUES_SysReg
66-
#include "RISCVGenCSSystemOperandsEnum.inc"
67-
} SysRegValue;
68-
6964
#endif

bindings/python/tests/test_all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import test_skipdata
66
import test_customized_mnem
77
import test_compatibility_layer
8+
import test_riscv_sysreg
89

910
errors = []
1011
errors.extend(test_lite.test_class())
1112
errors.extend(test_iter.test_class())
1213
errors.extend(test_skipdata.test_class())
1314
errors.extend(test_customized_mnem.test())
1415
errors.extend(test_compatibility_layer.test_compatibility())
16+
errors.extend(test_riscv_sysreg.test())
1517

1618
if errors:
1719
print("Some errors happened. Please check the output")
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from capstone import *
2+
from capstone.riscv import *
3+
from capstone.riscv_const import *
4+
import sys
5+
6+
def test():
7+
print("Test test_riscv_sysreg (Python)")
8+
errors = []
9+
# csrr a0, sstatus (0x10002573)
10+
# csrr a0, mtvec (0x30502573)
11+
# csrr a0, mcause (0x34202573)
12+
CODE = b"\x73\x25\x00\x10\x73\x25\x50\x30\x73\x25\x20\x34"
13+
14+
expected_sysregs = [
15+
RISCV_SYSREG_SSTATUS,
16+
RISCV_SYSREG_MTVEC,
17+
RISCV_SYSREG_MCAUSE,
18+
]
19+
20+
try:
21+
md = Cs(CS_ARCH_RISCV, CS_MODE_RISCV64)
22+
md.detail = True
23+
24+
count = 0
25+
for insn in md.disasm(CODE, 0):
26+
found_sysreg = False
27+
for op in insn.operands:
28+
if op.type == RISCV_OP_CSR:
29+
if count >= len(expected_sysregs):
30+
errors.append(f"FAIL: Found CSR operand in instruction {count}, but only {len(expected_sysregs)} expected.")
31+
break
32+
33+
if op.csr != expected_sysregs[count]:
34+
errors.append(f"FAIL: Expected sysreg {expected_sysregs[count]}, got {op.csr}")
35+
36+
if op.csr == RISCV_SYSREG_SSTATUS:
37+
print(" Recognized SSTATUS")
38+
elif op.csr == RISCV_SYSREG_MTVEC:
39+
print(" Recognized MTVEC")
40+
elif op.csr == RISCV_SYSREG_MCAUSE:
41+
print(" Recognized MCAUSE")
42+
43+
found_sysreg = True
44+
45+
if not found_sysreg:
46+
errors.append(f"FAIL: No CSR operand found in instruction {count}")
47+
48+
count += 1
49+
50+
if count != 3:
51+
errors.append(f"FAIL: Expected 3 instructions, got {count}")
52+
except CsError as e:
53+
errors.append(str(e))
54+
55+
return errors
56+
57+
if __name__ == '__main__':
58+
errs = test()
59+
if not errs:
60+
print("Python sysreg test PASSED")
61+
sys.exit(0)
62+
else:
63+
print("Python sysreg test FAILED")
64+
for e in errs:
65+
print(f" {e}")
66+
sys.exit(1)

0 commit comments

Comments
 (0)