Files
Diamond Lewis faf45576f1 heap corruption
MmAllocateContiguousMemory always allocates via VirtualAlloc, which on POSIX is mmap. MmFreeContiguousMemory picks its deallocator by asking VirtualQuery whether mbi.AllocationBase == BaseAddress. The POSIX VirtualQuery hardcodes AllocationBase = NULL. The comment says so outright: /* != address -> freed via _aligned_free */.
`_aligned_free(ptr) { free(ptr); }`
So the branch is unreachable in the VirtualFree direction, and every contiguous-memory alloc/free pair hands an mmap'd pointer to free().
2026-09-17 19:11:04 -05:00

110 lines
4.4 KiB
C

/*
* The kernel bridge.
*
* A title does not call the kernel by name. Its indirect calls land on a
* thunk table that xbox_kernel_bridge_init() has rewritten to synthetic VAs,
* and kernel_thunk_dispatch turns each of those into a bridge_* function.
* That is the path the runtime actually executes, and it is easy to leave
* untested: bridge_* functions are static and read guest CPU state rather
* than taking arguments, so they cannot be called directly.
*
* They are reachable all the same. recomp_lookup_kernel is not static: given
* a buffer standing in for guest memory it selects the slot and hands back
* the dispatcher. tests/kernel_directory uses the same seam for the directory
* ABI; this one covers kernel memory ordinals.
*
* Checked here with ordinal 173, MmGetPhysicalAddress, which had two
* implementations that disagreed -- the bridge translating through the
* contiguous window and xbox_MmGetPhysicalAddress returning its argument
* unchanged. The bridge now calls the latter, and that delegation is what
* these checks pin: asserting only on xbox_MmGetPhysicalAddress leaves a
* bridge that stops delegating entirely green.
*/
/* kernel.h brings the NT type vocabulary: <windows.h> on Windows, the
* shim in platform/xbox_winnt.h elsewhere. Nothing below is
* platform-specific. */
#include "kernel.h"
#include "xbox_memory_layout.h" /* RECOMP_TLS */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Provided by the generated title; a regression harness has no title, and a
* static archive resolves only what gets pulled in. Nothing here calls a
* guest function. */
typedef void (*recomp_func_t)(void);
recomp_func_t recomp_lookup(uint32_t xbox_va);
recomp_func_t recomp_lookup(uint32_t xbox_va) { (void)xbox_va; return NULL; }
recomp_func_t recomp_lookup_manual(uint32_t xbox_va);
recomp_func_t recomp_lookup_manual(uint32_t xbox_va) { (void)xbox_va; return NULL; }
/* The seam, plus the guest CPU state the dispatcher reads and writes. */
extern recomp_func_t recomp_lookup_kernel(uint32_t xbox_va);
extern RECOMP_TLS uint32_t g_eax, g_esp;
extern ptrdiff_t g_xbox_mem_offset;
static int failures;
static void check(int ok, const char *what, const char *detail)
{
printf(" %-58s %s", what, ok ? "PASS" : "FAIL");
if (!ok && detail) printf(" -- %s", detail);
putchar('\n');
if (!ok) failures++;
}
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
const uint32_t THUNK_VA = 0x10000, STACK_VA = 0x20000;
uint8_t *mem = VirtualAlloc(NULL, 16 * 1024 * 1024,
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
check(mem != NULL, "guest memory stand-in allocated", NULL);
if (!mem) {
printf("\n%d failure(s); the rest needs guest memory.\n", failures);
return 1;
}
g_xbox_mem_offset = (ptrdiff_t)mem;
/* One import, in the form an XBE stores it: 0x80000000 | ordinal. */
*(uint32_t *)(mem + THUNK_VA) = 0x80000000u | 173u;
xbox_kernel_set_thunk_address(THUNK_VA, 1);
xbox_kernel_bridge_init();
/* bridge_init rewrites the entry in place to a synthetic VA. */
recomp_func_t fn = recomp_lookup_kernel(*(uint32_t *)(mem + THUNK_VA));
check(fn != NULL, "ordinal 173 resolves to a bridge entry point",
"the thunk entry was not rewritten to a synthetic VA");
if (fn) {
/* stdcall: the guest pushed a return address, then the argument. */
uint32_t *sp = (uint32_t *)(mem + STACK_VA);
sp[0] = 0xBEEF0001u; /* guest return address */
sp[1] = 0x80001000u; /* inside the contiguous window */
g_esp = STACK_VA;
g_eax = 0xDEADBEEFu;
fn();
char detail[128];
snprintf(detail, sizeof detail,
"bridge returned 0x%08X, expected 0x00001000", g_eax);
check(g_eax == 0x00001000u,
"ordinal 173 translates through the contiguous window", detail);
/* And the argument must come off the guest stack. A wrong
* stdcall_args_for_ordinal entry corrupts the caller's frame away
* from the call, with nothing naming the ordinal. */
snprintf(detail, sizeof detail, "esp is 0x%08X, expected 0x%08X",
g_esp, STACK_VA + 8);
check(g_esp == STACK_VA + 8,
"ordinal 173 pops its stdcall argument", detail);
}
VirtualFree(mem, 0, MEM_RELEASE);
printf("\n%d failure(s)\n", failures);
return failures ? 1 : 0;
}