aboutsummaryrefslogtreecommitdiffstats
path: root/hw/session_interleave.py
blob: a4202c8f1066d666fdaf5a8f31898f59494da2bc (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
#!/usr/bin/env python3
"""Concurrent sessions: does interleaving invokes leak state across them?

Every hardware test so far used ONE session sequentially. SYS_DRV keeps a global 32-entry
slot table, so multiple live sessions share platform state. If interleaved invokes let one
session's data appear in another's buffer, that is cross-session contamination -- a
memory-safety bug, and the shape that precedes a corruption primitive.

Method: load the same TA N times (N live sessions), give each a buffer stamped with its own
unique marker, then invoke round-robin many times and check that no session's buffer ever
contains another session's marker.

Control: each session must actually produce output; if none does, the test proves nothing.
"""
import glob, 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)
N=8; CMD=5
def alloc(n):
    pa=adev.mm.palloc(n,align=0x1000,zero=True,boot=False); return pa,adev.paddr2mc(pa)
blob=open(glob.glob('/root/psp/contall/ta_16-0-0-9_9e9044cf*')[0],'rb').read()  # AUC: writes output
def load():
    ap_,amc=alloc(0x80000); cp,cmc=alloc(0x8000)
    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=0x4000
    r=submit(c); return r.resp.status&0xFFFFFFFF, r.resp.session_id, cp
sess=[]
for i in range(N):
    st,sid,cp=load()
    if st!=0: print('load %d failed %#x'%(i,st)); break
    sess.append((sid,cp,0xDEAD0000|i))
print('live sessions: %s'%[s[0] for s in sess], flush=True)
def stamp(cp,marker):
    buf=bytearray(struct.pack('<I',marker)*(0x4000//4)); struct.pack_into('<I',buf,0,CMD)
    adev.vram.view(cp,0x4000)[:]=bytes(buf); adev.gmc.flush_hdp()
for sid,cp,mk in sess: stamp(cp,mk)
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
    try: return submit(c).resp.status&0xFFFFFFFF
    except Exception as e:
        try: return int(str(e).strip().split()[-1])&0xFFFFFFFF
        except Exception: return -1
rounds=60; produced=set(); contam=[]
for r in range(rounds):
    for idx,(sid,cp,mk) in enumerate(sess):
        s=inv(sid,CMD)
        adev.gmc.flush_hdp()
        out=bytes(adev.vram.view(cp,0x4000)[:])
        vals={struct.unpack_from('<I',out,o)[0] for o in range(0,0x4000,4)}
        if any(v!=mk and (v>>16)==0xDEAD for v in vals):
            foreign=[hex(v) for v in vals if v!=mk and (v>>16)==0xDEAD]
            contam.append((r,sid,hex(mk),foreign[:3])); break
        if len(vals-{mk})>1: produced.add(sid)
    if contam: break
print('sessions that produced output (control): %d/%d'%(len(produced),len(sess)), flush=True)
print('cross-session contamination: %s'%(contam[:3] if contam else 'NONE over %d rounds'%rounds), flush=True)