From b86370285bf8de085202c84394d283c96be4ed9a Mon Sep 17 00:00:00 2001 From: Merijn Date: Thu, 8 Oct 2026 00:44:38 +0200 Subject: [PATCH] fix(kernel): throw on unsupported unwind tables in sceKernelGetModuleInfoForUnwind Once an image has .ehmeta, an eh_frame_hdr version other than 1, an eh_frame_ptr encoding other than absptr or pcrel, a 64-bit record length, an eh_frame_ptr outside the image or a missing terminator now throw instead of returning the empty answer. The test queries a fixture DLL whose .ehmeta it can break, since the unwinder reads the same section. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libkernel/CMakeLists.txt | 8 ++- core/libs/prx/libkernel/Module/src/Module.cpp | 20 +++--- core/libs/tests/GuestUnwindModuleInfo.cpp | 63 ++++++++++--------- .../tests/GuestUnwindModuleInfoFixture.cpp | 19 ++++++ .../tests/GuestUnwindModuleInfoFixture.hpp | 26 ++++++++ docs/dev/TechnicalDebt.md | 1 + 6 files changed, 98 insertions(+), 39 deletions(-) create mode 100644 core/libs/tests/GuestUnwindModuleInfoFixture.cpp create mode 100644 core/libs/tests/GuestUnwindModuleInfoFixture.hpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index cff639d7f..62fa131ed 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -294,12 +294,16 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() if(WIN32) + add_library(guest_unwind_module_info_fixture SHARED ${LIBS_INCLUDE_DIR}/tests/GuestUnwindModuleInfoFixture.cpp) + target_include_directories(guest_unwind_module_info_fixture PRIVATE ${LIBS_INCLUDE_DIR}) + target_compile_options(guest_unwind_module_info_fixture PRIVATE -fno-exceptions) add_test_executable(guest_unwind_module_info_tests ${LIBS_INCLUDE_DIR}/tests/GuestUnwindModuleInfo.cpp) target_include_directories(guest_unwind_module_info_tests PRIVATE ${LIBS_INCLUDE_DIR}) - target_link_libraries(guest_unwind_module_info_tests PRIVATE libkernel libc) + target_link_libraries(guest_unwind_module_info_tests PRIVATE libkernel libc guest_unwind_module_info_fixture) + configure_windows_unwind(guest_unwind_module_info_tests) add_test(NAME guest_unwind_module_info COMMAND guest_unwind_module_info_tests) set_tests_properties(guest_unwind_module_info PROPERTIES ENVIRONMENT_MODIFICATION - "PATH=path_list_prepend:$") + "PATH=path_list_prepend:$;PATH=path_list_prepend:$") endif() add_test_executable(guest_sync_on_address_tests ${LIBS_INCLUDE_DIR}/tests/GuestSyncOnAddress.cpp) target_include_directories(guest_sync_on_address_tests PRIVATE ${LIBS_INCLUDE_DIR}) diff --git a/core/libs/prx/libkernel/Module/src/Module.cpp b/core/libs/prx/libkernel/Module/src/Module.cpp index 64871e841..3bc8244d8 100644 --- a/core/libs/prx/libkernel/Module/src/Module.cpp +++ b/core/libs/prx/libkernel/Module/src/Module.cpp @@ -18,39 +18,43 @@ #ifdef _WIN32 namespace { std::uint64_t ReadEncoded(const std::uint8_t*& p, std::uint8_t encoding) { + const auto application = encoding & 0x70; + if ((encoding & 0x80) != 0 || (application != 0x00 && application != 0x10)) NotImplemented_nid_no_patch("sceKernelGetModuleInfoForUnwind eh_frame_ptr encoding other than absptr or pcrel"); std::uint64_t value = 0; const auto* at = p; switch (encoding & 0x0f) { case 0x03: { std::uint32_t v; std::memcpy(&v, p, 4); value = v; p += 4; break; } case 0x0b: { std::int32_t v; std::memcpy(&v, p, 4); value = static_cast(static_cast(v)); p += 4; break; } case 0x04: case 0x0c: std::memcpy(&value, p, 8); p += 8; break; - default: return 0; + default: NotImplemented_nid_no_patch("sceKernelGetModuleInfoForUnwind eh_frame_ptr value format"); } - if ((encoding & 0x70) == 0x10) value += reinterpret_cast(at); + if (application == 0x10) value += reinterpret_cast(at); return value; } void FillGuestUnwindInfo(const std::uint8_t* base, ModuleInfoForUnwind* info) { const auto* dos = reinterpret_cast(base); - if (dos->e_magic != IMAGE_DOS_SIGNATURE) return; + if (dos->e_magic != IMAGE_DOS_SIGNATURE) throw std::runtime_error("sceKernelGetModuleInfoForUnwind: image without a DOS header"); const auto* nt = reinterpret_cast(base + dos->e_lfanew); - if (nt->Signature != IMAGE_NT_SIGNATURE) return; + if (nt->Signature != IMAGE_NT_SIGNATURE) throw std::runtime_error("sceKernelGetModuleInfoForUnwind: image without an NT header"); const auto* sections = IMAGE_FIRST_SECTION(nt); for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i) { if (std::memcmp(sections[i].Name, ".ehmeta", 8) != 0) continue; std::uint32_t rva = 0; std::memcpy(&rva, base + sections[i].VirtualAddress, 4); const auto* header = base + rva; - if (header[0] != 1) return; + if (header[0] != 1) NotImplemented_nid_no_patch("sceKernelGetModuleInfoForUnwind eh_frame_hdr version other than 1"); const auto* p = header + 4; const auto frames = ReadEncoded(p, header[1]); - if (frames == 0) return; const auto* record = reinterpret_cast(frames); const auto* end = base + nt->OptionalHeader.SizeOfImage; - while (record + 4 <= end) { + if (record < base || record >= end) throw std::runtime_error("sceKernelGetModuleInfoForUnwind: eh_frame_ptr outside the image"); + for (;;) { + if (record + 4 > end) throw std::runtime_error("sceKernelGetModuleInfoForUnwind: eh_frame has no terminator inside the image"); std::uint32_t length = 0; std::memcpy(&length, record, 4); - if (length == 0 || length == 0xffffffffu) break; + if (length == 0) break; + if (length == 0xffffffffu) NotImplemented_nid_no_patch("sceKernelGetModuleInfoForUnwind eh_frame record with a 64-bit length"); record += 4 + length; } info->eh_frame_hdr_addr = reinterpret_cast(header); diff --git a/core/libs/tests/GuestUnwindModuleInfo.cpp b/core/libs/tests/GuestUnwindModuleInfo.cpp index 4c1ea5ec9..b5af722f4 100644 --- a/core/libs/tests/GuestUnwindModuleInfo.cpp +++ b/core/libs/tests/GuestUnwindModuleInfo.cpp @@ -1,52 +1,57 @@ #include "SceTypes.hpp" +#include "tests/GuestUnwindModuleInfoFixture.hpp" #include #include -#include +#include #include extern "C" int APS5_VABI sceKernelGetModuleInfoForUnwind(uint64_t addr, int flags, ModuleInfoForUnwind* info); static void Require(bool value) { if (!value) std::abort(); } -__attribute__((section(".ehmeta"), used)) static volatile std::uint32_t ehMeta = 0; - -struct alignas(4) FrameHeader { - std::uint8_t version = 1; - std::uint8_t framePointerEncoding = 0x1b; - std::uint8_t countEncoding = 0x03; - std::uint8_t tableEncoding = 0x3b; - std::int32_t frames = 0; - std::uint32_t count = 0; -}; - -struct alignas(4) Frames { - std::uint32_t cieLength = 12; - std::uint32_t cieId = 0; - std::uint8_t cie[8] = {1, 0, 1, 0x78, 16, 0, 0, 0}; - std::uint32_t terminator = 0; -}; - -static FrameHeader header; -static Frames frames; +static bool Throws(std::uint64_t address) { + ModuleInfoForUnwind info{}; + try { + sceKernelGetModuleInfoForUnwind(address, 0, &info); + } catch (const std::runtime_error&) { + return true; + } + return false; +} int main() { - const auto base = reinterpret_cast(GetModuleHandleW(nullptr)); - header.frames = static_cast(reinterpret_cast(&frames) - reinterpret_cast(&header.frames)); - DWORD old = 0; - Require(VirtualProtect(const_cast(&ehMeta), sizeof(ehMeta), PAGE_READWRITE, &old)); - ehMeta = static_cast(reinterpret_cast(&header) - base); + auto* fixture = GetUnwindFixture(); + Require(fixture != nullptr); + const auto address = reinterpret_cast(fixture); + MEMORY_BASIC_INFORMATION memory{}; + Require(VirtualQuery(reinterpret_cast(address), &memory, sizeof(memory)) != 0); + const auto base = reinterpret_cast(memory.AllocationBase); + Require(base != reinterpret_cast(GetModuleHandleW(nullptr))); ModuleInfoForUnwind info{}; - Require(sceKernelGetModuleInfoForUnwind(reinterpret_cast(&main), 0, &info) == 0); + Require(sceKernelGetModuleInfoForUnwind(address, 0, &info) == 0); Require(info.st_size == sizeof(ModuleInfoForUnwind)); - Require(info.eh_frame_hdr_addr == reinterpret_cast(&header)); - Require(info.eh_frame_addr == reinterpret_cast(&frames)); + Require(info.eh_frame_hdr_addr == reinterpret_cast(&fixture->header)); + Require(info.eh_frame_addr == reinterpret_cast(&fixture->frames)); Require(info.eh_frame_size == 4 + 12); Require(info.seg0_addr == base); const auto* dos = reinterpret_cast(base); const auto* nt = reinterpret_cast(base + dos->e_lfanew); Require(info.seg0_size == nt->OptionalHeader.SizeOfImage); + fixture->header.version = 2; + Require(Throws(address)); + fixture->header.version = 1; + for (const std::uint8_t encoding : {0x3b, 0x9b, 0x0f, 0xff}) { + fixture->header.framePointerEncoding = encoding; + Require(Throws(address)); + } + fixture->header.framePointerEncoding = 0x1b; + fixture->frames.cieLength = 0xffffffffu; + Require(Throws(address)); + fixture->frames.cieLength = 12; + Require(!Throws(address)); + ModuleInfoForUnwind host{}; Require(sceKernelGetModuleInfoForUnwind(reinterpret_cast(&GetModuleHandleW), 0, &host) == 0); Require(host.eh_frame_hdr_addr == 0 && host.eh_frame_addr == 0 && host.eh_frame_size == 0); diff --git a/core/libs/tests/GuestUnwindModuleInfoFixture.cpp b/core/libs/tests/GuestUnwindModuleInfoFixture.cpp new file mode 100644 index 000000000..f1c75003e --- /dev/null +++ b/core/libs/tests/GuestUnwindModuleInfoFixture.cpp @@ -0,0 +1,19 @@ +#include "tests/GuestUnwindModuleInfoFixture.hpp" + +#include +#include + +__attribute__((section(".ehmeta"), used)) static volatile std::uint32_t ehMeta = 0; + +extern "C" IMAGE_DOS_HEADER __ImageBase; + +static UnwindFixture fixture; + +extern "C" __declspec(dllexport) UnwindFixture* GetUnwindFixture() { + const auto base = reinterpret_cast(&__ImageBase); + fixture.header.frames = static_cast(reinterpret_cast(&fixture.frames) - reinterpret_cast(&fixture.header.frames)); + DWORD old = 0; + if (!VirtualProtect(const_cast(&ehMeta), sizeof(ehMeta), PAGE_READWRITE, &old)) return nullptr; + ehMeta = static_cast(reinterpret_cast(&fixture.header) - base); + return &fixture; +} diff --git a/core/libs/tests/GuestUnwindModuleInfoFixture.hpp b/core/libs/tests/GuestUnwindModuleInfoFixture.hpp new file mode 100644 index 000000000..d03be890d --- /dev/null +++ b/core/libs/tests/GuestUnwindModuleInfoFixture.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include + +struct alignas(4) UnwindFixtureHeader { + std::uint8_t version = 1; + std::uint8_t framePointerEncoding = 0x1b; + std::uint8_t countEncoding = 0x03; + std::uint8_t tableEncoding = 0x3b; + std::int32_t frames = 0; + std::uint32_t count = 0; +}; + +struct alignas(4) UnwindFixtureFrames { + std::uint32_t cieLength = 12; + std::uint32_t cieId = 0; + std::uint8_t cie[8] = {1, 0, 1, 0x78, 16, 0, 0, 0}; + std::uint32_t terminator = 0; +}; + +struct UnwindFixture { + UnwindFixtureHeader header; + UnwindFixtureFrames frames; +}; + +extern "C" UnwindFixture* GetUnwindFixture(); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 084451064..eb9c16f9f 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -93,6 +93,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceAgcDcbPrimeUtcl2](../../core/libs/prx/libSceAgc/DcbState/src/Display.cpp) (libSceAgc) - assumed to write the packet `sceAgcAcbPrimeUtcl2` writes: PRIME_UTCL2 has the same 5 dwords on the graphics and compute rings. Not confirmed on a title. Both write a NOP of that size, as there is no TLB to prime - [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) - [sceKernelInternalMemoryGetModuleSegmentInfo](../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - unknown signature +- [sceKernelGetModuleInfoForUnwind](../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - `eh_frame_size` is assumed to cover the records up to, not including, the zero terminator. LLVM libunwind's `CFI_Parser::findFDE` stops at the terminator or the length, whichever comes first, so it finds the same FDEs either way; what the console reports is not known. - [sceKernelGetModuleInfoFromAddr](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x1A8-byte info layout follows the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation; a title's `libc.prx` reads only `id` (PPSA14632). The meaning of `flags` is unknown; only 2 is accepted, as is only the full `st_size`. The id is the `sceKernelLoadStartModule` handle of an image opened through the loader and otherwise a stable id from the same range. The name is the host file name without `.guest.prx`. An image with more than 4 load segments (the converted executable has 7) reports its first 4. `tls_offset` is 0 and `ref_count` is 1, as the host loader exposes neither. Not implemented on Windows - [sceKernelReleaseFlexibleMemory](../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - unmaps the range like sceKernelMunmap, as fpPS4 and prosper do; whether the console keeps the virtual range reserved after releasing its pages is unverified - [Memory protection bits 0x100 and 0x200](../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - taken as audio coprocessor (ACP) read and write, as in the [KytyPS5](https://github.com/KytyPS5/KytyPS5) and [etaHEN](https://github.com/etaHEN/etaHEN) headers; the coprocessor work runs on the host CPU, so they grant host read and read/write access like the GPU bits