mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -294,12 +294,16 @@ if(BUILD_TESTING)
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
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:$<TARGET_FILE_DIR:libkernel>")
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>;PATH=path_list_prepend:$<TARGET_FILE_DIR:guest_unwind_module_info_fixture>")
|
||||
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})
|
||||
|
||||
@@ -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<std::uint64_t>(static_cast<std::int64_t>(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<std::uint64_t>(at);
|
||||
if (application == 0x10) value += reinterpret_cast<std::uint64_t>(at);
|
||||
return value;
|
||||
}
|
||||
|
||||
void FillGuestUnwindInfo(const std::uint8_t* base, ModuleInfoForUnwind* info) {
|
||||
const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(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<const IMAGE_NT_HEADERS64*>(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<const std::uint8_t*>(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<std::uint64_t>(header);
|
||||
|
||||
@@ -1,52 +1,57 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include "tests/GuestUnwindModuleInfoFixture.hpp"
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <windows.h>
|
||||
|
||||
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<std::uint64_t>(GetModuleHandleW(nullptr));
|
||||
header.frames = static_cast<std::int32_t>(reinterpret_cast<std::intptr_t>(&frames) - reinterpret_cast<std::intptr_t>(&header.frames));
|
||||
DWORD old = 0;
|
||||
Require(VirtualProtect(const_cast<std::uint32_t*>(&ehMeta), sizeof(ehMeta), PAGE_READWRITE, &old));
|
||||
ehMeta = static_cast<std::uint32_t>(reinterpret_cast<std::uint64_t>(&header) - base);
|
||||
auto* fixture = GetUnwindFixture();
|
||||
Require(fixture != nullptr);
|
||||
const auto address = reinterpret_cast<std::uint64_t>(fixture);
|
||||
MEMORY_BASIC_INFORMATION memory{};
|
||||
Require(VirtualQuery(reinterpret_cast<LPCVOID>(address), &memory, sizeof(memory)) != 0);
|
||||
const auto base = reinterpret_cast<std::uint64_t>(memory.AllocationBase);
|
||||
Require(base != reinterpret_cast<std::uint64_t>(GetModuleHandleW(nullptr)));
|
||||
|
||||
ModuleInfoForUnwind info{};
|
||||
Require(sceKernelGetModuleInfoForUnwind(reinterpret_cast<std::uint64_t>(&main), 0, &info) == 0);
|
||||
Require(sceKernelGetModuleInfoForUnwind(address, 0, &info) == 0);
|
||||
Require(info.st_size == sizeof(ModuleInfoForUnwind));
|
||||
Require(info.eh_frame_hdr_addr == reinterpret_cast<std::uint64_t>(&header));
|
||||
Require(info.eh_frame_addr == reinterpret_cast<std::uint64_t>(&frames));
|
||||
Require(info.eh_frame_hdr_addr == reinterpret_cast<std::uint64_t>(&fixture->header));
|
||||
Require(info.eh_frame_addr == reinterpret_cast<std::uint64_t>(&fixture->frames));
|
||||
Require(info.eh_frame_size == 4 + 12);
|
||||
Require(info.seg0_addr == base);
|
||||
const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(base);
|
||||
const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(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<std::uint64_t>(&GetModuleHandleW), 0, &host) == 0);
|
||||
Require(host.eh_frame_hdr_addr == 0 && host.eh_frame_addr == 0 && host.eh_frame_size == 0);
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "tests/GuestUnwindModuleInfoFixture.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <windows.h>
|
||||
|
||||
__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<std::uint64_t>(&__ImageBase);
|
||||
fixture.header.frames = static_cast<std::int32_t>(reinterpret_cast<std::intptr_t>(&fixture.frames) - reinterpret_cast<std::intptr_t>(&fixture.header.frames));
|
||||
DWORD old = 0;
|
||||
if (!VirtualProtect(const_cast<std::uint32_t*>(&ehMeta), sizeof(ehMeta), PAGE_READWRITE, &old)) return nullptr;
|
||||
ehMeta = static_cast<std::uint32_t>(reinterpret_cast<std::uint64_t>(&fixture.header) - base);
|
||||
return &fixture;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
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();
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user