Skip to content

Add support for Windows hot-patching #138972

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/include/clang/Basic/CodeGenOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ class CodeGenOptions : public CodeGenOptionsBase {
/// The name of a file to use with \c .secure_log_unique directives.
std::string AsSecureLogFile;

/// The name of a file that contains functions which will be compiled for
/// hotpatching. See -fms-hot-patch-functions-file.
std::string MSHotPatchFunctionsFile;

/// A list of functions which will be compiled for hotpatching.
/// See -fms-hot-patch-functions-list.
std::vector<std::string> MSHotPatchFunctionsList;

public:
// Define accessors/mutators for code generation options of enumeration type.
#define CODEGENOPT(Name, Bits, Default)
Expand Down
14 changes: 14 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3793,6 +3793,20 @@ def fms_hotpatch : Flag<["-"], "fms-hotpatch">, Group<f_Group>,
Visibility<[ClangOption, CC1Option, CLOption]>,
HelpText<"Ensure that all functions can be hotpatched at runtime">,
MarshallingInfoFlag<CodeGenOpts<"HotPatch">>;
def fms_hotpatch_functions_file
: Joined<["-"], "fms-hotpatch-functions-file=">,
Group<f_Group>,
Visibility<[ClangOption, CC1Option, CLOption]>,
MarshallingInfoString<CodeGenOpts<"MSHotPatchFunctionsFile">>,
HelpText<"Path to a file that contains a list of mangled symbol names of "
"functions that should be hot-patched">;
def fms_hotpatch_functions_list
: CommaJoined<["-"], "fms-hotpatch-functions-list=">,
Group<f_Group>,
Visibility<[ClangOption, CC1Option, CLOption]>,
MarshallingInfoStringVector<CodeGenOpts<"MSHotPatchFunctionsList">>,
HelpText<"List of mangled symbol names of functions that should be "
"hot-patched">;
def fpcc_struct_return : Flag<["-"], "fpcc-struct-return">, Group<f_Group>,
Visibility<[ClangOption, CC1Option]>,
HelpText<"Override the default ABI to return all structs on the stack">;
Expand Down
9 changes: 9 additions & 0 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,15 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
// CPU/feature overrides. addDefaultFunctionDefinitionAttributes
// handles these separately to set them based on the global defaults.
GetCPUAndFeaturesAttributes(CalleeInfo.getCalleeDecl(), FuncAttrs);

// Windows hotpatching support
if (!MSHotPatchFunctions.empty()) {
bool IsHotPatched = std::binary_search(MSHotPatchFunctions.begin(),
MSHotPatchFunctions.end(), Name);
if (IsHotPatched) {
FuncAttrs.addAttribute(llvm::Attribute::MarkedForWindowsHotPatching);
}
}
}

// Collect attributes from arguments and return values.
Expand Down
49 changes: 49 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,55 @@ CodeGenModule::CodeGenModule(ASTContext &C,
if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
CodeGenOpts.NumRegisterParameters);

// If there are any functions that are marked for Windows hot-patching,
// then build the list of functions now.
if (M.getTargetTriple().isOSBinFormatCOFF()) {
if (!CGO.MSHotPatchFunctionsFile.empty()) {
auto BufOrErr = llvm::MemoryBuffer::getFile(CGO.MSHotPatchFunctionsFile);
if (BufOrErr) {
const llvm::MemoryBuffer &FileBuffer = **BufOrErr;
for (llvm::line_iterator I(FileBuffer.getMemBufferRef(), true), E;
I != E; ++I) {
llvm::StringRef Line = llvm::StringRef(*I).trim();
if (!Line.empty()) {
this->MSHotPatchFunctions.push_back(std::string{Line});
}
}
} else {
auto &DE = Context.getDiagnostics();
unsigned DiagID =
DE.getCustomDiagID(DiagnosticsEngine::Error,
"failed to open hotpatch functions file "
"(-fms-hotpatch-functions-file): %0 : %1");
DE.Report(DiagID) << CGO.MSHotPatchFunctionsFile
<< BufOrErr.getError().message();
}
}

for (const auto &FuncName : CGO.MSHotPatchFunctionsList) {
this->MSHotPatchFunctions.push_back(FuncName);
}

std::sort(this->MSHotPatchFunctions.begin(),
this->MSHotPatchFunctions.end());
} else {
if (!CGO.MSHotPatchFunctionsFile.empty()) {
unsigned DiagID = diags.getCustomDiagID(
DiagnosticsEngine::Error,
"hotpatch functions file (-fms-hotpatch-functions-file) is only "
"supported on Windows targets");
diags.Report(DiagID);
}

if (!CGO.MSHotPatchFunctionsList.empty()) {
unsigned DiagID = diags.getCustomDiagID(
DiagnosticsEngine::Error,
"hotpatch functions list (-fms-hotpatch-functions-list) is only "
"supported on Windows targets");
diags.Report(DiagID);
}
}
}

