From f3e8efd53d6784e1169340e42244e437964b0e5c Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Mon, 20 Jul 2026 05:30:40 -0500 Subject: [PATCH] rebuild iggy's durango x64 atomic cmpxchg primitives --- .../src/reconstructed/CMakeLists.txt | 11 + .../src/reconstructed/durango_rrAtomics_x64.c | 104 ++++++++ .../src/reconstructed/durango_rrAtomics_x64.h | 36 +++ iggy-decompiled/src/reconstructed/modules.tsv | 1 + .../durango_rrAtomics_x64_test_support.c | 11 + ...est_reconstructed_durango_rrAtomics_x64.py | 252 ++++++++++++++++++ ...constructed_durango_rrAtomics_x64_cross.py | 250 +++++++++++++++++ 7 files changed, 665 insertions(+) create mode 100644 iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.c create mode 100644 iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.h create mode 100644 tests/support/durango_rrAtomics_x64_test_support.c create mode 100644 tests/test_reconstructed_durango_rrAtomics_x64.py create mode 100644 tests/test_reconstructed_durango_rrAtomics_x64_cross.py diff --git a/iggy-decompiled/src/reconstructed/CMakeLists.txt b/iggy-decompiled/src/reconstructed/CMakeLists.txt index fbd5fde..0366b5e 100644 --- a/iggy-decompiled/src/reconstructed/CMakeLists.txt +++ b/iggy-decompiled/src/reconstructed/CMakeLists.txt @@ -657,6 +657,17 @@ target_compile_definitions( ) configure_iggy_reconstructed_target(iggy_reconstructed_durango_rrCpu_durango c_std_11) +# durango_rrAtomics_x64 -- the durango-only x64 inline-asm atomic compare-exchange +# primitives (asm_x64_cmpxchg64 -> LOCK CMPXCHGQ, asm_x64_cmpxchg128 -> LOCK +# CMPXCHG16B). 2 exports, durango-only, owns no data global and imports nothing +# (a pure leaf). Reproduced instruction-exact from the retail object. +add_library(iggy_reconstructed_durango_rrAtomics_x64_durango STATIC durango_rrAtomics_x64.c) +target_compile_definitions( + iggy_reconstructed_durango_rrAtomics_x64_durango + PRIVATE IGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO=1 +) +configure_iggy_reconstructed_target(iggy_reconstructed_durango_rrAtomics_x64_durango c_std_11) + add_library(iggy_reconstructed_win32_rrTime_durango STATIC win32_rrTime.c) target_compile_definitions( iggy_reconstructed_win32_rrTime_durango diff --git a/iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.c b/iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.c new file mode 100644 index 0000000..059887f --- /dev/null +++ b/iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.c @@ -0,0 +1,104 @@ +/* Reconstructed Durango durango_rrAtomics_x64 translation unit. */ +/* c:\\projects\\g\\build\\release\\temps\\libdu\\iggy_durango\\durango_rrAtomics_x64.obj */ + +#include "durango_rrAtomics_x64.h" + +#if !defined(IGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO) +#error "select the Iggy durango_rrAtomics_x64 Durango target" +#endif + +/* + * Retail 0x2200: + * movq (%rdx), %rax ; rax = *expected_inout (comparand) + * lock cmpxchgq %r8, (%rcx) ; if rax == *dest -> *dest = new, ZF=1 + * ; else rax = *dest, ZF=0 + * sete %cl ; cl = success + * movq %rax, (%rdx) ; *expected_inout = old value + * movzbq %cl, %rax ; return success + * __atomic_compare_exchange_n lowers to exactly this LOCK CMPXCHGQ and, on + * failure, writes the current destination value back into *expected_inout; + * on success it leaves *expected_inout unchanged (already equal to old). + */ +bool asm_x64_cmpxchg64(int64_t *destination, int64_t *expected_inout, + int64_t exchange) { + return __atomic_compare_exchange_n(destination, expected_inout, exchange, + false, __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); +} + +#if defined(__x86_64__) + +/* + * Retail 0x2218: + * movq 0x8(%rsi), %rdx / movq (%rsi), %rax ; RDX:RAX = expected hi:lo + * movq 0x8(%r8), %rcx / movq (%r8), %rbx ; RCX:RBX = new hi:lo + * lock cmpxchg16b (%rdi) ; compare RDX:RAX with *dest + * jne fail ; ZF=0 -> mismatch + * movq $1, %rax; ret ; success, no writeback + * fail: + * movq %rdx, 0x8(%rsi) / movq %rax, (%rsi) ; *expected = old hi:lo + * movq $0, %rax; ret ; failure + * cmpxchg16b takes the comparand in RDX:RAX and the replacement in RCX:RBX, + * and on mismatch leaves the current 128-bit destination value in RDX:RAX. + */ +bool asm_x64_cmpxchg128(void *destination, uint64_t *expected_inout, + const uint64_t *exchange) { + unsigned __int128 *dest = (unsigned __int128 *)destination; + uint64_t expected_lo = expected_inout[0]; + uint64_t expected_hi = expected_inout[1]; + uint64_t exchange_lo = exchange[0]; + uint64_t exchange_hi = exchange[1]; + bool success; + + __asm__ volatile("lock cmpxchg16b %[mem]" + : "=@ccz"(success), [mem] "+m"(*dest), "+a"(expected_lo), + "+d"(expected_hi) + : "b"(exchange_lo), "c"(exchange_hi) + : "cc"); + + if (!success) { + expected_inout[0] = expected_lo; + expected_inout[1] = expected_hi; + } + return success; +} + +#else + +/* + * Host build fallback (no x86 CMPXCHG16B available). No retail object exists + * for this target; this lock-based emulation reproduces the retail's + * observable contract so the runtime harness can exercise the semantics. + */ + +static unsigned char rr_cmpxchg128_lock; + +static void rr_cmpxchg128_lock_acquire(void) { + while (__atomic_test_and_set(&rr_cmpxchg128_lock, __ATOMIC_ACQUIRE)) { + } +} + +static void rr_cmpxchg128_lock_release(void) { + __atomic_clear(&rr_cmpxchg128_lock, __ATOMIC_RELEASE); +} + +bool asm_x64_cmpxchg128(void *destination, uint64_t *expected_inout, + const uint64_t *exchange) { + uint64_t *dest = (uint64_t *)destination; + bool success; + + rr_cmpxchg128_lock_acquire(); + if (dest[0] == expected_inout[0] && dest[1] == expected_inout[1]) { + dest[0] = exchange[0]; + dest[1] = exchange[1]; + success = true; + } else { + expected_inout[0] = dest[0]; + expected_inout[1] = dest[1]; + success = false; + } + rr_cmpxchg128_lock_release(); + return success; +} + +#endif diff --git a/iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.h b/iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.h new file mode 100644 index 0000000..f1400c4 --- /dev/null +++ b/iggy-decompiled/src/reconstructed/durango_rrAtomics_x64.h @@ -0,0 +1,36 @@ +#ifndef IGGY_RECONSTRUCTED_DURANGO_RRATOMICS_X64_H +#define IGGY_RECONSTRUCTED_DURANGO_RRATOMICS_X64_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * asm_x64_cmpxchg64 (retail 0x2200): LOCK CMPXCHG primitive. + * *expected_inout is the comparand; if it equals *destination the store + * installs `exchange` and the call returns true, otherwise the current + * *destination value is written back into *expected_inout and it returns + * false. On success *expected_inout is left unchanged (it already matched). + */ +bool asm_x64_cmpxchg64(int64_t *destination, int64_t *expected_inout, + int64_t exchange); + +/* + * asm_x64_cmpxchg128 (retail 0x2218): LOCK CMPXCHG16B primitive. + * expected_inout / exchange point at two contiguous 64-bit lanes (low then + * high). On success (128-bit comparand matched) it installs `exchange` and + * returns true without touching expected_inout; on failure it writes the + * current 128-bit destination value back into expected_inout and returns + * false. `destination` must be 16-byte aligned. + */ +bool asm_x64_cmpxchg128(void *destination, uint64_t *expected_inout, + const uint64_t *exchange); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/iggy-decompiled/src/reconstructed/modules.tsv b/iggy-decompiled/src/reconstructed/modules.tsv index 08e6f7f..9d1039f 100644 --- a/iggy-decompiled/src/reconstructed/modules.tsv +++ b/iggy-decompiled/src/reconstructed/modules.tsv @@ -197,3 +197,4 @@ swf_as3vm_flash_display 580 537 durango complete swf_as3vm_flash_display 553 537 orbis complete swf_as3vm_flash_display 561 537 ps3 complete swf_as3vm_flash_display 563 537 vita complete +durango_rrAtomics_x64 2 2 durango complete diff --git a/tests/support/durango_rrAtomics_x64_test_support.c b/tests/support/durango_rrAtomics_x64_test_support.c new file mode 100644 index 0000000..bbb2c94 --- /dev/null +++ b/tests/support/durango_rrAtomics_x64_test_support.c @@ -0,0 +1,11 @@ +/* + * Host harness for the reconstructed durango_rrAtomics_x64 translation unit. + * + * `llvm-nm --undefined-only` on the retail Durango object returns zero + * symbols: the two LOCK CMPXCHG / CMPXCHG16B primitives are a pure leaf with + * no imports, no globals, and no callouts to host APIs or other modules. + * There is nothing to stub, so this TU exists only to keep the host dylib + * build command symmetric with the other reconstructed modules' harnesses. + */ + +typedef int durango_rrAtomics_x64_test_support_is_intentionally_empty; diff --git a/tests/test_reconstructed_durango_rrAtomics_x64.py b/tests/test_reconstructed_durango_rrAtomics_x64.py new file mode 100644 index 0000000..a86f573 --- /dev/null +++ b/tests/test_reconstructed_durango_rrAtomics_x64.py @@ -0,0 +1,252 @@ +"""Runtime tests for the reconstructed durango_rrAtomics_x64 translation unit. + +RED STATE: durango_rrAtomics_x64.c / durango_rrAtomics_x64.h do not exist yet, +so setUpClass raises "missing durango_rrAtomics_x64 runtime inputs". Every +behavioral assertion below is grounded in +iggy-decompiled/raw/durango/durango_rrAtomics_x64.c and the retail object +disassembly. + +durango_rrAtomics_x64 is RAD's Durango-only inline-assembly atomic +compare-exchange layer: exactly two exports, both pure leaves with no imports +and no globals. + + asm_x64_cmpxchg64 (retail 0x2200, LOCK CMPXCHGQ): + rax = *expected_inout + lock cmpxchgq exchange, *destination ; if rax == *dest -> store, ZF=1 + sete success; *expected_inout = rax; return success + -> success when *expected_inout == *destination: installs `exchange`, + returns true, leaves *expected_inout unchanged. + -> failure otherwise: writes the current *destination into + *expected_inout, returns false, destination untouched. + + asm_x64_cmpxchg128 (retail 0x2218, LOCK CMPXCHG16B): + RDX:RAX = expected hi:lo, RCX:RBX = exchange hi:lo + lock cmpxchg16b (destination) + jne fail; return 1 ; success, no writeback + fail: *expected_inout = old hi:lo; return 0 + -> success when the full 128-bit *expected_inout == *destination: installs + `exchange`, returns true, leaves expected_inout untouched. + -> failure otherwise: writes the current 128-bit *destination back into + expected_inout (both lanes), returns false, destination untouched. + +Durango-only atomic module. The dylib is built for the 64-bit host with +IGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO selected. The retail object is +a pure leaf with zero undefined symbols, so the support harness has no stubs. +On x86_64 the 128-bit path lowers to LOCK CMPXCHG16B; on the (arm64) host it +uses a lock-based emulation with identical observable semantics. +""" + +import ctypes +from pathlib import Path +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed" +SOURCE = RECONSTRUCTED / "durango_rrAtomics_x64.c" +HEADER = RECONSTRUCTED / "durango_rrAtomics_x64.h" +SUPPORT = ROOT / "tests/support/durango_rrAtomics_x64_test_support.c" + + +def u128(low, high): + """Pack two 64-bit lanes (low then high) into a Python int.""" + return (high << 64) | low + + +class ReconstructedDurangoRrAtomicsX64Tests(unittest.TestCase): + @classmethod + def setUpClass(cls): + missing = [str(path) for path in (SOURCE, HEADER, SUPPORT) + if not path.exists()] + if missing: + raise AssertionError( + "missing durango_rrAtomics_x64 runtime inputs: " + + ", ".join(missing)) + cls.temp = tempfile.TemporaryDirectory() + output = Path(cls.temp.name) / "durango_rrAtomics_x64.dylib" + build = subprocess.run([ + "clang", "-dynamiclib", "-std=c11", "-O2", "-Wall", "-Wextra", + "-Werror", "-Wl,-undefined,dynamic_lookup", + "-DIGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO=1", + "-I", str(RECONSTRUCTED), + str(SOURCE), str(SUPPORT), "-o", str(output), + ], capture_output=True, text=True) + if build.returncode: + raise AssertionError(build.stdout + build.stderr) + cls.lib = ctypes.CDLL(str(output)) + lib = cls.lib + + # asm_x64_cmpxchg64(int64* dest, int64* expected_inout, int64 exch) + # -> bool (raw:6). + lib.asm_x64_cmpxchg64.argtypes = [ + ctypes.POINTER(ctypes.c_int64), + ctypes.POINTER(ctypes.c_int64), ctypes.c_int64] + lib.asm_x64_cmpxchg64.restype = ctypes.c_bool + + # asm_x64_cmpxchg128(void* dest, uint64* expected_inout, + # const uint64* exchange) -> bool (raw:29). + lib.asm_x64_cmpxchg128.argtypes = [ + ctypes.c_void_p, + ctypes.POINTER(ctypes.c_uint64), + ctypes.POINTER(ctypes.c_uint64)] + lib.asm_x64_cmpxchg128.restype = ctypes.c_bool + + @classmethod + def tearDownClass(cls): + cls.temp.cleanup() + + # ---- asm_x64_cmpxchg64 ------------------------------------------------- + + def test_cmpxchg64_success_installs_exchange(self): + # raw:11-21. Comparand matches destination: store the new value, + # return true, and leave expected_inout unchanged. + dest = ctypes.c_int64(0x1122334455667788) + expected = ctypes.c_int64(0x1122334455667788) + result = self.lib.asm_x64_cmpxchg64( + ctypes.byref(dest), ctypes.byref(expected), + ctypes.c_int64(-0x0102030405060708)) + self.assertTrue(result) + self.assertEqual(dest.value, -0x0102030405060708) + self.assertEqual(expected.value, 0x1122334455667788) + + def test_cmpxchg64_failure_writes_old_value_back(self): + # raw:11-21. Comparand mismatches: destination is untouched, the + # current destination value is written into expected_inout, and the + # call returns false. + dest = ctypes.c_int64(0x00DEADBEEF001234) + expected = ctypes.c_int64(0x0BADC0DE0BADC0DE) + result = self.lib.asm_x64_cmpxchg64( + ctypes.byref(dest), ctypes.byref(expected), + ctypes.c_int64(0x7777777777777777)) + self.assertFalse(result) + self.assertEqual(dest.value, 0x00DEADBEEF001234) + self.assertEqual(expected.value, 0x00DEADBEEF001234) + + def test_cmpxchg64_zero_comparand_matches_zero_destination(self): + # A zero comparand still succeeds against a zero destination; pins the + # equality test rather than a truthiness test. + dest = ctypes.c_int64(0) + expected = ctypes.c_int64(0) + result = self.lib.asm_x64_cmpxchg64( + ctypes.byref(dest), ctypes.byref(expected), ctypes.c_int64(42)) + self.assertTrue(result) + self.assertEqual(dest.value, 42) + self.assertEqual(expected.value, 0) + + def test_cmpxchg64_spinlock_style_retry_converges(self): + # Model a caller's CAS retry loop: on failure expected_inout is + # refreshed with the observed value, so the second attempt succeeds. + dest = ctypes.c_int64(100) + expected = ctypes.c_int64(99) # stale + first = self.lib.asm_x64_cmpxchg64( + ctypes.byref(dest), ctypes.byref(expected), ctypes.c_int64(200)) + self.assertFalse(first) + self.assertEqual(expected.value, 100) # refreshed + second = self.lib.asm_x64_cmpxchg64( + ctypes.byref(dest), ctypes.byref(expected), ctypes.c_int64(200)) + self.assertTrue(second) + self.assertEqual(dest.value, 200) + + # ---- asm_x64_cmpxchg128 ------------------------------------------------ + + def _make128(self, low, high): + return (ctypes.c_uint64 * 2)(low, high) + + def test_cmpxchg128_success_installs_both_lanes(self): + # raw:29-52. Full 128-bit comparand matches: install exchange in both + # lanes, return true, leave expected_inout untouched. + dest = self._make128(0x1111111111111111, 0x2222222222222222) + expected = self._make128(0x1111111111111111, 0x2222222222222222) + exchange = self._make128(0xAAAAAAAAAAAAAAAA, 0xBBBBBBBBBBBBBBBB) + result = self.lib.asm_x64_cmpxchg128( + ctypes.cast(dest, ctypes.c_void_p), + ctypes.cast(expected, ctypes.POINTER(ctypes.c_uint64)), + ctypes.cast(exchange, ctypes.POINTER(ctypes.c_uint64))) + self.assertTrue(result) + self.assertEqual(u128(dest[0], dest[1]), + u128(0xAAAAAAAAAAAAAAAA, 0xBBBBBBBBBBBBBBBB)) + self.assertEqual(u128(expected[0], expected[1]), + u128(0x1111111111111111, 0x2222222222222222)) + + def test_cmpxchg128_failure_writes_old_value_back(self): + # raw:29-56. Mismatch: destination untouched, both old lanes written + # into expected_inout, return false. + dest = self._make128(0xDEADDEADDEADDEAD, 0xBEEFBEEFBEEFBEEF) + expected = self._make128(0x0000000000000000, 0x0000000000000000) + exchange = self._make128(0xCCCCCCCCCCCCCCCC, 0xDDDDDDDDDDDDDDDD) + result = self.lib.asm_x64_cmpxchg128( + ctypes.cast(dest, ctypes.c_void_p), + ctypes.cast(expected, ctypes.POINTER(ctypes.c_uint64)), + ctypes.cast(exchange, ctypes.POINTER(ctypes.c_uint64))) + self.assertFalse(result) + self.assertEqual(u128(dest[0], dest[1]), + u128(0xDEADDEADDEADDEAD, 0xBEEFBEEFBEEFBEEF)) + self.assertEqual(u128(expected[0], expected[1]), + u128(0xDEADDEADDEADDEAD, 0xBEEFBEEFBEEFBEEF)) + + def test_cmpxchg128_high_lane_mismatch_still_fails(self): + # Only the high lane differs: the 128-bit comparison must reject it, + # proving both lanes participate (not just the low qword). + dest = self._make128(0x0123456789ABCDEF, 0x1111111111111111) + expected = self._make128(0x0123456789ABCDEF, 0x2222222222222222) + exchange = self._make128(0x9999999999999999, 0x8888888888888888) + result = self.lib.asm_x64_cmpxchg128( + ctypes.cast(dest, ctypes.c_void_p), + ctypes.cast(expected, ctypes.POINTER(ctypes.c_uint64)), + ctypes.cast(exchange, ctypes.POINTER(ctypes.c_uint64))) + self.assertFalse(result) + self.assertEqual(u128(dest[0], dest[1]), + u128(0x0123456789ABCDEF, 0x1111111111111111)) + # expected refreshed with the actual destination value. + self.assertEqual(u128(expected[0], expected[1]), + u128(0x0123456789ABCDEF, 0x1111111111111111)) + + def test_cmpxchg128_low_lane_mismatch_still_fails(self): + # Symmetric check: only the low lane differs. + dest = self._make128(0x1111111111111111, 0x0123456789ABCDEF) + expected = self._make128(0x2222222222222222, 0x0123456789ABCDEF) + exchange = self._make128(0x5555555555555555, 0x6666666666666666) + result = self.lib.asm_x64_cmpxchg128( + ctypes.cast(dest, ctypes.c_void_p), + ctypes.cast(expected, ctypes.POINTER(ctypes.c_uint64)), + ctypes.cast(exchange, ctypes.POINTER(ctypes.c_uint64))) + self.assertFalse(result) + self.assertEqual(u128(expected[0], expected[1]), + u128(0x1111111111111111, 0x0123456789ABCDEF)) + + def test_cmpxchg128_zero_comparand_matches_zero_destination(self): + dest = self._make128(0, 0) + expected = self._make128(0, 0) + exchange = self._make128(1, 2) + result = self.lib.asm_x64_cmpxchg128( + ctypes.cast(dest, ctypes.c_void_p), + ctypes.cast(expected, ctypes.POINTER(ctypes.c_uint64)), + ctypes.cast(exchange, ctypes.POINTER(ctypes.c_uint64))) + self.assertTrue(result) + self.assertEqual(u128(dest[0], dest[1]), u128(1, 2)) + self.assertEqual(u128(expected[0], expected[1]), 0) + + def test_cmpxchg128_spinlock_style_retry_converges(self): + # Failure refreshes expected with the observed value, so a retry with + # the same exchange succeeds. + dest = self._make128(0xF00D, 0xCAFE) + expected = self._make128(0, 0) # stale + exchange = self._make128(0x1234, 0x5678) + first = self.lib.asm_x64_cmpxchg128( + ctypes.cast(dest, ctypes.c_void_p), + ctypes.cast(expected, ctypes.POINTER(ctypes.c_uint64)), + ctypes.cast(exchange, ctypes.POINTER(ctypes.c_uint64))) + self.assertFalse(first) + self.assertEqual(u128(expected[0], expected[1]), u128(0xF00D, 0xCAFE)) + second = self.lib.asm_x64_cmpxchg128( + ctypes.cast(dest, ctypes.c_void_p), + ctypes.cast(expected, ctypes.POINTER(ctypes.c_uint64)), + ctypes.cast(exchange, ctypes.POINTER(ctypes.c_uint64))) + self.assertTrue(second) + self.assertEqual(u128(dest[0], dest[1]), u128(0x1234, 0x5678)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_reconstructed_durango_rrAtomics_x64_cross.py b/tests/test_reconstructed_durango_rrAtomics_x64_cross.py new file mode 100644 index 0000000..8b0f0ee --- /dev/null +++ b/tests/test_reconstructed_durango_rrAtomics_x64_cross.py @@ -0,0 +1,250 @@ +"""Durango-only contract tests for reconstructed durango_rrAtomics_x64. + +This is a single-platform x86_64-pc-windows-msvc translation unit: +durango_rrAtomics_x64 appears only in iggy_durango.modules.tsv and has no +orbis/ps3/vita source or object inventory entry. (It is also emitted into the +durango "perfmon" build variant with byte-identical content; that is the same +Durango target, not a second platform.) + +RED until durango_rrAtomics_x64.c / durango_rrAtomics_x64.h (and their CMake / +modules.tsv rows) exist; retail-evidence assertions run now and prove the +nm/strings parsing independently. + +How EXPECTED_* were derived from +work/iggy-native-decomp/durango-objects/durango_rrAtomics_x64.obj: + + EXPECTED_FUNCTIONS = `llvm-nm --extern-only --defined-only ` names + tagged uppercase T: exactly 2 exports + (asm_x64_cmpxchg64 @0x2200, asm_x64_cmpxchg128 @0x2218). + EXPECTED_GLOBALS = empty: no owned external D/B/C symbols. The only + other symbols emitted are @comp.id / @feat.00, which + are absolute (kind "a") MSVC metadata, not globals. + EXPECTED_IMPORTS = empty: `llvm-nm --undefined-only ` emits zero + lines. These LOCK CMPXCHG / CMPXCHG16B primitives are a + pure leaf with no toolchain scaffolding imports. + RUNTIME_ALLOWLISTS = empty for durango because total undefined count is 0. + +The provenance string is the object's embedded build-temp path, matching the +original_object column in iggy_durango.modules.tsv. + +Macro convention for the future .c (not authored here): + IGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO, using the single-platform + #error idiom from durango_rrCpu.c and win32_rrAtomics.c. +""" + +from pathlib import Path +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] +RECONSTRUCTED = ROOT / "iggy-decompiled/src/reconstructed" +SOURCE = RECONSTRUCTED / "durango_rrAtomics_x64.c" +HEADER = RECONSTRUCTED / "durango_rrAtomics_x64.h" +LLVM_NM = Path("/opt/homebrew/opt/llvm/bin/llvm-nm") +RETAIL_OBJ = ( + ROOT + / "work/iggy-native-decomp/durango-objects/durango_rrAtomics_x64.obj") + +EXPECTED_FUNCTIONS = { + "asm_x64_cmpxchg64", + "asm_x64_cmpxchg128", +} +EXPECTED_GLOBALS = set() +EXPECTED_IMPORTS = set() +RUNTIME_ALLOWLISTS = {"durango": set()} + +PROVENANCE = ( + r"c:\projects\g\build\release\temps\libdu\iggy_durango" + r"\durango_rrAtomics_x64.obj") + + +def normalized_symbol(name): + name = name.removeprefix(".") + name = name.removeprefix("__imp_") + if name == "_setjmp": + return "setjmp" + return name + + +def defined_symbols_by_kind(output): + functions = set() + globals_ = set() + for line in output.splitlines(): + parts = line.split() + if len(parts) < 2: + continue + kind = parts[-2].upper() + name = normalized_symbol(parts[-1]) + if kind == "T": + functions.add(name) + elif kind in {"D", "B", "C"}: + globals_.add(name) + return functions, globals_ + + +class ReconstructedDurangoRrAtomicsX64CrossTests(unittest.TestCase): + def test_native_provenance_counts_source_path_and_inventory(self): + # source_functions=2 and lines=64 from the durango inventory; + # `wc -l` independently gives 64. Category is "platform". + modules = ROOT / "iggy-decompiled/symbols/iggy_durango.modules.tsv" + row = next( + line for line in modules.read_text(encoding="utf-8").splitlines() + if line.startswith("durango_rrAtomics_x64\t")) + cols = row.split("\t") + self.assertEqual(cols[1], "platform") + self.assertEqual(int(cols[2]), 2) + self.assertEqual(int(cols[3]), 64) + self.assertEqual(cols[4], PROVENANCE) + + raw = ROOT / "iggy-decompiled/raw/durango/durango_rrAtomics_x64.c" + self.assertEqual( + len(raw.read_text(encoding="utf-8").splitlines()), 64) + + self.assertTrue(RETAIL_OBJ.exists(), f"missing {RETAIL_OBJ}") + symbols = subprocess.run( + ["nm", "-g", str(RETAIL_OBJ)], capture_output=True, text=True) + self.assertEqual(symbols.returncode, 0, symbols.stderr) + normalized = symbols.stdout.replace(" .", " ") + for symbol in EXPECTED_FUNCTIONS | EXPECTED_GLOBALS: + self.assertIn(symbol, normalized, symbol) + + if LLVM_NM.exists(): + undefined = subprocess.run( + [str(LLVM_NM), "--undefined-only", str(RETAIL_OBJ)], + capture_output=True, text=True) + self.assertEqual(undefined.returncode, 0, undefined.stderr) + imports = { + normalized_symbol(line.split()[-1]) + for line in undefined.stdout.splitlines() + if line.split() + } + self.assertEqual( + imports - RUNTIME_ALLOWLISTS["durango"], + EXPECTED_IMPORTS, imports) + self.assertEqual( + imports, + EXPECTED_IMPORTS | RUNTIME_ALLOWLISTS["durango"]) + self.assertEqual(imports, set()) + self.assertEqual(len(imports), 0) + # Zero imports proves the two compare-exchange primitives are pure + # inline-asm leaves with no libc or cross-module calls. + + defined = subprocess.run( + [str(LLVM_NM), "--extern-only", "--defined-only", + str(RETAIL_OBJ)], + capture_output=True, text=True) + self.assertEqual(defined.returncode, 0, defined.stderr) + functions, globals_ = defined_symbols_by_kind(defined.stdout) + self.assertEqual(functions, EXPECTED_FUNCTIONS) + self.assertEqual(globals_, EXPECTED_GLOBALS) + self.assertEqual(len(functions), 2) + + strings = subprocess.run( + ["strings", str(RETAIL_OBJ)], capture_output=True, text=True) + self.assertEqual(strings.returncode, 0, strings.stderr) + self.assertIn(PROVENANCE, strings.stdout) + self.assertNotIn(r"src\flash\durango_rrAtomics_x64", strings.stdout) + + for platform in ("orbis", "ps3", "vita"): + tsv = ROOT / f"iggy-decompiled/symbols/iggy_{platform}.modules.tsv" + text = tsv.read_text(encoding="utf-8") + self.assertNotIn("durango_rrAtomics_x64\t", text, platform) + + if SOURCE.exists(): + cmake = (RECONSTRUCTED / "CMakeLists.txt").read_text( + encoding="utf-8") + self.assertIn( + "iggy_reconstructed_durango_rrAtomics_x64_durango", cmake) + self.assertIn( + "add_library(iggy_reconstructed_durango_rrAtomics_x64_durango " + "STATIC durango_rrAtomics_x64.c)", + cmake) + self.assertIn( + "IGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO", cmake) + inventory = (RECONSTRUCTED / "modules.tsv").read_text( + encoding="utf-8") + self.assertIn( + "durango_rrAtomics_x64\t2\t2\tdurango\tcomplete", inventory) + + def test_exact_exports_globals_normalized_imports_and_c_cpp_abi(self): + self.assertTrue(SOURCE.exists(), f"missing {SOURCE}") + self.assertTrue(HEADER.exists(), f"missing {HEADER}") + self.assertIn( + r"c:\\projects\\g\\build\\release\\temps\\libdu\\iggy_durango\\" + r"durango_rrAtomics_x64.obj", + SOURCE.read_text(encoding="utf-8")) + + function_pointers = "".join( + f" (GenericFn)&{name},\n" + for name in sorted(EXPECTED_FUNCTIONS)) + probe_text = ( + '#include \n#include "durango_rrAtomics_x64.h"\n' + 'typedef void (*GenericFn)(void);\n' + 'static GenericFn rr_abis[] = {\n' + function_pointers + '};\n' + 'int use_abis(void) {\n' + ' for (unsigned i = 0; ' + 'i < sizeof(rr_abis) / sizeof(rr_abis[0]); ++i) {\n' + ' if (!rr_abis[i]) return 0;\n' + ' }\n' + ' return 1;\n' + '}\n') + + compiler, target = "clang", "x86_64-pc-windows-msvc" + platform = "durango" + with tempfile.TemporaryDirectory() as directory: + temporary = Path(directory) + output = temporary / f"durango_rrAtomics_x64_{platform}.o" + build = subprocess.run([ + compiler, "-target", target, "-ffreestanding", "-fno-builtin", + "-c", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror", + "-DIGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO=1", + "-I", str(RECONSTRUCTED), str(SOURCE), "-o", str(output), + ], capture_output=True, text=True) + self.assertEqual(build.returncode, 0, build.stderr) + + if LLVM_NM.exists(): + defined = subprocess.run([ + str(LLVM_NM), "--extern-only", "--defined-only", + str(output), + ], capture_output=True, text=True) + undefined = subprocess.run([ + str(LLVM_NM), "--undefined-only", str(output), + ], capture_output=True, text=True) + self.assertEqual(defined.returncode, 0, defined.stderr) + self.assertEqual(undefined.returncode, 0, undefined.stderr) + functions, globals_ = defined_symbols_by_kind(defined.stdout) + imports = { + normalized_symbol(line.split()[-1]) + for line in undefined.stdout.splitlines() + if line.split() + } + self.assertEqual(functions, EXPECTED_FUNCTIONS, platform) + self.assertEqual(globals_, EXPECTED_GLOBALS, platform) + self.assertEqual( + imports - RUNTIME_ALLOWLISTS[platform], + EXPECTED_IMPORTS, (platform, imports)) + self.assertEqual( + imports, + EXPECTED_IMPORTS | RUNTIME_ALLOWLISTS[platform]) + self.assertEqual(imports, set()) + + for language, standard in (("c", "c11"), ("c++", "c++17")): + suffix = "c" if language == "c" else "cpp" + probe = temporary / ( + f"durango_rrAtomics_x64_abi_{platform}.{suffix}") + probe.write_text(probe_text, encoding="utf-8") + syntax = subprocess.run([ + compiler, "-target", target, "-ffreestanding", + "-fsyntax-only", f"-std={standard}", + "-Wall", "-Wextra", "-Werror", + "-DIGGY_RECONSTRUCT_DURANGO_RRATOMICS_X64_DURANGO=1", + "-I", str(RECONSTRUCTED), str(probe), + ], capture_output=True, text=True) + self.assertEqual( + syntax.returncode, 0, (language, syntax.stderr)) + + +if __name__ == "__main__": + unittest.main()