fix(libSceAgc): return null from flip and rendering-wait packets for unopened video outputs (#20294)

This commit is contained in:
Adriaan authored and GitHub committed 2026-12-04 10:18:06 +13:00
1 parent 192c4e45a0
commit 4f0ae22ffe
10 files changed
+54 -12

No files matched your search

@@ -73,6 +73,7 @@ std::uint32_t APS5_VABI sceAgcAcbRewindGetSize() {
}
std::uint32_t* APS5_VABI sceAgcAcbWaitUntilSafeForRendering(CommandBuffer* buf, std::uint32_t videoOutHandle, std::uint32_t displayBufferIndex) {
if (buf != nullptr && !AgcDriverVideoOutputRegistered_nid_postfix(videoOutHandle)) return nullptr;
auto* packet = Agc::Command::Allocate(buf, AgcDriver::RenderingWaitPacketWords, __func__);
packet[0] = AgcDriver::RenderingWaitPacketHeader;
packet[1] = videoOutHandle;
@@ -82,6 +83,7 @@ std::uint32_t* APS5_VABI sceAgcAcbWaitUntilSafeForRendering(CommandBuffer* buf,
}
std::uint32_t* APS5_VABI sceAgcAcbSetFlip(CommandBuffer* buf, std::uint32_t videoOutHandle, std::int32_t displayBufferIndex, std::uint32_t flipMode, std::int64_t flipArg) {
if (buf != nullptr && !AgcDriverVideoOutputRegistered_nid_postfix(videoOutHandle)) return nullptr;
auto* packet = Agc::Command::Allocate(buf, AgcDriver::FlipPacketWords, __func__);
packet[0] = AgcDriver::FlipPacketHeader;
packet[1] = videoOutHandle;
@@ -55,6 +55,7 @@ uint32_t APS5_VABI sceAgcDcbRewindGetSize(void) {
}
uint32_t* APS5_VABI sceAgcDcbWaitUntilSafeForRendering(CommandBuffer* buf, uint32_t video_out_handle, uint32_t display_buffer_index) {
if (buf != nullptr && !AgcDriverVideoOutputRegistered_nid_postfix(video_out_handle)) return nullptr;
auto* packet = Agc::Command::Allocate(buf, AgcDriver::RenderingWaitPacketWords, __func__);
packet[0] = AgcDriver::RenderingWaitPacketHeader;
packet[1] = video_out_handle;
@@ -11,6 +11,7 @@
extern "C" {
uint32_t* APS5_VABI sceAgcDcbSetFlip(CommandBuffer* buf, uint32_t video_out_handle, int32_t display_buffer_index, uint32_t flip_mode, int64_t flip_arg) {
if (buf != nullptr && !AgcDriverVideoOutputRegistered_nid_postfix(video_out_handle)) return nullptr;
auto* packet = Agc::Command::Allocate(buf, AgcDriver::FlipPacketWords, __func__);
packet[0] = AgcDriver::FlipPacketHeader;
packet[1] = video_out_handle;
@@ -35,6 +35,7 @@
#include "prx/libSceAgc/DcbFlow/include/Memory.hpp"
#include "prx/libSceAgc/Command/include/RegisterDefaults.hpp"
#include "prx/libc/include/Shutdown.hpp"
#include "prx/libSceAgcDriver/Execution/include/VideoOutput.hpp"
#include <algorithm>
#include <atomic>
@@ -45,6 +46,7 @@
#include <cstdio>
#include <stdexcept>
#include <cstring>
#include <memory>
#include <string>
#include <map>
@@ -67,6 +69,20 @@ void expectFailure(TAction action) {
throw std::runtime_error("expected an exception");
}
class StubOutput final : public AgcDriver::IVideoOutput {
public:
std::shared_ptr<AgcDriver::IFlipRequest> Reserve(const AgcDriver::FlipInfo&) override { return nullptr; }
void Fail(std::exception_ptr) noexcept override {}
};
void registerOutputs(const std::shared_ptr<AgcDriver::IVideoOutput>& output) {
for (const std::uint32_t handle : {1u, 7u, 0xfedcba98u}) AgcDriverRegisterVideoOutput_nid_postfix(handle, output);
}
void unregisterOutputs(const std::shared_ptr<AgcDriver::IVideoOutput>& output) {
for (const std::uint32_t handle : {1u, 7u, 0xfedcba98u}) AgcDriverUnregisterVideoOutput_nid_postfix(handle, output);
}
struct Storage {
std::array<std::uint32_t, 64> words{};
CommandBuffer buffer{words.data(), words.data() + words.size(), words.data(), words.data() + words.size(), nullptr, nullptr, 0};
@@ -791,6 +807,24 @@ void testFlip() {
check(sceAgcSuspendPoint() == 0, "empty suspend failed");
}
void testUnregisteredFlipHandle() {
struct Arguments { std::uint32_t handle; std::int32_t index; std::uint32_t mode; std::int64_t argument; };
const std::array<Arguments, 3> cases{{{0x11111111u, 0x22222222, 0x33333333u, 0x44444444}, {1, 2, 3, 4}, {0x22222222u, 0x44444444, 0, 0}}};
for (const auto& arguments : cases) {
const auto refused = [&](auto write) {
Storage storage;
storage.words.fill(0xdeadbeefu);
const bool isNull = write(storage.buffer) == nullptr;
const bool untouched = std::all_of(storage.words.begin(), storage.words.end(), [](std::uint32_t word) { return word == 0xdeadbeefu; });
return isNull && untouched && storage.buffer.cursor_up == storage.words.data();
};
check(refused([&](CommandBuffer& buffer) { return sceAgcDcbSetFlip(&buffer, arguments.handle, arguments.index, arguments.mode, arguments.argument); }), "graphics flip accepted an unregistered video output");
check(refused([&](CommandBuffer& buffer) { return sceAgcAcbSetFlip(&buffer, arguments.handle, arguments.index, arguments.mode, arguments.argument); }), "compute flip accepted an unregistered video output");
check(refused([&](CommandBuffer& buffer) { return sceAgcDcbWaitUntilSafeForRendering(&buffer, arguments.handle, static_cast<std::uint32_t>(arguments.index)); }), "graphics rendering wait accepted an unregistered video output");
check(refused([&](CommandBuffer& buffer) { return sceAgcAcbWaitUntilSafeForRendering(&buffer, arguments.handle, static_cast<std::uint32_t>(arguments.index)); }), "compute rendering wait accepted an unregistered video output");
}
}
void testRegisters() {
Storage storage;
const std::array<ShaderRegister, 3> registers{{{0x10, 7}, {0x11, 8}, {0x20, 9}}};
@@ -1530,6 +1564,8 @@ int main(int argc, char** argv) {
std::puts("AGC initialization tests passed");
return 0;
}
const auto output = std::make_shared<StubOutput>();
registerOutputs(output);
testPackets();
testReserveGrowthLock();
testEventWrite();
@@ -1557,6 +1593,8 @@ int main(int argc, char** argv) {
testFlowPackets();
testCondExecConsoleArguments();
testFlip();
unregisterOutputs(output);
testUnregisteredFlipHandle();
testRegisters();
testIndirectRegisterPatches();
testRegisterRange();
@@ -97,6 +97,7 @@ public:
bool Settled();
void RegisterVideoOutput(std::uint32_t handle, const std::shared_ptr<IVideoOutput>& output);
void UnregisterVideoOutput(std::uint32_t handle, const std::shared_ptr<IVideoOutput>& output);
bool VideoOutputRegistered(std::uint32_t handle);
void AttachWindow(const PresentationWindow& window);
void Present(const PresentationWindow& window, const DisplayBuffer* buffer, const DisplayBuffer* plane, bool opaque, void (*gpuReady)(void*), void* context);
void TracePresent(std::uint32_t width, std::uint32_t height);
@@ -64,5 +64,6 @@ extern "C" std::uint32_t AgcDriverReserveEopFlipAt_nid_postfix(const AgcDriver::
extern "C" void AgcDriverRegisterVideoOutput_nid_postfix(std::uint32_t handle, const std::shared_ptr<AgcDriver::IVideoOutput>& output);
extern "C" void AgcDriverUnregisterVideoOutput_nid_postfix(std::uint32_t handle, const std::shared_ptr<AgcDriver::IVideoOutput>& output);
extern "C" bool AgcDriverVideoOutputRegistered_nid_postfix(std::uint32_t handle);
#endif
@@ -245,6 +245,10 @@ extern "C" void AgcDriverUnregisterVideoOutput_nid_postfix(std::uint32_t handle,
AgcDriver::UnregisterVideoOutput(handle, output);
}
extern "C" bool AgcDriverVideoOutputRegistered_nid_postfix(std::uint32_t handle) {
return AgcDriver::DriverDetail::Driver::Get().VideoOutputRegistered(handle);
}
extern "C" void AgcDriverAttachWindow_nid_postfix(const AgcDriver::PresentationWindow& window) {
AgcDriver::AttachWindow(window);
}
@@ -63,6 +63,11 @@ void Driver::UnregisterVideoOutput(std::uint32_t handle, const std::shared_ptr<I
outputs.erase(it);
}
bool Driver::VideoOutputRegistered(std::uint32_t handle) {
std::lock_guard lock(mutex);
return outputs.contains(handle);
}
void Driver::AttachWindow(const PresentationWindow& window) {
CheckFailure();
std::unique_lock replacing(deviceReplacement);
+1
View File
@@ -44,6 +44,7 @@
- [sceAgcDcbQueueEndOfShaderActionGetSize](../../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - the builder is inline in the SDK and not exported; the size is assumed to be that of the RELEASE_MEM packet `sceAgcCbReleaseMem` writes (32 bytes), the same as `sceAgcAcbQueueEndOfShaderActionGetSize`, as in [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/main/src/game_compat.c) and [REmu](https://github.com/xnetcat/REmu/blob/main/crates/remu-runtime/src/agc_dcb.rs)
- [sceAgcDcbSetZPassPredicationEnableGetSize, sceAgcDcbSetPredicationDisableGetSize, sceAgcDcbSetBoolPredicationEnableGetSize](../../../core/libs/prx/libSceAgc/DcbState/src/Predication.cpp) (libSceAgc) - the builders are inline in the SDK and not exported; the size is assumed to be that of the SET_PREDICATION packet `sceAgcDcbSetPredication` writes (16 bytes)
- [sceAgcAcbWaitUntilSafeForRendering](../../../core/libs/prx/libSceAgc/Acb/src/Control.cpp) (libSceAgc) - `(buffer, videoOutHandle, displayBufferIndex)`, the declared arguments, which PPSA12544 (01.000.010) passes inside `waitUntilSafeForRendering` and `waitUntilSafeForRendering VR` markers on an asynchronous compute command buffer, ignoring the result. It writes the same rendering-wait packet as `sceAgcDcbWaitUntilSafeForRendering`, which the driver now accepts on compute queues too; the wait blocks only that queue's worker. Whether the console encodes the compute variant differently is not known. In PPSA12544 the Acb function is called from 2 sites (0x11d9538, 0x11d95a4) and the Dcb function from 4 (0x11d76d4, 0x11d7740, 0x16e5eac, 0x16f32a6), each with three arguments and no read of the result; which of the last two arguments is the handle and which the index is not shown.
- [sceAgcDcbSetFlip, sceAgcAcbSetFlip, sceAgcDcbWaitUntilSafeForRendering, sceAgcAcbWaitUntilSafeForRendering](../../../core/libs/prx/libSceAgc/DcbState/src/Display.cpp) (libSceAgc) - prosper's console measurements (`agcbuild` family) show all four returning null and leaving the command buffer untouched for handles `0x11111111`, `1` and `0x22222222` with no video output open. AnyPS5 now returns null without writing when the handle is not registered with the driver by `sceVideoOutOpen`, and still throws for a null buffer. Which check the console makes is not measured: it may reject the handle, the display buffer index, or the flip mode, and its answer for an open handle with an out-of-range index or mode is unknown.
- [sceAgcGetDefaultCxStateFlat](../../../core/libs/prx/libSceAgc/Misc/src/RegisterDefaults.cpp) (libSceAgc) - copies up to `size` bytes (clamped to 2048 bytes, the 512-dword `AgcContextState`) from the default context state initialized from the version 8 public register default groups (CX, SH, UC), indexed by register offset, following OpenAGC `src/context_state.c`; returns `0x80890001` on null destination; version selection without `sceAgcInit` is unverified
- [sceAgcDcbSetIndexIndirectArgs, sceAgcDcbSetIndexIndirectArgsGetSize](../../../core/libs/prx/libSceAgc/DcbDraw/src/IndexBuffer.cpp) (libSceAgc) - the `(buffer, attributeAddress, attributeIndex)` signature, a 64-bit address and a 32-bit index, is [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/3c7e8072f0d7684e930454794fd5c02056946d73/src/game_compat.c#L1742-L1757)'s. The console oracle in prosper measured the packet, an INDEX_ATTRIBUTES_INDIRECT (opcode 0x91) holding the low address dword, the high address dword and the index ([measured](https://github.com/mattias800/prosper/blob/3ea5cb9a846b6dcd00f5073360462bbda4f72288/prosper/tests/data/console_oracle/agcbuild.golden.tsv#L172-L174)), and a size of 16 bytes ([measured](https://github.com/mattias800/prosper/blob/3ea5cb9a846b6dcd00f5073360462bbda4f72288/prosper/tests/data/console_oracle/agc.golden.tsv#L67)). The measured rows show address bits 0 and 1 cleared and the index cut to its low 16 bits; bits 2 and 3 of the address are cleared as OpenAGC clears them, which no row measures. PPSA25286 01.000.568, PPSA34502 01.010.000 and PPSA36591 01.002.000 each link one wrapper per function (`SetIndexIndirectArgs`/`GetSize` at 0x405AC0/0x406030, 0xAA1C10/0xAA2180 and 0x898300/0x898870): the first passes index 0 with the buffer and address it was given, the second passes its arguments unchanged. Run-time use is untraced; PPSA01494, PPSA02225, PPSA03984, PPSA18194, PPSA30839 and PPSA31162 import neither.
- [sceAgcDcbSetIndexBuffer, sceAgcDcbSetIndexSize](../../../core/libs/prx/libSceAgc/DcbDraw/src/IndexBuffer.cpp) (libSceAgc) - an index size whose low two bits are 3 throws: its packet is not measured. Bits 2, 3, 6 and 7 of the index size and bits 3, 4 and 7 of the cache policy are dropped as parts of two-bit fields, which no row of the console oracle in prosper measures. Its rows clear bit 0 of the address ([measured](https://github.com/mattias800/prosper/blob/d44c89db977a162af271d986a8d1febbc31e702f/prosper/tests/data/console_oracle/agcbuild.golden.tsv#L166-L168)) and drop bits 4 and 5 of the index size and bits 2, 5 and 6 of the cache policy ([measured](https://github.com/mattias800/prosper/blob/d44c89db977a162af271d986a8d1febbc31e702f/prosper/tests/data/console_oracle/agcbuild.golden.tsv#L175-L177)). [OpenAGC](https://github.com/OpenAGC/OpenAGC/blob/3c7e8072f0d7684e930454794fd5c02056946d73/src/game_compat.c#L893-L907) keeps one bit of the cache policy.
-12
View File
@@ -17,12 +17,6 @@ agcbuild_AcbMemSemaphore_a throw the operation argument changes the control word
agcbuild_AcbMemSemaphore_b throw the operation argument changes the control word; not modelled
agcbuild_AcbMemSemaphore_c throw the operation argument changes the control word; not modelled
agcbuild_AcbResetQueue_b diff the packet differs from the console's; not yet analysed
agcbuild_AcbSetFlip_a diff the console returns null and writes nothing for these arguments
agcbuild_AcbSetFlip_b diff the console returns null and writes nothing for these arguments
agcbuild_AcbSetFlip_c diff the console returns null and writes nothing for these arguments
agcbuild_AcbWaitUntilSafeForRendering_a diff the console returns null and writes nothing for these arguments
agcbuild_AcbWaitUntilSafeForRendering_b diff the console returns null and writes nothing for these arguments
agcbuild_AcbWaitUntilSafeForRendering_c diff the console returns null and writes nothing for these arguments
agcbuild_CbBranch_a diff more than six arguments; the console's stack arguments are not known
agcbuild_CbBranch_b diff more than six arguments; the console's stack arguments are not known
agcbuild_CbBranch_c diff more than six arguments; the console's stack arguments are not known
@@ -58,13 +52,7 @@ agcbuild_DcbMemSemaphore_a throw the operation argument changes the control word
agcbuild_DcbMemSemaphore_b throw the operation argument changes the control word; not modelled
agcbuild_DcbMemSemaphore_c throw the operation argument changes the control word; not modelled
agcbuild_DcbResetQueue_b diff the packet differs from the console's; not yet analysed
agcbuild_DcbSetFlip_a diff the console returns null and writes nothing for these arguments
agcbuild_DcbSetFlip_b diff the console returns null and writes nothing for these arguments
agcbuild_DcbSetFlip_c diff the console returns null and writes nothing for these arguments
agcbuild_DcbWaitRegMem_b skip the ninth argument, pollCycles, comes from the caller's stack, which neither the replay nor the measurement sets
agcbuild_DcbWaitUntilSafeForRendering_a diff the console returns null and writes nothing for these arguments
agcbuild_DcbWaitUntilSafeForRendering_b diff the console returns null and writes nothing for these arguments
agcbuild_DcbWaitUntilSafeForRendering_c diff the console returns null and writes nothing for these arguments
kx_attr_getstack_default diff a 1 MiB default stack on purpose, see docs/dev/debt/libkernel.md
nh_escape_buffer_too_small diff a short buffer is left unfilled and the required size written; #12056 fills it as the console does
nh_ntop_v6 diff the console leaves stale bytes after the string, which are not modelled
1 agcbuild_AcbAtomicGds_a diff more than six arguments; the console's stack arguments are not known
17 agcbuild_AcbMemSemaphore_b throw the operation argument changes the control word; not modelled
18 agcbuild_AcbMemSemaphore_c throw the operation argument changes the control word; not modelled
19 agcbuild_AcbResetQueue_b diff the packet differs from the console's; not yet analysed
agcbuild_AcbSetFlip_a diff the console returns null and writes nothing for these arguments
agcbuild_AcbSetFlip_b diff the console returns null and writes nothing for these arguments
agcbuild_AcbSetFlip_c diff the console returns null and writes nothing for these arguments
agcbuild_AcbWaitUntilSafeForRendering_a diff the console returns null and writes nothing for these arguments
agcbuild_AcbWaitUntilSafeForRendering_b diff the console returns null and writes nothing for these arguments
agcbuild_AcbWaitUntilSafeForRendering_c diff the console returns null and writes nothing for these arguments
20 agcbuild_CbBranch_a diff more than six arguments; the console's stack arguments are not known
21 agcbuild_CbBranch_b diff more than six arguments; the console's stack arguments are not known
22 agcbuild_CbBranch_c diff more than six arguments; the console's stack arguments are not known
52 agcbuild_DcbMemSemaphore_b throw the operation argument changes the control word; not modelled
53 agcbuild_DcbMemSemaphore_c throw the operation argument changes the control word; not modelled
54 agcbuild_DcbResetQueue_b diff the packet differs from the console's; not yet analysed
agcbuild_DcbSetFlip_a diff the console returns null and writes nothing for these arguments
agcbuild_DcbSetFlip_b diff the console returns null and writes nothing for these arguments
agcbuild_DcbSetFlip_c diff the console returns null and writes nothing for these arguments
55 agcbuild_DcbWaitRegMem_b skip the ninth argument, pollCycles, comes from the caller's stack, which neither the replay nor the measurement sets
agcbuild_DcbWaitUntilSafeForRendering_a diff the console returns null and writes nothing for these arguments
agcbuild_DcbWaitUntilSafeForRendering_b diff the console returns null and writes nothing for these arguments
agcbuild_DcbWaitUntilSafeForRendering_c diff the console returns null and writes nothing for these arguments
56 kx_attr_getstack_default diff a 1 MiB default stack on purpose, see docs/dev/debt/libkernel.md
57 nh_escape_buffer_too_small diff a short buffer is left unfilled and the required size written; #12056 fills it as the console does
58 nh_ntop_v6 diff the console leaves stale bytes after the string, which are not modelled