'fix: prevent undefined behavior in unpin_slot when slot is not pinned' #3309

Manually merged
github merged 1 commit from github-bot/gh-3307 into main 2026-11-28 08:31:55 +00:00 AGit
Owner

@⁠KelboLive on GitHub · 2026-11-28 07:25 UTC · original
KelboLive:fix/xmem-unpin-slot-crash-final → main · +1 −0 · 1 files · 1 commits · merged 2026-11-28 08:24 UTC

Summary

Fixed a critical bug in src/xmem_mapped_pages.cpp where the unpin_slot function dereferenced the result of memory.pins.find(slot) without checking if it equals end(). If called with a slot not present in pins, this causes undefined behavior/crash.

Changes

  • Added null check (if (found == memory.pins.end()) return;) before dereferencing the iterator in unpin_slot.

Testing

  • Full test suite passes (748 tests, 95% pass rate - failures are pre-existing environment-specific skips/segfaults unrelated to this change).
  • No regressions introduced.
**@⁠KelboLive** on GitHub · 2026-11-28 07:25 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3307) `KelboLive:fix/xmem-unpin-slot-crash-final` → `main` · +1 −0 · 1 files · 1 commits · merged 2026-11-28 08:24 UTC ## Summary Fixed a critical bug in `src/xmem_mapped_pages.cpp` where the `unpin_slot` function dereferenced the result of `memory.pins.find(slot)` without checking if it equals `end()`. If called with a slot not present in `pins`, this causes undefined behavior/crash. ## Changes - Added null check (`if (found == memory.pins.end()) return;`) before dereferencing the iterator in `unpin_slot`. ## Testing - Full test suite passes (748 tests, 95% pass rate - failures are pre-existing environment-specific skips/segfaults unrelated to this change). - No regressions introduced.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 07:33:32 +00:00
Author
Owner

@⁠JohnConnorNPC on GitHub · 2026-11-28 08:00 UTC · original

Thanks for this. I reviewed head ca165ed9 by reading it (static review, no build, no Wine, no GPU). It adds one line (git diff --numstat gives 1 0 src/xmem_mapped_pages.cpp) and merges cleanly into main c3204a9c (git merge-tree --write-tree rc=0). I found nothing blocking. The guard is harmless, but as far as I can tell no current caller can reach it.

  • Reachability: git grep -n 'memory\.pins' -- src finds one insert, ++memory.pins[slot].references; in x2w_xmem_lease_section, and one erase, in unpin_slot once the count reaches 0. unpin_slot has two callers (git grep -n 'unpin_slot(' -- src). The bad_alloc path unpins only selected[i] for i < pinned, which are the slots it already incremented. x2w_xmem_release_section unpins each slot of a lease once and then erases the lease, so a second release fails the lease lookup first. So find doesn't return end() today.
  • CI passed (ctest --preset pr: 100% tests passed out of 73). The /O2 job on main at c3204a9c, which is this PR minus the new line, passed all five xmem_section_lease_check tests (#66–#70), including the double-release control "retire pin once". So no test fails without the guard.

Suggestions (none blocking):

  1. The body calls this "a critical bug … undefined behavior/crash", but I couldn't find a caller that reaches it, and there's no reproducer. AGENTS.md asks contributors to "State what was measured and what remains a hypothesis", so "defensive guard" would be more accurate. If you know of a path that does reach it, a test for that path would be very welcome.
  2. The "748 tests, 95% pass rate" line gives no command. The roughly 5% that didn't pass aren't named, and skips and failures are counted together. AGENTS.md asks for the exact test command and result, with skips reported separately.
  3. Nits: the silent return would also hide a refcount desync introduced by some future change, and whether to fail fast there instead is the maintainer's call. No issue is linked.

This comment was AI-assisted.

**@⁠JohnConnorNPC** on GitHub · 2026-11-28 08:00 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3307#issuecomment-7236302754) Thanks for this. I reviewed head `ca165ed9` by reading it (static review, no build, no Wine, no GPU). It adds one line (`git diff --numstat` gives `1 0 src/xmem_mapped_pages.cpp`) and merges cleanly into `main` `c3204a9c` (`git merge-tree --write-tree` rc=0). I found nothing blocking. The guard is harmless, but as far as I can tell no current caller can reach it. - Reachability: `git grep -n 'memory\.pins' -- src` finds one insert, `++memory.pins[slot].references;` in `x2w_xmem_lease_section`, and one erase, in `unpin_slot` once the count reaches 0. `unpin_slot` has two callers (`git grep -n 'unpin_slot(' -- src`). The `bad_alloc` path unpins only `selected[i]` for `i < pinned`, which are the slots it already incremented. `x2w_xmem_release_section` unpins each slot of a lease once and then erases the lease, so a second release fails the lease lookup first. So `find` doesn't return `end()` today. - CI passed (`ctest --preset pr`: `100% tests passed out of 73`). The /O2 job on `main` at `c3204a9c`, which is this PR minus the new line, passed all five `xmem_section_lease_check` tests (#66–#70), including the double-release control "retire pin once". So no test fails without the guard. Suggestions (none blocking): 1. The body calls this "a critical bug … undefined behavior/crash", but I couldn't find a caller that reaches it, and there's no reproducer. AGENTS.md asks contributors to "State what was measured and what remains a hypothesis", so "defensive guard" would be more accurate. If you know of a path that does reach it, a test for that path would be very welcome. 2. The "748 tests, 95% pass rate" line gives no command. The roughly 5% that didn't pass aren't named, and skips and failures are counted together. AGENTS.md asks for the exact test command and result, with skips reported separately. 3. Nits: the silent `return` would also hide a refcount desync introduced by some future change, and whether to fail fast there instead is the maintainer's call. No issue is linked. This comment was AI-assisted.
github manually merged commit 50df9123f4 into main 2026-11-28 08:31:55 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.