fix(libSceGLSlimVSH): copy texture uploads and glReadPixels output through checked host buffers #20807

Merged
milescoviello merged 1 commit from upstream-pr/20807 into main 2026-11-16 16:29:23 +00:00
milescoviello commented 2026-11-15 15:51:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-11-15T15:51:19Z.


What

glTexImage2D and glTexSubImage2D (through the shared Upload helper) decoded the guest pixels buffer in place, pixel by pixel. glReadPixels memcpy'd each converted pixel straight into the guest buffer. An unmapped or partly mapped pixel buffer faulted the host. The library's debt file already lists this as needing a bounded host buffer.

Upload

  • Upload now copies the source rows into a host vector with one GuestCopyIn_nid_no_patch, then decodes from there.
  • The copy covers exactly the bytes the old loop read: (height - 1) * stride + width * size, with no padding after the last row. A buffer that ends right after its last row still works.
  • A failed copy sets GL_INVALID_VALUE (0x0501). The texture is left unchanged: glTexImage2D does not replace the image, and glTexSubImage2D writes nothing.
  • glTexSubImage2D now catches bad_alloc from the host vector as GL_OUT_OF_MEMORY, the same as glTexImage2D already does.

glReadPixels

  • It converts the clipped region into a host vector.
  • It writes the clipped rows with one GuestCopyOutRows_nid_no_patch (#20832), whether they are contiguous in the guest buffer or strided by column clipping or pack padding. (Edited: before the rebase this wrote one GuestCopyOut_nid_no_patch per clipped row.)
  • Bytes outside the clipped region and the row padding stay untouched, as before.
  • A failed write sets 0x0501. A region that clips to nothing still writes nothing and sets no error.

These are whole-image copies: one checked copy per upload and one batched row copy per read. They replace per-pixel guest accesses, so the cost per call goes down rather than up.

The existing debt entry is rewritten through a ## change fragment under docs/dev/debt/changes/. It records 0x0501 as a host-safety fallback, since console behaviour here is not measured. That code matches what this library already returns for other unreadable arrays (glDelete*, glShaderSource).

Tested

Linux (Ubuntu 26.04.1), GCC 15.2, Release.

  • Rebased onto main e4caa07950 (head 2d02fe2db9) to use #20832's GuestCopyOutRows_nid_no_patch. The four guest_glslim* tests pass, 3 runs of 3, including the clipped and contiguous glReadPixels cases below.
  • New PixelFaults step in guest_glslim_state. The buffers sit at the end of a page followed by a PROT_NONE page (PAGE_NOACCESS on Windows).
    • Valid edge cases:
      • A 3×2 RGBA upload with UNPACK_ALIGNMENT 8 (stride 16) ends exactly at the page end, 28 bytes, and reads back right.
      • A 24-byte glReadPixels into the page end writes the expected bytes.
      • A column-clipped read (x = -1) leaves column 0 at its fill value and writes the rest.
    • Failure cases, each setting 0x0501 with errno left at 71:
      • glTexImage2D from 0x10;
      • a 3×3 upload whose third row would cross into the protected page;
      • glTexSubImage2D from 0x10;
      • glReadPixels to 0x10;
      • glReadPixels into a buffer whose second row is on the protected page, both contiguous and clipped.
    • After the failed uploads the texture still reads back the original 3×2 image.
  • The rest of guest_glslim_state and the other GLSlim tests pass.
  • Negative control: Images.cpp from main with these tests dies with SIGSEGV at address 0x10. The valid page-end cases run first and pass on main too.
  • Merges with my other GLSlim PRs:
    • git merge-tree is clean against main, #14212, #20798, #20802 and #20804.
    • With all four of my GLSlim branches merged onto main, all five GLSlim tests and debt_fold.py --check pass.
  • tools/check_conventions.py passes on the branch and this body.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5324415722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20807). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-11-15T15:51:19Z. --- ### What `glTexImage2D` and `glTexSubImage2D` (through the shared `Upload` helper) decoded the guest `pixels` buffer in place, pixel by pixel. `glReadPixels` `memcpy`'d each converted pixel straight into the guest buffer. An unmapped or partly mapped pixel buffer faulted the host. The library's debt file already lists this as needing a bounded host buffer. **Upload** - `Upload` now copies the source rows into a host vector with one `GuestCopyIn_nid_no_patch`, then decodes from there. - The copy covers exactly the bytes the old loop read: `(height - 1) * stride + width * size`, with no padding after the last row. A buffer that ends right after its last row still works. - A failed copy sets `GL_INVALID_VALUE` (0x0501). The texture is left unchanged: `glTexImage2D` does not replace the image, and `glTexSubImage2D` writes nothing. - `glTexSubImage2D` now catches `bad_alloc` from the host vector as `GL_OUT_OF_MEMORY`, the same as `glTexImage2D` already does. **glReadPixels** - It converts the clipped region into a host vector. - It writes the clipped rows with one `GuestCopyOutRows_nid_no_patch` (#20832), whether they are contiguous in the guest buffer or strided by column clipping or pack padding. (Edited: before the rebase this wrote one `GuestCopyOut_nid_no_patch` per clipped row.) - Bytes outside the clipped region and the row padding stay untouched, as before. - A failed write sets 0x0501. A region that clips to nothing still writes nothing and sets no error. These are whole-image copies: one checked copy per upload and one batched row copy per read. They replace per-pixel guest accesses, so the cost per call goes down rather than up. The existing debt entry is rewritten through a `## change` fragment under `docs/dev/debt/changes/`. It records 0x0501 as a host-safety fallback, since console behaviour here is not measured. That code matches what this library already returns for other unreadable arrays (`glDelete*`, `glShaderSource`). ### Tested Linux (Ubuntu 26.04.1), GCC 15.2, Release. - Rebased onto `main` `e4caa07950` (head `2d02fe2db9`) to use #20832's `GuestCopyOutRows_nid_no_patch`. The four `guest_glslim*` tests pass, 3 runs of 3, including the clipped and contiguous `glReadPixels` cases below. - New `PixelFaults` step in `guest_glslim_state`. The buffers sit at the end of a page followed by a `PROT_NONE` page (`PAGE_NOACCESS` on Windows). - Valid edge cases: - A 3×2 RGBA upload with `UNPACK_ALIGNMENT` 8 (stride 16) ends exactly at the page end, 28 bytes, and reads back right. - A 24-byte `glReadPixels` into the page end writes the expected bytes. - A column-clipped read (`x = -1`) leaves column 0 at its fill value and writes the rest. - Failure cases, each setting 0x0501 with `errno` left at 71: - `glTexImage2D` from `0x10`; - a 3×3 upload whose third row would cross into the protected page; - `glTexSubImage2D` from `0x10`; - `glReadPixels` to `0x10`; - `glReadPixels` into a buffer whose second row is on the protected page, both contiguous and clipped. - After the failed uploads the texture still reads back the original 3×2 image. - The rest of `guest_glslim_state` and the other GLSlim tests pass. - Negative control: `Images.cpp` from `main` with these tests dies with SIGSEGV at address `0x10`. The valid page-end cases run first and pass on `main` too. - Merges with my other GLSlim PRs: - `git merge-tree` is clean against `main`, #14212, #20798, #20802 and #20804. - With all four of my GLSlim branches merged onto `main`, all five GLSlim tests and `debt_fold.py --check` pass. - `tools/check_conventions.py` passes on the branch and this body. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) and the licence notice of code adapted from upstream (see AGENTS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!20807
No description provided.