-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
885 additions
and
364 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// This is the OS abstraction layer. | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <expected> | ||
#include <functional> | ||
|
||
namespace safetyhook { | ||
|
||
enum class OsError { | ||
FAILED_TO_ALLOCATE, | ||
FAILED_TO_PROTECT, | ||
FAILED_TO_QUERY, | ||
FAILED_TO_GET_NEXT_THREAD, | ||
FAILED_TO_GET_THREAD_CONTEXT, | ||
FAILED_TO_SET_THREAD_CONTEXT, | ||
FAILED_TO_FREEZE_THREAD, | ||
FAILED_TO_UNFREEZE_THREAD, | ||
FAILED_TO_GET_THREAD_ID, | ||
}; | ||
|
||
struct VmAccess { | ||
bool read : 1; | ||
bool write : 1; | ||
bool execute : 1; | ||
|
||
constexpr bool operator==(const VmAccess& other) const { | ||
return read == other.read && write == other.write && execute == other.execute; | ||
} | ||
}; | ||
|
||
constexpr VmAccess VM_ACCESS_R{.read = true, .write = false, .execute = false}; | ||
constexpr VmAccess VM_ACCESS_RW{.read = true, .write = true, .execute = false}; | ||
constexpr VmAccess VM_ACCESS_RX{.read = true, .write = false, .execute = true}; | ||
constexpr VmAccess VM_ACCESS_RWX{.read = true, .write = true, .execute = true}; | ||
|
||
struct VmBasicInfo { | ||
uint8_t* address; | ||
size_t size; | ||
VmAccess access; | ||
bool is_free; | ||
}; | ||
|
||
std::expected<uint8_t*, OsError> vm_allocate(uint8_t* address, size_t size, VmAccess access); | ||
void vm_free(uint8_t* address); | ||
std::expected<uint32_t, OsError> vm_protect(uint8_t* address, size_t size, VmAccess access); | ||
std::expected<uint32_t, OsError> vm_protect(uint8_t* address, size_t size, uint32_t access); | ||
std::expected<VmBasicInfo, OsError> vm_query(uint8_t* address); | ||
bool vm_is_readable(uint8_t* address, size_t size); | ||
bool vm_is_writable(uint8_t* address, size_t size); | ||
bool vm_is_executable(uint8_t* address); | ||
|
||
struct SystemInfo { | ||
uint32_t page_size; | ||
uint32_t allocation_granularity; | ||
uint8_t* min_address; | ||
uint8_t* max_address; | ||
}; | ||
|
||
SystemInfo system_info(); | ||
|
||
using ThreadId = uint32_t; | ||
using ThreadHandle = void*; | ||
using ThreadContext = void*; | ||
|
||
/// @brief Executes a function while all other threads are frozen. Also allows for visiting each frozen thread and | ||
/// modifying it's context. | ||
/// @param run_fn The function to run while all other threads are frozen. | ||
/// @param visit_fn The function that will be called for each frozen thread. | ||
/// @note The visit function will be called in the order that the threads were frozen. | ||
/// @note The visit function will be called before the run function. | ||
/// @note Keep the logic inside run_fn and visit_fn as simple as possible to avoid deadlocks. | ||
void execute_while_frozen(const std::function<void()>& run_fn, | ||
const std::function<void(ThreadId, ThreadHandle, ThreadContext)>& visit_fn = {}); | ||
|
||
/// @brief Will modify the context of a thread's IP to point to a new address if its IP is at the old address. | ||
/// @param ctx The thread context to modify. | ||
/// @param old_ip The old IP address. | ||
/// @param new_ip The new IP address. | ||
void fix_ip(ThreadContext ctx, uint8_t* old_ip, uint8_t* new_ip); | ||
|
||
} // namespace safetyhook |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.