Skip to content

Commit 1f61f39

Browse files
committed
Raise error if trying to use Windows hotpatching on non-Windows OS
1 parent 0b35988 commit 1f61f39

File tree

7 files changed

+73
-6
lines changed

7 files changed

+73
-6
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,7 @@ CodeGenModule::CodeGenModule(ASTContext &C,
456456

457457
// If there are any functions that are marked for Windows hot-patching,
458458
// then build the list of functions now.
459-
if (!CGO.MSHotPatchFunctionsFile.empty() ||
460-
!CGO.MSHotPatchFunctionsList.empty()) {
459+
if (M.getTargetTriple().isOSBinFormatCOFF()) {
461460
if (!CGO.MSHotPatchFunctionsFile.empty()) {
462461
auto BufOrErr = llvm::MemoryBuffer::getFile(CGO.MSHotPatchFunctionsFile);
463462
if (BufOrErr) {
@@ -486,6 +485,22 @@ CodeGenModule::CodeGenModule(ASTContext &C,
486485

487486
std::sort(this->MSHotPatchFunctions.begin(),
488487
this->MSHotPatchFunctions.end());
488+
} else {
489+
if (!CGO.MSHotPatchFunctionsFile.empty()) {
490+
unsigned DiagID = diags.getCustomDiagID(
491+
DiagnosticsEngine::Error,
492+
"hotpatch functions file (-fms-hotpatch-functions-file) is only "
493+
"supported on Windows targets");
494+
diags.Report(DiagID);
495+
}
496+
497+
if (!CGO.MSHotPatchFunctionsList.empty()) {
498+
unsigned DiagID = diags.getCustomDiagID(
499+
DiagnosticsEngine::Error,
500+
"hotpatch functions list (-fms-hotpatch-functions-list) is only "
501+
"supported on Windows targets");
502+
diags.Report(DiagID);
503+
}
489504
}
490505
}
491506

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This verifies that enabling Windows hotpatching when targeting a non-Windows OS causes an error.
2+
//
3+
// 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
4+
// CHECK: hotpatch functions file (-fms-hotpatch-functions-file) is only supported on Windows targets
5+
// CHECK: hotpatch functions list (-fms-hotpatch-functions-list) is only supported on Windows targets
6+
7+
void foo();

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,7 @@ void TargetPassConfig::addIRPasses() {
894894
if (EnableGlobalMergeFunc)
895895
addPass(createGlobalMergeFuncPass());
896896

897-
if (TM->getTargetTriple().isOSBinFormatCOFF())
898-
addPass(createWindowsHotPatch());
897+
addPass(createWindowsHotPatch());
899898
}
900899

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

llvm/lib/CodeGen/WindowsHotPatch.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,35 @@ ModulePass *llvm::createWindowsHotPatch() { return new WindowsHotPatch(); }
8484
// Find functions marked with Attribute::MarkedForWindowsHotPatching and modify
8585
// their code (if necessary) to account for accesses to global variables.
8686
bool WindowsHotPatch::runOnModule(Module &M) {
87+
// If the target OS is not Windows, then check that there are no functions
88+
// marked for Windows hot-patching.
89+
if (!M.getTargetTriple().isOSBinFormatCOFF()) {
90+
if (!LLVMMSHotPatchFunctionsFile.empty()) {
91+
M.getContext().diagnose(llvm::DiagnosticInfoGeneric{
92+
llvm::Twine("--ms-hotpatch-functions-file is only supported when "
93+
"target OS is Windows")});
94+
}
95+
96+
if (!LLVMMSHotPatchFunctionsList.empty()) {
97+
M.getContext().diagnose(llvm::DiagnosticInfoGeneric{
98+
llvm::Twine("--ms-hotpatch-functions-list is only supported when "
99+
"target OS is Windows")});
100+
}
101+
102+
// Functions may have already been marked for hot-patching by the front-end.
103+
// Check for any functions marked for hot-patching and report an error if
104+
// any are found.
105+
for (auto &F : M.functions()) {
106+
if (F.hasFnAttribute(Attribute::MarkedForWindowsHotPatching)) {
107+
M.getContext().diagnose(llvm::DiagnosticInfoGeneric{
108+
llvm::Twine("function is marked for Windows hot-patching, but the "
109+
"target OS is not Windows: ") +
110+
F.getName()});
111+
}
112+
}
113+
return false;
114+
}
115+
87116
// The front end may have already marked functions for hot-patching. However,
88117
// we also allow marking functions by passing -ms-hotpatch-functions-file or
89118
// -ms-hotpatch-functions-list directly to LLVM. This allows hot-patching to
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; This tests directly annotating a function with marked_for_windows_hot_patching, but on the wrong target OS.
2+
;
3+
; RUN: not llc -mtriple=x86_64-unknown-linux --ms-hotpatch-functions-list=this_never_gets_used --ms-hotpatch-functions-file=this_never_gets_used < %s 2>&1 | FileCheck %s
4+
;
5+
; CHECK: error: --ms-hotpatch-functions-file is only supported when target OS is Windows
6+
; CHECK: error: --ms-hotpatch-functions-list is only supported when target OS is Windows
7+
; CHECK: error: function is marked for Windows hot-patching, but the target OS is not Windows: this_gets_hotpatched
8+
9+
source_filename = ".\\ms-hotpatch-bad-os.ll"
10+
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
11+
target triple = "x86_64-unknown-linux"
12+
13+
define noundef i32 @this_gets_hotpatched() #0 {
14+
ret i32 0
15+
}
16+
17+
attributes #0 = { marked_for_windows_hot_patching mustprogress noinline nounwind optnone uwtable }

llvm/test/CodeGen/Generic/ms-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.ms-hotpatch-functions-file.txt
4+
; RUN: llc -mtriple=x86_64-windows --ms-hotpatch-functions-file=%t.ms-hotpatch-functions-file.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-file.txt

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

0 commit comments

Comments
 (0)