SafetyHook: C:/M/B/src/safetyhook-0.6.5/include/safetyhook/os.hpp Source File

SafetyHook

Loading...
Searching...
No Matches
os.hpp
1// This is the OS abstraction layer.
2#pragma once
3
4#ifndef SAFETYHOOK_USE_CXXMODULES
5#include <cstdint>
6#include <expected>
7#include <functional>
8#else
9import std.compat;
10#endif
11
12#include "safetyhook/common.hpp"
13
14namespace safetyhook {
15
16enum class OsError {
17 FAILED_TO_ALLOCATE,
18 FAILED_TO_PROTECT,
19 FAILED_TO_QUERY,
20 FAILED_TO_GET_NEXT_THREAD,
21 FAILED_TO_GET_THREAD_CONTEXT,
22 FAILED_TO_SET_THREAD_CONTEXT,
23 FAILED_TO_FREEZE_THREAD,
24 FAILED_TO_UNFREEZE_THREAD,
25 FAILED_TO_GET_THREAD_ID,
26};
27
28 struct VmAccess {
29 bool read : 1;
30 bool write : 1;
31 bool execute : 1;
32
33 constexpr bool operator==(const VmAccess& other) const {
34 return read == other.read && write == other.write && execute == other.execute;
35 }
36};
37
38constexpr VmAccess VM_ACCESS_R{true, false, false};
39constexpr VmAccess VM_ACCESS_RW{true, true, false};
40constexpr VmAccess VM_ACCESS_RX{true, false, true};
41constexpr VmAccess VM_ACCESS_RWX{true, true, true};
42
43 struct VmBasicInfo {
44 uint8_t* address;
45 size_t size;
46 VmAccess access;
47 bool is_free;
48};
49
50std::expected<uint8_t*, OsError> SAFETYHOOK_API vm_allocate(uint8_t* address, size_t size, VmAccess access);
51void SAFETYHOOK_API vm_free(uint8_t* address);
52std::expected<uint32_t, OsError> SAFETYHOOK_API vm_protect(uint8_t* address, size_t size, VmAccess access);
53std::expected<uint32_t, OsError> SAFETYHOOK_API vm_protect(uint8_t* address, size_t size, uint32_t access);
54std::expected<VmBasicInfo, OsError> SAFETYHOOK_API vm_query(uint8_t* address);
55bool SAFETYHOOK_API vm_is_readable(uint8_t* address, size_t size);
56bool SAFETYHOOK_API vm_is_writable(uint8_t* address, size_t size);
57bool SAFETYHOOK_API vm_is_executable(uint8_t* address);
58
59 struct SystemInfo {
60 uint32_t page_size;
61 uint32_t allocation_granularity;
62 uint8_t* min_address;
63 uint8_t* max_address;
64};
65
66 SystemInfo SAFETYHOOK_API system_info();
67
68using ThreadContext = void*;
69
70void SAFETYHOOK_API trap_threads(uint8_t* from, uint8_t* to, size_t len, const std::function<void()>& run_fn);
71
76void SAFETYHOOK_API fix_ip(ThreadContext ctx, uint8_t* old_ip, uint8_t* new_ip);
77
78} // namespace safetyhook
safetyhook::SystemInfo
Definition os.hpp:59
safetyhook::VmAccess
Definition os.hpp:28
safetyhook::VmBasicInfo
Definition os.hpp:43