"""Check the 1.1.0 climb/gear numeric, switch, full-width register and local-stack contracts. Synthetic checks need no game; --exefs also checks local original words and semantic anchors. Uses the shared differential harness CC selection, Windows/Linux suffixes and mappings, without fixed host paths. """ import argparse import math import os import struct import subprocess import sys import tempfile import zlib from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tests' / 'diff')) from common import BUF, BUF_SIZE, Emu, build_harness, compiler, diff_state, run_harness import patches from layout import layout from lifter import Lifter from module import Module NL = chr(10) CLIMB, GEAR_LOAD, GEAR_SCALE = 0x19279f0, 0x213eee4, 0x213f0d4 COMPARES = (0x213f10c, 0x213f174, 0x213f1b0) SITES = (CLIMB, GEAR_LOAD, GEAR_SCALE) def f32(x): return struct.unpack(' #include #include #include #include #include static _Alignas(16) unsigned char test_page[4096]; static int real_on, climb_on, high_on; static const char *cfg_get(const char *e) { if (!strcmp(e,"TOTK_REALTIME")) return real_on<0?NULL:real_on?"1":"0"; if (!strcmp(e,"TOTK_CLIMB_FIX")) return climb_on<0?NULL:climb_on?"1":"0"; if (!strcmp(e,"TOTK_HIGHFPS_FIX")) return high_on<0?NULL:high_on?"1":"0"; return NULL; } static const void *cfg_meta(const char *e) { return !strcmp(e,"TOTK_REALTIME") || !strcmp(e,"TOTK_CLIMB_FIX") || !strcmp(e,"TOTK_HIGHFPS_FIX") ? e : NULL; } static int cfg_bool(const char *e) { const char *v=cfg_get(e); return !v || strcmp(v,"0"); } static int plat_mem_commit(uint64_t a, size_t n) { return a==(uintptr_t)test_page && n==4096; } static void rt_fatal(const char *s, ...) { (void)s; abort(); } static void rt_log(const char *s, ...) { (void)s; } ''' macro = '#define RT_PATCH_COUNT %d' % len(table.sets) + NL macro += '#define RT_PATCH_PAGE ((uintptr_t)test_page)' + NL macro += '#define RT_PATCH_SETS(X) ' + ' '.join('X(%d,%s,"%s",%d)' % (i, *s) for i, s in enumerate(table.sets)) + NL frame_pre = '''static _Atomic uint64_t *step_word; static _Atomic uint32_t *step_interval; static double step_now; #define STEP_FIRST_S (1.0/60.0) static void frame_limit_live(void) {} ''' main = '''int main(void) { for (int i=-1;i<8;i++) { real_on=i<0?-1:!!(i&1); climb_on=i<0?-1:!!(i&2); high_on=i<0?-1:!!(i&4); memset(test_page,0x5a,sizeof test_page); step_word=NULL; step_interval=NULL; rt_patches_init(); frame_step_init(); assert(rt_patch_enabled(rt_patch_set("climb"))==((real_on!=0)&&(climb_on!=0))); assert(rt_patch_enabled(rt_patch_set("gears"))==(high_on!=0)); assert((step_word!=NULL)==(real_on!=0)); if (real_on==0) for (size_t j=0;jx[30]) { ' + lower(on) + ' } else { ' + lower(off) + ' }') programs.append((off, on)) args.work_dir.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(prefix='v110-highfps-', dir=args.work_dir) as temp: work = Path(temp) gating_check(work, table) exe = build_harness(work, bodies, 2, args.cflags) total = 0 raw_steps = (0.0, 1e-8, 0.125, 0.25, f32(0.5 - 2**-24), 0.5, f32(0.5 + 2**-23), 1.0, 2.0, 16.0) for raw in raw_steps: raw = f32(raw) mem = bytearray((i * 37 + 19) & 255 for i in range(BUF_SIZE)) struct.pack_into('> 64 for reg in (4, 8, 9)), 'Upper 64-bit SIMD sentinels must be nonzero' threshold = f32(f32(shipping(r) / 30) * raw) if enabled else f32(shipping(r) / 30) # Adjacent ULPs exercise greater-than, equal and less-than at all three branch thresholds. tb = bits(threshold) v[8] = (v[8] & ~0xffffffff) | (bits(0.5) if k == 0 else max(0, tb + edge)) sp, nzcv = BUF + BUF_SIZE - 0x100, ((total + len(cases)) % 16) << 28 code = programs[k][enabled] pc = base + spec[0] lo = pc & ~0xffff text = bytearray(pc - lo + 4 * len(code)) struct.pack_into('<' + 'I' * len(code), text, pc - lo, *code) emu = Emu([(lo, text), (table.page, page)]) emu.mu.mem_write(BUF, bytes(mem)) ex, esp, enz, epc, ev = emu.run(pc, 0, x, sp, nzcv, v, count=len(code)) allowed_x = {1} if GEAR_LOAD in spec else set() if len(spec) == 3: allowed_x |= {24} allowed_v = {0, 1} if k == 0 else {12} preservation(ex, esp, ev, x, sp, v, allowed_x, allowed_v) if len(spec) != 3: assert enz == nzcv touched = [] if enabled and k == 0: touched = [(sp - 96, x[8].to_bytes(8, 'little'))] touched += [(sp - 80 + i * 16, v[reg].to_bytes(16, 'little')) for i, reg in enumerate((5, 6, 7, 16, 17))] h = compensation(r, raw) for reg, want in ((0, max(shipping(r), h)), (1, max(1.5, h))): assert math.isclose(scalar(ev[reg]), want, rel_tol=2e-6, abs_tol=2e-7), (reg, r, raw, scalar(ev[reg]), want) assert ev[reg] >> 32 == 0 if r >= 1 and raw <= r: assert ev[0] & 0xffffffff == v[0] & 0xffffffff and ev[1] == bits(1.5), 'Shipping factor bits unchanged at 30 Hz and slower steps' if not enabled and k == 0: assert ev[0] == v[0] and ev[1] == bits(1.5) if enabled and GEAR_SCALE in spec: touched = [(sp - 16, v[4].to_bytes(16, 'little'))] want = f32(f32(scalar(v[0]) / scalar(v[4])) * raw) assert ev[12] == bits(want) if GEAR_LOAD in spec: assert ex[1] == BUF + 0x4000 if k == 1 and enabled: assert ev[12] == bits(raw) if len(spec) == 3: angle, limit = scalar(v[8]), scalar(ev[12]) # ARM64 b.le also accepts unordered (N != V), unlike Python NaN <= comparisons. want_branch = math.isnan(angle) or math.isnan(limit) or angle <= limit assert ex[24] == want_branch, (spec, enabled, raw, r, edge, ex[24], angle, limit, hex(enz)) want_mem = bytearray(mem) for address, data in touched: at = address - BUF want_mem[at:at + len(data)] = data actual_mem = bytes(emu.mu.mem_read(BUF, BUF_SIZE)) assert actual_mem == want_mem, 'Only exact save slots may change; all other memory, gaps and canaries must stay intact' assert actual_mem[sp - BUF - 112:sp - BUF - 96] == mem[sp - BUF - 112:sp - BUF - 96] assert actual_mem[sp - BUF:sp - BUF + 16] == mem[sp - BUF:sp - BUF + 16] cases.append((k, 0, x, sp, nzcv, v, True)) expected.append((ex, esp, enz, epc, ev, zlib.crc32(actual_mem))) page_path = work / 'page.bin' page_base = table.page & ~0xffff page_path.write_bytes(bytes(table.page - page_base) + page) records, err = run_harness(exe, work, mem, cases, [(page_base, page_path)]) assert not err and len(records) == len(cases), err for case, want, got in zip(cases, expected, records): delta = diff_state(got, want, False, True) assert not delta, (specs[case[0]], delta) total += len(cases) print(f'v110: {total} native/Unicorn cases passed; 6 contracts, on/off, 16 NZCV, full GPR/SIMD/SP/memory') return 0 if __name__ == '__main__': sys.exit(main())