"""Compile only our included original C, inspect it, compare a hypothesis.""" import hashlib, json, os, platform, random, re, shutil, subprocess, tempfile from pathlib import Path from recovered import award_points root = Path(__file__).resolve().parent def run(*args, ok=True): result = subprocess.run(list(map(str, args)), capture_output=True, text=True, timeout=15) if ok and result.returncode: raise RuntimeError(result.stderr) return result def main(): if platform.system() != 'Linux' or platform.machine() != 'x86_64': raise SystemExit('This native exercise expects Linux x86_64; other targets need their own acceptance.') tools = ['cc','objdump','strip','readelf','nm','strings'] for name in tools: if not shutil.which(name): raise SystemExit('Missing tool: '+name) versions = {name: run(name, '--version').stdout.splitlines()[0] for name in tools} fixed = [(0,0,0),(1,0,0),(2,0,0),(3,0,0),(2,2,0),(2,3,0),(2,4,0),(2,3,4),(99,3,0),(100,3,0),(999,999,999)] rng = random.Random(20261010) cases = fixed + [tuple(rng.randint(0,999) for _ in range(3)) for _ in range(100)] artifacts = {} with tempfile.TemporaryDirectory(prefix='forge-reverse-lab-') as temporary: folder=Path(temporary) for optimization in ['0','2']: binary=folder/('score-O'+optimization) run('cc','-std=c11','-Wall','-Wextra','-Werror','-g','-O'+optimization,root/'score_lab.c','-o',binary) header=run('readelf','-h',binary).stdout assert 'ELF64' in header and 'X86-64' in header.upper() symbols=run('nm',binary).stdout;assert 'award_points' in symbols assembly=run('objdump','-d','-M','intel','--disassemble=award_points',binary).stdout assert ':' in assembly stripped=folder/('score-O'+optimization+'-stripped') shutil.copy2(binary,stripped);run('strip','--strip-all',stripped) assert 'award_points' not in run('nm',stripped,ok=False).stdout assert 'Forge practice score:' in run('strings',stripped).stdout stripped_assembly=run('objdump','-d','-M','intel',stripped).stdout assert 'Disassembly of section .text:' in stripped_assembly for executable in [binary,stripped]: for values in cases: observed=run(executable,*values).stdout.strip() assert observed == 'Forge practice score: '+str(award_points(*values)), (values,observed) for invalid in [[],['-1','0','0'],['1000','0','0'],['x','0','0'],['1','2'],['1','2','3','4']]: assert run(executable,*invalid,ok=False).returncode==2 artifacts['O'+optimization]={'namedFunctionAssembly':assembly,'strippedStillDisassembles':True,'strippedLosesOwnSymbol':True,'originalAndStrippedBehaviorMatches':len(cases)} result={'result':'passed','scope':'Actual original-source compilation, ELF/symbol/string/disassembly/strip inspection and native execution versus original Python hypothesis; no third-party game, Ghidra UI or browser acceptance.','platform':platform.platform(),'versions':versions,'fixedObservations':[{'inputs':list(v),'score':award_points(*v)} for v in fixed],'inputsPerBinary':len(cases),'binaries':4,'behaviorComparisons':len(cases)*4,'invalidChecks':24,'artifacts':artifacts,'sourceSha256':{name:hashlib.sha256((root/name).read_bytes()).hexdigest() for name in ['score_lab.c','recovered.py','check.py']}} print(json.dumps(result,indent=2)) if __name__ == '__main__': main()