aboutsummaryrefslogtreecommitdiffstats
path: root/psp_out/repro_seq_123.py
blob: e6c2aa3986e0904069f79f55b397dbaea072dce6 (plain) (blame)
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
"""Hypothesis: fuzzing hdcp cmd1 (create_session) + cmd2 (destroy_session) corrupts
session state, and a later cmd3 (first_part_authentication) then hangs the TA.
All in ONE process so the state history is unambiguous."""
import ctypes,struct,time,random,sys,collections
from tinygrad import Device
from tinygrad.runtime.autogen.am import am
from tinygrad.helpers import lo32,hi32
d=Device["AMD"]; adev=d.iface.dev_impl; psp=adev.psp
SZ=ctypes.sizeof(am.struct_psp_gfx_cmd_resp); P='/Users/user/dev/amd-psp-re/psp_out/'
def stat(): return int.from_bytes(bytes(adev.vram.view(psp.cmd_paddr,SZ)[:])[864:868],"little")
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True); return pa,adev.paddr2mc(pa)
def load(cmd,blob):
    ap,amc=alloc(0x40000); cp,cmc=alloc(0x8000)
    adev.vram.view(ap,len(blob))[:]=blob; adev.gmc.flush_hdp()
    adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
    c=am.struct_psp_gfx_cmd_resp(cmd_id=cmd); t=c.cmd.cmd_load_ta
    t.app_phy_addr_hi,t.app_phy_addr_lo=hi32(amc),lo32(amc); t.app_len=len(blob)
    t.cmd_buf_phy_addr_hi,t.cmd_buf_phy_addr_lo=hi32(cmc),lo32(cmc); t.cmd_buf_len=0x8000
    return psp._ring_submit(c).resp.session_id,cp
def inv(sid,shm,buf,tc):
    adev.vram.view(shm,0x8000)[:]=bytes(buf); adev.gmc.flush_hdp()
    adev.vram.view(psp.cmd_paddr,SZ)[:]=b"\x00"*SZ
    ic=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD); iv=ic.cmd.cmd_invoke_cmd
    iv.session_id,iv.ta_cmd_id=sid,tc
    t0=time.time()
    try: psp._ring_submit(ic); return 0,bytes(adev.vram.view(shm,0x40)[:]),time.time()-t0
    except RuntimeError: return stat(),None,time.time()-t0
    except TimeoutError: return "TIMEOUT",None,time.time()-t0
a,_=load(am.GFX_CMD_ID_LOAD_ASD,open(P+'asd_full.bin','rb').read())
ds,dshm=load(am.GFX_CMD_ID_LOAD_TA,open(P+'dtm_full.bin','rb').read())
hs,hshm=load(am.GFX_CMD_ID_LOAD_TA,open(P+'hdcp_full.bin','rb').read())
def topo(h):
    b=bytearray(0x8000); struct.pack_into('<I',b,0,2); struct.pack_into('<10I',b,0x10,h,1,1,0,0,0,0,0,0,0)
    return inv(ds,dshm,b,2)[0]
for h in range(8): topo(h)
print("sids asd=%#x dtm=%#x hdcp=%#x ; bring-up done"%(a,ds,hs))
EX=[0,1,2,3,0xffffffff,0x7fffffff,0x80000000,0xfffffffe,0xffff,0x10000,255,256,24,25,32,33,62,63,64,0x27b]
def fuzz(cmd,n,rng):
    h=collections.Counter()
    for i in range(n):
        b=bytearray(0x8000); struct.pack_into('<I',b,0,cmd)
        struct.pack_into('<I',b,0x0C,rng.choice([0,1,2,3,62,63,64,255,0xffffffff]))
        struct.pack_into('<I',b,0x10,rng.choice(EX)); struct.pack_into('<I',b,0x14,rng.choice(EX))
        nn=rng.choice([8,32,128,512,2048])
        for k in range(0x28,0x28+nn): b[k]=rng.randrange(256)
        for _ in range(6):
            off=rng.randrange(0x28,0x28+max(nn-4,4))&~3
            struct.pack_into('<I',b,off,rng.choice(EX))
        ps,out,dt=inv(hs,hshm,b,cmd)
        ret=struct.unpack_from('<I',out,4)[0] if out else None
        h[ret]+=1
        if ps=="TIMEOUT" or dt>0.5:
            print("  >> HANG during cmd %#x at iter %d (dt=%.2f) sos_alive=%s"%(cmd,i,dt,psp.is_sos_alive()))
            open('hang_seq_cmd%02x.bin'%cmd,'wb').write(bytes(b)); return h,True
    return h,False
r1=random.Random(0x1235); r2=random.Random(0x1236); r3=random.Random(0x1237)
print("phase A: fuzz cmd 1 (create_session) x400 ->", end=" ")
hA,hung=fuzz(1,400,r1); print(dict(hA), "HUNG" if hung else "ok")
if not hung:
    print("phase B: fuzz cmd 2 (destroy_session) x400 ->", end=" ")
    hB,hung=fuzz(2,400,r2); print(dict(hB), "HUNG" if hung else "ok")
if not hung:
    print("phase C: fuzz cmd 3 (first_part_auth) x400 ->", end=" ")
    hC,hung=fuzz(3,400,r3); print(dict(hC), "HUNG" if hung else "ok")
print("sos_alive:",psp.is_sos_alive())