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
|
#!/usr/bin/env python3
"""Multi-session stateful fuzz with mid-run unload/reload — the slot-reuse shape.
Pass 112 carried state but within ONE session. The known GCP lifetime defects are about slot
reuse ACROSS sessions: a slot freed by one client and then reached by another. This runs
several concurrent sessions, interleaves state-carrying invokes among them, and randomly
unloads and reloads sessions mid-run so slots are recycled underneath the survivors.
Detection:
* a survivor session's fixed re-probe must not drift (state corruption);
* no session's buffer may contain another session's marker (cross-session leakage);
* no stall, no write past the declared window.
"""
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)
WIN=0x4000; TOTAL=0x8000
name=sys.argv[1] if len(sys.argv)>1 else 'GCP'
N=int(sys.argv[2]) if len(sys.argv)>2 else 1200
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'}
blob=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)
n=(len(blob)+3)&~3
adev.vram.view(ap_,n)[:]=blob+b'\x00'*(n-len(blob)); adev.gmc.flush_hdp()
def load():
cp,cmc=alloc(TOTAL)
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=WIN
try:
r=submit(c); return r.resp.status&0xFFFFFFFF, r.resp.session_id, cp
except Exception: return -1,None,cp
def unload(sid):
c=am.struct_psp_gfx_cmd_resp(cmd_id=am.GFX_CMD_ID_UNLOAD_TA); c.cmd.cmd_unload_ta.session_id=sid
try: return submit(c).resp.status&0xFFFFFFFF
except Exception: return -1
def inv(sid,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
sess=[]
for i in range(5):
st,sid,cp=load()
if st!=0 or sid is None: break
mk=0xC0DE0000|i
adev.vram.view(cp,TOTAL)[:]=bytes(struct.pack('<I',mk)*(TOTAL//4)); adev.gmc.flush_hdp()
sess.append([sid,cp,mk])
print('%s: %d live sessions %s'%(name,len(sess),[s[0] for s in sess]), flush=True)
if not sess: sys.exit(2)
surv=sess[0]
def probe():
b=bytearray(WIN); struct.pack_into('<I',b,0,0)
adev.vram.view(surv[1],WIN)[:]=bytes(b); adev.gmc.flush_hdp()
s,_=inv(surv[0],0); adev.gmc.flush_hdp()
return s, bytes(adev.vram.view(surv[1],0x40)[:])
ref=probe(); rng=random.Random(0xC0FFEE); hits=[]
for i in range(N):
k=rng.randrange(len(sess)); sid,cp,mk=sess[k]
cur=bytearray(adev.vram.view(cp,WIN)[:])
cid=rng.randrange(0,12); struct.pack_into('<I',cur,0,cid)
if rng.random()<0.4:
struct.pack_into('<I',cur,rng.randrange(1,WIN//4)*4,rng.choice([0,1,0xFFFFFFFF,0x1000]))
adev.vram.view(cp,WIN)[:]=bytes(cur); adev.gmc.flush_hdp()
st,dt=inv(sid,cid)
if dt>5: hits.append(('STALL',i,sid,cid)); break
adev.gmc.flush_hdp()
o=bytes(adev.vram.view(cp,TOTAL)[:])
foreign=[hex(v) for v in {struct.unpack_from('<I',o,x)[0] for x in range(WIN,TOTAL,4)}
if v!=mk and (v>>16)==0xC0DE]
if foreign: hits.append(('CROSS-SESSION',i,sid,foreign[:2])); break
# recycle a non-survivor session so slots churn underneath the survivor
if rng.random()<0.02 and len(sess)>1:
j=rng.randrange(1,len(sess))
unload(sess[j][0])
st2,sid2,cp2=load()
if st2==0 and sid2 is not None:
mk2=0xC0DE0000|(0x10+j)
adev.vram.view(cp2,TOTAL)[:]=bytes(struct.pack('<I',mk2)*(TOTAL//4)); adev.gmc.flush_hdp()
sess[j]=[sid2,cp2,mk2]
if i%250==0 and i and probe()!=ref: hits.append(('DRIFT',i)); break
print('%s: %d multi-session stateful invokes, hits=%s'%(name,N,hits[:4] if hits else 'none'), flush=True)
|