fix(libSceAmpr): record Nop as n dwords with a one-dword header #10936

Merged
AdnanKhan-ak47 merged 4 commits from upstream-pr/10936 into main 2026-10-27 08:15:40 +00:00
AdnanKhan-ak47 commented 2026-10-25 11:06:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AdnanKhan-ak47 on 2026-10-25T11:06:32Z.


What

sceAmprCommandBufferNop(n) now records n dwords and sceAmprCommandBufferNopWithData(n) (n + 1) dwords, as on the console, instead of 8 + 4n bytes. docs/dev/debt/libSceAmpr.md recorded the consequence: a title padding to an exact offset or to the end of its buffer with Nop(remaining / 4) got SCE_KERNEL_ERROR_EBUSY or a shifted offset.

The record is one dword of header followed by the payload. The header word is the console's, as observed by BunnyFl from firmware 13.42's libSceAmpr.sprx in this thread: 0x5452000F | payloadDwords << 8 | (type & 0xF) << 12, so Nop(1) writes 0x5452000F, Nop(16) 0x54520F0F, NopWithData(n) 0x5452000F | n << 8 and ConstructNop carries the low four bits of type (Apr::NopHeader, IsNopHeader, NopDwords, NopHeaderBytes in AprCommandBuffer.hpp). The executor recognises the record by (word & 0xFFFF00FF) == 0x5452000F before the 8-byte header guard; the low byte alone would collide with ReadFileGatherScatter = 15, and no 8-byte header carries 0x5452 in its upper half. A 4-byte Nop(1) at the end of a buffer is valid, a record past the buffer end is APR: malformed command, fewer than 4 remaining bytes APR: truncated command buffer, and the old 8-byte {Nop, bytes} record is APR: unknown opcode 0. A null base is reported (EPERM) before the count or payload range, as on the console. Nop(n) writes only the header and leaves the other n - 1 dwords as they were. ConstructNop writes its payload after the 4-byte header. The measure functions return 4n for both, keeping MeasureCommandSizeNopWithData's argument as the total dword count the existing tests use.

ConstructNop writes the word before the payload and zeroes the payload's padding, as in BunnyFl's dump of ConstructNop(buffer, 7, {1,2,3,4,5}, 5, &0xCAFEF00D) (0f 73 52 54 | 0d f0 fe ca | 01 02 03 04 | 05 00 00 00). One console behaviour is deliberately not reproduced and is recorded in docs/dev/debt/libSceAmpr.md: NopWithData with data and ConstructNop write four zero bytes right after the payload even where they fall past the end of the record and of the buffer; AnyPS5 stops at the record.

GuestAmpr.cpp: RequireAppended checks the dword header for Nop; TestNops pins the header word for Nop(1..16), NopWithData(0) and NopWithData(3), that Nop(n) leaves the n - 1 dwords after the header untouched, and that a buffer without base returns EPERM for out-of-range counts; TestConstructed pins 0x5452730F, the word at +4, the payload at +8 and zeroed padding at +13..15 for type 7 with a word and 5 payload bytes over pre-filled memory, with the byte past the record untouched, and 0x5452510F for type 0x15 with one payload dword; TestNopPadding pins Nop(1) = 4, Nop(16) = 64, fills a 64-byte buffer to exactly its end with Nop(remaining / 4) after a marker and a write, checks that one dword more is EBUSY before and after, submits it, and submits a buffer where a 4-byte Nop misaligns the following 8-byte-header command; TestMalformedCommandSizes covers the Nop count 0, count overflow and 3-byte-tail failures and keeps the header-record cases on PopMarker.

Tested

