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
..

Directory query ABI regression

This asset-free Windows x64 test dispatches the real kernel thunk for ordinal 207 through a synthetic guest stack. It checks ten-argument stack cleanup, ANSI search-mask translation, continuation, restart, exclusion of dot directories, and rejection of unsupported information classes.

Build and run from the repository root:

cmake -S tests/kernel_directory -B build/kernel-directory -A x64
cmake --build build/kernel-directory --config Release
ctest --test-dir build/kernel-directory -C Release --output-on-failure

The declaration is independently specified by nxdk's lib/xboxkrnl/xboxkrnl.h (NtQueryDirectoryFile) and Cxbx-Reloaded's src/core/kernel/exports/EmuKrnlNt.cpp. This test does not launch a title or require game assets.

The Windows lifecycle regression includes the file backend to inspect its private search table. It verifies both guest ordinal 187 and direct NtClose discard searches, runs 144 close/open cycles (72 per close path) (more than the 64-slot table), and starts newly opened directories without RestartScan. It checks private table ownership directly, so success does not depend on OS handle recycling. Ordinal 301 end-of-search/success/invalid-handle mappings are unchanged controls. No game data is required.