CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / amd-exec / token_access.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Test-only seeded executor instrumentation: reject EVERY out-of-buffer access. |
| 4 | Build one instrument per concrete ABI image; run production and numerical controls. |
| 5 | The instrumentation records actual maximum read/write byte offsets, not a formula. |
| 6 | """ |
| 7 | import base64 |
| 8 | import hashlib |
| 9 | import json |
| 10 | import pathlib |
| 11 | import runpy |
| 12 | import struct |
| 13 | import subprocess |
| 14 | import sys |
| 15 | HERE=pathlib.Path(__file__).parent |
| 16 | |
| 17 | def specs(): |
| 18 | g=runpy.run_path(str(HERE/'gdn.py')) |
| 19 | d=runpy.run_path(str(HERE/'decode.py')) |
| 20 | p=runpy.run_path(str(HERE/'prep_section.py')) |
| 21 | return [('gdnqknorm',g['qk_fixture'],g['QK_CASES'][-1],g['QK_CONTROLS']), |
| 22 | ('gdnstate',g['state_fixture'],g['STATE_CASES'][-1],g['STATE_CONTROLS'])]+[ |
| 23 | (n,f,(params[-1],),controls) for n,f,params,controls in d['SPECS']+p['SPECS']] |
| 24 | |
| 25 | def ranges(case): |
| 26 | if 'buffers' in case: return case['buffers'] |
| 27 | p=[v for kind,v in case['args'] if kind=='ptr']; end=len(base64.b64decode(case['memory'])) |
| 28 | return [dict(offset=x,bytes=y-x-64) for x,y in zip(p,p[1:]+[end])] |
| 29 | |
| 30 | def instrument(source,spans): |
| 31 | n=len(spans) |
| 32 | header=f'global hits ulong {n*2}\nglobal ob uchar 8\nfn touch p ulong w ulong rw ulong to void\n' |
| 33 | for i,(start,size) in enumerate(spans): |
| 34 | header+=f'if and ge p {start} lt p {start+size}\nif gt w sub ulong {start+size} p\nexit 68\nend\nv = sub add p w {start}\ni = add ulong {i*2} rw\nif gt v get hits i\nput hits i v\nend\nret\nend\n' |
| 35 | header+='exit 68\nend\n' |
| 36 | source=source.replace('fn mread p ulong to ulong\n','fn mread p ulong to ulong\ntouch p 4 0\n',1) |
| 37 | source=source.replace('fn mbyte p ulong to ulong\n','fn mbyte p ulong to ulong\ntouch p 1 0\n',1) |
| 38 | source=source.replace('fn mhalf p ulong to ulong\n','fn mhalf p ulong to ulong\ntouch p 2 0\n',1) |
| 39 | source=source.replace('fn mwrite p ulong v ulong to void\n','fn mwrite p ulong v ulong to void\ntouch p 4 1\n',1) |
| 40 | end=f'for i ulong {n*2}\nfor j ulong 8\nput ob j uchar and shr get hits i mul j 8 255\nend\nr = write 1 ob 0 8\nif ne r 8\nret 74\nend\nend\n' |
| 41 | assert source.endswith('ret 0\nend\n') |
| 42 | return source.replace('global st ulong 18\n',header+'global st ulong 18\n',1)[:-len('ret 0\nend\n')]+end+'ret 0\nend\n' |
| 43 | |
| 44 | def suite(omc,src,out): |
| 45 | source=''.join((src/'exec'/f).read_text() for f in ('fp.om','fma.om','main.om')) |
| 46 | reports=[] |
| 47 | for name,factory,params,controls in specs(): |
| 48 | case=factory(*params); buffers=ranges(case); size=sum(8 if k=='ptr' else 4 for k,v in case['args']) |
| 49 | spans=[(0,size)]+[(b['offset'],b['bytes']) for b in buffers] |
| 50 | text=instrument(source,spans); path=out/(name+'-access.om'); path.write_text(text) |
| 51 | exe=out/(name+'-access'); p=subprocess.run([omc],input=text.encode(),capture_output=True,timeout=120) |
| 52 | assert p.returncode==0 and not p.stderr,(name,p.stderr) |
| 53 | exe.write_bytes(p.stdout); exe.chmod(0o700) |
| 54 | objects=[] |
| 55 | for variant in (name,*controls): |
| 56 | memory=out/'access.memory'; memory.write_bytes(base64.b64decode(case['memory'])) |
| 57 | p=subprocess.run([str(exe),str(out/(variant+'.text')),str(memory),str(case['gx']*64),str(case['gy'])],capture_output=True,timeout=600) |
| 58 | assert p.returncode==0 and not p.stderr and len(p.stdout)==16*len(spans),(name,variant,p.returncode,p.stderr) |
| 59 | values=struct.unpack('<'+str(len(spans)*2)+'Q',p.stdout) |
| 60 | offsets=[dict(bytes=n,max_read_byte=(values[2*i]-1 if values[2*i] else None),max_write_byte=(values[2*i+1]-1 if values[2*i+1] else None)) for i,(start,n) in enumerate(spans)] |
| 61 | objects.append(dict(object=variant,access=offsets)) |
| 62 | print('PASS ACCESS',variant,json.dumps(offsets,separators=(',',':')),flush=True) |
| 63 | # Remove the last actually read byte; allocated padding is not a valid negative control. |
| 64 | last=objects[0]['access'][1]['max_read_byte']; assert last is not None |
| 65 | badspans=list(spans); start,n=badspans[1]; badspans[1]=(start,last) |
| 66 | bad=instrument(source,badspans); p=subprocess.run([omc],input=bad.encode(),capture_output=True,timeout=120) |
| 67 | assert p.returncode==0 and not p.stderr |
| 68 | mutant=out/(name+'-access-short'); mutant.write_bytes(p.stdout); mutant.chmod(0o700) |
| 69 | memory.write_bytes(base64.b64decode(case['memory'])) |
| 70 | p=subprocess.run([str(mutant),str(out/(name+'.text')),str(memory),str(case['gx']*64),str(case['gy'])],capture_output=True,timeout=600) |
| 71 | assert p.returncode==68 and not p.stdout and not p.stderr,(name,p.returncode,p.stderr) |
| 72 | print('CONTROL ACCESS',name,'short-input CAUGHT exit=68',flush=True) |
| 73 | reports.append(dict(kernel=name,case=case['label'],kernarg_bytes=size,grid=case['timing_grid'],block=[64,1,1],buffers=buffers,objects=objects,source_sha256=hashlib.sha256(text.encode()).hexdigest())) |
| 74 | (out/'token-access.json').write_text(json.dumps(reports,indent=2)+'\n') |
| 75 | if __name__=='__main__': suite(sys.argv[1],pathlib.Path(sys.argv[2]),pathlib.Path(sys.argv[3])) |