Handle invalid page protections

This fixes #420
This commit is contained in:
momo5502
2025-07-20 09:34:25 +02:00
parent 6a1eb46fd2
commit 6eb4ef33ff
4 changed files with 36 additions and 10 deletions

View File

@@ -20,7 +20,7 @@ inline std::string get_permission_string(const memory_permission permission)
return res;
}
inline nt_memory_permission map_nt_to_emulator_protection(uint32_t nt_protection)
inline std::optional<nt_memory_permission> try_map_nt_to_emulator_protection(uint32_t nt_protection)
{
memory_permission_ext ext = memory_permission_ext::none;
// TODO: Check for invalid combinations
@@ -51,14 +51,26 @@ inline nt_memory_permission map_nt_to_emulator_protection(uint32_t nt_protection
case PAGE_EXECUTE_READWRITE:
common = memory_permission::all;
break;
case 0:
case PAGE_EXECUTE_WRITECOPY:
default:
throw std::runtime_error("Failed to map protection");
return std::nullopt;
}
return nt_memory_permission{common, ext};
}
inline nt_memory_permission map_nt_to_emulator_protection(uint32_t nt_protection)
{
const auto protection = try_map_nt_to_emulator_protection(nt_protection);
if (protection.has_value())
{
return *protection;
}
throw std::runtime_error("Failed to map protection: " + std::to_string(nt_protection));
}
inline uint32_t map_emulator_to_nt_protection(const memory_permission permission)
{
const bool has_exec = (permission & memory_permission::exec) != memory_permission::none;