Files
Diamond Lewis 1c9630f2bd posix: HANDLEs are table indices, not object pointers
A HANDLE was the w32_object pointer itself, so nothing could tell a real
object from any other pointer. The Ke* layer casts a dispatcher object
straight across -- `HANDLE hObject = (HANDLE)Object` in
KeWaitForSingleObject -- and under the pointer scheme that cast was
accidentally well-typed: wait_single() took the guest KEVENT's address for
a w32_object and locked its `lock` field. Burnout 3 dies there before the
title screen, faulting at 0x35F464 in pthread_mutex_lock, eight bytes into
the guest object at 0x35F45C. Win32 fails such a call and the kernel is
written against that; only POSIX dereferenced it.

A handle is now HTAB_TAG | (slot + 1) * 4 into a table of live objects. The
tag is not decoration: a bare (slot + 1) * 4 makes the first handles 4 and
8, and the bridge turns any guest word it does not recognise into a HANDLE
by cast, so a title passing a small bogus value would land on a live object
and signal someone else's event. With the handle checkable, three callers
that trusted the lookup are guarded -- CloseHandle, DuplicateHandle and
WaitForSingleObject faulted where they should return ERROR_INVALID_HANDLE.

Two bugs the checkable handle then exposed, both silent before:

  Ke*BasePriorityThread take a PKTHREAD, and the bridge passed
  XBOX_TO_NATIVE(object) -- a native pointer into guest memory that was
  never a thread handle. Every set did nothing and every query answered 0.
  The bridge now resolves the object to the handle that owns it, inverting
  the token -> object map the handle table already shares an index with.

  WaitForMultipleObjectsEx polls each handle with a zero timeout, so an
  invalid one read as "not signalled": the call sat out its timeout and
  answered STATUS_TIMEOUT where Win32 fails, and a title that reads a
  timeout as "not yet" retries forever. It now rejects the whole call up
  front, as Win32 does.

GetTempPathA, CreateDirectoryA, RemoveDirectoryA, GetFileAttributesA and
DeleteFileA come with it, each normalising separators as CreateFileA
already did. Win32 takes either; on POSIX a backslash is an ordinary
filename character, so "<dir>\Child" quietly operated on a sibling with a
backslash in its name.

tests/win32_handles_posix covers both halves of the claim for each handle
type: that the value fits in 32 bits, and that one put through a guest slot
still works. Written against the pointer version first, where it fails 13
ways, four of them SIGSEGV.

Three regressions also stop being Windows-only, and each found one of the
bugs above once it could run: tests/kernel_events used _putenv_s, which
does not exist off Windows; tests/d3d8_smoke never declared
cmake_minimum_required, without which CMake 4 refuses to configure it; and
tests/kernel_directory counted live searches through whichever DIR_CONTEXT
its backend defines.

Let me know if I should break this up into 2 PR's
2026-10-20 22:22:29 -05:00
..

Kernel events

KEVENTs a title builds by writing the header itself, with no KeInitializeEvent call, checked through the thunk dispatcher. Like tests/memory_regressions, it needs no title and no game files: it runs the real memory layout on the small synthetic XBE from tools/conformance.

cmake -S tests/kernel_events -B build/kernel-events -A x64
cmake --build build/kernel-events --config Release
ctest --test-dir build/kernel-events -C Release --output-on-failure
Test What it checks
kernel_events_default No switch set. Pins what every title gets today: the guest address goes to Win32 as a HANDLE, so after KeSetEvent the wait still fails.
kernel_events_title_kevents RECOMP_TITLE_KEVENTS=1. The table below.

Each check fails on v0.12.0 (every wait returns 0xC0000001) and passes with the switch:

Check Why it matters
An unset synchronization event times out A zero timeout must report the state, not an error.
KeSetEvent sets it; the wait succeeds and clears SignalState A synchronization wait consumes the signal, in guest memory too.
A second wait times out again The same.
SignalState written to 1 by the title counts XDK code sets and clears events by writing the header.
A notification event stays set across two waits Notification events are not consumed.
KeResetEvent clears it Host event and guest header agree.

Not covered: a wait that really blocks and is released from another thread (the bridges use a host event for exactly that, so a zero timeout checks the same state), and any title.

The fixture also checks KeWaitForMultipleObjects through the dispatcher: first access at a nonzero WaitAny index; only the selected synchronization event being consumed; WaitAll success and timeout; header writes clearing a stale host signal; notification persistence; failed waits preserving guest state; and mixed header/explicitly initialized events. Every call checks stdcall stack cleanup. Explicit events resolve in both modes; header events remain opt-in, and the default multiple-wait rejection preserves their state.

The new multiple-wait checks fail against upstream main 193e2995 and pass with the bridge fix. These zero-timeout tests do not establish correctness for concurrent guest header writes, event reinitialization, abandoned mutexes, or cross-title compatibility. Synthetic test.xbe is generated locally and is not part of the contribution.