diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 1b3e069fe..339433c87 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -282,5 +282,10 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/ToIntelLinuxStubsTests.cpp ) add_test(NAME to_intel_linux_stubs COMMAND to_intel_linux_stubs_tests $) + + add_test_executable(rela_bounds_tests + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/RelaBoundsTests.cpp + ) + add_test(NAME rela_bounds COMMAND rela_bounds_tests $) endif() endif() diff --git a/core/relinker/relinker/tests/RelaBoundsTests.cpp b/core/relinker/relinker/tests/RelaBoundsTests.cpp new file mode 100644 index 000000000..a445c4f69 --- /dev/null +++ b/core/relinker/relinker/tests/RelaBoundsTests.cpp @@ -0,0 +1,68 @@ +#include "ElfFixture.hpp" +#include "RelinkerProcess.hpp" +#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 kCode = {0xC3}; + +constexpr std::size_t kTagStride = 16; +constexpr std::size_t kTagBase = kCodeOffset + kDynamicVaddr; +constexpr std::size_t kRelaTag = 4; +constexpr std::size_t kRelaSzTag = 5; +constexpr std::int64_t kDtOsRela = 0x6100002f; +constexpr std::int64_t kDtRelaSz = 8; +constexpr std::size_t kImageSize = 0x8000; + +void WriteTag(Bytes& image, const std::size_t index, const std::int64_t tag, const std::uint64_t value) { + Write(image, kTagBase + index * kTagStride, tag); + Write(image, kTagBase + index * kTagStride + 8, value); +} + +void RunAndRequireRejected(const std::string& binary, const Bytes& image, const std::string& what) { + const TempDirectory directory; + const auto input = directory.Path() / "input.elf"; + const auto output = directory.Path() / "output.elf"; + WriteFile(input, image); + const auto run = RunRelinker(binary, {"--skip-sce-module", "--to-intel", input.string(), output.string()}, directory.Path() / "relinker.log"); + require(run.ExitCode != 0, "Relinker accepted a relocation table " + what + " and exited 0:\n" + run.Output); + require(run.Output.find("Relocation entry out of bounds") != std::string::npos, + "Relinker did not report the entry as out of bounds for a table " + what + ":\n" + run.Output); +} + +} + +int main(const int argc, char** argv) { + try { + require(argc == 2, "usage: rela_bounds_tests "); + const std::string binary = argv[1]; + + { + Bytes image = MakeExecutable(kCode); + WriteTag(image, kRelaTag, kDtOsRela, 0xFFFFFFFFFFFFFFF8ull); + WriteTag(image, kRelaSzTag, kDtRelaSz, 24); + RunAndRequireRejected(binary, image, "whose offset is near UINT64_MAX"); + } + + { + Bytes image = MakeExecutable(kCode); + WriteTag(image, kRelaTag, kDtOsRela, kImageSize - 16); + WriteTag(image, kRelaSzTag, kDtRelaSz, 48); + RunAndRequireRejected(binary, image, "that does not fit in the file"); + } + + return 0; + } catch (const std::exception& error) { + std::cerr << "rela_bounds_tests: " << error.what() << '\n'; + return 1; + } +}