Repository navigation
Expand file tree
/
Copy pathmember_function.cpp
More file actions
64 lines (54 loc) · 2.09 KB
/
Copy pathmember_function.cpp
File metadata and controls
64 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// ─── Splice example: member-function hook ─────────────────────────────────
//
// Hooks a non-virtual C++ member function with SPLICE_HOOK_MEMBER. The
// callback receives the implicit `this` as its first explicit argument.
//
// Build:
// cmake --build ... --target splice_example_member_function
//
// Expected output:
// counter.add(5) before hook = 5
// [hook] Counter::add(self=0x.., delta=5)
// counter.add(5) after hook = 1005 (hook added +1000)
// ───────────────────────────────────────────────────────────────────────────
#include <splice/splice.h>
#include <cstdio>
class Counter {
public:
// Non-virtual member — its pointer-to-member carries a real code
// address that SPLICE_HOOK_MEMBER can patch. (A virtual would be a
// vtable offset; hook the resolved slot with SPLICE_HOOK_ADDR instead.)
int add(int delta); // defined out-of-line below (noinline)
int value() const { return m_value; }
private:
int m_value = 0;
};
#if defined(_MSC_VER)
__declspec(noinline)
#else
__attribute__((noinline))
#endif
int Counter::add(int delta) {
m_value += delta;
return m_value;
}
int main() {
Counter c;
std::printf("counter.add(5) before hook = %d\n", c.add(5));
c = Counter{}; // reset
SPLICE_HOOK_MEMBER(Counter::add)
.onInvoke([](auto orig, Counter* self, int delta) {
std::printf("[hook] Counter::add(self=%p, delta=%d)\n",
static_cast<void*>(self), delta);
return orig(self, delta) + 1000;
});
splice::install_all();
if (!splice_is_hooked(reinterpret_cast<void*>(
splice::detail::member_code_addr<decltype(&Counter::add)>(
&Counter::add)))) {
std::printf("(member not patchable on this build — stub backend)\n");
return 0;
}
std::printf("counter.add(5) after hook = %d\n", c.add(5));
return 0;
}