| 1 |
From 7db340bcc57fb7f4fabbae34b30065fbd77b0174 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Leonard Grey <[email protected]> |
| 3 |
Date: Mon, 8 Apr 2024 16:05:52 -0400 |
| 4 |
Subject: [PATCH] -fsanitize=function: fix .subsections_via_symbols (#87527) |
| 5 |
|
| 6 |
-fsanitize=function emits a signature and function hash before a |
| 7 |
function. Similar to 7f6e2c9, these can be sheared off when |
| 8 |
`.subsections_via_symbols` is used. |
| 9 |
|
| 10 |
This change uses the same technique 7f6e2c9 introduced for prefixes: |
| 11 |
emitting a symbol for the metadata, then marking the actual function |
| 12 |
entry as an .alt_entry symbol. |
| 13 |
--- |
| 14 |
llvm/include/llvm/CodeGen/AsmPrinter.h | 3 ++ |
| 15 |
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 43 ++++++++++++--------- |
| 16 |
llvm/test/CodeGen/AArch64/func-sanitizer.ll | 9 +++++ |
| 17 |
llvm/test/CodeGen/X86/func-sanitizer.ll | 10 +++++ |
| 18 |
4 files changed, 46 insertions(+), 19 deletions(-) |
| 19 |
|
| 20 |
diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h |
| 21 |
index 0ac497c5f8ef..96e302859f44 100644 |
| 22 |
--- a/llvm/include/llvm/CodeGen/AsmPrinter.h |
| 23 |
+++ b/llvm/include/llvm/CodeGen/AsmPrinter.h |
| 24 |
@@ -840,6 +840,9 @@ private: |
| 25 |
/// This method emits a comment next to header for the current function. |
| 26 |
virtual void emitFunctionHeaderComment(); |
| 27 |
|
| 28 |
+ /// This method emits prefix-like data before the current function. |
| 29 |
+ void emitFunctionPrefix(ArrayRef<const Constant *> Prefix); |
| 30 |
+ |
| 31 |
/// Emit a blob of inline asm to the output streamer. |
| 32 |
void |
| 33 |
emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI, |
| 34 |
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp |
| 35 |
index 5381dfdd184c..a1d4c72d2899 100644 |
| 36 |
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp |
| 37 |
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp |
| 38 |
@@ -896,6 +896,27 @@ void AsmPrinter::emitDebugValue(const MCExpr *Value, unsigned Size) const { |
| 39 |
|
| 40 |
void AsmPrinter::emitFunctionHeaderComment() {} |
| 41 |
|
| 42 |
+void AsmPrinter::emitFunctionPrefix(ArrayRef<const Constant *> Prefix) { |
| 43 |
+ const Function &F = MF->getFunction(); |
| 44 |
+ if (!MAI->hasSubsectionsViaSymbols()) { |
| 45 |
+ for (auto &C : Prefix) |
| 46 |
+ emitGlobalConstant(F.getParent()->getDataLayout(), C); |
| 47 |
+ return; |
| 48 |
+ } |
| 49 |
+ // Preserving prefix-like data on platforms which use subsections-via-symbols |
| 50 |
+ // is a bit tricky. Here we introduce a symbol for the prefix-like data |
| 51 |
+ // and use the .alt_entry attribute to mark the function's real entry point |
| 52 |
+ // as an alternative entry point to the symbol that precedes the function.. |
| 53 |
+ OutStreamer->emitLabel(OutContext.createLinkerPrivateTempSymbol()); |
| 54 |
+ |
| 55 |
+ for (auto &C : Prefix) { |
| 56 |
+ emitGlobalConstant(F.getParent()->getDataLayout(), C); |
| 57 |
+ } |
| 58 |
+ |
| 59 |
+ // Emit an .alt_entry directive for the actual function symbol. |
| 60 |
+ OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_AltEntry); |
| 61 |
+} |
| 62 |
+ |
| 63 |
/// EmitFunctionHeader - This method emits the header for the current |
| 64 |
/// function. |
| 65 |
void AsmPrinter::emitFunctionHeader() { |
| 66 |
@@ -935,23 +956,8 @@ void AsmPrinter::emitFunctionHeader() { |
| 67 |
OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_Cold); |
| 68 |
|
| 69 |
// Emit the prefix data. |
| 70 |
- if (F.hasPrefixData()) { |
| 71 |
- if (MAI->hasSubsectionsViaSymbols()) { |
| 72 |
- // Preserving prefix data on platforms which use subsections-via-symbols |
| 73 |
- // is a bit tricky. Here we introduce a symbol for the prefix data |
| 74 |
- // and use the .alt_entry attribute to mark the function's real entry point |
| 75 |
- // as an alternative entry point to the prefix-data symbol. |
| 76 |
- MCSymbol *PrefixSym = OutContext.createLinkerPrivateTempSymbol(); |
| 77 |
- OutStreamer->emitLabel(PrefixSym); |
| 78 |
- |
| 79 |
- emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrefixData()); |
| 80 |
- |
| 81 |
- // Emit an .alt_entry directive for the actual function symbol. |
| 82 |
- OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_AltEntry); |
| 83 |
- } else { |
| 84 |
- emitGlobalConstant(F.getParent()->getDataLayout(), F.getPrefixData()); |
| 85 |
- } |
| 86 |
- } |
| 87 |
+ if (F.hasPrefixData()) |
| 88 |
+ emitFunctionPrefix({F.getPrefixData()}); |
| 89 |
|
| 90 |
// Emit KCFI type information before patchable-function-prefix nops. |
| 91 |
emitKCFITypeId(*MF); |
| 92 |
@@ -983,8 +989,7 @@ void AsmPrinter::emitFunctionHeader() { |
| 93 |
|
| 94 |
auto *PrologueSig = mdconst::extract<Constant>(MD->getOperand(0)); |
| 95 |
auto *TypeHash = mdconst::extract<Constant>(MD->getOperand(1)); |
| 96 |
- emitGlobalConstant(F.getParent()->getDataLayout(), PrologueSig); |
| 97 |
- emitGlobalConstant(F.getParent()->getDataLayout(), TypeHash); |
| 98 |
+ emitFunctionPrefix({PrologueSig, TypeHash}); |
| 99 |
} |
| 100 |
|
| 101 |
if (isVerbose()) { |
| 102 |
diff --git a/llvm/test/CodeGen/AArch64/func-sanitizer.ll b/llvm/test/CodeGen/AArch64/func-sanitizer.ll |
| 103 |
index 89f23e7ed80e..de83d70a5784 100644 |
| 104 |
--- a/llvm/test/CodeGen/AArch64/func-sanitizer.ll |
| 105 |
+++ b/llvm/test/CodeGen/AArch64/func-sanitizer.ll |
| 106 |
@@ -1,4 +1,5 @@ |
| 107 |
; RUN: llc -mtriple=aarch64-unknown-linux-gnu < %s | FileCheck %s |
| 108 |
+; RUN: llc -mtriple=arm64-apple-darwin < %s | FileCheck %s --check-prefix=MACHO |
| 109 |
|
| 110 |
; CHECK-LABEL: .type _Z3funv,@function |
| 111 |
; CHECK-NEXT: .word 3238382334 // 0xc105cafe |
| 112 |
@@ -7,6 +8,14 @@ |
| 113 |
; CHECK-NEXT: // %bb.0: |
| 114 |
; CHECK-NEXT: ret |
| 115 |
|
| 116 |
+; MACHO: ltmp0: |
| 117 |
+; MACHO-NEXT: .long 3238382334 ; 0xc105cafe |
| 118 |
+; MACHO-NEXT: .long 42 ; 0x2a |
| 119 |
+; MACHO-NEXT: .alt_entry __Z3funv |
| 120 |
+; MACHO-NEXT: __Z3funv: |
| 121 |
+; MACHO-NEXT: ; %bb.0: |
| 122 |
+; MACHO-NEXT: ret |
| 123 |
+ |
| 124 |
define dso_local void @_Z3funv() nounwind !func_sanitize !0 { |
| 125 |
ret void |
| 126 |
} |
| 127 |
diff --git a/llvm/test/CodeGen/X86/func-sanitizer.ll b/llvm/test/CodeGen/X86/func-sanitizer.ll |
| 128 |
index b421cb53ddfe..71f062ae2f8c 100644 |
| 129 |
--- a/llvm/test/CodeGen/X86/func-sanitizer.ll |
| 130 |
+++ b/llvm/test/CodeGen/X86/func-sanitizer.ll |
| 131 |
@@ -1,4 +1,5 @@ |
| 132 |
; RUN: llc -mtriple=x86_64-unknown-linux-gnu < %s | FileCheck %s |
| 133 |
+; RUN: llc -mtriple=x86_64-apple-darwin < %s | FileCheck %s --check-prefix=MACHO |
| 134 |
|
| 135 |
; CHECK: .type _Z3funv,@function |
| 136 |
; CHECK-NEXT: .long 3238382334 # 0xc105cafe |
| 137 |
@@ -8,6 +9,15 @@ |
| 138 |
; CHECK-NEXT: # %bb.0: |
| 139 |
; CHECK-NEXT: retq |
| 140 |
|
| 141 |
+; MACHO: ltmp0: |
| 142 |
+; MACHO-NEXT: .long 3238382334 ## 0xc105cafe |
| 143 |
+; MACHO-NEXT: .long 42 ## 0x2a |
| 144 |
+; MACHO-NEXT: .alt_entry __Z3funv |
| 145 |
+; MACHO-NEXT: __Z3funv: |
| 146 |
+; MACHO-NEXT: .cfi_startproc |
| 147 |
+; MACHO-NEXT: # %bb.0: |
| 148 |
+; MACHO-NEXT: retq |
| 149 |
+ |
| 150 |
define dso_local void @_Z3funv() !func_sanitize !0 { |
| 151 |
ret void |
| 152 |
} |
| 153 |
-- |
| 154 |
2.44.0.1.g9765aa7075 |
| 155 |
|