Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/kfd/check.py # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / kfd / check.py` 440 lines ยท 23261 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Independent CPU-only test instrument, not an Omega runtime dependency. 3 Run under the assigned CPU pool lock. Never opens an existing /dev/kfd. 4 C provides installed-UAPI constants; Omega provides every tested operation. 5 """ 6 from pathlib import Path 7 import hashlib 8 import json 9 import os 10 import platform 11 import re 12 import shutil 13 import subprocess 14 import sys 15 16 root = Path(__file__).resolve().parents[2] 17 scan = subprocess.run([sys.executable, str(root / 'tools/uxl/windows_paths.py')]) 18 if scan.returncode: 19 sys.exit(scan.returncode) 20 21 host_sys = platform.system() 22 host_arch = platform.machine() 23 if host_sys != 'Linux' or not Path('/usr/include/linux/kfd_ioctl.h').is_file(): 24 print(f'NOT_RUN tests/kfd/check.py: requires Linux with /usr/include/linux/kfd_ioctl.h (host: {host_sys}/{host_arch})', file=sys.stderr) 25 sys.exit(77) 26 os.chdir(root) 27 out = root / 'build/kfd' 28 out.mkdir(parents=True, exist_ok=True) 29 omc = root / 'seed/out/omc' 30 rows = [] 31 32 def run(cmd, data=None): 33 p = subprocess.run([str(x) for x in cmd], input=data, stdout=subprocess.PIPE, 34 stderr=subprocess.PIPE, timeout=90) 35 return p 36 37 def record(name, actual, want, detail=''): 38 rows.append(dict(name=name, actual=actual, expected=want, detail=detail)) 39 print(('PASS' if actual == want else 'FAIL'), name, 'exit', actual, 'expected', want, detail, flush=True) 40 41 # Coordinated keyword deletion must not pass merely because generator and chain agree. 42 # The semantic canonicalizer inventory is copied unchanged into the isolated mutant. 43 p = run(['sh', 'seed/omc/gate18.sh']) 44 record('keyword inventory baseline', p.returncode, 0, p.stdout.decode()+p.stderr.decode()) 45 kwmut = out/'keyword-removal' 46 for rel in ('seed/omc/gate18.sh', 'seed/omc/genkw.awk', 'seed/omc/omc.om', 47 'seed/semhash/semhash.om'): 48 dest = kwmut/rel 49 dest.parent.mkdir(parents=True, exist_ok=True) 50 shutil.copyfile(root/rel, dest) 51 genpath = kwmut/'seed/omc/genkw.awk' 52 gen = genpath.read_text() 53 assert gen.count(' kfdctl:241') == 1 54 genpath.write_text(gen.replace(' kfdctl:241', '')) 55 chainpath = kwmut/'seed/omc/omc.om' 56 chain = chainpath.read_text() 57 chain, dropped = re.subn(r'^if eq w [0-9]+\nret 241\nend\n', '', chain, flags=re.M) 58 assert dropped == 1 59 chainpath.write_text(chain) 60 p = run(['sh', kwmut/'seed/omc/gate18.sh']) 61 record('keyword coordinated-removal control', p.returncode, 1, p.stdout.decode()+p.stderr.decode()) 62 record('keyword control failure reason', 63 int(b'generated keyword inventory differs from committed semantic inventory' in p.stderr), 1) 64 65 names = ['GET_VERSION','ACQUIRE_VM','GET_PROCESS_APERTURES_NEW','CREATE_QUEUE', 66 'DESTROY_QUEUE','ALLOC_MEMORY_OF_GPU','FREE_MEMORY_OF_GPU', 67 'MAP_MEMORY_TO_GPU','UNMAP_MEMORY_FROM_GPU','CREATE_EVENT','DESTROY_EVENT', 68 'WAIT_EVENTS','SET_SCRATCH_BACKING_VA'] 69 header = Path('/usr/include/linux/kfd_ioctl.h').read_text() 70 all_names = re.findall(r'^#define AMDKFD_IOC_(\w+)\s', header, re.M) 71 csrc = '#include \n#include \nint main(void){\n' 72 for name in all_names: 73 csrc += f'printf("{name} %lu %lu\\n", (unsigned long)AMDKFD_IOC_{name}, (unsigned long)_IOC_SIZE(AMDKFD_IOC_{name}));\n' 74 csrc += 'return 0; }\n' 75 (out/'abi.c').write_text(csrc) 76 p=run(['cc','-Wall','-Werror',out/'abi.c','-o',out/'abi']) 77 record('C UAPI oracle compile',p.returncode,0,p.stderr.decode()) 78 if p.returncode: sys.exit(1) 79 p=run([out/'abi']); record('C UAPI oracle run',p.returncode,0) 80 (out/'requests.txt').write_bytes(p.stdout) 81 abi={name:(int(q),int(n)) for name,q,n in (line.split() for line in p.stdout.decode().splitlines())} 82 83 common='global H uchar 88\nglobal P uchar 112\n' 84 def put(off, value, width=4, arr='H'): 85 return ''.join(f'put {arr} {off+i} {b}\n' for i,b in enumerate(value.to_bytes(width,'little'))) 86 87 def shape(name): 88 setup='' 89 suffix='' 90 if name=='GET_PROCESS_APERTURES_NEW': setup=put(8,1); suffix=' P 0 56' 91 if name in ('MAP_MEMORY_TO_GPU','UNMAP_MEMORY_FROM_GPU'): setup=put(16,1); suffix=' P 0 4' 92 if name=='WAIT_EVENTS': setup=put(8,1); suffix=' P 0 48' 93 if name in ('ALLOC_MEMORY_OF_GPU','SET_SCRATCH_BACKING_VA'): 94 setup='r = kfdres `2097152`\n'; suffix=' r' 95 if name=='ALLOC_MEMORY_OF_GPU': setup+=put(8,4096,8)+put(36,2) 96 if name=='CREATE_QUEUE': 97 setup='r = mapnew neg int 1 0 4096 3 34\n'+put(32,4096)+put(40,2) 98 suffix=' r r r long 0 long 0' 99 return setup,suffix 100 101 def qsetup(size, prot=3): 102 # Ring r and a separate queue-object mapping q of `size` bytes; queue type 2 is COMPUTE_AQL. 103 return f'r = mapnew neg int 1 0 4096 3 34\nq = mapnew neg int 1 0 {size} {prot} 34\n'+put(32,4096)+put(40,2) 104 105 def aperture(count=1, off=16, length=None, size=4096, prot=3, changes=''): 106 if length is None: length=count*56 107 setup=f'r = mapnew neg int 1 0 {size} {prot} 34\n'+put(8,count) 108 return source('GET_PROCESS_APERTURES_NEW',changes,setup_override=setup, 109 suffix_override=f' r {off} {length}') 110 111 112 def source(name, changes='', size=None, setup_override=None, suffix_override=None, request=None): 113 q,n=abi[name] 114 setup,suffix=shape(name) 115 if setup_override is not None: setup=setup_override 116 if suffix_override is not None: suffix=suffix_override 117 if size is not None: n=size 118 if request is not None: q=request 119 return common+'fn main to int\n'+setup+changes+f'v = kfdctl neg int 1 {q} H 0 {n}{suffix}\nif ne v neg long 9\nret 80\nend\nret 0\nend\n' 120 aperture_bad = { 121 'stale': aperture(changes='drop = mapdrop r\ns = mapnew neg int 1 0 4096 3 34\n'), 122 'dead': aperture(changes='drop = mapdrop r\n'), 123 'readonly': aperture(prot=1), 124 'extent': aperture(off=4090), 125 'wrap': aperture(off=2**64-16), 126 'count': aperture(count=3,length=2*56), 127 'count-u32-max': aperture(count=2**32-1,length=0), 128 'address': aperture(changes=put(0,1,8)), 129 } 130 131 def compile_case(label, src, target, want=0, compiler=omc): 132 stem=out/(target+'-'+label) 133 text=('target x86linux\n' if target=='x86' else '')+src 134 # QEMU-user's unsupported KFD ioctl translator returns ENOTTY before fd lookup. 135 # Native Linux on AArch64 reaches fd lookup and returns EBADF. This is not device evidence. 136 if target=='x86' and 'v = kfdctl ' in src: 137 text=text.replace('if ne v neg long 9\n','if ne v neg long 25\n') 138 stem.with_suffix('.om').write_text(text) 139 p=run([compiler],text.encode()) 140 stem.with_suffix('.err').write_bytes(p.stderr) 141 record(target+' compile '+label,p.returncode,want,p.stderr.decode().strip()) 142 if p.returncode: return None 143 stem.write_bytes(p.stdout); stem.chmod(0o755) 144 return stem 145 146 qemu=shutil.which('qemu-x86_64') 147 objdump=shutil.which('llvm-objdump-18') 148 print('qemu-x86_64',qemu,'llvm-objdump-18',objdump,flush=True) 149 150 def execute(label, binary, target, want=0): 151 if binary is None: return 152 if target=='x86' and not qemu: 153 print('NOT_RUN x86 '+label+' qemu-x86_64 missing'); return 154 p=run(([qemu] if target=='x86' else [])+[binary]) 155 binary.with_suffix('.run.err').write_bytes(p.stderr) 156 record(target+' run '+label,p.returncode,want,p.stderr.decode().strip()) 157 158 for target in ('a64','x86'): 159 for name in names: 160 b=compile_case(name,source(name),target) 161 execute(name,b,target,103 if name=='ACQUIRE_VM' else 0) 162 if b and objdump: 163 p=run([objdump,'-D','--print-imm-hex',b]); b.with_suffix('.dis').write_bytes(p.stdout) 164 record(target+' disassemble '+name,p.returncode,0) 165 # x86 has one literal movabs; AArch64 materializes MOVZ/MOVK halves. 166 q,n=abi[name] 167 dis=p.stdout.decode() 168 found=(hex(q) in dis) if target=='x86' else (hex(q&65535) in dis and hex(q>>16) in dis) 169 record(target+' UAPI immediate '+name,0 if found else 1,0,hex(q)) 170 print('NOT_RUN device '+target+' '+name+' no authorized AMD device') 171 for name in all_names: 172 if name not in names: 173 q,n=abi[name] 174 src=f'global H uchar {n}\nfn main to int\nv = kfdctl neg int 1 {q} H 0 {n}\nret 0\nend\n' 175 compile_case('sibling-refusal-'+name,src,target,9) 176 # One writable handle named as both write and read pointer selects the AMD queue object: 177 # the compiler binds base+56 and base+128 and requires the 2304-byte amd_queue_v2_t; 178 # distinct handles stay base-only. 179 for label,setup,suffix in [('queue-object-shared-ring',None,None), 180 ('queue-object-exact-2304',qsetup(2304),' r q q long 0 long 0'), 181 ('queue-base-distinct',qsetup(4096)+'p = mapnew neg int 1 0 4096 3 34\n',' r q p long 0 long 0')]: 182 execute(label,compile_case(label,source('CREATE_QUEUE',setup_override=setup,suffix_override=suffix),target),target) 183 for label,src in [('count-query',aperture(count=0,length=0)), 184 ('one',aperture()),('many-exact',aperture(count=7,size=16+7*56)), 185 ('end-empty',aperture(count=0,off=4096,length=0)), 186 ('runtime-span',aperture(off='start',length='bytes', 187 changes='start = ulong 16\nbytes = ulong 392\n'+put(8,7)))]: 188 execute('aperture-'+label,compile_case('aperture-'+label,src,target),target) 189 for label,src in aperture_bad.items(): 190 execute('aperture-'+label,compile_case('aperture-'+label,src,target),target,103) 191 dynamic_size=source('GET_VERSION',size='bad').replace('fn main to int\n','fn main to int\nbad = ulong 7\n') 192 bads=[('wrong-request',source('GET_VERSION',request=0),0,9), 193 ('wrong-size-literal',source('GET_VERSION',size=7),0,9), 194 ('wrong-size-dynamic',dynamic_size,103,0), 195 ('ring-small',source('CREATE_QUEUE',put(32,512)),103,0), 196 ('ring-nonpower',source('CREATE_QUEUE',put(32,3072)),103,0), 197 ('wrong-drm-kind',source('ACQUIRE_VM'),103,0), 198 ('span-count',source('GET_PROCESS_APERTURES_NEW',put(8,3)),103,0), 199 ('event-extension',source('WAIT_EVENTS',put(32,1,8,'P')),103,0), 200 ('userptr',source('ALLOC_MEMORY_OF_GPU',put(36,4)),103,0), 201 ('wrong-handle-kind',source('ALLOC_MEMORY_OF_GPU',setup_override='r = mapnew neg int 1 0 4096 3 34\n'+put(8,4096,8)),103,0), 202 ('stale-handle',source('ALLOC_MEMORY_OF_GPU',changes='drop = mapdrop r\n'),103,0), 203 ('short-handle',source('ALLOC_MEMORY_OF_GPU',put(8,`4194304`,8)),103,0)] 204 objq=' r q q long 0 long 0' 205 for kind in (0,1): 206 bads.append(('queue-object-type-'+str(kind),source('CREATE_QUEUE',put(40,kind),setup_override=qsetup(4096),suffix_override=objq),103,0)) 207 for size in (48,100,255,256,2303): 208 bads.append(('queue-object-short-'+str(size),source('CREATE_QUEUE',setup_override=qsetup(size),suffix_override=objq),103,0)) 209 bads.append(('queue-object-readonly',source('CREATE_QUEUE',setup_override=qsetup(4096,1),suffix_override=objq),103,0)) 210 bads.append(('queue-object-stale',source('CREATE_QUEUE','drop = mapdrop q\n',setup_override=qsetup(4096),suffix_override=objq),103,0)) 211 for off in (8,16): 212 for value in (56,128): 213 bads.append(('queue-object-address-'+str(off)+'-'+str(value),source('CREATE_QUEUE',put(off,value,8),setup_override=qsetup(4096),suffix_override=objq),103,0)) 214 distinct=qsetup(4096)+'p = mapnew neg int 1 0 4096 3 34\n' 215 bads.append(('queue-base-address-'+str(off)+'-'+str(value),source('CREATE_QUEUE',put(off,value,8),setup_override=distinct,suffix_override=' r q p long 0 long 0'),103,0)) 216 for name,offs in [('GET_PROCESS_APERTURES_NEW',[0]),('CREATE_QUEUE',[0,8,16,56,72]), 217 ('ALLOC_MEMORY_OF_GPU',[0]),('MAP_MEMORY_TO_GPU',[8]), 218 ('UNMAP_MEMORY_FROM_GPU',[8]),('WAIT_EVENTS',[0]), 219 ('SET_SCRATCH_BACKING_VA',[0])]: 220 for off in offs: 221 bads.append((name+'-address-'+str(off),source(name,put(off,1,8)),103,0)) 222 for label,src,want,ce in bads: 223 b=compile_case(label,src,target,ce) 224 if not ce: execute(label,b,target,want) 225 for word,expr in [('kfdctl',f'kfdctl neg int 1 {abi["GET_VERSION"][0]} H 0 8'), 226 ('kfdmap','kfdmap neg int 1 0 4096'),('kfdres','kfdres 4096')]: 227 compile_case('function-'+word,'fn f to long\nH = new uchar 8 0\nret '+expr+'\nend\n',target,9) 228 compile_case('om2-function-'+word,'om2\nfn f to long\nH = new uchar 8 0\nret '+expr+'\nend\n',target,9) 229 missing='text K "/dev/kfd"\nfn main to int\nf = fopen K 0 len K 2 0\nif ne f neg long 2\nret 81\nend\nret 0\nend\n' 230 if not Path('/dev/kfd').exists(): execute('absent-kfd',compile_case('absent-kfd',missing,target),target) 231 else: print('NOT_RUN absent-kfd: device exists; never open it from CPU gate') 232 mapping='''text K "kfd-cpu-map.bin" 233 fn main to int 234 r = kfdres `68721573888` 235 if le r 0 236 ret 82 237 end 238 if ne and mapaddr r `2097151` 0 239 ret 83 240 end 241 if ne mapdrop r 0 242 ret 84 243 end 244 f = fopen K 0 len K 578 384 245 if lt f 0 246 ret 85 247 end 248 if ne ftrunc int f 4096 0 249 ret 86 250 end 251 r = kfdres 4096 252 a = mapaddr r 253 if ne mapbind r int f 0 3 0 254 ret 87 255 end 256 if ne mapaddr r a 257 ret 88 258 end 259 if ne mapset r 0 8 12345 0 260 ret 89 261 end 262 s = mapnew int f 0 4096 3 1 263 if le s 0 264 ret 90 265 end 266 if ne mapget s 0 8 12345 267 ret 91 268 end 269 v = mapdrop r 270 v = mapdrop s 271 v = fclose int f 272 v = unlink K 0 len K 273 ret 0 274 end 275 ''' 276 execute('mapping-large-aligned',compile_case('mapping-large-aligned',mapping,target),target) 277 regular='text K "kfd-map-refusal.bin"\nfn main to int\nf = fopen K 0 len K 578 384\nr = unlink K 0 len K\nr = kfdmap int f 0 4096\nret 0\nend\n' 278 execute('map-regular-refusal',compile_case('map-regular-refusal',regular,target),target,103) 279 null='text K "/dev/null"\nfn main to int\nf = fopen K 0 len K 2 0\nr = kfdmap int f 0 4096\nret 0\nend\n' 280 execute('map-character-refusal',compile_case('map-character-refusal',null,target),target,103) 281 invalid='fn main to int\nr = kfdmap neg int 1 0 4096\nret 0\nend\n' 282 execute('map-invalid-fd',compile_case('map-invalid-fd',invalid,target),target,103) 283 for label,expr in [('reserve-zero','kfdres 0'),('reserve-unaligned','kfdres 4097'),('reserve-overflow','kfdres `18446744073709547520`')]: 284 execute(label,compile_case(label,'fn main to int\nr = '+expr+'\nret 0\nend\n',target),target,103) 285 286 # Native trace oracle observes the syscall boundary, independently of errno. 287 if host_arch == 'aarch64': 288 p=run(['cc','-Wall','-Wextra','-Werror',root/'tests/kfd/trace.c','-o',out/'trace']) 289 record('trace oracle compile',p.returncode,0,p.stderr.decode()) 290 if not p.returncode: 291 for name,offs in [('GET_VERSION',[]),('GET_PROCESS_APERTURES_NEW',[0]), 292 ('CREATE_QUEUE',[0,8,16,56,72]),('ALLOC_MEMORY_OF_GPU',[0]), 293 ('MAP_MEMORY_TO_GPU',[8]),('UNMAP_MEMORY_FROM_GPU',[8]), 294 ('WAIT_EVENTS',[0]),('SET_SCRATCH_BACKING_VA',[0])]: 295 kwargs={} 296 if name=='CREATE_QUEUE': 297 kwargs={'changes':put(64,64,8)+put(80,64),'suffix_override':' r r r r r'} 298 s=source(name,**kwargs).replace('v = kfdctl ','w = write int 1 P 0 112\nv = kfdctl ') 299 checks='' 300 for off in offs: 301 for i in range(8): checks+=f'if ne get H {off+i} 0\nret 95\nend\n' 302 if name in ('GET_VERSION','ALLOC_MEMORY_OF_GPU'): 303 off=0 if name=='GET_VERSION' else 16 304 for i,b in enumerate((0x44332211).to_bytes(8,'little')): 305 checks+=f'if ne get H {off+i} {b}\nret 96\nend\n' 306 s=s.replace('ret 0\nend\n',checks+'ret 0\nend\n') 307 b=compile_case('trace-'+name,s,'a64') 308 if b: 309 p=run([out/'trace',b]+([56,128] if name=='CREATE_QUEUE' else [])) 310 record('trace '+name,p.returncode,0,p.stderr.decode()) 311 checks=''.join(f'if ne get H {i} 0\nret 95\nend\n' for i in range(8)) 312 checks+= ''.join(f'if ne get H {8+i} {b}\nret 96\nend\n' 313 for i,b in enumerate((7).to_bytes(4,'little'))) 314 for label,src in [('count',aperture(count=0,length=0)),('fill',aperture(count=7))]: 315 b=compile_case('trace-aperture-'+label,src.replace('ret 0\nend\n',checks+'ret 0\nend\n'),'a64') 316 if b: 317 for expected,want in [(16,0),(17,1)]: 318 p=run([out/'trace',b,expected]) 319 record('trace aperture '+label+' expected-offset '+str(expected), 320 p.returncode,want,p.stderr.decode()) 321 p=run(['cc','-Wall','-Wextra','-Werror',root/'tests/kfd/fdtrace.c','-o',out/'fdtrace']) 322 record('fd trace oracle compile',p.returncode,0,p.stderr.decode()) 323 if not p.returncode: 324 size=`68721573888` 325 src=f'''text K "kfd-fd-oracle" 326 fn main to int 327 f = memfd K 0 len K 0 328 r = ftrunc int f {size} 329 h = kfdmap int f 0 {size} 330 if le h 0 331 ret 90 332 end 333 r = mapset h {size-8} 8 12345 334 if ne mapget h {size-8} 8 12345 335 ret 91 336 end 337 r = mapdrop h 338 ret 0 339 end 340 ''' 341 b=compile_case('fd-trace',src,'a64') 342 if b: 343 for mode in range(7): 344 p=run([out/'fdtrace',b,mode,size]) 345 record('fd trace synthetic mode '+str(mode),p.returncode,0,p.stderr.decode()) 346 else: 347 skip_msg = ( 348 'SKIP arm64-only tests/kfd/trace.c and tests/kfd/fdtrace.c: ' 349 'host is %s/%s (AArch64 ptrace oracle; not compiled, not passed)' 350 % (host_sys, host_arch) 351 ) 352 print(skip_msg) 353 rows.append(dict(name='arm64-only trace oracle', actual='SKIP', expected='SKIP', detail=skip_msg)) 354 355 # Faults are planted in independent compiler copies, never in the compiler under test. 356 # Each must compile and let precisely the formerly refused case through. 357 original=(root/'seed/omc/omc.om').read_text() 358 mutants=[ 359 ('request', [('ksize','fail 9\nret 0','ret 8')], 360 source('GET_VERSION',request=0),False), 361 ('size', [('kctl','kfdcmpv 0 n\nkfdtrap 1','kfdcmpv 0 n')], 362 source('GET_VERSION',size='bad').replace('fn main to int\n','fn main to int\nbad = ulong 7\n'),False), 363 ('address', [('kfdzero','kfdtrap 1','note planted missing address refusal')], 364 source('GET_PROCESS_APERTURES_NEW',put(0,1,8)),False), 365 ('kind', [('kbind','kfdcmpv 0 kind\nkfdtrap 1','kfdcmpv 0 kind')], 366 source('ALLOC_MEMORY_OF_GPU',setup_override='r = mapnew neg int 1 0 4096 3 34\n'+put(8,4096,8)),False), 367 ('function', [(f,'if eq ok 0\nfail 9','if eq ok 0\nnote planted missing mode refusal') 368 for f in ('hostval','xhost')], 369 'om2\nfn f to long\nret kfdres 4096\nend\n',True)] 370 mutants.extend([ 371 ('queue-object-type', [('kctl','kfdcmpv 0 2\nkfdtrap 1','kfdcmpv 0 2')], 372 source('CREATE_QUEUE',put(40,0),setup_override=qsetup(4096),suffix_override=' r q q long 0 long 0'),False), 373 ('queue-object-range', [('kbind','kfdtrap 8\nnote Subtract only after offset <= length; the existing extent check sees remaining bytes.','note Subtract only after offset <= length; the existing extent check sees remaining bytes.')], 374 source('CREATE_QUEUE',setup_override=qsetup(48),suffix_override=' r q q long 0 long 0'),False), 375 ('queue-object-extent', [('kbind','kfdtrap 8\nif eq get xtarget 0 0\nmstore 13 31 div off 8','if eq get xtarget 0 0\nmstore 13 31 div off 8')], 376 source('CREATE_QUEUE',setup_override=qsetup(2303),suffix_override=' r q q long 0 long 0'),False), 377 ('queue-object-v1-extent', [('kctl','shl ulong 2248 48','shl ulong 200 48'),('kctl','shl ulong 2176 48','shl ulong 128 48')], 378 source('CREATE_QUEUE',setup_override=qsetup(256),suffix_override=' r q q long 0 long 0'),False), 379 ('literal-size', [('kctl','if ne a_g 77 add x 11 n\nfail 9','if ne a_g 77 add x 11 n\nnote planted missing literal refusal'), 380 ('kctl','kfdcmpv 0 n\nkfdtrap 1','kfdcmpv 0 n')], 381 source('GET_VERSION',size=7),False), 382 ('map-fd', [('kmapfd','kchar mo','note planted missing fd class refusal'), 383 ('kmapfd','kfdcmpv 1 57856','kfdcmpv 1 0'), 384 ('kmapfd','kfdcmpv 0 128','kfdcmpv 0 0')], 385 regular,False), 386 ('sibling', [('ksize','fail 9\nret 0',f'if eq q {abi["DBG_TRAP"][0]}\nret {abi["DBG_TRAP"][1]}\nend\nfail 9\nret 0')], 387 source('GET_VERSION',size=abi['DBG_TRAP'][1],request=abi['DBG_TRAP'][0]),False)]) 388 mutants.extend([ 389 ('aperture-kind',[('kmspan','kfdcmpv 0 3\nkfdtrap 1','kfdcmpv 0 3')],aperture_bad['readonly'],False), 390 ('aperture-wrap',[('kmspan','kfdcmp 3 0\nkfdtrap 3','kfdcmp 3 0')],aperture_bad['wrap'],False), 391 ('aperture-extent',[('kmspan','kfdtrap 8','note planted missing mapping extent')],aperture_bad['extent'],False), 392 ('aperture-count',[('kspan','kfdcmp 0 2\nkfdtrap 8','kfdcmp 0 2')],aperture_bad['count'],False), 393 ('aperture-address',[('kfdzero','kfdtrap 1','note planted missing address refusal')],aperture_bad['address'],False), 394 ('aperture-generation',[ 395 ('mcheck','e3 `3942645760` 31 10 14\ntrapc 1 3','e3 `3942645760` 31 10 14'), 396 ('xhmcheck','xrr 57 10 9\nxtrap 1 3','xrr 57 10 9')], 397 aperture_bad['stale'],False), 398 ]) 399 for label, patches, case, function in mutants: 400 changed=original 401 for fn,old,new in patches: 402 start=changed.index('fn '+fn+' ') 403 stop=changed.find('\nfn ',start+1) 404 if stop<0: stop=len(changed) 405 body=changed[start:stop] 406 # Diagnostic site annotations may separate the semantic instructions. 407 # Still require exactly one complete seam; never silently skip a mutant. 408 seam = r'\n(?:note d2site [^\n]*\nput d2site 0 \d+\n)?' 409 pattern = seam.join(re.escape(line) for line in old.split('\n')) 410 matches = list(re.finditer(pattern, body)) 411 if len(matches)!=1: raise RuntimeError('mutation seam '+label+' '+fn) 412 hit = matches[0] 413 changed=changed[:start]+body[:hit.start()]+new+body[hit.end():]+changed[stop:] 414 m=out/('mutant-'+label) 415 m.with_suffix('.om').write_text(changed) 416 p=run([omc],changed.encode()) 417 record('build compiler mutant '+label,p.returncode,0,p.stderr.decode()) 418 if p.returncode: continue 419 m.write_bytes(p.stdout); m.chmod(0o755) 420 for target in ('a64','x86'): 421 b=compile_case('killed-'+label,case,target,compiler=m) 422 if not function: execute('killed-'+label,b,target) 423 424 (out/'results.json').write_text(json.dumps(rows,indent=2)+'\n') 425 print('compiler_sha256',hashlib.sha256(omc.read_bytes()).hexdigest()) 426 fail=sum(r['actual']!=r['expected'] for r in rows) 427 skipped=sum(r['actual']=='SKIP' for r in rows) 428 passed=sum(r['actual']==r['expected'] and r['actual']!='SKIP' for r in rows) 429 print('KFD_CPU',len(rows),'checks',passed,'passed',fail,'failures',skipped,'skipped; hardware NOT_RUN') 430 if not fail and '--record-history' in sys.argv[1:]: 431 # Measuring-instrument output, not a maintained copy of compiler facts. 432 ledger=root/'seed/omc/size-history.tsv' 433 digest=hashlib.sha256(omc.read_bytes()).hexdigest() 434 if not ledger.read_text().splitlines()[-1].endswith('\t'+digest): 435 lines=original.splitlines() 436 values=['kfd-uapi','KFD checked records and aligned reservations on Linux AArch64/x86', 437 len(lines),sum(s.startswith('fn ') for s in lines), 438 (omc.stat().st_size-176)//4,digest] 439 with ledger.open('a') as f: f.write('\t'.join(map(str,values))+'\n') 440 sys.exit(bool(fail))