aboutsummaryrefslogtreecommitdiffstats
path: root/hw/ta_seqfuzz.py
blob: cf91f315238356a01ddf37ae5501f11f2e1eb3a3 (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
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
"""Stateful sequence fuzz: carry each invoke's OUTPUT into the next invoke's INPUT.

Every fuzz so far issued independent invokes with a freshly built buffer, which cannot reach
defects that need a sequence -- create, use, destroy, use again. That is exactly the shape of
the GCP lifetime defects, and pass 94 recorded that the generic handle test was too coarse.

This keeps the buffer live: the output of invoke N becomes the input of invoke N+1 (sometimes
mutated), over long randomised command sequences, so handles the TA emits are fed straight
back to it -- including after whatever command might free them.

Detection: non-benign status, stall, write past the declared window, or drift in a fixed
re-probe (cmd 0 on a pristine buffer) sampled periodically -- drift being the signature of
corrupted internal state.
"""
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=0x22222222; WIN=0x4000; TOTAL=0x8000
BENIGN={0x0,0xffff0000}
TAS={'GCP':'ta_22-0-0-77_7c426e7f','MERIT':'ta_24-0-0-70_5cd4afda','OPM':'ta_25-0-0-81_dc596ba6',
     'CSU':'ta_42-0-0-6_1c874c3c','OC':'ta_50-0-0-1_cbd47c59','XGMI':'ta_32-0-0-20_20a3d1a4',
     'AUC':'ta_16-0-0-9_9e9044cf'}
name=sys.argv[1]; N=int(sys.argv[2]) if len(sys.argv)>2 else 1500
SKIP={'AUC':{6}}.get(name,set())
base=open(glob.glob('/root/psp/contall/'+TAS[name]+'*')[0],'rb').read()
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
ap_,amc=alloc(0x80000); cp,cmc=alloc(TOTAL)
n=(len(base)+3)&~3
adev.vram.view(ap_,n)[:]=base+b'\x00'*(n-len(base)); 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(base)
t.cmd_buf_phy_addr_lo,t.cmd_buf_phy_addr_hi=cmc&0xFFFFFFFF,cmc>>32; t.cmd_buf_len=WIN
r=submit(c); sid=r.resp.session_id
print('%s load %#x sid=%s'%(name,r.resp.status&0xFFFFFFFF,sid), flush=True)
def raw_invoke(cid):
    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: st=submit(c).resp.status&0xFFFFFFFF
    except Exception as e:
        try: st=int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: st=-1
    return st, time.time()-t0
def probe():
    b=bytearray(WIN); struct.pack_into('<I',b,0,0)
    adev.vram.view(cp,WIN)[:]=bytes(b); adev.gmc.flush_hdp()
    s,_=raw_invoke(0); adev.gmc.flush_hdp()
    return s, bytes(adev.vram.view(cp,0x40)[:])
ref=probe()
rng=random.Random(0xFACE)
adev.vram.view(cp+WIN,TOTAL-WIN)[:]=bytes(struct.pack('<I',PAT)*((TOTAL-WIN)//4)); adev.gmc.flush_hdp()
hits=[]
for i in range(N):
    cid=rng.randrange(0,12)
    if cid in SKIP: continue
    # carry state: leave the buffer as the previous invoke left it, set the command id,
    # and sometimes perturb a few dwords of the carried-forward content
    cur=bytearray(adev.vram.view(cp,WIN)[:])
    struct.pack_into('<I',cur,0,cid)
    if rng.random()<0.5:
        for _ in range(rng.randrange(1,4)):
            struct.pack_into('<I',cur,rng.randrange(1,WIN//4)*4,rng.choice(
                [0,1,0xFFFFFFFF,0x80000000,0x1000,0x10000]))
    adev.vram.view(cp,WIN)[:]=bytes(cur); adev.gmc.flush_hdp()
    st,dt=raw_invoke(cid)
    if dt>5: hits.append(('STALL',i,cid)); break
    adev.gmc.flush_hdp()
    o=bytes(adev.vram.view(cp,TOTAL)[:])
    if any(struct.unpack_from('<I',o,x)[0]!=PAT for x in range(WIN,TOTAL,4)):
        hits.append(('OOB',i,cid)); break
    if st not in BENIGN and (st>>16)!=0xffff: hits.append(('STATUS',i,cid,hex(st)))
    if i%300==0 and i:
        if probe()!=ref: hits.append(('DRIFT',i)); break
        adev.vram.view(cp+WIN,TOTAL-WIN)[:]=bytes(struct.pack('<I',PAT)*((TOTAL-WIN)//4))
        adev.gmc.flush_hdp()
print('%s: %d stateful invokes, hits=%s'%(name,N,hits[:5] if hits else 'none'), flush=True)