diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 18:37:58 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-21 18:37:58 -0500 |
| commit | ed6b5236e5b84c6ef8e2dd1c787b1c780a80dea4 (patch) | |
| tree | c7e149ff62e4e9dfd1367da765763cbe51b9fd7e /runtime/test/loader_test.cpp | |
| parent | 99f39062e3d418db1bdcdfe354cf0e69900a9a9d (diff) | |
| parent | d3a02408ff7980ef50042c9f59c9c2ce7344a704 (diff) | |
Merge pull request #348 from traeb0n
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 98 |
1 files changed, 83 insertions, 15 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index 6165352f6..901afcb49 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | // Tests for the native PE loader. Every image here is built byte by byte, so nothing from the game is needed and | 1 | // Tests for the native PE loader. Every image here is built byte by byte, so nothing from the game is needed and |
| 2 | // nothing is read from the repo: the only files written go to a temporary directory. | 2 | // nothing is read from the repo: the only files written go to a leonida_loader_test folder in the temporary |
| 3 | // directory, or in LEONIDA_TEST_DIR when that is set (for machines whose security software blocks the images there). | ||
| 3 | // | 4 | // |
| 4 | // What it pins down: a valid image maps exactly as its headers say, and a truncated or corrupt one is rejected with | 5 | // What it pins down: a valid image maps exactly as its headers say, and a truncated or corrupt one is rejected with |
| 5 | // a message instead of being followed out of bounds. The truncation case covers every length of a valid image. | 6 | // a message instead of being followed out of bounds. The truncation case covers every length of a valid image. |
| @@ -7,7 +8,9 @@ | |||
| 7 | #include <intrin.h> | 8 | #include <intrin.h> |
| 8 | 9 | ||
| 9 | #include <algorithm> | 10 | #include <algorithm> |
| 11 | #include <chrono> | ||
| 10 | #include <cstdio> | 12 | #include <cstdio> |
| 13 | #include <cstdlib> | ||
| 11 | #include <cstring> | 14 | #include <cstring> |
| 12 | #include <filesystem> | 15 | #include <filesystem> |
| 13 | #include <fstream> | 16 | #include <fstream> |
| @@ -748,10 +751,59 @@ void test_unwind_registration() { | |||
| 748 | expect_rejects("unwind data past the image", make_unwind_image({{0x1000, 0x1010, 0x1FFE}})); | 751 | expect_rejects("unwind data past the image", make_unwind_image({{0x1000, 0x1010, 0x1FFE}})); |
| 749 | } | 752 | } |
| 750 | 753 | ||
| 754 | // The folder the images go to: leonida_loader_test\<process id> in LEONIDA_TEST_DIR when that is set, otherwise in | ||
| 755 | // the temporary directory. A folder per run, because security software can hold the previous run's images for a | ||
| 756 | // while after they are written (so they can be neither deleted nor overwritten); the fixed parent name is what an | ||
| 757 | // exclusion names. | ||
| 758 | std::filesystem::path work_dir() { | ||
| 759 | wchar_t* env = nullptr; | ||
| 760 | size_t size = 0; | ||
| 761 | std::filesystem::path base; | ||
| 762 | if (_wdupenv_s(&env, &size, L"LEONIDA_TEST_DIR") == 0 && env && *env) base = env; | ||
| 763 | std::free(env); | ||
| 764 | if (base.empty()) base = std::filesystem::temp_directory_path(); | ||
| 765 | return base / "leonida_loader_test" / std::to_string(GetCurrentProcessId()); | ||
| 766 | } | ||
| 767 | |||
| 768 | // Security software can scan each file that looks like an executable as it is written, and hold it while it does: | ||
| 769 | // measured with Trend Micro Maximum Security, opening such a file afterwards fails with "access denied" for a while | ||
| 770 | // (50 to 800 ms each), plain files are unaffected, and an excluded folder takes about 2 ms. Cases then fail at | ||
| 771 | // random and the truncation case, which writes over a thousand images, stalls for minutes. So the test first writes | ||
| 772 | // and reads back a few images, and stops with the reason if any is refused or they take too long. Empty when the | ||
| 773 | // folder behaves. | ||
| 774 | std::string written_images_blocked() { | ||
| 775 | constexpr int probes = 8; | ||
| 776 | constexpr auto slow = std::chrono::milliseconds(25); // per image, on average | ||
| 777 | const std::vector<uint8_t> image = make_image(); | ||
| 778 | const auto start = std::chrono::steady_clock::now(); | ||
| 779 | for (int i = 0; i < probes; ++i) { | ||
| 780 | const auto path = write_image_as(image, "readback_check_" + std::to_string(i) + ".bin"); | ||
| 781 | const HANDLE file = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, | ||
| 782 | FILE_ATTRIBUTE_NORMAL, nullptr); | ||
| 783 | if (file == INVALID_HANDLE_VALUE) | ||
| 784 | return "opening one failed with Windows error " + std::to_string(GetLastError()); | ||
| 785 | std::vector<uint8_t> back(image.size() + 1); | ||
| 786 | DWORD read = 0; | ||
| 787 | const BOOL ok = ReadFile(file, back.data(), DWORD(back.size()), &read, nullptr); | ||
| 788 | const DWORD error = GetLastError(); | ||
| 789 | CloseHandle(file); | ||
| 790 | if (!ok) return "reading one failed with Windows error " + std::to_string(error); | ||
| 791 | if (read != image.size() || !std::equal(image.begin(), image.end(), back.begin())) | ||
| 792 | return "one read back as " + std::to_string(read) + " bytes that differ from the " + | ||
| 793 | std::to_string(image.size()) + " written"; | ||
| 794 | } | ||
| 795 | const auto elapsed = std::chrono::steady_clock::now() - start; | ||
| 796 | const auto each = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed) / probes; | ||
| 797 | if (each > slow) | ||
| 798 | return "writing and reading one back took " + std::to_string(each.count()) + " ms on average (over " + | ||
| 799 | std::to_string(slow.count()) + " ms means each is being scanned)"; | ||
| 800 | return {}; | ||
| 801 | } | ||
| 802 | |||
| 751 | } // namespace | 803 | } // namespace |
| 752 | 804 | ||
| 753 | int main() { | 805 | int main() { |
| 754 | g_dir = std::filesystem::temp_directory_path() / "leonida_loader_test"; | 806 | g_dir = work_dir(); |
| 755 | std::error_code ec; | 807 | std::error_code ec; |
| 756 | std::filesystem::remove_all(g_dir, ec); | 808 | std::filesystem::remove_all(g_dir, ec); |
| 757 | std::filesystem::create_directories(g_dir, ec); | 809 | std::filesystem::create_directories(g_dir, ec); |
| @@ -760,21 +812,37 @@ int main() { | |||
| 760 | return 1; | 812 | return 1; |
| 761 | } | 813 | } |
| 762 | 814 | ||
| 763 | test_valid_image(); | 815 | try { |
| 764 | test_header_bounds(); | 816 | if (const std::string why = written_images_blocked(); !why.empty()) { |
| 765 | test_section_bounds(); | 817 | std::printf("FAIL PE images written to %s are not readable straight away: %s.\n" |
| 766 | test_truncation(); | 818 | " Security software that scans newly written executable files does this. Exclude %s\n" |
| 767 | test_relocations(); | 819 | " from scanning, or set LEONIDA_TEST_DIR to a folder it does not scan.\n", |
| 768 | test_exports(); | 820 | g_dir.string().c_str(), why.c_str(), g_dir.parent_path().string().c_str()); |
| 769 | test_imports(); | 821 | std::filesystem::remove_all(g_dir, ec); |
| 770 | test_delay_imports(); | 822 | return 1; |
| 771 | test_initialise(); | 823 | } |
| 772 | test_initialise_rejects(); | 824 | test_valid_image(); |
| 773 | test_delay_descriptor_fields(); | 825 | test_header_bounds(); |
| 774 | test_forwarded_exports(); | 826 | test_section_bounds(); |
| 775 | test_unwind_registration(); | 827 | test_truncation(); |
| 828 | test_relocations(); | ||
| 829 | test_exports(); | ||
| 830 | test_imports(); | ||
| 831 | test_delay_imports(); | ||
| 832 | test_initialise(); | ||
| 833 | test_initialise_rejects(); | ||
| 834 | test_delay_descriptor_fields(); | ||
| 835 | test_forwarded_exports(); | ||
| 836 | test_unwind_registration(); | ||
| 837 | } catch (const std::exception& e) { | ||
| 838 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. | ||
| 839 | failed("writing the test images", e.what()); | ||
| 840 | } | ||
| 776 | 841 | ||
| 777 | std::filesystem::remove_all(g_dir, ec); | 842 | std::filesystem::remove_all(g_dir, ec); |
| 843 | if (ec) // not a failure of the loader; the next run uses a folder of its own anyway | ||
| 844 | std::printf("note could not remove %s: %s\n", g_dir.string().c_str(), ec.message().c_str()); | ||
| 845 | std::filesystem::remove(g_dir.parent_path(), ec); // only if no other run's folder is left | ||
| 778 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); | 846 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); |
| 779 | return g_failed ? 1 : 0; | 847 | return g_failed ? 1 : 0; |
| 780 | } | 848 | } |