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:
authorlumenpearson <139921639+lumenpearson@users.noreply.github.com>2026-11-22 23:25:25 +0300
committerlumenpearson <139921639+lumenpearson@users.noreply.github.com>2026-11-22 23:25:25 +0300
commitf324bfbc71f0eabf9515bcd7c8b7c9e4b89448bf (patch)
tree2db4ae6fd5412abbbc3e1894f8bc7ed88c8ae514 /runtime/test/loader_test.cpp
parent18380c6d2baf1422109b369860a6e8be5e185b05 (diff)
parentf847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff)
Merge upstream main into SHADER-537
Diffstat (limited to 'runtime/test/loader_test.cpp')
-rw-r--r--runtime/test/loader_test.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/runtime/test/loader_test.cpp b/runtime/test/loader_test.cpp
index 194174b00..73bc49206 100644
--- a/runtime/test/loader_test.cpp
+++ b/runtime/test/loader_test.cpp
@@ -184,6 +184,28 @@ void expect_rejects(const char* what, const std::vector<uint8_t>& bytes) {
184 184
185void test_valid_image() { expect_loads("a valid image maps with its section intact", make_image()); } 185void test_valid_image() { expect_loads("a valid image maps with its section intact", make_image()); }
186 186
187void test_entry_bounds() {
188 for (bool is_dll : {false, true}) {
189 for (uint32_t entry : {0u, 0x1000u, 0x1FFFu, 0x2000u, 0x2001u}) {
190 auto img = make_image();
191 if (is_dll) nt_of(img)->FileHeader.Characteristics |= IMAGE_FILE_DLL;
192 nt_of(img)->OptionalHeader.AddressOfEntryPoint = entry;
193 const std::string what = std::string(is_dll ? "DLL" : "EXE") + " entry RVA " + std::to_string(entry);
194 if (entry < nt_of(img)->OptionalHeader.SizeOfImage) {
195 expect_loads(what.c_str(), img);
196 } else {
197 std::string error;
198 if (load(write_image(img), error) == 0)
199 failed(what.c_str(), "the image loaded");
200 else if (error.find("the entry point is outside the image") == std::string::npos)
201 failed(what.c_str(), "unexpected error: " + error);
202 else
203 pass(what.c_str());
204 }
205 }
206 }
207}
208
187void test_header_bounds() { 209void test_header_bounds() {
188 { 210 {
189 auto img = make_image(); 211 auto img = make_image();
@@ -941,6 +963,19 @@ std::vector<uint8_t> make_unwind_image(const std::vector<RUNTIME_FUNCTION>& entr
941 return img; 963 return img;
942} 964}
943 965
966// PE/COFF: NumberOfRvaAndSizes counts the data directories; the slots past it are not directories whatever they hold.
967// With 9 directories the TLS slot (index 9) is not one, so bytes left in it must not make the image fail to load.
968void test_tls_directory_count() {
969 auto img = make_image();
970 auto& tls = nt_of(img)->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS];
971 tls.VirtualAddress = 0xFFFF0000; // would run past the image if it were read as a TLS directory
972 tls.Size = sizeof(IMAGE_TLS_DIRECTORY64);
973 nt_of(img)->OptionalHeader.NumberOfRvaAndSizes = IMAGE_DIRECTORY_ENTRY_TLS;
974 expect_loads("a TLS slot past NumberOfRvaAndSizes is not read as a TLS directory", img);
975 nt_of(img)->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
976 expect_rejects("the same TLS slot within NumberOfRvaAndSizes is checked and refused", img);
977}
978
944void test_unwind_registration() { 979void test_unwind_registration() {
945 const std::vector<RUNTIME_FUNCTION> entries = {{0x1000, 0x1010, kUnwindRva}, {0x1010, 0x1020, kUnwindRva}}; 980 const std::vector<RUNTIME_FUNCTION> entries = {{0x1000, 0x1010, kUnwindRva}, {0x1010, 0x1020, kUnwindRva}};
946 uintptr_t pc = 0; 981 uintptr_t pc = 0;
@@ -1093,6 +1128,7 @@ int main() {
1093 return 1; 1128 return 1;
1094 } 1129 }
1095 test_valid_image(); 1130 test_valid_image();
1131 test_entry_bounds();
1096 test_header_bounds(); 1132 test_header_bounds();
1097 test_section_bounds(); 1133 test_section_bounds();
1098 test_truncation(); 1134 test_truncation();
@@ -1109,6 +1145,7 @@ int main() {
1109 test_delay_descriptor_fields(); 1145 test_delay_descriptor_fields();
1110 test_forwarded_exports(); 1146 test_forwarded_exports();
1111 test_export_bounds(); 1147 test_export_bounds();
1148 test_tls_directory_count();
1112 test_unwind_registration(); 1149 test_unwind_registration();
1113 } catch (const std::exception& e) { 1150 } catch (const std::exception& e) {
1114 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process. 1151 // A write that fails (write_image throws) ends the run with the reason, instead of terminating the process.