Skip to content

Commit c680d76

Browse files
refactor
1 parent 9bd9816 commit c680d76

File tree

7 files changed

+400
-92
lines changed

7 files changed

+400
-92
lines changed

include/blook/function.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ class Function {
7676
template <typename ReturnVal, typename... Args>
7777
static inline auto into_function_pointer(
7878
std::function<ReturnVal(Args...)> *fn) -> ReturnVal (*)(Args...) {
79-
std::cout << (void *)fn << std::endl;
8079
using namespace zasm;
8180

8281
Program program(utils::compileMachineMode());

include/blook/hook.h

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@
88
#include <optional>
99

1010
namespace 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+
1124
class Function;
1225
class InlineHook {
1326
void *target;
14-
std::optional<std::vector<unsigned char>> origData;
15-
size_t trampoline_size = 0;
1627
bool installed = false;
17-
1828
protected:
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 {
8084
public:
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

include/blook/memo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class Pointer {
3434

3535
protected:
3636
size_t _offset = 0;
37-
std::shared_ptr<Process> proc = nullptr;
3837
friend MemoryPatch;
3938

4039
public:
40+
std::shared_ptr<Process> proc = nullptr;
4141
static void *malloc_rwx(size_t size);
4242

4343
static void protect_rwx(void *p, size_t size);

0 commit comments

Comments
 (0)