Skip to content

Commit 7382ec9

Browse files
committed
Windows hotpatching support
move hotpatch tests to X86 subdir move llvm hotpatch tests to X86 dir switch to strbool attributes
1 parent 118bfcd commit 7382ec9

34 files changed

+1199
-0
lines changed

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,13 @@ class CodeGenOptions : public CodeGenOptionsBase {
495495

496496
/// A list of functions that are replacable by the loader.
497497
std::vector<std::string> LoaderReplaceableFunctionNames;
498+
/// The name of a file that contains functions which will be compiled for
499+
/// hotpatching. See -fms-secure-hotpatch-functions-file.
500+
std::string MSSecureHotPatchFunctionsFile;
501+
502+
/// A list of functions which will be compiled for hotpatching.
503+
/// See -fms-secure-hotpatch-functions-list.
504+
std::vector<std::string> MSSecureHotPatchFunctionsList;
498505

499506
public:
500507
// Define accessors/mutators for code generation options of enumeration type.

clang/include/clang/Driver/Options.td

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3837,6 +3837,24 @@ def fms_hotpatch : Flag<["-"], "fms-hotpatch">, Group<f_Group>,
38373837
Visibility<[ClangOption, CC1Option, CLOption]>,
38383838
HelpText<"Ensure that all functions can be hotpatched at runtime">,
38393839
MarshallingInfoFlag<CodeGenOpts<"HotPatch">>;
3840+
3841+
// See llvm/lib/CodeGen/WindowsSecureHotPatching.cpp
3842+
def fms_secure_hotpatch_functions_file
3843+
: Joined<["-"], "fms-secure-hotpatch-functions-file=">,
3844+
Group<f_Group>,
3845+
Visibility<[ClangOption, CC1Option, CLOption]>,
3846+
MarshallingInfoString<CodeGenOpts<"MSSecureHotPatchFunctionsFile">>,
3847+
HelpText<"Path to a file that contains a list of mangled names of "
3848+
"functions that should be hot-patched for Windows Secure "
3849+
"Hot-Patching">;
3850+
def fms_secure_hotpatch_functions_list
3851+
: CommaJoined<["-"], "fms-secure-hotpatch-functions-list=">,
3852+
Group<f_Group>,
3853+
Visibility<[ClangOption, CC1Option, CLOption]>,
3854+
MarshallingInfoStringVector<CodeGenOpts<"MSSecureHotPatchFunctionsList">>,
3855+
HelpText<"List of mangled symbol names of functions that should be "
3856+
"hot-patched for Windows Secure Hot-Patching">;
3857+
38403858
def fpcc_struct_return : Flag<["-"], "fpcc-struct-return">, Group<f_Group>,
38413859
Visibility<[ClangOption, CC1Option]>,
38423860
HelpText<"Override the default ABI to return all structs on the stack">;

clang/lib/CodeGen/CGCall.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,6 +2660,13 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
26602660
// CPU/feature overrides. addDefaultFunctionDefinitionAttributes
26612661
// handles these separately to set them based on the global defaults.
26622662
GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);
2663+
2664+
// Windows hotpatching support
2665+
if (!MSHotPatchFunctions.empty()) {
2666+
bool IsHotPatched = llvm::binary_search(MSHotPatchFunctions, Name);
2667+
if (IsHotPatched)
2668+
FuncAttrs.addAttribute("marked_for_windows_hot_patching");
2669+
}
26632670
}
26642671

26652672
// Mark functions that are replaceable by the loader.

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,35 @@ CodeGenModule::CodeGenModule(ASTContext &C,
458458
if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
459459
getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
460460
CodeGenOpts.NumRegisterParameters);
461+
462+
// If there are any functions that are marked for Windows secure hot-patching,
463+
// then build the list of functions now.
464+
if (!CGO.MSSecureHotPatchFunctionsFile.empty() ||
465+
!CGO.MSSecureHotPatchFunctionsList.empty()) {
466+
if (!CGO.MSSecureHotPatchFunctionsFile.empty()) {
467+
auto BufOrErr =
468+
llvm::MemoryBuffer::getFile(CGO.MSSecureHotPatchFunctionsFile);
469+
if (BufOrErr) {
470+
const llvm::MemoryBuffer &FileBuffer = **BufOrErr;
471+
for (llvm::line_iterator I(FileBuffer.getMemBufferRef(), true), E;
472+
I != E; ++I)
473+
this->MSHotPatchFunctions.push_back(std::string{*I});
474+
} else {
475+
auto &DE = Context.getDiagnostics();
476+
unsigned DiagID =
477+
DE.getCustomDiagID(DiagnosticsEngine::Error,
478+
"failed to open hotpatch functions file "
479+
"(-fms-hotpatch-functions-file): %0 : %1");
480+
DE.Report(DiagID) << CGO.MSSecureHotPatchFunctionsFile
481+
<< BufOrErr.getError().message();
482+
}
483+
}
484+
485+
for (const auto &FuncName : CGO.MSSecureHotPatchFunctionsList)
486+
this->MSHotPatchFunctions.push_back(FuncName);
487+
488+
llvm::sort(this->MSHotPatchFunctions);
489+
}
461490
}
462491

