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 / nvrm_alloc_check.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | """Host-only verification: retained C, exact state traces, source mutants, link negatives.""" |
| 4 | import hashlib |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import shlex |
| 8 | import subprocess |
| 9 | import tempfile |
| 10 | ROOT=Path(__file__).resolve().parents[1] |
| 11 | BRIDGE=Path(os.environ['OMEGA_BRIDGE']) |
| 12 | OMC=Path(os.environ['OMEGA_OMC']) |
| 13 | # Pins come from om/units.sh, the one place they are recorded (a copied toc.om pin went stale at the bridge repin). |
| 14 | UNITS_PIN={k:v for k,v in (l.split('=',1) for l in (ROOT/'om/units.sh').read_text().splitlines() if l.split('=',1)[0] in ('OMC_PIN','CONVERTER_PIN','FRONTEND_PIN'))} |
| 15 | for p,h in [(OMC,UNITS_PIN['OMC_PIN']),(BRIDGE/'src/toc.om',UNITS_PIN['CONVERTER_PIN']),(BRIDGE/'tools/bridge-seeded',UNITS_PIN['FRONTEND_PIN'])]: |
| 16 | if hashlib.sha256(p.read_bytes()).hexdigest()!=h: raise SystemExit('NOT_RUN unpinned input '+str(p)) |
| 17 | if Path('/dev/nvidiactl').exists(): raise SystemExit('REFUSED: isolate /dev') |
| 18 | # Default to the vendored 580.173.02 headers the Makefile builds against; NVRM_VENDOR still overrides. |
| 19 | VENDOR=Path(os.environ.get('NVRM_VENDOR',str(ROOT/'third_party/nvidia-open-580.173.02'))) |
| 20 | INCS=[ROOT/'src',ROOT/'nvrm']+[VENDOR/p for p in ('src/common/sdk/nvidia/inc','kernel-open/common/inc','kernel-open/nvidia-uvm','src/nvidia/arch/nvalloc/unix/include')] |
| 21 | CC=shlex.split(os.environ.get('CC','cc'))+['-std=gnu11','-D_GNU_SOURCE','-O2','-Wall','-Wextra','-Werror','-ffp-contract=off','-ffunction-sections','-fdata-sections','-Wl,--gc-sections']+['-I'+str(p) for p in INCS] |
| 22 | UNITS=['nvrm_va','nvrm_alloc_scalar','nvrm_counts','nvrm_alloc_push'] |
| 23 | SRCS=[ROOT/'src'/('cudapp_om_'+u+'.c') for u in UNITS]+[ROOT/'src/cudapp_om_nvrm_policy.c',ROOT/'src/omega_ampere_qmd.c',ROOT/'src/cudapp_om_amp_runtime.c',ROOT/'src/cudapp_om_amp_qmd.c'] |
| 24 | ENV=dict(os.environ,OMEGA_SEEDED_OMC=str(OMC)) |
| 25 | def run(cmd,**kw): return subprocess.run([str(c) for c in cmd],check=True,timeout=180,**kw) |
| 26 | def same(a,b): |
| 27 | with a.open('rb') as x,b.open('rb') as y: |
| 28 | while True: |
| 29 | c=x.read(65536);d=y.read(65536) |
| 30 | if c!=d:return False |
| 31 | if not c:return True |
| 32 | MUTANTS=[ |
| 33 | ('overlap-edge','nvrm_va','if gt na_end a sub i 1 va','if ge na_end a sub i 1 va'), |
| 34 | ('coalesce-next','nvrm_va','if eq vend get a mul add i 1 2','if ne vend get a mul add i 1 2'), |
| 35 | ('exact-fit','nvrm_va','if eq vend rend','if ne vend rend'), |
| 36 | ('split-tail','nvrm_va','put s 0 add n 1\nend\nend\nput s 3 va','put s 0 n\nend\nend\nput s 3 va'), |
| 37 | ('pool-reuse','nvrm_va','ret get a n','ret 0'), |
| 38 | ('counter-step','nvrm_alloc_scalar','ret add a 4294967296','ret add a 4294967295'), |
| 39 | ('method-mode','nvrm_alloc_scalar','if eq mode 1','if eq mode 2'), |
| 40 | ('packet-envelope','nvrm_alloc_push','if or ne shr h 28 2 eq cnt 0','if or ne shr h 28 3 eq cnt 0'), |
| 41 | ('decimal-digit','nvrm_counts','v = add mul v 10 d','v = add mul v 10 0'), |
| 42 | ('order-edge','nvrm_counts','if ge o get state 0','if gt o get state 0'), |
| 43 | ] |
| 44 | PATHS={ |
| 45 | 'nvrm_va':['native/nvrm/alloc_profile.om','native/nvrm/alloc.om'], |
| 46 | 'nvrm_alloc_scalar':['native/nvrm/policy.om','native/nvrm/alloc_scalar.om'], |
| 47 | 'nvrm_alloc_push':['native/nvrm/alloc_profile.om','om/encoder/amp_runtime.om','native/nvrm/alloc_push.om'], |
| 48 | 'nvrm_counts':['native/nvrm/counts.om'], |
| 49 | } |
| 50 | with tempfile.TemporaryDirectory(prefix='nvrm-alloc-check-') as tmp: |
| 51 | tmp=Path(tmp); oracle=tmp/'oracle'; normal=tmp/'normal' |
| 52 | driver=ROOT/'tests/nvrm_alloc_test.c'; counter=ROOT/'tests/nvrm_counts_test.c' |
| 53 | run(CC+['-DCUDAPP_MIGRATION_PENDING_C_NVRM_ALLOC=1',driver]+SRCS+['-o',oracle]) |
| 54 | run(CC+[driver]+SRCS+['-o',normal]) |
| 55 | ctrace=tmp/'c.trace';otrace=tmp/'om.trace' |
| 56 | with ctrace.open('wb') as f:run([oracle],stdout=f) |
| 57 | with otrace.open('wb') as f:run([normal],stdout=f) |
| 58 | if not same(ctrace,otrace):raise SystemExit('FAIL C/OMEGA allocator/packet/method/slot state differential') |
| 59 | print('NVRM ALLOC DIFFERENTIAL PASS (32768 randomized operations plus source boundaries; exact active state)',flush=True) |
| 60 | with otrace.open('wb') as f:run([normal,'--red'],stdout=f) |
| 61 | if same(ctrace,otrace):raise SystemExit('FAIL red trace accepted') |
| 62 | print('NVRM ALLOC RED CONTROL KILLED',flush=True) |
| 63 | counts=tmp/'counts';run(CC+[counter,SRCS[2],'-o',counts]);run([counts]) |
| 64 | # The driver and counter executable jointly require every strong export. |
| 65 | for unit in UNITS: |
| 66 | target=counter if unit=='nvrm_counts' else driver |
| 67 | sources=[p for p in SRCS if p.name!='cudapp_om_'+unit+'.c'] |
| 68 | r=subprocess.run([str(c) for c in CC+[target]+sources+['-o',tmp/'missing']],stdout=subprocess.PIPE,stderr=subprocess.PIPE,timeout=180) |
| 69 | if r.returncode==0 or ('omega_'+unit).encode() not in r.stderr:raise SystemExit('FAIL missing-unit negative '+unit) |
| 70 | print('NVRM ALLOC STRONG LINK NEGATIVE PASS '+unit,flush=True) |
| 71 | for name,unit,before,after in MUTANTS: |
| 72 | src='\n'.join((ROOT/p).read_text() for p in PATHS[unit])+'\n' |
| 73 | if src.count(before)!=1:raise SystemExit('FAIL mutant site count '+name) |
| 74 | om=tmp/(name+'.om');om.write_text(src.replace(before,after));generated=tmp/(name+'.c') |
| 75 | cmd=['sh',BRIDGE/'tools/bridge-seeded']+([] if unit=='nvrm_alloc_scalar' else ['--runtime-views'])+['c',om] |
| 76 | with generated.open('wb') as f:run(cmd,env=ENV,stdout=f) |
| 77 | exe=tmp/name |
| 78 | target=counter if unit=='nvrm_counts' else driver |
| 79 | sources=[generated if p.name=='cudapp_om_'+unit+'.c' else p for p in SRCS] |
| 80 | run(CC+[target]+sources+['-o',exe]) |
| 81 | with otrace.open('wb') as f:r=subprocess.run([str(exe)],stdout=f,stderr=subprocess.PIPE,timeout=180) |
| 82 | if unit=='nvrm_counts': |
| 83 | killed=r.returncode!=0 and b'Assertion' in r.stderr |
| 84 | else:killed=(r.returncode==0 and not same(ctrace,otrace)) or (r.returncode!=0 and b'Assertion' in r.stderr) |
| 85 | if not killed:raise SystemExit('FAIL semantic mutant not killed '+name+' rc='+str(r.returncode)) |
| 86 | print('NVRM ALLOC MUTANT KILLED '+name,flush=True) |
| 87 | print('NVRM ALLOC MIGRATION PASS (10 compiled semantic mutants; HOST_ONLY)') |