Skip to content

Commit 6e727eb

Browse files
committed
Rename to Secure, in LLVM
1 parent 18bcf7a commit 6e727eb

11 files changed

+80
-46
lines changed

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ namespace llvm {
619619
FunctionPass *createKCFIPass();
620620

621621
/// Creates Windows Hot Patch pass. \see WindowsHotPatch.cpp
622-
ModulePass *createWindowsHotPatch();
622+
ModulePass *createWindowsSecureHotPatching();
623623
} // End llvm namespace
624624

625625
#endif

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ void initializeVirtRegMapWrapperLegacyPass(PassRegistry &);
323323
void initializeVirtRegRewriterLegacyPass(PassRegistry &);
324324
void initializeWasmEHPreparePass(PassRegistry &);
325325
void initializeWinEHPreparePass(PassRegistry &);
326-
void initializeWindowsHotPatchPass(PassRegistry &);
326+
void initializeWindowsSecureHotPatchingPass(PassRegistry &);
327327
void initializeWriteBitcodePassPass(PassRegistry &);
328328
void initializeXRayInstrumentationLegacyPass(PassRegistry &);
329329

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ add_llvm_component_library(LLVMCodeGen
250250
VirtRegMap.cpp
251251
WasmEHPrepare.cpp
252252
WindowScheduler.cpp
253-
WindowsHotPatch.cpp
253+
WindowsSecureHotPatching.cpp
254254
WinEHPrepare.cpp
255255
XRayInstrumentation.cpp
256256
${GeneratedMLSources}

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ void TargetPassConfig::addIRPasses() {
895895
addPass(createGlobalMergeFuncPass());
896896

897897
if (TM->getTargetTriple().isOSBinFormatCOFF())
898-
addPass(createWindowsHotPatch());
898+
addPass(createWindowsSecureHotPatching());
899899
}
900900

901901
/// Turn exception handling constructs into something the code generators can

llvm/lib/CodeGen/WindowsHotPatch.cpp renamed to llvm/lib/CodeGen/WindowsSecureHotPatching.cpp

Lines changed: 72 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,50 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// Marks functions with the `marked_for_windows_hot_patching` attribute.
9+
// Provides support for the Windows "Secure Hot-Patching" feature.
10+
//
11+
// Windows contains technology, called "Secure Hot-Patching" (SHP), for securely applying
12+
// hot-patches to a running system. Hot-patches may be applied to the kernel, kernel-mode
13+
// components, device drivers, user-mode system services, etc.
14+
//
15+
// SHP relies on integration between many tools, including compiler, linker, hot-patch
16+
// generation tools, and the Windows kernel. This file implements that part of the workflow
17+
// needed in compilers / code generators.
18+
//
19+
// SHP is not intended for productivity scenarios, such as Edit-and-Continue or interactive
20+
// development. SHP is intended to minimize downtime during installation of Windows OS patches.
21+
//
22+
// In order to work with SHP, LLVM must do all of the following:
23+
//
24+
// * On some architectures (X86, AMD64), the function prolog must begin with hot-patchable
25+
// instructions. This is handled by the MSVC `/hotpatch` option and the equivalent `-fms-hotpatch`
26+
// function. This is necessary because we generally cannot anticipate which functions will need
27+
// to be patched in the future. This option ensures that a function can be hot-patched in the
28+
// future, but does not actually generate any hot-patch for it.
29+
//
30+
// * For a selected set of functions that are being hot-patched (which are identified using
31+
// command-line options), LLVM must generate the `S_HOTPATCHFUNC` CodeView record (symbol).
32+
// This record indicates that a function was compiled with hot-patching enabled.
33+
//
34+
// This implementation uses the `MarkedForWindowsHotPatching` attribute to annotate those
35+
// functions that were marked for hot-patching by command-line parameters. The attribute
36+
// may be specified by a language front-end by setting an attribute when a function is created
37+
// in LLVM IR, or it may be set by passing LLVM arguments.
38+
//
39+
// * For those functions that are hot-patched, LLVM must rewrite references to global variables
40+
// so that they are indirected through a `__ref_*` pointer variable. For each global variable,
41+
// that is accessed by a hot-patched function, e.g. `FOO`, a `__ref_FOO` global pointer variable
42+
// is created and all references to the original `FOO` are rewritten as dereferences of the
43+
// `__ref_FOO` pointer.
44+
//
45+
// Some globals do not need `__ref_*` indirection. The pointer indirection behavior can be
46+
// disabled for these globals by marking them with the `AllowDirectAccessInHotPatchFunction`.
47+
//
48+
// References
49+
//
50+
// * "Hotpatching on Windows": https://techcommunity.microsoft.com/blog/windowsosplatform/hotpatching-on-windows/2959541
51+
// * "Hotpatch for Windows client now available": https://techcommunity.microsoft.com/blog/windows-itpro-blog/hotpatch-for-windows-client-now-available/4399808
52+
// * "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
1053
//
1154
//===----------------------------------------------------------------------===//
1255

@@ -30,20 +73,19 @@ using namespace llvm;
3073
#define DEBUG_TYPE "windows-hot-patch"
3174

3275
// A file containing list of mangled function names to mark for hot patching.
33-
static cl::opt<std::string> LLVMMSHotPatchFunctionsFile(
34-
"ms-hotpatch-functions-file", cl::value_desc("filename"),
35-
cl::desc("A file containing list of mangled function names to mark for hot "
36-
"patching"));
76+
static cl::opt<std::string> LLVMMSSecureHotPatchFunctionsFile(
77+
"ms-secure-hotpatch-functions-file", cl::value_desc("filename"),
78+
cl::desc("A file containing list of mangled function names to mark for Windows Secure Hot-Patching"));
3779

3880
// A list of mangled function names to mark for hot patching.
39-
static cl::list<std::string> LLVMMSHotPatchFunctionsList(
40-
"ms-hotpatch-functions-list", cl::value_desc("list"),
41-
cl::desc("A list of mangled function names to mark for hot patching"),
81+
static cl::list<std::string> LLVMMSSecureHotPatchFunctionsList(
82+
"ms-secure-hotpatch-functions-list", cl::value_desc("list"),
83+
cl::desc("A list of mangled function names to mark for Windows Secure Hot-Patching"),
4284
cl::CommaSeparated);
4385

4486
namespace {
4587

46-
class WindowsHotPatch : public ModulePass {
88+
class WindowsSecureHotPatching : public ModulePass {
4789
struct GlobalVariableUse {
4890
GlobalVariable *GV;
4991
Instruction *User;
@@ -53,8 +95,8 @@ class WindowsHotPatch : public ModulePass {
5395
public:
5496
static char ID;
5597

56-
WindowsHotPatch() : ModulePass(ID) {
57-
initializeWindowsHotPatchPass(*PassRegistry::getPassRegistry());
98+
WindowsSecureHotPatching() : ModulePass(ID) {
99+
initializeWindowsSecureHotPatchingPass(*PassRegistry::getPassRegistry());
58100
}
59101

60102
void getAnalysisUsage(AnalysisUsage &AU) const override {
@@ -75,74 +117,67 @@ class WindowsHotPatch : public ModulePass {
75117

76118
} // end anonymous namespace
77119

78-
char WindowsHotPatch::ID = 0;
120+
char WindowsSecureHotPatching::ID = 0;
79121

80-
INITIALIZE_PASS(WindowsHotPatch, "windows-hot-patch",
122+
INITIALIZE_PASS(WindowsSecureHotPatching, "windows-secure-hot-patch",
81123
"Mark functions for Windows hot patch support", false, false)
82-
ModulePass *llvm::createWindowsHotPatch() { return new WindowsHotPatch(); }
124+
ModulePass *llvm::createWindowsSecureHotPatching() { return new WindowsSecureHotPatching(); }
83125

84126
// Find functions marked with Attribute::MarkedForWindowsHotPatching and modify
85127
// their code (if necessary) to account for accesses to global variables.
86-
bool WindowsHotPatch::runOnModule(Module &M) {
128+
bool WindowsSecureHotPatching::runOnModule(Module &M) {
87129
// The front end may have already marked functions for hot-patching. However,
88130
// we also allow marking functions by passing -ms-hotpatch-functions-file or
89131
// -ms-hotpatch-functions-list directly to LLVM. This allows hot-patching to
90132
// work with languages that have not yet updated their front-ends.
91-
if (!LLVMMSHotPatchFunctionsFile.empty() ||
92-
!LLVMMSHotPatchFunctionsList.empty()) {
133+
if (!LLVMMSSecureHotPatchFunctionsFile.empty() ||
134+
!LLVMMSSecureHotPatchFunctionsList.empty()) {
93135
std::vector<std::string> HotPatchFunctionsList;
94136

95-
if (!LLVMMSHotPatchFunctionsFile.empty()) {
96-
auto BufOrErr = llvm::MemoryBuffer::getFile(LLVMMSHotPatchFunctionsFile);
137+
if (!LLVMMSSecureHotPatchFunctionsFile.empty()) {
138+
auto BufOrErr = llvm::MemoryBuffer::getFile(LLVMMSSecureHotPatchFunctionsFile);
97139
if (BufOrErr) {
98140
const llvm::MemoryBuffer &FileBuffer = **BufOrErr;
99141
for (llvm::line_iterator I(FileBuffer.getMemBufferRef(), true), E;
100-
I != E; ++I) {
142+
I != E; ++I)
101143
HotPatchFunctionsList.push_back(std::string{*I});
102-
}
103144
} else {
104145
M.getContext().diagnose(llvm::DiagnosticInfoGeneric{
105146
llvm::Twine("failed to open hotpatch functions file "
106147
"(--ms-hotpatch-functions-file): ") +
107-
LLVMMSHotPatchFunctionsFile + llvm::Twine(" : ") +
148+
LLVMMSSecureHotPatchFunctionsFile + llvm::Twine(" : ") +
108149
BufOrErr.getError().message()});
109150
}
110151
}
111152

112-
if (!LLVMMSHotPatchFunctionsList.empty()) {
113-
for (const auto &FuncName : LLVMMSHotPatchFunctionsList) {
153+
if (!LLVMMSSecureHotPatchFunctionsList.empty())
154+
for (const auto &FuncName : LLVMMSSecureHotPatchFunctionsList)
114155
HotPatchFunctionsList.push_back(FuncName);
115-
}
116-
}
117156

118157
// Build a set for quick lookups. This points into HotPatchFunctionsList, so
119158
// HotPatchFunctionsList must live longer than HotPatchFunctionsSet.
120159
llvm::SmallSet<llvm::StringRef, 16> HotPatchFunctionsSet;
121-
for (const auto &FuncName : HotPatchFunctionsList) {
160+
for (const auto &FuncName : HotPatchFunctionsList)
122161
HotPatchFunctionsSet.insert(llvm::StringRef{FuncName});
123-
}
124162

125163
// Iterate through all of the functions and check whether they need to be
126164
// marked for hotpatching using the list provided directly to LLVM.
127165
for (auto &F : M.functions()) {
128166
// Ignore declarations that are not definitions.
129-
if (F.isDeclarationForLinker()) {
167+
if (F.isDeclarationForLinker())
130168
continue;
131-
}
132169

133-
if (HotPatchFunctionsSet.contains(F.getName())) {
170+
if (HotPatchFunctionsSet.contains(F.getName()))
134171
F.addFnAttr(Attribute::MarkedForWindowsHotPatching);
135-
}
136172
}
137173
}
138174

139175
SmallDenseMap<GlobalVariable *, GlobalVariable *> RefMapping;
140176
bool MadeChanges = false;
141177
for (auto &F : M.functions()) {
142178
if (F.hasFnAttribute(Attribute::MarkedForWindowsHotPatching)) {
143-
if (runOnFunction(F, RefMapping)) {
179+
if (runOnFunction(F, RefMapping))
144180
MadeChanges = true;
145-
}
146181
}
147182
}
148183
return MadeChanges;
@@ -168,7 +203,7 @@ bool WindowsHotPatch::runOnModule(Module &M) {
168203
// CodeViewDebug::emitHotPatchInformation().
169204
//
170205
// Returns true if any changes were made to the function.
171-
bool WindowsHotPatch::runOnFunction(
206+
bool WindowsSecureHotPatching::runOnFunction(
172207
Function &F,
173208
SmallDenseMap<GlobalVariable *, GlobalVariable *> &RefMapping) {
174209
SmallVector<GlobalVariableUse, 32> GVUses;
@@ -190,16 +225,15 @@ bool WindowsHotPatch::runOnFunction(
190225
Subprogram != nullptr ? Subprogram->getUnit()
191226
: nullptr};
192227
replaceGlobalVariableUses(F, GVUses, RefMapping, DebugInfo);
193-
if (Subprogram != nullptr) {
228+
if (Subprogram != nullptr)
194229
DebugInfo.finalize();
195-
}
196230
return true;
197231
} else {
198232
return false;
199233
}
200234
}
201235

202-
void WindowsHotPatch::replaceGlobalVariableUses(
236+
void WindowsSecureHotPatching::replaceGlobalVariableUses(
203237
Function &F, SmallVectorImpl<GlobalVariableUse> &GVUses,
204238
SmallDenseMap<GlobalVariable *, GlobalVariable *> &RefMapping,
205239
DIBuilder &DebugInfo) {

llvm/test/CodeGen/Generic/ms-hotpatch-functions-file.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

llvm/test/CodeGen/Generic/ms-hotpatch-bad-file.ll renamed to llvm/test/CodeGen/Generic/ms-secure-hotpatch-bad-file.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -mtriple=x86_64-windows --ms-hotpatch-functions-file=%S/this-file-is-intentionally-missing-do-not-create-it.txt < %s 2>&1 | FileCheck %s
1+
; RUN: not llc -mtriple=x86_64-windows --ms-secure-hotpatch-functions-file=%S/this-file-is-intentionally-missing-do-not-create-it.txt < %s 2>&1 | FileCheck %s
22
; CHECK: failed to open hotpatch functions file
33

44
source_filename = ".\\ms-hotpatch.ll"

llvm/test/CodeGen/Generic/ms-hotpatch-functions-file.ll renamed to llvm/test/CodeGen/Generic/ms-secure-hotpatch-functions-file.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; This tests annotating a function with marked_for_windows_hot_patching by using --ms-hotpatch-functions-file.
22
;
3-
; RUN: llc -mtriple=x86_64-windows --ms-hotpatch-functions-file=%S/ms-hotpatch-functions-file.txt < %s | FileCheck %s
3+
; RUN: echo this_gets_hotpatched > %t.patch-functions.txt
4+
; RUN: llc -mtriple=x86_64-windows --ms-secure-hotpatch-functions-file=%t.patch-functions.txt < %s | FileCheck %s
45

56
source_filename = ".\\ms-hotpatch-functions-file.ll"
67
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

llvm/test/CodeGen/Generic/ms-hotpatch-functions-list.ll renamed to llvm/test/CodeGen/Generic/ms-secure-hotpatch-functions-list.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; This tests annotating a function with marked_for_windows_hot_patching by using --ms-hotpatch-functions-list.
22
;
3-
; RUN: llc -mtriple=x86_64-windows --ms-hotpatch-functions-list=this_gets_hotpatched < %s | FileCheck %s
3+
; RUN: llc -mtriple=x86_64-windows --ms-secure-hotpatch-functions-list=this_gets_hotpatched < %s | FileCheck %s
44

55
source_filename = ".\\ms-hotpatch-functions-list.ll"
66
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)