forked from Source-Python-Dev-Team/Source.Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_pointer.cpp
More file actions
executable file
·380 lines (329 loc) · 9.94 KB
/
Copy pathmemory_pointer.cpp
File metadata and controls
executable file
·380 lines (329 loc) · 9.94 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
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
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2015 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
// ============================================================================
// >> INCLUDES
// ============================================================================
// Memory
#include "memory_pointer.h"
#include "memory_utilities.h"
// Utilities
#include "utilities/call_python.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/mman.h>
#include <unistd.h>
int PAGE_SIZE = sysconf(_SC_PAGESIZE);
#define ALIGN_PAGE(ar) ((void*) ((long) ar & ~(PAGE_SIZE-1)))
#endif
// ============================================================================
// >> HELPER FUNCTIONS
// ============================================================================
inline int GetProtection(Protection_t prot)
{
switch (prot)
{
#ifdef _WIN32
case PROTECTION_NONE: return PAGE_NOACCESS;
case PROTECTION_READ: return PAGE_READONLY;
case PROTECTION_READ_WRITE: return PAGE_READWRITE;
case PROTECTION_EXECUTE: return PAGE_EXECUTE;
case PROTECTION_EXECUTE_READ: return PAGE_EXECUTE_READ;
case PROTECTION_EXECUTE_READ_WRITE: return PAGE_EXECUTE_READWRITE;
#elif __linux__
case PROTECTION_NONE: return PROT_NONE;
case PROTECTION_READ: return PROT_READ;
case PROTECTION_READ_WRITE: return PROT_READ | PROT_WRITE;
case PROTECTION_EXECUTE: return PROT_EXEC;
case PROTECTION_EXECUTE_READ: return PROT_EXEC | PROT_READ;
case PROTECTION_EXECUTE_READ_WRITE: return PROT_EXEC | PROT_READ | PROT_WRITE;
#else
#error Unsupported platform.
#endif
default: BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Unsupported protection type.")
}
return -1; // To fix a warning...
}
inline int align(int value, int alignment)
{
int unaligned = value % alignment;
if (unaligned == 0)
return value;
return value + alignment - unaligned;
}
// ============================================================================
// >> CPointer
// ============================================================================
CPointer::CPointer(unsigned long ulAddr /* = 0 */, bool bAutoDealloc /* false */)
{
m_ulAddr = ulAddr;
m_bAutoDealloc = bAutoDealloc;
}
const char * CPointer::GetStringArray(int iOffset /* = 0 */)
{
Validate();
TRY_SEGV()
return (const char *) m_ulAddr + iOffset;
EXCEPT_SEGV()
return NULL;
}
void CPointer::SetStringArray(char* szText, int iOffset /* = 0 */)
{
Validate();
TRY_SEGV()
strcpy((char *) (m_ulAddr + iOffset), szText);
EXCEPT_SEGV()
}
CPointer* CPointer::GetPtr(int iOffset /* = 0 */)
{
Validate();
return new CPointer(GetPtrHelper(m_ulAddr + iOffset));
}
void SetPtrHelper(unsigned long addr, unsigned long ptr)
{
TRY_SEGV()
*(unsigned long *) addr = ptr;
EXCEPT_SEGV()
}
void CPointer::SetPtr(object oPtr, int iOffset /* = 0 */)
{
Validate();
SetPtrHelper(m_ulAddr + iOffset, ExtractAddress(oPtr));
}
int CompareHelper(void* first, void* second, unsigned long length)
{
TRY_SEGV()
return memcmp(first, second, length);
EXCEPT_SEGV()
return 0;
}
int CPointer::Compare(object oOther, unsigned long ulNum)
{
Validate();
return CompareHelper((void *) m_ulAddr, (void *) ExtractAddress(oOther, true), ulNum);
}
bool CPointer::IsOverlapping(object oOther, unsigned long ulNumBytes)
{
unsigned long ulOther = ExtractAddress(oOther);
if (m_ulAddr <= ulOther)
return m_ulAddr + ulNumBytes > ulOther;
return ulOther + ulNumBytes > m_ulAddr;
}
void* SearchBytesHelper(unsigned char* base, unsigned char* end, unsigned char* bytes, unsigned long length)
{
TRY_SEGV()
while (base < end)
{
unsigned long i = 0;
for(; i < length; i++)
{
if (bytes[i] == '\x2A')
continue;
if (bytes[i] != base[i])
break;
}
if (i == length)
return base;
base++;
}
EXCEPT_SEGV()
return NULL;
}
CPointer* CPointer::SearchBytes(object oBytes, unsigned long ulNumBytes)
{
Validate();
unsigned long iByteLen = len(oBytes);
if (ulNumBytes < iByteLen)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Search range is too small.")
unsigned char* base = (unsigned char *) m_ulAddr;
unsigned char* end = (unsigned char *) (m_ulAddr + ulNumBytes - (iByteLen - 1));
unsigned char* bytes = (unsigned char *) PyBytes_AsString(oBytes.ptr());
if (!bytes)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to read the given signature.");
return new CPointer((unsigned long) SearchBytesHelper(base, end, bytes, iByteLen));
}
void CopyHelper(void* dest, void* source, unsigned long length)
{
TRY_SEGV()
memcpy(dest, source, length);
EXCEPT_SEGV()
}
void CPointer::Copy(object oDest, unsigned long ulNumBytes)
{
Validate();
if (ulNumBytes == 0) {
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "'num_bytes' must be greater than 0.")
}
if (IsOverlapping(oDest, ulNumBytes))
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Pointers are overlapping!")
CopyHelper((void *) ExtractAddress(oDest, true), (void *) m_ulAddr, ulNumBytes);
}
void MoveHelper(void* dest, void* source, unsigned long length)
{
TRY_SEGV()
memmove(dest, source, length);
EXCEPT_SEGV()
}
void CPointer::Move(object oDest, unsigned long ulNumBytes)
{
Validate();
if (ulNumBytes == 0) {
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "'num_bytes' must be greater than 0.")
}
MoveHelper((void *) ExtractAddress(oDest, true), (void *) m_ulAddr, ulNumBytes);
}
unsigned long GetVirtualFuncHelper(unsigned long addr, int index)
{
TRY_SEGV()
void** vtable = *(void ***) addr;
if (!vtable)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to get the virtual function table.")
return (unsigned long) vtable[index];
EXCEPT_SEGV()
return 0;
}
CPointer* CPointer::GetVirtualFunc(int iIndex)
{
Validate();
return new CPointer(GetVirtualFuncHelper(m_ulAddr, iIndex));
}
CPointer* CPointer::Realloc(int iSize)
{
return new CPointer((unsigned long) UTIL_Realloc((void *) m_ulAddr, iSize));
}
CFunction* CPointer::MakeFunction(CFunctionInfo& info)
{
Validate();
return new CFunction(
m_ulAddr,
object(info.m_eCallingConvention),
info.GetArgumentTypes(),
object(info.m_eReturnType)
);
}
CFunction* CPointer::MakeFunction(object oCallingConvention, object oArgs, object oReturnType)
{
Validate();
return new CFunction(m_ulAddr, oCallingConvention, oArgs, oReturnType);
}
CFunction* CPointer::MakeVirtualFunction(int iIndex, object oCallingConvention, object args, object return_type)
{
CPointer *pPtr = GetVirtualFunc(iIndex);
CFunction *pFunc = pPtr->MakeFunction(oCallingConvention, args, return_type);
delete pPtr;
return pFunc;
}
CFunction* CPointer::MakeVirtualFunction(CFunctionInfo& info)
{
Validate();
if (!info.m_bIsVirtual)
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Function is not a virtual function.")
return new CFunction(
GetVirtualFuncHelper(m_ulAddr + info.m_iVtableOffset, info.m_iVtableIndex),
object(info.m_eCallingConvention),
info.GetArgumentTypes(),
object(info.m_eReturnType)
);
}
void CPointer::CallCallback(PyObject* self, char* szCallback)
{
if (PyObject_HasAttrString(self, szCallback))
{
PyObject* callback = PyObject_GetAttrString(self, szCallback);
if (callback && PyCallable_Check(callback))
{
if (!PyObject_HasAttrString(callback, "__self__"))
{
xdecref(PyObject_CallFunction(callback, "O", self));
}
else
{
xdecref(PyObject_CallMethod(self, szCallback, NULL));
}
}
xdecref(callback);
}
}
void CPointer::PreDealloc(PyObject* self)
{
CallCallback(self, "on_dealloc");
CPointer* pointer = extract<CPointer *>(self);
pointer->Dealloc();
}
CPointer* CPointer::PreRealloc(PyObject* self, int iSize)
{
CallCallback(self, "on_realloc");
CPointer* pointer = extract<CPointer *>(self);
return pointer->Realloc(iSize);
}
void CPointer::__del__(PyObject* self)
{
// This can happens when a subclass fails to initialize before we properly did.
extract<CPointer *> extractor(self);
if (!extractor.check()) {
return;
}
CPointer* ptr = extractor();
if (ptr->m_bAutoDealloc)
{
PythonLog(4, "Automatically deallocating pointer at %u.", ptr->m_ulAddr);
PreDealloc(self);
}
}
IBaseType* CPointer::GetTypeInfo()
{
Validate();
TRY_SEGV()
return GetType((void*) m_ulAddr);
EXCEPT_SEGV()
return NULL;
}
void CPointer::Validate()
{
if (!IsValid())
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Pointer is NULL.")
}
void CPointer::SetProtection(Protection_t prot, int size)
{
#ifdef _WIN32
DWORD old_protect;
if (!VirtualProtect((void *) m_ulAddr, size, GetProtection(prot), &old_protect))
#elif __linux__
unsigned long addr = (unsigned long) ALIGN_PAGE(m_ulAddr);
if (mprotect((void*) addr, align(size + m_ulAddr - addr, PAGE_SIZE), GetProtection(prot)) != 0)
#else
#error Unsupported platform.
#endif
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Failed to set memory protection to %i (size=%i).", prot, size)
}
void CPointer::Protect(int size)
{
SetProtection(PROTECTION_READ, size);
}
void CPointer::UnProtect(int size)
{
SetProtection(PROTECTION_EXECUTE_READ_WRITE, size);
}