From 7b5bf4854af2553b9e0d1c54cd8a1e543650bd7c Mon Sep 17 00:00:00 2001 From: coah <100182500+coah80@users.noreply.github.com> Date: Fri, 2 Oct 2026 21:48:46 -0500 Subject: [PATCH] fix(cdb): correct crypt-buffer ownership slot types --- libs/RevoEX/src/cdb/CDBCrypt.c | 12 ++++----- .../allhands-cdb-buffer-ownership.attempts.md | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 tools/decomp-assist/allhands-cdb-buffer-ownership.attempts.md diff --git a/libs/RevoEX/src/cdb/CDBCrypt.c b/libs/RevoEX/src/cdb/CDBCrypt.c index 8dfb9c6a..6827ea6b 100644 --- a/libs/RevoEX/src/cdb/CDBCrypt.c +++ b/libs/RevoEX/src/cdb/CDBCrypt.c @@ -8,13 +8,11 @@ void CDBCryptBufSysInit(CDBCryptBuf* cryptBuf) { cryptBuf->allocated = FALSE; } -// Some weird crap is going on with these functions. - -CDBErr CDBCryptBufAllocate(CDBCryptBuf* cryptBuf) { +CDBErr CDBCryptBufAllocate(CDBCryptBuf** cryptBuf) { CDBErr err = CDB_ERROR_OK; if (s_cryptBuf->allocated == FALSE) { - cryptBuf->buffer32[0] = (u32)&s_cryptBuf[err]; + *cryptBuf = &s_cryptBuf[err]; s_cryptBuf[err].allocated = TRUE; return CDB_ERROR_OK; } else { @@ -23,12 +21,12 @@ CDBErr CDBCryptBufAllocate(CDBCryptBuf* cryptBuf) { } } -CDBErr CDBCryptBufFree(CDBCryptBuf* cryptBuf) { +CDBErr CDBCryptBufFree(CDBCryptBuf** cryptBuf) { CDBErr err = CDB_ERROR_OK; - if ((CDBCryptBuf*)*cryptBuf->buffer32 == s_cryptBuf) { + if (*cryptBuf == s_cryptBuf) { s_cryptBuf[err].allocated = FALSE; - *cryptBuf->buffer32 = 0; + *cryptBuf = NULL; return CDB_ERROR_OK; } else { CDBReportError("failed to free crypt buffer\n"); diff --git a/tools/decomp-assist/allhands-cdb-buffer-ownership.attempts.md b/tools/decomp-assist/allhands-cdb-buffer-ownership.attempts.md new file mode 100644 index 00000000..63dcd12e --- /dev/null +++ b/tools/decomp-assist/allhands-cdb-buffer-ownership.attempts.md @@ -0,0 +1,26 @@ +# CDB crypt-buffer ownership contract + +Base: 20ba34a8, 43U. Focused source correction after the read-only CDBRecordEncrypt audit found only register-allocation differences in that function. + +## Evidence and change + +CDBRecord.c already declares CDBCryptBufAllocate and CDBCryptBufFree with CDBCryptBuf** parameters. Its callers supply the address of the record's crypt-buffer pointer slot. CDBCrypt.c instead declared CDBCryptBuf* and accessed buffer32[0] as a u32-encoded pointer, producing incompatible cross-translation-unit function types and describing the caller's pointer slot as an entire crypt-buffer object. + +Both definitions now take CDBCryptBuf** and use direct typed pointer loads/stores. Allocation assigns the pool pointer; free compares the owned pointer and clears the slot to NULL. The existing pool-index expression, flag update order, diagnostics and failure behavior are retained. The obsolete placeholder comment is removed. No header, payload layout, pool extent, Encrypt implementation, or linking change. + +## Validation + +- Configure: `python3 configure.py --version 43U --wrapper ../toolchain/wibo-build/wibo` +- Build: `WIBO_SJIS_MISSING_IMPORTS=1 ../.venv/bin/ninja` +- Entire compiled CDBCrypt.o is byte-identical to the baseline, including all code/data, symbols and relocations +- CDBCryptBufAllocate: 36/36 instructions, ctxdiff zero +- CDBCryptBufFree: 33/33 instructions, ctxdiff zero +- Unit remains 100% fuzzy, 3/3 exact functions, 296/296 code bytes and 80/80 data bytes +- Full-project before/after objdiff reports are identical; no metric gain is claimed +- Pool identical: 2/2 strings; literal-reference audit: 2 arguments across all 3 functions, no candidates/errors/skips +- Host test compiles the production source with the actual CDBCryptBuf layout and minimal platform/error stubs. AddressSanitizer and UBSan pass 50,036 ownership transitions, including every combination of six representative allocation flags and null/owned/foreign pointer slots, 10,000 randomized state cases, and 10,000 allocate/reallocate/free/refree cycles. Checks cover pointer-slot canaries, complete payload preservation, allocation flag, return value and diagnostic count +- LeakSanitizer is disabled because it cannot run under this execution environment's ptrace; the harness makes no heap allocations. Address and undefined-behavior instrumentation remain enabled +- Full 43U build and build/43U/ok pass; DOL SHA1 is 26116613f624061ba99c8d1a299aaa6efa85670d +- git diff --check passes + +Private reproducible evidence is in /tmp/cdb-buffer-ownership (baseline object, full reports, host validator and build log). Original objects/disassembly from the preceding read-only audit remain private under /tmp/cdb-record-encrypt-audit. -- 2.49.1