aboutsummaryrefslogtreecommitdiffstats
path: root/hw/ta_deepfuzz.py
blob: ad03d6ac8e792ecd975184ed66b706f6a80cb8b8 (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
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Deep randomized fuzz, enabled by the wrapping ring (pass 85).

Every earlier fuzz was capped near 1000 invokes per boot by our own ring bug. With
`ring_wrap.install` that ceiling is gone, so this runs thousands of randomized param-block
mutations per TA and watches for: non-benign status, stalls, writes past the declared window,
and drift in a fixed re-probe (state corruption).

Deterministic: seeded RNG, so any hit can be replayed exactly.
"""
import glob, random, struct, sys, time
sys.path.insert(0,'/root/psp/hw')
from tinygrad import Device
import tinygrad.runtime.autogen.am.am as am
from ring_wrap import install
adev=Device["AMD"].iface.dev_impl; psp=adev.psp
submit=install(psp,adev)
PAT=0xA5A5A5A5; DECL=0x4000; TOTAL=0x8000
BENIGN={0x0,0xffff0000}
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
def load(blob):
    ap_,amc=alloc(0x80000); cp,cmc=alloc(TOTAL)
    n=(len(blob)+3)&~3
    adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_LOAD_TA); t=c.cmd.cmd_load_ta
    t.app_phy_addr_lo,t.app_phy_addr_hi=amc&0xFFFFFFFF,amc>>32; t.app_len=len(blob)
    t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=DECL
    r=submit(c); return r.resp.status&0xFFFFFFFF, r.resp.session_id, cp
INTERESTING=[0,1,2,4,8,0x10,0x20,0x40,0x80,0x100,0x1000,0x4000,0x8000,0x10000,
             0x7FFFFFFF,0x80000000,0xFFFFFFF0,0xFFFFFFFF,0xE0000000,0x00400000]
def inv(sid,shm,cid,mut):
    buf=bytearray(TOTAL); struct.pack_into('<I',buf,0,cid)
    for off,val in mut: struct.pack_into('<I',buf,off,val)
    for o in range(DECL,TOTAL,4): struct.pack_into('<I',buf,o,PAT)
    adev.vram.view(shm,TOTAL)[:]=bytes(buf); adev.gmc.flush_hdp()
    c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_INVOKE_CMD)
    iv=c.cmd.cmd_invoke_cmd; iv.session_id,iv.ta_cmd_id=sid,cid
    t0=time.time()
    try: r=submit(c); st=r.resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    dt=time.time()-t0
    adev.gmc.flush_hdp()
    out=bytes(adev.vram.view(shm,TOTAL)[:])
    bey=sum(1 for o in range(DECL,TOTAL,4) if struct.unpack_from('<I',out,o)[0]!=PAT)
    return st,dt,bey,out[:16]
TAS={'CSU':('ta_42-0-0-6_1c874c3c',set()),'GCP':('ta_22-0-0-77_7c426e7f',set()),
     'OC':('ta_50-0-0-1_cbd47c59',set()),'OPM':('ta_25-0-0-81_dc596ba6',set()),
     'MERIT':('ta_24-0-0-70_5cd4afda',set()),'XGMI':('ta_32-0-0-20_20a3d1a4',set()),
     'AUC':('ta_16-0-0-9_9e9044cf',{6}),'FP':('ta_19-0-0-8_7e8be282',{7}),
     'WSPE':('ta_30-0-0-38_ec5dee6d',set()),'RAP':('ta_7-0-2-19_f368676e',set())}
name=sys.argv[1]; N=int(sys.argv[2]) if len(sys.argv)>2 else 2000
stem,skip=TAS[name]
rng=random.Random(0xC0FFEE)
st,sid,shm=load(open(glob.glob('/root/psp/contall/'+stem+'*')[0],'rb').read())
print('%s load %#x sid=%s'%(name,st,sid), flush=True)
if st!=0: sys.exit(2)
ref=inv(sid,shm,0,[])[3]
hits=[]
for i in range(N):
    cid=rng.randrange(0,12)
    if cid in skip: continue
    # offsets drawn from the WHOLE declared window, not just the first 0x100 bytes
    mut=[(rng.randrange(1,DECL//4)*4, rng.choice(INTERESTING)) for _ in range(rng.randrange(1,6))]
    s,dt,bey,out=inv(sid,shm,cid,mut)
    if bey: hits.append(('OOB',i,cid,mut,bey)); break
    if dt>5: hits.append(('STALL',i,cid,mut)); break
    if s not in BENIGN and (s>>16)!=0xffff: hits.append(('STATUS',i,cid,mut,hex(s)))
    if i%400==0 and i:
        r2=inv(sid,shm,0,[])[3]
        if r2!=ref: hits.append(('DRIFT',i,ref.hex(),r2.hex())); break
print('%s: %d invokes, hits=%s'%(name,N,hits[:5] if hits else 'none'), flush=True)