upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/loader_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index b9c48f0b3..71a5ede73 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -382,6 +382,56 @@ void test_exports() {
382 } 382 }
383} 383}
384 384
385void test_empty_named_export() {
386 const char* what = "an empty named export stays unresolved without hiding valid exports";
387 auto img = make_forwarder_image("KERNEL32.GetCurrentProcessId", 0x180000000);
388 auto* ed = reinterpret_cast<IMAGE_EXPORT_DIRECTORY*>(img.data() + 0x200);
389 ed->NumberOfFunctions = 3;
390 ed->NumberOfNames = 3;
391 nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = 0x1A0;
392 auto* funcs = reinterpret_cast<uint32_t*>(img.data() + 0x300);
393 funcs[1] = 0;
394 funcs[2] = 0x11C0; // a code export outside the export directory
395 auto* names = reinterpret_cast<uint32_t*>(img.data() + 0x320);
396 names[0] = 0x1188;
397 names[1] = 0x1180;
398 names[2] = 0x1190;
399 auto* ords = reinterpret_cast<uint16_t*>(img.data() + 0x340);
400 ords[0] = 1;
401 ords[1] = 0;
402 ords[2] = 2;
403 std::memcpy(img.data() + 0x388, "empty\0", 6);
404 std::memcpy(img.data() + 0x390, "valid\0", 6);
405 auto provider = write_image_as(img, "empty_exports.dll");
406 try {
407 rt::Loader loader(g_dir);
408 rt::Module& m = loader.load(provider);
409 for (const char* sym : {"empty", "#2"}) {
410 bool owns = false;
411 if (m.exports.count(sym) || m.forwarders.count(sym) ||
412 loader.resolve_module_export(m.base, sym, owns) || !owns)
413 return failed(what, std::string(sym) + " did not remain absent from the mapped module");
414 }
415 for (const char* sym : {"valid", "#3", "fwd", "#1"}) {
416 bool owns = false;
417 void* expected = (std::strcmp(sym, "valid") == 0 || std::strcmp(sym, "#3") == 0)
418 ? m.base + 0x11C0 : host_symbol("kernel32.dll", "GetCurrentProcessId");
419 if (!expected || loader.resolve_module_export(m.base, sym, owns) != expected || !owns)
420 return failed(what, std::string(sym) + " no longer resolves to its valid target");
421 }
422 auto importer = write_image_as(make_import_image("empty_exports.dll", "empty"), "empty_import.exe");
423 rt::Module& importing = loader.load(importer);
424 if (importing.imports.size() != 1 || importing.imports[0].binding != rt::Binding::Unimplemented)
425 return failed(what, "an import of the empty slot did not bind as unimplemented");
426 void* bound = *reinterpret_cast<void**>(importing.base + 0x1180);
427 if (!bound || bound == m.base)
428 return failed(what, "the empty import was not given a failure thunk");
429 pass(what);
430 } catch (const std::exception& e) {
431 failed(what, e.what());
432 }
433}
434
385void test_imports() { 435void test_imports() {
386 { 436 {
387 auto img = make_image(); 437 auto img = make_image();
@@ -1190,6 +1240,7 @@ int main() {
1190 test_truncation(); 1240 test_truncation();
1191 test_relocations(); 1241 test_relocations();
1192 test_exports(); 1242 test_exports();
1243 test_empty_named_export();
1193 test_imports(); 1244 test_imports();
1194 test_import_name_rvas(); 1245 test_import_name_rvas();
1195 test_delay_imports(); 1246 test_delay_imports();