diff options
Diffstat (limited to 'runtime/test/loader_test.cpp')
| -rw-r--r-- | runtime/test/loader_test.cpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp index aba37cdf3..a925fa021 100644 --- a/runtime/test/loader_test.cpp +++ b/runtime/test/loader_test.cpp | |||
| @@ -1308,6 +1308,122 @@ std::string written_images_blocked() { | |||
| 1308 | return {}; | 1308 | return {}; |
| 1309 | } | 1309 | } |
| 1310 | 1310 | ||
| 1311 | // A DLL Windows' own loader accepts (LEONIDA_COMPAT_DIR loads with LoadLibraryExW, not with rt::Loader): no entry | ||
| 1312 | // point, no imports, one export "Answer" (ordinal 1) whose code returns 42. | ||
| 1313 | std::vector<uint8_t> make_os_dll(uint64_t image_base) { | ||
| 1314 | auto img = make_image(); | ||
| 1315 | std::fill(img.begin() + 0x200, img.begin() + 0x400, uint8_t{0}); | ||
| 1316 | auto* nt = nt_of(img); | ||
| 1317 | nt->FileHeader.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_DLL | IMAGE_FILE_LARGE_ADDRESS_AWARE; | ||
| 1318 | auto& oh = nt->OptionalHeader; | ||
| 1319 | oh.ImageBase = leonida_test::free_image_base(image_base); | ||
| 1320 | oh.SectionAlignment = 0x1000; | ||
| 1321 | oh.FileAlignment = 0x200; | ||
| 1322 | oh.MajorOperatingSystemVersion = 6; | ||
| 1323 | oh.MajorSubsystemVersion = 6; | ||
| 1324 | oh.AddressOfEntryPoint = 0; | ||
| 1325 | oh.BaseOfCode = 0x1000; | ||
| 1326 | oh.SizeOfCode = 0x200; | ||
| 1327 | oh.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI; | ||
| 1328 | oh.DllCharacteristics = IMAGE_DLLCHARACTERISTICS_NX_COMPAT; | ||
| 1329 | oh.SizeOfStackReserve = oh.SizeOfHeapReserve = 0x100000; | ||
| 1330 | oh.SizeOfStackCommit = oh.SizeOfHeapCommit = 0x1000; | ||
| 1331 | auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200); | ||
| 1332 | ed->Base = 1; | ||
| 1333 | ed->NumberOfFunctions = 1; | ||
| 1334 | ed->AddressOfFunctions = 0x1100; | ||
| 1335 | ed->NumberOfNames = 1; | ||
| 1336 | ed->AddressOfNames = 0x1120; | ||
| 1337 | ed->AddressOfNameOrdinals = 0x1140; | ||
| 1338 | *reinterpret_cast<uint32_t*>(img.data() + 0x300) = 0x1180; // code, outside the export directory | ||
| 1339 | *reinterpret_cast<uint32_t*>(img.data() + 0x320) = 0x1160; | ||
| 1340 | *reinterpret_cast<uint16_t*>(img.data() + 0x340) = 0; | ||
| 1341 | std::memcpy(img.data() + 0x360, "Answer\0", 7); | ||
| 1342 | const uint8_t code[] = {0xB8, 0x2A, 0x00, 0x00, 0x00, 0xC3}; // mov eax, 42; ret | ||
| 1343 | std::memcpy(img.data() + 0x380, code, sizeof(code)); | ||
| 1344 | auto& dir = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; | ||
| 1345 | dir.VirtualAddress = 0x1000; | ||
| 1346 | dir.Size = 0x170; | ||
| 1347 | return img; | ||
| 1348 | } | ||
| 1349 | |||
| 1350 | // An importer of `dll`!`symbol` bound under LEONIDA_COMPAT_DIR = `compat` (empty = unset). Returns its one record and | ||
| 1351 | // writes the IAT slot to `bound`. | ||
| 1352 | rt::ImportRecord bind_with_compat(const std::filesystem::path& compat, const char* dll, const char* symbol, | ||
| 1353 | void*& bound) { | ||
| 1354 | if (compat.empty()) SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", nullptr); | ||
| 1355 | else SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", compat.c_str()); | ||
| 1356 | struct Reset { | ||
| 1357 | ~Reset() { SetEnvironmentVariableW(L"LEONIDA_COMPAT_DIR", nullptr); } | ||
| 1358 | } reset; | ||
| 1359 | rt::Loader loader(g_dir); | ||
| 1360 | rt::Module& m = loader.load(write_image(make_import_image(dll, symbol))); | ||
| 1361 | if (m.imports.size() != 1) throw std::runtime_error("expected one import record"); | ||
| 1362 | bound = *reinterpret_cast<void**>(m.base + 0x1180); | ||
| 1363 | return m.imports[0]; | ||
| 1364 | } | ||
| 1365 | |||
| 1366 | void test_compat_modules() { | ||
| 1367 | const std::filesystem::path compat = g_dir / "compat"; | ||
| 1368 | std::filesystem::create_directories(compat); | ||
| 1369 | const auto dll_path = compat / "testcompat.dll"; | ||
| 1370 | write_image_as(make_os_dll(0x1A0000000), "compat/testcompat.dll"); | ||
| 1371 | write_image_as(make_os_dll(0x1B0000000), "compat/testshim.dll"); // has Answer, not the shim's F | ||
| 1372 | try { | ||
| 1373 | void* bound = nullptr; | ||
| 1374 | { | ||
| 1375 | const char* what = "without LEONIDA_COMPAT_DIR an import with no shim stays unimplemented"; | ||
| 1376 | auto r = bind_with_compat({}, "testcompat.dll", "Answer", bound); | ||
| 1377 | if (r.binding != rt::Binding::Unimplemented) failed(what, "it was bound"); | ||
| 1378 | else pass(what); | ||
| 1379 | } | ||
| 1380 | { | ||
| 1381 | const char* what = "LEONIDA_COMPAT_DIR binds an import to the drop-in DLL's export, and it runs"; | ||
| 1382 | auto r = bind_with_compat(compat, "TestCompat.dll", "Answer", bound); | ||
| 1383 | HMODULE h = GetModuleHandleW(dll_path.c_str()); | ||
| 1384 | auto expected = h ? reinterpret_cast<void*>(GetProcAddress(h, "Answer")) : nullptr; | ||
| 1385 | if (r.binding != rt::Binding::Host) failed(what, "the import was not bound as Host"); | ||
| 1386 | else if (!expected || bound != expected) failed(what, "the IAT does not hold the DLL's export"); | ||
| 1387 | else if (reinterpret_cast<int (*)()>(bound)() != 42) failed(what, "the export did not return 42"); | ||
| 1388 | else pass(what); | ||
| 1389 | } | ||
| 1390 | { | ||
| 1391 | const char* what = "a compat DLL owns its module: a symbol it lacks is unimplemented, not shimmed"; | ||
| 1392 | auto r = bind_with_compat(compat, "testshim.dll", "F", bound); | ||
| 1393 | if (r.binding != rt::Binding::Unimplemented) failed(what, "F was not left unimplemented"); | ||
| 1394 | else if (!bound || bound == reinterpret_cast<void*>(&test_shim_function)) failed(what, "F reached the shim"); | ||
| 1395 | else pass(what); | ||
| 1396 | } | ||
| 1397 | { | ||
| 1398 | const char* what = "a DLL that is not in the compat folder still binds to its shim"; | ||
| 1399 | std::filesystem::path empty = g_dir / "compat_empty"; | ||
| 1400 | std::filesystem::create_directories(empty); | ||
| 1401 | auto r = bind_with_compat(empty, "testshim.dll", "F", bound); | ||
| 1402 | if (r.binding != rt::Binding::Shim || bound != reinterpret_cast<void*>(&test_shim_function)) | ||
| 1403 | failed(what, "F did not bind to the test shim"); | ||
| 1404 | else pass(what); | ||
| 1405 | } | ||
| 1406 | { | ||
| 1407 | const char* what = "a compat file Windows cannot load is reported, not skipped"; | ||
| 1408 | std::filesystem::path bad = g_dir / "compat_bad"; | ||
| 1409 | std::filesystem::create_directories(bad); | ||
| 1410 | write_image_as(std::vector<uint8_t>(64, 0x5A), "compat_bad/testcompat.dll"); | ||
| 1411 | try { | ||
| 1412 | bind_with_compat(bad, "testcompat.dll", "Answer", bound); | ||
| 1413 | failed(what, "the load succeeded"); | ||
| 1414 | } catch (const std::exception& e) { | ||
| 1415 | if (std::string(e.what()).find("LEONIDA_COMPAT_DIR") == std::string::npos) failed(what, e.what()); | ||
| 1416 | else pass(what); | ||
| 1417 | } | ||
| 1418 | } | ||
| 1419 | } catch (const std::exception& e) { | ||
| 1420 | failed("LEONIDA_COMPAT_DIR", e.what()); | ||
| 1421 | } | ||
| 1422 | // Loaders keep compat DLLs for the process lifetime; drop the references so the work folder can be removed. | ||
| 1423 | for (const auto& p : {dll_path, compat / "testshim.dll"}) | ||
| 1424 | while (HMODULE h = GetModuleHandleW(p.c_str())) FreeLibrary(h); | ||
| 1425 | } | ||
| 1426 | |||
| 1311 | } // namespace | 1427 | } // namespace |
| 1312 | 1428 | ||
| 1313 | int main() { | 1429 | int main() { |
| @@ -1354,6 +1470,7 @@ int main() { | |||
| 1354 | test_tls_directory_count(); | 1470 | test_tls_directory_count(); |
| 1355 | test_unwind_registration(); | 1471 | test_unwind_registration(); |
| 1356 | test_lookup_during_loads(); | 1472 | test_lookup_during_loads(); |
| 1473 | test_compat_modules(); | ||
| 1357 | } catch (const std::exception& e) { | 1474 | } catch (const std::exception& e) { |
| 1358 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. | 1475 | // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. |
| 1359 | failed("writing the test images", e.what()); | 1476 | failed("writing the test images", e.what()); |