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) <noreply@anthropic.com>
This commit is contained in:
Michon
2026-10-01 00:02:31 +02:00
parent a67b644ff6
commit 548162a203
2 changed files with 15 additions and 1 deletions
@@ -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<std::uint32_t>::max(), function, "command buffer capacity overflow");
return static_cast<std::uint32_t>(count - buffer.reserved_dw);
}
@@ -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<Storage*>(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__); });