-
Notifications
You must be signed in to change notification settings - Fork 148
/
interop.cpp
382 lines (350 loc) · 10.6 KB
/
interop.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <algorithm>
#include <cstring>
#include <stdexcept>
#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif // _WIN32
#include "../console.h"
#include "interop.hpp"
#pragma warning(disable : 4731) // frame pointer register 'ebp' modified by inline assembly code
#define PLATFORM_X86
#if defined(__GNUC__)
#ifdef __clang__
#define DISABLE_OPT __attribute__((noinline, optnone))
#else
#define DISABLE_OPT __attribute__((noinline, optimize("O0")))
#endif // __clang__
#else
#define DISABLE_OPT
#endif // defined(__GNUC__)
#ifdef USE_MMAP
#if defined(PLATFORM_64BIT)
#define GOOD_PLACE_FOR_DATA_SEGMENT ((uintptr_t)0x200000000)
#elif defined(PLATFORM_32BIT)
#define GOOD_PLACE_FOR_DATA_SEGMENT ((uintptr_t)0x09000000)
#else
#error "Unknown platform"
#endif
#else
#define GOOD_PLACE_FOR_DATA_SEGMENT ((uintptr_t)0x8A4000)
#endif
namespace openloco::interop
{
registers::registers()
{
// We set registers to known undefined values so we are easily aware when
// code is attempting to use undefined registers.
std::memset(this, 0xCC, sizeof(registers));
}
// This variable serves a purpose of identifying a crash if it has happened inside original code.
// When switching to original code, stack frame pointer is modified and prevents breakpad from providing stack trace.
volatile int32_t _originalAddress = 0;
#ifdef _ENABLE_CALL_BYVALUE_
static int32_t DISABLE_OPT call_byval(int32_t address, int32_t _eax, int32_t _ebx, int32_t _ecx, int32_t _edx, int32_t _esi, int32_t _edi, int32_t _ebp)
{
int32_t result = 0;
_originalAddress = address;
#if defined(PLATFORM_X86)
#ifdef _MSC_VER
// clang-format off
__asm {
push ebp
push address
mov eax, _eax
mov ebx, _ebx
mov ecx, _ecx
mov edx, _edx
mov esi, _esi
mov edi, _edi
mov ebp, _ebp
call[esp]
lahf
pop ebp
pop ebp
/* Load result with flags */
mov result, eax
}
// clang-format on
#else
// clang-format off
__asm__ volatile ("\
\n\
push %%ebx \n\
push %%ebp \n\
push %[address] \n\
mov %[eax], %%eax \n\
mov %[ebx], %%ebx \n\
mov %[ecx], %%ecx \n\
mov %[edx], %%edx \n\
mov %[esi], %%esi \n\
mov %[edi], %%edi \n\
mov %[ebp], %%ebp \n\
call *(%%esp) \n\
lahf \n\
add $4, %%esp \n\
pop %%ebp \n\
pop %%ebx \n\
/* Load result with flags */ \n\
mov %%eax, %[result] \n\
" : [address] "+m" (address), [eax] "+m" (_eax), [ebx] "+m" (_ebx), [ecx] "+m" (_ecx), [edx] "+m" (_edx), [esi] "+m" (_esi), [edi] "+m" (_edi), [ebp] "+m" (_ebp), [result] "+m" (result)
:
: "eax", "ecx", "edx", "esi", "edi", "memory"
);
// clang-format on
#endif
#endif // PLATFORM_X86
_originalAddress = 0;
// lahf only modifies ah, zero out the rest
return result & 0xFF00;
}
#endif
static int32_t DISABLE_OPT call_byref(int32_t address, int32_t* _eax, int32_t* _ebx, int32_t* _ecx, int32_t* _edx, int32_t* _esi, int32_t* _edi, int32_t* _ebp)
{
openloco::console::group("0x%x", address);
int32_t result = 0;
_originalAddress = address;
#if defined(PLATFORM_X86)
#ifdef _MSC_VER
// clang-format off
__asm {
// Store C's base pointer
push ebp
push ebx
// Store address to call
push address
// Set all registers to the input values
mov eax, [_eax]
mov eax, [eax]
mov ebx, [_ebx]
mov ebx, [ebx]
mov ecx, [_ecx]
mov ecx, [ecx]
mov edx, [_edx]
mov edx, [edx]
mov esi, [_esi]
mov esi, [esi]
mov edi, [_edi]
mov edi, [edi]
mov ebp, [_ebp]
mov ebp, [ebp]
// Call function
call[esp]
// Store output eax
push eax
push ebp
push ebx
mov ebp, [esp + 20]
mov ebx, [esp + 16]
// Get resulting ecx, edx, esi, edi registers
mov eax, [_edi]
mov[eax], edi
mov eax, [_esi]
mov[eax], esi
mov eax, [_edx]
mov[eax], edx
mov eax, [_ecx]
mov[eax], ecx
// Pop ebx reg into ecx
pop ecx
mov eax, [_ebx]
mov[eax], ecx
// Pop ebp reg into ecx
pop ecx
mov eax, [_ebp]
mov[eax], ecx
pop eax
// Get resulting eax register
mov ecx, [_eax]
mov[ecx], eax
// Save flags as return in eax
lahf
// Pop address
pop ebp
pop ebx
pop ebp
/* Load result with flags */
mov result, eax
}
// clang-format on
#else
// clang-format off
__asm__ volatile ("\
\n\
/* Store C's base pointer*/ \n\
push %%ebp \n\
push %%ebx \n\
\n\
/* Store %[address] to call*/ \n\
push %[address] \n\
\n\
/* Set all registers to the input values*/ \n\
mov %[_eax], %%eax \n\
mov (%%eax), %%eax \n\
mov %[_ebx], %%ebx \n\
mov (%%ebx), %%ebx \n\
mov %[_ecx], %%ecx \n\
mov (%%ecx), %%ecx \n\
mov %[_edx], %%edx \n\
mov (%%edx), %%edx \n\
mov %[_esi], %%esi \n\
mov (%%esi), %%esi \n\
mov %[_edi], %%edi \n\
mov (%%edi), %%edi \n\
mov %[_ebp], %%ebp \n\
mov (%%ebp), %%ebp \n\
\n\
/* Call function*/ \n\
call *(%%esp) \n\
\n\
/* Store output eax */ \n\
push %%eax \n\
push %%ebp \n\
push %%ebx \n\
mov 20(%%esp), %%ebp \n\
mov 16(%%esp), %%ebx \n\
/* Get resulting ecx, edx, esi, edi registers*/ \n\
mov %[_edi], %%eax \n\
mov %%edi, (%%eax) \n\
mov %[_esi], %%eax \n\
mov %%esi, (%%eax) \n\
mov %[_edx], %%eax \n\
mov %%edx, (%%eax) \n\
mov %[_ecx], %%eax \n\
mov %%ecx, (%%eax) \n\
/* Pop ebx reg into ecx*/ \n\
pop %%ecx\n\
mov %[_ebx], %%eax \n\
mov %%ecx, (%%eax) \n\
\n\
/* Pop ebp reg into ecx */\n\
pop %%ecx \n\
mov %[_ebp], %%eax \n\
mov %%ecx, (%%eax) \n\
\n\
pop %%eax \n\
/* Get resulting eax register*/ \n\
mov %[_eax], %%ecx \n\
mov %%eax, (%%ecx) \n\
\n\
/* Save flags as return in eax*/ \n\
lahf \n\
/* Pop address*/ \n\
pop %%ebp \n\
\n\
pop %%ebx \n\
pop %%ebp \n\
/* Load result with flags */ \n\
mov %%eax, %[result] \n\
" : [address] "+m" (address), [_eax] "+m" (_eax), [_ebx] "+m" (_ebx), [_ecx] "+m" (_ecx), [_edx] "+m" (_edx), [_esi] "+m" (_esi), [_edi] "+m" (_edi), [_ebp] "+m" (_ebp), [result] "+m" (result)
:
: "eax", "ecx", "edx", "esi", "edi", "memory"
);
// clang-format on
#endif
#endif // PLATFORM_X86
_originalAddress = 0;
openloco::console::group_end();
// lahf only modifies ah, zero out the rest
return result & 0xFF00;
}
#ifdef _ENABLE_CALL_BYVALUE_
static int32_t call_byval(int32_t address, const registers& registers)
{
return call_byval(
address,
registers.eax,
registers.ebx,
registers.ecx,
registers.edx,
registers.esi,
registers.edi,
registers.ebp);
}
#endif
int32_t call(int32_t address)
{
registers regs;
return call(address, regs);
}
int32_t call(int32_t address, registers& registers)
{
return call_byref(
address,
®isters.eax,
®isters.ebx,
®isters.ecx,
®isters.edx,
®isters.esi,
®isters.edi,
®isters.ebp);
}
uintptr_t remap_address(uintptr_t locoAddress)
{
return GOOD_PLACE_FOR_DATA_SEGMENT - 0x8A4000 + locoAddress;
}
void read_memory(uint32_t address, void* data, size_t size)
{
#ifdef _WIN32
if (!ReadProcessMemory(GetCurrentProcess(), (LPVOID)address, data, size, nullptr))
{
throw std::runtime_error("ReadProcessMemory failed");
}
#else
// We own the pages with PROT_WRITE | PROT_EXEC, we can simply just memcpy the data
std::memcpy(data, (void*)address, size);
#endif // _WIN32
}
void write_memory(uint32_t address, const void* data, size_t size)
{
#ifdef _WIN32
if (!WriteProcessMemory(GetCurrentProcess(), (LPVOID)address, data, size, nullptr))
{
throw std::runtime_error("WriteProcessMemory failed");
}
#else
// We own the pages with PROT_WRITE | PROT_EXEC, we can simply just memcpy the data
std::memcpy((void*)address, data, size);
#endif // _WIN32
}
save_state::save_state(uintptr_t begin, uintptr_t end)
: begin(begin)
, end(end)
{
state.resize(end - begin);
read_memory(begin, state.data(), state.size());
}
void save_state::reset()
{
interop::write_memory(begin, state.data(), state.size());
}
void save_state::log_diff(const save_state& lhs, const save_state& rhs)
{
// TODO should we allow different base addresses?
// if so then we need to do extra work for that.
auto length = std::min(lhs.state.size(), rhs.state.size());
for (size_t i = 0; i < length; i++)
{
auto left = lhs.state[i];
auto right = rhs.state[i];
if (left != right)
{
uint32_t addr = lhs.begin + i;
std::printf("0x%06X: %02X %02X\n", addr, (uint8_t)left, (uint8_t)right);
}
}
}
bool operator==(const save_state& lhs, const save_state& rhs)
{
return std::equal(
lhs.get_state().begin(),
lhs.get_state().end(),
rhs.get_state().begin(),
rhs.get_state().end());
}
bool operator!=(const save_state& lhs, const save_state& rhs)
{
return !(lhs == rhs);
}
}