Skip to content

fix(agc-replay): read version 1 captures after the display buffer gained an alignment field - #561

Merged
RINNECODER merged 1 commit into
mainfrom
fix/agc-replay-v1-captures
Oct 18, 2026
Merged

RINNECODER merged 1 commit into
mainfrom
fix/agc-replay-v1-captures

Conversation

@RINNECODER

@RINNECODER RINNECODER commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Problem

The DisplayBuffer struct gained a trailing alignment field in the upstream batch 13 merge (PR #528) without a capture version bump. The frame capture writes the struct verbatim, so every capture taken before that merge is eight bytes short in its Display record, and every capture taken after it is still version 1 while carrying the longer struct. anyps5_agc_replay assumed the older layout and aborted before the first repeat:

libc++abi: terminating due to uncaught exception of type std::runtime_error:
VideoOut: 4 KiB display buffer alignment is only supported for linear buffers
  #8 AgcDriver::DisplayBufferSize
  #10 AgcDriver::Metal::MetalPresentation::ReadPixels

All existing captures (agc-menu, agc-map, agc-live-map) were unreplayable, which also takes the pixel gates and the frame benchmarks off the table.

Fix

The reader decodes a display record as whichever layout satisfies both of:

  • the payload length equals the header, struct, pixel-hash and pixel-count fields plus the stored byte count;
  • the decoded fields pass DisplayBufferSize (dimensions, pixel format, alignment, pitch, address alignment, size).

A struct read at the wrong offset shifts every field and fails the second check, so the 48-byte and 56-byte version 1 layouts are told apart without a format change. A record matching neither fails with capture display record has an inconsistent payload length. Every read is bounded by the record length first.

Verification

  • python3 tools/check_conventions.py --base main: pass.
  • ctest --test-dir build-rosetta: 25/25 pass.
  • ctest --test-dir build-guest-replay (native Metal, 63 tests): 63/63 pass.
  • Both version 1 layouts replay, on two captures: agc-live-map and agc-menu were each replayed as stored (48-byte struct) and rewritten to the 56-byte struct. Both layouts report the capture's stored display hash (5e5670079f1ae58a and 7b290cf73841d336) and produce the same frame within the frame's race noise (83-85 px differ, max delta 79).
  • A record carrying a size field for version 2 is rejected: unsupported capture version 2.
  • agc-map still replays pixel-exact: hash 51a268f2e73efb9c, stable over the timed repeats and equal to the game's frame.
  • agc-menu and agc-live-map replay instead of aborting. Their stored reference PNGs predate the batch 13-37 merges: they now differ by 1.2k and 14.2k low-amplitude pixels (max delta 74-85) while the repeat-to-repeat noise is 71-92 px, so those references need a refresh; the agc-map hash shows rendering is unchanged.

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 00bf26af-94c3-4209-bec3-0af48fbce729
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-18T21:32:01.053864Z 4d4f4d2 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0ccee7350c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/agc-replay/AgcReplay.mm Outdated
Comment on lines +232 to +235
} else {
// Version 1 wrote the DisplayBuffer without its trailing alignment field, which
// defaults to the 64 KiB rule the PS5 buffers then always used.
display.buffer.address = in.Get<std::uint64_t>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve post-alignment version 1 captures

Version 1 is ambiguous because the parent commit already wrote the current 56-byte DisplayBuffer (including alignment) while still declaring format version 1. For any capture created after that field was added but before this version bump, this branch consumes only the first 48 bytes, then interprets alignment/padding as the pixel hash and the real hash as the raw/stored lengths, usually aborting as a truncated record. Detect both version-1 payload layouts—such as by validating the remaining display payload—rather than treating every version-1 display as the older struct.

Useful? React with 👍 / 👎.

@RINNECODER
RINNECODER force-pushed the fix/agc-replay-v1-captures branch from 0ccee73 to 8ef3b5e Compare October 18, 2026 21:12
@RINNECODER

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8ef3b5e77a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tools/agc-replay/AgcReplay.mm Outdated
return 8u + candidate + 16u + stored == bytes;
};
std::uint32_t alignment = 0;
std::memcpy(&alignment, payload + 8u + 48u, 4);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Bounds-check before reading the alignment discriminator

When a malformed or truncated Display record contains the frame header but fewer than 60 payload bytes, this unconditional memcpy reads past the record before consistent() can reject it. If that record lies near the final mapped page boundary, the read can cross the mmap and terminate the replay process with SIGBUS/segmentation fault instead of reporting the inconsistent payload; first establish that the 56-byte layout has enough bytes (for example via consistent(sizeof(DisplayBuffer))) before accessing its alignment field.

Useful? React with 👍 / 👎.

The DisplayBuffer struct gained a trailing alignment field in the upstream batch
13 merge without a capture version bump, so the replay read every existing
capture's Display record eight bytes short and aborted in DisplayBufferSize with
"4 KiB display buffer alignment is only supported for linear buffers". Every
capture taken before that merge became unreplayable, and captures written after
it are still version 1 while carrying the longer struct.

A display record is now decoded as whichever layout satisfies both the record's
payload length and DisplayBufferSize, so a struct read at the wrong offset is
rejected rather than shifting the pixel hash and lengths; a record matching
neither layout fails with a clear message. All reads are bounded by the record
length.
@RINNECODER
RINNECODER force-pushed the fix/agc-replay-v1-captures branch from 8ef3b5e to 4d4f4d2 Compare October 18, 2026 21:30
@RINNECODER

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 4d4f4d2099

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@RINNECODER
RINNECODER merged commit 90b390f into main Oct 18, 2026
1 check passed
@RINNECODER
RINNECODER deleted the fix/agc-replay-v1-captures branch October 18, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant