Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't break stack walking #308

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

jdu2600
Copy link

@jdu2600 jdu2600 commented Nov 6, 2023

Detouring a function should not break our ability to walk the call stack.

On X64, suggest that we require the Detour to reside in MEM_IMAGE so that Windows has access to the compiler generated function information necessary for unwinding. This will prevent surprises.

src/detours.cpp Outdated
// to register this information. This supports kernel stack walks.
// See https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64
DWORD64 imageBase;
if (NULL == RtlLookupFunctionEntry((DWORD64)pDetour, &imageBase, NULL)) {

Choose a reason for hiding this comment

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

In some cases, A Detour function could legitimately be a leaf function and thus have no function table entry, so having this requirement is somewhat over-strict.
Regardless, this shouldn't be just DETOURS_X64 specific - function table unwinding is also used on ARM64, for example.

Copy link
Author

Choose a reason for hiding this comment

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

re: leaf functions
Table entries are also required for any functions that allocate stack space.

But point taken that the detour could be a very simple function.
The first version of this PR simply checked for MEM_IMAGE - and trusted the compiler to have done its job. On consideration though I changed this to the function table lookup - since I didn't want to entirely preclude the use of a JIT detour. A combination of these two approaches would seem reasonable.

re: ARM64
Agreed, but that's not something I felt comfortable writing.

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