1//===--- CGClass.cpp - Emit LLVM Code for C++ classes -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code dealing with C++ code generation of classes
10//
11//===----------------------------------------------------------------------===//
12
13#include "ABIInfoImpl.h"
14#include "CGBlocks.h"
15#include "CGCXXABI.h"
16#include "CGDebugInfo.h"
17#include "CGRecordLayout.h"
18#include "CodeGenFunction.h"
19#include "TargetInfo.h"
20#include "clang/AST/Attr.h"
21#include "clang/AST/CXXInheritance.h"
22#include "clang/AST/CharUnits.h"
23#include "clang/AST/DeclTemplate.h"
24#include "clang/AST/EvaluatedExprVisitor.h"
25#include "clang/AST/RecordLayout.h"
26#include "clang/AST/StmtCXX.h"
27#include "clang/Basic/CodeGenOptions.h"
28#include "clang/CodeGen/CGFunctionInfo.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/Metadata.h"
31#include "llvm/Support/SaveAndRestore.h"
32#include "llvm/Transforms/Utils/SanitizerStats.h"
33#include <optional>
34
35using namespace clang;
36using namespace CodeGen;
37
38/// Return the best known alignment for an unknown pointer to a
39/// particular class.
40CharUnits CodeGenModule::getClassPointerAlignment(const CXXRecordDecl *RD) {
41 if (!RD->hasDefinition())
42 return CharUnits::One(); // Hopefully won't be used anywhere.
43
44 auto &layout = getContext().getASTRecordLayout(D: RD);
45
46 // If the class is final, then we know that the pointer points to an
47 // object of that type and can use the full alignment.
48 if (RD->isEffectivelyFinal())
49 return layout.getAlignment();
50
51 // Otherwise, we have to assume it could be a subclass.
52 return layout.getNonVirtualAlignment();
53}
54
55/// Return the smallest possible amount of storage that might be allocated
56/// starting from the beginning of an object of a particular class.
57///
58/// This may be smaller than sizeof(RD) if RD has virtual base classes.
59CharUnits CodeGenModule::getMinimumClassObjectSize(const CXXRecordDecl *RD) {
60 if (!RD->hasDefinition())
61 return CharUnits::One();
62
63 auto &layout = getContext().getASTRecordLayout(D: RD);
64
65 // If the class is final, then we know that the pointer points to an
66 // object of that type and can use the full alignment.
67 if (RD->isEffectivelyFinal())
68 return layout.getSize();
69
70 // Otherwise, we have to assume it could be a subclass.
71 return std::max(a: layout.getNonVirtualSize(), b: CharUnits::One());
72}
73
74/// Return the best known alignment for a pointer to a virtual base,
75/// given the alignment of a pointer to the derived class.
76CharUnits CodeGenModule::getVBaseAlignment(CharUnits actualDerivedAlign,
77 const CXXRecordDecl *derivedClass,
78 const CXXRecordDecl *vbaseClass) {
79 // The basic idea here is that an underaligned derived pointer might
80 // indicate an underaligned base pointer.
81
82 assert(vbaseClass->isCompleteDefinition());
83 auto &baseLayout = getContext().getASTRecordLayout(D: vbaseClass);
84 CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment();
85
86 return getDynamicOffsetAlignment(ActualAlign: actualDerivedAlign, Class: derivedClass,
87 ExpectedTargetAlign: expectedVBaseAlign);
88}
89
90CharUnits
91CodeGenModule::getDynamicOffsetAlignment(CharUnits actualBaseAlign,
92 const CXXRecordDecl *baseDecl,
93 CharUnits expectedTargetAlign) {
94 // If the base is an incomplete type (which is, alas, possible with
95 // member pointers), be pessimistic.
96 if (!baseDecl->isCompleteDefinition())
97 return std::min(a: actualBaseAlign, b: expectedTargetAlign);
98
99 auto &baseLayout = getContext().getASTRecordLayout(D: baseDecl);
100 CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment();
101
102 // If the class is properly aligned, assume the target offset is, too.
103 //
104 // This actually isn't necessarily the right thing to do --- if the
105 // class is a complete object, but it's only properly aligned for a
106 // base subobject, then the alignments of things relative to it are
107 // probably off as well. (Note that this requires the alignment of
108 // the target to be greater than the NV alignment of the derived
109 // class.)
110 //
111 // However, our approach to this kind of under-alignment can only
112 // ever be best effort; after all, we're never going to propagate
113 // alignments through variables or parameters. Note, in particular,
114 // that constructing a polymorphic type in an address that's less
115 // than pointer-aligned will generally trap in the constructor,
116 // unless we someday add some sort of attribute to change the
117 // assumed alignment of 'this'. So our goal here is pretty much
118 // just to allow the user to explicitly say that a pointer is
119 // under-aligned and then safely access its fields and vtables.
120 if (actualBaseAlign >= expectedBaseAlign) {
121 return expectedTargetAlign;
122 }
123
124 // Otherwise, we might be offset by an arbitrary multiple of the
125 // actual alignment. The correct adjustment is to take the min of
126 // the two alignments.
127 return std::min(a: actualBaseAlign, b: expectedTargetAlign);
128}
129
130Address CodeGenFunction::LoadCXXThisAddress() {
131 assert(CurFuncDecl && "loading 'this' without a func declaration?");
132 auto *MD = cast<CXXMethodDecl>(Val: CurFuncDecl);
133
134 // Lazily compute CXXThisAlignment.
135 if (CXXThisAlignment.isZero()) {
136 // Just use the best known alignment for the parent.
137 // TODO: if we're currently emitting a complete-object ctor/dtor,
138 // we can always use the complete-object alignment.
139 CXXThisAlignment = CGM.getClassPointerAlignment(RD: MD->getParent());
140 }
141
142 return makeNaturalAddressForPointer(
143 Ptr: LoadCXXThis(), T: MD->getFunctionObjectParameterType(), Alignment: CXXThisAlignment,
144 ForPointeeType: false, BaseInfo: nullptr, TBAAInfo: nullptr, IsKnownNonNull: KnownNonNull);
145}
146
147/// Emit the address of a field using a member data pointer.
148///
149/// \param E Only used for emergency diagnostics
150Address CodeGenFunction::EmitCXXMemberDataPointerAddress(
151 const Expr *E, Address base, llvm::Value *memberPtr,
152 const MemberPointerType *memberPtrType, bool IsInBounds,
153 LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) {
154 // Ask the ABI to compute the actual address.
155 llvm::Value *ptr = CGM.getCXXABI().EmitMemberDataPointerAddress(
156 CGF&: *this, E, Base: base, MemPtr: memberPtr, MPT: memberPtrType, IsInBounds);
157
158 QualType memberType = memberPtrType->getPointeeType();
159 CharUnits memberAlign =
160 CGM.getNaturalTypeAlignment(T: memberType, BaseInfo, TBAAInfo);
161 memberAlign = CGM.getDynamicOffsetAlignment(
162 actualBaseAlign: base.getAlignment(), baseDecl: memberPtrType->getMostRecentCXXRecordDecl(),
163 expectedTargetAlign: memberAlign);
164 return Address(ptr, ConvertTypeForMem(T: memberPtrType->getPointeeType()),
165 memberAlign);
166}
167
168CharUnits CodeGenModule::computeNonVirtualBaseClassOffset(
169 const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start,
170 CastExpr::path_const_iterator End) {
171 CharUnits Offset = CharUnits::Zero();
172
173 const ASTContext &Context = getContext();
174 const CXXRecordDecl *RD = DerivedClass;
175
176 for (CastExpr::path_const_iterator I = Start; I != End; ++I) {
177 const CXXBaseSpecifier *Base = *I;
178 assert(!Base->isVirtual() && "Should not see virtual bases here!");
179
180 // Get the layout.
181 const ASTRecordLayout &Layout = Context.getASTRecordLayout(D: RD);
182
183 const auto *BaseDecl = Base->getType()->castAsCXXRecordDecl();
184 // Add the offset.
185 Offset += Layout.getBaseClassOffset(Base: BaseDecl);
186
187 RD = BaseDecl;
188 }
189
190 return Offset;
191}
192
193llvm::Constant *CodeGenModule::GetNonVirtualBaseClassOffset(
194 const CXXRecordDecl *ClassDecl, CastExpr::path_const_iterator PathBegin,
195 CastExpr::path_const_iterator PathEnd) {
196 assert(PathBegin != PathEnd && "Base path should not be empty!");
197
198 CharUnits Offset =
199 computeNonVirtualBaseClassOffset(DerivedClass: ClassDecl, Start: PathBegin, End: PathEnd);
200 if (Offset.isZero())
201 return nullptr;
202
203 llvm::Type *PtrDiffTy =
204 getTypes().ConvertType(T: getContext().getPointerDiffType());
205
206 return llvm::ConstantInt::get(Ty: PtrDiffTy, V: Offset.getQuantity());
207}
208
209/// Gets the address of a direct base class within a complete object.
210/// This should only be used for (1) non-virtual bases or (2) virtual bases
211/// when the type is known to be complete (e.g. in complete destructors).
212///
213/// The object pointed to by 'This' is assumed to be non-null.
214Address CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(
215 Address This, const CXXRecordDecl *Derived, const CXXRecordDecl *Base,
216 bool BaseIsVirtual) {
217 // 'this' must be a pointer (in some address space) to Derived.
218 assert(This.getElementType() == ConvertType(Derived));
219
220 // Compute the offset of the virtual base.
221 CharUnits Offset;
222 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D: Derived);
223 if (BaseIsVirtual)
224 Offset = Layout.getVBaseClassOffset(VBase: Base);
225 else
226 Offset = Layout.getBaseClassOffset(Base);
227
228 // Shift and cast down to the base type.
229 // TODO: for complete types, this should be possible with a GEP.
230 Address V = This;
231 if (!Offset.isZero()) {
232 V = V.withElementType(ElemTy: Int8Ty);
233 V = Builder.CreateConstInBoundsByteGEP(Addr: V, Offset);
234 }
235 return V.withElementType(ElemTy: ConvertType(T: Base));
236}
237
238static Address ApplyNonVirtualAndVirtualOffset(
239 CodeGenFunction &CGF, Address addr, CharUnits nonVirtualOffset,
240 llvm::Value *virtualOffset, const CXXRecordDecl *derivedClass,
241 const CXXRecordDecl *nearestVBase) {
242 // Assert that we have something to do.
243 assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr);
244
245 // Compute the offset from the static and dynamic components.
246 llvm::Value *baseOffset;
247 if (!nonVirtualOffset.isZero()) {
248 llvm::Type *OffsetType =
249 (CGF.CGM.getTarget().getCXXABI().isItaniumFamily() &&
250 CGF.CGM.getLangOpts().RelativeCXXABIVTables)
251 ? CGF.Int32Ty
252 : CGF.PtrDiffTy;
253 baseOffset =
254 llvm::ConstantInt::get(Ty: OffsetType, V: nonVirtualOffset.getQuantity());
255 if (virtualOffset) {
256 baseOffset = CGF.Builder.CreateAdd(LHS: virtualOffset, RHS: baseOffset);
257 }
258 } else {
259 baseOffset = virtualOffset;
260 }
261
262 // Apply the base offset.
263 llvm::Value *ptr = addr.emitRawPointer(CGF);
264 ptr = CGF.Builder.CreateInBoundsGEP(Ty: CGF.Int8Ty, Ptr: ptr, IdxList: baseOffset, Name: "add.ptr");
265
266 // If we have a virtual component, the alignment of the result will
267 // be relative only to the known alignment of that vbase.
268 CharUnits alignment;
269 if (virtualOffset) {
270 assert(nearestVBase && "virtual offset without vbase?");
271 alignment = CGF.CGM.getVBaseAlignment(actualDerivedAlign: addr.getAlignment(), derivedClass,
272 vbaseClass: nearestVBase);
273 } else {
274 alignment = addr.getAlignment();
275 }
276 alignment = alignment.alignmentAtOffset(offset: nonVirtualOffset);
277
278 return Address(ptr, CGF.Int8Ty, alignment);
279}
280
281Address CodeGenFunction::GetAddressOfBaseClass(
282 Address Value, const CXXRecordDecl *Derived,
283 CastExpr::path_const_iterator PathBegin,
284 CastExpr::path_const_iterator PathEnd, bool NullCheckValue,
285 SourceLocation Loc) {
286 assert(PathBegin != PathEnd && "Base path should not be empty!");
287
288 CastExpr::path_const_iterator Start = PathBegin;
289 const CXXRecordDecl *VBase = nullptr;
290
291 // Sema has done some convenient canonicalization here: if the
292 // access path involved any virtual steps, the conversion path will
293 // *start* with a step down to the correct virtual base subobject,
294 // and hence will not require any further steps.
295 if ((*Start)->isVirtual()) {
296 VBase = (*Start)->getType()->castAsCXXRecordDecl();
297 ++Start;
298 }
299
300 // Compute the static offset of the ultimate destination within its
301 // allocating subobject (the virtual base, if there is one, or else
302 // the "complete" object that we see).
303 CharUnits NonVirtualOffset = CGM.computeNonVirtualBaseClassOffset(
304 DerivedClass: VBase ? VBase : Derived, Start, End: PathEnd);
305
306 // If there's a virtual step, we can sometimes "devirtualize" it.
307 // For now, that's limited to when the derived type is final.
308 // TODO: "devirtualize" this for accesses to known-complete objects.
309 if (VBase && Derived->hasAttr<FinalAttr>()) {
310 const ASTRecordLayout &layout = getContext().getASTRecordLayout(D: Derived);
311 CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase);
312 NonVirtualOffset += vBaseOffset;
313 VBase = nullptr; // we no longer have a virtual step
314 }
315
316 // Get the base pointer type.
317 llvm::Type *BaseValueTy = ConvertType(T: (PathEnd[-1])->getType());
318 llvm::Type *PtrTy = llvm::PointerType::get(
319 C&: CGM.getLLVMContext(), AddressSpace: Value.getType()->getPointerAddressSpace());
320
321 CanQualType DerivedTy = getContext().getCanonicalTagType(TD: Derived);
322 CharUnits DerivedAlign = CGM.getClassPointerAlignment(RD: Derived);
323
324 // If the static offset is zero and we don't have a virtual step,
325 // just do a bitcast; null checks are unnecessary.
326 if (NonVirtualOffset.isZero() && !VBase) {
327 if (sanitizePerformTypeCheck()) {
328 SanitizerSet SkippedChecks;
329 SkippedChecks.set(K: SanitizerKind::Null, Value: !NullCheckValue);
330 EmitTypeCheck(TCK: TCK_Upcast, Loc, V: Value.emitRawPointer(CGF&: *this), Type: DerivedTy,
331 Alignment: DerivedAlign, SkippedChecks);
332 }
333 return Value.withElementType(ElemTy: BaseValueTy);
334 }
335
336 llvm::BasicBlock *origBB = nullptr;
337 llvm::BasicBlock *endBB = nullptr;
338
339 // Skip over the offset (and the vtable load) if we're supposed to
340 // null-check the pointer.
341 if (NullCheckValue) {
342 origBB = Builder.GetInsertBlock();
343 llvm::BasicBlock *notNullBB = createBasicBlock(name: "cast.notnull");
344 endBB = createBasicBlock(name: "cast.end");
345
346 llvm::Value *isNull = Builder.CreateIsNull(Addr: Value);
347 Builder.CreateCondBr(Cond: isNull, True: endBB, False: notNullBB);
348 EmitBlock(BB: notNullBB);
349 }
350
351 if (sanitizePerformTypeCheck()) {
352 SanitizerSet SkippedChecks;
353 SkippedChecks.set(K: SanitizerKind::Null, Value: true);
354 EmitTypeCheck(TCK: VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc,
355 V: Value.emitRawPointer(CGF&: *this), Type: DerivedTy, Alignment: DerivedAlign,
356 SkippedChecks);
357 }
358
359 // Compute the virtual offset.
360 llvm::Value *VirtualOffset = nullptr;
361 if (VBase) {
362 VirtualOffset =
363 CGM.getCXXABI().GetVirtualBaseClassOffset(CGF&: *this, This: Value, ClassDecl: Derived, BaseClassDecl: VBase);
364 }
365
366 // Apply both offsets.
367 Value = ApplyNonVirtualAndVirtualOffset(CGF&: *this, addr: Value, nonVirtualOffset: NonVirtualOffset,
368 virtualOffset: VirtualOffset, derivedClass: Derived, nearestVBase: VBase);
369
370 // Cast to the destination type.
371 Value = Value.withElementType(ElemTy: BaseValueTy);
372
373 // Build a phi if we needed a null check.
374 if (NullCheckValue) {
375 llvm::BasicBlock *notNullBB = Builder.GetInsertBlock();
376 Builder.CreateBr(Dest: endBB);
377 EmitBlock(BB: endBB);
378
379 llvm::PHINode *PHI = Builder.CreatePHI(Ty: PtrTy, NumReservedValues: 2, Name: "cast.result");
380 PHI->addIncoming(V: Value.emitRawPointer(CGF&: *this), BB: notNullBB);
381 PHI->addIncoming(V: llvm::Constant::getNullValue(Ty: PtrTy), BB: origBB);
382 Value = Value.withPointer(NewPointer: PHI, IsKnownNonNull: NotKnownNonNull);
383 }
384
385 return Value;
386}
387
388Address CodeGenFunction::GetAddressOfDerivedClass(
389 Address BaseAddr, const CXXRecordDecl *Derived,
390 CastExpr::path_const_iterator PathBegin,
391 CastExpr::path_const_iterator PathEnd, bool NullCheckValue) {
392 assert(PathBegin != PathEnd && "Base path should not be empty!");
393
394 CanQualType DerivedTy = getContext().getCanonicalTagType(TD: Derived);
395 llvm::Type *DerivedValueTy = ConvertType(T: DerivedTy);
396
397 llvm::Value *NonVirtualOffset =
398 CGM.GetNonVirtualBaseClassOffset(ClassDecl: Derived, PathBegin, PathEnd);
399
400 if (!NonVirtualOffset) {
401 // No offset, we can just cast back.
402 return BaseAddr.withElementType(ElemTy: DerivedValueTy);
403 }
404
405 llvm::BasicBlock *CastNull = nullptr;
406 llvm::BasicBlock *CastNotNull = nullptr;
407 llvm::BasicBlock *CastEnd = nullptr;
408
409 if (NullCheckValue) {
410 CastNull = createBasicBlock(name: "cast.null");
411 CastNotNull = createBasicBlock(name: "cast.notnull");
412 CastEnd = createBasicBlock(name: "cast.end");
413
414 llvm::Value *IsNull = Builder.CreateIsNull(Addr: BaseAddr);
415 Builder.CreateCondBr(Cond: IsNull, True: CastNull, False: CastNotNull);
416 EmitBlock(BB: CastNotNull);
417 }
418
419 // Apply the offset.
420 Address Addr = BaseAddr.withElementType(ElemTy: Int8Ty);
421 Addr = Builder.CreateInBoundsGEP(
422 Addr, IdxList: Builder.CreateNeg(V: NonVirtualOffset), ElementType: Int8Ty,
423 Align: CGM.getClassPointerAlignment(RD: Derived), Name: "sub.ptr");
424
425 // Just cast.
426 Addr = Addr.withElementType(ElemTy: DerivedValueTy);
427
428 // Produce a PHI if we had a null-check.
429 if (NullCheckValue) {
430 Builder.CreateBr(Dest: CastEnd);
431 EmitBlock(BB: CastNull);
432 Builder.CreateBr(Dest: CastEnd);
433 EmitBlock(BB: CastEnd);
434
435 llvm::Value *Value = Addr.emitRawPointer(CGF&: *this);
436 llvm::PHINode *PHI = Builder.CreatePHI(Ty: Value->getType(), NumReservedValues: 2);
437 PHI->addIncoming(V: Value, BB: CastNotNull);
438 PHI->addIncoming(V: llvm::Constant::getNullValue(Ty: Value->getType()), BB: CastNull);
439 return Address(PHI, Addr.getElementType(),
440 CGM.getClassPointerAlignment(RD: Derived));
441 }
442
443 return Addr;
444}
445
446llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD,
447 bool ForVirtualBase,
448 bool Delegating) {
449 if (!CGM.getCXXABI().NeedsVTTParameter(GD)) {
450 // This constructor/destructor does not need a VTT parameter.
451 return nullptr;
452 }
453
454 const CXXRecordDecl *RD = cast<CXXMethodDecl>(Val: CurCodeDecl)->getParent();
455 const CXXRecordDecl *Base = cast<CXXMethodDecl>(Val: GD.getDecl())->getParent();
456
457 uint64_t SubVTTIndex;
458
459 if (Delegating) {
460 // If this is a delegating constructor call, just load the VTT.
461 return LoadCXXVTT();
462 } else if (RD == Base) {
463 // If the record matches the base, this is the complete ctor/dtor
464 // variant calling the base variant in a class with virtual bases.
465 assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) &&
466 "doing no-op VTT offset in base dtor/ctor?");
467 assert(!ForVirtualBase && "Can't have same class as virtual base!");
468 SubVTTIndex = 0;
469 } else {
470 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D: RD);
471 CharUnits BaseOffset = ForVirtualBase ? Layout.getVBaseClassOffset(VBase: Base)
472 : Layout.getBaseClassOffset(Base);
473
474 SubVTTIndex =
475 CGM.getVTables().getSubVTTIndex(RD, Base: BaseSubobject(Base, BaseOffset));
476 assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!");
477 }
478
479 llvm::Value *VTT;
480 if (CGM.getCXXABI().NeedsVTTParameter(GD: CurGD)) {
481 // A VTT parameter was passed to the constructor, use it.
482 VTT = LoadCXXVTT();
483 } else {
484 // We're the complete constructor, so get the VTT by name.
485 VTT = CGM.getVTables().GetAddrOfVTT(RD);
486 }
487 return Builder.CreateConstInBoundsGEP1_64(Ty: CGM.GlobalsInt8PtrTy, Ptr: VTT,
488 Idx0: SubVTTIndex);
489}
490
491namespace {
492/// Call the destructor for a direct base class.
493struct CallBaseDtor final : EHScopeStack::Cleanup {
494 const CXXRecordDecl *BaseClass;
495 bool BaseIsVirtual;
496 CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual)
497 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
498
499 void Emit(CodeGenFunction &CGF, Flags flags) override {
500 const CXXRecordDecl *DerivedClass =
501 cast<CXXMethodDecl>(Val: CGF.CurCodeDecl)->getParent();
502
503 const CXXDestructorDecl *D = BaseClass->getDestructor();
504 // We are already inside a destructor, so presumably the object being
505 // destroyed should have the expected type.
506 QualType ThisTy = D->getFunctionObjectParameterType();
507 Address Addr = CGF.GetAddressOfDirectBaseInCompleteClass(
508 This: CGF.LoadCXXThisAddress(), Derived: DerivedClass, Base: BaseClass, BaseIsVirtual);
509 CGF.EmitCXXDestructorCall(D, Type: Dtor_Base, ForVirtualBase: BaseIsVirtual,
510 /*Delegating=*/false, This: Addr, ThisTy);
511 }
512};
513
514/// A visitor which checks whether an initializer uses 'this' in a
515/// way which requires the vtable to be properly set.
516struct DynamicThisUseChecker
517 : ConstEvaluatedExprVisitor<DynamicThisUseChecker> {
518 typedef ConstEvaluatedExprVisitor<DynamicThisUseChecker> super;
519
520 bool UsesThis;
521
522 DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {}
523
524 // Black-list all explicit and implicit references to 'this'.
525 //
526 // Do we need to worry about external references to 'this' derived
527 // from arbitrary code? If so, then anything which runs arbitrary
528 // external code might potentially access the vtable.
529 void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; }
530};
531} // end anonymous namespace
532
533static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) {
534 DynamicThisUseChecker Checker(C);
535 Checker.Visit(S: Init);
536 return Checker.UsesThis;
537}
538
539static void EmitBaseInitializer(CodeGenFunction &CGF,
540 const CXXRecordDecl *ClassDecl,
541 CXXCtorInitializer *BaseInit) {
542 assert(BaseInit->isBaseInitializer() && "Must have base initializer!");
543
544 Address ThisPtr = CGF.LoadCXXThisAddress();
545
546 const auto *BaseClassDecl = BaseInit->getBaseClass()->castAsCXXRecordDecl();
547
548 bool isBaseVirtual = BaseInit->isBaseVirtual();
549
550 // If the initializer for the base (other than the constructor
551 // itself) accesses 'this' in any way, we need to initialize the
552 // vtables.
553 if (BaseInitializerUsesThis(C&: CGF.getContext(), Init: BaseInit->getInit()))
554 CGF.InitializeVTablePointers(ClassDecl);
555
556 // We can pretend to be a complete class because it only matters for
557 // virtual bases, and we only do virtual bases for complete ctors.
558 Address V = CGF.GetAddressOfDirectBaseInCompleteClass(
559 This: ThisPtr, Derived: ClassDecl, Base: BaseClassDecl, BaseIsVirtual: isBaseVirtual);
560 AggValueSlot AggSlot = AggValueSlot::forAddr(
561 addr: V, quals: Qualifiers(), isDestructed: AggValueSlot::IsDestructed,
562 needsGC: AggValueSlot::DoesNotNeedGCBarriers, isAliased: AggValueSlot::IsNotAliased,
563 mayOverlap: CGF.getOverlapForBaseInit(RD: ClassDecl, BaseRD: BaseClassDecl, IsVirtual: isBaseVirtual));
564
565 CGF.EmitAggExpr(E: BaseInit->getInit(), AS: AggSlot);
566
567 if (CGF.CGM.getLangOpts().Exceptions &&
568 !BaseClassDecl->hasTrivialDestructor())
569 CGF.EHStack.pushCleanup<CallBaseDtor>(Kind: EHCleanup, A: BaseClassDecl,
570 A: isBaseVirtual);
571}
572
573static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF,
574 CXXCtorInitializer *MemberInit,
575 LValue &LHS) {
576 FieldDecl *Field = MemberInit->getAnyMember();
577 if (MemberInit->isIndirectMemberInitializer()) {
578 // If we are initializing an anonymous union field, drill down to the field.
579 IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember();
580 for (const auto *I : IndirectField->chain())
581 LHS = CGF.EmitLValueForFieldInitialization(Base: LHS, Field: cast<FieldDecl>(Val: I));
582 } else {
583 LHS = CGF.EmitLValueForFieldInitialization(Base: LHS, Field);
584 }
585}
586
587static void EmitMemberInitializer(CodeGenFunction &CGF,
588 const CXXRecordDecl *ClassDecl,
589 CXXCtorInitializer *MemberInit,
590 const CXXConstructorDecl *Constructor,
591 FunctionArgList &Args) {
592 ApplyAtomGroup Grp(CGF.getDebugInfo());
593 ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation());
594 assert(MemberInit->isAnyMemberInitializer() &&
595 "Must have member initializer!");
596 assert(MemberInit->getInit() && "Must have initializer!");
597
598 // non-static data member initializers.
599 FieldDecl *Field = MemberInit->getAnyMember();
600 QualType FieldType = Field->getType();
601
602 llvm::Value *ThisPtr = CGF.LoadCXXThis();
603 CanQualType RecordTy = CGF.getContext().getCanonicalTagType(TD: ClassDecl);
604 LValue LHS;
605
606 // If a base constructor is being emitted, create an LValue that has the
607 // non-virtual alignment.
608 if (CGF.CurGD.getCtorType() == Ctor_Base)
609 LHS = CGF.MakeNaturalAlignPointeeAddrLValue(V: ThisPtr, T: RecordTy);
610 else
611 LHS = CGF.MakeNaturalAlignAddrLValue(V: ThisPtr, T: RecordTy);
612
613 EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS);
614
615 // Special case: if we are in a copy or move constructor, and we are copying
616 // an array of PODs or classes with trivial copy constructors, ignore the
617 // AST and perform the copy we know is equivalent.
618 // FIXME: This is hacky at best... if we had a bit more explicit information
619 // in the AST, we could generalize it more easily.
620 const ConstantArrayType *Array =
621 CGF.getContext().getAsConstantArrayType(T: FieldType);
622 if (Array && Constructor->isDefaulted() &&
623 Constructor->isCopyOrMoveConstructor()) {
624 QualType BaseElementTy = CGF.getContext().getBaseElementType(VAT: Array);
625 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Val: MemberInit->getInit());
626 if (BaseElementTy.isPODType(Context: CGF.getContext()) ||
627 (CE && CE->getConstructor()->isMemcpyEquivalentSpecialMember(
628 Ctx: CGF.getContext()))) {
629 unsigned SrcArgIndex =
630 CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args);
631 llvm::Value *SrcPtr =
632 CGF.Builder.CreateLoad(Addr: CGF.GetAddrOfLocalVar(VD: Args[SrcArgIndex]));
633 LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(V: SrcPtr, T: RecordTy);
634 LValue Src = CGF.EmitLValueForFieldInitialization(Base: ThisRHSLV, Field);
635
636 // Copy the aggregate.
637 CGF.EmitAggregateCopy(Dest: LHS, Src, EltTy: FieldType,
638 MayOverlap: CGF.getOverlapForFieldInit(FD: Field),
639 isVolatile: LHS.isVolatileQualified());
640 // Ensure that we destroy the objects if an exception is thrown later in
641 // the constructor.
642 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
643 if (CGF.needsEHCleanup(kind: dtorKind))
644 CGF.pushEHDestroy(dtorKind, addr: LHS.getAddress(), type: FieldType);
645 return;
646 }
647 }
648
649 CGF.EmitInitializerForField(Field, LHS, Init: MemberInit->getInit());
650}
651
652void CodeGenFunction::EmitInitializerForField(FieldDecl *Field, LValue LHS,
653 Expr *Init) {
654 QualType FieldType = Field->getType();
655 switch (getEvaluationKind(T: FieldType)) {
656 case TEK_Scalar:
657 if (LHS.isSimple()) {
658 EmitExprAsInit(init: Init, D: Field, lvalue: LHS, capturedByInit: false);
659 } else {
660 RValue RHS = RValue::get(V: EmitScalarExpr(E: Init));
661 EmitStoreThroughLValue(Src: RHS, Dst: LHS);
662 }
663 break;
664 case TEK_Complex:
665 EmitComplexExprIntoLValue(E: Init, dest: LHS, /*isInit*/ true);
666 break;
667 case TEK_Aggregate: {
668 AggValueSlot Slot = AggValueSlot::forLValue(
669 LV: LHS, isDestructed: AggValueSlot::IsDestructed, needsGC: AggValueSlot::DoesNotNeedGCBarriers,
670 isAliased: AggValueSlot::IsNotAliased, mayOverlap: getOverlapForFieldInit(FD: Field),
671 isZeroed: AggValueSlot::IsNotZeroed,
672 // Checks are made by the code that calls constructor.
673 isChecked: AggValueSlot::IsSanitizerChecked);
674 EmitAggExpr(E: Init, AS: Slot);
675 break;
676 }
677 }
678
679 // Ensure that we destroy this object if an exception is thrown
680 // later in the constructor.
681 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
682 if (needsEHCleanup(kind: dtorKind))
683 pushEHDestroy(dtorKind, addr: LHS.getAddress(), type: FieldType);
684}
685
686/// Checks whether the given constructor is a valid subject for the
687/// complete-to-base constructor delegation optimization, i.e.
688/// emitting the complete constructor as a simple call to the base
689/// constructor.
690bool CodeGenFunction::IsConstructorDelegationValid(
691 const CXXConstructorDecl *Ctor) {
692
693 // Currently we disable the optimization for classes with virtual
694 // bases because (1) the addresses of parameter variables need to be
695 // consistent across all initializers but (2) the delegate function
696 // call necessarily creates a second copy of the parameter variable.
697 //
698 // The limiting example (purely theoretical AFAIK):
699 // struct A { A(int &c) { c++; } };
700 // struct B : virtual A {
701 // B(int count) : A(count) { printf("%d\n", count); }
702 // };
703 // ...although even this example could in principle be emitted as a
704 // delegation since the address of the parameter doesn't escape.
705 if (Ctor->getParent()->getNumVBases()) {
706 // TODO: white-list trivial vbase initializers. This case wouldn't
707 // be subject to the restrictions below.
708
709 // TODO: white-list cases where:
710 // - there are no non-reference parameters to the constructor
711 // - the initializers don't access any non-reference parameters
712 // - the initializers don't take the address of non-reference
713 // parameters
714 // - etc.
715 // If we ever add any of the above cases, remember that:
716 // - function-try-blocks will always exclude this optimization
717 // - we need to perform the constructor prologue and cleanup in
718 // EmitConstructorBody.
719
720 return false;
721 }
722
723 // We also disable the optimization for variadic functions because
724 // it's impossible to "re-pass" varargs.
725 if (Ctor->getType()->castAs<FunctionProtoType>()->isVariadic())
726 return false;
727
728 // FIXME: Decide if we can do a delegation of a delegating constructor.
729 if (Ctor->isDelegatingConstructor())
730 return false;
731
732 return true;
733}
734
735// Emit code in ctor (Prologue==true) or dtor (Prologue==false)
736// to poison the extra field paddings inserted under
737// -fsanitize-address-field-padding=1|2.
738void CodeGenFunction::EmitAsanPrologueOrEpilogue(bool Prologue) {
739 ASTContext &Context = getContext();
740 const CXXRecordDecl *ClassDecl =
741 Prologue ? cast<CXXConstructorDecl>(Val: CurGD.getDecl())->getParent()
742 : cast<CXXDestructorDecl>(Val: CurGD.getDecl())->getParent();
743 if (!ClassDecl->mayInsertExtraPadding())
744 return;
745
746 struct SizeAndOffset {
747 uint64_t Size;
748 uint64_t Offset;
749 };
750
751 unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits();
752 const ASTRecordLayout &Info = Context.getASTRecordLayout(D: ClassDecl);
753
754 // Populate sizes and offsets of fields.
755 SmallVector<SizeAndOffset, 16> SSV(Info.getFieldCount());
756 for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i)
757 SSV[i].Offset =
758 Context.toCharUnitsFromBits(BitSize: Info.getFieldOffset(FieldNo: i)).getQuantity();
759
760 size_t NumFields = 0;
761 for (const auto *Field : ClassDecl->fields()) {
762 const FieldDecl *D = Field;
763 auto FieldInfo = Context.getTypeInfoInChars(T: D->getType());
764 CharUnits FieldSize = FieldInfo.Width;
765 assert(NumFields < SSV.size());
766 SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity();
767 NumFields++;
768 }
769 assert(NumFields == SSV.size());
770 if (SSV.size() <= 1)
771 return;
772
773 // We will insert calls to __asan_* run-time functions.
774 // LLVM AddressSanitizer pass may decide to inline them later.
775 llvm::Type *Args[2] = {IntPtrTy, IntPtrTy};
776 llvm::FunctionType *FTy = llvm::FunctionType::get(Result: CGM.VoidTy, Params: Args, isVarArg: false);
777 llvm::FunctionCallee F = CGM.CreateRuntimeFunction(
778 Ty: FTy, Name: Prologue ? "__asan_poison_intra_object_redzone"
779 : "__asan_unpoison_intra_object_redzone");
780
781 llvm::Value *ThisPtr = LoadCXXThis();
782 ThisPtr = Builder.CreatePtrToInt(V: ThisPtr, DestTy: IntPtrTy);
783 uint64_t TypeSize = Info.getNonVirtualSize().getQuantity();
784 // For each field check if it has sufficient padding,
785 // if so (un)poison it with a call.
786 for (size_t i = 0; i < SSV.size(); i++) {
787 uint64_t AsanAlignment = 8;
788 uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset;
789 uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size;
790 uint64_t EndOffset = SSV[i].Offset + SSV[i].Size;
791 if (PoisonSize < AsanAlignment || !SSV[i].Size ||
792 (NextField % AsanAlignment) != 0)
793 continue;
794 Builder.CreateCall(
795 Callee: F, Args: {Builder.CreateAdd(LHS: ThisPtr, RHS: Builder.getIntN(N: PtrSize, C: EndOffset)),
796 Builder.getIntN(N: PtrSize, C: PoisonSize)});
797 }
798}
799
800/// EmitConstructorBody - Emits the body of the current constructor.
801void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) {
802 EmitAsanPrologueOrEpilogue(Prologue: true);
803 const CXXConstructorDecl *Ctor = cast<CXXConstructorDecl>(Val: CurGD.getDecl());
804 CXXCtorType CtorType = CurGD.getCtorType();
805
806 assert((CGM.getTarget().getCXXABI().hasConstructorVariants() ||
807 CtorType == Ctor_Complete) &&
808 "can only generate complete ctor for this ABI");
809
810 // Before we go any further, try the complete->base constructor
811 // delegation optimization.
812 if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) &&
813 CGM.getTarget().getCXXABI().hasConstructorVariants()) {
814 EmitDelegateCXXConstructorCall(Ctor, CtorType: Ctor_Base, Args, Loc: Ctor->getEndLoc());
815 return;
816 }
817
818 const FunctionDecl *Definition = nullptr;
819 Stmt *Body = Ctor->getBody(Definition);
820 assert(Definition == Ctor && "emitting wrong constructor body");
821
822 // Enter the function-try-block before the constructor prologue if
823 // applicable.
824 bool IsTryBody = isa_and_nonnull<CXXTryStmt>(Val: Body);
825 if (IsTryBody)
826 EnterCXXTryStmt(S: *cast<CXXTryStmt>(Val: Body), IsFnTryBlock: true);
827
828 incrementProfileCounter(S: Body);
829 maybeCreateMCDCCondBitmap();
830
831 RunCleanupsScope RunCleanups(*this);
832
833 // TODO: in restricted cases, we can emit the vbase initializers of
834 // a complete ctor and then delegate to the base ctor.
835
836 // Emit the constructor prologue, i.e. the base and member
837 // initializers.
838 EmitCtorPrologue(CD: Ctor, Type: CtorType, Args);
839
840 // Emit the body of the statement.
841 if (IsTryBody)
842 EmitStmt(S: cast<CXXTryStmt>(Val: Body)->getTryBlock());
843 else if (Body)
844 EmitStmt(S: Body);
845
846 // Emit any cleanup blocks associated with the member or base
847 // initializers, which includes (along the exceptional path) the
848 // destructors for those members and bases that were fully
849 // constructed.
850 RunCleanups.ForceCleanup();
851
852 if (IsTryBody)
853 ExitCXXTryStmt(S: *cast<CXXTryStmt>(Val: Body), IsFnTryBlock: true);
854}
855
856namespace {
857/// RAII object to indicate that codegen is copying the value representation
858/// instead of the object representation. Useful when copying a struct or
859/// class which has uninitialized members and we're only performing
860/// lvalue-to-rvalue conversion on the object but not its members.
861class CopyingValueRepresentation {
862public:
863 explicit CopyingValueRepresentation(CodeGenFunction &CGF)
864 : CGF(CGF), OldSanOpts(CGF.SanOpts) {
865 CGF.SanOpts.set(K: SanitizerKind::Bool, Value: false);
866 CGF.SanOpts.set(K: SanitizerKind::Enum, Value: false);
867 }
868 ~CopyingValueRepresentation() { CGF.SanOpts = OldSanOpts; }
869
870private:
871 CodeGenFunction &CGF;
872 SanitizerSet OldSanOpts;
873};
874} // end anonymous namespace
875
876namespace {
877class FieldMemcpyizer {
878public:
879 FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl,
880 const VarDecl *SrcRec)
881 : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec),
882 RecLayout(CGF.getContext().getASTRecordLayout(D: ClassDecl)),
883 FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0),
884 LastFieldOffset(0), LastAddedFieldIndex(0) {}
885
886 bool isMemcpyableField(FieldDecl *F) const {
887 // Never memcpy fields when we are adding poisoned paddings.
888 if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding)
889 return false;
890 Qualifiers Qual = F->getType().getQualifiers();
891 if (Qual.hasVolatile() || Qual.hasObjCLifetime())
892 return false;
893 if (PointerAuthQualifier Q = F->getType().getPointerAuth();
894 Q && Q.isAddressDiscriminated())
895 return false;
896 // Non-trivially-copyable fields with pointer field protection need to be
897 // copied one by one.
898 if (!CGF.getContext().arePFPFieldsTriviallyCopyable(RD: ClassDecl) &&
899 CGF.getContext().isPFPField(Field: F))
900 return false;
901 return true;
902 }
903
904 void addMemcpyableField(FieldDecl *F) {
905 if (isEmptyFieldForLayout(Context: CGF.getContext(), FD: F))
906 return;
907 if (!FirstField)
908 addInitialField(F);
909 else
910 addNextField(F);
911 }
912
913 CharUnits getMemcpySize(uint64_t FirstByteOffset) const {
914 ASTContext &Ctx = CGF.getContext();
915 unsigned LastFieldSize =
916 LastField->isBitField()
917 ? LastField->getBitWidthValue()
918 : Ctx.toBits(
919 CharSize: Ctx.getTypeInfoDataSizeInChars(T: LastField->getType()).Width);
920 uint64_t MemcpySizeBits = LastFieldOffset + LastFieldSize -
921 FirstByteOffset + Ctx.getCharWidth() - 1;
922 CharUnits MemcpySize = Ctx.toCharUnitsFromBits(BitSize: MemcpySizeBits);
923 return MemcpySize;
924 }
925
926 void emitMemcpy() {
927 // Give the subclass a chance to bail out if it feels the memcpy isn't
928 // worth it (e.g. Hasn't aggregated enough data).
929 if (!FirstField) {
930 return;
931 }
932
933 uint64_t FirstByteOffset;
934 if (FirstField->isBitField()) {
935 const CGRecordLayout &RL =
936 CGF.getTypes().getCGRecordLayout(FirstField->getParent());
937 const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FD: FirstField);
938 // FirstFieldOffset is not appropriate for bitfields,
939 // we need to use the storage offset instead.
940 FirstByteOffset = CGF.getContext().toBits(CharSize: BFInfo.StorageOffset);
941 } else {
942 FirstByteOffset = FirstFieldOffset;
943 }
944
945 CharUnits MemcpySize = getMemcpySize(FirstByteOffset);
946 CanQualType RecordTy = CGF.getContext().getCanonicalTagType(TD: ClassDecl);
947 Address ThisPtr = CGF.LoadCXXThisAddress();
948 LValue DestLV = CGF.MakeAddrLValue(Addr: ThisPtr, T: RecordTy);
949 LValue Dest = CGF.EmitLValueForFieldInitialization(Base: DestLV, Field: FirstField);
950 llvm::Value *SrcPtr = CGF.Builder.CreateLoad(Addr: CGF.GetAddrOfLocalVar(VD: SrcRec));
951 LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(V: SrcPtr, T: RecordTy);
952 LValue Src = CGF.EmitLValueForFieldInitialization(Base: SrcLV, Field: FirstField);
953
954 emitMemcpyIR(DestPtr: Dest.isBitField() ? Dest.getBitFieldAddress()
955 : Dest.getAddress(),
956 SrcPtr: Src.isBitField() ? Src.getBitFieldAddress() : Src.getAddress(),
957 Size: MemcpySize);
958 reset();
959 }
960
961 void reset() { FirstField = nullptr; }
962
963protected:
964 CodeGenFunction &CGF;
965 const CXXRecordDecl *ClassDecl;
966
967private:
968 void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) {
969 DestPtr = DestPtr.withElementType(ElemTy: CGF.Int8Ty);
970 SrcPtr = SrcPtr.withElementType(ElemTy: CGF.Int8Ty);
971 auto *I = CGF.Builder.CreateMemCpy(Dest: DestPtr, Src: SrcPtr, Size: Size.getQuantity());
972 CGF.addInstToCurrentSourceAtom(KeyInstruction: I, Backup: nullptr);
973 }
974
975 void addInitialField(FieldDecl *F) {
976 FirstField = F;
977 LastField = F;
978 FirstFieldOffset = RecLayout.getFieldOffset(FieldNo: F->getFieldIndex());
979 LastFieldOffset = FirstFieldOffset;
980 LastAddedFieldIndex = F->getFieldIndex();
981 }
982
983 void addNextField(FieldDecl *F) {
984 // For the most part, the following invariant will hold:
985 // F->getFieldIndex() == LastAddedFieldIndex + 1
986 // The one exception is that Sema won't add a copy-initializer for an
987 // unnamed bitfield, which will show up here as a gap in the sequence.
988 assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 &&
989 "Cannot aggregate fields out of order.");
990 LastAddedFieldIndex = F->getFieldIndex();
991
992 // The 'first' and 'last' fields are chosen by offset, rather than field
993 // index. This allows the code to support bitfields, as well as regular
994 // fields.
995 uint64_t FOffset = RecLayout.getFieldOffset(FieldNo: F->getFieldIndex());
996 if (FOffset < FirstFieldOffset) {
997 FirstField = F;
998 FirstFieldOffset = FOffset;
999 } else if (FOffset >= LastFieldOffset) {
1000 LastField = F;
1001 LastFieldOffset = FOffset;
1002 }
1003 }
1004
1005 const VarDecl *SrcRec;
1006 const ASTRecordLayout &RecLayout;
1007 FieldDecl *FirstField;
1008 FieldDecl *LastField;
1009 uint64_t FirstFieldOffset, LastFieldOffset;
1010 unsigned LastAddedFieldIndex;
1011};
1012
1013class ConstructorMemcpyizer : public FieldMemcpyizer {
1014private:
1015 /// Get source argument for copy constructor. Returns null if not a copy
1016 /// constructor.
1017 static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF,
1018 const CXXConstructorDecl *CD,
1019 FunctionArgList &Args) {
1020 if (CD->isCopyOrMoveConstructor() && CD->isDefaulted())
1021 return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)];
1022 return nullptr;
1023 }
1024
1025 // Returns true if a CXXCtorInitializer represents a member initialization
1026 // that can be rolled into a memcpy.
1027 bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const {
1028 if (!MemcpyableCtor)
1029 return false;
1030 FieldDecl *Field = MemberInit->getMember();
1031 assert(Field && "No field for member init.");
1032 QualType FieldType = Field->getType();
1033 CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Val: MemberInit->getInit());
1034
1035 // Bail out on non-memcpyable, not-trivially-copyable members.
1036 if (!(CE && CE->getConstructor()->isMemcpyEquivalentSpecialMember(
1037 Ctx: CGF.getContext())) &&
1038 !(FieldType.isTriviallyCopyableType(Context: CGF.getContext()) ||
1039 FieldType->isReferenceType()))
1040 return false;
1041
1042 // Bail out on volatile fields.
1043 if (!isMemcpyableField(F: Field))
1044 return false;
1045
1046 // Otherwise we're good.
1047 return true;
1048 }
1049
1050public:
1051 ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD,
1052 FunctionArgList &Args)
1053 : FieldMemcpyizer(CGF, CD->getParent(),
1054 getTrivialCopySource(CGF, CD, Args)),
1055 ConstructorDecl(CD),
1056 MemcpyableCtor(CD->isDefaulted() && CD->isCopyOrMoveConstructor() &&
1057 CGF.getLangOpts().getGC() == LangOptions::NonGC),
1058 Args(Args) {}
1059
1060 void addMemberInitializer(CXXCtorInitializer *MemberInit) {
1061 if (isMemberInitMemcpyable(MemberInit)) {
1062 AggregatedInits.push_back(Elt: MemberInit);
1063 addMemcpyableField(F: MemberInit->getMember());
1064 } else {
1065 emitAggregatedInits();
1066 EmitMemberInitializer(CGF, ClassDecl: ConstructorDecl->getParent(), MemberInit,
1067 Constructor: ConstructorDecl, Args);
1068 }
1069 }
1070
1071 void emitAggregatedInits() {
1072 if (AggregatedInits.size() <= 1) {
1073 // This memcpy is too small to be worthwhile. Fall back on default
1074 // codegen.
1075 if (!AggregatedInits.empty()) {
1076 CopyingValueRepresentation CVR(CGF);
1077 EmitMemberInitializer(CGF, ClassDecl: ConstructorDecl->getParent(),
1078 MemberInit: AggregatedInits[0], Constructor: ConstructorDecl, Args);
1079 AggregatedInits.clear();
1080 }
1081 reset();
1082 return;
1083 }
1084
1085 pushEHDestructors();
1086 ApplyAtomGroup Grp(CGF.getDebugInfo());
1087 emitMemcpy();
1088 AggregatedInits.clear();
1089 }
1090
1091 void pushEHDestructors() {
1092 Address ThisPtr = CGF.LoadCXXThisAddress();
1093 CanQualType RecordTy = CGF.getContext().getCanonicalTagType(TD: ClassDecl);
1094 LValue LHS = CGF.MakeAddrLValue(Addr: ThisPtr, T: RecordTy);
1095
1096 for (unsigned i = 0; i < AggregatedInits.size(); ++i) {
1097 CXXCtorInitializer *MemberInit = AggregatedInits[i];
1098 QualType FieldType = MemberInit->getAnyMember()->getType();
1099 QualType::DestructionKind dtorKind = FieldType.isDestructedType();
1100 if (!CGF.needsEHCleanup(kind: dtorKind))
1101 continue;
1102 LValue FieldLHS = LHS;
1103 EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS&: FieldLHS);
1104 CGF.pushEHDestroy(dtorKind, addr: FieldLHS.getAddress(), type: FieldType);
1105 }
1106 }
1107
1108 void finish() { emitAggregatedInits(); }
1109
1110private:
1111 const CXXConstructorDecl *ConstructorDecl;
1112 bool MemcpyableCtor;
1113 FunctionArgList &Args;
1114 SmallVector<CXXCtorInitializer *, 16> AggregatedInits;
1115};
1116
1117class AssignmentMemcpyizer : public FieldMemcpyizer {
1118private:
1119 // Returns the memcpyable field copied by the given statement, if one
1120 // exists. Otherwise returns null.
1121 FieldDecl *getMemcpyableField(Stmt *S) {
1122 if (!AssignmentsMemcpyable)
1123 return nullptr;
1124 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Val: S)) {
1125 // Recognise trivial assignments.
1126 if (BO->getOpcode() != BO_Assign)
1127 return nullptr;
1128 MemberExpr *ME = dyn_cast<MemberExpr>(Val: BO->getLHS());
1129 if (!ME)
1130 return nullptr;
1131 FieldDecl *Field = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
1132 if (!Field || !isMemcpyableField(F: Field))
1133 return nullptr;
1134 Stmt *RHS = BO->getRHS();
1135 if (ImplicitCastExpr *EC = dyn_cast<ImplicitCastExpr>(Val: RHS))
1136 RHS = EC->getSubExpr();
1137 if (!RHS)
1138 return nullptr;
1139 if (MemberExpr *ME2 = dyn_cast<MemberExpr>(Val: RHS)) {
1140 if (ME2->getMemberDecl() == Field)
1141 return Field;
1142 }
1143 return nullptr;
1144 } else if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(Val: S)) {
1145 CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: MCE->getCalleeDecl());
1146 if (!(MD && MD->isMemcpyEquivalentSpecialMember(Ctx: CGF.getContext())))
1147 return nullptr;
1148 MemberExpr *IOA = dyn_cast<MemberExpr>(Val: MCE->getImplicitObjectArgument());
1149 if (!IOA)
1150 return nullptr;
1151 FieldDecl *Field = dyn_cast<FieldDecl>(Val: IOA->getMemberDecl());
1152 if (!Field || !isMemcpyableField(F: Field))
1153 return nullptr;
1154 MemberExpr *Arg0 = dyn_cast<MemberExpr>(Val: MCE->getArg(Arg: 0));
1155 if (!Arg0 || Field != dyn_cast<FieldDecl>(Val: Arg0->getMemberDecl()))
1156 return nullptr;
1157 return Field;
1158 } else if (CallExpr *CE = dyn_cast<CallExpr>(Val: S)) {
1159 FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: CE->getCalleeDecl());
1160 if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy)
1161 return nullptr;
1162 Expr *DstPtr = CE->getArg(Arg: 0);
1163 if (ImplicitCastExpr *DC = dyn_cast<ImplicitCastExpr>(Val: DstPtr))
1164 DstPtr = DC->getSubExpr();
1165 UnaryOperator *DUO = dyn_cast<UnaryOperator>(Val: DstPtr);
1166 if (!DUO || DUO->getOpcode() != UO_AddrOf)
1167 return nullptr;
1168 MemberExpr *ME = dyn_cast<MemberExpr>(Val: DUO->getSubExpr());
1169 if (!ME)
1170 return nullptr;
1171 FieldDecl *Field = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
1172 if (!Field || !isMemcpyableField(F: Field))
1173 return nullptr;
1174 Expr *SrcPtr = CE->getArg(Arg: 1);
1175 if (ImplicitCastExpr *SC = dyn_cast<ImplicitCastExpr>(Val: SrcPtr))
1176 SrcPtr = SC->getSubExpr();
1177 UnaryOperator *SUO = dyn_cast<UnaryOperator>(Val: SrcPtr);
1178 if (!SUO || SUO->getOpcode() != UO_AddrOf)
1179 return nullptr;
1180 MemberExpr *ME2 = dyn_cast<MemberExpr>(Val: SUO->getSubExpr());
1181 if (!ME2 || Field != dyn_cast<FieldDecl>(Val: ME2->getMemberDecl()))
1182 return nullptr;
1183 return Field;
1184 }
1185
1186 return nullptr;
1187 }
1188
1189 bool AssignmentsMemcpyable;
1190 SmallVector<Stmt *, 16> AggregatedStmts;
1191
1192public:
1193 AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD,
1194 FunctionArgList &Args)
1195 : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]),
1196 AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) {
1197 assert(Args.size() == 2);
1198 }
1199
1200 void emitAssignment(Stmt *S) {
1201 FieldDecl *F = getMemcpyableField(S);
1202 if (F) {
1203 addMemcpyableField(F);
1204 AggregatedStmts.push_back(Elt: S);
1205 } else {
1206 emitAggregatedStmts();
1207 CGF.EmitStmt(S);
1208 }
1209 }
1210
1211 void emitAggregatedStmts() {
1212 if (AggregatedStmts.size() <= 1) {
1213 if (!AggregatedStmts.empty()) {
1214 CopyingValueRepresentation CVR(CGF);
1215 CGF.EmitStmt(S: AggregatedStmts[0]);
1216 }
1217 reset();
1218 }
1219
1220 ApplyAtomGroup Grp(CGF.getDebugInfo());
1221 emitMemcpy();
1222 AggregatedStmts.clear();
1223 }
1224
1225 void finish() { emitAggregatedStmts(); }
1226};
1227
1228} // end anonymous namespace
1229
1230static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit) {
1231 const Type *BaseType = BaseInit->getBaseClass();
1232 return BaseType->castAsCXXRecordDecl()->isDynamicClass();
1233}
1234
1235/// EmitCtorPrologue - This routine generates necessary code to initialize
1236/// base classes and non-static data members belonging to this constructor.
1237void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD,
1238 CXXCtorType CtorType,
1239 FunctionArgList &Args) {
1240 if (CD->isDelegatingConstructor())
1241 return EmitDelegatingCXXConstructorCall(Ctor: CD, Args);
1242
1243 const CXXRecordDecl *ClassDecl = CD->getParent();
1244
1245 // Virtual base initializers aren't needed if:
1246 // - This is a base ctor variant
1247 // - There are no vbases
1248 // - The class is abstract, so a complete object of it cannot be constructed
1249 //
1250 // The check for an abstract class is necessary because sema may not have
1251 // marked virtual base destructors referenced.
1252 bool ConstructVBases = CtorType != Ctor_Base &&
1253 ClassDecl->getNumVBases() != 0 &&
1254 !ClassDecl->isAbstract();
1255
1256 // In the Microsoft C++ ABI, there are no constructor variants. Instead, the
1257 // constructor of a class with virtual bases takes an additional parameter to
1258 // conditionally construct the virtual bases. Emit that check here.
1259 llvm::BasicBlock *BaseCtorContinueBB = nullptr;
1260 if (ConstructVBases &&
1261 !CGM.getTarget().getCXXABI().hasConstructorVariants()) {
1262 BaseCtorContinueBB =
1263 CGM.getCXXABI().EmitCtorCompleteObjectHandler(CGF&: *this, RD: ClassDecl);
1264 assert(BaseCtorContinueBB);
1265 }
1266
1267 // Create three separate ranges for the different types of initializers.
1268 auto AllInits = CD->inits();
1269
1270 // Find the boundaries between the three groups.
1271 auto VirtualBaseEnd = std::find_if(
1272 first: AllInits.begin(), last: AllInits.end(), pred: [](const CXXCtorInitializer *Init) {
1273 return !(Init->isBaseInitializer() && Init->isBaseVirtual());
1274 });
1275
1276 auto NonVirtualBaseEnd = std::find_if(first: VirtualBaseEnd, last: AllInits.end(),
1277 pred: [](const CXXCtorInitializer *Init) {
1278 return !Init->isBaseInitializer();
1279 });
1280
1281 // Create the three ranges.
1282 auto VirtualBaseInits = llvm::make_range(x: AllInits.begin(), y: VirtualBaseEnd);
1283 auto NonVirtualBaseInits =
1284 llvm::make_range(x: VirtualBaseEnd, y: NonVirtualBaseEnd);
1285 auto MemberInits = llvm::make_range(x: NonVirtualBaseEnd, y: AllInits.end());
1286
1287 // Process virtual base initializers, if necessary.
1288 if (ConstructVBases) {
1289 for (CXXCtorInitializer *Initializer : VirtualBaseInits) {
1290 SaveAndRestore ThisRAII(CXXThisValue);
1291 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1292 CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1293 isInitializerOfDynamicClass(BaseInit: Initializer))
1294 CXXThisValue = Builder.CreateLaunderInvariantGroup(Ptr: LoadCXXThis());
1295 EmitBaseInitializer(CGF&: *this, ClassDecl, BaseInit: Initializer);
1296 }
1297 }
1298
1299 if (BaseCtorContinueBB) {
1300 // Complete object handler should continue to the remaining initializers.
1301 Builder.CreateBr(Dest: BaseCtorContinueBB);
1302 EmitBlock(BB: BaseCtorContinueBB);
1303 }
1304
1305 // Then, non-virtual base initializers.
1306 for (CXXCtorInitializer *Initializer : NonVirtualBaseInits) {
1307 assert(!Initializer->isBaseVirtual());
1308 SaveAndRestore ThisRAII(CXXThisValue);
1309 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1310 CGM.getCodeGenOpts().OptimizationLevel > 0 &&
1311 isInitializerOfDynamicClass(BaseInit: Initializer))
1312 CXXThisValue = Builder.CreateLaunderInvariantGroup(Ptr: LoadCXXThis());
1313 EmitBaseInitializer(CGF&: *this, ClassDecl, BaseInit: Initializer);
1314 }
1315
1316 InitializeVTablePointers(ClassDecl);
1317
1318 // And finally, initialize class members.
1319 FieldConstructionScope FCS(*this, LoadCXXThisAddress());
1320 ConstructorMemcpyizer CM(*this, CD, Args);
1321 for (CXXCtorInitializer *Member : MemberInits) {
1322 assert(!Member->isBaseInitializer());
1323 assert(Member->isAnyMemberInitializer() &&
1324 "Delegating initializer on non-delegating constructor");
1325 CM.addMemberInitializer(MemberInit: Member);
1326 }
1327
1328 CM.finish();
1329}
1330
1331static bool FieldHasTrivialDestructorBody(ASTContext &Context,
1332 const FieldDecl *Field);
1333
1334static bool
1335HasTrivialDestructorBody(ASTContext &Context,
1336 const CXXRecordDecl *BaseClassDecl,
1337 const CXXRecordDecl *MostDerivedClassDecl) {
1338 // If the destructor is trivial we don't have to check anything else.
1339 if (BaseClassDecl->hasTrivialDestructor())
1340 return true;
1341
1342 if (!BaseClassDecl->getDestructor()->hasTrivialBody())
1343 return false;
1344
1345 // Check fields.
1346 for (const auto *Field : BaseClassDecl->fields())
1347 if (!FieldHasTrivialDestructorBody(Context, Field))
1348 return false;
1349
1350 // Check non-virtual bases.
1351 for (const auto &I : BaseClassDecl->bases()) {
1352 if (I.isVirtual())
1353 continue;
1354
1355 const auto *NonVirtualBase = I.getType()->castAsCXXRecordDecl();
1356 if (!HasTrivialDestructorBody(Context, BaseClassDecl: NonVirtualBase,
1357 MostDerivedClassDecl))
1358 return false;
1359 }
1360
1361 if (BaseClassDecl == MostDerivedClassDecl) {
1362 // Check virtual bases.
1363 for (const auto &I : BaseClassDecl->vbases()) {
1364 const auto *VirtualBase = I.getType()->castAsCXXRecordDecl();
1365 if (!HasTrivialDestructorBody(Context, BaseClassDecl: VirtualBase, MostDerivedClassDecl))
1366 return false;
1367 }
1368 }
1369
1370 return true;
1371}
1372
1373static bool FieldHasTrivialDestructorBody(ASTContext &Context,
1374 const FieldDecl *Field) {
1375 QualType FieldBaseElementType = Context.getBaseElementType(QT: Field->getType());
1376
1377 auto *FieldClassDecl = FieldBaseElementType->getAsCXXRecordDecl();
1378 if (!FieldClassDecl)
1379 return true;
1380
1381 // The destructor for an implicit anonymous union member is never invoked.
1382 if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
1383 return true;
1384
1385 return HasTrivialDestructorBody(Context, BaseClassDecl: FieldClassDecl, MostDerivedClassDecl: FieldClassDecl);
1386}
1387
1388/// CanSkipVTablePointerInitialization - Check whether we need to initialize
1389/// any vtable pointers before calling this destructor.
1390static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF,
1391 const CXXDestructorDecl *Dtor) {
1392 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1393 if (!ClassDecl->isDynamicClass())
1394 return true;
1395
1396 // For a final class, the vtable pointer is known to already point to the
1397 // class's vtable.
1398 if (ClassDecl->isEffectivelyFinal())
1399 return true;
1400
1401 if (!Dtor->hasTrivialBody())
1402 return false;
1403
1404 // Check the fields.
1405 for (const auto *Field : ClassDecl->fields())
1406 if (!FieldHasTrivialDestructorBody(Context&: CGF.getContext(), Field))
1407 return false;
1408
1409 return true;
1410}
1411
1412static void EmitConditionalArrayDtorCall(const CXXDestructorDecl *DD,
1413 CodeGenFunction &CGF,
1414 llvm::Value *ShouldDeleteCondition) {
1415 Address ThisPtr = CGF.LoadCXXThisAddress();
1416 llvm::BasicBlock *ScalarBB = CGF.createBasicBlock(name: "dtor.scalar");
1417 llvm::BasicBlock *callDeleteBB =
1418 CGF.createBasicBlock(name: "dtor.call_delete_after_array_destroy");
1419 llvm::BasicBlock *VectorBB = CGF.createBasicBlock(name: "dtor.vector");
1420 auto *CondTy = cast<llvm::IntegerType>(Val: ShouldDeleteCondition->getType());
1421 llvm::Value *CheckTheBitForArrayDestroy = CGF.Builder.CreateAnd(
1422 LHS: ShouldDeleteCondition, RHS: llvm::ConstantInt::get(Ty: CondTy, V: 2));
1423 llvm::Value *ShouldDestroyArray =
1424 CGF.Builder.CreateIsNull(Arg: CheckTheBitForArrayDestroy);
1425 CGF.Builder.CreateCondBr(Cond: ShouldDestroyArray, True: ScalarBB, False: VectorBB);
1426
1427 CGF.EmitBlock(BB: VectorBB);
1428
1429 llvm::Value *numElements = nullptr;
1430 llvm::Value *allocatedPtr = nullptr;
1431 CharUnits cookieSize;
1432 QualType EltTy = DD->getThisType()->getPointeeType();
1433 CGF.CGM.getCXXABI().ReadArrayCookie(CGF, Ptr: ThisPtr, ElementType: EltTy, NumElements&: numElements,
1434 AllocPtr&: allocatedPtr, CookieSize&: cookieSize);
1435
1436 // Destroy the elements.
1437 QualType::DestructionKind dtorKind = EltTy.isDestructedType();
1438
1439 assert(dtorKind);
1440 assert(numElements && "no element count for a type with a destructor!");
1441
1442 CharUnits elementSize = CGF.getContext().getTypeSizeInChars(T: EltTy);
1443 CharUnits elementAlign =
1444 ThisPtr.getAlignment().alignmentOfArrayElement(elementSize);
1445
1446 llvm::Value *arrayBegin = ThisPtr.emitRawPointer(CGF);
1447 llvm::Value *arrayEnd = CGF.Builder.CreateInBoundsGEP(
1448 Ty: ThisPtr.getElementType(), Ptr: arrayBegin, IdxList: numElements, Name: "delete.end");
1449
1450 // We already checked that the array is not 0-length before entering vector
1451 // deleting dtor.
1452 CGF.emitArrayDestroy(begin: arrayBegin, end: arrayEnd, elementType: EltTy, elementAlign,
1453 destroyer: CGF.getDestroyer(destructionKind: dtorKind),
1454 /*checkZeroLength*/ false, useEHCleanup: CGF.needsEHCleanup(kind: dtorKind));
1455
1456 llvm::BasicBlock *VectorBBCont = CGF.createBasicBlock(name: "dtor.vector.cont");
1457 CGF.EmitBlock(BB: VectorBBCont);
1458
1459 llvm::Value *CheckTheBitForDeleteCall = CGF.Builder.CreateAnd(
1460 LHS: ShouldDeleteCondition, RHS: llvm::ConstantInt::get(Ty: CondTy, V: 1));
1461
1462 llvm::Value *ShouldCallDelete =
1463 CGF.Builder.CreateIsNull(Arg: CheckTheBitForDeleteCall);
1464 CGF.Builder.CreateCondBr(Cond: ShouldCallDelete, True: CGF.ReturnBlock.getBlock(),
1465 False: callDeleteBB);
1466 CGF.EmitBlock(BB: callDeleteBB);
1467 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(Val: CGF.CurCodeDecl);
1468 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1469 if (Dtor->getArrayOperatorDelete()) {
1470 if (!Dtor->getGlobalArrayOperatorDelete()) {
1471 CGF.EmitDeleteCall(DeleteFD: Dtor->getArrayOperatorDelete(), Ptr: allocatedPtr,
1472 DeleteTy: CGF.getContext().getCanonicalTagType(TD: ClassDecl),
1473 NumElements: numElements, CookieSize: cookieSize);
1474 } else {
1475 // If global operator[] is set, the class had its own operator delete[].
1476 // In that case, check the 4th bit. If it is set, we need to call
1477 // ::delete[].
1478 llvm::Value *CheckTheBitForGlobDeleteCall = CGF.Builder.CreateAnd(
1479 LHS: ShouldDeleteCondition, RHS: llvm::ConstantInt::get(Ty: CondTy, V: 4));
1480
1481 llvm::Value *ShouldCallGlobDelete =
1482 CGF.Builder.CreateIsNull(Arg: CheckTheBitForGlobDeleteCall);
1483 llvm::BasicBlock *GlobDelete =
1484 CGF.createBasicBlock(name: "dtor.call_glob_delete_after_array_destroy");
1485 llvm::BasicBlock *ClassDelete =
1486 CGF.createBasicBlock(name: "dtor.call_class_delete_after_array_destroy");
1487 CGF.Builder.CreateCondBr(Cond: ShouldCallGlobDelete, True: ClassDelete, False: GlobDelete);
1488 CGF.EmitBlock(BB: ClassDelete);
1489 CGF.EmitDeleteCall(DeleteFD: Dtor->getArrayOperatorDelete(), Ptr: allocatedPtr,
1490 DeleteTy: CGF.getContext().getCanonicalTagType(TD: ClassDecl),
1491 NumElements: numElements, CookieSize: cookieSize);
1492 CGF.EmitBranchThroughCleanup(Dest: CGF.ReturnBlock);
1493
1494 CGF.EmitBlock(BB: GlobDelete);
1495 CGF.EmitDeleteCall(DeleteFD: Dtor->getGlobalArrayOperatorDelete(), Ptr: allocatedPtr,
1496 DeleteTy: CGF.getContext().getCanonicalTagType(TD: ClassDecl),
1497 NumElements: numElements, CookieSize: cookieSize);
1498 }
1499 } else {
1500 // No operators delete[] were found, so emit a trap.
1501 llvm::CallInst *TrapCall = CGF.EmitTrapCall(IntrID: llvm::Intrinsic::trap);
1502 TrapCall->setDoesNotReturn();
1503 TrapCall->setDoesNotThrow();
1504 CGF.Builder.CreateUnreachable();
1505 CGF.Builder.ClearInsertionPoint();
1506 }
1507
1508 CGF.EmitBranchThroughCleanup(Dest: CGF.ReturnBlock);
1509 CGF.EmitBlock(BB: ScalarBB);
1510}
1511
1512/// EmitDestructorBody - Emits the body of the current destructor.
1513void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) {
1514 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(Val: CurGD.getDecl());
1515 CXXDtorType DtorType = CurGD.getDtorType();
1516
1517 // For an abstract class, non-base destructors are never used (and can't
1518 // be emitted in general, because vbase dtors may not have been validated
1519 // by Sema), but the Itanium ABI doesn't make them optional and Clang may
1520 // in fact emit references to them from other compilations, so emit them
1521 // as functions containing a trap instruction.
1522 if (DtorType != Dtor_Base && Dtor->getParent()->isAbstract()) {
1523 llvm::CallInst *TrapCall = EmitTrapCall(IntrID: llvm::Intrinsic::trap);
1524 TrapCall->setDoesNotReturn();
1525 TrapCall->setDoesNotThrow();
1526 Builder.CreateUnreachable();
1527 Builder.ClearInsertionPoint();
1528 return;
1529 }
1530
1531 Stmt *Body = Dtor->getBody();
1532 if (Body) {
1533 incrementProfileCounter(S: Body);
1534 maybeCreateMCDCCondBitmap();
1535 }
1536
1537 // The call to operator delete in a deleting destructor happens
1538 // outside of the function-try-block, which means it's always
1539 // possible to delegate the destructor body to the complete
1540 // destructor. Do so.
1541 if (DtorType == Dtor_Deleting || DtorType == Dtor_VectorDeleting) {
1542 if (CXXStructorImplicitParamValue && DtorType == Dtor_VectorDeleting)
1543 EmitConditionalArrayDtorCall(DD: Dtor, CGF&: *this, ShouldDeleteCondition: CXXStructorImplicitParamValue);
1544 RunCleanupsScope DtorEpilogue(*this);
1545 EnterDtorCleanups(Dtor, Type: Dtor_Deleting);
1546 if (HaveInsertPoint()) {
1547 QualType ThisTy = Dtor->getFunctionObjectParameterType();
1548 EmitCXXDestructorCall(D: Dtor, Type: Dtor_Complete, /*ForVirtualBase=*/false,
1549 /*Delegating=*/false, This: LoadCXXThisAddress(), ThisTy);
1550 }
1551 return;
1552 }
1553
1554 // If the body is a function-try-block, enter the try before
1555 // anything else.
1556 bool isTryBody = isa_and_nonnull<CXXTryStmt>(Val: Body);
1557 if (isTryBody)
1558 EnterCXXTryStmt(S: *cast<CXXTryStmt>(Val: Body), IsFnTryBlock: true);
1559 EmitAsanPrologueOrEpilogue(Prologue: false);
1560
1561 // Enter the epilogue cleanups.
1562 RunCleanupsScope DtorEpilogue(*this);
1563
1564 // If this is the complete variant, just invoke the base variant;
1565 // the epilogue will destruct the virtual bases. But we can't do
1566 // this optimization if the body is a function-try-block, because
1567 // we'd introduce *two* handler blocks. In the Microsoft ABI, we
1568 // always delegate because we might not have a definition in this TU.
1569 switch (DtorType) {
1570 case Dtor_Unified:
1571 llvm_unreachable("not expecting a unified dtor");
1572 case Dtor_Comdat:
1573 llvm_unreachable("not expecting a COMDAT");
1574 case Dtor_Deleting:
1575 llvm_unreachable("already handled deleting case");
1576 case Dtor_VectorDeleting:
1577 llvm_unreachable("already handled vector deleting case");
1578
1579 case Dtor_Complete:
1580 assert((Body || getTarget().getCXXABI().isMicrosoft()) &&
1581 "can't emit a dtor without a body for non-Microsoft ABIs");
1582
1583 // Enter the cleanup scopes for virtual bases.
1584 EnterDtorCleanups(Dtor, Type: Dtor_Complete);
1585
1586 if (!isTryBody) {
1587 QualType ThisTy = Dtor->getFunctionObjectParameterType();
1588 EmitCXXDestructorCall(D: Dtor, Type: Dtor_Base, /*ForVirtualBase=*/false,
1589 /*Delegating=*/false, This: LoadCXXThisAddress(), ThisTy);
1590 break;
1591 }
1592
1593 // Fallthrough: act like we're in the base variant.
1594 [[fallthrough]];
1595
1596 case Dtor_Base:
1597 assert(Body);
1598
1599 // Enter the cleanup scopes for fields and non-virtual bases.
1600 EnterDtorCleanups(Dtor, Type: Dtor_Base);
1601
1602 // Initialize the vtable pointers before entering the body.
1603 if (!CanSkipVTablePointerInitialization(CGF&: *this, Dtor)) {
1604 // Insert the llvm.launder.invariant.group intrinsic before initializing
1605 // the vptrs to cancel any previous assumptions we might have made.
1606 if (CGM.getCodeGenOpts().StrictVTablePointers &&
1607 CGM.getCodeGenOpts().OptimizationLevel > 0)
1608 CXXThisValue = Builder.CreateLaunderInvariantGroup(Ptr: LoadCXXThis());
1609 InitializeVTablePointers(ClassDecl: Dtor->getParent());
1610 }
1611
1612 if (isTryBody)
1613 EmitStmt(S: cast<CXXTryStmt>(Val: Body)->getTryBlock());
1614 else if (Body)
1615 EmitStmt(S: Body);
1616 else {
1617 assert(Dtor->isImplicit() && "bodyless dtor not implicit");
1618 // nothing to do besides what's in the epilogue
1619 }
1620 // -fapple-kext must inline any call to this dtor into
1621 // the caller's body.
1622 if (getLangOpts().AppleKext)
1623 CurFn->addFnAttr(Kind: llvm::Attribute::AlwaysInline);
1624
1625 break;
1626 }
1627
1628 // Jump out through the epilogue cleanups.
1629 DtorEpilogue.ForceCleanup();
1630
1631 // Exit the try if applicable.
1632 if (isTryBody)
1633 ExitCXXTryStmt(S: *cast<CXXTryStmt>(Val: Body), IsFnTryBlock: true);
1634}
1635
1636void CodeGenFunction::emitImplicitAssignmentOperatorBody(
1637 FunctionArgList &Args) {
1638 const CXXMethodDecl *AssignOp = cast<CXXMethodDecl>(Val: CurGD.getDecl());
1639 const Stmt *RootS = AssignOp->getBody();
1640 assert(isa<CompoundStmt>(RootS) &&
1641 "Body of an implicit assignment operator should be compound stmt.");
1642 const CompoundStmt *RootCS = cast<CompoundStmt>(Val: RootS);
1643
1644 LexicalScope Scope(*this, RootCS->getSourceRange());
1645
1646 incrementProfileCounter(S: RootCS);
1647 maybeCreateMCDCCondBitmap();
1648 AssignmentMemcpyizer AM(*this, AssignOp, Args);
1649 for (auto *I : RootCS->body())
1650 AM.emitAssignment(S: I);
1651
1652 AM.finish();
1653}
1654
1655namespace {
1656llvm::Value *LoadThisForDtorDelete(CodeGenFunction &CGF,
1657 const CXXDestructorDecl *DD) {
1658 if (Expr *ThisArg = DD->getOperatorDeleteThisArg())
1659 return CGF.EmitScalarExpr(E: ThisArg);
1660 return CGF.LoadCXXThis();
1661}
1662
1663/// Call the operator delete associated with the current destructor.
1664struct CallDtorDelete final : EHScopeStack::Cleanup {
1665 CallDtorDelete() {}
1666
1667 void Emit(CodeGenFunction &CGF, Flags flags) override {
1668 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(Val: CGF.CurCodeDecl);
1669 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1670 CGF.EmitDeleteCall(DeleteFD: Dtor->getOperatorDelete(),
1671 Ptr: LoadThisForDtorDelete(CGF, DD: Dtor),
1672 DeleteTy: CGF.getContext().getCanonicalTagType(TD: ClassDecl));
1673 }
1674};
1675
1676// This function implements generation of scalar deleting destructor body for
1677// the case when the destructor also accepts an implicit flag. Right now only
1678// Microsoft ABI requires deleting destructors to accept implicit flags.
1679// The flag indicates whether an operator delete should be called and whether
1680// it should be a class-specific operator delete or a global one.
1681void EmitConditionalDtorDeleteCall(CodeGenFunction &CGF,
1682 llvm::Value *ShouldDeleteCondition,
1683 bool ReturnAfterDelete) {
1684 const CXXDestructorDecl *Dtor = cast<CXXDestructorDecl>(Val: CGF.CurCodeDecl);
1685 const CXXRecordDecl *ClassDecl = Dtor->getParent();
1686 const FunctionDecl *OD = Dtor->getOperatorDelete();
1687 assert(OD->isDestroyingOperatorDelete() == ReturnAfterDelete &&
1688 "unexpected value for ReturnAfterDelete");
1689 auto *CondTy = cast<llvm::IntegerType>(Val: ShouldDeleteCondition->getType());
1690 // MSVC calls global operator delete inside of the dtor body, but clang
1691 // aligned with this behavior only after a particular version. This is not
1692 // ABI-compatible with previous versions.
1693 ASTContext &Context = CGF.getContext();
1694 bool CallGlobDelete = Context.getTargetInfo().callGlobalDeleteInDeletingDtor(
1695 Context.getLangOpts());
1696 if (CallGlobDelete && OD->isDestroyingOperatorDelete()) {
1697 llvm::BasicBlock *CallDtor = CGF.createBasicBlock(name: "dtor.call_dtor");
1698 llvm::BasicBlock *DontCallDtor = CGF.createBasicBlock(name: "dtor.entry_cont");
1699 // Third bit set signals that global operator delete is called. That means
1700 // despite class having destroying operator delete which is responsible
1701 // for calling dtor, we need to call dtor because global operator delete
1702 // won't do that.
1703 llvm::Value *Check3rdBit = CGF.Builder.CreateAnd(
1704 LHS: ShouldDeleteCondition, RHS: llvm::ConstantInt::get(Ty: CondTy, V: 4));
1705 llvm::Value *ShouldCallDtor = CGF.Builder.CreateIsNull(Arg: Check3rdBit);
1706 CGF.Builder.CreateCondBr(Cond: ShouldCallDtor, True: DontCallDtor, False: CallDtor);
1707 CGF.EmitBlock(BB: CallDtor);
1708 QualType ThisTy = Dtor->getFunctionObjectParameterType();
1709 CGF.EmitCXXDestructorCall(D: Dtor, Type: Dtor_Complete, /*ForVirtualBase=*/false,
1710 /*Delegating=*/false, This: CGF.LoadCXXThisAddress(),
1711 ThisTy);
1712 CGF.Builder.CreateBr(Dest: DontCallDtor);
1713 CGF.EmitBlock(BB: DontCallDtor);
1714 }
1715 llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock(name: "dtor.call_delete");
1716 llvm::BasicBlock *continueBB = CGF.createBasicBlock(name: "dtor.continue");
1717 // First bit set signals that operator delete must be called.
1718 llvm::Value *Check1stBit = CGF.Builder.CreateAnd(
1719 LHS: ShouldDeleteCondition, RHS: llvm::ConstantInt::get(Ty: CondTy, V: 1));
1720 llvm::Value *ShouldCallDelete = CGF.Builder.CreateIsNull(Arg: Check1stBit);
1721 CGF.Builder.CreateCondBr(Cond: ShouldCallDelete, True: continueBB, False: callDeleteBB);
1722
1723 CGF.EmitBlock(BB: callDeleteBB);
1724 auto EmitDeleteAndGoToEnd = [&](const FunctionDecl *DeleteOp) {
1725 CGF.EmitDeleteCall(DeleteFD: DeleteOp, Ptr: LoadThisForDtorDelete(CGF, DD: Dtor),
1726 DeleteTy: Context.getCanonicalTagType(TD: ClassDecl));
1727 if (ReturnAfterDelete)
1728 CGF.EmitBranchThroughCleanup(Dest: CGF.ReturnBlock);
1729 else
1730 CGF.Builder.CreateBr(Dest: continueBB);
1731 };
1732 // If Sema only found a global operator delete previously, the dtor can
1733 // always call it. Otherwise we need to check the third bit and call the
1734 // appropriate operator delete, i.e. global or class-specific.
1735 if (const FunctionDecl *GlobOD = Dtor->getOperatorGlobalDelete();
1736 isa<CXXMethodDecl>(Val: OD) && GlobOD && CallGlobDelete) {
1737 // Third bit set signals that global operator delete is called, i.e.
1738 // ::delete appears on the callsite.
1739 llvm::Value *CheckTheBitForGlobDeleteCall = CGF.Builder.CreateAnd(
1740 LHS: ShouldDeleteCondition, RHS: llvm::ConstantInt::get(Ty: CondTy, V: 4));
1741 llvm::Value *ShouldCallGlobDelete =
1742 CGF.Builder.CreateIsNull(Arg: CheckTheBitForGlobDeleteCall);
1743 llvm::BasicBlock *GlobDelete =
1744 CGF.createBasicBlock(name: "dtor.call_glob_delete");
1745 llvm::BasicBlock *ClassDelete =
1746 CGF.createBasicBlock(name: "dtor.call_class_delete");
1747 CGF.Builder.CreateCondBr(Cond: ShouldCallGlobDelete, True: ClassDelete, False: GlobDelete);
1748 CGF.EmitBlock(BB: GlobDelete);
1749
1750 EmitDeleteAndGoToEnd(GlobOD);
1751 CGF.EmitBlock(BB: ClassDelete);
1752 }
1753 EmitDeleteAndGoToEnd(OD);
1754 CGF.EmitBlock(BB: continueBB);
1755}
1756
1757struct CallDtorDeleteConditional final : EHScopeStack::Cleanup {
1758 llvm::Value *ShouldDeleteCondition;
1759
1760public:
1761 CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition)
1762 : ShouldDeleteCondition(ShouldDeleteCondition) {
1763 assert(ShouldDeleteCondition != nullptr);
1764 }
1765
1766 void Emit(CodeGenFunction &CGF, Flags flags) override {
1767 EmitConditionalDtorDeleteCall(CGF, ShouldDeleteCondition,
1768 /*ReturnAfterDelete*/ false);
1769 }
1770};
1771
1772class DestroyField final : public EHScopeStack::Cleanup {
1773 const FieldDecl *field;
1774 CodeGenFunction::Destroyer *destroyer;
1775 bool useEHCleanupForArray;
1776
1777public:
1778 DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer,
1779 bool useEHCleanupForArray)
1780 : field(field), destroyer(destroyer),
1781 useEHCleanupForArray(useEHCleanupForArray) {}
1782
1783 void Emit(CodeGenFunction &CGF, Flags flags) override {
1784 // Find the address of the field.
1785 Address thisValue = CGF.LoadCXXThisAddress();
1786 CanQualType RecordTy =
1787 CGF.getContext().getCanonicalTagType(TD: field->getParent());
1788 LValue ThisLV = CGF.MakeAddrLValue(Addr: thisValue, T: RecordTy);
1789 LValue LV = CGF.EmitLValueForField(Base: ThisLV, Field: field);
1790 assert(LV.isSimple());
1791
1792 CGF.emitDestroy(addr: LV.getAddress(), type: field->getType(), destroyer,
1793 useEHCleanupForArray: flags.isForNormalCleanup() && useEHCleanupForArray);
1794 }
1795};
1796
1797class DeclAsInlineDebugLocation {
1798 CGDebugInfo *DI;
1799 llvm::MDNode *InlinedAt;
1800 std::optional<ApplyDebugLocation> Location;
1801
1802public:
1803 DeclAsInlineDebugLocation(CodeGenFunction &CGF, const NamedDecl &Decl)
1804 : DI(CGF.getDebugInfo()) {
1805 if (!DI)
1806 return;
1807 InlinedAt = DI->getInlinedAt();
1808 DI->setInlinedAt(CGF.Builder.getCurrentDebugLocation());
1809 Location.emplace(args&: CGF, args: Decl.getLocation());
1810 }
1811
1812 ~DeclAsInlineDebugLocation() {
1813 if (!DI)
1814 return;
1815 Location.reset();
1816 DI->setInlinedAt(InlinedAt);
1817 }
1818};
1819
1820static void EmitSanitizerDtorCallback(
1821 CodeGenFunction &CGF, StringRef Name, llvm::Value *Ptr,
1822 std::optional<CharUnits::QuantityType> PoisonSize = {}) {
1823 CodeGenFunction::SanitizerScope SanScope(&CGF);
1824 // Pass in void pointer and size of region as arguments to runtime
1825 // function
1826 SmallVector<llvm::Value *, 2> Args = {Ptr};
1827 SmallVector<llvm::Type *, 2> ArgTypes = {CGF.VoidPtrTy};
1828
1829 if (PoisonSize.has_value()) {
1830 Args.emplace_back(Args: llvm::ConstantInt::get(Ty: CGF.SizeTy, V: *PoisonSize));
1831 ArgTypes.emplace_back(Args&: CGF.SizeTy);
1832 }
1833
1834 llvm::FunctionType *FnType =
1835 llvm::FunctionType::get(Result: CGF.VoidTy, Params: ArgTypes, isVarArg: false);
1836 llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(Ty: FnType, Name);
1837
1838 CGF.EmitNounwindRuntimeCall(callee: Fn, args: Args);
1839}
1840
1841static void
1842EmitSanitizerDtorFieldsCallback(CodeGenFunction &CGF, llvm::Value *Ptr,
1843 CharUnits::QuantityType PoisonSize) {
1844 EmitSanitizerDtorCallback(CGF, Name: "__sanitizer_dtor_callback_fields", Ptr,
1845 PoisonSize);
1846}
1847
1848/// Poison base class with a trivial destructor.
1849struct SanitizeDtorTrivialBase final : EHScopeStack::Cleanup {
1850 const CXXRecordDecl *BaseClass;
1851 bool BaseIsVirtual;
1852 SanitizeDtorTrivialBase(const CXXRecordDecl *Base, bool BaseIsVirtual)
1853 : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {}
1854
1855 void Emit(CodeGenFunction &CGF, Flags flags) override {
1856 const CXXRecordDecl *DerivedClass =
1857 cast<CXXMethodDecl>(Val: CGF.CurCodeDecl)->getParent();
1858
1859 Address Addr = CGF.GetAddressOfDirectBaseInCompleteClass(
1860 This: CGF.LoadCXXThisAddress(), Derived: DerivedClass, Base: BaseClass, BaseIsVirtual);
1861
1862 const ASTRecordLayout &BaseLayout =
1863 CGF.getContext().getASTRecordLayout(D: BaseClass);
1864 CharUnits BaseSize = BaseLayout.getSize();
1865
1866 if (!BaseSize.isPositive())
1867 return;
1868
1869 // Use the base class declaration location as inline DebugLocation. All
1870 // fields of the class are destroyed.
1871 DeclAsInlineDebugLocation InlineHere(CGF, *BaseClass);
1872 EmitSanitizerDtorFieldsCallback(CGF, Ptr: Addr.emitRawPointer(CGF),
1873 PoisonSize: BaseSize.getQuantity());
1874
1875 // Prevent the current stack frame from disappearing from the stack trace.
1876 CGF.CurFn->addFnAttr(Kind: "disable-tail-calls", Val: "true");
1877 }
1878};
1879
1880class SanitizeDtorFieldRange final : public EHScopeStack::Cleanup {
1881 const CXXDestructorDecl *Dtor;
1882 unsigned StartIndex;
1883 unsigned EndIndex;
1884
1885public:
1886 SanitizeDtorFieldRange(const CXXDestructorDecl *Dtor, unsigned StartIndex,
1887 unsigned EndIndex)
1888 : Dtor(Dtor), StartIndex(StartIndex), EndIndex(EndIndex) {}
1889
1890 // Generate function call for handling object poisoning.
1891 // Disables tail call elimination, to prevent the current stack frame
1892 // from disappearing from the stack trace.
1893 void Emit(CodeGenFunction &CGF, Flags flags) override {
1894 const ASTContext &Context = CGF.getContext();
1895 const ASTRecordLayout &Layout =
1896 Context.getASTRecordLayout(D: Dtor->getParent());
1897
1898 // It's a first trivial field so it should be at the begining of a char,
1899 // still round up start offset just in case.
1900 CharUnits PoisonStart = Context.toCharUnitsFromBits(
1901 BitSize: Layout.getFieldOffset(FieldNo: StartIndex) + Context.getCharWidth() - 1);
1902 llvm::ConstantInt *OffsetSizePtr =
1903 llvm::ConstantInt::get(Ty: CGF.SizeTy, V: PoisonStart.getQuantity());
1904
1905 llvm::Value *OffsetPtr =
1906 CGF.Builder.CreateGEP(Ty: CGF.Int8Ty, Ptr: CGF.LoadCXXThis(), IdxList: OffsetSizePtr);
1907
1908 CharUnits PoisonEnd;
1909 if (EndIndex >= Layout.getFieldCount()) {
1910 PoisonEnd = Layout.getNonVirtualSize();
1911 } else {
1912 PoisonEnd = Context.toCharUnitsFromBits(BitSize: Layout.getFieldOffset(FieldNo: EndIndex));
1913 }
1914 CharUnits PoisonSize = PoisonEnd - PoisonStart;
1915 if (!PoisonSize.isPositive())
1916 return;
1917
1918 // Use the top field declaration location as inline DebugLocation.
1919 DeclAsInlineDebugLocation InlineHere(
1920 CGF, **std::next(x: Dtor->getParent()->field_begin(), n: StartIndex));
1921 EmitSanitizerDtorFieldsCallback(CGF, Ptr: OffsetPtr, PoisonSize: PoisonSize.getQuantity());
1922
1923 // Prevent the current stack frame from disappearing from the stack trace.
1924 CGF.CurFn->addFnAttr(Kind: "disable-tail-calls", Val: "true");
1925 }
1926};
1927
1928class SanitizeDtorVTable final : public EHScopeStack::Cleanup {
1929 const CXXDestructorDecl *Dtor;
1930
1931public:
1932 SanitizeDtorVTable(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {}
1933
1934 // Generate function call for handling vtable pointer poisoning.
1935 void Emit(CodeGenFunction &CGF, Flags flags) override {
1936 assert(Dtor->getParent()->isDynamicClass());
1937 (void)Dtor;
1938 // Poison vtable and vtable ptr if they exist for this class.
1939 llvm::Value *VTablePtr = CGF.LoadCXXThis();
1940
1941 // Pass in void pointer and size of region as arguments to runtime
1942 // function
1943 EmitSanitizerDtorCallback(CGF, Name: "__sanitizer_dtor_callback_vptr", Ptr: VTablePtr);
1944 }
1945};
1946
1947class SanitizeDtorCleanupBuilder {
1948 ASTContext &Context;
1949 EHScopeStack &EHStack;
1950 const CXXDestructorDecl *DD;
1951 std::optional<unsigned> StartIndex;
1952
1953public:
1954 SanitizeDtorCleanupBuilder(ASTContext &Context, EHScopeStack &EHStack,
1955 const CXXDestructorDecl *DD)
1956 : Context(Context), EHStack(EHStack), DD(DD), StartIndex(std::nullopt) {}
1957 void PushCleanupForField(const FieldDecl *Field) {
1958 if (isEmptyFieldForLayout(Context, FD: Field))
1959 return;
1960 unsigned FieldIndex = Field->getFieldIndex();
1961 if (FieldHasTrivialDestructorBody(Context, Field)) {
1962 if (!StartIndex)
1963 StartIndex = FieldIndex;
1964 } else if (StartIndex) {
1965 EHStack.pushCleanup<SanitizeDtorFieldRange>(Kind: NormalAndEHCleanup, A: DD,
1966 A: *StartIndex, A: FieldIndex);
1967 StartIndex = std::nullopt;
1968 }
1969 }
1970 void End() {
1971 if (StartIndex)
1972 EHStack.pushCleanup<SanitizeDtorFieldRange>(Kind: NormalAndEHCleanup, A: DD,
1973 A: *StartIndex, A: -1);
1974 }
1975};
1976} // end anonymous namespace
1977
1978/// Emit all code that comes at the end of class's
1979/// destructor. This is to call destructors on members and base classes
1980/// in reverse order of their construction.
1981///
1982/// For a deleting destructor, this also handles the case where a destroying
1983/// operator delete completely overrides the definition.
1984void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD,
1985 CXXDtorType DtorType) {
1986 assert((!DD->isTrivial() || DD->hasAttr<DLLExportAttr>()) &&
1987 "Should not emit dtor epilogue for non-exported trivial dtor!");
1988
1989 // The deleting-destructor phase just needs to call the appropriate
1990 // operator delete that Sema picked up.
1991 if (DtorType == Dtor_Deleting) {
1992 assert(DD->getOperatorDelete() &&
1993 "operator delete missing - EnterDtorCleanups");
1994 if (CXXStructorImplicitParamValue) {
1995 // If there is an implicit param to the deleting dtor, it's a boolean
1996 // telling whether this is a deleting destructor.
1997 if (DD->getOperatorDelete()->isDestroyingOperatorDelete())
1998 EmitConditionalDtorDeleteCall(CGF&: *this, ShouldDeleteCondition: CXXStructorImplicitParamValue,
1999 /*ReturnAfterDelete*/ true);
2000 else
2001 EHStack.pushCleanup<CallDtorDeleteConditional>(
2002 Kind: NormalAndEHCleanup, A: CXXStructorImplicitParamValue);
2003 } else {
2004 if (DD->getOperatorDelete()->isDestroyingOperatorDelete()) {
2005 const CXXRecordDecl *ClassDecl = DD->getParent();
2006 EmitDeleteCall(DeleteFD: DD->getOperatorDelete(),
2007 Ptr: LoadThisForDtorDelete(CGF&: *this, DD),
2008 DeleteTy: getContext().getCanonicalTagType(TD: ClassDecl));
2009 EmitBranchThroughCleanup(Dest: ReturnBlock);
2010 } else {
2011 EHStack.pushCleanup<CallDtorDelete>(Kind: NormalAndEHCleanup);
2012 }
2013 }
2014 return;
2015 }
2016
2017 const CXXRecordDecl *ClassDecl = DD->getParent();
2018
2019 // Unions have no bases and do not call field destructors.
2020 if (ClassDecl->isUnion())
2021 return;
2022
2023 // The complete-destructor phase just destructs all the virtual bases.
2024 if (DtorType == Dtor_Complete) {
2025 // Poison the vtable pointer such that access after the base
2026 // and member destructors are invoked is invalid.
2027 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
2028 SanOpts.has(K: SanitizerKind::Memory) && ClassDecl->getNumVBases() &&
2029 ClassDecl->isPolymorphic())
2030 EHStack.pushCleanup<SanitizeDtorVTable>(Kind: NormalAndEHCleanup, A: DD);
2031
2032 // We push them in the forward order so that they'll be popped in
2033 // the reverse order.
2034 for (const auto &Base : ClassDecl->vbases()) {
2035 auto *BaseClassDecl = Base.getType()->castAsCXXRecordDecl();
2036 if (BaseClassDecl->hasTrivialDestructor()) {
2037 // Under SanitizeMemoryUseAfterDtor, poison the trivial base class
2038 // memory. For non-trival base classes the same is done in the class
2039 // destructor.
2040 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
2041 SanOpts.has(K: SanitizerKind::Memory) && !BaseClassDecl->isEmpty())
2042 EHStack.pushCleanup<SanitizeDtorTrivialBase>(Kind: NormalAndEHCleanup,
2043 A: BaseClassDecl,
2044 /*BaseIsVirtual*/ A: true);
2045 } else {
2046 EHStack.pushCleanup<CallBaseDtor>(Kind: NormalAndEHCleanup, A: BaseClassDecl,
2047 /*BaseIsVirtual*/ A: true);
2048 }
2049 }
2050
2051 return;
2052 }
2053
2054 assert(DtorType == Dtor_Base);
2055 // Poison the vtable pointer if it has no virtual bases, but inherits
2056 // virtual functions.
2057 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
2058 SanOpts.has(K: SanitizerKind::Memory) && !ClassDecl->getNumVBases() &&
2059 ClassDecl->isPolymorphic())
2060 EHStack.pushCleanup<SanitizeDtorVTable>(Kind: NormalAndEHCleanup, A: DD);
2061
2062 // Destroy non-virtual bases.
2063 for (const auto &Base : ClassDecl->bases()) {
2064 // Ignore virtual bases.
2065 if (Base.isVirtual())
2066 continue;
2067
2068 CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl();
2069
2070 if (BaseClassDecl->hasTrivialDestructor()) {
2071 if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
2072 SanOpts.has(K: SanitizerKind::Memory) && !BaseClassDecl->isEmpty())
2073 EHStack.pushCleanup<SanitizeDtorTrivialBase>(Kind: NormalAndEHCleanup,
2074 A: BaseClassDecl,
2075 /*BaseIsVirtual*/ A: false);
2076 } else {
2077 EHStack.pushCleanup<CallBaseDtor>(Kind: NormalAndEHCleanup, A: BaseClassDecl,
2078 /*BaseIsVirtual*/ A: false);
2079 }
2080 }
2081
2082 // Poison fields such that access after their destructors are
2083 // invoked, and before the base class destructor runs, is invalid.
2084 bool SanitizeFields = CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor &&
2085 SanOpts.has(K: SanitizerKind::Memory);
2086 SanitizeDtorCleanupBuilder SanitizeBuilder(getContext(), EHStack, DD);
2087
2088 // Destroy direct fields.
2089 for (const auto *Field : ClassDecl->fields()) {
2090 if (SanitizeFields)
2091 SanitizeBuilder.PushCleanupForField(Field);
2092
2093 QualType type = Field->getType();
2094 QualType::DestructionKind dtorKind = type.isDestructedType();
2095 if (!dtorKind)
2096 continue;
2097
2098 // Anonymous union members do not have their destructors called.
2099 const RecordType *RT = type->getAsUnionType();
2100 if (RT && RT->getDecl()->isAnonymousStructOrUnion())
2101 continue;
2102
2103 CleanupKind cleanupKind = getCleanupKind(kind: dtorKind);
2104 EHStack.pushCleanup<DestroyField>(
2105 Kind: cleanupKind, A: Field, A: getDestroyer(destructionKind: dtorKind), A: cleanupKind & EHCleanup);
2106 }
2107
2108 if (SanitizeFields)
2109 SanitizeBuilder.End();
2110}
2111
2112/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
2113/// constructor for each of several members of an array.
2114///
2115/// \param ctor the constructor to call for each element
2116/// \param arrayType the type of the array to initialize
2117/// \param arrayBegin an arrayType*
2118/// \param zeroInitialize true if each element should be
2119/// zero-initialized before it is constructed
2120void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor,
2121 const ArrayType *arrayType,
2122 Address arrayBegin,
2123 const CXXConstructExpr *E,
2124 bool NewPointerIsChecked,
2125 bool zeroInitialize) {
2126 QualType elementType;
2127 llvm::Value *numElements =
2128 emitArrayLength(arrayType, baseType&: elementType, addr&: arrayBegin);
2129
2130 EmitCXXAggrConstructorCall(D: ctor, NumElements: numElements, ArrayPtr: arrayBegin, E,
2131 NewPointerIsChecked, ZeroInitialization: zeroInitialize);
2132}
2133
2134/// EmitCXXAggrConstructorCall - Emit a loop to call a particular
2135/// constructor for each of several members of an array.
2136///
2137/// \param ctor the constructor to call for each element
2138/// \param numElements the number of elements in the array;
2139/// may be zero
2140/// \param arrayBase a T*, where T is the type constructed by ctor
2141/// \param zeroInitialize true if each element should be
2142/// zero-initialized before it is constructed
2143void CodeGenFunction::EmitCXXAggrConstructorCall(
2144 const CXXConstructorDecl *ctor, llvm::Value *numElements, Address arrayBase,
2145 const CXXConstructExpr *E, bool NewPointerIsChecked, bool zeroInitialize) {
2146 // It's legal for numElements to be zero. This can happen both
2147 // dynamically, because x can be zero in 'new A[x]', and statically,
2148 // because of GCC extensions that permit zero-length arrays. There
2149 // are probably legitimate places where we could assume that this
2150 // doesn't happen, but it's not clear that it's worth it.
2151 llvm::CondBrInst *zeroCheckBranch = nullptr;
2152
2153 // Optimize for a constant count.
2154 llvm::ConstantInt *constantCount = dyn_cast<llvm::ConstantInt>(Val: numElements);
2155 if (constantCount) {
2156 // Just skip out if the constant count is zero.
2157 if (constantCount->isZero())
2158 return;
2159
2160 // Otherwise, emit the check.
2161 } else {
2162 llvm::BasicBlock *loopBB = createBasicBlock(name: "new.ctorloop");
2163 llvm::Value *iszero = Builder.CreateIsNull(Arg: numElements, Name: "isempty");
2164 zeroCheckBranch = Builder.CreateCondBr(Cond: iszero, True: loopBB, False: loopBB);
2165 EmitBlock(BB: loopBB);
2166 }
2167
2168 // Find the end of the array.
2169 llvm::Type *elementType = arrayBase.getElementType();
2170 llvm::Value *arrayBegin = arrayBase.emitRawPointer(CGF&: *this);
2171 llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(
2172 Ty: elementType, Ptr: arrayBegin, IdxList: numElements, Name: "arrayctor.end");
2173
2174 // Enter the loop, setting up a phi for the current location to initialize.
2175 llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
2176 llvm::BasicBlock *loopBB = createBasicBlock(name: "arrayctor.loop");
2177 EmitBlock(BB: loopBB);
2178 llvm::PHINode *cur =
2179 Builder.CreatePHI(Ty: arrayBegin->getType(), NumReservedValues: 2, Name: "arrayctor.cur");
2180 cur->addIncoming(V: arrayBegin, BB: entryBB);
2181
2182 // Inside the loop body, emit the constructor call on the array element.
2183 if (CGM.shouldEmitConvergenceTokens())
2184 ConvergenceTokenStack.push_back(Elt: emitConvergenceLoopToken(BB: loopBB));
2185
2186 // The alignment of the base, adjusted by the size of a single element,
2187 // provides a conservative estimate of the alignment of every element.
2188 // (This assumes we never start tracking offsetted alignments.)
2189 //
2190 // Note that these are complete objects and so we don't need to
2191 // use the non-virtual size or alignment.
2192 CanQualType type = getContext().getCanonicalTagType(TD: ctor->getParent());
2193 CharUnits eltAlignment = arrayBase.getAlignment().alignmentOfArrayElement(
2194 elementSize: getContext().getTypeSizeInChars(T: type));
2195 Address curAddr = Address(cur, elementType, eltAlignment);
2196
2197 // Zero initialize the storage, if requested.
2198 if (zeroInitialize)
2199 EmitNullInitialization(DestPtr: curAddr, Ty: type);
2200
2201 // C++ [class.temporary]p4:
2202 // There are two contexts in which temporaries are destroyed at a different
2203 // point than the end of the full-expression. The first context is when a
2204 // default constructor is called to initialize an element of an array.
2205 // If the constructor has one or more default arguments, the destruction of
2206 // every temporary created in a default argument expression is sequenced
2207 // before the construction of the next array element, if any.
2208
2209 {
2210 RunCleanupsScope Scope(*this);
2211
2212 // Evaluate the constructor and its arguments in a regular
2213 // partial-destroy cleanup.
2214 if (getLangOpts().Exceptions &&
2215 !ctor->getParent()->hasTrivialDestructor()) {
2216 Destroyer *destroyer = destroyCXXObject;
2217 pushRegularPartialArrayCleanup(arrayBegin, arrayEnd: cur, elementType: type, elementAlignment: eltAlignment,
2218 destroyer: *destroyer);
2219 }
2220 auto currAVS = AggValueSlot::forAddr(
2221 addr: curAddr, quals: type.getQualifiers(), isDestructed: AggValueSlot::IsDestructed,
2222 needsGC: AggValueSlot::DoesNotNeedGCBarriers, isAliased: AggValueSlot::IsNotAliased,
2223 mayOverlap: AggValueSlot::DoesNotOverlap, isZeroed: AggValueSlot::IsNotZeroed,
2224 isChecked: NewPointerIsChecked ? AggValueSlot::IsSanitizerChecked
2225 : AggValueSlot::IsNotSanitizerChecked);
2226 EmitCXXConstructorCall(D: ctor, Type: Ctor_Complete, /*ForVirtualBase=*/false,
2227 /*Delegating=*/false, ThisAVS: currAVS, E);
2228 }
2229
2230 // Go to the next element.
2231 llvm::Value *next = Builder.CreateInBoundsGEP(
2232 Ty: elementType, Ptr: cur, IdxList: llvm::ConstantInt::get(Ty: SizeTy, V: 1), Name: "arrayctor.next");
2233 cur->addIncoming(V: next, BB: Builder.GetInsertBlock());
2234
2235 // Check whether that's the end of the loop.
2236 llvm::Value *done = Builder.CreateICmpEQ(LHS: next, RHS: arrayEnd, Name: "arrayctor.done");
2237 llvm::BasicBlock *contBB = createBasicBlock(name: "arrayctor.cont");
2238 Builder.CreateCondBr(Cond: done, True: contBB, False: loopBB);
2239
2240 // Patch the earlier check to skip over the loop.
2241 if (zeroCheckBranch)
2242 zeroCheckBranch->setSuccessor(idx: 0, NewSucc: contBB);
2243
2244 if (CGM.shouldEmitConvergenceTokens())
2245 ConvergenceTokenStack.pop_back();
2246
2247 EmitBlock(BB: contBB);
2248}
2249
2250void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF, Address addr,
2251 QualType type) {
2252 const CXXDestructorDecl *dtor = type->castAsCXXRecordDecl()->getDestructor();
2253 assert(!dtor->isTrivial());
2254 CGF.EmitCXXDestructorCall(D: dtor, Type: Dtor_Complete, /*for vbase*/ ForVirtualBase: false,
2255 /*Delegating=*/false, This: addr, ThisTy: type);
2256}
2257
2258void CodeGenFunction::EmitCXXConstructorCall(
2259 const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase,
2260 bool Delegating, AggValueSlot ThisAVS, const CXXConstructExpr *E) {
2261 CallArgList Args;
2262 Address This = ThisAVS.getAddress();
2263 LangAS SlotAS = ThisAVS.getQualifiers().getAddressSpace();
2264 LangAS ThisAS = D->getFunctionObjectParameterType().getAddressSpace();
2265 llvm::Value *ThisPtr =
2266 getAsNaturalPointerTo(Addr: This, PointeeType: D->getThisType()->getPointeeType());
2267
2268 if (SlotAS != ThisAS) {
2269 unsigned TargetThisAS = getContext().getTargetAddressSpace(AS: ThisAS);
2270 llvm::Type *NewType =
2271 llvm::PointerType::get(C&: getLLVMContext(), AddressSpace: TargetThisAS);
2272 ThisPtr = performAddrSpaceCast(Src: ThisPtr, DestTy: NewType);
2273 }
2274
2275 // Push the this ptr.
2276 Args.add(rvalue: RValue::get(V: ThisPtr), type: D->getThisType());
2277
2278 // If this is a trivial constructor, emit a memcpy now before we lose
2279 // the alignment information on the argument.
2280 // FIXME: It would be better to preserve alignment information into CallArg.
2281 if (D->isMemcpyEquivalentSpecialMember(Ctx: getContext())) {
2282 assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
2283
2284 const Expr *Arg = E->getArg(Arg: 0);
2285 LValue Src = EmitLValue(E: Arg);
2286 CanQualType DestTy = getContext().getCanonicalTagType(TD: D->getParent());
2287 LValue Dest = MakeAddrLValue(Addr: This, T: DestTy);
2288 EmitAggregateCopyCtor(Dest, Src, MayOverlap: ThisAVS.mayOverlap());
2289 return;
2290 }
2291
2292 // Add the rest of the user-supplied arguments.
2293 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
2294 EvaluationOrder Order = E->isListInitialization()
2295 ? EvaluationOrder::ForceLeftToRight
2296 : EvaluationOrder::Default;
2297 EmitCallArgs(Args, Prototype: FPT, ArgRange: E->arguments(), AC: E->getConstructor(),
2298 /*ParamsToSkip*/ 0, Order);
2299
2300 EmitCXXConstructorCall(D, Type, ForVirtualBase, Delegating, This, Args,
2301 Overlap: ThisAVS.mayOverlap(), Loc: E->getExprLoc(),
2302 NewPointerIsChecked: ThisAVS.isSanitizerChecked());
2303}
2304
2305static bool canEmitDelegateCallArgs(CodeGenFunction &CGF,
2306 const CXXConstructorDecl *Ctor,
2307 CXXCtorType Type, CallArgList &Args) {
2308 // We can't forward a variadic call.
2309 if (Ctor->isVariadic())
2310 return false;
2311
2312 if (CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) {
2313 // If the parameters are callee-cleanup, it's not safe to forward.
2314 for (auto *P : Ctor->parameters())
2315 if (P->needsDestruction(Ctx: CGF.getContext()))
2316 return false;
2317
2318 // Likewise if they're inalloca.
2319 const CGFunctionInfo &Info =
2320 CGF.CGM.getTypes().arrangeCXXConstructorCall(Args, D: Ctor, CtorKind: Type, ExtraPrefixArgs: 0, ExtraSuffixArgs: 0);
2321 if (Info.usesInAlloca())
2322 return false;
2323 }
2324
2325 // Anything else should be OK.
2326 return true;
2327}
2328
2329void CodeGenFunction::EmitCXXConstructorCall(
2330 const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase,
2331 bool Delegating, Address This, CallArgList &Args,
2332 AggValueSlot::Overlap_t Overlap, SourceLocation Loc,
2333 bool NewPointerIsChecked, llvm::CallBase **CallOrInvoke) {
2334 const CXXRecordDecl *ClassDecl = D->getParent();
2335
2336 if (!NewPointerIsChecked)
2337 EmitTypeCheck(TCK: CodeGenFunction::TCK_ConstructorCall, Loc, Addr: This,
2338 Type: getContext().getCanonicalTagType(TD: ClassDecl),
2339 Alignment: CharUnits::Zero());
2340
2341 if (D->isTrivial() && D->isDefaultConstructor()) {
2342 assert(Args.size() == 1 && "trivial default ctor with args");
2343 return;
2344 }
2345
2346 // If this is a trivial constructor, just emit what's needed. If this is a
2347 // union copy constructor, we must emit a memcpy, because the AST does not
2348 // model that copy.
2349 if (D->isMemcpyEquivalentSpecialMember(Ctx: getContext())) {
2350 assert(Args.size() == 2 && "unexpected argcount for trivial ctor");
2351 QualType SrcTy = D->getParamDecl(i: 0)->getType().getNonReferenceType();
2352 Address Src = makeNaturalAddressForPointer(
2353 Ptr: Args[1].getRValue(CGF&: *this).getScalarVal(), T: SrcTy);
2354 LValue SrcLVal = MakeAddrLValue(Addr: Src, T: SrcTy);
2355 CanQualType DestTy = getContext().getCanonicalTagType(TD: ClassDecl);
2356 LValue DestLVal = MakeAddrLValue(Addr: This, T: DestTy);
2357 EmitAggregateCopyCtor(Dest: DestLVal, Src: SrcLVal, MayOverlap: Overlap);
2358 return;
2359 }
2360
2361 bool PassPrototypeArgs = true;
2362 // Check whether we can actually emit the constructor before trying to do so.
2363 if (auto Inherited = D->getInheritedConstructor()) {
2364 PassPrototypeArgs = getTypes().inheritingCtorHasParams(Inherited, Type);
2365 if (PassPrototypeArgs && !canEmitDelegateCallArgs(CGF&: *this, Ctor: D, Type, Args)) {
2366 EmitInlinedInheritingCXXConstructorCall(Ctor: D, CtorType: Type, ForVirtualBase,
2367 Delegating, Args);
2368 return;
2369 }
2370 }
2371
2372 // Insert any ABI-specific implicit constructor arguments.
2373 CGCXXABI::AddedStructorArgCounts ExtraArgs =
2374 CGM.getCXXABI().addImplicitConstructorArgs(CGF&: *this, D, Type, ForVirtualBase,
2375 Delegating, Args);
2376
2377 // Emit the call.
2378 llvm::Constant *CalleePtr = CGM.getAddrOfCXXStructor(GD: GlobalDecl(D, Type));
2379 const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall(
2380 Args, D, CtorKind: Type, ExtraPrefixArgs: ExtraArgs.Prefix, ExtraSuffixArgs: ExtraArgs.Suffix, PassProtoArgs: PassPrototypeArgs);
2381 CGCallee Callee = CGCallee::forDirect(functionPtr: CalleePtr, abstractInfo: GlobalDecl(D, Type));
2382 EmitCall(CallInfo: Info, Callee, ReturnValue: ReturnValueSlot(), Args, CallOrInvoke, IsMustTail: false, Loc);
2383
2384 // Generate vtable assumptions if we're constructing a complete object
2385 // with a vtable. We don't do this for base subobjects for two reasons:
2386 // first, it's incorrect for classes with virtual bases, and second, we're
2387 // about to overwrite the vptrs anyway.
2388 // We also have to make sure if we can refer to vtable:
2389 // - Otherwise we can refer to vtable if it's safe to speculatively emit.
2390 // FIXME: If vtable is used by ctor/dtor, or if vtable is external and we are
2391 // sure that definition of vtable is not hidden,
2392 // then we are always safe to refer to it.
2393 // FIXME: It looks like InstCombine is very inefficient on dealing with
2394 // assumes. Make assumption loads require -fstrict-vtable-pointers
2395 // temporarily.
2396 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2397 ClassDecl->isDynamicClass() && Type != Ctor_Base &&
2398 CGM.getCXXABI().canSpeculativelyEmitVTable(RD: ClassDecl) &&
2399 CGM.getCodeGenOpts().StrictVTablePointers)
2400 EmitVTableAssumptionLoads(ClassDecl, This);
2401}
2402
2403void CodeGenFunction::EmitInheritedCXXConstructorCall(
2404 const CXXConstructorDecl *D, bool ForVirtualBase, Address This,
2405 bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E) {
2406 CallArgList Args;
2407 CallArg ThisArg(RValue::get(V: getAsNaturalPointerTo(
2408 Addr: This, PointeeType: D->getThisType()->getPointeeType())),
2409 D->getThisType());
2410
2411 // Forward the parameters.
2412 if (InheritedFromVBase &&
2413 CGM.getTarget().getCXXABI().hasConstructorVariants()) {
2414 // Nothing to do; this construction is not responsible for constructing
2415 // the base class containing the inherited constructor.
2416 // FIXME: Can we just pass undef's for the remaining arguments if we don't
2417 // have constructor variants?
2418 Args.push_back(Elt: ThisArg);
2419 } else if (!CXXInheritedCtorInitExprArgs.empty()) {
2420 // The inheriting constructor was inlined; just inject its arguments.
2421 assert(CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() &&
2422 "wrong number of parameters for inherited constructor call");
2423 Args = CXXInheritedCtorInitExprArgs;
2424 Args[0] = ThisArg;
2425 } else {
2426 // The inheriting constructor was not inlined. Emit delegating arguments.
2427 Args.push_back(Elt: ThisArg);
2428 const auto *OuterCtor = cast<CXXConstructorDecl>(Val: CurCodeDecl);
2429 assert(OuterCtor->getNumParams() == D->getNumParams());
2430 assert(!OuterCtor->isVariadic() && "should have been inlined");
2431
2432 for (const auto *Param : OuterCtor->parameters()) {
2433 assert(getContext().hasSameUnqualifiedType(
2434 OuterCtor->getParamDecl(Param->getFunctionScopeIndex())->getType(),
2435 Param->getType()));
2436 EmitDelegateCallArg(args&: Args, param: Param, loc: E->getLocation());
2437
2438 // Forward __attribute__(pass_object_size).
2439 if (Param->hasAttr<PassObjectSizeAttr>()) {
2440 auto *POSParam = SizeArguments[Param];
2441 assert(POSParam && "missing pass_object_size value for forwarding");
2442 EmitDelegateCallArg(args&: Args, param: POSParam, loc: E->getLocation());
2443 }
2444 }
2445 }
2446
2447 EmitCXXConstructorCall(D, Type: Ctor_Base, ForVirtualBase, /*Delegating*/ false,
2448 This, Args, Overlap: AggValueSlot::MayOverlap, Loc: E->getLocation(),
2449 /*NewPointerIsChecked*/ true);
2450}
2451
2452void CodeGenFunction::EmitInlinedInheritingCXXConstructorCall(
2453 const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase,
2454 bool Delegating, CallArgList &Args) {
2455 GlobalDecl GD(Ctor, CtorType);
2456 InlinedInheritingConstructorScope Scope(*this, GD);
2457 ApplyInlineDebugLocation DebugScope(*this, GD);
2458 RunCleanupsScope RunCleanups(*this);
2459
2460 // Save the arguments to be passed to the inherited constructor.
2461 CXXInheritedCtorInitExprArgs = Args;
2462
2463 FunctionArgList Params;
2464 QualType RetType = BuildFunctionArgList(GD: CurGD, Args&: Params);
2465 FnRetTy = RetType;
2466
2467 // Insert any ABI-specific implicit constructor arguments.
2468 CGM.getCXXABI().addImplicitConstructorArgs(CGF&: *this, D: Ctor, Type: CtorType,
2469 ForVirtualBase, Delegating, Args);
2470
2471 // Emit a simplified prolog. We only need to emit the implicit params.
2472 assert(Args.size() >= Params.size() && "too few arguments for call");
2473 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
2474 if (I < Params.size() && isa<ImplicitParamDecl>(Val: Params[I])) {
2475 const RValue &RV = Args[I].getRValue(CGF&: *this);
2476 assert(!RV.isComplex() && "complex indirect params not supported");
2477 ParamValue Val = RV.isScalar()
2478 ? ParamValue::forDirect(value: RV.getScalarVal())
2479 : ParamValue::forIndirect(addr: RV.getAggregateAddress());
2480 EmitParmDecl(D: *Params[I], Arg: Val, ArgNo: I + 1);
2481 }
2482 }
2483
2484 // Create a return value slot if the ABI implementation wants one.
2485 // FIXME: This is dumb, we should ask the ABI not to try to set the return
2486 // value instead.
2487 if (!RetType->isVoidType())
2488 ReturnValue = CreateIRTempWithoutCast(T: RetType, Name: "retval.inhctor");
2489
2490 CGM.getCXXABI().EmitInstanceFunctionProlog(CGF&: *this);
2491 CXXThisValue = CXXABIThisValue;
2492
2493 // Directly emit the constructor initializers.
2494 EmitCtorPrologue(CD: Ctor, CtorType, Args&: Params);
2495}
2496
2497void CodeGenFunction::EmitVTableAssumptionLoad(const VPtr &Vptr, Address This) {
2498 llvm::Value *VTableGlobal =
2499 CGM.getCXXABI().getVTableAddressPoint(Base: Vptr.Base, VTableClass: Vptr.VTableClass);
2500 if (!VTableGlobal)
2501 return;
2502
2503 // We can just use the base offset in the complete class.
2504 CharUnits NonVirtualOffset = Vptr.Base.getBaseOffset();
2505
2506 if (!NonVirtualOffset.isZero())
2507 This =
2508 ApplyNonVirtualAndVirtualOffset(CGF&: *this, addr: This, nonVirtualOffset: NonVirtualOffset, virtualOffset: nullptr,
2509 derivedClass: Vptr.VTableClass, nearestVBase: Vptr.NearestVBase);
2510
2511 llvm::Value *VPtrValue =
2512 GetVTablePtr(This, VTableTy: VTableGlobal->getType(), VTableClass: Vptr.VTableClass);
2513 llvm::Value *Cmp =
2514 Builder.CreateICmpEQ(LHS: VPtrValue, RHS: VTableGlobal, Name: "cmp.vtables");
2515 Builder.CreateAssumption(Cond: Cmp);
2516}
2517
2518void CodeGenFunction::EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl,
2519 Address This) {
2520 if (CGM.getCXXABI().doStructorsInitializeVPtrs(VTableClass: ClassDecl))
2521 for (const VPtr &Vptr : getVTablePointers(VTableClass: ClassDecl))
2522 EmitVTableAssumptionLoad(Vptr, This);
2523}
2524
2525void CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(
2526 const CXXConstructorDecl *D, Address This, Address Src,
2527 const CXXConstructExpr *E) {
2528 const FunctionProtoType *FPT = D->getType()->castAs<FunctionProtoType>();
2529
2530 CallArgList Args;
2531
2532 // Push the this ptr.
2533 Args.add(rvalue: RValue::get(V: getAsNaturalPointerTo(Addr: This, PointeeType: D->getThisType())),
2534 type: D->getThisType());
2535
2536 // Push the src ptr.
2537 QualType QT = *(FPT->param_type_begin());
2538 llvm::Type *t = CGM.getTypes().ConvertType(T: QT);
2539 llvm::Value *Val = getAsNaturalPointerTo(Addr: Src, PointeeType: D->getThisType());
2540 llvm::Value *SrcVal = Builder.CreateBitCast(V: Val, DestTy: t);
2541 Args.add(rvalue: RValue::get(V: SrcVal), type: QT);
2542
2543 // Skip over first argument (Src).
2544 EmitCallArgs(Args, Prototype: FPT, ArgRange: drop_begin(RangeOrContainer: E->arguments(), N: 1), AC: E->getConstructor(),
2545 /*ParamsToSkip*/ 1);
2546
2547 EmitCXXConstructorCall(D, Type: Ctor_Complete, /*ForVirtualBase*/ false,
2548 /*Delegating*/ false, This, Args,
2549 Overlap: AggValueSlot::MayOverlap, Loc: E->getExprLoc(),
2550 /*NewPointerIsChecked*/ false);
2551}
2552
2553void CodeGenFunction::EmitDelegateCXXConstructorCall(
2554 const CXXConstructorDecl *Ctor, CXXCtorType CtorType,
2555 const FunctionArgList &Args, SourceLocation Loc) {
2556 CallArgList DelegateArgs;
2557
2558 FunctionArgList::const_iterator I = Args.begin(), E = Args.end();
2559 assert(I != E && "no parameters to constructor");
2560
2561 // this
2562 Address This = LoadCXXThisAddress();
2563 DelegateArgs.add(rvalue: RValue::get(V: getAsNaturalPointerTo(
2564 Addr: This, PointeeType: (*I)->getType()->getPointeeType())),
2565 type: (*I)->getType());
2566 ++I;
2567
2568 // FIXME: The location of the VTT parameter in the parameter list is
2569 // specific to the Itanium ABI and shouldn't be hardcoded here.
2570 if (CGM.getCXXABI().NeedsVTTParameter(GD: CurGD)) {
2571 assert(I != E && "cannot skip vtt parameter, already done with args");
2572 assert((*I)->getType()->isPointerType() &&
2573 "skipping parameter not of vtt type");
2574 ++I;
2575 }
2576
2577 // Explicit arguments.
2578 for (; I != E; ++I) {
2579 const VarDecl *param = *I;
2580 // FIXME: per-argument source location
2581 EmitDelegateCallArg(args&: DelegateArgs, param, loc: Loc);
2582 }
2583
2584 EmitCXXConstructorCall(D: Ctor, Type: CtorType, /*ForVirtualBase=*/false,
2585 /*Delegating=*/true, This, Args&: DelegateArgs,
2586 Overlap: AggValueSlot::MayOverlap, Loc,
2587 /*NewPointerIsChecked=*/true);
2588}
2589
2590namespace {
2591struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup {
2592 const CXXDestructorDecl *Dtor;
2593 Address Addr;
2594 CXXDtorType Type;
2595
2596 CallDelegatingCtorDtor(const CXXDestructorDecl *D, Address Addr,
2597 CXXDtorType Type)
2598 : Dtor(D), Addr(Addr), Type(Type) {}
2599
2600 void Emit(CodeGenFunction &CGF, Flags flags) override {
2601 // We are calling the destructor from within the constructor.
2602 // Therefore, "this" should have the expected type.
2603 QualType ThisTy = Dtor->getFunctionObjectParameterType();
2604 CGF.EmitCXXDestructorCall(D: Dtor, Type, /*ForVirtualBase=*/false,
2605 /*Delegating=*/true, This: Addr, ThisTy);
2606 }
2607};
2608} // end anonymous namespace
2609
2610void CodeGenFunction::EmitDelegatingCXXConstructorCall(
2611 const CXXConstructorDecl *Ctor, const FunctionArgList &Args) {
2612 assert(Ctor->isDelegatingConstructor());
2613
2614 Address ThisPtr = LoadCXXThisAddress();
2615
2616 AggValueSlot AggSlot = AggValueSlot::forAddr(
2617 addr: ThisPtr, quals: Qualifiers(), isDestructed: AggValueSlot::IsDestructed,
2618 needsGC: AggValueSlot::DoesNotNeedGCBarriers, isAliased: AggValueSlot::IsNotAliased,
2619 mayOverlap: AggValueSlot::MayOverlap, isZeroed: AggValueSlot::IsNotZeroed,
2620 // Checks are made by the code that calls constructor.
2621 isChecked: AggValueSlot::IsSanitizerChecked);
2622
2623 EmitAggExpr(E: Ctor->init_begin()[0]->getInit(), AS: AggSlot);
2624
2625 const CXXRecordDecl *ClassDecl = Ctor->getParent();
2626 if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) {
2627 CXXDtorType Type =
2628 CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base;
2629
2630 EHStack.pushCleanup<CallDelegatingCtorDtor>(
2631 Kind: EHCleanup, A: ClassDecl->getDestructor(), A: ThisPtr, A: Type);
2632 }
2633}
2634
2635void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD,
2636 CXXDtorType Type,
2637 bool ForVirtualBase,
2638 bool Delegating, Address This,
2639 QualType ThisTy) {
2640 CGM.getCXXABI().EmitDestructorCall(CGF&: *this, DD, Type, ForVirtualBase,
2641 Delegating, This, ThisTy);
2642}
2643
2644namespace {
2645struct CallLocalDtor final : EHScopeStack::Cleanup {
2646 const CXXDestructorDecl *Dtor;
2647 Address Addr;
2648 QualType Ty;
2649
2650 CallLocalDtor(const CXXDestructorDecl *D, Address Addr, QualType Ty)
2651 : Dtor(D), Addr(Addr), Ty(Ty) {}
2652
2653 void Emit(CodeGenFunction &CGF, Flags flags) override {
2654 CGF.EmitCXXDestructorCall(DD: Dtor, Type: Dtor_Complete,
2655 /*ForVirtualBase=*/false,
2656 /*Delegating=*/false, This: Addr, ThisTy: Ty);
2657 }
2658};
2659} // end anonymous namespace
2660
2661void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D,
2662 QualType T, Address Addr) {
2663 EHStack.pushCleanup<CallLocalDtor>(Kind: NormalAndEHCleanup, A: D, A: Addr, A: T);
2664}
2665
2666void CodeGenFunction::PushDestructorCleanup(QualType T, Address Addr) {
2667 CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl();
2668 if (!ClassDecl)
2669 return;
2670 if (ClassDecl->hasTrivialDestructor())
2671 return;
2672
2673 const CXXDestructorDecl *D = ClassDecl->getDestructor();
2674 assert(D && D->isUsed() && "destructor not marked as used!");
2675 PushDestructorCleanup(D, T, Addr);
2676}
2677
2678void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) {
2679 // Compute the address point.
2680 llvm::Value *VTableAddressPoint =
2681 CGM.getCXXABI().getVTableAddressPointInStructor(
2682 CGF&: *this, RD: Vptr.VTableClass, Base: Vptr.Base, NearestVBase: Vptr.NearestVBase);
2683
2684 if (!VTableAddressPoint)
2685 return;
2686
2687 // Compute where to store the address point.
2688 llvm::Value *VirtualOffset = nullptr;
2689 CharUnits NonVirtualOffset = CharUnits::Zero();
2690
2691 if (CGM.getCXXABI().isVirtualOffsetNeededForVTableField(CGF&: *this, Vptr)) {
2692 // We need to use the virtual base offset offset because the virtual base
2693 // might have a different offset in the most derived class.
2694
2695 VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(
2696 CGF&: *this, This: LoadCXXThisAddress(), ClassDecl: Vptr.VTableClass, BaseClassDecl: Vptr.NearestVBase);
2697 NonVirtualOffset = Vptr.OffsetFromNearestVBase;
2698 } else {
2699 // We can just use the base offset in the complete class.
2700 NonVirtualOffset = Vptr.Base.getBaseOffset();
2701 }
2702
2703 // Apply the offsets.
2704 Address VTableField = LoadCXXThisAddress();
2705 if (!NonVirtualOffset.isZero() || VirtualOffset)
2706 VTableField = ApplyNonVirtualAndVirtualOffset(
2707 CGF&: *this, addr: VTableField, nonVirtualOffset: NonVirtualOffset, virtualOffset: VirtualOffset, derivedClass: Vptr.VTableClass,
2708 nearestVBase: Vptr.NearestVBase);
2709
2710 // Finally, store the address point. Use the same LLVM types as the field to
2711 // support optimization.
2712 unsigned GlobalsAS = CGM.getDataLayout().getDefaultGlobalsAddressSpace();
2713 llvm::Type *PtrTy = llvm::PointerType::get(C&: CGM.getLLVMContext(), AddressSpace: GlobalsAS);
2714 // vtable field is derived from `this` pointer, therefore they should be in
2715 // the same addr space. Note that this might not be LLVM address space 0.
2716 VTableField = VTableField.withElementType(ElemTy: PtrTy);
2717
2718 if (auto AuthenticationInfo = CGM.getVTablePointerAuthInfo(
2719 Context: this, Record: Vptr.Base.getBase(), StorageAddress: VTableField.emitRawPointer(CGF&: *this)))
2720 VTableAddressPoint =
2721 EmitPointerAuthSign(Info: *AuthenticationInfo, Pointer: VTableAddressPoint);
2722
2723 llvm::StoreInst *Store = Builder.CreateStore(Val: VTableAddressPoint, Addr: VTableField);
2724 TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTablePtrType: PtrTy);
2725 CGM.DecorateInstructionWithTBAA(Inst: Store, TBAAInfo);
2726 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2727 CGM.getCodeGenOpts().StrictVTablePointers)
2728 CGM.DecorateInstructionWithInvariantGroup(I: Store, RD: Vptr.VTableClass);
2729}
2730
2731CodeGenFunction::VPtrsVector
2732CodeGenFunction::getVTablePointers(const CXXRecordDecl *VTableClass) {
2733 CodeGenFunction::VPtrsVector VPtrsResult;
2734 VisitedVirtualBasesSetTy VBases;
2735 getVTablePointers(Base: BaseSubobject(VTableClass, CharUnits::Zero()),
2736 /*NearestVBase=*/nullptr,
2737 /*OffsetFromNearestVBase=*/CharUnits::Zero(),
2738 /*BaseIsNonVirtualPrimaryBase=*/false, VTableClass, VBases,
2739 vptrs&: VPtrsResult);
2740 return VPtrsResult;
2741}
2742
2743void CodeGenFunction::getVTablePointers(BaseSubobject Base,
2744 const CXXRecordDecl *NearestVBase,
2745 CharUnits OffsetFromNearestVBase,
2746 bool BaseIsNonVirtualPrimaryBase,
2747 const CXXRecordDecl *VTableClass,
2748 VisitedVirtualBasesSetTy &VBases,
2749 VPtrsVector &Vptrs) {
2750 // If this base is a non-virtual primary base the address point has already
2751 // been set.
2752 if (!BaseIsNonVirtualPrimaryBase) {
2753 // Initialize the vtable pointer for this base.
2754 VPtr Vptr = {.Base: Base, .NearestVBase: NearestVBase, .OffsetFromNearestVBase: OffsetFromNearestVBase, .VTableClass: VTableClass};
2755 Vptrs.push_back(Elt: Vptr);
2756 }
2757
2758 const CXXRecordDecl *RD = Base.getBase();
2759
2760 // Traverse bases.
2761 for (const auto &I : RD->bases()) {
2762 auto *BaseDecl = I.getType()->castAsCXXRecordDecl();
2763 // Ignore classes without a vtable.
2764 if (!BaseDecl->isDynamicClass())
2765 continue;
2766
2767 CharUnits BaseOffset;
2768 CharUnits BaseOffsetFromNearestVBase;
2769 bool BaseDeclIsNonVirtualPrimaryBase;
2770
2771 if (I.isVirtual()) {
2772 // Check if we've visited this virtual base before.
2773 if (!VBases.insert(Ptr: BaseDecl).second)
2774 continue;
2775
2776 const ASTRecordLayout &Layout =
2777 getContext().getASTRecordLayout(D: VTableClass);
2778
2779 BaseOffset = Layout.getVBaseClassOffset(VBase: BaseDecl);
2780 BaseOffsetFromNearestVBase = CharUnits::Zero();
2781 BaseDeclIsNonVirtualPrimaryBase = false;
2782 } else {
2783 const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D: RD);
2784
2785 BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(Base: BaseDecl);
2786 BaseOffsetFromNearestVBase =
2787 OffsetFromNearestVBase + Layout.getBaseClassOffset(Base: BaseDecl);
2788 BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl;
2789 }
2790
2791 getVTablePointers(
2792 Base: BaseSubobject(BaseDecl, BaseOffset),
2793 NearestVBase: I.isVirtual() ? BaseDecl : NearestVBase, OffsetFromNearestVBase: BaseOffsetFromNearestVBase,
2794 BaseIsNonVirtualPrimaryBase: BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, Vptrs);
2795 }
2796}
2797
2798void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) {
2799 // Ignore classes without a vtable.
2800 if (!RD->isDynamicClass())
2801 return;
2802
2803 // Initialize the vtable pointers for this class and all of its bases.
2804 if (CGM.getCXXABI().doStructorsInitializeVPtrs(VTableClass: RD))
2805 for (const VPtr &Vptr : getVTablePointers(VTableClass: RD))
2806 InitializeVTablePointer(Vptr);
2807
2808 if (RD->getNumVBases())
2809 CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(CGF&: *this, RD);
2810}
2811
2812llvm::Value *CodeGenFunction::GetVTablePtr(Address This, llvm::Type *VTableTy,
2813 const CXXRecordDecl *RD,
2814 VTableAuthMode AuthMode) {
2815 Address VTablePtrSrc = This.withElementType(ElemTy: VTableTy);
2816 llvm::Instruction *VTable = Builder.CreateLoad(Addr: VTablePtrSrc, Name: "vtable");
2817 TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTablePtrType: VTableTy);
2818 CGM.DecorateInstructionWithTBAA(Inst: VTable, TBAAInfo);
2819
2820 if (auto AuthenticationInfo =
2821 CGM.getVTablePointerAuthInfo(Context: this, Record: RD, StorageAddress: This.emitRawPointer(CGF&: *this))) {
2822 if (AuthMode != VTableAuthMode::UnsafeUbsanStrip) {
2823 VTable = cast<llvm::Instruction>(
2824 Val: EmitPointerAuthAuth(Info: *AuthenticationInfo, Pointer: VTable));
2825 if (AuthMode == VTableAuthMode::MustTrap) {
2826 // This is clearly suboptimal but until we have an ability
2827 // to rely on the authentication intrinsic trapping and force
2828 // an authentication to occur we don't really have a choice.
2829 VTable =
2830 cast<llvm::Instruction>(Val: Builder.CreateBitCast(V: VTable, DestTy: Int8PtrTy));
2831 Builder.CreateLoad(Addr: RawAddress(VTable, Int8Ty, CGM.getPointerAlign()),
2832 /* IsVolatile */ true);
2833 }
2834 } else {
2835 VTable = cast<llvm::Instruction>(Val: EmitPointerAuthAuth(
2836 Info: CGPointerAuthInfo(0, PointerAuthenticationMode::Strip, false, false,
2837 nullptr),
2838 Pointer: VTable));
2839 }
2840 }
2841
2842 if (CGM.getCodeGenOpts().OptimizationLevel > 0 &&
2843 CGM.getCodeGenOpts().StrictVTablePointers)
2844 CGM.DecorateInstructionWithInvariantGroup(I: VTable, RD);
2845
2846 return VTable;
2847}
2848
2849// If a class has a single non-virtual base and does not introduce or override
2850// virtual member functions or fields, it will have the same layout as its base.
2851// This function returns the least derived such class.
2852//
2853// Casting an instance of a base class to such a derived class is technically
2854// undefined behavior, but it is a relatively common hack for introducing member
2855// functions on class instances with specific properties (e.g. llvm::Operator)
2856// that works under most compilers and should not have security implications, so
2857// we allow it by default. It can be disabled with -fsanitize=cfi-cast-strict.
2858static const CXXRecordDecl *
2859LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD) {
2860 if (!RD->field_empty())
2861 return RD;
2862
2863 if (RD->getNumVBases() != 0)
2864 return RD;
2865
2866 if (RD->getNumBases() != 1)
2867 return RD;
2868
2869 for (const CXXMethodDecl *MD : RD->methods()) {
2870 if (MD->isVirtual()) {
2871 // Virtual member functions are only ok if they are implicit destructors
2872 // because the implicit destructor will have the same semantics as the
2873 // base class's destructor if no fields are added.
2874 if (isa<CXXDestructorDecl>(Val: MD) && MD->isImplicit())
2875 continue;
2876 return RD;
2877 }
2878 }
2879
2880 return LeastDerivedClassWithSameLayout(
2881 RD: RD->bases_begin()->getType()->getAsCXXRecordDecl());
2882}
2883
2884void CodeGenFunction::EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD,
2885 llvm::Value *VTable,
2886 SourceLocation Loc) {
2887 if (SanOpts.has(K: SanitizerKind::CFIVCall))
2888 EmitVTablePtrCheckForCall(RD, VTable, TCK: CodeGenFunction::CFITCK_VCall, Loc);
2889 // Emit the intrinsics of (type_test and assume) for the features of WPD and
2890 // speculative devirtualization. For WPD, emit the intrinsics only for the
2891 // case of non_public LTO visibility.
2892 // TODO: refactor this condition and similar ones into a function (e.g.,
2893 // ShouldEmitDevirtualizationMD) to encapsulate the details of the different
2894 // types of devirtualization.
2895 else if ((CGM.getCodeGenOpts().WholeProgramVTables &&
2896 !CGM.AlwaysHasLTOVisibilityPublic(RD)) ||
2897 CGM.getCodeGenOpts().DevirtualizeSpeculatively) {
2898 CanQualType Ty = CGM.getContext().getCanonicalTagType(TD: RD);
2899 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(T: Ty);
2900 llvm::Value *TypeId = llvm::MetadataAsValue::get(Context&: CGM.getLLVMContext(), MD);
2901
2902 // If we already know that the call has hidden LTO visibility, emit
2903 // @llvm.type.test(). Otherwise emit @llvm.public.type.test(), which WPD
2904 // will convert to @llvm.type.test() if we assert at link time that we have
2905 // whole program visibility.
2906 llvm::Intrinsic::ID IID = CGM.HasHiddenLTOVisibility(RD)
2907 ? llvm::Intrinsic::type_test
2908 : llvm::Intrinsic::public_type_test;
2909 llvm::Value *TypeTest =
2910 Builder.CreateCall(Callee: CGM.getIntrinsic(IID), Args: {VTable, TypeId});
2911 Builder.CreateCall(Callee: CGM.getIntrinsic(IID: llvm::Intrinsic::assume), Args: TypeTest);
2912 }
2913}
2914
2915/// Converts the CFITypeCheckKind into SanitizerKind::SanitizerOrdinal and
2916/// llvm::SanitizerStatKind.
2917static std::pair<SanitizerKind::SanitizerOrdinal, llvm::SanitizerStatKind>
2918SanitizerInfoFromCFICheckKind(CodeGenFunction::CFITypeCheckKind TCK) {
2919 switch (TCK) {
2920 case CodeGenFunction::CFITCK_VCall:
2921 return std::make_pair(x: SanitizerKind::SO_CFIVCall, y: llvm::SanStat_CFI_VCall);
2922 case CodeGenFunction::CFITCK_NVCall:
2923 return std::make_pair(x: SanitizerKind::SO_CFINVCall,
2924 y: llvm::SanStat_CFI_NVCall);
2925 case CodeGenFunction::CFITCK_DerivedCast:
2926 return std::make_pair(x: SanitizerKind::SO_CFIDerivedCast,
2927 y: llvm::SanStat_CFI_DerivedCast);
2928 case CodeGenFunction::CFITCK_UnrelatedCast:
2929 return std::make_pair(x: SanitizerKind::SO_CFIUnrelatedCast,
2930 y: llvm::SanStat_CFI_UnrelatedCast);
2931 case CodeGenFunction::CFITCK_ICall:
2932 case CodeGenFunction::CFITCK_NVMFCall:
2933 case CodeGenFunction::CFITCK_VMFCall:
2934 llvm_unreachable("unexpected sanitizer kind");
2935 }
2936 llvm_unreachable("Unknown CFITypeCheckKind enum");
2937}
2938
2939void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXRecordDecl *RD,
2940 llvm::Value *VTable,
2941 CFITypeCheckKind TCK,
2942 SourceLocation Loc) {
2943 if (!SanOpts.has(K: SanitizerKind::CFICastStrict))
2944 RD = LeastDerivedClassWithSameLayout(RD);
2945
2946 auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
2947 SanitizerDebugLocation SanScope(this, {Ordinal},
2948 SanitizerHandler::CFICheckFail);
2949
2950 EmitVTablePtrCheck(RD, VTable, TCK, Loc);
2951}
2952
2953void CodeGenFunction::EmitVTablePtrCheckForCast(QualType T, Address Derived,
2954 bool MayBeNull,
2955 CFITypeCheckKind TCK,
2956 SourceLocation Loc) {
2957 if (!getLangOpts().CPlusPlus)
2958 return;
2959
2960 const auto *ClassDecl = T->getAsCXXRecordDecl();
2961 if (!ClassDecl)
2962 return;
2963
2964 if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass())
2965 return;
2966
2967 if (!SanOpts.has(K: SanitizerKind::CFICastStrict))
2968 ClassDecl = LeastDerivedClassWithSameLayout(RD: ClassDecl);
2969
2970 auto [Ordinal, _] = SanitizerInfoFromCFICheckKind(TCK);
2971 SanitizerDebugLocation SanScope(this, {Ordinal},
2972 SanitizerHandler::CFICheckFail);
2973
2974 llvm::BasicBlock *ContBlock = nullptr;
2975
2976 if (MayBeNull) {
2977 llvm::Value *DerivedNotNull =
2978 Builder.CreateIsNotNull(Arg: Derived.emitRawPointer(CGF&: *this), Name: "cast.nonnull");
2979
2980 llvm::BasicBlock *CheckBlock = createBasicBlock(name: "cast.check");
2981 ContBlock = createBasicBlock(name: "cast.cont");
2982
2983 Builder.CreateCondBr(Cond: DerivedNotNull, True: CheckBlock, False: ContBlock);
2984
2985 EmitBlock(BB: CheckBlock);
2986 }
2987
2988 llvm::Value *VTable;
2989 std::tie(args&: VTable, args&: ClassDecl) =
2990 CGM.getCXXABI().LoadVTablePtr(CGF&: *this, This: Derived, RD: ClassDecl);
2991
2992 EmitVTablePtrCheck(RD: ClassDecl, VTable, TCK, Loc);
2993
2994 if (MayBeNull) {
2995 Builder.CreateBr(Dest: ContBlock);
2996 EmitBlock(BB: ContBlock);
2997 }
2998}
2999
3000void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD,
3001 llvm::Value *VTable,
3002 CFITypeCheckKind TCK,
3003 SourceLocation Loc) {
3004 assert(IsSanitizerScope);
3005
3006 if (!CGM.getCodeGenOpts().SanitizeCfiCrossDso &&
3007 !CGM.HasHiddenLTOVisibility(RD))
3008 return;
3009
3010 auto [M, SSK] = SanitizerInfoFromCFICheckKind(TCK);
3011
3012 std::string TypeName = RD->getQualifiedNameAsString();
3013 if (getContext().getNoSanitizeList().containsType(
3014 Mask: SanitizerMask::bitPosToMask(Pos: M), MangledTypeName: TypeName))
3015 return;
3016
3017 EmitSanitizerStatReport(SSK);
3018
3019 CanQualType T = CGM.getContext().getCanonicalTagType(TD: RD);
3020 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(T);
3021 llvm::Value *TypeId = llvm::MetadataAsValue::get(Context&: getLLVMContext(), MD);
3022
3023 llvm::Value *TypeTest = Builder.CreateCall(
3024 Callee: CGM.getIntrinsic(IID: llvm::Intrinsic::type_test), Args: {VTable, TypeId});
3025
3026 llvm::Constant *StaticData[] = {
3027 llvm::ConstantInt::get(Ty: Int8Ty, V: TCK),
3028 EmitCheckSourceLocation(Loc),
3029 EmitCheckTypeDescriptor(T),
3030 };
3031
3032 auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD);
3033 if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) {
3034 EmitCfiSlowPathCheck(Ordinal: M, Cond: TypeTest, TypeId: CrossDsoTypeId, Ptr: VTable, StaticArgs: StaticData);
3035 return;
3036 }
3037
3038 if (CGM.getCodeGenOpts().SanitizeTrap.has(O: M)) {
3039 bool NoMerge = !CGM.getCodeGenOpts().SanitizeMergeHandlers.has(O: M);
3040 EmitTrapCheck(Checked: TypeTest, CheckHandlerID: SanitizerHandler::CFICheckFail, NoMerge);
3041 return;
3042 }
3043
3044 llvm::Value *AllVtables = llvm::MetadataAsValue::get(
3045 Context&: CGM.getLLVMContext(),
3046 MD: llvm::MDString::get(Context&: CGM.getLLVMContext(), Str: "all-vtables"));
3047 llvm::Value *ValidVtable = Builder.CreateCall(
3048 Callee: CGM.getIntrinsic(IID: llvm::Intrinsic::type_test), Args: {VTable, AllVtables});
3049 EmitCheck(Checked: std::make_pair(x&: TypeTest, y&: M), Check: SanitizerHandler::CFICheckFail,
3050 StaticArgs: StaticData, DynamicArgs: {VTable, ValidVtable});
3051}
3052
3053bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) {
3054 if ((!CGM.getCodeGenOpts().WholeProgramVTables ||
3055 !CGM.HasHiddenLTOVisibility(RD)) &&
3056 !CGM.getCodeGenOpts().DevirtualizeSpeculatively)
3057 return false;
3058
3059 if (CGM.getCodeGenOpts().VirtualFunctionElimination)
3060 return true;
3061
3062 if (!SanOpts.has(K: SanitizerKind::CFIVCall) ||
3063 !CGM.getCodeGenOpts().SanitizeTrap.has(K: SanitizerKind::CFIVCall))
3064 return false;
3065
3066 std::string TypeName = RD->getQualifiedNameAsString();
3067 return !getContext().getNoSanitizeList().containsType(Mask: SanitizerKind::CFIVCall,
3068 MangledTypeName: TypeName);
3069}
3070
3071llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad(
3072 const CXXRecordDecl *RD, llvm::Value *VTable, llvm::Type *VTableTy,
3073 uint64_t VTableByteOffset) {
3074 auto CheckOrdinal = SanitizerKind::SO_CFIVCall;
3075 auto CheckHandler = SanitizerHandler::CFICheckFail;
3076 SanitizerDebugLocation SanScope(this, {CheckOrdinal}, CheckHandler);
3077
3078 EmitSanitizerStatReport(SSK: llvm::SanStat_CFI_VCall);
3079
3080 CanQualType T = CGM.getContext().getCanonicalTagType(TD: RD);
3081 llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(T);
3082 llvm::Value *TypeId = llvm::MetadataAsValue::get(Context&: CGM.getLLVMContext(), MD);
3083
3084 auto CheckedLoadIntrinsic = CGM.getLangOpts().RelativeCXXABIVTables
3085 ? llvm::Intrinsic::type_checked_load_relative
3086 : llvm::Intrinsic::type_checked_load;
3087 llvm::Value *CheckedLoad = Builder.CreateCall(
3088 Callee: CGM.getIntrinsic(IID: CheckedLoadIntrinsic),
3089 Args: {VTable, llvm::ConstantInt::get(Ty: Int32Ty, V: VTableByteOffset), TypeId});
3090
3091 llvm::Value *CheckResult = Builder.CreateExtractValue(Agg: CheckedLoad, Idxs: 1);
3092
3093 std::string TypeName = RD->getQualifiedNameAsString();
3094 if (SanOpts.has(K: SanitizerKind::CFIVCall) &&
3095 !getContext().getNoSanitizeList().containsType(Mask: SanitizerKind::CFIVCall,
3096 MangledTypeName: TypeName)) {
3097 EmitCheck(Checked: std::make_pair(x&: CheckResult, y&: CheckOrdinal), Check: CheckHandler, StaticArgs: {}, DynamicArgs: {});
3098 }
3099
3100 return Builder.CreateBitCast(V: Builder.CreateExtractValue(Agg: CheckedLoad, Idxs: 0),
3101 DestTy: VTableTy);
3102}
3103
3104void CodeGenFunction::EmitForwardingCallToLambda(
3105 const CXXMethodDecl *callOperator, CallArgList &callArgs,
3106 const CGFunctionInfo *calleeFnInfo, llvm::Constant *calleePtr) {
3107 // Get the address of the call operator.
3108 if (!calleeFnInfo)
3109 calleeFnInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(MD: callOperator);
3110
3111 if (!calleePtr)
3112 calleePtr =
3113 CGM.GetAddrOfFunction(GD: GlobalDecl(callOperator),
3114 Ty: CGM.getTypes().GetFunctionType(Info: *calleeFnInfo));
3115
3116 // Prepare the return slot.
3117 const FunctionProtoType *FPT =
3118 callOperator->getType()->castAs<FunctionProtoType>();
3119 QualType resultType = FPT->getReturnType();
3120 ReturnValueSlot returnSlot;
3121 if (!resultType->isVoidType() &&
3122 calleeFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect &&
3123 !hasScalarEvaluationKind(T: calleeFnInfo->getReturnType()))
3124 returnSlot =
3125 ReturnValueSlot(ReturnValue, resultType.isVolatileQualified(),
3126 /*IsUnused=*/false, /*IsExternallyDestructed=*/true);
3127
3128 // We don't need to separately arrange the call arguments because
3129 // the call can't be variadic anyway --- it's impossible to forward
3130 // variadic arguments.
3131
3132 // Now emit our call.
3133 auto callee = CGCallee::forDirect(functionPtr: calleePtr, abstractInfo: GlobalDecl(callOperator));
3134 RValue RV = EmitCall(CallInfo: *calleeFnInfo, Callee: callee, ReturnValue: returnSlot, Args: callArgs);
3135
3136 // If necessary, copy the returned value into the slot.
3137 if (!resultType->isVoidType() && returnSlot.isNull()) {
3138 if (getLangOpts().ObjCAutoRefCount && resultType->isObjCRetainableType()) {
3139 RV = RValue::get(V: EmitARCRetainAutoreleasedReturnValue(value: RV.getScalarVal()));
3140 }
3141 EmitReturnOfRValue(RV, Ty: resultType);
3142 } else
3143 EmitBranchThroughCleanup(Dest: ReturnBlock);
3144}
3145
3146void CodeGenFunction::EmitLambdaBlockInvokeBody() {
3147 const BlockDecl *BD = BlockInfo->getBlockDecl();
3148 const VarDecl *variable = BD->capture_begin()->getVariable();
3149 const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl();
3150 const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
3151
3152 if (CallOp->isVariadic()) {
3153 // FIXME: Making this work correctly is nasty because it requires either
3154 // cloning the body of the call operator or making the call operator
3155 // forward.
3156 CGM.ErrorUnsupported(D: CurCodeDecl, Type: "lambda conversion to variadic function");
3157 return;
3158 }
3159
3160 // Start building arguments for forwarding call
3161 CallArgList CallArgs;
3162
3163 CanQualType ThisType =
3164 getContext().getPointerType(T: getContext().getCanonicalTagType(TD: Lambda));
3165 Address ThisPtr = GetAddrOfBlockDecl(var: variable);
3166 CallArgs.add(rvalue: RValue::get(V: getAsNaturalPointerTo(Addr: ThisPtr, PointeeType: ThisType)), type: ThisType);
3167
3168 // Add the rest of the parameters.
3169 for (auto *param : BD->parameters())
3170 EmitDelegateCallArg(args&: CallArgs, param, loc: param->getBeginLoc());
3171
3172 assert(!Lambda->isGenericLambda() &&
3173 "generic lambda interconversion to block not implemented");
3174 EmitForwardingCallToLambda(callOperator: CallOp, callArgs&: CallArgs);
3175}
3176
3177void CodeGenFunction::EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD) {
3178 if (MD->isVariadic()) {
3179 // FIXME: Making this work correctly is nasty because it requires either
3180 // cloning the body of the call operator or making the call operator
3181 // forward.
3182 CGM.ErrorUnsupported(D: MD, Type: "lambda conversion to variadic function");
3183 return;
3184 }
3185
3186 const CXXRecordDecl *Lambda = MD->getParent();
3187
3188 // Start building arguments for forwarding call
3189 CallArgList CallArgs;
3190
3191 CanQualType LambdaType = getContext().getCanonicalTagType(TD: Lambda);
3192 CanQualType ThisType = getContext().getPointerType(T: LambdaType);
3193 Address ThisPtr = CreateMemTemp(T: LambdaType, Name: "unused.capture");
3194 CallArgs.add(rvalue: RValue::get(V: ThisPtr.emitRawPointer(CGF&: *this)), type: ThisType);
3195
3196 EmitLambdaDelegatingInvokeBody(MD, CallArgs);
3197}
3198
3199void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD,
3200 CallArgList &CallArgs) {
3201 // Add the rest of the forwarded parameters.
3202 for (auto *Param : MD->parameters())
3203 EmitDelegateCallArg(args&: CallArgs, param: Param, loc: Param->getBeginLoc());
3204
3205 const CXXRecordDecl *Lambda = MD->getParent();
3206 const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator();
3207 // For a generic lambda, find the corresponding call operator specialization
3208 // to which the call to the static-invoker shall be forwarded.
3209 if (Lambda->isGenericLambda()) {
3210 assert(MD->isFunctionTemplateSpecialization());
3211 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
3212 FunctionTemplateDecl *CallOpTemplate =
3213 CallOp->getDescribedFunctionTemplate();
3214 void *InsertPos = nullptr;
3215 FunctionDecl *CorrespondingCallOpSpecialization =
3216 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos);
3217 assert(CorrespondingCallOpSpecialization);
3218 CallOp = cast<CXXMethodDecl>(Val: CorrespondingCallOpSpecialization);
3219 }
3220
3221 // Special lambda forwarding when there are inalloca parameters.
3222 if (hasInAllocaArg(MD)) {
3223 const CGFunctionInfo *ImplFnInfo = nullptr;
3224 llvm::Function *ImplFn = nullptr;
3225 EmitLambdaInAllocaImplFn(CallOp, ImplFnInfo: &ImplFnInfo, ImplFn: &ImplFn);
3226
3227 EmitForwardingCallToLambda(callOperator: CallOp, callArgs&: CallArgs, calleeFnInfo: ImplFnInfo, calleePtr: ImplFn);
3228 return;
3229 }
3230
3231 EmitForwardingCallToLambda(callOperator: CallOp, callArgs&: CallArgs);
3232}
3233
3234void CodeGenFunction::EmitLambdaInAllocaCallOpBody(const CXXMethodDecl *MD) {
3235 if (MD->isVariadic()) {
3236 // FIXME: Making this work correctly is nasty because it requires either
3237 // cloning the body of the call operator or making the call operator
3238 // forward.
3239 CGM.ErrorUnsupported(D: MD, Type: "lambda conversion to variadic function");
3240 return;
3241 }
3242
3243 // Forward %this argument.
3244 CallArgList CallArgs;
3245 CanQualType LambdaType = getContext().getCanonicalTagType(TD: MD->getParent());
3246 CanQualType ThisType = getContext().getPointerType(T: LambdaType);
3247 llvm::Value *ThisArg = CurFn->getArg(i: 0);
3248 CallArgs.add(rvalue: RValue::get(V: ThisArg), type: ThisType);
3249
3250 EmitLambdaDelegatingInvokeBody(MD, CallArgs);
3251}
3252
3253void CodeGenFunction::EmitLambdaInAllocaImplFn(
3254 const CXXMethodDecl *CallOp, const CGFunctionInfo **ImplFnInfo,
3255 llvm::Function **ImplFn) {
3256 const CGFunctionInfo &FnInfo =
3257 CGM.getTypes().arrangeCXXMethodDeclaration(MD: CallOp);
3258 llvm::Function *CallOpFn =
3259 cast<llvm::Function>(Val: CGM.GetAddrOfFunction(GD: GlobalDecl(CallOp)));
3260
3261 // Emit function containing the original call op body. __invoke will delegate
3262 // to this function.
3263 SmallVector<CanQualType, 4> ArgTypes;
3264 for (auto I = FnInfo.arg_begin(); I != FnInfo.arg_end(); ++I)
3265 ArgTypes.push_back(Elt: I->type);
3266 *ImplFnInfo = &CGM.getTypes().arrangeLLVMFunctionInfo(
3267 returnType: FnInfo.getReturnType(), opts: FnInfoOpts::IsDelegateCall, argTypes: ArgTypes,
3268 info: FnInfo.getExtInfo(), paramInfos: {}, args: FnInfo.getRequiredArgs());
3269
3270 // Create mangled name as if this was a method named __impl. If for some
3271 // reason the name doesn't look as expected then just tack __impl to the
3272 // front.
3273 // TODO: Use the name mangler to produce the right name instead of using
3274 // string replacement.
3275 StringRef CallOpName = CallOpFn->getName();
3276 std::string ImplName;
3277 if (size_t Pos = CallOpName.find_first_of(Chars: "<lambda"))
3278 ImplName = ("?__impl@" + CallOpName.drop_front(N: Pos)).str();
3279 else
3280 ImplName = ("__impl" + CallOpName).str();
3281
3282 llvm::Function *Fn = CallOpFn->getParent()->getFunction(Name: ImplName);
3283 if (!Fn) {
3284 Fn = llvm::Function::Create(Ty: CGM.getTypes().GetFunctionType(Info: **ImplFnInfo),
3285 Linkage: llvm::GlobalValue::InternalLinkage, N: ImplName,
3286 M&: CGM.getModule());
3287 CGM.SetInternalFunctionAttributes(GD: CallOp, F: Fn, FI: **ImplFnInfo);
3288
3289 const GlobalDecl &GD = GlobalDecl(CallOp);
3290 const auto *D = cast<FunctionDecl>(Val: GD.getDecl());
3291 CodeGenFunction(CGM).GenerateCode(GD, Fn, FnInfo: **ImplFnInfo);
3292 CGM.SetLLVMFunctionAttributesForDefinition(D, F: Fn);
3293 }
3294 *ImplFn = Fn;
3295}
3296