| 21 | } |
| 22 | |
| 23 | bool WriteExecutableByte(void* target, uint8_t value) { |
| 24 | if (!target) { |
| 25 | OSTP_LOG_WARN("WriteExecutableByte: target is null"); |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | DWORD oldProtect = 0; |
| 30 | if (!VirtualProtect(target, 1, PAGE_EXECUTE_READWRITE, &oldProtect)) { |
| 31 | OSTP_LOG_WARN("WriteExecutableByte(target={}) VirtualProtect(RWX) failed (error={})", |
| 32 | target, GetLastError()); |
| 33 | return false; |
| 34 | } |
| 35 | *static_cast<uint8_t*>(target) = value; |
| 36 | |
| 37 | DWORD ignored = 0; |
| 38 | if (!VirtualProtect(target, 1, oldProtect, &ignored)) { |
| 39 | OSTP_LOG_WARN("WriteExecutableByte(target={}) VirtualProtect(restore=0x{:X}) failed (error={})", |
| 40 | target, oldProtect, GetLastError()); |
| 41 | return false; |
| 42 | } |
| 43 | if (!FlushInstructionCache(GetCurrentProcess(), target, 1)) { |
| 44 | OSTP_LOG_WARN("WriteExecutableByte(target={}) FlushInstructionCache failed (error={})", |
| 45 | target, GetLastError()); |
| 46 | return false; |
| 47 | } |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | } // namespace OSTPlatform::Memory |
no outgoing calls
no test coverage detected