-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Add support for Windows Secure 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
2
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
2 commits
Select commit
Hold shift + click to select a range
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-secure-hotpatch-functions-file argument that points | ||
// to a missing file. | ||
// | ||
// 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 | ||
// 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,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-secure-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,24 @@ | ||
// Global constant data such as exception handler tables should not be redirected by Windows Secure Hot-Patching | ||
// | ||
// 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 | ||
|
||
class Foo { | ||
public: | ||
int x; | ||
}; | ||
|
||
void this_might_throw(); | ||
|
||
extern "C" int this_gets_hotpatched(int k) { | ||
int ret; | ||
try { | ||
this_might_throw(); | ||
ret = 1; | ||
} catch (Foo& f) { | ||
ret = 2; | ||
} | ||
return ret; | ||
} | ||
|
||
// We expect that RTTI data is not redirected. | ||
// CHECK-NOT: "__ref_??_R0?AVFoo@@@8" |
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,133 @@ | ||
// This verifies that global variable redirection works correctly when using hotpatching. | ||
// | ||
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 \ | ||
// RUN: -fms-secure-hotpatch-functions-list=hp1,hp2,hp3,hp4,hp5_phi_ptr_mixed,hp_phi_ptr_both,hp_const_ptr_sub \ | ||
// RUN: /clang:-S /clang:-o- %s | FileCheck %s | ||
|
||
#ifdef __clang__ | ||
#define NO_TAIL __attribute__((disable_tail_calls)) | ||
#else | ||
#define NO_TAIL | ||
#endif | ||
|
||
extern int g_data[10]; | ||
|
||
struct SomeData { | ||
int x; | ||
int y; | ||
}; | ||
|
||
const struct SomeData g_this_is_const = { 100, 200 }; | ||
|
||
struct HasPointers { | ||
int* ptr; | ||
int x; | ||
}; | ||
|
||
extern struct HasPointers g_has_pointers; | ||
|
||
void take_data(const void* p); | ||
|
||
void do_side_effects(); | ||
void do_other_side_effects(); | ||
|
||
void hp1() NO_TAIL { | ||
take_data(&g_data[5]); | ||
} | ||
|
||
// CHECK: hp1: | ||
// CHECK: mov rcx, qword ptr [rip + __ref_g_data] | ||
// CHECK: add rcx, 20 | ||
// CHECK: call take_data | ||
// CHECK: .seh_endproc | ||
|
||
void hp2() NO_TAIL { | ||
// We do not expect string literals to be redirected. | ||
take_data("hello, world!"); | ||
} | ||
|
||
// CHECK: hp2: | ||
// CHECK: lea rcx, [rip + "??_C@_0O@KJBLMJCB@hello?0?5world?$CB?$AA@"] | ||
// CHECK: call take_data | ||
// CHECK: .seh_endproc | ||
|
||
void hp3() NO_TAIL { | ||
// We do not expect g_this_is_const to be redirected because it is const | ||
// and contains no pointers. | ||
take_data(&g_this_is_const); | ||
} | ||
|
||
// CHECK: hp3: | ||
// CHECK: lea rcx, [rip + g_this_is_const] | ||
// CHECK: call take_data | ||
// CHECK-NOT: __ref_g_this_is_const | ||
// CHECK: .seh_endproc | ||
|
||
void hp4() NO_TAIL { | ||
take_data(&g_has_pointers); | ||
// We expect &g_has_pointers to be redirected. | ||
} | ||
|
||
// CHECK: hp4: | ||
// CHECK: mov rcx, qword ptr [rip + __ref_g_has_pointers] | ||
// CHECK: call take_data | ||
// CHECK: .seh_endproc | ||
|
||
// This case checks that global variable redirection interacts correctly with PHI nodes. | ||
// The IR for this generates a "phi ptr g_has_pointers, g_this_is_const" node. | ||
// We expect g_has_pointers to be redirected, but not g_this_is_const. | ||
void hp5_phi_ptr_mixed(int x) NO_TAIL { | ||
const void* y; | ||
if (x) { | ||
y = &g_has_pointers; | ||
do_side_effects(); | ||
} else { | ||
y = &g_this_is_const; | ||
do_other_side_effects(); | ||
} | ||
take_data(y); | ||
} | ||
|
||
// CHECK: hp5_phi_ptr_mixed | ||
// CHECK: .seh_endprologue | ||
// CHECK: test ecx, ecx | ||
// CHECK: mov rsi, qword ptr [rip + __ref_g_has_pointers] | ||
// CHECK: call do_side_effects | ||
// CHECK: jmp | ||
// CHECK: call do_other_side_effects | ||
// CHECK: lea rsi, [rip + g_this_is_const] | ||
// CHECK: mov rcx, rsi | ||
// CHECK: call take_data | ||
// CHECK: .seh_endproc | ||
|
||
// This case tests that global variable redirection interacts correctly with PHI nodes, | ||
// where two (all) operands of a given PHI node are globabl variables that redirect. | ||
void hp_phi_ptr_both(int x) NO_TAIL { | ||
const void* y; | ||
if (x) { | ||
y = &g_has_pointers; | ||
do_side_effects(); | ||
} else { | ||
y = &g_data[5]; | ||
do_other_side_effects(); | ||
} | ||
take_data(y); | ||
} | ||
|
||
// CHECK: hp_phi_ptr_both: | ||
// CHECK: .seh_endprologue | ||
// CHECK: test ecx, ecx | ||
// CHECK: mov rsi, qword ptr [rip + __ref_g_has_pointers] | ||
// CHECK: mov rsi, qword ptr [rip + __ref_g_data] | ||
// CHECK: take_data | ||
// CHECK: .seh_endproc | ||
|
||
// Test a constant expression which references global variable addresses. | ||
size_t hp_const_ptr_sub() NO_TAIL { | ||
return (unsigned char*)&g_has_pointers - (unsigned char*)&g_data; | ||
} | ||
|
||
// CHECK: hp_const_ptr_sub: | ||
// CHECK: mov rax, qword ptr [rip + __ref_g_has_pointers] | ||
// CHECK: sub rax, qword ptr [rip + __ref_g_data] | ||
// CHECK: 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,24 @@ | ||
// This verifies that hotpatch function attributes are correctly propagated through LLVM IR when compiling with LTO. | ||
// | ||
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-secure-hotpatch-functions-list=this_gets_hotpatched -flto /Fo%t.bc %s | ||
// RUN: llvm-dis %t.bc -o - | FileCheck %s | ||
// | ||
// CHECK-LABEL: define dso_local noundef i32 @this_gets_hotpatched() | ||
// CHECK-SAME: #0 | ||
// | ||
// CHECK-LABEL: define dso_local noundef i32 @this_does_not_get_hotpatched() | ||
// CHECK-SAME: #1 | ||
|
||
// CHECK: attributes #0 | ||
// CHECK-SAME: "marked_for_windows_hot_patching" | ||
|
||
// CHECK: attributes #1 | ||
// CHECK-NOT: "marked_for_windows_hot_patching" | ||
|
||
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,21 @@ | ||
// This verifies that hotpatch function attributes are correctly propagated when compiling directly to OBJ. | ||
// | ||
// RUN: echo this_gets_hotpatched > %t.patch-functions.txt | ||
// RUN: %clang_cl -c --target=x86_64-windows-msvc -O2 /Z7 -fms-secure-hotpatch-functions-file=%t.patch-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
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.
Uh oh!
There was an error while loading. Please reload this page.