microsoft / Detours

Detours is a software package for monitoring and instrumenting API calls on Windows. It is distributed in source code form.
MIT License
5.18k stars 1k forks source link

Work-around an issue in Arm64 regarding the isolated use of CONTEXT_CONTROL. #313

Open pmsjt opened 6 months ago

pmsjt commented 6 months ago

Work-around an issue in Arm64 (and Arm64EC) in which LR and FP registers may become zeroed when CONTEXT_CONTROL is used without CONTEXT_INTEGER.

The addition of the CONTEXT_INTEGER flag does not translate to any real overhead: The kernel performs relatively expensive stack unwind operations for Get and Set ThreadContext touching and scanning over large unwind info datastructures. In scenario the extra few integers register copies (which share cache lines with CONTEXT_CONTROL) can't be observed in the scale and the noise.

This change is adding CONTEXT_INTEGER to the Get and SetThread context calls for both Arm64 and x86_64 so that Arm64EC is also addressed.

This issue is being addressed in the OS as well, but it will take time to disseminate the fix and backport it to all released versions. When the fix is out, this change can arguably be considered redundant, but given there is no real downside, I don't see an objective reason to not add it or making diligent plans to remove it.