Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 72 additions & 8 deletions core/libs/prx/libSceSysmodule/Export.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <cstdint>
#include <cstring>
#include <map>
#include <mutex>
#include <stdexcept>
#include <string>
Expand Down Expand Up @@ -38,23 +39,86 @@ void* openHostModule(const char* name) {
return dlopen_nid_postfix(relative.c_str(), kRtldNow);
}

#ifdef _WIN32
struct ImageRegion {
std::uint64_t end = 0;
std::uintptr_t image = 0;
DWORD stamp = 0;
DWORD size = 0;
std::string name;
};

std::mutex gRegionsMutex;
std::map<std::uint64_t, ImageRegion> gRegions;

bool imageIdentity(std::uintptr_t image, DWORD& stamp, DWORD& size) {
const auto* dos = reinterpret_cast<const IMAGE_DOS_HEADER*>(image);
if (dos->e_magic != IMAGE_DOS_SIGNATURE) return false;
const auto* nt = reinterpret_cast<const IMAGE_NT_HEADERS64*>(image + dos->e_lfanew);
if (nt->Signature != IMAGE_NT_SIGNATURE) return false;
stamp = nt->FileHeader.TimeDateStamp;
size = nt->OptionalHeader.SizeOfImage;
return true;
}

std::uintptr_t loaderImage(std::uint64_t addr) {
PVOID image = nullptr;
if (RtlPcToFileHeader(reinterpret_cast<PVOID>(addr), &image) == nullptr) return 0;
return reinterpret_cast<std::uintptr_t>(image);
}

void writeUnwindRegion(std::uint64_t begin, std::uint64_t size, const std::string& name, ModuleInfoForUnwind* info) {
info->st_size = sizeof(ModuleInfoForUnwind);
info->eh_frame_hdr_addr = 0;
info->eh_frame_addr = 0;
info->eh_frame_size = 0;
info->seg0_addr = begin;
info->seg0_size = size;
std::strncpy(info->name, name.c_str(), sizeof(info->name) - 1);
info->name[sizeof(info->name) - 1] = '\0';
}

bool fillCachedImageRegion(std::uint64_t addr, std::uintptr_t image, ModuleInfoForUnwind* info) {
std::lock_guard lock(gRegionsMutex);
auto found = gRegions.upper_bound(addr);
if (found == gRegions.begin()) return false;
--found;
const auto& region = found->second;
if (addr >= region.end) return false;
DWORD stamp = 0;
DWORD size = 0;
if (region.image != image || !imageIdentity(image, stamp, size) || stamp != region.stamp || size != region.size) {
gRegions.erase(found);
return false;
}
constexpr std::uint64_t page = 4096;
const std::uint64_t begin = addr & ~(page - 1);
writeUnwindRegion(begin, region.end - begin, region.name, info);
return true;
}
#endif

