diff options
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 b756cc1b6..944378bcb 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -1,12 +1,15 @@ | |||
| 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. |
| 6 | #include <windows.h> | 7 | #include <windows.h> |
| 7 | 8 | ||
| 8 | #include <algorithm> | 9 | #include <algorithm> |
| 10 | #include <chrono> | ||
| 9 | #include <cstdio> | 11 | #include <cstdio> |
| 12 | #include <cstdlib> | ||
| 10 | #include <cstring> | 13 | #include <cstring> |
| 11 | #include <filesystem> | 14 | #include <filesystem> |
| 12 | #include <fstream> | 15 | #include <fstream> |
| @@ -738,10 +741,59 @@ void test_unwind_registration() { | |||
| 738 | expect_rejects("unwind data past the image", make_unwind_image({{0x1000, 0x1010, 0x1FFE}})); | 741 | expect_rejects("unwind data past the image", make_unwind_image({{0x1000, 0x1010, 0x1FFE}})); |
| 739 | } | 742 | } |
| 740 | 743 | ||
| 744 | // The folder the images go to: leonida_loader_test\<process id> in LEONIDA_TEST_DIR when that is set, otherwise in | ||
| 745 | // the temporary directory. A folder per run, because security software can hold the previous run's images for a | ||
| 746 | // while after they are written (so they can be neither deleted nor overwritten); the fixed parent name is what an | ||
| 747 | // exclusion names. | ||
| 748 | std::filesystem::path work_dir() { | ||
| 749 | wchar_t* env = nullptr; | ||
| 750 | size_t size = 0; | ||
| 751 | std::filesystem::path base; | ||
| 752 | if (_wdupenv_s(&env, &size, L"LEONIDA_TEST_DIR") == 0 && env && *env) base = env; | ||
| 753 | std::free(env); | ||
| 754 | if (base.empty()) base = std::filesystem::temp_directory_path(); | ||
| 755 | return base / "leonida_loader_test" / std::to_string(GetCurrentProcessId()); | ||
| 756 | } | ||
| 757 | |||
| 758 | // Security software can scan each file that looks like an executable as it is written, and hold it while it does: | ||
| 759 | // measured with Trend Micro Maximum Security, opening such a file afterwards fails with "access denied" for a while | ||
| 760 | // (50 to 800 ms each), plain files are unaffected, and an excluded folder takes about 2 ms. Cases then fail at | ||
| 761 | // random and the truncation case, which writes over a thousand images, stalls for minutes. So the test first writes | ||
| 762 | // and reads back a few images, and stops with the reason if any is refused or they take too long. Empty when the | ||
| 763 | // folder behaves. | ||
| 764 | std::string written_images_blocked() { | ||
| 765 | constexpr int probes = 8; | ||
| 766 | constexpr auto slow = std::chrono::milliseconds(25); // per image, on average | ||
| 767 | const std::vector<uint8_t> image = make_image(); | ||
| 768 | const auto start = std::chrono::steady_clock::now(); | ||
| 769 | for (int i = 0; i < probes; ++i) { | ||
| 770 | const auto path = write_image_as(image, "readback_check_" + std::to_string(i) + ".bin"); | ||
| 771 | const HANDLE file = CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, | ||
| 772 | FILE_ATTRIBUTE_NORMAL, nullptr); | ||
| 773 | if (file == INVALID_HANDLE_VALUE) | ||
| 774 | return "opening one failed with Windows error " + std::to_string(GetLastError()); | ||
| 775 | std::vector<uint8_t> back(image.size() + 1); | ||
| 776 | DWORD read = 0; | ||
| 777 | const BOOL ok = ReadFile(file, back.data(), DWORD(back.size()), &read, nullptr); | ||
| 778 | const DWORD error = GetLastError(); | ||
| 779 | CloseHandle(file); | ||
| 780 | if (!ok) return "reading one failed with Windows error " + std::to_string(error); | ||
| 781 | if (read != image.size() || !std::equal(image.begin(), image.end(), back.begin())) | ||
| 782 | return "one read back as " + std::to_string(read) + " bytes that differ from the " + | ||
| 783 | std::to_string(image.size()) + " written"; | ||
| 784 | } | ||
| 785 | const auto elapsed = std::chrono::steady_clock::now() - start; | ||
| 786 | const auto each = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed) / probes; | ||
| 787 | if (each > slow) | ||
| 788 | return "writing and reading one back took " + std::to_string(each.count()) + " ms on average (over " + | ||
| 789 | std::to_string(slow.count()) + " ms means each is being scanned)"; | ||
| 790 | return {}; | ||
| 791 | } | ||
| 792 | |||
| 741 | } // namespace | 793 | } // namespace |
| 742 | 794 | ||
| 743 | int main() { | 795 | int main() { |
| 744 | g_dir = std::filesystem::temp_directory_path() / "leonida_loader_test"; | 796 | g_dir = work_dir(); |
| 745 | std::error_code ec; | 797 | std::error_code ec; |
| 746 | std::filesystem::remove_all(g_dir, ec); | 798 | std::filesystem::remove_all(g_dir, ec); |
| 747 | std::filesystem::create_directories(g_dir, ec); | 799 | std::filesystem::create_directories(g_dir, ec); |
| @@ -750,21 +802,37 @@ int main() { | |||
| 750 | return 1; | 802 | return 1; |
| 751 | } | 803 | } |
| 752 | 804 | ||
| 753 | test_valid_image(); | 805 | try { |
| 754 | test_header_bounds(); | 806 | if (const std::string why = written_images_blocked(); !why.empty()) { |
| 755 | test_section_bounds(); | 807 | std::printf("FAIL PE images written to %s are not readable straight away: %s.\n" |
| 756 | test_truncation(); | 808 | " Security software that scans newly written executable files does this. Exclude %s\n" |
| 757 | test_relocations(); | 809 | " from scanning, or set LEONIDA_TEST_DIR to a folder it does not scan.\n", |
| 758 | test_exports(); | 810 | g_dir.string().c_str(), why.c_str(), g_dir.parent_path().string().c_str()); |
| 759 | test_imports(); | 811 | std::filesystem::remove_all(g_dir, ec); |
| 760 | test_delay_imports(); | 812 | return 1; |
| 761 | test_initialise(); | 813 | } |
| 762 | test_initialise_rejects(); | 814 | test_valid_image(); |
| 763 | test_delay_descriptor_fields(); | 815 | test_header_bounds(); |
| 764 | test_forwarded_exports(); | 816 | test_section_bounds(); |
| 765 | test_unwind_registration(); | 817 | test_truncation(); |
| 818 | test_relocations(); | ||
| 819 | test_exports(); | ||
| 820 | test_imports(); | ||
| 821 | test_delay_imports(); | ||
| 822 | test_initialise(); | ||
| 823 | test_initialise_rejects(); | ||
| 824 | test_delay_descriptor_fields(); | ||
| 825 | test_forwarded_exports(); | ||
| 826 | test_unwind_registration(); | ||
| 827 | } catch (const std::exception& e) { | ||
| 828 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. | ||
| 829 | failed("writing the test images", e.what()); | ||
| 830 | } | ||
| 766 | 831 | ||
| 767 | std::filesystem::remove_all(g_dir, ec); | 832 | std::filesystem::remove_all(g_dir, ec); |
| 833 | if (ec) // not a failure of the loader; the next run uses a folder of its own anyway | ||
| 834 | std::printf("note could not remove %s: %s\n", g_dir.string().c_str(), ec.message().c_str()); | ||
| 835 | std::filesystem::remove(g_dir.parent_path(), ec); // only if no other run's folder is left | ||
| 768 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); | 836 | std::printf("\n%d checks, %d failed\n", g_checks, g_failed); |
| 769 | return g_failed ? 1 : 0; | 837 | return g_failed ? 1 : 0; |
| 770 | } | 838 | } |