[clang] [llvm] Add support for Windows hot-patching (PR #138972)

via cfe-commits cfe-commits at lists.llvm.org
Wed May 14 15:11:48 PDT 2025


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff HEAD~1 HEAD --extensions c,h,cpp -- clang/test/CodeGen/ms-secure-hotpatch-bad-file.c clang/test/CodeGen/ms-secure-hotpatch-cpp.cpp clang/test/CodeGen/ms-secure-hotpatch-lto.c clang/test/CodeGen/ms-secure-hotpatch.c llvm/lib/CodeGen/WindowsSecureHotPatching.cpp clang/include/clang/Basic/CodeGenOptions.h clang/lib/CodeGen/CGCall.cpp clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/CodeGenModule.h clang/lib/Driver/ToolChains/Clang.cpp llvm/include/llvm/Bitcode/LLVMBitCodes.h llvm/include/llvm/CodeGen/Passes.h llvm/include/llvm/DebugInfo/CodeView/SymbolRecord.h llvm/include/llvm/InitializePasses.h llvm/lib/Bitcode/Reader/BitcodeReader.cpp llvm/lib/Bitcode/Writer/BitcodeWriter.cpp llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.h llvm/lib/CodeGen/TargetPassConfig.cpp llvm/lib/DebugInfo/CodeView/SymbolDumper.cpp llvm/lib/DebugInfo/CodeView/SymbolRecordMapping.cpp llvm/lib/ObjectYAML/CodeViewYAMLSymbols.cpp llvm/lib/Transforms/Utils/CodeExtractor.cpp llvm/tools/llvm-pdbutil/MinimalSymbolDumper.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/CodeGen/WindowsSecureHotPatching.cpp b/llvm/lib/CodeGen/WindowsSecureHotPatching.cpp
index 977f950ee..acde07000 100644
--- a/llvm/lib/CodeGen/WindowsSecureHotPatching.cpp
+++ b/llvm/lib/CodeGen/WindowsSecureHotPatching.cpp
@@ -8,48 +8,61 @@
 //
 // Provides support for the Windows "Secure Hot-Patching" feature.
 //
-// Windows contains technology, called "Secure Hot-Patching" (SHP), for securely applying
-// hot-patches to a running system. Hot-patches may be applied to the kernel, kernel-mode
-// components, device drivers, user-mode system services, etc.
+// Windows contains technology, called "Secure Hot-Patching" (SHP), for securely
+// applying hot-patches to a running system. Hot-patches may be applied to the
+// kernel, kernel-mode components, device drivers, user-mode system services,
+// etc.
 //
-// SHP relies on integration between many tools, including compiler, linker, hot-patch
-// generation tools, and the Windows kernel. This file implements that part of the workflow
-// needed in compilers / code generators.
+// SHP relies on integration between many tools, including compiler, linker,
+// hot-patch generation tools, and the Windows kernel. This file implements that
+// part of the workflow needed in compilers / code generators.
 //
-// SHP is not intended for productivity scenarios, such as Edit-and-Continue or interactive
-// development. SHP is intended to minimize downtime during installation of Windows OS patches.
+// SHP is not intended for productivity scenarios, such as Edit-and-Continue or
+// interactive development. SHP is intended to minimize downtime during
+// installation of Windows OS patches.
 //
 // In order to work with SHP, LLVM must do all of the following:
 //
-// * On some architectures (X86, AMD64), the function prolog must begin with hot-patchable
-//   instructions. This is handled by the MSVC `/hotpatch` option and the equivalent `-fms-hotpatch`
-//   function. This is necessary because we generally cannot anticipate which functions will need
-//   to be patched in the future. This option ensures that a function can be hot-patched in the
-//   future, but does not actually generate any hot-patch for it.
+// * On some architectures (X86, AMD64), the function prolog must begin with
+// hot-patchable
+//   instructions. This is handled by the MSVC `/hotpatch` option and the
+//   equivalent `-fms-hotpatch` function. This is necessary because we generally
+//   cannot anticipate which functions will need to be patched in the future.
+//   This option ensures that a function can be hot-patched in the future, but
+//   does not actually generate any hot-patch for it.
 //
-// * For a selected set of functions that are being hot-patched (which are identified using
-//   command-line options), LLVM must generate the `S_HOTPATCHFUNC` CodeView record (symbol).
-//   This record indicates that a function was compiled with hot-patching enabled.
+// * For a selected set of functions that are being hot-patched (which are
+// identified using
+//   command-line options), LLVM must generate the `S_HOTPATCHFUNC` CodeView
+//   record (symbol). This record indicates that a function was compiled with
+//   hot-patching enabled.
 //
-//   This implementation uses the `MarkedForWindowsHotPatching` attribute to annotate those
-//   functions that were marked for hot-patching by command-line parameters. The attribute
-//   may be specified by a language front-end by setting an attribute when a function is created
-//   in LLVM IR, or it may be set by passing LLVM arguments.
+//   This implementation uses the `MarkedForWindowsHotPatching` attribute to
+//   annotate those functions that were marked for hot-patching by command-line
+//   parameters. The attribute may be specified by a language front-end by
+//   setting an attribute when a function is created in LLVM IR, or it may be
+//   set by passing LLVM arguments.
 //
-// * For those functions that are hot-patched, LLVM must rewrite references to global variables
-//   so that they are indirected through a `__ref_*` pointer variable.  For each global variable,
-//   that is accessed by a hot-patched function, e.g. `FOO`, a `__ref_FOO` global pointer variable
-//   is created and all references to the original `FOO` are rewritten as dereferences of the
+// * For those functions that are hot-patched, LLVM must rewrite references to
+// global variables
+//   so that they are indirected through a `__ref_*` pointer variable.  For each
+//   global variable, that is accessed by a hot-patched function, e.g. `FOO`, a
+//   `__ref_FOO` global pointer variable is created and all references to the
+//   original `FOO` are rewritten as dereferences of the
 //   `__ref_FOO` pointer.
 //
-//   Some globals do not need `__ref_*` indirection. The pointer indirection behavior can be
-//   disabled for these globals by marking them with the `AllowDirectAccessInHotPatchFunction`.
+//   Some globals do not need `__ref_*` indirection. The pointer indirection
+//   behavior can be disabled for these globals by marking them with the
+//   `AllowDirectAccessInHotPatchFunction`.
 //
 // References
 //
-// * "Hotpatching on Windows": https://techcommunity.microsoft.com/blog/windowsosplatform/hotpatching-on-windows/2959541
-// * "Hotpatch for Windows client now available": https://techcommunity.microsoft.com/blog/windows-itpro-blog/hotpatch-for-windows-client-now-available/4399808
-// * "Get hotpatching for Windows Server": https://www.microsoft.com/en-us/windows-server/blog/2025/04/24/tired-of-all-the-restarts-get-hotpatching-for-windows-server/?msockid=19a6f8f09bd160ac0b18ed449afc614b
+// * "Hotpatching on Windows":
+// https://techcommunity.microsoft.com/blog/windowsosplatform/hotpatching-on-windows/2959541
+// * "Hotpatch for Windows client now available":
+// https://techcommunity.microsoft.com/blog/windows-itpro-blog/hotpatch-for-windows-client-now-available/4399808
+// * "Get hotpatching for Windows Server":
+// https://www.microsoft.com/en-us/windows-server/blog/2025/04/24/tired-of-all-the-restarts-get-hotpatching-for-windows-server/?msockid=19a6f8f09bd160ac0b18ed449afc614b
 //
 //===----------------------------------------------------------------------===//
 
@@ -75,12 +88,14 @@ using namespace llvm;
 // A file containing list of mangled function names to mark for hot patching.
 static cl::opt<std::string> LLVMMSSecureHotPatchFunctionsFile(
     "ms-secure-hotpatch-functions-file", cl::value_desc("filename"),
-    cl::desc("A file containing list of mangled function names to mark for Windows Secure Hot-Patching"));
+    cl::desc("A file containing list of mangled function names to mark for "
+             "Windows Secure Hot-Patching"));
 
 // A list of mangled function names to mark for hot patching.
 static cl::list<std::string> LLVMMSSecureHotPatchFunctionsList(
     "ms-secure-hotpatch-functions-list", cl::value_desc("list"),
-    cl::desc("A list of mangled function names to mark for Windows Secure Hot-Patching"),
+    cl::desc("A list of mangled function names to mark for Windows Secure "
+             "Hot-Patching"),
     cl::CommaSeparated);
 
 namespace {
@@ -121,7 +136,9 @@ char WindowsSecureHotPatching::ID = 0;
 
 INITIALIZE_PASS(WindowsSecureHotPatching, "windows-secure-hot-patch",
                 "Mark functions for Windows hot patch support", false, false)
-ModulePass *llvm::createWindowsSecureHotPatching() { return new WindowsSecureHotPatching(); }
+ModulePass *llvm::createWindowsSecureHotPatching() {
+  return new WindowsSecureHotPatching();
+}
 
 // Find functions marked with Attribute::MarkedForWindowsHotPatching and modify
 // their code (if necessary) to account for accesses to global variables.
@@ -135,7 +152,8 @@ bool WindowsSecureHotPatching::runOnModule(Module &M) {
     std::vector<std::string> HotPatchFunctionsList;
 
     if (!LLVMMSSecureHotPatchFunctionsFile.empty()) {
-      auto BufOrErr = llvm::MemoryBuffer::getFile(LLVMMSSecureHotPatchFunctionsFile);
+      auto BufOrErr =
+          llvm::MemoryBuffer::getFile(LLVMMSSecureHotPatchFunctionsFile);
       if (BufOrErr) {
         const llvm::MemoryBuffer &FileBuffer = **BufOrErr;
         for (llvm::line_iterator I(FileBuffer.getMemBufferRef(), true), E;

``````````

</details>


https://github.com/llvm/llvm-project/pull/138972


More information about the cfe-commits mailing list