omega-gpu-driver
aien-dev/omega-gpu-driver
Brought by drakestapleton
omega-gpu-driver / tests / rms_check.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Host-only fake CUSE acceptance. Python is an independent test oracle, not backend.""" |
| 3 | import argparse, hashlib, os, random, socket, struct, subprocess, tempfile |
| 4 | from pathlib import Path |
| 5 | import rme_check as base |
| 6 | from rms_fixture import fixture |
| 7 | ROOT=base.ROOT |
| 8 | class Server(base.Server): |
| 9 | def ioctl(self,fh,nr,header,params=b'',nested=b'',error=0,raw=False): |
| 10 | cmd=nr if raw else 0xc0004600|(len(header)<<16)|nr |
| 11 | out=self.raw(fh,cmd);stages=[header,header+params,header+params+nested] |
| 12 | for stage in stages: |
| 13 | if not (len(out)>=16 and struct.unpack_from('<I',out,4)[0]==4):break |
| 14 | count=struct.unpack_from('<I',out,8)[0] |
| 15 | assert count in (1,2,3) |
| 16 | payload=stages[count-1] |
| 17 | out=self.raw(fh,cmd,payload,error=error if len(payload)==len(stages[-1]) else 0) |
| 18 | if error and len(payload)==len(stages[-1]):return b'' |
| 19 | assert out[:16]==bytes(16),(nr,out.hex()) |
| 20 | return out[16:] |
| 21 | def ctrlx(self,fh,cli,obj,cmd,params,nested=b''): |
| 22 | h=struct.pack('<IIIIQII',cli,obj,cmd,0,0x200000 if params else 0,len(params),0) |
| 23 | out=self.ioctl(fh,42,h,params,nested) |
| 24 | return struct.unpack_from('<I',out,28)[0],out[32:32+len(params)],out[32+len(params):] |
| 25 | |
| 26 | def wire(n,fields): |
| 27 | b=bytearray(n) |
| 28 | for off,fmt,value in fields:struct.pack_into('<'+fmt,b,off,value) |
| 29 | return bytes(b) |
| 30 | def allocmem(s,fh,cli,dev,obj,size=4096,owner=None,cls=62): |
| 31 | p=wire(128,[(0,'I',cli if owner is None else owner),(64,'Q',size),(72,'Q',4096),(88,'Q',max(size-1,0))]) |
| 32 | h=struct.pack('<IIIIQII',cli,dev,obj,cls,0x200000,128,0) |
| 33 | out=s.ioctl(fh,43,h,p);return struct.unpack_from('<I',out,28)[0] |
| 34 | def dma(s,fh,cli,dev,virt,obj,va,n=4096,off=0): |
| 35 | h=wire(64,[(0,'I',cli),(4,'I',dev),(8,'I',virt),(12,'I',obj),(16,'Q',off),(24,'Q',n),(48,'Q',va)]) |
| 36 | return struct.unpack_from('<I',s.ioctl(fh,87,h),56)[0] |
| 37 | def unmap(s,fh,cli,dev,virt,obj,va,n=4096): |
| 38 | h=wire(48,[(0,'I',cli),(4,'I',dev),(8,'I',virt),(12,'I',obj),(24,'Q',va),(32,'Q',n)]) |
| 39 | return struct.unpack_from('<I',s.ioctl(fh,88,h),40)[0] |
| 40 | def heap(s,fh,cli,dev): |
| 41 | out=s.ioctl(fh,74,wire(184,[(0,'I',cli),(4,'I',dev),(8,'I',5)])) |
| 42 | return struct.unpack_from('<IQQ',out,20) |
| 43 | class Memory: |
| 44 | def __init__(self):self.objects={};self.dead=set();self.maps={} |
| 45 | def alloc(self,obj,size,owner): |
| 46 | if obj in self.dead:return 52 |
| 47 | if owner!=1:return 54 |
| 48 | if not size or size%4096 or size>4194304:return 31 |
| 49 | if sum(self.objects.values())+size>4194304:return 81 |
| 50 | self.objects[obj]=size;self.dead.add(obj);return 0 |
| 51 | def map(self,obj,va,n,off=0): |
| 52 | if obj not in self.objects:return 51 |
| 53 | if off>self.objects[obj] or n>self.objects[obj]-off or not n or n%4096 or va%4096:return 31 |
| 54 | if any(va<v+length and v<va+n for v,length in self.maps.values()):return 52 |
| 55 | self.maps[obj]=(va,n);return 0 |
| 56 | def free(self,obj): |
| 57 | if obj not in self.objects:return 51 |
| 58 | self.objects.pop(obj);self.maps.pop(obj,None);return 0 |
| 59 | |
| 60 | def exercise(binary,red=False): |
| 61 | s=Server(binary) |
| 62 | try: |
| 63 | a=s.open();b=s.open();root=s.alloc(a,0,0,0,65)[1];assert root==1 |
| 64 | dev=s.alloc(a,root,root,100,128)[1];sub=s.alloc(a,root,dev,101,8320)[1] |
| 65 | assert s.alloc(a,root,dev,102,112,bytes(24))[0]==0 |
| 66 | # Exercise local pages and both USERMODE classes without doorbells. |
| 67 | assert allocmem(s,a,root,dev,150,cls=64)==0 |
| 68 | for obj,cls in ((151,50017),(152,50785)): |
| 69 | assert s.alloc(a,root,sub,obj,cls)[0]==0 |
| 70 | h=wire(56,[(0,'I',root),(4,'I',dev),(8,'I',obj),(24,'Q',65536),(48,'I',a)]) |
| 71 | out=s.ioctl(a,78,h);assert struct.unpack_from('<I',out,40)[0]==0 |
| 72 | addr=struct.unpack_from('<Q',out,32)[0] |
| 73 | assert struct.unpack_from('<I',s.ioctl(a,78,h),40)[0]==52 |
| 74 | u=wire(32,[(0,'I',root),(4,'I',dev),(8,'I',obj),(16,'Q',addr)]) |
| 75 | assert struct.unpack_from('<I',s.ioctl(a,79,u),24)[0]==0 |
| 76 | assert struct.unpack_from('<I',s.ioctl(a,79,u),24)[0]==51 |
| 77 | assert s.free(a,root,sub,obj)==0 |
| 78 | assert s.free(a,root,dev,150)==0 |
| 79 | m=Memory() |
| 80 | def alloc(obj,size=4096,owner=1): |
| 81 | expected=m.alloc(obj,size,owner);observed=allocmem(s,a,root,dev,obj,size,owner) |
| 82 | assert observed==(31 if red else expected),('alloc',obj,size,owner,observed,expected) |
| 83 | def mapping(obj,va,n=4096,off=0): |
| 84 | expected=m.map(obj,va,n,off);got=dma(s,a,root,dev,102,obj,va,n,off) |
| 85 | assert got==expected,('map',obj,va,n,off,got,expected) |
| 86 | def free(obj): |
| 87 | expected=m.free(obj);observed=s.free(a,root,dev,obj) |
| 88 | assert observed==expected,('free',obj,observed,expected) |
| 89 | alloc(200);alloc(201,8192);alloc(202,4096,2) |
| 90 | mapping(200,0x100000);mapping(201,0x100000);mapping(201,0x102000,8192) |
| 91 | assert dma(s,a,root+1,dev,102,200,0x104000)==51 |
| 92 | assert unmap(s,a,root,dev,102,201,0x102000,4096)==31 |
| 93 | assert unmap(s,a,root,dev,102,201,0x102000,8192)==0;m.maps.pop(201) |
| 94 | assert unmap(s,a,root,dev,102,201,0x102000,8192)==51 |
| 95 | mapping(201,0x102000,8192);free(200);free(200);alloc(200) |
| 96 | # Cross-open access is refused before a capability import and shared after it. |
| 97 | assert dma(s,b,root,dev,102,201,0x110000)==51 |
| 98 | s.ioctl(b,201,struct.pack('<I',a),error=-16) |
| 99 | s.release(b);b=s.open() |
| 100 | s.ioctl(b,201,struct.pack('<I',0x7ffffffe),error=-9) |
| 101 | s.ioctl(b,201,struct.pack('<I',a)) |
| 102 | assert dma(s,b,root,dev,102,201,0x110000)==0 |
| 103 | assert unmap(s,a,root,dev,102,201,0x110000)==0 |
| 104 | c=s.open();s.release(c);d=s.open();s.ioctl(d,201,struct.pack('<I',c),error=-9);s.release(d) |
| 105 | # Local-in-vendor inventory is fixture-derived; GR is a validated static GSP reply. |
| 106 | card=s.ioctl(a,200,bytes(4608));assert struct.unpack_from('<I',card)[0]==1 |
| 107 | assert s.ctrlx(a,root,root,517,struct.pack('<I',1)+bytes(28))[0]==0 |
| 108 | st,p,n=s.ctrlx(a,root,dev,0x800201,bytes(16));assert st==0 and struct.unpack_from('<I',p)[0]==3 |
| 109 | st,p,n=s.ctrlx(a,root,dev,0x800201,struct.pack('<IIQ',3,0,0x300000),bytes(12));assert st==0 and n==struct.pack('<III',0xcec0,0xc96f,0xc661) |
| 110 | st,p,n=s.ctrlx(a,root,sub,0x20801201,struct.pack('<IIQQQ',1,0,0x300000,0,0),struct.pack('<II',12,0));assert st==0 and struct.unpack_from('<I',n,4)[0]==0xc01 |
| 111 | gid=wire(268,[(4,'I',2),(8,'I',16)]);st,p,n=s.ctrlx(a,root,sub,0x2080014a,gid);assert st==0 and p[12:28]==b'HOST_MODEL_UUID1' |
| 112 | ver=s.ioctl(a,210,bytes(72));assert ver[8:].split(b'\0')[0]==b'ogd HOST_MODEL/1' |
| 113 | for nr in (206,207):assert struct.unpack_from('<I',s.ioctl(a,nr,bytes(16)),12)[0]==86 |
| 114 | # Randomized oracle comparison includes allocation exhaustion and out-of-range maps. |
| 115 | rng=random.Random(8025) |
| 116 | for obj in range(210,235): |
| 117 | size=rng.choice([4096,8192,16384,0,4097,4194304]);alloc(obj,size) |
| 118 | if obj in m.objects:mapping(obj,0x200000+(obj-210)*0x10000,rng.choice([4096,8192])) |
| 119 | expected_free=4194304-sum(m.objects.values()) |
| 120 | status,total,left=heap(s,a,root,dev);assert (status,total,left)==(0,4194304,expected_free) |
| 121 | assert s.free(a,root,root,dev)==0 |
| 122 | assert s.free(a,root,dev,201)==51 |
| 123 | # Cascade reclaims pages and maps, so an exact-full pool allocation succeeds. |
| 124 | dev2=s.alloc(a,root,root,400,128)[1] |
| 125 | assert allocmem(s,a,root,dev2,401,4194304)==0 |
| 126 | assert heap(s,a,root,dev2)==(0,4194304,0) |
| 127 | assert allocmem(s,a,root,dev2,402)==81 |
| 128 | s.release(a) |
| 129 | assert s.free(b,root,dev2,401)==0 # imported open retains the shared namespace |
| 130 | s.close() |
| 131 | except BaseException:s.abort();raise |
| 132 | |
| 133 | def uvm_cascade(binary): |
| 134 | s=Server(binary) |
| 135 | try: |
| 136 | a=s.open();u=s.open();cli=s.alloc(a,0,0,0,65)[1] |
| 137 | dev=s.alloc(a,cli,cli,100,128)[1] |
| 138 | assert s.alloc(a,cli,dev,102,112,bytes(24))[0]==0 |
| 139 | assert s.alloc(a,cli,dev,103,37105,bytes(56))[0]==0 |
| 140 | uuid=b'HOST_MODEL_UUID1' |
| 141 | def call(cmd,p,off=16): |
| 142 | return struct.unpack_from('<I',s.ioctl(u,cmd,p,raw=True),off)[0] |
| 143 | assert call(805306369,bytes(16),8)==0 |
| 144 | assert call(37,wire(40,[(0,'16s',uuid),(24,'I',0xffffffff)]),36)==0 |
| 145 | assert call(25,wire(32,[(0,'16s',uuid),(16,'I',a),(20,'I',cli),(24,'I',103)]),28)==0 |
| 146 | area=wire(24,[(0,'Q',0x100000),(8,'Q',4096)]) |
| 147 | assert call(73,area)==0 |
| 148 | assert allocmem(s,a,cli,dev,200)==0 |
| 149 | assert dma(s,a,cli,dev,102,200,0x100000)==0 |
| 150 | p=wire(9264,[(0,'Q',0x100000),(8,'Q',4096),(24,'16s',uuid),(9240,'Q',1),(9248,'I',a),(9252,'I',cli),(9256,'I',200)]) |
| 151 | assert call(33,p,9260)==0 |
| 152 | assert s.free(a,cli,dev,200)==0 |
| 153 | got=call(73,area) |
| 154 | assert got==0,('UVM mapping survived backing free',got) |
| 155 | assert call(34,area)==0 |
| 156 | assert s.free(a,cli,dev,103)==0 |
| 157 | assert call(73,area)==54 |
| 158 | assert call(38,wire(20,[(0,'16s',uuid)]))==0 |
| 159 | s.close() |
| 160 | except BaseException:s.abort();raise |
| 161 | |
| 162 | MUTANTS=[ |
| 163 | ('ownership','rm/memory.om','if ne rmget b 104 4 get a 0','if false'), |
| 164 | ('map-overlap','rm/memory.om','if rmmover t k va get a 2','if false'), |
| 165 | ('range-limit','rm/memory.om','if or gt get a 1 size gt get a 2 sub size get a 1','if false'), |
| 166 | ('stale-handle','rm/handles.om','if rmseen t cli obj','if false'), |
| 167 | ('double-free','rm/handles.om','if eq s 64\nret 2','if eq s 64\nret 0'), |
| 168 | ('cascade-pages','rm/memory.om','put t add add 1024 get t add k 1 p 0','put t add add 1024 get t add k 1 p 1'), |
| 169 | ('token-forgery','rm/import.om','src = rmclient c rmget b 72 4','src = ulong 0'), |
| 170 | ('cross-client','rm/handles.om','if eq get t add k 2 cli\nret j','if true\nret j'), |
| 171 | ('partial-unmap','rm/memory.om','if and ne get a 2 0 ne get a 2 get t add k 4','if false'), |
| 172 | ('uvm-cascade','rm/memory.om','\nrmmuvgc t\n','\nnote UVM collection removed\n'), |
| 173 | ] |
| 174 | def client(tmp,binary,checkout): |
| 175 | pin='16f11c47fa9587349b607d5ed174113b6fc9b504' |
| 176 | actual=subprocess.check_output(['git','-C',str(checkout),'rev-parse','HEAD'],text=True).strip() |
| 177 | assert actual==pin,('unmodified CUDApp source pin',actual,pin) |
| 178 | subprocess.run(['git','-C',str(checkout),'diff','--quiet','HEAD','--'],check=True) |
| 179 | print('UNMODIFIED CUDApp source pin',pin,'tracked tree clean PASS',flush=True) |
| 180 | inc=[checkout/'nvrm',checkout/'src',base.OGKM/'src/common/sdk/nvidia/inc',base.OGKM/'kernel-open/common/inc',base.OGKM/'src/nvidia/arch/nvalloc/unix/include',base.OGKM/'src/common/inc',base.OGKM/'kernel-open/nvidia-uvm'] |
| 181 | names=['cudapp_om_amp_runtime.c','omega_ampere_qmd.c','cudapp_om_nvrm_wire_pack.c','cudapp_om_nvrm_wire_reply.c','cudapp_om_nvrm_heap_wire.c','cudapp_om_nvrm_uvm_wire.c','cudapp_om_nvrm_uvm_map_wire.c'] |
| 182 | cmd=['cc','-std=gnu11','-O1','-ffunction-sections','-fdata-sections','-DCUDAPP_NVRM_TEST_TRANSPORT=1',*[f'-I{p}' for p in inc],str(ROOT/'tests/rms_client.c'),*[str(checkout/'src'/n) for n in names],'-Wl,--gc-sections','-o',str(tmp/'client')] |
| 183 | subprocess.run(cmd,check=True) |
| 184 | subprocess.run([str(tmp/'client'),str(binary),str(ROOT/'rm/host-model.fixture')],check=True,timeout=45) |
| 185 | def main(): |
| 186 | p=argparse.ArgumentParser();p.add_argument('--mutants',action='store_true');p.add_argument('--client',type=Path);args=p.parse_args() |
| 187 | assert os.environ.get('AIEN_TEST_CPUS')=='10-14' |
| 188 | assert not Path('/dev/nvidiactl').exists() and not Path('/dev/cuse').exists() |
| 189 | assert (ROOT/'rm/host-model.fixture').read_bytes()==fixture() |
| 190 | with tempfile.TemporaryDirectory(prefix='ogd-rms-') as d: |
| 191 | tmp=Path(d);binary=base.build(tmp) |
| 192 | print('RM serving compiler SHA256',base.SHA,flush=True) |
| 193 | print('RM serving source SHA256',hashlib.sha256((tmp/'rme.om').read_bytes()).hexdigest(),flush=True) |
| 194 | print('RM serving ELF SHA256',hashlib.sha256(binary.read_bytes()).hexdigest(),flush=True) |
| 195 | for name,off in (('provenance',0),('checksum',288)): |
| 196 | bad=bytearray(fixture());bad[off]^=1;path=tmp/name;path.write_bytes(bad) |
| 197 | p=subprocess.run([str(binary),'--fake','--host-model',str(path)],input=b'',capture_output=True,timeout=10) |
| 198 | assert p.returncode==65,(name,p.returncode) |
| 199 | p=subprocess.run([str(binary),'--fake'],input=b'',capture_output=True,timeout=10);assert p.returncode==64 |
| 200 | print('RM fixture provenance/checksum and explicit-mode negative controls PASS',flush=True) |
| 201 | exercise(binary);print('RM serving Python differential HOST_MODEL PASS',flush=True) |
| 202 | uvm_cascade(binary);print('RM/UVM mapped-range and VAS cascading free PASS',flush=True) |
| 203 | try:exercise(binary,red=True) |
| 204 | except AssertionError:print('RM serving red control KILLED',flush=True) |
| 205 | else:raise AssertionError('red control survived') |
| 206 | if args.client:client(tmp,binary,args.client) |
| 207 | if args.mutants: |
| 208 | for name,unit,old,new in MUTANTS: |
| 209 | with tempfile.TemporaryDirectory(dir=tmp) as folder: |
| 210 | mutant=base.build(Path(folder),(unit,old,new)) |
| 211 | try:exercise(mutant);uvm_cascade(mutant) |
| 212 | except (AssertionError,ConnectionError,socket.timeout):print('RM serving mutant',name,'KILLED',flush=True) |
| 213 | else:raise AssertionError('surviving mutant '+name) |
| 214 | print('RM serving HOST_MODEL PASS; GPU_BACKING/DOORBELLS/CHIP NOT_RUN',flush=True) |
| 215 | if __name__=='__main__':main() |