#!/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('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('>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('