-
Notifications
You must be signed in to change notification settings - Fork 30
/
sample_01_params.cpp
200 lines (172 loc) · 4.39 KB
/
sample_01_params.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "plh_Hook.h"
#include <stdio.h>
//..............................................................................
// target function -- all register arguments are used up
int foo(
int a, double b,
int c, double d,
int e, double f, // on microsoft x64, e passed on stack
int g, double h,
int i, double j,
int k, double l,
int m, double n, // on systemv amd64, m passed on stack
int o, double p,
int q, double r // on systemv amd64, r passed on stack
)
{
printf(
"foo("
"%d, %f, %d, %f, %d, %f, "
"%d, %f, %d, %f, %d, %f, "
"%d, %f, %d, %f, %d, %f)\n",
a, b, c, d, e, f,
g, h, i, j, k, l,
m, n, o, p, q, r
);
return 123;
}
//..............................................................................
plh::HookAction
fooHookEnter(
void* targetFunc,
void* callbackParam,
size_t frameBase
)
{
printf(
"fooHookEnter(func: %p, param: %p, frame: %p)\n",
targetFunc,
callbackParam,
(void*)frameBase
);
#if (_PLH_CPU_AMD64)
# if (_PLH_CPP_MSC)
plh::RegArgBlock* regArgBlock = (plh::RegArgBlock*)(frameBase + plh::FrameOffset_RegArgBlock);
int a = (int)regArgBlock->m_rcx;
double b = regArgBlock->m_xmm1[0];
int c = (int)regArgBlock->m_r8;
double d = regArgBlock->m_xmm3[0];
plh::VaList va;
plh::vaStart(va, frameBase);
int e = plh::vaArg<int>(va);
double f = plh::vaArg<double>(va);
int g = plh::vaArg<int>(va);
double h = plh::vaArg<double>(va);
int i = plh::vaArg<int>(va);
double j = plh::vaArg<double>(va);
int k = plh::vaArg<int>(va);
double l = plh::vaArg<double>(va);
int m = plh::vaArg<int>(va);
double n = plh::vaArg<double>(va);
int o = plh::vaArg<int>(va);
double p = plh::vaArg<double>(va);
int q = plh::vaArg<int>(va);
double r = plh::vaArg<double>(va);
# elif (_PLH_CPP_GCC)
plh::RegArgBlock* regArgBlock = (plh::RegArgBlock*)(frameBase + plh::FrameOffset_RegArgBlock);
int a = (int)regArgBlock->m_rdi;
int c = (int)regArgBlock->m_rsi;
int e = (int)regArgBlock->m_rdx;
int g = (int)regArgBlock->m_rcx;
int i = (int)regArgBlock->m_r8;
int k = (int)regArgBlock->m_r9;
double b = regArgBlock->m_xmm0[0];
double d = regArgBlock->m_xmm1[0];
double f = regArgBlock->m_xmm2[0];
double h = regArgBlock->m_xmm3[0];
double j = regArgBlock->m_xmm4[0];
double l = regArgBlock->m_xmm5[0];
double n = regArgBlock->m_xmm6[0];
double p = regArgBlock->m_xmm7[0];
plh::VaList va;
plh::vaStart(va, frameBase);
int m = plh::vaArg<int>(va);
int o = plh::vaArg<int>(va);
int q = plh::vaArg<int>(va);
double r = plh::vaArg<double>(va);
# endif
#elif (_PLH_CPU_X86)
plh::VaList va;
plh::vaStart(va, frameBase);
int a = plh::vaArg<int>(va);
double b = plh::vaArg<double>(va);
int c = plh::vaArg<int>(va);
double d = plh::vaArg<double>(va);
int e = plh::vaArg<int>(va);
double f = plh::vaArg<double>(va);
int g = plh::vaArg<int>(va);
double h = plh::vaArg<double>(va);
int i = plh::vaArg<int>(va);
double j = plh::vaArg<double>(va);
int k = plh::vaArg<int>(va);
double l = plh::vaArg<double>(va);
int m = plh::vaArg<int>(va);
double n = plh::vaArg<double>(va);
int o = plh::vaArg<int>(va);
double p = plh::vaArg<double>(va);
int q = plh::vaArg<int>(va);
double r = plh::vaArg<double>(va);
#endif
printf(
" ("
"%d, %f, %d, %f, %d, %f, "
"%d, %f, %d, %f, %d, %f, "
"%d, %f, %d, %f, %d, %f)\n",
a, b, c, d, e, f,
g, h, i, j, k, l,
m, n, o, p, q, r
);
return plh::HookAction_Default;
}
void
fooHookLeave(
void* targetFunc,
void* callbackParam,
size_t frameBase
)
{
plh::RegRetBlock* regRetBlock = (plh::RegRetBlock*)(frameBase + plh::FrameOffset_RegRetBlock);
#if (_PLH_CPU_AMD64)
int returnValue = (int)regRetBlock->m_rax;
#elif (_PLH_CPU_X86)
int returnValue = regRetBlock->m_eax;
#endif
printf(
"fooHookLeave(func: %p, param: %p, frame: %p)\n -> %d\n",
targetFunc,
callbackParam,
(void*)frameBase,
returnValue
);
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
int main()
{
typedef int FooFunc(
int, double,
int, double,
int, double,
int, double,
int, double,
int, double,
int, double,
int, double,
int, double
);
plh::HookArena arena;
plh::Hook* fooHook = arena.allocate((void*)foo, (void*)0xabcdef, fooHookEnter, fooHookLeave);
plh::enableHooks();
((FooFunc*)fooHook)(
1, 10.1,
2, 20.2,
3, 30.3,
4, 40.4,
5, 50.5,
6, 60.6,
7, 70.7,
8, 80.8,
9, 90.9
);
return 0;
}
//..............................................................................