Skip to content

Commit

Permalink
Add hat::process::is_writable
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroMemes committed Oct 8, 2024
1 parent d1c34d0 commit f4cee10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/libhat/Process.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace hat::process {
/// Returns whether the entirety of a given memory region is readable
[[nodiscard]] bool is_readable(std::span<const std::byte> region);

/// Returns whether the entirety of a given memory region is writable
[[nodiscard]] bool is_writable(std::span<const std::byte> region);

/// Returns the module for the current process's base executable
[[nodiscard]] hat::process::module get_process_module();

Expand Down
21 changes: 21 additions & 0 deletions src/os/win32/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ namespace hat::process {
return true;
}

bool is_writable(const std::span<const std::byte> region) {
constexpr DWORD writeFlags = PAGE_EXECUTE_READWRITE
| PAGE_EXECUTE_WRITECOPY
| PAGE_READWRITE
| PAGE_WRITECOPY;
for (auto* addr = region.data(); addr < region.data() + region.size();) {
MEMORY_BASIC_INFORMATION mbi{};
if (!VirtualQuery(addr, &mbi, sizeof(mbi))) {
return false;
}
if (mbi.State != MEM_COMMIT) {
return false;
}
if (!(mbi.Protect & writeFlags)) {
return false;
}
addr = static_cast<const std::byte*>(mbi.BaseAddress) + mbi.RegionSize;
}
return true;
}

hat::process::module get_process_module() {
return module{reinterpret_cast<uintptr_t>(GetModuleHandleW(nullptr))};
}
Expand Down

0 comments on commit f4cee10

Please sign in to comment.