Skip to content

Commit 6d3ffb3

Browse files
committed
Fixed allocation counter
1 parent 264dfe7 commit 6d3ffb3

2 files changed

Lines changed: 31 additions & 46 deletions

File tree

src/FAllocHook.cpp

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#include <containers.hpp>
33
#include "AdapterTracy.hpp"
44
extern std::shared_ptr<ProfilerAdapter> GProfilerAdapter;
5-
#include <Tracy.hpp>
5+
#define TRACY_ENABLE
6+
#include <tracy/Tracy.hpp>
67
using namespace std::string_view_literals;
78

9+
#include <intercept.hpp>
10+
811
FAllocHook::FAllocHook()
912
{
1013
}
@@ -23,62 +26,29 @@ extern "C" {
2326

2427
void afterAlloc() {
2528
auto tracyProf = std::reinterpret_pointer_cast<AdapterTracy>(GProfilerAdapter);
26-
tracyProf->setCounter(allocalloctmp->_allocName, allocalloctmp->allocated_count);
29+
tracyProf->setCounter(allocalloctmp->_allocName, (int64_t)allocalloctmp->allocated_count);
2730
}
2831

2932
void afterFree() {
3033
auto tracyProf = std::reinterpret_pointer_cast<AdapterTracy>(GProfilerAdapter);
31-
tracyProf->setCounter(freealloctmp->_allocName, freealloctmp->allocated_count);
34+
tracyProf->setCounter(freealloctmp->_allocName, (int64_t)freealloctmp->allocated_count);
3235
}
3336

3437

3538
void engineAllocRedir();
3639
void engineFreeRedir();
3740
}
3841

