From 4d13e99a0debe028db08727832866998bf326582 Mon Sep 17 00:00:00 2001 From: "husari.orozco" Date: Wed, 7 Oct 2026 15:42:20 -0600 Subject: [PATCH] test(relinker): execute the guest Intel trampolines end to end --- core/relinker/CMakeLists.txt | 4 + core/relinker/relinker/tests/ElfFixture.hpp | 39 ++++++++++ .../tests/GuestIntelTrampolinesTests.cpp | 73 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 core/relinker/relinker/tests/GuestIntelTrampolinesTests.cpp diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 711fcf509..449e5454f 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -295,5 +295,9 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/RelaBoundsTests.cpp ) add_test(NAME rela_bounds COMMAND rela_bounds_tests $) + add_test_executable(guest_intel_trampolines_tests + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/GuestIntelTrampolinesTests.cpp + ) + add_test(NAME guest_intel_trampolines_execution COMMAND guest_intel_trampolines_tests $) endif() endif() diff --git a/core/relinker/relinker/tests/ElfFixture.hpp b/core/relinker/relinker/tests/ElfFixture.hpp index 9962b9d85..8c44f1179 100644 --- a/core/relinker/relinker/tests/ElfFixture.hpp +++ b/core/relinker/relinker/tests/ElfFixture.hpp @@ -18,6 +18,9 @@ inline constexpr std::uint64_t kCodeOffset = 0x4000; inline constexpr std::uint64_t kCodeSize = 0x1000; inline constexpr std::uint64_t kDynamicVaddr = 0x600; inline constexpr std::uint16_t kSpareProgramHeaders = 3; +inline constexpr std::uint64_t kModuleDataOffset = 0x5000; +inline constexpr std::uint64_t kModuleDataVaddr = 0x2000; +inline constexpr std::uint64_t kModuleDataSize = 0x400; template void Write(Bytes& bytes, const std::size_t offset, const TValue value) { @@ -72,6 +75,42 @@ inline Bytes MakeExecutable(const std::span code) { return image; } +inline Bytes MakeModule(const std::span code) { + if (code.size() > kCodeSize) throw std::runtime_error("ELF fixture code exceeds the code segment"); + Bytes image(0x8000); + const std::uint8_t ident[16] = {0x7F, 'E', 'L', 'F', 2, 1, 1}; + std::memcpy(image.data(), ident, sizeof(ident)); + Write(image, 16, 3); + Write(image, 18, 62); + Write(image, 20, 1); + Write(image, 24, 0); + Write(image, 32, 64); + Write(image, 52, 64); + Write(image, 54, 56); + Write(image, 56, 3 + kSpareProgramHeaders); + Write(image, 58, 64); + const std::pair tags[] = { + {5, kModuleDataVaddr + 0x200}, {10, 1}, + {6, kModuleDataVaddr + 0x220}, {11, 24}, + {4, kModuleDataVaddr + 0x240}, + {7, kModuleDataVaddr + 0x300}, {8, 0}, {9, 24}, + {0, 0}, + }; + WriteProgramHeader(image, 0, 1, 5, kCodeOffset, 0, kCodeSize, 0x4000); + WriteProgramHeader(image, 1, 1, 6, kModuleDataOffset, kModuleDataVaddr, kModuleDataSize, 8); + WriteProgramHeader(image, 2, 2, 6, kModuleDataOffset, kModuleDataVaddr, sizeof(tags) / sizeof(tags[0]) * 16, 8); + for (std::uint16_t index = 3; index < 3 + kSpareProgramHeaders; ++index) WriteProgramHeader(image, index, 0x6FFFFF01, 0, 0, 0, 0, 1); + std::fill(image.begin() + kCodeOffset, image.begin() + kCodeOffset + kCodeSize, 0xCC); + std::copy(code.begin(), code.end(), image.begin() + kCodeOffset); + for (std::size_t index = 0; index < sizeof(tags) / sizeof(tags[0]); ++index) { + Write(image, kModuleDataOffset + index * 16, tags[index].first); + Write(image, kModuleDataOffset + index * 16 + 8, tags[index].second); + } + Write(image, kModuleDataOffset + 0x240, 1); + Write(image, kModuleDataOffset + 0x244, 1); + return image; +} + class MappedImage { public: explicit MappedImage(const Bytes& elf) { diff --git a/core/relinker/relinker/tests/GuestIntelTrampolinesTests.cpp b/core/relinker/relinker/tests/GuestIntelTrampolinesTests.cpp new file mode 100644 index 000000000..ff50df293 --- /dev/null +++ b/core/relinker/relinker/tests/GuestIntelTrampolinesTests.cpp @@ -0,0 +1,73 @@ +#include "ElfFixture.hpp" +#include "RelinkerProcess.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using namespace RelinkerTests; + +void require(const bool condition, const std::string& message) { + if (!condition) throw std::runtime_error(message); +} + +const Bytes kMainCode = {0xC3}; + +const Bytes kModuleCode = { + 0x66, 0x48, 0x0F, 0x6E, 0xCF, + 0x66, 0x48, 0x0F, 0x6E, 0xD6, + 0x66, 0x0F, 0x78, 0xC1, 0x08, 0x08, + 0xF2, 0x0F, 0x78, 0xD1, 0x08, 0x10, + 0x66, 0x48, 0x0F, 0x7E, 0xD0, + 0xC3, +}; +constexpr std::uint64_t kSites[] = {10, 16}; +constexpr std::size_t kSiteLength = 6; + +std::uint64_t expected(const std::uint64_t value, const std::uint64_t destination) { + return (destination & ~0xFF0000ull) | (((value >> 8) & 0xFF) << 16); +} + +} + +int main(const int argc, char** argv) { + try { + require(argc == 2, "usage: guest_intel_trampolines_tests "); + const TempDirectory directory; + const auto input = directory.Path() / "input.elf"; + const auto output = directory.Path() / "output.elf"; + const auto modules = directory.Path() / "sce_module"; + std::filesystem::create_directory(modules); + WriteFile(input, MakeExecutable(kMainCode)); + WriteFile(modules / "sample.prx", MakeModule(kModuleCode)); + + const auto run = RunRelinker(argv[1], {"--to-intel", input.string(), output.string()}, directory.Path() / "relinker.log"); + require(run.ExitCode == 0, "Relinker failed:\n" + run.Output); + + const auto module = directory.Path() / "app0" / "sce_module" / "sample.prx.guest.prx"; + require(std::filesystem::exists(module), "The relinker did not write the converted guest module:\n" + run.Output); + + const MappedImage image(ReadFile(module)); + for (const auto site : kSites) { + const auto* bytes = image.At(site); + require(bytes[0] == 0xE9, "SSE4a site in the guest module was not replaced by a jump"); + for (std::size_t index = 5; index < kSiteLength; ++index) require(bytes[index] == 0x90, "SSE4a site tail in the guest module is not padded with NOPs"); + std::int32_t displacement; + std::memcpy(&displacement, bytes + 1, sizeof(displacement)); + require(site + 5 + displacement >= kCodeSize, "Guest module SSE4a stub is inside the original code segment"); + } + + const auto function = image.At(0); + for (const auto [value, destination] : {std::pair{0x1122334455667788ull, 0x0123456789ABCDEFull}, {0ull, ~0ull}, {~0ull, 0ull}, {0x9E3779B97F4A7C15ull, 0x0F1E2D3C4B5A6978ull}}) + require(function(value, destination) == expected(value, destination), "Relinked guest module trampolines computed the wrong value"); + std::cout << "Guest Intel trampoline execution tests passed\n"; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +}