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
14#include "safetyhook/common.hpp"
17#include "safetyhook/utility.hpp"
18
19namespace safetyhook {
20
22using MidHookFn = void (*)(Context& ctx);
23
25class SAFETYHOOK_API MidHook final {
26public:
28 struct Error {
30 enum : uint8_t {
31 BAD_ALLOCATION,
32 BAD_INLINE_HOOK,
33 } type;
34
36 union {
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,
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 for allocating memory near target addresses.
Error
The error type returned by the allocate functions.
Definition allocator.hpp:80
An inline hook.
Definition inline_hook.hpp:23
A mid function hook.
Definition mid_hook.hpp:25
Flags
Flags for MidHook.
Definition mid_hook.hpp:63
@ StartDisabled
Start the hook disabled.
Definition mid_hook.hpp:65
@ Default
Default flags.
Definition mid_hook.hpp:64
std::expected< void, Error > disable()
Disable the hook.
uint8_t * target() const
Get a pointer to the target.
Definition mid_hook.hpp:129
void reset()
Reset the hook.
static std::expected< MidHook, Error > create(T target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object.
Definition mid_hook.hpp:86
uintptr_t target_address() const
Get the address of the target.
Definition mid_hook.hpp:133
static std::expected< MidHook, Error > create(void *target, MidHookFn destination_fn, Flags flags=Default)
Creates a new MidHook object.
std::expected< void, Error > enable()
Enable the hook.
MidHookFn destination() const
Get the destination function.
Definition mid_hook.hpp:137
bool enabled() const
Check if the hook is enabled.
Definition mid_hook.hpp:154
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.
const auto & original_bytes() const
Returns a vector containing the original bytes of the target function.
Definition mid_hook.hpp:141
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 structure for MidHook.
Inline hooking class.
void(*)(Context &ctx) MidHookFn
A MidHook destination function.
Definition mid_hook.hpp:22
Error type for MidHook.
Definition mid_hook.hpp:28
Error type for InlineHook.
Definition inline_hook.hpp:26
Error type for MidHook.
Definition mid_hook.hpp:28
static Error bad_inline_hook(InlineHook::Error err)
Create a BAD_INLINE_HOOK error.
Definition mid_hook.hpp:54
Allocator::Error allocator_error
Allocator error information.
Definition mid_hook.hpp:37
InlineHook::Error inline_hook_error
InlineHook error information.
Definition mid_hook.hpp:38
static Error bad_allocation(Allocator::Error err)
Create a BAD_ALLOCATION error.
Definition mid_hook.hpp:44
enum safetyhook::MidHook::Error::@223334117246373367141155211052230331244233124151 type
The type of error.