mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
test(relinker): execute the guest Intel trampolines end to end
This commit is contained in:
@@ -295,5 +295,9 @@ if(BUILD_TESTING)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/RelaBoundsTests.cpp
|
||||
)
|
||||
add_test(NAME rela_bounds COMMAND rela_bounds_tests $<TARGET_FILE:relinker>)
|
||||
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 $<TARGET_FILE:relinker>)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -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<typename TValue>
|
||||
void Write(Bytes& bytes, const std::size_t offset, const TValue value) {
|
||||
@@ -72,6 +75,42 @@ inline Bytes MakeExecutable(const std::span<const std::uint8_t> code) {
|
||||
return image;
|
||||
}
|
||||
|
||||
inline Bytes MakeModule(const std::span<const std::uint8_t> 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<std::uint16_t>(image, 16, 3);
|
||||
Write<std::uint16_t>(image, 18, 62);
|
||||
Write<std::uint32_t>(image, 20, 1);
|
||||
Write<std::uint64_t>(image, 24, 0);
|
||||
Write<std::uint64_t>(image, 32, 64);
|
||||
Write<std::uint16_t>(image, 52, 64);
|
||||
Write<std::uint16_t>(image, 54, 56);
|
||||
Write<std::uint16_t>(image, 56, 3 + kSpareProgramHeaders);
|
||||
Write<std::uint16_t>(image, 58, 64);
|
||||
const std::pair<std::int64_t, std::uint64_t> 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<std::int64_t>(image, kModuleDataOffset + index * 16, tags[index].first);
|
||||
Write<std::uint64_t>(image, kModuleDataOffset + index * 16 + 8, tags[index].second);
|
||||
}
|
||||
Write<std::uint32_t>(image, kModuleDataOffset + 0x240, 1);
|
||||
Write<std::uint32_t>(image, kModuleDataOffset + 0x244, 1);
|
||||
return image;
|
||||
}
|
||||
|
||||
class MappedImage {
|
||||
public:
|
||||
explicit MappedImage(const Bytes& elf) {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "ElfFixture.hpp"
|
||||
#include "RelinkerProcess.hpp"
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
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 <relinker>");
|
||||
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<const std::uint8_t>(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<std::uint64_t(std::uint64_t, std::uint64_t)>(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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user