Skip to content

Run hot kernel32 TEB and critical section calls in guest mode - #159

Open
cweerm wants to merge 1 commit into
decompals:mainfrom
cweerm:fast-thunks
Open

cweerm wants to merge 1 commit into
decompals:mainfrom
cweerm:fast-thunks

Conversation

@cweerm

@cweerm cweerm commented Oct 28, 2026

Copy link
Copy Markdown

Runs GetCurrentThreadId, GetLastError, SetLastError, and the uncontended and recursive paths of EnterCriticalSection and LeaveCriticalSection in guest mode, without going through the host thunk.

The generated thunks swap FS and GS and switch stacks to reach the C++ implementation. For calls that only touch the TEB, that costs far more than the work itself. The stubs live in src/fast_thunks.S. A contended critical section, or a release that has to wake a waiter, falls through to the existing host implementation.

installTibForCurrentThread now fills ClientId in the TEB, because the GetCurrentThreadId stub reads the thread id from there.

Does not depend on #157 or #158.

Testing: the repo CI workflow, run on this branch in my fork, passes everything except test_namedpipe, see below.

test_namedpipe fails in the Ubuntu 32-bit CI jobs on current main as well (test_namedpipe.c:64, seen in a fork CI run of d394386), so that failure is not from this PR.

🤖 Generated with Claude Code

@simonlindholm simonlindholm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me modulo minor comments, but I recall @encounter having some opinions along the lines of preferring fast paths to be written in (32-bit, Windows-targetting, similar to MSVCRT DLLs) C, because asm isn't very maintainable and more fast paths are bound to get added. Is that something that would be viable?

Comment thread src/macros.h
// RTL_CRITICAL_SECTION
#define CS_LOCK_COUNT 0x4
#define CS_RECURSION_COUNT 0x8
#define CS_OWNING_THREAD 0xc

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't put right this in the middle of TEB offset macros, and give them some names that indicate that they are struct offsets

Comment thread src/types.h
static_assert(offsetof(TEB, Peb) == 0x30, "PEB pointer offset mismatch");
static_assert(offsetof(TEB, LastErrorValue) == 0x34, "LastErrorValue offset mismatch");
static_assert(offsetof(TEB, LastErrorValue) == TEB_LAST_ERROR, "LastErrorValue offset mismatch");
static_assert(offsetof(TEB, ClientId) + sizeof(HANDLE) == TEB_CLIENT_ID_THREAD,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does

Suggested change
static_assert(offsetof(TEB, ClientId) + sizeof(HANDLE) == TEB_CLIENT_ID_THREAD,
static_assert(offsetof(TEB, ClientId.UniqueThread) == TEB_CLIENT_ID_THREAD,

work?

Comment thread src/main.cpp
if (!tibPtr) {
return false;
}
// Guest code reads these straight out of the TEB.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useless comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants