-
Notifications
You must be signed in to change notification settings - Fork 0
/
muitv.h
93 lines (76 loc) · 2.36 KB
/
muitv.h
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
#pragma once
#include <cstdint>
#include <cstdlib>
extern "C" __declspec(dllimport) void* muitv_alloc(size_t size);
extern "C" __declspec(dllimport) void* muitv_calloc(size_t count, size_t size);
extern "C" __declspec(dllimport) void* muitv_realloc(void* ptr, size_t size);
extern "C" __declspec(dllimport) void muitv_free(void* ptr);
extern "C" __declspec(dllimport) size_t muitv_get_size(void* ptr);
extern "C" __declspec(dllimport) void muitv_add_call_stack_to_tree(size_t size);
extern "C" __declspec(dllimport) void muitv_add_object(void* ptr, size_t size);
extern "C" __declspec(dllimport) void muitv_remove_object(void* ptr);
#if !defined(MUITV_MANUAL_ONLY)
void* operator new(size_t size)
{
return muitv_alloc(size);
}
void* operator new[](size_t size)
{
return muitv_alloc(size);
}
void operator delete(void* ptr)
{
muitv_free(ptr);
}
void operator delete[](void* ptr)
{
muitv_free(ptr);
}
#endif
#if defined(MUITV_OVERRIDE_CLIB_MALLOC)
extern "C" __declspec(dllimport) int __stdcall VirtualProtect(void* lpAddress, unsigned long dwSize, unsigned long flNewProtect, unsigned long* lpflOldProtect);
namespace muitv
{
static inline void patch_with_jump(void* dest, void* address)
{
// get offset for relative jmp
unsigned int offset = (unsigned int)((char*)address - (char*)dest - 5);
// unprotect memory
unsigned long old_protect;
VirtualProtect(dest, 5, 0x04, &old_protect);
// write jmp
*(unsigned char*)dest = 0xe9;
*(unsigned int*)((char*)dest + 1) = offset;
// protect memory
VirtualProtect(dest, 5, old_protect, &old_protect);
}
unsigned patch_clib_functions()
{
muitv_free(0);
patch_with_jump((void*)malloc, (void*)muitv_alloc);
patch_with_jump((void*)calloc, (void*)muitv_calloc);
patch_with_jump((void*)realloc, (void*)muitv_realloc);
patch_with_jump((void*)free, (void*)muitv_free);
return 1;
}
#pragma warning(push)
#pragma warning(disable: 4073)
#pragma init_seg(lib)
static const unsigned patch_clib_functions_ready = patch_clib_functions();
#pragma warning(pop)
}
#else
namespace muitv
{
unsigned patch_clib_functions()
{
muitv_free(0);
return 0;
}
#pragma warning(push)
#pragma warning(disable: 4073)
#pragma init_seg(lib)
static const unsigned patch_clib_functions_ready = patch_clib_functions();
#pragma warning(pop)
}
#endif