Files
DucielPerf/tests/bench_alloctrack.cpp
DucielandClaude Opus 5.5 a9350ded53 Watchdog: live Lua line numbers; alloctrack: charge arg tables to the callee (0.8.0)
- watchdog: read the running function's position through its call record's
  pc pointer (+0x10, CI_HASFRAME) instead of the stale savedpc copy, which
  gave wrong lines or ":-1".
- alloctrack: hook adjust_varargs (0x6F6200) while tracking is on, so the
  `arg` table of a vararg function is charged to that function rather than
  to its caller (wrappers no longer pay for their callees).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 21:30:34 +01:00

171 lines
7.6 KiB
C++

// Correctness test and cost benchmark for per-addon allocation accounting
// (src/modules/alloctrack_core.h). Must be built as 32-bit (x86): it lays out
// fake Lua 5.0 structures with the client's exact field offsets.
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "../src/modules/alloctrack_core.h"
#include "MinHook.h"
using namespace alloctrack;
static State g_state;
static int g_failures;
static void expect(const char *what, int bucket, const char *name) {
bool ok = strcmp(g_state.buckets[bucket].name, name) == 0;
printf(" %-44s -> %-12s %s\n", what, g_state.buckets[bucket].name, ok ? "ok" : "FAIL");
if (!ok) g_failures++;
}
// --- Fake Lua 5.0 memory ---------------------------------------------------------------
alignas(16) static uint8_t mem[4096];
static uint32_t at(int off) { return (uint32_t)(uintptr_t)(mem + off); }
static void put32(uint32_t a, uint32_t v) { *(uint32_t *)(uintptr_t)a = v; }
static void put8(uint32_t a, uint8_t v) { *(uint8_t *)(uintptr_t)a = v; }
static uint32_t makeTString(int off, const char *s, uint32_t hash) {
uint32_t ts = at(off);
put32(ts + 0x08, hash);
put32(ts + 0x0C, (uint32_t)strlen(s));
memcpy((void *)(uintptr_t)(ts + 0x10), s, strlen(s) + 1);
return ts;
}
static uint32_t L, ci_base, stack;
// stack slots: 0 = Lua function (pfQuest), 1-3 its locals, 4 = C function, 5+ C frame
static void buildState(uint32_t lua_source) {
memset(mem + 1024, 0, sizeof mem - 1024);
uint32_t proto = at(1024);
put32(proto + 0x20, lua_source);
uint32_t lcl = at(1200);
put8(lcl + 6, 0);
put32(lcl + 0x0C, proto);
uint32_t ccl = at(1300);
put8(ccl + 6, 1);
stack = at(2048);
put32(stack + 0 * 16, 6);
put32(stack + 0 * 16 + 8, lcl);
put32(stack + 4 * 16, 6);
put32(stack + 4 * 16 + 8, ccl);
ci_base = at(1600); // 4 records of 0x18
put32(ci_base + 1 * 0x18, stack + 1 * 16); // Lua function
put32(ci_base + 2 * 0x18, stack + 5 * 16); // C function it called
L = at(1800);
put32(L + 0x14, ci_base + 2 * 0x18); // current: the C function
put32(L + 0x28, ci_base);
put32(L + 0x24, ci_base + 3 * 0x18);
put32(L + 0x1C, stack);
put32(L + 0x18, stack + 60 * 16);
}
// --- A stand-in for LuaMemoryRealloc, hooked like the DLL does --------------------------
static volatile uint32_t g_sink;
__attribute__((noinline)) static void *__fastcall fakeRealloc(uint32_t l, void *block, uint32_t oldsize,
uint32_t newsize, uint32_t a, uint32_t b) {
g_sink += newsize;
return block;
}
typedef void *(__fastcall *ReallocFn)(uint32_t, void *, uint32_t, uint32_t, uint32_t, uint32_t);
static ReallocFn o_fake;
static void *__fastcall detour(uint32_t l, void *block, uint32_t oldsize, uint32_t newsize, uint32_t a, uint32_t b) {
account(g_state, l, oldsize, newsize);
return o_fake(l, block, oldsize, newsize, a, b);
}
static double timeCalls(ReallocFn f, uint32_t l, int n) {
LARGE_INTEGER t0, t1, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&t0);
for (int i = 0; i < n; i++) f(l, (void *)(uintptr_t)i, 0, 32 + (i & 31), 0, 0);
QueryPerformanceCounter(&t1);
return (double)(t1.QuadPart - t0.QuadPart) * 1e9 / freq.QuadPart / n;
}
int main() {
init(g_state);
uint32_t pfquest = makeTString(0, "@Interface\\AddOns\\pfQuest\\database.lua", 0x1234);
uint32_t framexml = makeTString(128, "Interface\\FrameXML\\UIParent.lua", 0x2345);
uint32_t inline_xml = makeTString(256, "PlayerFrame:OnEvent", 0x3456);
uint32_t forward = makeTString(384, "@Interface/AddOns/BigWigs/Core.lua", 0x4567);
uint32_t sv_account = makeTString(512, "@WTF\\Account\\ME\\SavedVariables\\pfUI.lua", 0x5678);
uint32_t sv_char = makeTString(640, "@WTF\\Account\\ME\\N'Zoth\\Char\\SavedVariables\\Atlas-CFM.lua", 0x6789);
printf("Attribution:\n");
buildState(pfquest);
expect("C API call made from pfQuest code", currentBucket(g_state, L), "pfQuest");
put32(L + 0x14, ci_base + 1 * 0x18);
expect("pfQuest code itself", currentBucket(g_state, L), "pfQuest");
put32(L + 0x14, ci_base);
expect("no Lua function running", currentBucket(g_state, L), "(engine)");
expect("no lua_State", currentBucket(g_state, 0), "(engine)");
put32(L + 0x14, ci_base + 7); // misaligned call record
expect("corrupted call record", currentBucket(g_state, L), "(engine)");
buildState(framexml);
expect("Blizzard FrameXML code", currentBucket(g_state, L), "FrameXML");
buildState(inline_xml);
expect("XML inline handler (no file path)", currentBucket(g_state, L), "(unknown)");
buildState(forward);
expect("forward slashes in path", currentBucket(g_state, L), "BigWigs");
buildState(sv_account);
expect("account SavedVariables file", currentBucket(g_state, L), "SV:pfUI");
buildState(sv_char);
expect("character SavedVariables file", currentBucket(g_state, L), "SV:Atlas-CFM");
// Vararg `arg` table: built while the caller (pfQuest, current record) is
// calling a BigWigs vararg function sitting just below `base`.
{
buildState(pfquest);
put32(L + 0x14, ci_base + 1 * 0x18);
uint32_t proto2 = at(1400), cl2 = at(1450), slot = stack + 10 * 16;
put32(proto2 + 0x20, forward);
put8(cl2 + 6, 0);
put32(cl2 + 0x0C, proto2);
put32(slot, 6);
put32(slot + 8, cl2);
VarargSaved prev = varargBegin(g_state, L, slot + 16);
expect("vararg arg table: charged to the called function", currentBucket(g_state, L), "BigWigs");
expect("other lua_State during it: unchanged", currentBucket(g_state, 0), "(engine)");
varargEnd(g_state, prev);
expect("after adjust_varargs: the caller again", currentBucket(g_state, L), "pfQuest");
put8(cl2 + 6, 1); // a C function below base (not a Lua call): no override
prev = varargBegin(g_state, L, slot + 16);
expect("non-Lua callee: caller", currentBucket(g_state, L), "pfQuest");
varargEnd(g_state, prev);
}
printf("Accounting:\n");
buildState(pfquest);
reset(g_state);
account(g_state, L, 0, 100); // new block: 100 bytes
account(g_state, L, 100, 40); // shrink: ignored
account(g_state, L, 40, 64); // growth: 24 bytes
account(g_state, L, 64, 0); // free: ignored
int b = currentBucket(g_state, L);
bool acc_ok = g_state.buckets[b].bytes == 124 && g_state.buckets[b].count == 2;
printf(" pfQuest bytes=%llu count=%u (expect 124 / 2) %s\n", (unsigned long long)g_state.buckets[b].bytes,
g_state.buckets[b].count, acc_ok ? "ok" : "FAIL");
if (!acc_ok) g_failures++;
printf("Cost per allocation (10M calls, warm cache):\n");
const int N = 10000000;
double unhooked = timeCalls(&fakeRealloc, L, N);
MH_Initialize();
MH_CreateHook((LPVOID)&fakeRealloc, (LPVOID)&detour, (LPVOID *)&o_fake);
MH_EnableHook((LPVOID)&fakeRealloc);
double hooked = timeCalls(&fakeRealloc, L, N);
put32(L + 0x14, ci_base); // engine bucket: shortest path
double hooked_engine = timeCalls(&fakeRealloc, L, N);
MH_DisableHook((LPVOID)&fakeRealloc);
double after_off = timeCalls(&fakeRealloc, L, N);
printf(" tracking off (hook removed): %6.2f ns\n", unhooked);
printf(" tracking on, addon code: %6.2f ns (+%.2f ns)\n", hooked, hooked - unhooked);
printf(" tracking on, engine alloc: %6.2f ns (+%.2f ns)\n", hooked_engine, hooked_engine - unhooked);
printf(" switched off again: %6.2f ns\n", after_off);
printf("%s\n", g_failures ? "FAIL" : "PASS");
return g_failures ? 1 : 0;
}