Coverage Report

Created: 2020-02-18 08:44

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