39-
40-
HookManager::Pattern pat_allocReg{ // "Out of FastCAlloc slots"
41-
"xxxxxxxxxxxxxxx?????x????xxx????xx????xxxxx????xxxxxxxxxxx????xxxxxxxxx????x????xxxxxxxxx"sv,
42-
"\x40\x53\x48\x83\xEC\x30\x45\x33\xC9\x48\x8B\xD9\xC7\x44\x24\x00\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x48\x63\x15\x00\x00\x00\x00\x81\xFA\x00\x00\x00\x00\x7D\x1C\x48\x8D\x0D\x00\x00\x00\x00\x48\x8B\xC3\x48\x89\x1C\xD1\xFF\xC2\x89\x15\x00\x00\x00\x00\x48\x83\xC4\x30\x5B\xC3\x48\x8D\x0D\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x48\x8B\xC3\x48\x83\xC4\x30\x5B\xC3"sv
43-
};
44-
45-
HookManager::Pattern pat_allocC{
46-
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx????xxxx????xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx????xxxxxxxxxxx"sv,
47-
"\x40\x53\x48\x83\xEC\x20\xFF\x41\x60\x48\x8B\x41\x08\x48\x8B\xD9\x48\x3B\xC1\x74\x0B\x48\x85\xC0\x74\x06\x48\x83\xC0\xE0\x75\x2B\x48\x8D\x41\x18\x48\x8B\x49\x20\x48\x3B\xC8\x74\x0E\x48\x85\xC9\x74\x09\x48\x8D\x41\xE0\x48\x85\xC0\x75\x10\x48\x8B\xCB\xE8\x00\x00\x00\x00\x84\xC0\x0F\x84\x00\x00\x00\x00\x4C\x8B\x43\x08\x32\xC9\x45\x33\xD2\x4C\x3B\xC3\x74\x0B\x4D\x85\xC0\x74\x06\x49\x83\xC0\xE0\x75\x2A\x4C\x8B\x43\x20\x48\x8D\x43\x18\x4C\x3B\xC0\x0F\x84\x00\x00\x00\x00\x4D\x85\xC0\x74\x06\x49\x83\xC0\xE0\xEB\x03"sv
48-
};
49-
50-
HookManager::Pattern pat_freeC{
51-
"xxxxx????xxxxxxxxxxxxx?xxxxxxxxxxxxxxxxxxxxxxx????x????xxxxxx????xxxxxxx?xxxxxx????xxxxxxxxxx??xxxxxxxxxxx"sv,
52-
"\x48\x85\xD2\x0F\x84\x00\x00\x00\x00\x53\x48\x83\xEC\x20\x48\x63\x41\x58\x48\x89\x7C\x24\x00\x48\x8B\xFA\x48\xFF\xC8\x48\x8B\xD9\x48\x23\xC2\x48\x2B\xF8\x83\x3F\x00\x74\x28\x48\x8D\x0D\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x44\x8B\x07\x48\x8D\x0D\x00\x00\x00\x00\x48\x8B\xD7\x48\x8B\x7C\x24\x00\x48\x83\xC4\x20\x5B\xE9\x00\x00\x00\x00\x48\x8B\x47\x18\x48\x89\x02\x48\x83\x7F\x00\x00\x48\x89\x57\x18\x0F\x94\xC0\x48\x89\x7A\x08"sv
53-
};
54-
5542
void FAllocHook::init() {
5643
#ifndef __linux__
57-
auto found = hooks.findPattern(pat_allocReg, 0x0);
58-
59-
60-
61-
auto aicp = *reinterpret_cast<uint32_t*>(found + 0x1C);
62-
auto rip = found + 0x20;
63-
auto aic = *reinterpret_cast<int32_t*>(rip + aicp);
64-
65-
//+1C
66-
//70 C4 4C 01
67-
//014cc470
68-
//0002932C90
69-
70-
71-
auto aidp = *reinterpret_cast<uint32_t*>(found + 0x2B);
72-
rip = found + 0x2F;
73-
auto aid = reinterpret_cast<intercept::types::rv_pool_allocator**>(rip + aidp);
74-
//71 C4 4C 01
75-
44+
auto poolAlloc = intercept::client::host::functions.get_engine_allocator()->poolFuncAlloc;
45+
auto poolDealloc = intercept::client::host::functions.get_engine_allocator()->poolFuncDealloc;
7646

77-
auto allocF = hooks.findPattern(pat_allocC);
78-
engineAlloc = hooks.placeHookTotalOffs(allocF, reinterpret_cast<uintptr_t>(engineAllocRedir))+2;
47+
auto allocF = poolAlloc; // hooks.findPattern(pat_allocC);
48+
engineAlloc = hooks.placeHookTotalOffs(allocF, reinterpret_cast<uintptr_t>(engineAllocRedir))+4;
7949

80-
auto FreeF = hooks.findPattern(pat_freeC);
81-
engineFree = hooks.placeHookTotalOffs(FreeF + 0x9, reinterpret_cast<uintptr_t>(engineFreeRedir));
50+
auto FreeF = poolDealloc; // We inject after the null check
51+
engineFree = hooks.placeHookTotalOffs(FreeF + 0x9, reinterpret_cast<uintptr_t>(engineFreeRedir))+1;
8252
//
8353
//__debugbreak();
8454
#endif

src/hooks.asm

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,29 +136,36 @@ _TEXT SEGMENT
136136
doEngineAlloc PROC
137137

138138
;fixup
139-
push rbx
139+
mov [rsp+8], rbx
140+
mov [rsp+10h], rsi
141+
push rdi
140142
sub rsp, 20h
141143
inc dword ptr [rcx+60h]
142-
mov rax, [rcx+8]
143-
mov rbx, rcx
144+
144145
jmp engineAlloc;
145146

146147
doEngineAlloc ENDP
147148

148149
doEngineFree PROC
149150

150151
;fixup
152+
151153
push rbx
152154
sub rsp, 20h
153155
movsxd rax, dword ptr [rcx+58h]
154-
mov [rsp+30h], rdi
156+
mov rbx, rcx
157+
dec rax
158+
155159
jmp engineFree;
156160
doEngineFree ENDP
157161

158162

159163
;##########
160164
PUBLIC engineAllocRedir
161165
engineAllocRedir PROC
166+
167+
sub rsp, 20h ; Stack saver, otherwise we corrupt smth
168+
162169
mov allocalloctmp, rcx; get rid of this and just keep rcx on stack
163170
call doEngineAlloc;
164171
push rax;
@@ -175,12 +182,17 @@ _TEXT SEGMENT
175182
pop rcx;
176183
pop rax;
177184

185+
add rsp, 20h ; Undo the stack saver
186+
178187
ret
179188
engineAllocRedir ENDP
180189

181190
;##########
182191
PUBLIC engineFreeRedir
183192
engineFreeRedir PROC
193+
194+
;sub rsp, 40h ; Stack saver, otherwise we corrupt smth
195+
184196
mov freealloctmp, rcx; get rid of this, I keep track of rcx now anyway
185197
push rax;
186198
push r8;
@@ -194,6 +206,9 @@ _TEXT SEGMENT
194206
pop rcx;
195207
pop r8;
196208
pop rax;
209+
210+
;add rsp, 40h ; Undo the stack saver
211+
197212
ret
198213
engineFreeRedir ENDP
199214

0 commit comments

Comments
 (0)