88#include < optional>
99
1010namespace blook {
11+
12+ namespace HwBp {
13+ enum class When { ReadOrWritten, Written, Executed };
14+ }
15+
16+
17+ struct Trampoline {
18+ Pointer pTrampoline;
19+ size_t trampolineSize;
20+ static Trampoline make (Pointer pCode, size_t minByteSize,
21+ bool near_alloc = true );
22+ };
23+
1124class Function ;
1225class InlineHook {
1326 void *target;
14- std::optional<std::vector<unsigned char >> origData;
15- size_t trampoline_size = 0 ;
1627 bool installed = false ;
17-
1828protected:
1929 void *hook_func = nullptr ;
20- void *p_trampoline = nullptr ;
30+ std::optional<Trampoline> trampoline;
31+ std::optional<MemoryPatch> patch;
2132
2233 friend Function;
2334
@@ -26,21 +37,19 @@ class InlineHook {
2637 InlineHook (void *target);
2738 InlineHook (void *target, void *hook_func);
2839 void *trampoline_raw ();
29- inline bool is_installed () const noexcept {
30- return installed;
31- }
40+ inline bool is_installed () const noexcept { return installed; }
3241 template <typename ReturnVal, typename ... Args>
3342 inline auto trampoline_t () -> ReturnVal (*)(Args...) {
34- return reinterpret_cast <ReturnVal (*)(Args...)>(p_trampoline );
43+ return reinterpret_cast <ReturnVal (*)(Args...)>(trampoline-> pTrampoline . data () );
3544 }
3645
3746 template <typename Func> inline auto trampoline_t () -> Func * {
38- return reinterpret_cast <Func *>(p_trampoline );
47+ return reinterpret_cast <Func *>(trampoline-> pTrampoline . data () );
3948 }
4049
4150 template <typename ReturnType, typename ... Args>
4251 inline auto call_trampoline (Args... args) -> ReturnType {
43- return reinterpret_cast <ReturnType (*)(Args...)>(p_trampoline )(args...);
52+ return reinterpret_cast <ReturnType (*)(Args...)>(trampoline-> pTrampoline . data () )(args...);
4453 }
4554
4655 void install (bool try_trampoline = true );
@@ -71,27 +80,59 @@ class InlineHook {
7180 }
7281};
7382
74- class AnywhereHook {
75- std::optional<MemoryPatch> patch;
76- Pointer target;
77- void *hook_func = nullptr ;
78- bool installed = false ;
79-
83+ class VEHHookManager {
8084public:
81- AnywhereHook (Pointer target);
85+ struct VEHHookContext {};
86+ using BreakpointCallback = std::function<void (VEHHookContext &ctx)>;
8287
83- void install (auto &&func) {
84- if (installed)
85- throw std::runtime_error (" The hook was already installed." );
88+ struct HardwareBreakpoint {
89+ void *address = nullptr ;
90+ short dr_index = -1 ;
91+ int size = 0 ;
92+ HwBp::When when = HwBp::When::Executed;
93+ };
8694
87- hook_func = Function::into_safe_function_pointer (
88- std::forward<decltype (func)>(func));
95+ struct SoftwareBreakpoint {
96+ void *address = nullptr ;
97+ std::vector<uint8_t > original_bytes;
98+ };
8999
90- patch = target.reassembly ([](zasm::x86::Assembler a) {
100+ struct PagefaultBreakpoint {
101+ void *address = nullptr ;
102+ int32_t origin_protection = 0 ;
103+ };
91104
92- });
105+ struct HardwareBreakpointInformation {
106+ HardwareBreakpoint bp;
107+ BreakpointCallback callback;
108+ void *trampoline_address = nullptr ;
109+ size_t trampoline_size = 0 ;
110+ };
111+
112+ static VEHHookManager &instance () {
113+ static VEHHookManager instance;
114+ return instance;
93115 }
94116
95- void uninstall () {}
117+ struct HardwareBreakpointHandler {
118+ short dr_index = -1 ;
119+ };
120+
121+ using VEHHookHandler =
122+ std::variant<std::monostate, HardwareBreakpointHandler>;
123+
124+ VEHHookHandler add_breakpoint (HardwareBreakpoint bp,
125+ BreakpointCallback callback);
126+ VEHHookHandler add_breakpoint (SoftwareBreakpoint bp,
127+ BreakpointCallback callback);
128+ VEHHookHandler add_breakpoint (PagefaultBreakpoint bp,
129+ BreakpointCallback callback);
130+ void remove_breakpoint (const VEHHookHandler &handler);
131+
132+ private:
133+ std::array<std::optional<HardwareBreakpointInformation>, 4 > hw_breakpoints;
134+
135+ void sync_hw_breakpoints ();
136+ VEHHookManager () {}
96137};
97138} // namespace blook
0 commit comments