463492
CodeGenModule::~CodeGenModule() {}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,11 @@ class CodeGenModule : public CodeGenTypeCache {
678678

679679
AtomicOptions AtomicOpts;
680680

681+
// A set of functions which should be hot-patched; see
682+
// -fms-hotpatch-functions-file (and -list). This will nearly always be empty.
683+
// The list is sorted for binary-searching.
684+
std::vector<std::string> MSHotPatchFunctions;
685+
681686
public:
682687
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
683688
const HeaderSearchOptions &headersearchopts,

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6801,6 +6801,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
68016801

68026802
Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch);
68036803

6804+
if (Arg *A = Args.getLastArg(options::OPT_fms_secure_hotpatch_functions_file))
6805+
Args.AddLastArg(CmdArgs, options::OPT_fms_secure_hotpatch_functions_file);
6806+
6807+
for (const auto &A :
6808+
Args.getAllArgValues(options::OPT_fms_secure_hotpatch_functions_list))
6809+
CmdArgs.push_back(
6810+
Args.MakeArgString("-fms-secure-hotpatch-functions-list=" + Twine(A)));
6811+
68046812
if (TC.SupportsProfiling()) {
68056813
Args.AddLastArg(CmdArgs, options::OPT_pg);
68066814

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This verifies that we correctly handle a -fms-secure-hotpatch-functions-file argument that points
2+
// to a missing file.
3+
//
4+
// RUN: not %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-secure-hotpatch-functions-file=%S/this-file-is-intentionally-missing-do-not-create-it.txt /Fo%t.obj %s 2>&1 | FileCheck %s
5+
// CHECK: failed to open hotpatch functions file
6+
7+
void this_might_have_side_effects();
8+
9+
int __declspec(noinline) this_gets_hotpatched() {
10+
this_might_have_side_effects();
11+
return 42;
12+
}
13+
14+
int __declspec(noinline) this_does_not_get_hotpatched() {
15+
return this_gets_hotpatched() + 100;
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This verifies that hotpatch function attributes are correctly propagated when compiling directly to OBJ,
2+
// and that name mangling works as expected.
3+
//
4+
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-secure-hotpatch-functions-list=?this_gets_hotpatched@@YAHXZ /Fo%t.obj %s
5+
// RUN: llvm-readobj --codeview %t.obj | FileCheck %s
6+
7+
void this_might_have_side_effects();
8+
9+
int __declspec(noinline) this_gets_hotpatched() {
10+
this_might_have_side_effects();
11+
return 42;
12+
}
13+
14+
// CHECK: Kind: S_HOTPATCHFUNC (0x1169)
15+
// CHECK-NEXT: Function: this_gets_hotpatched
16+
// CHECK-NEXT: Name: ?this_gets_hotpatched@@YAHXZ
17+
18+
extern "C" int __declspec(noinline) this_does_not_get_hotpatched() {
19+
return this_gets_hotpatched() + 100;
20+
}
21+
22+
// CHECK-NOT: S_HOTPATCHFUNC
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Global constant data such as exception handler tables should not be redirected by Windows Secure Hot-Patching
2+
//
3+
// RUN: %clang_cl -c --target=x86_64-windows-msvc /EHsc -O2 -fms-secure-hotpatch-functions-list=this_gets_hotpatched /Fo%t.obj /clang:-S /clang:-o- %s 2>& 1 | FileCheck %s
4+
5+
class Foo {
6+
public:
7+
int x;
8+
};
9+
10+
void this_might_throw();
11+
12+
extern "C" int this_gets_hotpatched(int k) {
13+
int ret;
14+
try {
15+
this_might_throw();
16+
ret = 1;
17+
} catch (Foo& f) {
18+
ret = 2;
19+
}
20+
return ret;
21+
}
22+
23+
// We expect that RTTI data is not redirected.
24+
// CHECK-NOT: "__ref_??_R0?AVFoo@@@8"
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// This verifies that global variable redirection works correctly when using hotpatching.
2+
//
3+
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 \
4+
// RUN: -fms-secure-hotpatch-functions-list=hp1,hp2,hp3,hp4,hp5_phi_ptr_mixed,hp_phi_ptr_both,hp_const_ptr_sub \
5+
// RUN: /clang:-S /clang:-o- %s | FileCheck %s
6+
7+
#ifdef __clang__
8+
#define NO_TAIL __attribute__((disable_tail_calls))
9+
#else
10+
#define NO_TAIL
11+
#endif
12+
13+
extern int g_data[10];
14+
15+
struct SomeData {
16+
int x;
17+
int y;
18+
};
19+
20+
const struct SomeData g_this_is_const = { 100, 200 };
21+
22+
struct HasPointers {
23+
int* ptr;
24+
int x;
25+
};
26+
27+
extern struct HasPointers g_has_pointers;
28+
29+
void take_data(const void* p);
30+
31+
void do_side_effects();
32+
void do_other_side_effects();
33+
34+
void hp1() NO_TAIL {
35+
take_data(&g_data[5]);
36+
}
37+
38+
// CHECK: hp1:
39+
// CHECK: mov rcx, qword ptr [rip + __ref_g_data]
40+
// CHECK: add rcx, 20
41+
// CHECK: call take_data
42+
// CHECK: .seh_endproc
43+
44+
void hp2() NO_TAIL {
45+
// We do not expect string literals to be redirected.
46+
take_data("hello, world!");
47+
}
48+
49+
// CHECK: hp2:
50+
// CHECK: lea rcx, [rip + "??_C@_0O@KJBLMJCB@hello?0?5world?$CB?$AA@"]
51+
// CHECK: call take_data
52+
// CHECK: .seh_endproc
53+
54+
void hp3() NO_TAIL {
55+
// We do not expect g_this_is_const to be redirected because it is const
56+
// and contains no pointers.
57+
take_data(&g_this_is_const);
58+
}
59+
60+
// CHECK: hp3:
61+
// CHECK: lea rcx, [rip + g_this_is_const]
62+
// CHECK: call take_data
63+
// CHECK-NOT: __ref_g_this_is_const
64+
// CHECK: .seh_endproc
65+
66+
void hp4() NO_TAIL {
67+
take_data(&g_has_pointers);
68+
// We expect &g_has_pointers to be redirected.
69+
}
70+
71+
// CHECK: hp4:
72+
// CHECK: mov rcx, qword ptr [rip + __ref_g_has_pointers]
73+
// CHECK: call take_data
74+
// CHECK: .seh_endproc
75+
76+
// This case checks that global variable redirection interacts correctly with PHI nodes.
77+
// The IR for this generates a "phi ptr g_has_pointers, g_this_is_const" node.
78+
// We expect g_has_pointers to be redirected, but not g_this_is_const.
79+
void hp5_phi_ptr_mixed(int x) NO_TAIL {
80+
const void* y;
81+
if (x) {
82+
y = &g_has_pointers;
83+
do_side_effects();
84+
} else {
85+
y = &g_this_is_const;
86+
do_other_side_effects();
87+
}
88+
take_data(y);
89+
}
90+
91+
// CHECK: hp5_phi_ptr_mixed
92+
// CHECK: .seh_endprologue
93+
// CHECK: test ecx, ecx
94+
// CHECK: mov rsi, qword ptr [rip + __ref_g_has_pointers]
95+
// CHECK: call do_side_effects
96+
// CHECK: jmp
97+
// CHECK: call do_other_side_effects
98+
// CHECK: lea rsi, [rip + g_this_is_const]
99+
// CHECK: mov rcx, rsi
100+
// CHECK: call take_data
101+
// CHECK: .seh_endproc
102+
103+
// This case tests that global variable redirection interacts correctly with PHI nodes,
104+
// where two (all) operands of a given PHI node are globabl variables that redirect.
105+
void hp_phi_ptr_both(int x) NO_TAIL {
106+
const void* y;
107+
if (x) {
108+
y = &g_has_pointers;
109+
do_side_effects();
110+
} else {
111+
y = &g_data[5];
112+
do_other_side_effects();
113+
}
114+
take_data(y);
115+
}
116+
117+
// CHECK: hp_phi_ptr_both:
118+
// CHECK: .seh_endprologue
119+
// CHECK: test ecx, ecx
120+
// CHECK: mov rsi, qword ptr [rip + __ref_g_has_pointers]
121+
// CHECK: mov rsi, qword ptr [rip + __ref_g_data]
122+
// CHECK: take_data
123+
// CHECK: .seh_endproc
124+
125+
// Test a constant expression which references global variable addresses.
126+
size_t hp_const_ptr_sub() NO_TAIL {
127+
return (unsigned char*)&g_has_pointers - (unsigned char*)&g_data;
128+
}
129+
130+
// CHECK: hp_const_ptr_sub:
131+
// CHECK: mov rax, qword ptr [rip + __ref_g_has_pointers]
132+
// CHECK: sub rax, qword ptr [rip + __ref_g_data]
133+
// CHECK: ret
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This verifies that hotpatch function attributes are correctly propagated through LLVM IR when compiling with LTO.
2+
//
3+
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-secure-hotpatch-functions-list=this_gets_hotpatched -flto /Fo%t.bc %s
4+
// RUN: llvm-dis %t.bc -o - | FileCheck %s
5+
//
6+
// CHECK-LABEL: define dso_local noundef i32 @this_gets_hotpatched()
7+
// CHECK-SAME: #0
8+
//
9+
// CHECK-LABEL: define dso_local noundef i32 @this_does_not_get_hotpatched()
10+
// CHECK-SAME: #1
11+
12+
// CHECK: attributes #0
13+
// CHECK-SAME: "marked_for_windows_hot_patching"
14+
15+
// CHECK: attributes #1
16+
// CHECK-NOT: "marked_for_windows_hot_patching"
17+
18+
int __declspec(noinline) this_gets_hotpatched() {
19+
return 42;
20+
}
21+
22+
int __declspec(noinline) this_does_not_get_hotpatched() {
23+
return this_gets_hotpatched() + 100;
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This verifies that hotpatch function attributes are correctly propagated when compiling directly to OBJ.
2+
//
3+
// RUN: echo this_gets_hotpatched > %t.patch-functions.txt
4+
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-secure-hotpatch-functions-file=%t.patch-functions.txt /Fo%t.obj %s
5+
// RUN: llvm-readobj --codeview %t.obj | FileCheck %s
6+
7+
void this_might_have_side_effects();
8+
9+
int __declspec(noinline) this_gets_hotpatched() {
10+
this_might_have_side_effects();
11+
return 42;
12+
}
13+
14+
// CHECK: Kind: S_HOTPATCHFUNC (0x1169)
15+
// CHECK-NEXT: Function: this_gets_hotpatched
16+
17+
int __declspec(noinline) this_does_not_get_hotpatched() {
18+
return this_gets_hotpatched() + 100;
19+
}
20+
21+
// CHECK-NOT: S_HOTPATCHFUNC

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ LLVM_ABI FunctionPass *createSelectOptimizePass();
618618

619619
LLVM_ABI FunctionPass *createCallBrPass();
620620

621+
/// Creates Windows Secure Hot Patch pass. \see WindowsSecureHotPatching.cpp
622+
ModulePass *createWindowsSecureHotPatchingPass();
623+
621624
/// Lowers KCFI operand bundles for indirect calls.
622625
LLVM_ABI FunctionPass *createKCFIPass();
623626
} // namespace llvm

llvm/include/llvm/DebugInfo/CodeView/CodeViewSymbols.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ SYMBOL_RECORD_ALIAS(S_GTHREAD32 , 0x1113, GlobalTLS, ThreadLocalDataSym)
256256
SYMBOL_RECORD(S_UNAMESPACE , 0x1124, UsingNamespaceSym)
257257
SYMBOL_RECORD(S_ANNOTATION , 0x1019, AnnotationSym)
258258

259+
SYMBOL_RECORD(S_HOTPATCHFUNC , 0x1169, HotPatchFuncSym)
260+
259261
#undef CV_SYMBOL
260262
#undef SYMBOL_RECORD
261263
#undef SYMBOL_RECORD_ALIAS

0 commit comments

Comments
 (0)