-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
sivadeilra
wants to merge
5
commits into
llvm:main
Choose a base branch
from
sivadeilra:upstream-hotpatch-pr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
69be013
Windows hotpatching support
sivadeilra 44c4643
Address PR feedback
sivadeilra 0b35988
Resolve one integration test failure
sivadeilra 1f61f39
Raise error if trying to use Windows hotpatching on non-Windows OS
sivadeilra 1e1cd1b
Rewrite globals for __ref_* access by loading pointers at function start
sivadeilra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.