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