Repository navigation
Expand file tree
/
Copy pathpool.c
More file actions
122 lines (112 loc) · 5.86 KB
/
Copy pathpool.c
File metadata and controls
122 lines (112 loc) · 5.86 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
// From the Attila mod (heapfix/pool.c): the eight functions are byte-identical in ToB, only their addresses differ.
// Faster "which pool chunk owns this pointer" for the game's small-object pools.
// empire.retail.dll frees a pooled object by walking the chunk list of every size class until a
// chunk contains the pointer (+0x7AF3E0 (Attila FUN_107b6f60) for 18 classes of 4..0xA0 bytes, +0x7AF4F0 (Attila FUN_107b7070) for 19
// classes of 0xA4..0x774 bytes). Pools gain chunks as a campaign goes on, so frees get slower.
// The replacement gives the same answer but tries a per-thread cache first (pool + page -> class,
// chunk), verified against the live chunk list and range, and moves the owning chunk to the front
// of its list. A stale cache entry only costs a full search, never a wrong answer.
#include <windows.h>
#include <string.h>
struct node { char *base; struct node *next; };
struct cls { void *freelist; struct node *chunks; unsigned count; };
static const unsigned SIZES_A[18] = {4, 6, 8, 12, 0x10, 0x12, 0x14, 0x18, 0x20,
0x2a, 0x30, 0x38, 0x40, 0x6c, 0x70, 0x90, 0x9c, 0xa0};
static const unsigned SIZES_B[19] = {0xa4, 0xa8, 0xac, 0xb0, 0xb4, 0xb8, 0xbc, 0xc0, 0xc8, 0xd0,
0xe0, 0xe4, 0xf0, 0xf4, 0x108, 0x14c, 0x154, 0x1d4, 0x774};
#define CACHE_SLOTS 4096
struct centry { struct cls *pool; unsigned page; struct node *n; unsigned char k; };
// allocated at a thread's first pool free, freed when it ends (pool_lookup_thread_exit): a static __thread array was
// 64 KB of TLS for every thread in the process, most never freeing a pool object (Attila's ultrareview 2026-11-02)
static __thread struct centry *t_cache;
void pool_lookup_thread_exit(void) {
if (t_cache) HeapFree(GetProcessHeap(), 0, t_cache);
t_cache = NULL;
}
static unsigned char take(struct cls *c, struct node **link, struct node *n, unsigned size, char *p, unsigned *out) {
if (link != &c->chunks) { // move to front
*link = n->next;
n->next = c->chunks;
c->chunks = n;
}
*(void **)p = c->freelist;
c->freelist = p;
*out = size;
return 1;
}
static unsigned char pool_free(struct cls *pool, const unsigned *sizes, int ncls, char *p, unsigned *out) {
unsigned page = (unsigned)(UINT_PTR)p >> 12;
if (!t_cache) {
DWORD err = GetLastError(); // the game's free must not change it
t_cache = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, CACHE_SLOTS * sizeof *t_cache);
SetLastError(err);
}
struct centry none = {0}; // no memory for the cache: the full search, nothing remembered
struct centry *e = t_cache ? &t_cache[(page ^ ((unsigned)(UINT_PTR)pool >> 4) * 0x9E3779B1u) & (CACHE_SLOTS - 1)] : &none;
if (e->pool == pool && e->page == page && e->k < ncls) {
struct cls *c = &pool[e->k];
for (struct node **link = &c->chunks, *n; (n = *link) != NULL; link = &n->next)
if (n == e->n) {
if (n->base <= p && p < n->base + c->count * sizes[e->k])
return take(c, link, n, sizes[e->k], p, out);
break;
}
}
for (int k = 0; k < ncls; k++) {
struct cls *c = &pool[k];
for (struct node **link = &c->chunks, *n; (n = *link) != NULL; link = &n->next)
if (n->base <= p && p < n->base + c->count * sizes[k]) {
e->pool = pool;
e->page = page;
e->n = n;
e->k = (unsigned char)k;
return take(c, link, n, sizes[k], p, out);
}
}
return 0;
}
unsigned char __thiscall pool_free_a(struct cls *pool, char *p, unsigned *out) { return pool_free(pool, SIZES_A, 18, p, out); }
unsigned char __thiscall pool_free_b(struct cls *pool, char *p, unsigned *out) { return pool_free(pool, SIZES_B, 19, p, out); }
// Every function the replacement stands in for, hashed whole (FNV-1a): the two entry points and
// the six they chain to. Their bytes are position independent (no absolute addresses).
static const struct { DWORD rva, len, fnv; } POOL_FUNCS[] = {
{0x7AF3E0, 267, 0xFB03A4BCu}, {0x7AEC80, 308, 0x54D5B620u}, {0x7AF2C0, 279, 0x2690827Au},
{0x7AEF00, 310, 0x88DAD0F2u}, {0x7AF4F0, 311, 0xCFCF782Du}, {0x7AF040, 311, 0xB6AABF0Fu},
{0x7AF180, 309, 0xCD52FD71u}, {0x7AEDC0, 309, 0xAEA56E55u},
};
static DWORD fnv1a(const unsigned char *b, DWORD n) {
DWORD h = 0x811C9DC5u;
while (n--) h = (h ^ *b++) * 0x01000193u;
return h;
}
static int write_jmp(unsigned char *site, void *to) {
DWORD old;
if (!VirtualProtect(site, 5, PAGE_EXECUTE_READWRITE, &old)) return 0;
site[0] = 0xE9;
DWORD rel = (DWORD)((unsigned char *)to - (site + 5));
memcpy(site + 1, &rel, 4);
VirtualProtect(site, 5, old, &old);
FlushInstructionCache(GetCurrentProcess(), site, 5);
return 1;
}
// Returns 1 if both pool entry points were redirected, 0 if nothing was changed.
int install_pool(HMODULE m, void (*log)(const char *fmt, ...)) {
unsigned char *base = (unsigned char *)m;
for (size_t i = 0; i < sizeof POOL_FUNCS / sizeof POOL_FUNCS[0]; i++)
if (fnv1a(base + POOL_FUNCS[i].rva, POOL_FUNCS[i].len) != POOL_FUNCS[i].fnv) {
log("pool function +0x%X does not match this game version - pool lookup not patched", POOL_FUNCS[i].rva);
return 0;
}
if (!write_jmp(base + 0x7AF3E0, (void *)pool_free_a)) {
log("pool lookup: VirtualProtect failed (%lu) - not patched", GetLastError());
return 0;
}
// Either entry works alone (the replacement and the original give the same answers), so a
// failure here leaves a consistent, partly patched game.
if (!write_jmp(base + 0x7AF4F0, (void *)pool_free_b)) {
log("pool lookup: only the small-size pools were patched (%lu)", GetLastError());
return 1;
}
log("pool free lookup replaced (per-thread cache + move-to-front)");
return 1;
}