CodeGenModule::~CodeGenModule() {}
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,11 @@ class CodeGenModule : public CodeGenTypeCache {

AtomicOptions AtomicOpts;

// A set of functions which should be hot-patched; see
// -fms-hotpatch-functions-file (and -list). This will nearly always be empty.
// The list is sorted for binary-searching.
std::vector<std::string> MSHotPatchFunctions;

public:
CodeGenModule(ASTContext &C, IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
const HeaderSearchOptions &headersearchopts,
Expand Down
10 changes: 10 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6945,6 +6945,16 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,

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

if (Arg *A = Args.getLastArg(options::OPT_fms_hotpatch_functions_file)) {
Args.AddLastArg(CmdArgs, options::OPT_fms_hotpatch_functions_file);
}

for (const auto &A :
Args.getAllArgValues(options::OPT_fms_hotpatch_functions_list)) {
CmdArgs.push_back(
Args.MakeArgString("-fms-hotpatch-functions-list=" + Twine(A)));
}

if (TC.SupportsProfiling()) {
Args.AddLastArg(CmdArgs, options::OPT_pg);

Expand Down
16 changes: 16 additions & 0 deletions clang/test/CodeGen/ms-hotpatch-bad-file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This verifies that we correctly handle a -fms-hotpatch-functions-file argument that points
// to a missing file.
//
// RUN: not %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-hotpatch-functions-file=%S/this-file-is-intentionally-missing-do-not-create-it.txt /Fo%t.obj %s 2>&1 | FileCheck %s
// CHECK: failed to open hotpatch functions file

void this_might_have_side_effects();

int __declspec(noinline) this_gets_hotpatched() {
this_might_have_side_effects();
return 42;
}

int __declspec(noinline) this_does_not_get_hotpatched() {
return this_gets_hotpatched() + 100;
}
7 changes: 7 additions & 0 deletions clang/test/CodeGen/ms-hotpatch-bad-os.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This verifies that enabling Windows hotpatching when targeting a non-Windows OS causes an error.
//
// RUN: not %clang -c --target=x86_64-unknown-linux-elf -fms-hotpatch -fms-hotpatch-functions-list=foo -fms-hotpatch-functions-file=this_will_never_be_accessed -o%t.o %s 2>&1 | FileCheck %s
// CHECK: hotpatch functions file (-fms-hotpatch-functions-file) is only supported on Windows targets
// CHECK: hotpatch functions list (-fms-hotpatch-functions-list) is only supported on Windows targets

void foo();
22 changes: 22 additions & 0 deletions clang/test/CodeGen/ms-hotpatch-cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This verifies that hotpatch function attributes are correctly propagated when compiling directly to OBJ,
// and that name mangling works as expected.
//
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-hotpatch-functions-list=?this_gets_hotpatched@@YAHXZ /Fo%t.obj %s
// RUN: llvm-readobj --codeview %t.obj | FileCheck %s

void this_might_have_side_effects();

int __declspec(noinline) this_gets_hotpatched() {
this_might_have_side_effects();
return 42;
}

// CHECK: Kind: S_HOTPATCHFUNC (0x1169)
// CHECK-NEXT: Function: this_gets_hotpatched
// CHECK-NEXT: Name: ?this_gets_hotpatched@@YAHXZ

extern "C" int __declspec(noinline) this_does_not_get_hotpatched() {
return this_gets_hotpatched() + 100;
}

// CHECK-NOT: S_HOTPATCHFUNC
21 changes: 21 additions & 0 deletions clang/test/CodeGen/ms-hotpatch-file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This verifies that hotpatch function attributes are correctly propagated when compiling directly to OBJ.
//
// RUN: echo this_gets_hotpatched > %t-hotpatch-functions.txt
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-hotpatch-functions-file=%t-hotpatch-functions.txt /Fo%t.obj %s
// RUN: llvm-readobj --codeview %t.obj | FileCheck %s

void this_might_have_side_effects();

int __declspec(noinline) this_gets_hotpatched() {
this_might_have_side_effects();
return 42;
}

// CHECK: Kind: S_HOTPATCHFUNC (0x1169)
// CHECK-NEXT: Function: this_gets_hotpatched

int __declspec(noinline) this_does_not_get_hotpatched() {
return this_gets_hotpatched() + 100;
}

// CHECK-NOT: S_HOTPATCHFUNC
19 changes: 19 additions & 0 deletions clang/test/CodeGen/ms-hotpatch-globals.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This verifies different patterns of accesses to global variables within functions that are hotpatched
//
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 -fms-hotpatch-functions-list=this_gets_hotpatched /Fo%t.obj /clang:-S /clang:-o- %s 2>& 1 | FileCheck %s

extern int g_foo;
extern int g_bar;

int* this_gets_hotpatched(int k, void g()) {
g_foo = 10;

int* ret;
if (k) {
g();
ret = &g_foo;
} else {
ret = &g_bar;
}
return ret;
}
19 changes: 19 additions & 0 deletions clang/test/CodeGen/ms-hotpatch-lto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This verifies that hotpatch function attributes are correctly propagated through LLVM IR when compiling with LTO.
//
// RUN: echo this_gets_hotpatched > %t-hotpatch-functions.txt
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-hotpatch-functions-file=%t-hotpatch-functions.txt -flto /Fo%t.bc %s
// RUN: llvm-dis %t.bc -o - | FileCheck %s
//
// CHECK: ; Function Attrs: marked_for_windows_hot_patching mustprogress nofree noinline norecurse nosync nounwind sspstrong willreturn memory(none) uwtable
// CHECK-NEXT: define dso_local noundef i32 @this_gets_hotpatched()
//
// CHECK: ; Function Attrs: mustprogress nofree noinline norecurse nosync nounwind sspstrong willreturn memory(none) uwtable
// CHECK-NEXT: define dso_local noundef i32 @this_does_not_get_hotpatched()

int __declspec(noinline) this_gets_hotpatched() {
return 42;
}

int __declspec(noinline) this_does_not_get_hotpatched() {
return this_gets_hotpatched() + 100;
}
20 changes: 20 additions & 0 deletions clang/test/CodeGen/ms-hotpatch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// This verifies that hotpatch function attributes are correctly propagated when compiling directly to OBJ.
//
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-hotpatch-functions-list=this_gets_hotpatched /Fo%t.obj %s
// RUN: llvm-readobj --codeview %t.obj | FileCheck %s

void this_might_have_side_effects();

int __declspec(noinline) this_gets_hotpatched() {
this_might_have_side_effects();
return 42;
}

// CHECK: Kind: S_HOTPATCHFUNC (0x1169)
// CHECK-NEXT: Function: this_gets_hotpatched

int __declspec(noinline) this_does_not_get_hotpatched() {
return this_gets_hotpatched() + 100;
}

// CHECK-NOT: S_HOTPATCHFUNC
2 changes: 2 additions & 0 deletions llvm/include/llvm/Bitcode/LLVMBitCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,8 @@ enum AttributeKindCodes {
ATTR_KIND_NO_DIVERGENCE_SOURCE = 100,
ATTR_KIND_SANITIZE_TYPE = 101,
ATTR_KIND_CAPTURES = 102,
ATTR_KIND_ALLOW_DIRECT_ACCESS_IN_HOT_PATCH_FUNCTION = 103,
ATTR_KIND_MARKED_FOR_WINDOWS_HOT_PATCHING = 104,
};

enum ComdatSelectionKindCodes {
Expand Down
3 changes: 3 additions & 0 deletions llvm/include/llvm/CodeGen/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@ namespace llvm {

/// Lowers KCFI operand bundles for indirect calls.
FunctionPass *createKCFIPass();

/// Creates Windows Hot Patch pass. \see WindowsHotPatch.cpp
ModulePass *createWindowsHotPatch();
} // End llvm namespace

#endif
2 changes: 2 additions & 0 deletions llvm/include/llvm/DebugInfo/CodeView/CodeViewSymbols.def
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ SYMBOL_RECORD_ALIAS(S_GTHREAD32 , 0x1113, GlobalTLS, ThreadLocalDataSym)
SYMBOL_RECORD(S_UNAMESPACE , 0x1124, UsingNamespaceSym)
SYMBOL_RECORD(S_ANNOTATION , 0x1019, AnnotationSym)

SYMBOL_RECORD(S_HOTPATCHFUNC , 0x1169, HotPatchFuncSym)

#undef CV_SYMBOL
#undef SYMBOL_RECORD
#undef SYMBOL_RECORD_ALIAS
15 changes: 15 additions & 0 deletions llvm/include/llvm/DebugInfo/CodeView/SymbolRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ class CallerSym : public SymbolRecord {
uint32_t RecordOffset = 0;
};

class HotPatchFuncSym : public SymbolRecord {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity (related to my other questions), historically Windows updates only deliver binaries (DLLs, EXEs), not PDBs. PDBs are usually fetched by users when debugging, through the Microsoft symbol server. Then how is the kernel gonna find this record if the PDB isn't there by default?

Copy link
Author

@sivadeilra sivadeilra May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our hot-patch tools read PDBs and then generate metadata that is placed into the final DLL/SYS/EXE, into a COFF section that is reserved for this purpose. The PDBs are not distributed with the hot-patch, and are not needed by the OS that is installing the hot-patch.

public:
explicit HotPatchFuncSym(SymbolRecordKind Kind) : SymbolRecord(Kind) {}
HotPatchFuncSym(uint32_t RecordOffset)
: SymbolRecord(SymbolRecordKind::HotPatchFuncSym),
RecordOffset(RecordOffset) {}

// This is an ItemID in the IPI stream, which points to an LF_FUNC_ID or
// LF_MFUNC_ID record.
TypeIndex Function;
StringRef Name;

uint32_t RecordOffset = 0;
};

struct DecodedAnnotation {
StringRef Name;
ArrayRef<uint8_t> Bytes;
Expand Down
11 changes: 11 additions & 0 deletions llvm/include/llvm/IR/Attributes.td
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,17 @@ def CoroDestroyOnlyWhenComplete : EnumAttr<"coro_only_destroy_when_complete", In
/// pipeline to perform elide on the call or invoke instruction.
def CoroElideSafe : EnumAttr<"coro_elide_safe", IntersectPreserve, [FnAttr]>;

/// Function is marked for Windows Hot Patching
def MarkedForWindowsHotPatching
: EnumAttr<"marked_for_windows_hot_patching", IntersectPreserve, [FnAttr]>;

/// Global variable should not be accessed through a "__ref_" global variable in
/// a hot patching function This attribute is applied to the global variable
/// decl, not the hotpatched function.
def AllowDirectAccessInHotPatchFunction
: EnumAttr<"allow_direct_access_in_hot_patch_function",
IntersectPreserve, [FnAttr]>;

/// Target-independent string attributes.
def LessPreciseFPMAD : StrBoolAttr<"less-precise-fpmad">;
def NoInfsFPMath : StrBoolAttr<"no-infs-fp-math">;
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/InitializePasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ void initializeVirtRegMapWrapperLegacyPass(PassRegistry &);
void initializeVirtRegRewriterLegacyPass(PassRegistry &);
void initializeWasmEHPreparePass(PassRegistry &);
void initializeWinEHPreparePass(PassRegistry &);
void initializeWindowsHotPatchPass(PassRegistry &);
void initializeWriteBitcodePassPass(PassRegistry &);
void initializeXRayInstrumentationLegacyPass(PassRegistry &);

Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2248,6 +2248,10 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
return Attribute::NoExt;
case bitc::ATTR_KIND_CAPTURES:
return Attribute::Captures;
case bitc::ATTR_KIND_ALLOW_DIRECT_ACCESS_IN_HOT_PATCH_FUNCTION:
return Attribute::AllowDirectAccessInHotPatchFunction;
case bitc::ATTR_KIND_MARKED_FOR_WINDOWS_HOT_PATCHING:
return Attribute::MarkedForWindowsHotPatching;
}
}

Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,10 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
return bitc::ATTR_KIND_NO_EXT;
case Attribute::Captures:
return bitc::ATTR_KIND_CAPTURES;
case Attribute::AllowDirectAccessInHotPatchFunction:
return bitc::ATTR_KIND_ALLOW_DIRECT_ACCESS_IN_HOT_PATCH_FUNCTION;
case Attribute::MarkedForWindowsHotPatching:
return bitc::ATTR_KIND_MARKED_FOR_WINDOWS_HOT_PATCHING;
case Attribute::EndAttrKinds:
llvm_unreachable("Can not encode end-attribute kinds marker.");
case Attribute::None:
Expand Down
Loading