1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/usr/bin/env python3
"""Signed SYS_DRV .54 opcode 0x1076 short-output test, synthetic caller region.
No sOS/GCP transport or hardware effects are modeled. The successful selector-1
path also clears SYS_DRV internal state and invokes SVC 0x7B: never use this
harness as a justification for an unplanned hardware request.
"""
from __future__ import annotations
import sys
from pathlib import Path
from unicorn import UC_HOOK_CODE
from unicorn.arm_const import UC_ARM_REG_R1
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from pspemu import sysdrv_env # noqa: E402
from pspemu.component import Fault # noqa: E402
SIGNED = Path(__file__).resolve().parents[2] / 'psp_out/elf/sys_drv_0.49.0.54_18bd9d26.elf'
ENTRY = 0xE087E4
LEAF = 0xE2005C
TABLE = 0xE6873C
END_OF_GCP = 0x41C000
def main() -> None:
sd = sysdrv_env.make()
assert Path(sd.image_path).read_bytes() == SIGNED.read_bytes(), 'image differs from signed .54'
init_ret, init_fault, _ = sysdrv_env.run_init(sd)
assert init_ret == 0 and not init_fault and sysdrv_env.init_flag(sd) == 1
req, cfg = sd.alloc(0x100), sd.alloc(0x400)
sd.write(cfg, b'\x00' * 0x400)
seen = []
sd.uc.hook_add(UC_HOOK_CODE, lambda uc, ea, size, data: seen.append(ea),
begin=LEAF, end=LEAF)
sd.write(req, b'\x00' * 0x100)
for i, v in ((0, 0x1076), (1, 2), (sysdrv_env.REQ_CONFIG_DWORD, cfg)):
sd.wr32(req + 4 * i, v)
for declared in (4, 52):
out = sd.alloc(0x80, fill=b'\xA5')
sd.wr32(out, 1) # selector 1 selects the fixed 24-byte zeroing branch
sd.wr32(req + 8, out)
sd.wr32(req + 12, declared)
sd.wr32(TABLE + 4, 0x55667788)
before = len(seen)
rv = sd.run_function(ENTRY, (req, 2, 0, 0), count=3_000_000,
timeout=4_000_000)
after = sd.read(out, 0x80)
assert rv == 0 and len(seen) == before + 1 and not sd.faults, (rv, sd.faults)
assert after[:4] == b'\x01\x00\x00\x00'
assert after[4:28] == b'\xA5' * 24 and after[28:52] == b'\x00' * 24
assert after[52:] == b'\xA5' * (0x80 - 52)
assert sd.rd32(TABLE + 4) == 0
print('declared', declared, 'status', hex(rv), 'leaf', len(seen) - before,
'changed offsets 28..51; synthetic table cleared')
# Caller-window model, not the live sOS/GCP mapping. The declaration of 52
# must be rejected, while 4 may pass and then fault at the first store.
base, end = 0x400000, END_OF_GCP
sd.uc.mem_map(base, end - base)
edge = end - 4
sd.wr32(edge, 1)
sd.wr32(req + 8, edge)
sd.wr32(TABLE + 4, 0x55667788)
def caller_window(comp, num):
descriptor = comp.uc.reg_read(UC_ARM_REG_R1)
for i, val in enumerate((base, end, 0, 0)):
comp.wr32(descriptor + 4 * i, val)
return 0
sd.svc.register(0xB5, caller_window)
before = len(seen)
sd.wr32(req + 12, 52)
rejected = sd.run_function(ENTRY, (req, 2, 0, 0), count=3_000_000,
timeout=4_000_000)
assert rejected != 0 and len(seen) == before
assert sd.rd32(edge) == 1 and sd.rd32(TABLE + 4) == 0x55667788
sd.wr32(req + 12, 4)
try:
sd.run_function(ENTRY, (req, 2, 0, 0), count=3_000_000,
timeout=4_000_000)
except Fault as exc:
assert end <= exc.addr < end + 0x40, (exc, sd.faults)
assert len(seen) == before + 1 and sd.rd32(TABLE + 4) == 0x55667788
assert sd.rd32(edge) == 1
print('synthetic window: length52 rejected', hex(rejected),
'length4 first out-of-range store faults at', hex(exc.addr))
else:
raise AssertionError('short length did not fault at synthetic window edge')
if __name__ == '__main__':
main()
|