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