Arch Linux x86-64, GCC 16.2.1, head 0d14468ccc: ctest -R 'guest_ampr|guest_amm|guest_apr' 9/9 pass (guest_ampr_markers, guest_ampr_threads, guest_ampr_cpp_error, guest_amm_placement, 5 × guest_apr_read_*). With ada5ad0990's Export.cpp restored, guest_ampr_tests fails at the padding check (GuestAmpr.cpp:540); with the head before that restored, at the Nop header check (GuestAmpr.cpp:68); with main's Export.cpp and Apr.cpp restored it aborts at the same check. tools/check_conventions.py --base origin/main and tools/pr_overlap.py clean. Not run against a title; the header word, sizes, ranges and error order match BunnyFl's emulator run of firmware 13.42 in this thread.

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
  • 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:5004153249 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10936). Originally posted by [AdnanKhan-ak47](https://github.com/AdnanKhan-ak47) on 2026-10-25T11:06:32Z. --- ### What `sceAmprCommandBufferNop(n)` now records n dwords and `sceAmprCommandBufferNopWithData(n)` (n + 1) dwords, as on the console, instead of 8 + 4n bytes. `docs/dev/debt/libSceAmpr.md` recorded the consequence: a title padding to an exact offset or to the end of its buffer with `Nop(remaining / 4)` got `SCE_KERNEL_ERROR_EBUSY` or a shifted offset. The record is one dword of header followed by the payload. The header word is the console's, as observed by BunnyFl from firmware 13.42's `libSceAmpr.sprx` in this thread: `0x5452000F | payloadDwords << 8 | (type & 0xF) << 12`, so `Nop(1)` writes `0x5452000F`, `Nop(16)` `0x54520F0F`, `NopWithData(n)` `0x5452000F | n << 8` and `ConstructNop` carries the low four bits of `type` (`Apr::NopHeader`, `IsNopHeader`, `NopDwords`, `NopHeaderBytes` in `AprCommandBuffer.hpp`). The executor recognises the record by `(word & 0xFFFF00FF) == 0x5452000F` before the 8-byte header guard; the low byte alone would collide with `ReadFileGatherScatter = 15`, and no 8-byte header carries `0x5452` in its upper half. A 4-byte `Nop(1)` at the end of a buffer is valid, a record past the buffer end is `APR: malformed command`, fewer than 4 remaining bytes `APR: truncated command buffer`, and the old 8-byte `{Nop, bytes}` record is `APR: unknown opcode 0`. A null `base` is reported (`EPERM`) before the count or payload range, as on the console. `Nop(n)` writes only the header and leaves the other n - 1 dwords as they were. `ConstructNop` writes its payload after the 4-byte header. The measure functions return 4n for both, keeping `MeasureCommandSizeNopWithData`'s argument as the total dword count the existing tests use. `ConstructNop` writes the word before the payload and zeroes the payload's padding, as in BunnyFl's dump of `ConstructNop(buffer, 7, {1,2,3,4,5}, 5, &0xCAFEF00D)` (`0f 73 52 54 | 0d f0 fe ca | 01 02 03 04 | 05 00 00 00`). One console behaviour is deliberately not reproduced and is recorded in `docs/dev/debt/libSceAmpr.md`: `NopWithData` with data and `ConstructNop` write four zero bytes right after the payload even where they fall past the end of the record and of the buffer; AnyPS5 stops at the record. `GuestAmpr.cpp`: `RequireAppended` checks the dword header for Nop; `TestNops` pins the header word for `Nop(1..16)`, `NopWithData(0)` and `NopWithData(3)`, that `Nop(n)` leaves the n - 1 dwords after the header untouched, and that a buffer without `base` returns `EPERM` for out-of-range counts; `TestConstructed` pins `0x5452730F`, the word at +4, the payload at +8 and zeroed padding at +13..15 for type 7 with a word and 5 payload bytes over pre-filled memory, with the byte past the record untouched, and `0x5452510F` for type `0x15` with one payload dword; `TestNopPadding` pins `Nop(1) = 4`, `Nop(16) = 64`, fills a 64-byte buffer to exactly its end with `Nop(remaining / 4)` after a marker and a write, checks that one dword more is `EBUSY` before and after, submits it, and submits a buffer where a 4-byte Nop misaligns the following 8-byte-header command; `TestMalformedCommandSizes` covers the Nop count 0, count overflow and 3-byte-tail failures and keeps the header-record cases on `PopMarker`. ### Tested Arch Linux x86-64, GCC 16.2.1, head `0d14468ccc`: `ctest -R 'guest_ampr|guest_amm|guest_apr'` 9/9 pass (`guest_ampr_markers`, `guest_ampr_threads`, `guest_ampr_cpp_error`, `guest_amm_placement`, 5 × `guest_apr_read_*`). With `ada5ad0990`'s `Export.cpp` restored, `guest_ampr_tests` fails at the padding check (`GuestAmpr.cpp:540`); with the head before that restored, at the Nop header check (`GuestAmpr.cpp:68`); with `main`'s `Export.cpp` and `Apr.cpp` restored it aborts at the same check. `tools/check_conventions.py --base origin/main` and `tools/pr_overlap.py` clean. Not run against a title; the header word, sizes, ranges and error order match BunnyFl's emulator run of firmware 13.42 in this thread. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) - [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
MarcZX10 commented 2026-10-25 11:22:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:22:27Z.

Review: APPROVED


Reviewed exact head 0924ce7. The one-dword NOP header carries its total dword count; append, construct, measure, execute, and malformed-record checks are internally consistent. Boundary coverage includes exact-buffer padding and odd alignment before another command. Local diff and conventions checks passed. Hosted policy checks pass and the author reports nine focused runtime tests pass on Linux; I did not rerun the AMPR suite locally. The debt entry now distinguishes NOP from the remaining emulator command format.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642904247 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10936#pullrequestreview-5642904247). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:22:27Z. **Review:** APPROVED --- Reviewed exact head 0924ce7. The one-dword NOP header carries its total dword count; append, construct, measure, execute, and malformed-record checks are internally consistent. Boundary coverage includes exact-buffer padding and odd alignment before another command. Local diff and conventions checks passed. Hosted policy checks pass and the author reports nine focused runtime tests pass on Linux; I did not rerun the AMPR suite locally. The debt entry now distinguishes NOP from the remaining emulator command format.
thepoppfish commented 2026-10-25 11:25:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thepoppfish on 2026-10-25T11:25:46Z.

Review: COMMENTED


Additional recorder-only native Windows validation of exact head 0924ce7c38d0e3e3b88c215a6e238bdc3f907ec4, adding a platform not in the author's Linux test report. This is comment-only because the APR executor and complete GuestAmpr test were not run in this harness.

Windows 11 build 26200, MSYS2 UCRT64 GCC 16.2.0-6, CMake 4.3.1, Ninja 1.13.2, Release -O3 -DNDEBUG with LTO:

  • Built the actual unmodified libSceAmpr/Export.cpp and actual public headers with an external recorder probe. No replacement recorder functions, kernel adapters or behavior stubs. LTO discards unrelated exports; the first non-LTO standalone link needed those unrelated library dependencies and was a setup failure, not a PR defect.
  • 326 recorder scenarios pass, covering Nop 0..17, NopWithData 0..16 with real/null payloads, and ConstructNop byte lengths 0..61 with/without the optional word. Checks include the literal one-dword header, payload bytes and untouched padding/canaries, offset/command counts, valid size queries, exact-capacity writes, one-byte-short refusals, invalid counts, and null/unbound buffer errors. Failed operations must leave bytes and counts unchanged.
  • The same probe, headers, compiler and flags with only the recorder source restored from base b91a6a60c5abe08be4a315cf2e1f42c11f414564 builds but exits 1 at the recorder contract assertion, as expected. This is a runtime negative control, not a link failure.
  • Conventions against fetched main 16ff208dc32cf3927ba1bae492eee363a5ae7ae8 and whitespace pass. A tree-only merge with that main is clean (tree 5277cd98643c17ec000b22887ba8dfb93c5cd8a9); it was not built/tested as a merged tree.

This checks AnyPS5's internal record format and producer bounds. It does not independently validate PS5 wire encoding or console behavior, execute APR commands, run the full submitted GuestAmpr fixture, build shared libraries/full runtime, exercise a title/GPU, or certify hosted CI. The author checkout remains clean and unchanged. AI-assisted: yes, OpenAI Codex.

Native probe replay

Save the two files below outside the checkout. With GCC 16.2/CMake/Ninja on PATH and the checkout at the reviewed head:

cmake -S <harness> -B <harness>/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DAMPR_REPO=<checkout>
cmake --build <harness>/build
ctest --test-dir <harness>/build --output-on-failure -V

For the negative control, save git show b91a6a60c5abe08be4a315cf2e1f42c11f414564:core/libs/prx/libSceAmpr/Export.cpp as BaselineExport.cpp outside the checkout. Configure a separate build with the same arguments plus -DAMPR_SOURCE=<harness>/BaselineExport.cpp; its probe should exit 1 with native Ampr recorder contract failed.

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)
project(AmprWriterReview LANGUAGES CXX)
enable_testing()
set(CMAKE_CXX_STANDARD 20)
get_filename_component(AMPR_DEFAULT_REPO "${CMAKE_CURRENT_SOURCE_DIR}/../review-10936" ABSOLUTE)
set(AMPR_REPO "${AMPR_DEFAULT_REPO}" CACHE PATH "Exact PR checkout")
set(AMPR_SOURCE "${AMPR_REPO}/core/libs/prx/libSceAmpr/Export.cpp" CACHE FILEPATH "Production recorder source")
add_executable(ampr_writer_probe WriterProbe.cpp "${AMPR_SOURCE}")
set_property(TARGET ampr_writer_probe PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
target_include_directories(ampr_writer_probe PRIVATE "${AMPR_REPO}/core/libs")
target_compile_options(ampr_writer_probe PRIVATE -ffunction-sections -fdata-sections)
target_link_options(ampr_writer_probe PRIVATE -Wl,--gc-sections)
add_test(NAME ampr_writer_probe COMMAND ampr_writer_probe)
set_tests_properties(ampr_writer_probe PROPERTIES TIMEOUT 10)

WriterProbe.cpp

#include "prx/libSceAmpr/include/Api.hpp"
#include <array>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <stdexcept>

namespace {
constexpr int Invalid = static_cast<int>(0x80020016u);
constexpr int Busy = static_cast<int>(0x80020010u);
constexpr int Permission = static_cast<int>(0x80020001u);
std::uint32_t cases = 0;

void Require(bool condition) {
    if (!condition) throw std::runtime_error("native Ampr recorder contract failed");
}

struct Recorder {
    std::array<std::uint8_t, 128> bytes;
    Apr::CommandBufferObject buffer{Apr::BufferType::Generic, 0, 4, 7, 68, nullptr};
    Recorder() {
        bytes.fill(0xa5);
        buffer.base = bytes.data() + 8;
    }
};

void CheckPacket(const Recorder& recorder, std::uint32_t dwords, const void* payload, std::uint32_t payloadBytes, const std::uint32_t* word = nullptr) {
    auto expected = Recorder().bytes;
    const std::uint32_t header = dwords << 8;
    std::memcpy(expected.data() + 12, &header, 4);
    std::uint32_t cursor = 16;
    if (word) {
        std::memcpy(expected.data() + cursor, word, 4);
        cursor += 4;
    }
    if (payloadBytes) std::memcpy(expected.data() + cursor, payload, payloadBytes);
    Require(recorder.bytes == expected);
    Require(recorder.buffer.offset == 4 + dwords * 4 && recorder.buffer.numCommands == 8);
    ++cases;
}

void CheckUnchanged(const Recorder& recorder) {
    Require(recorder.bytes == Recorder().bytes);
    Require(recorder.buffer.offset == 4 && recorder.buffer.numCommands == 7);
    ++cases;
}

void CheckNop() {
    for (std::uint32_t n = 0; n <= 17; ++n) {
        Recorder recorder;
        const int result = sceAmprCommandBufferNop(&recorder.buffer, n);
        if (n == 0 || n > 16) {
            Require(result == Invalid);
            CheckUnchanged(recorder);
            Require(sceAmprMeasureCommandSizeNop(n) == static_cast<std::uint32_t>(Invalid));
        } else {
            Require(result == 0);
            CheckPacket(recorder, n, nullptr, 0);
            Require(sceAmprMeasureCommandSizeNop(n) == 4u * n);
            Recorder shortBuffer;
            shortBuffer.buffer.size = 4 + n * 4 - 1;
            Require(sceAmprCommandBufferNop(&shortBuffer.buffer, n) == Busy);
            CheckUnchanged(shortBuffer);
        }
    }
    Require(sceAmprCommandBufferNop(nullptr, 1) == Invalid);
    Recorder unbound;
    unbound.buffer.base = nullptr;
    Require(sceAmprCommandBufferNop(&unbound.buffer, 1) == Permission);
    CheckUnchanged(unbound);
}

void CheckWithData() {
    std::array<std::uint32_t, 16> payload;
    for (std::uint32_t i = 0; i < payload.size(); ++i) payload[i] = 0x10203040u + i;
    for (std::uint32_t n = 0; n <= 16; ++n) {
        Recorder recorder;
        const int result = sceAmprCommandBufferNopWithData(&recorder.buffer, n, payload.data());
        if (n == 16) {
            Require(result == Invalid);
            CheckUnchanged(recorder);
        } else {
            Require(result == 0);
            CheckPacket(recorder, n + 1, payload.data(), 4u * n);
            Recorder missingPayload;
            Require(sceAmprCommandBufferNopWithData(&missingPayload.buffer, n, nullptr) == 0);
            CheckPacket(missingPayload, n + 1, nullptr, 0);
            Require(sceAmprMeasureCommandSizeNopWithData(n + 1) == 4u * (n + 1));
            Recorder shortBuffer;
            shortBuffer.buffer.size = 4 + (n + 1) * 4 - 1;
            Require(sceAmprCommandBufferNopWithData(&shortBuffer.buffer, n, payload.data()) == Busy);
            CheckUnchanged(shortBuffer);
        }
    }
    Require(sceAmprMeasureCommandSizeNopWithData(0) == static_cast<std::uint32_t>(Invalid));
    Require(sceAmprMeasureCommandSizeNopWithData(17) == static_cast<std::uint32_t>(Invalid));
}

void CheckConstruct() {
    std::array<std::uint8_t, 64> payload;
    for (std::uint32_t i = 0; i < payload.size(); ++i) payload[i] = static_cast<std::uint8_t>(i + 17);
    const std::uint32_t word = 0x12345678;
    for (const bool extraWord : {false, true}) {
        for (std::uint32_t size = 0; size <= 61; ++size) {
            Recorder recorder;
            const int result = sceAmprCommandBufferConstructNop(&recorder.buffer, 0, payload.data(), size, extraWord ? &word : nullptr);
            if (size > (extraWord ? 56u : 60u)) {
                Require(result == Invalid);
                CheckUnchanged(recorder);
            } else {
                const std::uint32_t dwords = 1 + (size + 3) / 4 + (extraWord ? 1 : 0);
                Require(result == 0);
                CheckPacket(recorder, dwords, payload.data(), size, extraWord ? &word : nullptr);
                Recorder shortBuffer;
                shortBuffer.buffer.size = 4 + dwords * 4 - 1;
                Require(sceAmprCommandBufferConstructNop(&shortBuffer.buffer, 0, payload.data(), size, extraWord ? &word : nullptr) == Busy);
                CheckUnchanged(shortBuffer);
            }
        }
    }
}
}

int main() {
    try {
        CheckNop();
        CheckWithData();
        CheckConstruct();
        std::cout << cases << " recorder scenarios pass\n";
    } catch (const std::exception& error) {
        std::cerr << error.what() << '\n';
        return 1;
    }
}

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5642924931 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10936#pullrequestreview-5642924931). Originally posted by [thepoppfish](https://github.com/thepoppfish) on 2026-10-25T11:25:46Z. **Review:** COMMENTED --- Additional **recorder-only native Windows validation** of exact head `0924ce7c38d0e3e3b88c215a6e238bdc3f907ec4`, adding a platform not in the author's Linux test report. This is comment-only because the APR executor and complete GuestAmpr test were not run in this harness. Windows 11 build 26200, MSYS2 UCRT64 GCC 16.2.0-6, CMake 4.3.1, Ninja 1.13.2, Release `-O3 -DNDEBUG` with LTO: - Built the actual unmodified `libSceAmpr/Export.cpp` and actual public headers with an external recorder probe. No replacement recorder functions, kernel adapters or behavior stubs. LTO discards unrelated exports; the first non-LTO standalone link needed those unrelated library dependencies and was a setup failure, not a PR defect. - **326 recorder scenarios pass**, covering Nop 0..17, NopWithData 0..16 with real/null payloads, and ConstructNop byte lengths 0..61 with/without the optional word. Checks include the literal one-dword header, payload bytes and untouched padding/canaries, offset/command counts, valid size queries, exact-capacity writes, one-byte-short refusals, invalid counts, and null/unbound buffer errors. Failed operations must leave bytes and counts unchanged. - The same probe, headers, compiler and flags with only the recorder source restored from base `b91a6a60c5abe08be4a315cf2e1f42c11f414564` builds but exits 1 at the recorder contract assertion, as expected. This is a runtime negative control, not a link failure. - Conventions against fetched main `16ff208dc32cf3927ba1bae492eee363a5ae7ae8` and whitespace pass. A tree-only merge with that main is clean (tree `5277cd98643c17ec000b22887ba8dfb93c5cd8a9`); it was not built/tested as a merged tree. This checks AnyPS5's internal record format and producer bounds. It does **not** independently validate PS5 wire encoding or console behavior, execute APR commands, run the full submitted GuestAmpr fixture, build shared libraries/full runtime, exercise a title/GPU, or certify hosted CI. The author checkout remains clean and unchanged. AI-assisted: yes, OpenAI Codex. <details><summary>Native probe replay</summary> Save the two files below outside the checkout. With GCC 16.2/CMake/Ninja on PATH and the checkout at the reviewed head: ```text cmake -S <harness> -B <harness>/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DAMPR_REPO=<checkout> cmake --build <harness>/build ctest --test-dir <harness>/build --output-on-failure -V ``` For the negative control, save `git show b91a6a60c5abe08be4a315cf2e1f42c11f414564:core/libs/prx/libSceAmpr/Export.cpp` as `BaselineExport.cpp` outside the checkout. Configure a separate build with the same arguments plus `-DAMPR_SOURCE=<harness>/BaselineExport.cpp`; its probe should exit 1 with `native Ampr recorder contract failed`. `CMakeLists.txt` ```cmake cmake_minimum_required(VERSION 3.20) project(AmprWriterReview LANGUAGES CXX) enable_testing() set(CMAKE_CXX_STANDARD 20) get_filename_component(AMPR_DEFAULT_REPO "${CMAKE_CURRENT_SOURCE_DIR}/../review-10936" ABSOLUTE) set(AMPR_REPO "${AMPR_DEFAULT_REPO}" CACHE PATH "Exact PR checkout") set(AMPR_SOURCE "${AMPR_REPO}/core/libs/prx/libSceAmpr/Export.cpp" CACHE FILEPATH "Production recorder source") add_executable(ampr_writer_probe WriterProbe.cpp "${AMPR_SOURCE}") set_property(TARGET ampr_writer_probe PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) target_include_directories(ampr_writer_probe PRIVATE "${AMPR_REPO}/core/libs") target_compile_options(ampr_writer_probe PRIVATE -ffunction-sections -fdata-sections) target_link_options(ampr_writer_probe PRIVATE -Wl,--gc-sections) add_test(NAME ampr_writer_probe COMMAND ampr_writer_probe) set_tests_properties(ampr_writer_probe PROPERTIES TIMEOUT 10) ``` `WriterProbe.cpp` ```cpp #include "prx/libSceAmpr/include/Api.hpp" #include <array> #include <cstdint> #include <cstring> #include <iostream> #include <stdexcept> namespace { constexpr int Invalid = static_cast<int>(0x80020016u); constexpr int Busy = static_cast<int>(0x80020010u); constexpr int Permission = static_cast<int>(0x80020001u); std::uint32_t cases = 0; void Require(bool condition) { if (!condition) throw std::runtime_error("native Ampr recorder contract failed"); } struct Recorder { std::array<std::uint8_t, 128> bytes; Apr::CommandBufferObject buffer{Apr::BufferType::Generic, 0, 4, 7, 68, nullptr}; Recorder() { bytes.fill(0xa5); buffer.base = bytes.data() + 8; } }; void CheckPacket(const Recorder& recorder, std::uint32_t dwords, const void* payload, std::uint32_t payloadBytes, const std::uint32_t* word = nullptr) { auto expected = Recorder().bytes; const std::uint32_t header = dwords << 8; std::memcpy(expected.data() + 12, &header, 4); std::uint32_t cursor = 16; if (word) { std::memcpy(expected.data() + cursor, word, 4); cursor += 4; } if (payloadBytes) std::memcpy(expected.data() + cursor, payload, payloadBytes); Require(recorder.bytes == expected); Require(recorder.buffer.offset == 4 + dwords * 4 && recorder.buffer.numCommands == 8); ++cases; } void CheckUnchanged(const Recorder& recorder) { Require(recorder.bytes == Recorder().bytes); Require(recorder.buffer.offset == 4 && recorder.buffer.numCommands == 7); ++cases; } void CheckNop() { for (std::uint32_t n = 0; n <= 17; ++n) { Recorder recorder; const int result = sceAmprCommandBufferNop(&recorder.buffer, n); if (n == 0 || n > 16) { Require(result == Invalid); CheckUnchanged(recorder); Require(sceAmprMeasureCommandSizeNop(n) == static_cast<std::uint32_t>(Invalid)); } else { Require(result == 0); CheckPacket(recorder, n, nullptr, 0); Require(sceAmprMeasureCommandSizeNop(n) == 4u * n); Recorder shortBuffer; shortBuffer.buffer.size = 4 + n * 4 - 1; Require(sceAmprCommandBufferNop(&shortBuffer.buffer, n) == Busy); CheckUnchanged(shortBuffer); } } Require(sceAmprCommandBufferNop(nullptr, 1) == Invalid); Recorder unbound; unbound.buffer.base = nullptr; Require(sceAmprCommandBufferNop(&unbound.buffer, 1) == Permission); CheckUnchanged(unbound); } void CheckWithData() { std::array<std::uint32_t, 16> payload; for (std::uint32_t i = 0; i < payload.size(); ++i) payload[i] = 0x10203040u + i; for (std::uint32_t n = 0; n <= 16; ++n) { Recorder recorder; const int result = sceAmprCommandBufferNopWithData(&recorder.buffer, n, payload.data()); if (n == 16) { Require(result == Invalid); CheckUnchanged(recorder); } else { Require(result == 0); CheckPacket(recorder, n + 1, payload.data(), 4u * n); Recorder missingPayload; Require(sceAmprCommandBufferNopWithData(&missingPayload.buffer, n, nullptr) == 0); CheckPacket(missingPayload, n + 1, nullptr, 0); Require(sceAmprMeasureCommandSizeNopWithData(n + 1) == 4u * (n + 1)); Recorder shortBuffer; shortBuffer.buffer.size = 4 + (n + 1) * 4 - 1; Require(sceAmprCommandBufferNopWithData(&shortBuffer.buffer, n, payload.data()) == Busy); CheckUnchanged(shortBuffer); } } Require(sceAmprMeasureCommandSizeNopWithData(0) == static_cast<std::uint32_t>(Invalid)); Require(sceAmprMeasureCommandSizeNopWithData(17) == static_cast<std::uint32_t>(Invalid)); } void CheckConstruct() { std::array<std::uint8_t, 64> payload; for (std::uint32_t i = 0; i < payload.size(); ++i) payload[i] = static_cast<std::uint8_t>(i + 17); const std::uint32_t word = 0x12345678; for (const bool extraWord : {false, true}) { for (std::uint32_t size = 0; size <= 61; ++size) { Recorder recorder; const int result = sceAmprCommandBufferConstructNop(&recorder.buffer, 0, payload.data(), size, extraWord ? &word : nullptr); if (size > (extraWord ? 56u : 60u)) { Require(result == Invalid); CheckUnchanged(recorder); } else { const std::uint32_t dwords = 1 + (size + 3) / 4 + (extraWord ? 1 : 0); Require(result == 0); CheckPacket(recorder, dwords, payload.data(), size, extraWord ? &word : nullptr); Recorder shortBuffer; shortBuffer.buffer.size = 4 + dwords * 4 - 1; Require(sceAmprCommandBufferConstructNop(&shortBuffer.buffer, 0, payload.data(), size, extraWord ? &word : nullptr) == Busy); CheckUnchanged(shortBuffer); } } } } } int main() { try { CheckNop(); CheckWithData(); CheckConstruct(); std::cout << cases << " recorder scenarios pass\n"; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; } } ``` </details>
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!10936
No description provided.