Skip to content

Hot-patch __ref_* variables should be placed in .rdata, not .data #151008

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

Merged
merged 1 commit into from
Jul 28, 2025

Conversation

sivadeilra
Copy link
Contributor

This is a refinment of #145565 . That PR added support for "Windows Secure Hot-patching". In this design, functions that are compiled for hot-patching need to be modified when they access mutable global variables. The modification is to insert a level of indirection, the so-called __ref_* variables.

Ref variables are supposed to be inserted into the .rdata section, not .data. This provides a degree of protection against modification (accidental or malicious) of ref variables during program execution. When the Windows hot-patch subsystem loads a module as a hot-patch, it finds all ref variables and changes the page protections for the pages containing them to read/write. Then it sets the ref variables to point to the real variable locations within the base image. Then it changes page protections back to read-only.

This relies on the variables being placed in the .rdata section, not .data.

However, it is still important that the LLVM GlobalVariable that is created for the ref variable be created with isConstant = false. This prevents LLVM from optimizing accesses to the GlobalVariable, i.e. assuming that the variable can never change and thus inlining its value into expressions that would ordinarily dereference it. That optimization would defeat the purpose of hot-patching, so isConstant = false is still the correct value for these ref variables.

@dpaoliello dpaoliello merged commit 8ecbfa6 into llvm:main Jul 28, 2025
11 checks passed
@sivadeilra sivadeilra deleted the user/ardavis/hotpatch-rdata branch August 7, 2025 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants