db-move: moved webkitgtk-6.0 from [testing] to [extra] (x86_64)
[arch-packages.git] / clang14 / trunk / clang-coroutines-ubsan.patch
blobc3fe1f517a9ef48f8f069cec82de7a85d1cef69d
1 From 1030297d77ae5110b7873530f645aeec3a4264ba Mon Sep 17 00:00:00 2001
2 From: Yuanfang Chen <yuanfang.chen@sony.com>
3 Date: Mon, 27 Jun 2022 11:33:45 -0700
4 Subject: [PATCH] [ubsan] Using metadata instead of prologue data for function
5 sanitizer
7 Information in the function `Prologue Data` is intentionally opaque.
8 When a function with `Prologue Data` is duplicated. The self (global
9 value) references inside `Prologue Data` is still pointing to the
10 original function. This may cause errors like `fatal error: error in backend: Cannot represent a difference across sections`.
12 This patch detaches the information from function `Prologue Data`
13 and attaches it to a function metadata node.
15 This and D116130 fix https://github.com/llvm/llvm-project/issues/49689.
17 Reviewed By: pcc
19 Differential Revision: https://reviews.llvm.org/D115844
21 (cherry picked from commit 6678f8e505b19069a9dbdc3e3ee088d543752412)
22 ---
23 clang/lib/CodeGen/CodeGenFunction.cpp | 36 ++++--------------
24 clang/lib/CodeGen/CodeGenFunction.h | 4 --
25 clang/lib/CodeGen/CodeGenModule.cpp | 16 ++++++++
26 clang/lib/CodeGen/CodeGenModule.h | 5 +++
27 clang/lib/Driver/SanitizerArgs.cpp | 13 +++++++
28 clang/test/CodeGen/ubsan-function.cpp | 5 ++-
29 .../test/CodeGenCXX/catch-undef-behavior.cpp | 37 +++++++++----------
30 .../CodeGenCXX/ubsan-function-noexcept.cpp | 6 ++-
31 clang/test/Driver/fsanitize.c | 3 ++
32 9 files changed, 69 insertions(+), 56 deletions(-)
34 diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
35 index 50e1638924d1..d7393526bb2c 100644
36 --- a/clang/lib/CodeGen/CodeGenFunction.cpp
37 +++ b/clang/lib/CodeGen/CodeGenFunction.cpp
38 @@ -560,29 +560,6 @@ bool CodeGenFunction::AlwaysEmitXRayTypedEvents() const {
39 XRayInstrKind::Typed);
42 -llvm::Constant *
43 -CodeGenFunction::EncodeAddrForUseInPrologue(llvm::Function *F,
44 - llvm::Constant *Addr) {
45 - // Addresses stored in prologue data can't require run-time fixups and must
46 - // be PC-relative. Run-time fixups are undesirable because they necessitate
47 - // writable text segments, which are unsafe. And absolute addresses are
48 - // undesirable because they break PIE mode.
50 - // Add a layer of indirection through a private global. Taking its address
51 - // won't result in a run-time fixup, even if Addr has linkonce_odr linkage.
52 - auto *GV = new llvm::GlobalVariable(CGM.getModule(), Addr->getType(),
53 - /*isConstant=*/true,
54 - llvm::GlobalValue::PrivateLinkage, Addr);
56 - // Create a PC-relative address.
57 - auto *GOTAsInt = llvm::ConstantExpr::getPtrToInt(GV, IntPtrTy);
58 - auto *FuncAsInt = llvm::ConstantExpr::getPtrToInt(F, IntPtrTy);
59 - auto *PCRelAsInt = llvm::ConstantExpr::getSub(GOTAsInt, FuncAsInt);
60 - return (IntPtrTy == Int32Ty)
61 - ? PCRelAsInt
62 - : llvm::ConstantExpr::getTrunc(PCRelAsInt, Int32Ty);
65 llvm::Value *
66 CodeGenFunction::DecodeAddrUsedInPrologue(llvm::Value *F,
67 llvm::Value *EncodedAddr) {
68 @@ -926,12 +903,13 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
69 FD->getType(), EST_None);
70 llvm::Constant *FTRTTIConst =
71 CGM.GetAddrOfRTTIDescriptor(ProtoTy, /*ForEH=*/true);
72 - llvm::Constant *FTRTTIConstEncoded =
73 - EncodeAddrForUseInPrologue(Fn, FTRTTIConst);
74 - llvm::Constant *PrologueStructElems[] = {PrologueSig, FTRTTIConstEncoded};
75 - llvm::Constant *PrologueStructConst =
76 - llvm::ConstantStruct::getAnon(PrologueStructElems, /*Packed=*/true);
77 - Fn->setPrologueData(PrologueStructConst);
78 + llvm::GlobalVariable *FTRTTIProxy =
79 + CGM.GetOrCreateRTTIProxyGlobalVariable(FTRTTIConst);
80 + llvm::LLVMContext &Ctx = Fn->getContext();
81 + llvm::MDBuilder MDB(Ctx);
82 + Fn->setMetadata(llvm::LLVMContext::MD_func_sanitize,
83 + MDB.createRTTIPointerPrologue(PrologueSig, FTRTTIProxy));
84 + CGM.addCompilerUsedGlobal(FTRTTIProxy);
88 diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h
89 index df99cd9a1b79..046b249b1aac 100644
90 --- a/clang/lib/CodeGen/CodeGenFunction.h
91 +++ b/clang/lib/CodeGen/CodeGenFunction.h
92 @@ -2351,10 +2351,6 @@ public:
93 /// XRay typed event handling calls.
94 bool AlwaysEmitXRayTypedEvents() const;
96 - /// Encode an address into a form suitable for use in a function prologue.
97 - llvm::Constant *EncodeAddrForUseInPrologue(llvm::Function *F,
98 - llvm::Constant *Addr);
100 /// Decode an address used in a function prologue, encoded by \c
101 /// EncodeAddrForUseInPrologue.
102 llvm::Value *DecodeAddrUsedInPrologue(llvm::Value *F,
103 diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
104 index 2777fc22600d..58eef1b0fdca 100644
105 --- a/clang/lib/CodeGen/CodeGenModule.cpp
106 +++ b/clang/lib/CodeGen/CodeGenModule.cpp
107 @@ -1826,6 +1826,22 @@ CodeGenModule::getMostBaseClasses(const CXXRecordDecl *RD) {
108 return MostBases.takeVector();
111 +llvm::GlobalVariable *
112 +CodeGenModule::GetOrCreateRTTIProxyGlobalVariable(llvm::Constant *Addr) {
113 + auto It = RTTIProxyMap.find(Addr);
114 + if (It != RTTIProxyMap.end())
115 + return It->second;
117 + auto *FTRTTIProxy = new llvm::GlobalVariable(
118 + TheModule, Addr->getType(),
119 + /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, Addr,
120 + "__llvm_rtti_proxy");
121 + FTRTTIProxy->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
123 + RTTIProxyMap[Addr] = FTRTTIProxy;
124 + return FTRTTIProxy;
127 void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
128 llvm::Function *F) {
129 llvm::AttrBuilder B(F->getContext());
130 diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
131 index a8a63c8da57f..3a9d542eb2e0 100644
132 --- a/clang/lib/CodeGen/CodeGenModule.h
133 +++ b/clang/lib/CodeGen/CodeGenModule.h
134 @@ -561,6 +561,8 @@ private:
135 MetadataTypeMap VirtualMetadataIdMap;
136 MetadataTypeMap GeneralizedMetadataIdMap;
138 + llvm::DenseMap<const llvm::Constant *, llvm::GlobalVariable *> RTTIProxyMap;
140 public:
141 CodeGenModule(ASTContext &C, const HeaderSearchOptions &headersearchopts,
142 const PreprocessorOptions &ppopts,
143 @@ -1411,6 +1413,9 @@ public:
144 std::vector<const CXXRecordDecl *>
145 getMostBaseClasses(const CXXRecordDecl *RD);
147 + llvm::GlobalVariable *
148 + GetOrCreateRTTIProxyGlobalVariable(llvm::Constant *Addr);
150 /// Get the declaration of std::terminate for the platform.
151 llvm::FunctionCallee getTerminateFn();
153 diff --git a/clang/lib/Driver/SanitizerArgs.cpp b/clang/lib/Driver/SanitizerArgs.cpp
154 index 403fac76f060..96cef9eb80b8 100644
155 --- a/clang/lib/Driver/SanitizerArgs.cpp
156 +++ b/clang/lib/Driver/SanitizerArgs.cpp
157 @@ -367,6 +367,19 @@ SanitizerArgs::SanitizerArgs(const ToolChain &TC,
158 Add &= ~NotAllowedWithMinimalRuntime;
161 + if (llvm::opt::Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
162 + StringRef CM = A->getValue();
163 + if (CM != "small" &&
164 + (Add & SanitizerKind::Function & ~DiagnosedKinds)) {
165 + if (DiagnoseErrors)
166 + D.Diag(diag::err_drv_argument_only_allowed_with)
167 + << "-fsanitize=function"
168 + << "-mcmodel=small";
169 + Add &= ~SanitizerKind::Function;
170 + DiagnosedKinds |= SanitizerKind::Function;
174 // FIXME: Make CFI on member function calls compatible with cross-DSO CFI.
175 // There are currently two problems:
176 // - Virtual function call checks need to pass a pointer to the function
177 diff --git a/clang/test/CodeGen/ubsan-function.cpp b/clang/test/CodeGen/ubsan-function.cpp
178 index 2466d8a2645d..8a16dfdf5da1 100644
179 --- a/clang/test/CodeGen/ubsan-function.cpp
180 +++ b/clang/test/CodeGen/ubsan-function.cpp
181 @@ -1,6 +1,7 @@
182 // RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s -fsanitize=function -fno-sanitize-recover=all | FileCheck %s
184 -// CHECK-LABEL: define{{.*}} void @_Z3funv() #0 prologue <{ i32, i32 }> <{ i32 846595819, i32 trunc (i64 sub (i64 ptrtoint (i8** @0 to i64), i64 ptrtoint (void ()* @_Z3funv to i64)) to i32) }> {
185 +// CHECK: @[[PROXY:.*]] = private unnamed_addr constant i8* bitcast ({ i8*, i8* }* @_ZTIFvvE to i8*)
186 +// CHECK: define{{.*}} void @_Z3funv() #0 !func_sanitize ![[FUNCSAN:.*]] {
187 void fun() {}
189 // CHECK-LABEL: define{{.*}} void @_Z6callerPFvvE(void ()* noundef %f)
190 @@ -20,3 +21,5 @@ void fun() {}
191 // CHECK: [[LABEL3]]:
192 // CHECK: br label %[[LABEL4]], !nosanitize
193 void caller(void (*f)()) { f(); }
195 +// CHECK: ![[FUNCSAN]] = !{i32 846595819, i8** @[[PROXY]]}
196 diff --git a/clang/test/CodeGenCXX/catch-undef-behavior.cpp b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
197 index d6b094cb5b82..ade29797f5a0 100644
198 --- a/clang/test/CodeGenCXX/catch-undef-behavior.cpp
199 +++ b/clang/test/CodeGenCXX/catch-undef-behavior.cpp
200 @@ -1,8 +1,8 @@
201 -// RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=signed-integer-overflow,integer-divide-by-zero,float-divide-by-zero,shift-base,shift-exponent,unreachable,return,vla-bound,alignment,null,vptr,object-size,float-cast-overflow,bool,enum,array-bounds,function -fsanitize-recover=signed-integer-overflow,integer-divide-by-zero,float-divide-by-zero,shift-base,shift-exponent,vla-bound,alignment,null,vptr,object-size,float-cast-overflow,bool,enum,array-bounds,function -emit-llvm %s -o - -triple x86_64-linux-gnu | opt -instnamer -S | FileCheck %s
202 +// RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=signed-integer-overflow,integer-divide-by-zero,float-divide-by-zero,shift-base,shift-exponent,unreachable,return,vla-bound,alignment,null,vptr,object-size,float-cast-overflow,bool,enum,array-bounds,function -fsanitize-recover=signed-integer-overflow,integer-divide-by-zero,float-divide-by-zero,shift-base,shift-exponent,vla-bound,alignment,null,vptr,object-size,float-cast-overflow,bool,enum,array-bounds,function -emit-llvm %s -o - -triple x86_64-linux-gnu | opt -instnamer -S | FileCheck %s --check-prefixes=CHECK,CHECK-FUNCSAN
203 // RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=vptr,address -fsanitize-recover=vptr,address -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=CHECK-ASAN
204 // RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=vptr -fsanitize-recover=vptr -emit-llvm %s -o - -triple x86_64-linux-gnu | FileCheck %s --check-prefix=DOWNCAST-NULL
205 -// RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=function -emit-llvm %s -o - -triple x86_64-linux-gnux32 | FileCheck %s --check-prefix=CHECK-X32
206 -// RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=function -emit-llvm %s -o - -triple i386-linux-gnu | FileCheck %s --check-prefix=CHECK-X86
207 +// RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=function -emit-llvm %s -o - -triple x86_64-linux-gnux32 | FileCheck %s --check-prefix=CHECK-FUNCSAN
208 +// RUN: %clang_cc1 -disable-noundef-analysis -std=c++11 -fsanitize=function -emit-llvm %s -o - -triple i386-linux-gnu | FileCheck %s --check-prefix=CHECK-FUNCSAN
210 struct S {
211 double d;
212 @@ -16,9 +16,7 @@ struct S {
213 // Check that type mismatch handler is not modified by ASan.
214 // CHECK-ASAN: private unnamed_addr global { { [{{.*}} x i8]*, i32, i32 }, { i16, i16, [4 x i8] }*, i8*, i8 } { {{.*}}, { i16, i16, [4 x i8] }* [[TYPE_DESCR]], {{.*}} }
216 -// CHECK: [[IndirectRTTI_ZTIFvPFviEE:@.+]] = private constant i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*)
217 -// CHECK-X86: [[IndirectRTTI_ZTIFvPFviEE:@.+]] = private constant i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*)
218 -// CHECK-X32: [[IndirectRTTI_ZTIFvPFviEE:@.+]] = private constant i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*)
219 +// CHECK-FUNCSAN: [[PROXY:@.+]] = private unnamed_addr constant i8* bitcast ({ i8*, i8* }* @_ZTIFvPFviEE to i8*)
221 struct T : S {};
223 @@ -399,10 +397,7 @@ void downcast_reference(B &b) {
224 // CHECK-NEXT: br i1 [[AND]]
228 -// CHECK-LABEL: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i32 }> <{ i32 846595819, i32 trunc (i64 sub (i64 ptrtoint (i8** {{.*}} to i64), i64 ptrtoint (void (void (i32)*)* @_Z22indirect_function_callPFviE to i64)) to i32) }>
229 -// CHECK-X32: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i32 }> <{ i32 846595819, i32 sub (i32 ptrtoint (i8** [[IndirectRTTI_ZTIFvPFviEE]] to i32), i32 ptrtoint (void (void (i32)*)* @_Z22indirect_function_callPFviE to i32)) }>
230 -// CHECK-X86: @_Z22indirect_function_callPFviE({{.*}} prologue <{ i32, i32 }> <{ i32 846595819, i32 sub (i32 ptrtoint (i8** [[IndirectRTTI_ZTIFvPFviEE]] to i32), i32 ptrtoint (void (void (i32)*)* @_Z22indirect_function_callPFviE to i32)) }>
231 +// CHECK-FUNCSAN: @_Z22indirect_function_callPFviE({{.*}} !func_sanitize ![[FUNCSAN:.*]] {
232 void indirect_function_call(void (*p)(int)) {
233 // CHECK: [[PTR:%.+]] = bitcast void (i32)* {{.*}} to <{ i32, i32 }>*
235 @@ -483,34 +478,34 @@ void force_irgen() {
238 // CHECK-LABEL: define{{.*}} void @_ZN29FunctionSanitizerVirtualCalls1B1fEv
239 -// CHECK-NOT: prologue
240 +// CHECK-NOT: !func_sanitize
242 // CHECK-LABEL: define{{.*}} void @_ZTv0_n24_N29FunctionSanitizerVirtualCalls1B1fEv
243 -// CHECK-NOT: prologue
244 +// CHECK-NOT: !func_sanitize
246 // CHECK-LABEL: define{{.*}} void @_ZN29FunctionSanitizerVirtualCalls11force_irgenEv()
247 -// CHECK: prologue
248 +// CHECK: !func_sanitize
250 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1AC1Ev
251 -// CHECK-NOT: prologue
252 +// CHECK-NOT: !func_sanitize
254 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1A1gEv
255 -// CHECK-NOT: prologue
256 +// CHECK-NOT: !func_sanitize
258 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1A1hEv
259 -// CHECK-NOT: prologue
260 +// CHECK-NOT: !func_sanitize
262 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1BC1Ev
263 -// CHECK-NOT: prologue
264 +// CHECK-NOT: !func_sanitize
266 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1B1bEv
267 -// CHECK-NOT: prologue
268 +// CHECK-NOT: !func_sanitize
270 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1B1gEv
271 -// CHECK-NOT: prologue
272 +// CHECK-NOT: !func_sanitize
274 // CHECK-LABEL: define linkonce_odr void @_ZN29FunctionSanitizerVirtualCalls1B1qEv
275 -// CHECK: prologue
276 +// CHECK: !func_sanitize
280 @@ -754,3 +749,5 @@ void ThisAlign::this_align_lambda_2() {
283 // CHECK: attributes [[NR_NUW]] = { noreturn nounwind }
285 +// CHECK-FUNCSAN: ![[FUNCSAN]] = !{i32 846595819, i8** [[PROXY]]}
286 diff --git a/clang/test/CodeGenCXX/ubsan-function-noexcept.cpp b/clang/test/CodeGenCXX/ubsan-function-noexcept.cpp
287 index 3c0c0e8be91f..9d5eb1edefe5 100644
288 --- a/clang/test/CodeGenCXX/ubsan-function-noexcept.cpp
289 +++ b/clang/test/CodeGenCXX/ubsan-function-noexcept.cpp
290 @@ -2,8 +2,8 @@
292 // Check that typeinfo recorded in function prolog doesn't have "Do" noexcept
293 // qualifier in its mangled name.
294 -// CHECK: @[[RTTI:[0-9]+]] = private constant i8* bitcast ({ i8*, i8* }* @_ZTIFvvE to i8*)
295 -// CHECK: define{{.*}} void @_Z1fv() #{{.*}} prologue <{ i32, i32 }> <{ i32 {{.*}}, i32 trunc (i64 sub (i64 ptrtoint (i8** @[[RTTI]] to i64), i64 ptrtoint (void ()* @_Z1fv to i64)) to i32) }>
296 +// CHECK: [[PROXY:@.*]] = private unnamed_addr constant i8* bitcast ({ i8*, i8* }* @_ZTIFvvE to i8*)
297 +// CHECK: define{{.*}} void @_Z1fv() #{{.*}} !func_sanitize ![[FUNCSAN:.*]] {
298 void f() noexcept {}
300 // CHECK: define{{.*}} void @_Z1gPDoFvvE
301 @@ -13,3 +13,5 @@ void g(void (*p)() noexcept) {
302 // CHECK: icmp eq i8* %{{.*}}, bitcast ({ i8*, i8* }* @_ZTIFvvE to i8*), !nosanitize
303 p();
306 +// CHECK: ![[FUNCSAN]] = !{i32 846595819, i8** [[PROXY]]}
307 diff --git a/clang/test/Driver/fsanitize.c b/clang/test/Driver/fsanitize.c
308 index 17fce1981eea..624dc98478ec 100644
309 --- a/clang/test/Driver/fsanitize.c
310 +++ b/clang/test/Driver/fsanitize.c
311 @@ -915,3 +915,6 @@
313 // RUN: %clang -fsanitize=undefined,float-divide-by-zero %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-DIVBYZERO-UBSAN
314 // CHECK-DIVBYZERO-UBSAN: "-fsanitize={{.*}},float-divide-by-zero,{{.*}}"
316 +// RUN: %clang -target x86_64-linux-gnu -fsanitize=undefined,function -mcmodel=large %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-UBSAN-FUNCTION-CODE-MODEL
317 +// CHECK-UBSAN-FUNCTION-CODE-MODEL: error: invalid argument '-fsanitize=function' only allowed with '-mcmodel=small'