Better aslr patch errors

This commit is contained in:
Federico Cecchetto 2022-06-25 00:10:45 +02:00
parent 17d4e4d35a
commit 1eebe439f5

View File

@ -40,12 +40,12 @@ launcher::mode detect_mode_from_arguments()
return launcher::mode::none; return launcher::mode::none;
} }
bool apply_aslr_patch(std::string* data) void apply_aslr_patch(std::string* data)
{ {
// mp binary, sp binary // mp binary, sp binary
if (data->size() != 0x1B97788 && data->size() != 0x1346D88) if (data->size() != 0x1B97788 && data->size() != 0x1346D88)
{ {
return false; throw std::runtime_error("File size mismatch, bad game files");
} }
auto* dos_header = reinterpret_cast<PIMAGE_DOS_HEADER>(&data->at(0)); auto* dos_header = reinterpret_cast<PIMAGE_DOS_HEADER>(&data->at(0));
@ -56,22 +56,26 @@ bool apply_aslr_patch(std::string* data)
{ {
optional_header->DllCharacteristics &= ~(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE); optional_header->DllCharacteristics &= ~(IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE);
} }
return true;
} }
void get_aslr_patched_binary(std::string* binary, std::string* data) void get_aslr_patched_binary(std::string* binary, std::string* data)
{ {
std::string patched_binary = "h1-mod\\" + *binary; const auto patched_binary = "h1-mod\\"s + *binary;
if (!apply_aslr_patch(data) || try
(!utils::io::file_exists(patched_binary) &&
!utils::io::write_file(patched_binary, *data, false)))
{ {
throw std::runtime_error(utils::string::va( apply_aslr_patch(data);
"Could not create aslr patched binary!\n(%s)", if (!utils::io::file_exists(patched_binary) && !utils::io::write_file(patched_binary, *data, false))
binary->data() {
)); throw std::runtime_error("Could not write file");
}
}
catch (const std::exception& e)
{
throw std::runtime_error(
utils::string::va("Could not create aslr patched binary for %s! %s",
binary->data(), e.what())
);
} }
*binary = patched_binary; *binary = patched_binary;