From 548162a2030299a279d15e368c9cc8633d94d2fb Mon Sep 17 00:00:00 2001 From: Michon <231849@buas.nl> Date: Thu, 1 Oct 2026 00:02:31 +0200 Subject: [PATCH] fix(agc): grow a command buffer whose free space is below its reserved words A command buffer with fewer free dwords than reserved_dw (an empty one whose title allocates its storage in the callback, as Marvel's Wolverine does) threw "reserved space exceeds command buffer capacity" before its callback ran. Its available space is now 0, so the allocation goes to the callback with the reserved words added, as for any other full buffer. Co-Authored-By: Claude Opus 5.5 (1M context) --- core/libs/prx/libSceAgc/Command/src/Packet.cpp | 2 +- core/libs/prx/libSceAgc/tests/Commands.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libSceAgc/Command/src/Packet.cpp b/core/libs/prx/libSceAgc/Command/src/Packet.cpp index 62a12734b..e2bf38b21 100644 --- a/core/libs/prx/libSceAgc/Command/src/Packet.cpp +++ b/core/libs/prx/libSceAgc/Command/src/Packet.cpp @@ -45,7 +45,7 @@ std::uint32_t available(const CommandBuffer& buffer, const char* function) { Require(bottom <= up && up <= down && down <= top, function, "invalid command buffer cursors"); Require(bottom != 0 || top == 0, function, "null command buffer storage"); const auto count = (down - up) / sizeof(std::uint32_t); - Require(count >= buffer.reserved_dw, function, "reserved space exceeds command buffer capacity"); + if (count <= buffer.reserved_dw) return 0; Require(count - buffer.reserved_dw <= std::numeric_limits::max(), function, "command buffer capacity overflow"); return static_cast(count - buffer.reserved_dw); } diff --git a/core/libs/prx/libSceAgc/tests/Commands.cpp b/core/libs/prx/libSceAgc/tests/Commands.cpp index 821a95ce7..5b394c586 100644 --- a/core/libs/prx/libSceAgc/tests/Commands.cpp +++ b/core/libs/prx/libSceAgc/tests/Commands.cpp @@ -63,6 +63,16 @@ bool APS5_VABI grow(CommandBuffer* buffer, std::uint32_t count, void* userData) return true; } +bool APS5_VABI growReserved(CommandBuffer* buffer, std::uint32_t count, void* userData) { + auto& storage = *static_cast(userData); + check(count == 5 + 4, "incorrect callback allocation including reserved words"); + buffer->bottom = storage.words.data(); + buffer->top = storage.words.data() + storage.words.size(); + buffer->cursor_up = buffer->bottom; + buffer->cursor_down = buffer->top; + return true; +} + void testPackets() { Storage storage; sceAgcDcbResetQueue(&storage.buffer, 0, 3); @@ -77,6 +87,10 @@ void testPackets() { CommandBuffer empty{nullptr, nullptr, nullptr, nullptr, grow, &destination, 0}; Agc::Command::Emit(&empty, 0x15u, {1, 1, 1, 0x41u}, __func__); check(empty.cursor_up == destination.words.data() + 5, "guest ABI allocation callback failed"); + Storage reservedDestination; + CommandBuffer reserved{nullptr, nullptr, nullptr, nullptr, growReserved, &reservedDestination, 4}; + Agc::Command::Emit(&reserved, 0x15u, {1, 1, 1, 0x41u}, __func__); + check(reserved.cursor_up == reservedDestination.words.data() + 5, "buffer with less room than its reserved words did not grow"); Storage exhausted; exhausted.buffer.cursor_down = exhausted.words.data() + 2; expectFailure([&] { Agc::Command::WriteNop(&exhausted.buffer, 3, __func__); });