bool fillModuleInfoForUnwind(std::uint64_t addr, ModuleInfoForUnwind* info) {
#ifdef _WIN32
const auto image = loaderImage(addr);
if (image != 0 && fillCachedImageRegion(addr, image, info)) return true;
MEMORY_BASIC_INFORMATION mbi{};
if (!VirtualQuery(reinterpret_cast<LPCVOID>(addr), &mbi, sizeof(mbi))) {
return false;
}
info->st_size = sizeof(ModuleInfoForUnwind);
info->eh_frame_hdr_addr = 0;
info->eh_frame_addr = 0;
info->eh_frame_size = 0;
info->seg0_addr = reinterpret_cast<std::uint64_t>(mbi.BaseAddress);
info->seg0_size = mbi.RegionSize;
char path[4096] = {};
DWORD len = GetMappedFileNameA(GetCurrentProcess(), mbi.BaseAddress, path, sizeof(path) - 1);
path[len] = '\0';
std::strncpy(info->name, path, sizeof(info->name) - 1);
info->name[sizeof(info->name) - 1] = '\0';
const auto begin = reinterpret_cast<std::uint64_t>(mbi.BaseAddress);
writeUnwindRegion(begin, mbi.RegionSize, path, info);
ImageRegion region;
if (mbi.Type == MEM_IMAGE && image != 0 && imageIdentity(image, region.stamp, region.size) && begin >= image && begin + mbi.RegionSize <= image + region.size) {
region.end = begin + mbi.RegionSize;
region.image = image;
region.name = path;
std::lock_guard lock(gRegionsMutex);
gRegions.insert_or_assign(begin, std::move(region));
}
return true;
#else
const auto mapping = HostMapping::Find(addr, "sceSysmoduleGetModuleInfoForUnwind");
Expand Down
68 changes: 67 additions & 1 deletion core/libs/tests/GuestSysmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <stdexcept>
#include <string>
#ifdef _WIN32
#include <windows.h>
#include <psapi.h>
#endif
#ifdef __linux__
#include <cstdio>
#include <fstream>
Expand Down Expand Up @@ -103,6 +108,67 @@ void CheckUnwindInfo() {
}
#endif

#ifdef _WIN32
void RequireUnwindMatchesRegion(const void* pointer) {
const auto address = reinterpret_cast<std::uint64_t>(pointer);
MEMORY_BASIC_INFORMATION expected{};
Require(VirtualQuery(pointer, &expected, sizeof(expected)) != 0);
char mapped[4096] = {};
const DWORD length = GetMappedFileNameA(GetCurrentProcess(), expected.BaseAddress, mapped, sizeof(mapped) - 1);
mapped[length] = '\0';
for (int repeat = 0; repeat < 3; ++repeat) {
ModuleInfoForUnwind info{};
Require(sceSysmoduleGetModuleInfoForUnwind(address, 0, &info) == 0);
Require(info.st_size == sizeof(ModuleInfoForUnwind));
Require(info.seg0_addr == reinterpret_cast<std::uint64_t>(expected.BaseAddress) && info.seg0_size == expected.RegionSize);
Require(std::strncmp(info.name, mapped, sizeof(info.name) - 1) == 0);
Require(info.eh_frame_hdr_addr == 0 && info.eh_frame_addr == 0 && info.eh_frame_size == 0);
}
}

void CheckUnwindInfo() {
RequireUnwindMatchesRegion(reinterpret_cast<const void*>(&RequireUnwindMatchesRegion));
RequireUnwindMatchesRegion(reinterpret_cast<const void*>(&sceSysmoduleGetModuleInfoForUnwind));
RequireUnwindMatchesRegion(reinterpret_cast<const void*>(&GetModuleHandleW));
const auto* host = reinterpret_cast<const char*>(&GetModuleHandleW);
MEMORY_BASIC_INFORMATION hostRegion{};
Require(VirtualQuery(host, &hostRegion, sizeof(hostRegion)) != 0);
for (const std::size_t step : {std::size_t{0x1000}, std::size_t{0x3000}, std::size_t{0x10000}}) {
if (hostRegion.RegionSize > step) RequireUnwindMatchesRegion(host + step);
}
RequireUnwindMatchesRegion(host + 0x2000);
RequireUnwindMatchesRegion(host);
const auto* anchor = reinterpret_cast<const char*>(&RtlPcToFileHeader);
MEMORY_BASIC_INFORMATION anchorRegion{};
Require(VirtualQuery(anchor, &anchorRegion, sizeof(anchorRegion)) != 0);
if (anchorRegion.RegionSize > 0x4000) {
RequireUnwindMatchesRegion(anchor + 0x3000);
RequireUnwindMatchesRegion(anchor);
RequireUnwindMatchesRegion(anchor + 0x1000);
RequireUnwindMatchesRegion(anchor + 0x2000);
}
static int data = 1;
static const char text[] = "unwind region";
RequireUnwindMatchesRegion(&data);
RequireUnwindMatchesRegion(text);
RequireUnwindMatchesRegion(reinterpret_cast<const void*>(&RequireUnwindMatchesRegion));
int local = 0;
RequireUnwindMatchesRegion(&local);
SYSTEM_INFO system{};
GetSystemInfo(&system);
const std::size_t page = system.dwPageSize;
auto* memory = static_cast<char*>(VirtualAlloc(nullptr, 3 * page, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE));
Require(memory != nullptr);
RequireUnwindMatchesRegion(memory);
DWORD previous = 0;
Require(VirtualProtect(memory + 2 * page, page, PAGE_READONLY, &previous) != 0);
RequireUnwindMatchesRegion(memory);
RequireUnwindMatchesRegion(memory + 2 * page);
Require(VirtualFree(memory, 0, MEM_RELEASE) != 0);
RequireUnwindMatchesRegion(memory);
}
#endif

}

int main() {
Expand Down Expand Up @@ -178,7 +244,7 @@ int main() {
Require(sceSysmoduleUnloadModuleInternal(kAgcModuleId) == 0);
Require(sceSysmoduleUnloadModuleInternal(kAgcModuleId) == 0);
Require(sceSysmoduleUnloadModuleInternal(kAgcModuleId) == kModuleUnloaded);
#ifdef __linux__
#if defined(__linux__) || defined(_WIN32)
CheckUnwindInfo();
#endif

Expand Down
5 changes: 5 additions & 0 deletions docs/dev/debt/libSceSysmodule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libSceSysmodule technical debt

### Functional

- [sceSysmoduleGetModuleInfoForUnwind](../../../core/libs/prx/libSceSysmodule/Export.cpp) (libSceSysmodule) - on Windows the region and file name of an address inside a loaded image are cached after the first lookup, because `VirtualQuery` on a large image section is slow. An entry is used only while the loader still reports the same image for the address (same base, time stamp and size). A page protection change inside the image after the first lookup, which would split or merge the region `VirtualQuery` reports, is not noticed. Addresses outside loaded images are queried every time
Loading