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

SafetyHook

Loading...
Searching...
No Matches
mid_hook.hpp
Go to the documentation of this file.
1
3
4#pragma once
5
6#ifndef SAFETYHOOK_USE_CXXMODULES
7#include <cstdint>
8#include <memory>
9#else
10import std.compat;
11#endif
12
13#include "safetyhook/allocator.hpp"
14#include "safetyhook/common.hpp"
15#include "safetyhook/context.hpp"
16#include "safetyhook/inline_hook.hpp"
17#include "safetyhook/utility.hpp"
18
19namespace safetyhook {
20
22 using MidHookFn = void (*)(Context& ctx);
23
25 class SAFETYHOOK_API MidHook final {
26public:
28 struct Error {
30 enum : uint8_t {
31 BAD_ALLOCATION,
32 BAD_INLINE_HOOK,
33 } type;
34
36 union {
37 Allocator::Error allocator_error;
38 InlineHook::Error inline_hook_error;
39 };
40
44 [[nodiscard]] static Error bad_allocation(Allocator::Error err) {
45 Error error{};
46 error.type = BAD_ALLOCATION;
47 error.allocator_error = err;
48 return error;
49 }
50
54 [[nodiscard]] static Error bad_inline_hook(InlineHook::Error err) {
55 Error error{};
56 error.type = BAD_INLINE_HOOK;
57 error.inline_hook_error = err;
58 return error;
59 }
60 };
61
63 enum Flags : int {
64 Default = 0,
65 StartDisabled = 1,
66 };
67
75 [[nodiscard]] static std::expected<MidHook, Error> create(
76 void* target, MidHookFn destination_fn, Flags flags = Default);
77
85 template <typename T>
86 [[nodiscard]] static std::expected<MidHook, Error> create(
87 T target, MidHookFn destination_fn, Flags flags = Default) {
88 return create(reinterpret_cast<void*>(target), destination_fn, flags);
89 }
90
98 [[nodiscard]] static std::expected<MidHook, Error> create(
99 const std::shared_ptr<Allocator>& allocator, void* target, MidHookFn destination_fn, Flags flags = Default);
100
109 template <typename T>
110 [[nodiscard]] static std::expected<MidHook, Error> create(
111 const std::shared_ptr<Allocator>& allocator, T target, MidHookFn destination_fn, Flags flags = Default) {
112 return create(allocator, reinterpret_cast<void*>(target), destination_fn, flags);
113 }
114
115 MidHook() = default;
116 MidHook(const MidHook&) = delete;
117 MidHook(MidHook&& other) noexcept;
118 MidHook& operator=(const MidHook&) = delete;
119 MidHook& operator=(MidHook&& other) noexcept;
120 ~MidHook() = default;
121
125 void reset();
126
129 [[nodiscard]] uint8_t* target() const { return m_target; }
130
133 [[nodiscard]] uintptr_t target_address() const { return reinterpret_cast<uintptr_t>(m_target); }
134
137 [[nodiscard]] MidHookFn destination() const { return m_destination; }
138
141 [[nodiscard]] const auto& original_bytes() const { return m_hook.m_original_bytes; }
142
145 explicit operator bool() const { return static_cast<bool>(m_stub); }
146
148 [[nodiscard]] std::expected<void, Error> enable();
149
151 [[nodiscard]] std::expected<void, Error> disable();
152
154 [[nodiscard]] bool enabled() const { return m_hook.enabled(); }
155
156private:
157 InlineHook m_hook{};
158 uint8_t* m_target{};
159 Allocation m_stub{};
160 MidHookFn m_destination{};
161
162 std::expected<void, Error> setup(
163 const std::shared_ptr<Allocator>& allocator, uint8_t* target, MidHookFn destination);
164};
165} // namespace safetyhook
allocator.hpp
Allocator for allocating memory near target addresses.
safetyhook::Allocator::Error
Error
The error type returned by the allocate functions.
Definition allocator.hpp:80
safetyhook::InlineHook
An inline hook.
Definition inline_hook.hpp:23
safetyhook::MidHook
A mid function hook.
Definition mid_hook.hpp:25
safetyhook::MidHook::Flags
Flags
Flags for MidHook.
Definition mid_hook.hpp:63
safetyhook::MidHook::StartDisabled
@ StartDisabled
Start the hook disabled.
Definition mid_hook.hpp:65
safetyhook::MidHook::Default
@ Default
Default flags.
Definition mid_hook.hpp:64
safetyhook::MidHook::disable
std::expected< void, Error > disable()
Disable the hook.
safetyhook::MidHook::target
uint8_t * target() const
Get a pointer to the target.
Definition mid_hook.hpp:129
safetyhook::MidHook::reset
void reset()
Reset the hook.
safetyhook::MidHook::create
static std::expected< MidHook, Error > create(T target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object.
Definition mid_hook.hpp:86
safetyhook::MidHook::target_address
uintptr_t target_address() const
Get the address of the target.
Definition mid_hook.hpp:133
safetyhook::MidHook::create
static std::expected< MidHook, Error > create(void *target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object.
safetyhook::MidHook::enable
std::expected< void, Error > enable()
Enable the hook.
safetyhook::MidHook::destination
MidHookFn destination() const
Get the destination function.
Definition mid_hook.hpp:137
safetyhook::MidHook::enabled
bool enabled() const
Check if the hook is enabled.
Definition mid_hook.hpp:154
safetyhook::MidHook::create
static std::expected< MidHook, Error > create(const std::shared_ptr< Allocator > &allocator, void *target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object with a given Allocator.
safetyhook::MidHook::original_bytes
const auto & original_bytes() const
Returns a vector containing the original bytes of the target function.
Definition mid_hook.hpp:141
safetyhook::MidHook::create
static std::expected< MidHook, Error > create(const std::shared_ptr< Allocator > &allocator, T target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object with a given Allocator.
Definition mid_hook.hpp:110
context.hpp
Context structure for MidHook.
inline_hook.hpp
Inline hooking class.
safetyhook::MidHookFn
void(*)(Context &ctx) MidHookFn
A MidHook destination function.
Definition mid_hook.hpp:22
MidHook::Error
Error type for MidHook.
Definition mid_hook.hpp:28
safetyhook::InlineHook::Error
Error type for InlineHook.
Definition inline_hook.hpp:26
safetyhook::MidHook::Error
Error type for MidHook.
Definition mid_hook.hpp:28
safetyhook::MidHook::Error::bad_inline_hook
static Error bad_inline_hook(InlineHook::Error err)
Create a BAD_INLINE_HOOK error.
Definition mid_hook.hpp:54
safetyhook::MidHook::Error::allocator_error
Allocator::Error allocator_error
Allocator error information.
Definition mid_hook.hpp:37
safetyhook::MidHook::Error::inline_hook_error
InlineHook::Error inline_hook_error
InlineHook error information.
Definition mid_hook.hpp:38
safetyhook::MidHook::Error::bad_allocation
static Error bad_allocation(Allocator::Error err)
Create a BAD_ALLOCATION error.
Definition mid_hook.hpp:44
safetyhook::MidHook::Error::type
enum safetyhook::MidHook::Error::@256275364120111112142314323223332043336305225220 type
The type of error.