Index: head/contrib/llvm-project/clang/lib/CodeGen/CGClass.cpp =================================================================== --- head/contrib/llvm-project/clang/lib/CodeGen/CGClass.cpp (revision 359825) +++ head/contrib/llvm-project/clang/lib/CodeGen/CGClass.cpp (revision 359826) @@ -1,2942 +1,2942 @@ //===--- CGClass.cpp - Emit LLVM Code for C++ classes -----------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This contains code dealing with C++ code generation of classes // //===----------------------------------------------------------------------===// #include "CGBlocks.h" #include "CGCXXABI.h" #include "CGDebugInfo.h" #include "CGRecordLayout.h" #include "CodeGenFunction.h" #include "TargetInfo.h" #include "clang/AST/Attr.h" #include "clang/AST/CXXInheritance.h" #include "clang/AST/DeclTemplate.h" #include "clang/AST/EvaluatedExprVisitor.h" #include "clang/AST/RecordLayout.h" #include "clang/AST/StmtCXX.h" #include "clang/Basic/CodeGenOptions.h" #include "clang/Basic/TargetBuiltins.h" #include "clang/CodeGen/CGFunctionInfo.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/Metadata.h" #include "llvm/Transforms/Utils/SanitizerStats.h" using namespace clang; using namespace CodeGen; /// Return the best known alignment for an unknown pointer to a /// particular class. CharUnits CodeGenModule::getClassPointerAlignment(const CXXRecordDecl *RD) { if (!RD->isCompleteDefinition()) return CharUnits::One(); // Hopefully won't be used anywhere. auto &layout = getContext().getASTRecordLayout(RD); // If the class is final, then we know that the pointer points to an // object of that type and can use the full alignment. if (RD->hasAttr()) { return layout.getAlignment(); // Otherwise, we have to assume it could be a subclass. } else { return layout.getNonVirtualAlignment(); } } /// Return the best known alignment for a pointer to a virtual base, /// given the alignment of a pointer to the derived class. CharUnits CodeGenModule::getVBaseAlignment(CharUnits actualDerivedAlign, const CXXRecordDecl *derivedClass, const CXXRecordDecl *vbaseClass) { // The basic idea here is that an underaligned derived pointer might // indicate an underaligned base pointer. assert(vbaseClass->isCompleteDefinition()); auto &baseLayout = getContext().getASTRecordLayout(vbaseClass); CharUnits expectedVBaseAlign = baseLayout.getNonVirtualAlignment(); return getDynamicOffsetAlignment(actualDerivedAlign, derivedClass, expectedVBaseAlign); } CharUnits CodeGenModule::getDynamicOffsetAlignment(CharUnits actualBaseAlign, const CXXRecordDecl *baseDecl, CharUnits expectedTargetAlign) { // If the base is an incomplete type (which is, alas, possible with // member pointers), be pessimistic. if (!baseDecl->isCompleteDefinition()) return std::min(actualBaseAlign, expectedTargetAlign); auto &baseLayout = getContext().getASTRecordLayout(baseDecl); CharUnits expectedBaseAlign = baseLayout.getNonVirtualAlignment(); // If the class is properly aligned, assume the target offset is, too. // // This actually isn't necessarily the right thing to do --- if the // class is a complete object, but it's only properly aligned for a // base subobject, then the alignments of things relative to it are // probably off as well. (Note that this requires the alignment of // the target to be greater than the NV alignment of the derived // class.) // // However, our approach to this kind of under-alignment can only // ever be best effort; after all, we're never going to propagate // alignments through variables or parameters. Note, in particular, // that constructing a polymorphic type in an address that's less // than pointer-aligned will generally trap in the constructor, // unless we someday add some sort of attribute to change the // assumed alignment of 'this'. So our goal here is pretty much // just to allow the user to explicitly say that a pointer is // under-aligned and then safely access its fields and vtables. if (actualBaseAlign >= expectedBaseAlign) { return expectedTargetAlign; } // Otherwise, we might be offset by an arbitrary multiple of the // actual alignment. The correct adjustment is to take the min of // the two alignments. return std::min(actualBaseAlign, expectedTargetAlign); } Address CodeGenFunction::LoadCXXThisAddress() { assert(CurFuncDecl && "loading 'this' without a func declaration?"); assert(isa(CurFuncDecl)); // Lazily compute CXXThisAlignment. if (CXXThisAlignment.isZero()) { // Just use the best known alignment for the parent. // TODO: if we're currently emitting a complete-object ctor/dtor, // we can always use the complete-object alignment. auto RD = cast(CurFuncDecl)->getParent(); CXXThisAlignment = CGM.getClassPointerAlignment(RD); } return Address(LoadCXXThis(), CXXThisAlignment); } /// Emit the address of a field using a member data pointer. /// /// \param E Only used for emergency diagnostics Address CodeGenFunction::EmitCXXMemberDataPointerAddress(const Expr *E, Address base, llvm::Value *memberPtr, const MemberPointerType *memberPtrType, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) { // Ask the ABI to compute the actual address. llvm::Value *ptr = CGM.getCXXABI().EmitMemberDataPointerAddress(*this, E, base, memberPtr, memberPtrType); QualType memberType = memberPtrType->getPointeeType(); CharUnits memberAlign = getNaturalTypeAlignment(memberType, BaseInfo, TBAAInfo); memberAlign = CGM.getDynamicOffsetAlignment(base.getAlignment(), memberPtrType->getClass()->getAsCXXRecordDecl(), memberAlign); return Address(ptr, memberAlign); } CharUnits CodeGenModule::computeNonVirtualBaseClassOffset( const CXXRecordDecl *DerivedClass, CastExpr::path_const_iterator Start, CastExpr::path_const_iterator End) { CharUnits Offset = CharUnits::Zero(); const ASTContext &Context = getContext(); const CXXRecordDecl *RD = DerivedClass; for (CastExpr::path_const_iterator I = Start; I != End; ++I) { const CXXBaseSpecifier *Base = *I; assert(!Base->isVirtual() && "Should not see virtual bases here!"); // Get the layout. const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); const auto *BaseDecl = cast(Base->getType()->castAs()->getDecl()); // Add the offset. Offset += Layout.getBaseClassOffset(BaseDecl); RD = BaseDecl; } return Offset; } llvm::Constant * CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd) { assert(PathBegin != PathEnd && "Base path should not be empty!"); CharUnits Offset = computeNonVirtualBaseClassOffset(ClassDecl, PathBegin, PathEnd); if (Offset.isZero()) return nullptr; llvm::Type *PtrDiffTy = Types.ConvertType(getContext().getPointerDiffType()); return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity()); } /// Gets the address of a direct base class within a complete object. /// This should only be used for (1) non-virtual bases or (2) virtual bases /// when the type is known to be complete (e.g. in complete destructors). /// /// The object pointed to by 'This' is assumed to be non-null. Address CodeGenFunction::GetAddressOfDirectBaseInCompleteClass(Address This, const CXXRecordDecl *Derived, const CXXRecordDecl *Base, bool BaseIsVirtual) { // 'this' must be a pointer (in some address space) to Derived. assert(This.getElementType() == ConvertType(Derived)); // Compute the offset of the virtual base. CharUnits Offset; const ASTRecordLayout &Layout = getContext().getASTRecordLayout(Derived); if (BaseIsVirtual) Offset = Layout.getVBaseClassOffset(Base); else Offset = Layout.getBaseClassOffset(Base); // Shift and cast down to the base type. // TODO: for complete types, this should be possible with a GEP. Address V = This; if (!Offset.isZero()) { V = Builder.CreateElementBitCast(V, Int8Ty); V = Builder.CreateConstInBoundsByteGEP(V, Offset); } V = Builder.CreateElementBitCast(V, ConvertType(Base)); return V; } static Address ApplyNonVirtualAndVirtualOffset(CodeGenFunction &CGF, Address addr, CharUnits nonVirtualOffset, llvm::Value *virtualOffset, const CXXRecordDecl *derivedClass, const CXXRecordDecl *nearestVBase) { // Assert that we have something to do. assert(!nonVirtualOffset.isZero() || virtualOffset != nullptr); // Compute the offset from the static and dynamic components. llvm::Value *baseOffset; if (!nonVirtualOffset.isZero()) { baseOffset = llvm::ConstantInt::get(CGF.PtrDiffTy, nonVirtualOffset.getQuantity()); if (virtualOffset) { baseOffset = CGF.Builder.CreateAdd(virtualOffset, baseOffset); } } else { baseOffset = virtualOffset; } // Apply the base offset. llvm::Value *ptr = addr.getPointer(); unsigned AddrSpace = ptr->getType()->getPointerAddressSpace(); ptr = CGF.Builder.CreateBitCast(ptr, CGF.Int8Ty->getPointerTo(AddrSpace)); ptr = CGF.Builder.CreateInBoundsGEP(ptr, baseOffset, "add.ptr"); // If we have a virtual component, the alignment of the result will // be relative only to the known alignment of that vbase. CharUnits alignment; if (virtualOffset) { assert(nearestVBase && "virtual offset without vbase?"); alignment = CGF.CGM.getVBaseAlignment(addr.getAlignment(), derivedClass, nearestVBase); } else { alignment = addr.getAlignment(); } alignment = alignment.alignmentAtOffset(nonVirtualOffset); return Address(ptr, alignment); } Address CodeGenFunction::GetAddressOfBaseClass( Address Value, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue, SourceLocation Loc) { assert(PathBegin != PathEnd && "Base path should not be empty!"); CastExpr::path_const_iterator Start = PathBegin; const CXXRecordDecl *VBase = nullptr; // Sema has done some convenient canonicalization here: if the // access path involved any virtual steps, the conversion path will // *start* with a step down to the correct virtual base subobject, // and hence will not require any further steps. if ((*Start)->isVirtual()) { VBase = cast( (*Start)->getType()->castAs()->getDecl()); ++Start; } // Compute the static offset of the ultimate destination within its // allocating subobject (the virtual base, if there is one, or else // the "complete" object that we see). CharUnits NonVirtualOffset = CGM.computeNonVirtualBaseClassOffset( VBase ? VBase : Derived, Start, PathEnd); // If there's a virtual step, we can sometimes "devirtualize" it. // For now, that's limited to when the derived type is final. // TODO: "devirtualize" this for accesses to known-complete objects. if (VBase && Derived->hasAttr()) { const ASTRecordLayout &layout = getContext().getASTRecordLayout(Derived); CharUnits vBaseOffset = layout.getVBaseClassOffset(VBase); NonVirtualOffset += vBaseOffset; VBase = nullptr; // we no longer have a virtual step } // Get the base pointer type. llvm::Type *BasePtrTy = ConvertType((PathEnd[-1])->getType()) ->getPointerTo(Value.getType()->getPointerAddressSpace()); QualType DerivedTy = getContext().getRecordType(Derived); CharUnits DerivedAlign = CGM.getClassPointerAlignment(Derived); // If the static offset is zero and we don't have a virtual step, // just do a bitcast; null checks are unnecessary. if (NonVirtualOffset.isZero() && !VBase) { if (sanitizePerformTypeCheck()) { SanitizerSet SkippedChecks; SkippedChecks.set(SanitizerKind::Null, !NullCheckValue); EmitTypeCheck(TCK_Upcast, Loc, Value.getPointer(), DerivedTy, DerivedAlign, SkippedChecks); } return Builder.CreateBitCast(Value, BasePtrTy); } llvm::BasicBlock *origBB = nullptr; llvm::BasicBlock *endBB = nullptr; // Skip over the offset (and the vtable load) if we're supposed to // null-check the pointer. if (NullCheckValue) { origBB = Builder.GetInsertBlock(); llvm::BasicBlock *notNullBB = createBasicBlock("cast.notnull"); endBB = createBasicBlock("cast.end"); llvm::Value *isNull = Builder.CreateIsNull(Value.getPointer()); Builder.CreateCondBr(isNull, endBB, notNullBB); EmitBlock(notNullBB); } if (sanitizePerformTypeCheck()) { SanitizerSet SkippedChecks; SkippedChecks.set(SanitizerKind::Null, true); EmitTypeCheck(VBase ? TCK_UpcastToVirtualBase : TCK_Upcast, Loc, Value.getPointer(), DerivedTy, DerivedAlign, SkippedChecks); } // Compute the virtual offset. llvm::Value *VirtualOffset = nullptr; if (VBase) { VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset(*this, Value, Derived, VBase); } // Apply both offsets. Value = ApplyNonVirtualAndVirtualOffset(*this, Value, NonVirtualOffset, VirtualOffset, Derived, VBase); // Cast to the destination type. Value = Builder.CreateBitCast(Value, BasePtrTy); // Build a phi if we needed a null check. if (NullCheckValue) { llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); Builder.CreateBr(endBB); EmitBlock(endBB); llvm::PHINode *PHI = Builder.CreatePHI(BasePtrTy, 2, "cast.result"); PHI->addIncoming(Value.getPointer(), notNullBB); PHI->addIncoming(llvm::Constant::getNullValue(BasePtrTy), origBB); Value = Address(PHI, Value.getAlignment()); } return Value; } Address CodeGenFunction::GetAddressOfDerivedClass(Address BaseAddr, const CXXRecordDecl *Derived, CastExpr::path_const_iterator PathBegin, CastExpr::path_const_iterator PathEnd, bool NullCheckValue) { assert(PathBegin != PathEnd && "Base path should not be empty!"); QualType DerivedTy = getContext().getCanonicalType(getContext().getTagDeclType(Derived)); unsigned AddrSpace = BaseAddr.getPointer()->getType()->getPointerAddressSpace(); llvm::Type *DerivedPtrTy = ConvertType(DerivedTy)->getPointerTo(AddrSpace); llvm::Value *NonVirtualOffset = CGM.GetNonVirtualBaseClassOffset(Derived, PathBegin, PathEnd); if (!NonVirtualOffset) { // No offset, we can just cast back. return Builder.CreateBitCast(BaseAddr, DerivedPtrTy); } llvm::BasicBlock *CastNull = nullptr; llvm::BasicBlock *CastNotNull = nullptr; llvm::BasicBlock *CastEnd = nullptr; if (NullCheckValue) { CastNull = createBasicBlock("cast.null"); CastNotNull = createBasicBlock("cast.notnull"); CastEnd = createBasicBlock("cast.end"); llvm::Value *IsNull = Builder.CreateIsNull(BaseAddr.getPointer()); Builder.CreateCondBr(IsNull, CastNull, CastNotNull); EmitBlock(CastNotNull); } // Apply the offset. llvm::Value *Value = Builder.CreateBitCast(BaseAddr.getPointer(), Int8PtrTy); Value = Builder.CreateInBoundsGEP(Value, Builder.CreateNeg(NonVirtualOffset), "sub.ptr"); // Just cast. Value = Builder.CreateBitCast(Value, DerivedPtrTy); // Produce a PHI if we had a null-check. if (NullCheckValue) { Builder.CreateBr(CastEnd); EmitBlock(CastNull); Builder.CreateBr(CastEnd); EmitBlock(CastEnd); llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); PHI->addIncoming(Value, CastNotNull); PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); Value = PHI; } return Address(Value, CGM.getClassPointerAlignment(Derived)); } llvm::Value *CodeGenFunction::GetVTTParameter(GlobalDecl GD, bool ForVirtualBase, bool Delegating) { if (!CGM.getCXXABI().NeedsVTTParameter(GD)) { // This constructor/destructor does not need a VTT parameter. return nullptr; } const CXXRecordDecl *RD = cast(CurCodeDecl)->getParent(); const CXXRecordDecl *Base = cast(GD.getDecl())->getParent(); llvm::Value *VTT; uint64_t SubVTTIndex; if (Delegating) { // If this is a delegating constructor call, just load the VTT. return LoadCXXVTT(); } else if (RD == Base) { // If the record matches the base, this is the complete ctor/dtor // variant calling the base variant in a class with virtual bases. assert(!CGM.getCXXABI().NeedsVTTParameter(CurGD) && "doing no-op VTT offset in base dtor/ctor?"); assert(!ForVirtualBase && "Can't have same class as virtual base!"); SubVTTIndex = 0; } else { const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); CharUnits BaseOffset = ForVirtualBase ? Layout.getVBaseClassOffset(Base) : Layout.getBaseClassOffset(Base); SubVTTIndex = CGM.getVTables().getSubVTTIndex(RD, BaseSubobject(Base, BaseOffset)); assert(SubVTTIndex != 0 && "Sub-VTT index must be greater than zero!"); } if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { // A VTT parameter was passed to the constructor, use it. VTT = LoadCXXVTT(); VTT = Builder.CreateConstInBoundsGEP1_64(VTT, SubVTTIndex); } else { // We're the complete constructor, so get the VTT by name. VTT = CGM.getVTables().GetAddrOfVTT(RD); VTT = Builder.CreateConstInBoundsGEP2_64(VTT, 0, SubVTTIndex); } return VTT; } namespace { /// Call the destructor for a direct base class. struct CallBaseDtor final : EHScopeStack::Cleanup { const CXXRecordDecl *BaseClass; bool BaseIsVirtual; CallBaseDtor(const CXXRecordDecl *Base, bool BaseIsVirtual) : BaseClass(Base), BaseIsVirtual(BaseIsVirtual) {} void Emit(CodeGenFunction &CGF, Flags flags) override { const CXXRecordDecl *DerivedClass = cast(CGF.CurCodeDecl)->getParent(); const CXXDestructorDecl *D = BaseClass->getDestructor(); // We are already inside a destructor, so presumably the object being // destroyed should have the expected type. QualType ThisTy = D->getThisObjectType(); Address Addr = CGF.GetAddressOfDirectBaseInCompleteClass(CGF.LoadCXXThisAddress(), DerivedClass, BaseClass, BaseIsVirtual); CGF.EmitCXXDestructorCall(D, Dtor_Base, BaseIsVirtual, /*Delegating=*/false, Addr, ThisTy); } }; /// A visitor which checks whether an initializer uses 'this' in a /// way which requires the vtable to be properly set. struct DynamicThisUseChecker : ConstEvaluatedExprVisitor { typedef ConstEvaluatedExprVisitor super; bool UsesThis; DynamicThisUseChecker(const ASTContext &C) : super(C), UsesThis(false) {} // Black-list all explicit and implicit references to 'this'. // // Do we need to worry about external references to 'this' derived // from arbitrary code? If so, then anything which runs arbitrary // external code might potentially access the vtable. void VisitCXXThisExpr(const CXXThisExpr *E) { UsesThis = true; } }; } // end anonymous namespace static bool BaseInitializerUsesThis(ASTContext &C, const Expr *Init) { DynamicThisUseChecker Checker(C); Checker.Visit(Init); return Checker.UsesThis; } static void EmitBaseInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *BaseInit) { assert(BaseInit->isBaseInitializer() && "Must have base initializer!"); Address ThisPtr = CGF.LoadCXXThisAddress(); const Type *BaseType = BaseInit->getBaseClass(); const auto *BaseClassDecl = cast(BaseType->castAs()->getDecl()); bool isBaseVirtual = BaseInit->isBaseVirtual(); // If the initializer for the base (other than the constructor // itself) accesses 'this' in any way, we need to initialize the // vtables. if (BaseInitializerUsesThis(CGF.getContext(), BaseInit->getInit())) CGF.InitializeVTablePointers(ClassDecl); // We can pretend to be a complete class because it only matters for // virtual bases, and we only do virtual bases for complete ctors. Address V = CGF.GetAddressOfDirectBaseInCompleteClass(ThisPtr, ClassDecl, BaseClassDecl, isBaseVirtual); AggValueSlot AggSlot = AggValueSlot::forAddr( V, Qualifiers(), AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased, CGF.getOverlapForBaseInit(ClassDecl, BaseClassDecl, isBaseVirtual)); CGF.EmitAggExpr(BaseInit->getInit(), AggSlot); if (CGF.CGM.getLangOpts().Exceptions && !BaseClassDecl->hasTrivialDestructor()) CGF.EHStack.pushCleanup(EHCleanup, BaseClassDecl, isBaseVirtual); } static bool isMemcpyEquivalentSpecialMember(const CXXMethodDecl *D) { auto *CD = dyn_cast(D); if (!(CD && CD->isCopyOrMoveConstructor()) && !D->isCopyAssignmentOperator() && !D->isMoveAssignmentOperator()) return false; // We can emit a memcpy for a trivial copy or move constructor/assignment. if (D->isTrivial() && !D->getParent()->mayInsertExtraPadding()) return true; // We *must* emit a memcpy for a defaulted union copy or move op. if (D->getParent()->isUnion() && D->isDefaulted()) return true; return false; } static void EmitLValueForAnyFieldInitialization(CodeGenFunction &CGF, CXXCtorInitializer *MemberInit, LValue &LHS) { FieldDecl *Field = MemberInit->getAnyMember(); if (MemberInit->isIndirectMemberInitializer()) { // If we are initializing an anonymous union field, drill down to the field. IndirectFieldDecl *IndirectField = MemberInit->getIndirectMember(); for (const auto *I : IndirectField->chain()) LHS = CGF.EmitLValueForFieldInitialization(LHS, cast(I)); } else { LHS = CGF.EmitLValueForFieldInitialization(LHS, Field); } } static void EmitMemberInitializer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, CXXCtorInitializer *MemberInit, const CXXConstructorDecl *Constructor, FunctionArgList &Args) { ApplyDebugLocation Loc(CGF, MemberInit->getSourceLocation()); assert(MemberInit->isAnyMemberInitializer() && "Must have member initializer!"); assert(MemberInit->getInit() && "Must have initializer!"); // non-static data member initializers. FieldDecl *Field = MemberInit->getAnyMember(); QualType FieldType = Field->getType(); llvm::Value *ThisPtr = CGF.LoadCXXThis(); QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); LValue LHS; // If a base constructor is being emitted, create an LValue that has the // non-virtual alignment. if (CGF.CurGD.getCtorType() == Ctor_Base) LHS = CGF.MakeNaturalAlignPointeeAddrLValue(ThisPtr, RecordTy); else LHS = CGF.MakeNaturalAlignAddrLValue(ThisPtr, RecordTy); EmitLValueForAnyFieldInitialization(CGF, MemberInit, LHS); // Special case: if we are in a copy or move constructor, and we are copying // an array of PODs or classes with trivial copy constructors, ignore the // AST and perform the copy we know is equivalent. // FIXME: This is hacky at best... if we had a bit more explicit information // in the AST, we could generalize it more easily. const ConstantArrayType *Array = CGF.getContext().getAsConstantArrayType(FieldType); if (Array && Constructor->isDefaulted() && Constructor->isCopyOrMoveConstructor()) { QualType BaseElementTy = CGF.getContext().getBaseElementType(Array); CXXConstructExpr *CE = dyn_cast(MemberInit->getInit()); if (BaseElementTy.isPODType(CGF.getContext()) || (CE && isMemcpyEquivalentSpecialMember(CE->getConstructor()))) { unsigned SrcArgIndex = CGF.CGM.getCXXABI().getSrcArgforCopyCtor(Constructor, Args); llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(Args[SrcArgIndex])); LValue ThisRHSLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); LValue Src = CGF.EmitLValueForFieldInitialization(ThisRHSLV, Field); // Copy the aggregate. CGF.EmitAggregateCopy(LHS, Src, FieldType, CGF.getOverlapForFieldInit(Field), LHS.isVolatileQualified()); // Ensure that we destroy the objects if an exception is thrown later in // the constructor. QualType::DestructionKind dtorKind = FieldType.isDestructedType(); if (CGF.needsEHCleanup(dtorKind)) CGF.pushEHDestroy(dtorKind, LHS.getAddress(CGF), FieldType); return; } } CGF.EmitInitializerForField(Field, LHS, MemberInit->getInit()); } void CodeGenFunction::EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init) { QualType FieldType = Field->getType(); switch (getEvaluationKind(FieldType)) { case TEK_Scalar: if (LHS.isSimple()) { EmitExprAsInit(Init, Field, LHS, false); } else { RValue RHS = RValue::get(EmitScalarExpr(Init)); EmitStoreThroughLValue(RHS, LHS); } break; case TEK_Complex: EmitComplexExprIntoLValue(Init, LHS, /*isInit*/ true); break; case TEK_Aggregate: { AggValueSlot Slot = AggValueSlot::forLValue( LHS, *this, AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased, getOverlapForFieldInit(Field), AggValueSlot::IsNotZeroed, // Checks are made by the code that calls constructor. AggValueSlot::IsSanitizerChecked); EmitAggExpr(Init, Slot); break; } } // Ensure that we destroy this object if an exception is thrown // later in the constructor. QualType::DestructionKind dtorKind = FieldType.isDestructedType(); if (needsEHCleanup(dtorKind)) pushEHDestroy(dtorKind, LHS.getAddress(*this), FieldType); } /// Checks whether the given constructor is a valid subject for the /// complete-to-base constructor delegation optimization, i.e. /// emitting the complete constructor as a simple call to the base /// constructor. bool CodeGenFunction::IsConstructorDelegationValid( const CXXConstructorDecl *Ctor) { // Currently we disable the optimization for classes with virtual // bases because (1) the addresses of parameter variables need to be // consistent across all initializers but (2) the delegate function // call necessarily creates a second copy of the parameter variable. // // The limiting example (purely theoretical AFAIK): // struct A { A(int &c) { c++; } }; // struct B : virtual A { // B(int count) : A(count) { printf("%d\n", count); } // }; // ...although even this example could in principle be emitted as a // delegation since the address of the parameter doesn't escape. if (Ctor->getParent()->getNumVBases()) { // TODO: white-list trivial vbase initializers. This case wouldn't // be subject to the restrictions below. // TODO: white-list cases where: // - there are no non-reference parameters to the constructor // - the initializers don't access any non-reference parameters // - the initializers don't take the address of non-reference // parameters // - etc. // If we ever add any of the above cases, remember that: // - function-try-blocks will always blacklist this optimization // - we need to perform the constructor prologue and cleanup in // EmitConstructorBody. return false; } // We also disable the optimization for variadic functions because // it's impossible to "re-pass" varargs. if (Ctor->getType()->castAs()->isVariadic()) return false; // FIXME: Decide if we can do a delegation of a delegating constructor. if (Ctor->isDelegatingConstructor()) return false; return true; } // Emit code in ctor (Prologue==true) or dtor (Prologue==false) // to poison the extra field paddings inserted under // -fsanitize-address-field-padding=1|2. void CodeGenFunction::EmitAsanPrologueOrEpilogue(bool Prologue) { ASTContext &Context = getContext(); const CXXRecordDecl *ClassDecl = Prologue ? cast(CurGD.getDecl())->getParent() : cast(CurGD.getDecl())->getParent(); if (!ClassDecl->mayInsertExtraPadding()) return; struct SizeAndOffset { uint64_t Size; uint64_t Offset; }; unsigned PtrSize = CGM.getDataLayout().getPointerSizeInBits(); const ASTRecordLayout &Info = Context.getASTRecordLayout(ClassDecl); // Populate sizes and offsets of fields. SmallVector SSV(Info.getFieldCount()); for (unsigned i = 0, e = Info.getFieldCount(); i != e; ++i) SSV[i].Offset = Context.toCharUnitsFromBits(Info.getFieldOffset(i)).getQuantity(); size_t NumFields = 0; for (const auto *Field : ClassDecl->fields()) { const FieldDecl *D = Field; std::pair FieldInfo = Context.getTypeInfoInChars(D->getType()); CharUnits FieldSize = FieldInfo.first; assert(NumFields < SSV.size()); SSV[NumFields].Size = D->isBitField() ? 0 : FieldSize.getQuantity(); NumFields++; } assert(NumFields == SSV.size()); if (SSV.size() <= 1) return; // We will insert calls to __asan_* run-time functions. // LLVM AddressSanitizer pass may decide to inline them later. llvm::Type *Args[2] = {IntPtrTy, IntPtrTy}; llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, Args, false); llvm::FunctionCallee F = CGM.CreateRuntimeFunction( FTy, Prologue ? "__asan_poison_intra_object_redzone" : "__asan_unpoison_intra_object_redzone"); llvm::Value *ThisPtr = LoadCXXThis(); ThisPtr = Builder.CreatePtrToInt(ThisPtr, IntPtrTy); uint64_t TypeSize = Info.getNonVirtualSize().getQuantity(); // For each field check if it has sufficient padding, // if so (un)poison it with a call. for (size_t i = 0; i < SSV.size(); i++) { uint64_t AsanAlignment = 8; uint64_t NextField = i == SSV.size() - 1 ? TypeSize : SSV[i + 1].Offset; uint64_t PoisonSize = NextField - SSV[i].Offset - SSV[i].Size; uint64_t EndOffset = SSV[i].Offset + SSV[i].Size; if (PoisonSize < AsanAlignment || !SSV[i].Size || (NextField % AsanAlignment) != 0) continue; Builder.CreateCall( F, {Builder.CreateAdd(ThisPtr, Builder.getIntN(PtrSize, EndOffset)), Builder.getIntN(PtrSize, PoisonSize)}); } } /// EmitConstructorBody - Emits the body of the current constructor. void CodeGenFunction::EmitConstructorBody(FunctionArgList &Args) { EmitAsanPrologueOrEpilogue(true); const CXXConstructorDecl *Ctor = cast(CurGD.getDecl()); CXXCtorType CtorType = CurGD.getCtorType(); assert((CGM.getTarget().getCXXABI().hasConstructorVariants() || CtorType == Ctor_Complete) && "can only generate complete ctor for this ABI"); // Before we go any further, try the complete->base constructor // delegation optimization. if (CtorType == Ctor_Complete && IsConstructorDelegationValid(Ctor) && CGM.getTarget().getCXXABI().hasConstructorVariants()) { EmitDelegateCXXConstructorCall(Ctor, Ctor_Base, Args, Ctor->getEndLoc()); return; } const FunctionDecl *Definition = nullptr; Stmt *Body = Ctor->getBody(Definition); assert(Definition == Ctor && "emitting wrong constructor body"); // Enter the function-try-block before the constructor prologue if // applicable. bool IsTryBody = (Body && isa(Body)); if (IsTryBody) EnterCXXTryStmt(*cast(Body), true); incrementProfileCounter(Body); RunCleanupsScope RunCleanups(*this); // TODO: in restricted cases, we can emit the vbase initializers of // a complete ctor and then delegate to the base ctor. // Emit the constructor prologue, i.e. the base and member // initializers. EmitCtorPrologue(Ctor, CtorType, Args); // Emit the body of the statement. if (IsTryBody) EmitStmt(cast(Body)->getTryBlock()); else if (Body) EmitStmt(Body); // Emit any cleanup blocks associated with the member or base // initializers, which includes (along the exceptional path) the // destructors for those members and bases that were fully // constructed. RunCleanups.ForceCleanup(); if (IsTryBody) ExitCXXTryStmt(*cast(Body), true); } namespace { /// RAII object to indicate that codegen is copying the value representation /// instead of the object representation. Useful when copying a struct or /// class which has uninitialized members and we're only performing /// lvalue-to-rvalue conversion on the object but not its members. class CopyingValueRepresentation { public: explicit CopyingValueRepresentation(CodeGenFunction &CGF) : CGF(CGF), OldSanOpts(CGF.SanOpts) { CGF.SanOpts.set(SanitizerKind::Bool, false); CGF.SanOpts.set(SanitizerKind::Enum, false); } ~CopyingValueRepresentation() { CGF.SanOpts = OldSanOpts; } private: CodeGenFunction &CGF; SanitizerSet OldSanOpts; }; } // end anonymous namespace namespace { class FieldMemcpyizer { public: FieldMemcpyizer(CodeGenFunction &CGF, const CXXRecordDecl *ClassDecl, const VarDecl *SrcRec) : CGF(CGF), ClassDecl(ClassDecl), SrcRec(SrcRec), RecLayout(CGF.getContext().getASTRecordLayout(ClassDecl)), FirstField(nullptr), LastField(nullptr), FirstFieldOffset(0), LastFieldOffset(0), LastAddedFieldIndex(0) {} bool isMemcpyableField(FieldDecl *F) const { // Never memcpy fields when we are adding poisoned paddings. if (CGF.getContext().getLangOpts().SanitizeAddressFieldPadding) return false; Qualifiers Qual = F->getType().getQualifiers(); if (Qual.hasVolatile() || Qual.hasObjCLifetime()) return false; return true; } void addMemcpyableField(FieldDecl *F) { if (F->isZeroSize(CGF.getContext())) return; if (!FirstField) addInitialField(F); else addNextField(F); } CharUnits getMemcpySize(uint64_t FirstByteOffset) const { ASTContext &Ctx = CGF.getContext(); unsigned LastFieldSize = LastField->isBitField() ? LastField->getBitWidthValue(Ctx) : Ctx.toBits( Ctx.getTypeInfoDataSizeInChars(LastField->getType()).first); uint64_t MemcpySizeBits = LastFieldOffset + LastFieldSize - FirstByteOffset + Ctx.getCharWidth() - 1; CharUnits MemcpySize = Ctx.toCharUnitsFromBits(MemcpySizeBits); return MemcpySize; } void emitMemcpy() { // Give the subclass a chance to bail out if it feels the memcpy isn't // worth it (e.g. Hasn't aggregated enough data). if (!FirstField) { return; } uint64_t FirstByteOffset; if (FirstField->isBitField()) { const CGRecordLayout &RL = CGF.getTypes().getCGRecordLayout(FirstField->getParent()); const CGBitFieldInfo &BFInfo = RL.getBitFieldInfo(FirstField); // FirstFieldOffset is not appropriate for bitfields, // we need to use the storage offset instead. FirstByteOffset = CGF.getContext().toBits(BFInfo.StorageOffset); } else { FirstByteOffset = FirstFieldOffset; } CharUnits MemcpySize = getMemcpySize(FirstByteOffset); QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); Address ThisPtr = CGF.LoadCXXThisAddress(); LValue DestLV = CGF.MakeAddrLValue(ThisPtr, RecordTy); LValue Dest = CGF.EmitLValueForFieldInitialization(DestLV, FirstField); llvm::Value *SrcPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar(SrcRec)); LValue SrcLV = CGF.MakeNaturalAlignAddrLValue(SrcPtr, RecordTy); LValue Src = CGF.EmitLValueForFieldInitialization(SrcLV, FirstField); emitMemcpyIR( Dest.isBitField() ? Dest.getBitFieldAddress() : Dest.getAddress(CGF), Src.isBitField() ? Src.getBitFieldAddress() : Src.getAddress(CGF), MemcpySize); reset(); } void reset() { FirstField = nullptr; } protected: CodeGenFunction &CGF; const CXXRecordDecl *ClassDecl; private: void emitMemcpyIR(Address DestPtr, Address SrcPtr, CharUnits Size) { llvm::PointerType *DPT = DestPtr.getType(); llvm::Type *DBP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), DPT->getAddressSpace()); DestPtr = CGF.Builder.CreateBitCast(DestPtr, DBP); llvm::PointerType *SPT = SrcPtr.getType(); llvm::Type *SBP = llvm::Type::getInt8PtrTy(CGF.getLLVMContext(), SPT->getAddressSpace()); SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, SBP); CGF.Builder.CreateMemCpy(DestPtr, SrcPtr, Size.getQuantity()); } void addInitialField(FieldDecl *F) { FirstField = F; LastField = F; FirstFieldOffset = RecLayout.getFieldOffset(F->getFieldIndex()); LastFieldOffset = FirstFieldOffset; LastAddedFieldIndex = F->getFieldIndex(); } void addNextField(FieldDecl *F) { // For the most part, the following invariant will hold: // F->getFieldIndex() == LastAddedFieldIndex + 1 // The one exception is that Sema won't add a copy-initializer for an // unnamed bitfield, which will show up here as a gap in the sequence. assert(F->getFieldIndex() >= LastAddedFieldIndex + 1 && "Cannot aggregate fields out of order."); LastAddedFieldIndex = F->getFieldIndex(); // The 'first' and 'last' fields are chosen by offset, rather than field // index. This allows the code to support bitfields, as well as regular // fields. uint64_t FOffset = RecLayout.getFieldOffset(F->getFieldIndex()); if (FOffset < FirstFieldOffset) { FirstField = F; FirstFieldOffset = FOffset; } else if (FOffset >= LastFieldOffset) { LastField = F; LastFieldOffset = FOffset; } } const VarDecl *SrcRec; const ASTRecordLayout &RecLayout; FieldDecl *FirstField; FieldDecl *LastField; uint64_t FirstFieldOffset, LastFieldOffset; unsigned LastAddedFieldIndex; }; class ConstructorMemcpyizer : public FieldMemcpyizer { private: /// Get source argument for copy constructor. Returns null if not a copy /// constructor. static const VarDecl *getTrivialCopySource(CodeGenFunction &CGF, const CXXConstructorDecl *CD, FunctionArgList &Args) { if (CD->isCopyOrMoveConstructor() && CD->isDefaulted()) return Args[CGF.CGM.getCXXABI().getSrcArgforCopyCtor(CD, Args)]; return nullptr; } // Returns true if a CXXCtorInitializer represents a member initialization // that can be rolled into a memcpy. bool isMemberInitMemcpyable(CXXCtorInitializer *MemberInit) const { if (!MemcpyableCtor) return false; FieldDecl *Field = MemberInit->getMember(); assert(Field && "No field for member init."); QualType FieldType = Field->getType(); CXXConstructExpr *CE = dyn_cast(MemberInit->getInit()); // Bail out on non-memcpyable, not-trivially-copyable members. if (!(CE && isMemcpyEquivalentSpecialMember(CE->getConstructor())) && !(FieldType.isTriviallyCopyableType(CGF.getContext()) || FieldType->isReferenceType())) return false; // Bail out on volatile fields. if (!isMemcpyableField(Field)) return false; // Otherwise we're good. return true; } public: ConstructorMemcpyizer(CodeGenFunction &CGF, const CXXConstructorDecl *CD, FunctionArgList &Args) : FieldMemcpyizer(CGF, CD->getParent(), getTrivialCopySource(CGF, CD, Args)), ConstructorDecl(CD), MemcpyableCtor(CD->isDefaulted() && CD->isCopyOrMoveConstructor() && CGF.getLangOpts().getGC() == LangOptions::NonGC), Args(Args) { } void addMemberInitializer(CXXCtorInitializer *MemberInit) { if (isMemberInitMemcpyable(MemberInit)) { AggregatedInits.push_back(MemberInit); addMemcpyableField(MemberInit->getMember()); } else { emitAggregatedInits(); EmitMemberInitializer(CGF, ConstructorDecl->getParent(), MemberInit, ConstructorDecl, Args); } } void emitAggregatedInits() { if (AggregatedInits.size() <= 1) { // This memcpy is too small to be worthwhile. Fall back on default // codegen. if (!AggregatedInits.empty()) { CopyingValueRepresentation CVR(CGF); EmitMemberInitializer(CGF, ConstructorDecl->getParent(), AggregatedInits[0], ConstructorDecl, Args); AggregatedInits.clear(); } reset(); return; } pushEHDestructors(); emitMemcpy(); AggregatedInits.clear(); } void pushEHDestructors() { Address ThisPtr = CGF.LoadCXXThisAddress(); QualType RecordTy = CGF.getContext().getTypeDeclType(ClassDecl); LValue LHS = CGF.MakeAddrLValue(ThisPtr, RecordTy); for (unsigned i = 0; i < AggregatedInits.size(); ++i) { CXXCtorInitializer *MemberInit = AggregatedInits[i]; QualType FieldType = MemberInit->getAnyMember()->getType(); QualType::DestructionKind dtorKind = FieldType.isDestructedType(); if (!CGF.needsEHCleanup(dtorKind)) continue; LValue FieldLHS = LHS; EmitLValueForAnyFieldInitialization(CGF, MemberInit, FieldLHS); CGF.pushEHDestroy(dtorKind, FieldLHS.getAddress(CGF), FieldType); } } void finish() { emitAggregatedInits(); } private: const CXXConstructorDecl *ConstructorDecl; bool MemcpyableCtor; FunctionArgList &Args; SmallVector AggregatedInits; }; class AssignmentMemcpyizer : public FieldMemcpyizer { private: // Returns the memcpyable field copied by the given statement, if one // exists. Otherwise returns null. FieldDecl *getMemcpyableField(Stmt *S) { if (!AssignmentsMemcpyable) return nullptr; if (BinaryOperator *BO = dyn_cast(S)) { // Recognise trivial assignments. if (BO->getOpcode() != BO_Assign) return nullptr; MemberExpr *ME = dyn_cast(BO->getLHS()); if (!ME) return nullptr; FieldDecl *Field = dyn_cast(ME->getMemberDecl()); if (!Field || !isMemcpyableField(Field)) return nullptr; Stmt *RHS = BO->getRHS(); if (ImplicitCastExpr *EC = dyn_cast(RHS)) RHS = EC->getSubExpr(); if (!RHS) return nullptr; if (MemberExpr *ME2 = dyn_cast(RHS)) { if (ME2->getMemberDecl() == Field) return Field; } return nullptr; } else if (CXXMemberCallExpr *MCE = dyn_cast(S)) { CXXMethodDecl *MD = dyn_cast(MCE->getCalleeDecl()); if (!(MD && isMemcpyEquivalentSpecialMember(MD))) return nullptr; MemberExpr *IOA = dyn_cast(MCE->getImplicitObjectArgument()); if (!IOA) return nullptr; FieldDecl *Field = dyn_cast(IOA->getMemberDecl()); if (!Field || !isMemcpyableField(Field)) return nullptr; MemberExpr *Arg0 = dyn_cast(MCE->getArg(0)); if (!Arg0 || Field != dyn_cast(Arg0->getMemberDecl())) return nullptr; return Field; } else if (CallExpr *CE = dyn_cast(S)) { FunctionDecl *FD = dyn_cast(CE->getCalleeDecl()); if (!FD || FD->getBuiltinID() != Builtin::BI__builtin_memcpy) return nullptr; Expr *DstPtr = CE->getArg(0); if (ImplicitCastExpr *DC = dyn_cast(DstPtr)) DstPtr = DC->getSubExpr(); UnaryOperator *DUO = dyn_cast(DstPtr); if (!DUO || DUO->getOpcode() != UO_AddrOf) return nullptr; MemberExpr *ME = dyn_cast(DUO->getSubExpr()); if (!ME) return nullptr; FieldDecl *Field = dyn_cast(ME->getMemberDecl()); if (!Field || !isMemcpyableField(Field)) return nullptr; Expr *SrcPtr = CE->getArg(1); if (ImplicitCastExpr *SC = dyn_cast(SrcPtr)) SrcPtr = SC->getSubExpr(); UnaryOperator *SUO = dyn_cast(SrcPtr); if (!SUO || SUO->getOpcode() != UO_AddrOf) return nullptr; MemberExpr *ME2 = dyn_cast(SUO->getSubExpr()); if (!ME2 || Field != dyn_cast(ME2->getMemberDecl())) return nullptr; return Field; } return nullptr; } bool AssignmentsMemcpyable; SmallVector AggregatedStmts; public: AssignmentMemcpyizer(CodeGenFunction &CGF, const CXXMethodDecl *AD, FunctionArgList &Args) : FieldMemcpyizer(CGF, AD->getParent(), Args[Args.size() - 1]), AssignmentsMemcpyable(CGF.getLangOpts().getGC() == LangOptions::NonGC) { assert(Args.size() == 2); } void emitAssignment(Stmt *S) { FieldDecl *F = getMemcpyableField(S); if (F) { addMemcpyableField(F); AggregatedStmts.push_back(S); } else { emitAggregatedStmts(); CGF.EmitStmt(S); } } void emitAggregatedStmts() { if (AggregatedStmts.size() <= 1) { if (!AggregatedStmts.empty()) { CopyingValueRepresentation CVR(CGF); CGF.EmitStmt(AggregatedStmts[0]); } reset(); } emitMemcpy(); AggregatedStmts.clear(); } void finish() { emitAggregatedStmts(); } }; } // end anonymous namespace static bool isInitializerOfDynamicClass(const CXXCtorInitializer *BaseInit) { const Type *BaseType = BaseInit->getBaseClass(); const auto *BaseClassDecl = cast(BaseType->castAs()->getDecl()); return BaseClassDecl->isDynamicClass(); } /// EmitCtorPrologue - This routine generates necessary code to initialize /// base classes and non-static data members belonging to this constructor. void CodeGenFunction::EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType CtorType, FunctionArgList &Args) { if (CD->isDelegatingConstructor()) return EmitDelegatingCXXConstructorCall(CD, Args); const CXXRecordDecl *ClassDecl = CD->getParent(); CXXConstructorDecl::init_const_iterator B = CD->init_begin(), E = CD->init_end(); // Virtual base initializers first, if any. They aren't needed if: // - This is a base ctor variant // - There are no vbases // - The class is abstract, so a complete object of it cannot be constructed // // The check for an abstract class is necessary because sema may not have // marked virtual base destructors referenced. bool ConstructVBases = CtorType != Ctor_Base && ClassDecl->getNumVBases() != 0 && !ClassDecl->isAbstract(); // In the Microsoft C++ ABI, there are no constructor variants. Instead, the // constructor of a class with virtual bases takes an additional parameter to // conditionally construct the virtual bases. Emit that check here. llvm::BasicBlock *BaseCtorContinueBB = nullptr; if (ConstructVBases && !CGM.getTarget().getCXXABI().hasConstructorVariants()) { BaseCtorContinueBB = CGM.getCXXABI().EmitCtorCompleteObjectHandler(*this, ClassDecl); assert(BaseCtorContinueBB); } llvm::Value *const OldThis = CXXThisValue; for (; B != E && (*B)->isBaseInitializer() && (*B)->isBaseVirtual(); B++) { if (!ConstructVBases) continue; if (CGM.getCodeGenOpts().StrictVTablePointers && CGM.getCodeGenOpts().OptimizationLevel > 0 && isInitializerOfDynamicClass(*B)) CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis()); EmitBaseInitializer(*this, ClassDecl, *B); } if (BaseCtorContinueBB) { // Complete object handler should continue to the remaining initializers. Builder.CreateBr(BaseCtorContinueBB); EmitBlock(BaseCtorContinueBB); } // Then, non-virtual base initializers. for (; B != E && (*B)->isBaseInitializer(); B++) { assert(!(*B)->isBaseVirtual()); if (CGM.getCodeGenOpts().StrictVTablePointers && CGM.getCodeGenOpts().OptimizationLevel > 0 && isInitializerOfDynamicClass(*B)) CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis()); EmitBaseInitializer(*this, ClassDecl, *B); } CXXThisValue = OldThis; InitializeVTablePointers(ClassDecl); // And finally, initialize class members. FieldConstructionScope FCS(*this, LoadCXXThisAddress()); ConstructorMemcpyizer CM(*this, CD, Args); for (; B != E; B++) { CXXCtorInitializer *Member = (*B); assert(!Member->isBaseInitializer()); assert(Member->isAnyMemberInitializer() && "Delegating initializer on non-delegating constructor"); CM.addMemberInitializer(Member); } CM.finish(); } static bool FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field); static bool HasTrivialDestructorBody(ASTContext &Context, const CXXRecordDecl *BaseClassDecl, const CXXRecordDecl *MostDerivedClassDecl) { // If the destructor is trivial we don't have to check anything else. if (BaseClassDecl->hasTrivialDestructor()) return true; if (!BaseClassDecl->getDestructor()->hasTrivialBody()) return false; // Check fields. for (const auto *Field : BaseClassDecl->fields()) if (!FieldHasTrivialDestructorBody(Context, Field)) return false; // Check non-virtual bases. for (const auto &I : BaseClassDecl->bases()) { if (I.isVirtual()) continue; const CXXRecordDecl *NonVirtualBase = cast(I.getType()->castAs()->getDecl()); if (!HasTrivialDestructorBody(Context, NonVirtualBase, MostDerivedClassDecl)) return false; } if (BaseClassDecl == MostDerivedClassDecl) { // Check virtual bases. for (const auto &I : BaseClassDecl->vbases()) { const CXXRecordDecl *VirtualBase = cast(I.getType()->castAs()->getDecl()); if (!HasTrivialDestructorBody(Context, VirtualBase, MostDerivedClassDecl)) return false; } } return true; } static bool FieldHasTrivialDestructorBody(ASTContext &Context, const FieldDecl *Field) { QualType FieldBaseElementType = Context.getBaseElementType(Field->getType()); const RecordType *RT = FieldBaseElementType->getAs(); if (!RT) return true; CXXRecordDecl *FieldClassDecl = cast(RT->getDecl()); // The destructor for an implicit anonymous union member is never invoked. if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion()) return false; return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl); } /// CanSkipVTablePointerInitialization - Check whether we need to initialize /// any vtable pointers before calling this destructor. static bool CanSkipVTablePointerInitialization(CodeGenFunction &CGF, const CXXDestructorDecl *Dtor) { const CXXRecordDecl *ClassDecl = Dtor->getParent(); if (!ClassDecl->isDynamicClass()) return true; if (!Dtor->hasTrivialBody()) return false; // Check the fields. for (const auto *Field : ClassDecl->fields()) if (!FieldHasTrivialDestructorBody(CGF.getContext(), Field)) return false; return true; } /// EmitDestructorBody - Emits the body of the current destructor. void CodeGenFunction::EmitDestructorBody(FunctionArgList &Args) { const CXXDestructorDecl *Dtor = cast(CurGD.getDecl()); CXXDtorType DtorType = CurGD.getDtorType(); // For an abstract class, non-base destructors are never used (and can't // be emitted in general, because vbase dtors may not have been validated // by Sema), but the Itanium ABI doesn't make them optional and Clang may // in fact emit references to them from other compilations, so emit them // as functions containing a trap instruction. if (DtorType != Dtor_Base && Dtor->getParent()->isAbstract()) { llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); TrapCall->setDoesNotReturn(); TrapCall->setDoesNotThrow(); Builder.CreateUnreachable(); Builder.ClearInsertionPoint(); return; } Stmt *Body = Dtor->getBody(); if (Body) incrementProfileCounter(Body); // The call to operator delete in a deleting destructor happens // outside of the function-try-block, which means it's always // possible to delegate the destructor body to the complete // destructor. Do so. if (DtorType == Dtor_Deleting) { RunCleanupsScope DtorEpilogue(*this); EnterDtorCleanups(Dtor, Dtor_Deleting); if (HaveInsertPoint()) { QualType ThisTy = Dtor->getThisObjectType(); EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, /*Delegating=*/false, LoadCXXThisAddress(), ThisTy); } return; } // If the body is a function-try-block, enter the try before // anything else. bool isTryBody = (Body && isa(Body)); if (isTryBody) EnterCXXTryStmt(*cast(Body), true); EmitAsanPrologueOrEpilogue(false); // Enter the epilogue cleanups. RunCleanupsScope DtorEpilogue(*this); // If this is the complete variant, just invoke the base variant; // the epilogue will destruct the virtual bases. But we can't do // this optimization if the body is a function-try-block, because // we'd introduce *two* handler blocks. In the Microsoft ABI, we // always delegate because we might not have a definition in this TU. switch (DtorType) { case Dtor_Comdat: llvm_unreachable("not expecting a COMDAT"); case Dtor_Deleting: llvm_unreachable("already handled deleting case"); case Dtor_Complete: assert((Body || getTarget().getCXXABI().isMicrosoft()) && "can't emit a dtor without a body for non-Microsoft ABIs"); // Enter the cleanup scopes for virtual bases. EnterDtorCleanups(Dtor, Dtor_Complete); if (!isTryBody) { QualType ThisTy = Dtor->getThisObjectType(); EmitCXXDestructorCall(Dtor, Dtor_Base, /*ForVirtualBase=*/false, /*Delegating=*/false, LoadCXXThisAddress(), ThisTy); break; } // Fallthrough: act like we're in the base variant. LLVM_FALLTHROUGH; case Dtor_Base: assert(Body); // Enter the cleanup scopes for fields and non-virtual bases. EnterDtorCleanups(Dtor, Dtor_Base); // Initialize the vtable pointers before entering the body. if (!CanSkipVTablePointerInitialization(*this, Dtor)) { // Insert the llvm.launder.invariant.group intrinsic before initializing // the vptrs to cancel any previous assumptions we might have made. if (CGM.getCodeGenOpts().StrictVTablePointers && CGM.getCodeGenOpts().OptimizationLevel > 0) CXXThisValue = Builder.CreateLaunderInvariantGroup(LoadCXXThis()); InitializeVTablePointers(Dtor->getParent()); } if (isTryBody) EmitStmt(cast(Body)->getTryBlock()); else if (Body) EmitStmt(Body); else { assert(Dtor->isImplicit() && "bodyless dtor not implicit"); // nothing to do besides what's in the epilogue } // -fapple-kext must inline any call to this dtor into // the caller's body. if (getLangOpts().AppleKext) CurFn->addFnAttr(llvm::Attribute::AlwaysInline); break; } // Jump out through the epilogue cleanups. DtorEpilogue.ForceCleanup(); // Exit the try if applicable. if (isTryBody) ExitCXXTryStmt(*cast(Body), true); } void CodeGenFunction::emitImplicitAssignmentOperatorBody(FunctionArgList &Args) { const CXXMethodDecl *AssignOp = cast(CurGD.getDecl()); const Stmt *RootS = AssignOp->getBody(); assert(isa(RootS) && "Body of an implicit assignment operator should be compound stmt."); const CompoundStmt *RootCS = cast(RootS); LexicalScope Scope(*this, RootCS->getSourceRange()); incrementProfileCounter(RootCS); AssignmentMemcpyizer AM(*this, AssignOp, Args); for (auto *I : RootCS->body()) AM.emitAssignment(I); AM.finish(); } namespace { llvm::Value *LoadThisForDtorDelete(CodeGenFunction &CGF, const CXXDestructorDecl *DD) { if (Expr *ThisArg = DD->getOperatorDeleteThisArg()) return CGF.EmitScalarExpr(ThisArg); return CGF.LoadCXXThis(); } /// Call the operator delete associated with the current destructor. struct CallDtorDelete final : EHScopeStack::Cleanup { CallDtorDelete() {} void Emit(CodeGenFunction &CGF, Flags flags) override { const CXXDestructorDecl *Dtor = cast(CGF.CurCodeDecl); const CXXRecordDecl *ClassDecl = Dtor->getParent(); CGF.EmitDeleteCall(Dtor->getOperatorDelete(), LoadThisForDtorDelete(CGF, Dtor), CGF.getContext().getTagDeclType(ClassDecl)); } }; void EmitConditionalDtorDeleteCall(CodeGenFunction &CGF, llvm::Value *ShouldDeleteCondition, bool ReturnAfterDelete) { llvm::BasicBlock *callDeleteBB = CGF.createBasicBlock("dtor.call_delete"); llvm::BasicBlock *continueBB = CGF.createBasicBlock("dtor.continue"); llvm::Value *ShouldCallDelete = CGF.Builder.CreateIsNull(ShouldDeleteCondition); CGF.Builder.CreateCondBr(ShouldCallDelete, continueBB, callDeleteBB); CGF.EmitBlock(callDeleteBB); const CXXDestructorDecl *Dtor = cast(CGF.CurCodeDecl); const CXXRecordDecl *ClassDecl = Dtor->getParent(); CGF.EmitDeleteCall(Dtor->getOperatorDelete(), LoadThisForDtorDelete(CGF, Dtor), CGF.getContext().getTagDeclType(ClassDecl)); assert(Dtor->getOperatorDelete()->isDestroyingOperatorDelete() == ReturnAfterDelete && "unexpected value for ReturnAfterDelete"); if (ReturnAfterDelete) CGF.EmitBranchThroughCleanup(CGF.ReturnBlock); else CGF.Builder.CreateBr(continueBB); CGF.EmitBlock(continueBB); } struct CallDtorDeleteConditional final : EHScopeStack::Cleanup { llvm::Value *ShouldDeleteCondition; public: CallDtorDeleteConditional(llvm::Value *ShouldDeleteCondition) : ShouldDeleteCondition(ShouldDeleteCondition) { assert(ShouldDeleteCondition != nullptr); } void Emit(CodeGenFunction &CGF, Flags flags) override { EmitConditionalDtorDeleteCall(CGF, ShouldDeleteCondition, /*ReturnAfterDelete*/false); } }; class DestroyField final : public EHScopeStack::Cleanup { const FieldDecl *field; CodeGenFunction::Destroyer *destroyer; bool useEHCleanupForArray; public: DestroyField(const FieldDecl *field, CodeGenFunction::Destroyer *destroyer, bool useEHCleanupForArray) : field(field), destroyer(destroyer), useEHCleanupForArray(useEHCleanupForArray) {} void Emit(CodeGenFunction &CGF, Flags flags) override { // Find the address of the field. Address thisValue = CGF.LoadCXXThisAddress(); QualType RecordTy = CGF.getContext().getTagDeclType(field->getParent()); LValue ThisLV = CGF.MakeAddrLValue(thisValue, RecordTy); LValue LV = CGF.EmitLValueForField(ThisLV, field); assert(LV.isSimple()); CGF.emitDestroy(LV.getAddress(CGF), field->getType(), destroyer, flags.isForNormalCleanup() && useEHCleanupForArray); } }; static void EmitSanitizerDtorCallback(CodeGenFunction &CGF, llvm::Value *Ptr, CharUnits::QuantityType PoisonSize) { CodeGenFunction::SanitizerScope SanScope(&CGF); // Pass in void pointer and size of region as arguments to runtime // function llvm::Value *Args[] = {CGF.Builder.CreateBitCast(Ptr, CGF.VoidPtrTy), llvm::ConstantInt::get(CGF.SizeTy, PoisonSize)}; llvm::Type *ArgTypes[] = {CGF.VoidPtrTy, CGF.SizeTy}; llvm::FunctionType *FnType = llvm::FunctionType::get(CGF.VoidTy, ArgTypes, false); llvm::FunctionCallee Fn = CGF.CGM.CreateRuntimeFunction(FnType, "__sanitizer_dtor_callback"); CGF.EmitNounwindRuntimeCall(Fn, Args); } class SanitizeDtorMembers final : public EHScopeStack::Cleanup { const CXXDestructorDecl *Dtor; public: SanitizeDtorMembers(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {} // Generate function call for handling object poisoning. // Disables tail call elimination, to prevent the current stack frame // from disappearing from the stack trace. void Emit(CodeGenFunction &CGF, Flags flags) override { const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Dtor->getParent()); // Nothing to poison. if (Layout.getFieldCount() == 0) return; // Prevent the current stack frame from disappearing from the stack trace. CGF.CurFn->addFnAttr("disable-tail-calls", "true"); // Construct pointer to region to begin poisoning, and calculate poison // size, so that only members declared in this class are poisoned. ASTContext &Context = CGF.getContext(); unsigned fieldIndex = 0; int startIndex = -1; // RecordDecl::field_iterator Field; for (const FieldDecl *Field : Dtor->getParent()->fields()) { // Poison field if it is trivial if (FieldHasTrivialDestructorBody(Context, Field)) { // Start sanitizing at this field if (startIndex < 0) startIndex = fieldIndex; // Currently on the last field, and it must be poisoned with the // current block. if (fieldIndex == Layout.getFieldCount() - 1) { PoisonMembers(CGF, startIndex, Layout.getFieldCount()); } } else if (startIndex >= 0) { // No longer within a block of memory to poison, so poison the block PoisonMembers(CGF, startIndex, fieldIndex); // Re-set the start index startIndex = -1; } fieldIndex += 1; } } private: /// \param layoutStartOffset index of the ASTRecordLayout field to /// start poisoning (inclusive) /// \param layoutEndOffset index of the ASTRecordLayout field to /// end poisoning (exclusive) void PoisonMembers(CodeGenFunction &CGF, unsigned layoutStartOffset, unsigned layoutEndOffset) { ASTContext &Context = CGF.getContext(); const ASTRecordLayout &Layout = Context.getASTRecordLayout(Dtor->getParent()); llvm::ConstantInt *OffsetSizePtr = llvm::ConstantInt::get( CGF.SizeTy, Context.toCharUnitsFromBits(Layout.getFieldOffset(layoutStartOffset)) .getQuantity()); llvm::Value *OffsetPtr = CGF.Builder.CreateGEP( CGF.Builder.CreateBitCast(CGF.LoadCXXThis(), CGF.Int8PtrTy), OffsetSizePtr); CharUnits::QuantityType PoisonSize; if (layoutEndOffset >= Layout.getFieldCount()) { PoisonSize = Layout.getNonVirtualSize().getQuantity() - Context.toCharUnitsFromBits( Layout.getFieldOffset(layoutStartOffset)) .getQuantity(); } else { PoisonSize = Context.toCharUnitsFromBits( Layout.getFieldOffset(layoutEndOffset) - Layout.getFieldOffset(layoutStartOffset)) .getQuantity(); } if (PoisonSize == 0) return; EmitSanitizerDtorCallback(CGF, OffsetPtr, PoisonSize); } }; class SanitizeDtorVTable final : public EHScopeStack::Cleanup { const CXXDestructorDecl *Dtor; public: SanitizeDtorVTable(const CXXDestructorDecl *Dtor) : Dtor(Dtor) {} // Generate function call for handling vtable pointer poisoning. void Emit(CodeGenFunction &CGF, Flags flags) override { assert(Dtor->getParent()->isDynamicClass()); (void)Dtor; ASTContext &Context = CGF.getContext(); // Poison vtable and vtable ptr if they exist for this class. llvm::Value *VTablePtr = CGF.LoadCXXThis(); CharUnits::QuantityType PoisonSize = Context.toCharUnitsFromBits(CGF.PointerWidthInBits).getQuantity(); // Pass in void pointer and size of region as arguments to runtime // function EmitSanitizerDtorCallback(CGF, VTablePtr, PoisonSize); } }; } // end anonymous namespace /// Emit all code that comes at the end of class's /// destructor. This is to call destructors on members and base classes /// in reverse order of their construction. /// /// For a deleting destructor, this also handles the case where a destroying /// operator delete completely overrides the definition. void CodeGenFunction::EnterDtorCleanups(const CXXDestructorDecl *DD, CXXDtorType DtorType) { assert((!DD->isTrivial() || DD->hasAttr()) && "Should not emit dtor epilogue for non-exported trivial dtor!"); // The deleting-destructor phase just needs to call the appropriate // operator delete that Sema picked up. if (DtorType == Dtor_Deleting) { assert(DD->getOperatorDelete() && "operator delete missing - EnterDtorCleanups"); if (CXXStructorImplicitParamValue) { // If there is an implicit param to the deleting dtor, it's a boolean // telling whether this is a deleting destructor. if (DD->getOperatorDelete()->isDestroyingOperatorDelete()) EmitConditionalDtorDeleteCall(*this, CXXStructorImplicitParamValue, /*ReturnAfterDelete*/true); else EHStack.pushCleanup( NormalAndEHCleanup, CXXStructorImplicitParamValue); } else { if (DD->getOperatorDelete()->isDestroyingOperatorDelete()) { const CXXRecordDecl *ClassDecl = DD->getParent(); EmitDeleteCall(DD->getOperatorDelete(), LoadThisForDtorDelete(*this, DD), getContext().getTagDeclType(ClassDecl)); EmitBranchThroughCleanup(ReturnBlock); } else { EHStack.pushCleanup(NormalAndEHCleanup); } } return; } const CXXRecordDecl *ClassDecl = DD->getParent(); // Unions have no bases and do not call field destructors. if (ClassDecl->isUnion()) return; // The complete-destructor phase just destructs all the virtual bases. if (DtorType == Dtor_Complete) { // Poison the vtable pointer such that access after the base // and member destructors are invoked is invalid. if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && SanOpts.has(SanitizerKind::Memory) && ClassDecl->getNumVBases() && ClassDecl->isPolymorphic()) EHStack.pushCleanup(NormalAndEHCleanup, DD); // We push them in the forward order so that they'll be popped in // the reverse order. for (const auto &Base : ClassDecl->vbases()) { auto *BaseClassDecl = cast(Base.getType()->castAs()->getDecl()); // Ignore trivial destructors. if (BaseClassDecl->hasTrivialDestructor()) continue; EHStack.pushCleanup(NormalAndEHCleanup, BaseClassDecl, /*BaseIsVirtual*/ true); } return; } assert(DtorType == Dtor_Base); // Poison the vtable pointer if it has no virtual bases, but inherits // virtual functions. if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && SanOpts.has(SanitizerKind::Memory) && !ClassDecl->getNumVBases() && ClassDecl->isPolymorphic()) EHStack.pushCleanup(NormalAndEHCleanup, DD); // Destroy non-virtual bases. for (const auto &Base : ClassDecl->bases()) { // Ignore virtual bases. if (Base.isVirtual()) continue; CXXRecordDecl *BaseClassDecl = Base.getType()->getAsCXXRecordDecl(); // Ignore trivial destructors. if (BaseClassDecl->hasTrivialDestructor()) continue; EHStack.pushCleanup(NormalAndEHCleanup, BaseClassDecl, /*BaseIsVirtual*/ false); } // Poison fields such that access after their destructors are // invoked, and before the base class destructor runs, is invalid. if (CGM.getCodeGenOpts().SanitizeMemoryUseAfterDtor && SanOpts.has(SanitizerKind::Memory)) EHStack.pushCleanup(NormalAndEHCleanup, DD); // Destroy direct fields. for (const auto *Field : ClassDecl->fields()) { QualType type = Field->getType(); QualType::DestructionKind dtorKind = type.isDestructedType(); if (!dtorKind) continue; // Anonymous union members do not have their destructors called. const RecordType *RT = type->getAsUnionType(); if (RT && RT->getDecl()->isAnonymousStructOrUnion()) continue; CleanupKind cleanupKind = getCleanupKind(dtorKind); EHStack.pushCleanup(cleanupKind, Field, getDestroyer(dtorKind), cleanupKind & EHCleanup); } } /// EmitCXXAggrConstructorCall - Emit a loop to call a particular /// constructor for each of several members of an array. /// /// \param ctor the constructor to call for each element /// \param arrayType the type of the array to initialize /// \param arrayBegin an arrayType* /// \param zeroInitialize true if each element should be /// zero-initialized before it is constructed void CodeGenFunction::EmitCXXAggrConstructorCall( const CXXConstructorDecl *ctor, const ArrayType *arrayType, Address arrayBegin, const CXXConstructExpr *E, bool NewPointerIsChecked, bool zeroInitialize) { QualType elementType; llvm::Value *numElements = emitArrayLength(arrayType, elementType, arrayBegin); EmitCXXAggrConstructorCall(ctor, numElements, arrayBegin, E, NewPointerIsChecked, zeroInitialize); } /// EmitCXXAggrConstructorCall - Emit a loop to call a particular /// constructor for each of several members of an array. /// /// \param ctor the constructor to call for each element /// \param numElements the number of elements in the array; /// may be zero /// \param arrayBase a T*, where T is the type constructed by ctor /// \param zeroInitialize true if each element should be /// zero-initialized before it is constructed void CodeGenFunction::EmitCXXAggrConstructorCall(const CXXConstructorDecl *ctor, llvm::Value *numElements, Address arrayBase, const CXXConstructExpr *E, bool NewPointerIsChecked, bool zeroInitialize) { // It's legal for numElements to be zero. This can happen both // dynamically, because x can be zero in 'new A[x]', and statically, // because of GCC extensions that permit zero-length arrays. There // are probably legitimate places where we could assume that this // doesn't happen, but it's not clear that it's worth it. llvm::BranchInst *zeroCheckBranch = nullptr; // Optimize for a constant count. llvm::ConstantInt *constantCount = dyn_cast(numElements); if (constantCount) { // Just skip out if the constant count is zero. if (constantCount->isZero()) return; // Otherwise, emit the check. } else { llvm::BasicBlock *loopBB = createBasicBlock("new.ctorloop"); llvm::Value *iszero = Builder.CreateIsNull(numElements, "isempty"); zeroCheckBranch = Builder.CreateCondBr(iszero, loopBB, loopBB); EmitBlock(loopBB); } // Find the end of the array. llvm::Value *arrayBegin = arrayBase.getPointer(); llvm::Value *arrayEnd = Builder.CreateInBoundsGEP(arrayBegin, numElements, "arrayctor.end"); // Enter the loop, setting up a phi for the current location to initialize. llvm::BasicBlock *entryBB = Builder.GetInsertBlock(); llvm::BasicBlock *loopBB = createBasicBlock("arrayctor.loop"); EmitBlock(loopBB); llvm::PHINode *cur = Builder.CreatePHI(arrayBegin->getType(), 2, "arrayctor.cur"); cur->addIncoming(arrayBegin, entryBB); // Inside the loop body, emit the constructor call on the array element. // The alignment of the base, adjusted by the size of a single element, // provides a conservative estimate of the alignment of every element. // (This assumes we never start tracking offsetted alignments.) // // Note that these are complete objects and so we don't need to // use the non-virtual size or alignment. QualType type = getContext().getTypeDeclType(ctor->getParent()); CharUnits eltAlignment = arrayBase.getAlignment() .alignmentOfArrayElement(getContext().getTypeSizeInChars(type)); Address curAddr = Address(cur, eltAlignment); // Zero initialize the storage, if requested. if (zeroInitialize) EmitNullInitialization(curAddr, type); // C++ [class.temporary]p4: // There are two contexts in which temporaries are destroyed at a different // point than the end of the full-expression. The first context is when a // default constructor is called to initialize an element of an array. // If the constructor has one or more default arguments, the destruction of // every temporary created in a default argument expression is sequenced // before the construction of the next array element, if any. { RunCleanupsScope Scope(*this); // Evaluate the constructor and its arguments in a regular // partial-destroy cleanup. if (getLangOpts().Exceptions && !ctor->getParent()->hasTrivialDestructor()) { Destroyer *destroyer = destroyCXXObject; pushRegularPartialArrayCleanup(arrayBegin, cur, type, eltAlignment, *destroyer); } auto currAVS = AggValueSlot::forAddr( curAddr, type.getQualifiers(), AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased, AggValueSlot::DoesNotOverlap, AggValueSlot::IsNotZeroed, NewPointerIsChecked ? AggValueSlot::IsSanitizerChecked : AggValueSlot::IsNotSanitizerChecked); EmitCXXConstructorCall(ctor, Ctor_Complete, /*ForVirtualBase=*/false, /*Delegating=*/false, currAVS, E); } // Go to the next element. llvm::Value *next = Builder.CreateInBoundsGEP(cur, llvm::ConstantInt::get(SizeTy, 1), "arrayctor.next"); cur->addIncoming(next, Builder.GetInsertBlock()); // Check whether that's the end of the loop. llvm::Value *done = Builder.CreateICmpEQ(next, arrayEnd, "arrayctor.done"); llvm::BasicBlock *contBB = createBasicBlock("arrayctor.cont"); Builder.CreateCondBr(done, contBB, loopBB); // Patch the earlier check to skip over the loop. if (zeroCheckBranch) zeroCheckBranch->setSuccessor(0, contBB); EmitBlock(contBB); } void CodeGenFunction::destroyCXXObject(CodeGenFunction &CGF, Address addr, QualType type) { const RecordType *rtype = type->castAs(); const CXXRecordDecl *record = cast(rtype->getDecl()); const CXXDestructorDecl *dtor = record->getDestructor(); assert(!dtor->isTrivial()); CGF.EmitCXXDestructorCall(dtor, Dtor_Complete, /*for vbase*/ false, /*Delegating=*/false, addr, type); } void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase, bool Delegating, AggValueSlot ThisAVS, const CXXConstructExpr *E) { CallArgList Args; Address This = ThisAVS.getAddress(); LangAS SlotAS = ThisAVS.getQualifiers().getAddressSpace(); QualType ThisType = D->getThisType(); LangAS ThisAS = ThisType.getTypePtr()->getPointeeType().getAddressSpace(); llvm::Value *ThisPtr = This.getPointer(); if (SlotAS != ThisAS) { unsigned TargetThisAS = getContext().getTargetAddressSpace(ThisAS); llvm::Type *NewType = ThisPtr->getType()->getPointerElementType()->getPointerTo(TargetThisAS); ThisPtr = getTargetHooks().performAddrSpaceCast(*this, This.getPointer(), ThisAS, SlotAS, NewType); } // Push the this ptr. Args.add(RValue::get(ThisPtr), D->getThisType()); // If this is a trivial constructor, emit a memcpy now before we lose // the alignment information on the argument. // FIXME: It would be better to preserve alignment information into CallArg. if (isMemcpyEquivalentSpecialMember(D)) { assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor"); const Expr *Arg = E->getArg(0); LValue Src = EmitLValue(Arg); QualType DestTy = getContext().getTypeDeclType(D->getParent()); LValue Dest = MakeAddrLValue(This, DestTy); EmitAggregateCopyCtor(Dest, Src, ThisAVS.mayOverlap()); return; } // Add the rest of the user-supplied arguments. const FunctionProtoType *FPT = D->getType()->castAs(); EvaluationOrder Order = E->isListInitialization() ? EvaluationOrder::ForceLeftToRight : EvaluationOrder::Default; EmitCallArgs(Args, FPT, E->arguments(), E->getConstructor(), /*ParamsToSkip*/ 0, Order); EmitCXXConstructorCall(D, Type, ForVirtualBase, Delegating, This, Args, ThisAVS.mayOverlap(), E->getExprLoc(), ThisAVS.isSanitizerChecked()); } static bool canEmitDelegateCallArgs(CodeGenFunction &CGF, const CXXConstructorDecl *Ctor, CXXCtorType Type, CallArgList &Args) { // We can't forward a variadic call. if (Ctor->isVariadic()) return false; if (CGF.getTarget().getCXXABI().areArgsDestroyedLeftToRightInCallee()) { // If the parameters are callee-cleanup, it's not safe to forward. for (auto *P : Ctor->parameters()) if (P->needsDestruction(CGF.getContext())) return false; // Likewise if they're inalloca. const CGFunctionInfo &Info = CGF.CGM.getTypes().arrangeCXXConstructorCall(Args, Ctor, Type, 0, 0); if (Info.usesInAlloca()) return false; } // Anything else should be OK. return true; } void CodeGenFunction::EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type, bool ForVirtualBase, bool Delegating, Address This, CallArgList &Args, AggValueSlot::Overlap_t Overlap, SourceLocation Loc, bool NewPointerIsChecked) { const CXXRecordDecl *ClassDecl = D->getParent(); if (!NewPointerIsChecked) EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, Loc, This.getPointer(), getContext().getRecordType(ClassDecl), CharUnits::Zero()); if (D->isTrivial() && D->isDefaultConstructor()) { assert(Args.size() == 1 && "trivial default ctor with args"); return; } // If this is a trivial constructor, just emit what's needed. If this is a // union copy constructor, we must emit a memcpy, because the AST does not // model that copy. if (isMemcpyEquivalentSpecialMember(D)) { assert(Args.size() == 2 && "unexpected argcount for trivial ctor"); QualType SrcTy = D->getParamDecl(0)->getType().getNonReferenceType(); Address Src(Args[1].getRValue(*this).getScalarVal(), getNaturalTypeAlignment(SrcTy)); LValue SrcLVal = MakeAddrLValue(Src, SrcTy); QualType DestTy = getContext().getTypeDeclType(ClassDecl); LValue DestLVal = MakeAddrLValue(This, DestTy); EmitAggregateCopyCtor(DestLVal, SrcLVal, Overlap); return; } bool PassPrototypeArgs = true; // Check whether we can actually emit the constructor before trying to do so. if (auto Inherited = D->getInheritedConstructor()) { PassPrototypeArgs = getTypes().inheritingCtorHasParams(Inherited, Type); if (PassPrototypeArgs && !canEmitDelegateCallArgs(*this, D, Type, Args)) { EmitInlinedInheritingCXXConstructorCall(D, Type, ForVirtualBase, Delegating, Args); return; } } // Insert any ABI-specific implicit constructor arguments. CGCXXABI::AddedStructorArgs ExtraArgs = CGM.getCXXABI().addImplicitConstructorArgs(*this, D, Type, ForVirtualBase, Delegating, Args); // Emit the call. llvm::Constant *CalleePtr = CGM.getAddrOfCXXStructor(GlobalDecl(D, Type)); const CGFunctionInfo &Info = CGM.getTypes().arrangeCXXConstructorCall( Args, D, Type, ExtraArgs.Prefix, ExtraArgs.Suffix, PassPrototypeArgs); CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(D, Type)); - EmitCall(Info, Callee, ReturnValueSlot(), Args); + EmitCall(Info, Callee, ReturnValueSlot(), Args, nullptr, Loc); // Generate vtable assumptions if we're constructing a complete object // with a vtable. We don't do this for base subobjects for two reasons: // first, it's incorrect for classes with virtual bases, and second, we're // about to overwrite the vptrs anyway. // We also have to make sure if we can refer to vtable: // - Otherwise we can refer to vtable if it's safe to speculatively emit. // FIXME: If vtable is used by ctor/dtor, or if vtable is external and we are // sure that definition of vtable is not hidden, // then we are always safe to refer to it. // FIXME: It looks like InstCombine is very inefficient on dealing with // assumes. Make assumption loads require -fstrict-vtable-pointers temporarily. if (CGM.getCodeGenOpts().OptimizationLevel > 0 && ClassDecl->isDynamicClass() && Type != Ctor_Base && CGM.getCXXABI().canSpeculativelyEmitVTable(ClassDecl) && CGM.getCodeGenOpts().StrictVTablePointers) EmitVTableAssumptionLoads(ClassDecl, This); } void CodeGenFunction::EmitInheritedCXXConstructorCall( const CXXConstructorDecl *D, bool ForVirtualBase, Address This, bool InheritedFromVBase, const CXXInheritedCtorInitExpr *E) { CallArgList Args; CallArg ThisArg(RValue::get(This.getPointer()), D->getThisType()); // Forward the parameters. if (InheritedFromVBase && CGM.getTarget().getCXXABI().hasConstructorVariants()) { // Nothing to do; this construction is not responsible for constructing // the base class containing the inherited constructor. // FIXME: Can we just pass undef's for the remaining arguments if we don't // have constructor variants? Args.push_back(ThisArg); } else if (!CXXInheritedCtorInitExprArgs.empty()) { // The inheriting constructor was inlined; just inject its arguments. assert(CXXInheritedCtorInitExprArgs.size() >= D->getNumParams() && "wrong number of parameters for inherited constructor call"); Args = CXXInheritedCtorInitExprArgs; Args[0] = ThisArg; } else { // The inheriting constructor was not inlined. Emit delegating arguments. Args.push_back(ThisArg); const auto *OuterCtor = cast(CurCodeDecl); assert(OuterCtor->getNumParams() == D->getNumParams()); assert(!OuterCtor->isVariadic() && "should have been inlined"); for (const auto *Param : OuterCtor->parameters()) { assert(getContext().hasSameUnqualifiedType( OuterCtor->getParamDecl(Param->getFunctionScopeIndex())->getType(), Param->getType())); EmitDelegateCallArg(Args, Param, E->getLocation()); // Forward __attribute__(pass_object_size). if (Param->hasAttr()) { auto *POSParam = SizeArguments[Param]; assert(POSParam && "missing pass_object_size value for forwarding"); EmitDelegateCallArg(Args, POSParam, E->getLocation()); } } } EmitCXXConstructorCall(D, Ctor_Base, ForVirtualBase, /*Delegating*/false, This, Args, AggValueSlot::MayOverlap, E->getLocation(), /*NewPointerIsChecked*/true); } void CodeGenFunction::EmitInlinedInheritingCXXConstructorCall( const CXXConstructorDecl *Ctor, CXXCtorType CtorType, bool ForVirtualBase, bool Delegating, CallArgList &Args) { GlobalDecl GD(Ctor, CtorType); InlinedInheritingConstructorScope Scope(*this, GD); ApplyInlineDebugLocation DebugScope(*this, GD); RunCleanupsScope RunCleanups(*this); // Save the arguments to be passed to the inherited constructor. CXXInheritedCtorInitExprArgs = Args; FunctionArgList Params; QualType RetType = BuildFunctionArgList(CurGD, Params); FnRetTy = RetType; // Insert any ABI-specific implicit constructor arguments. CGM.getCXXABI().addImplicitConstructorArgs(*this, Ctor, CtorType, ForVirtualBase, Delegating, Args); // Emit a simplified prolog. We only need to emit the implicit params. assert(Args.size() >= Params.size() && "too few arguments for call"); for (unsigned I = 0, N = Args.size(); I != N; ++I) { if (I < Params.size() && isa(Params[I])) { const RValue &RV = Args[I].getRValue(*this); assert(!RV.isComplex() && "complex indirect params not supported"); ParamValue Val = RV.isScalar() ? ParamValue::forDirect(RV.getScalarVal()) : ParamValue::forIndirect(RV.getAggregateAddress()); EmitParmDecl(*Params[I], Val, I + 1); } } // Create a return value slot if the ABI implementation wants one. // FIXME: This is dumb, we should ask the ABI not to try to set the return // value instead. if (!RetType->isVoidType()) ReturnValue = CreateIRTemp(RetType, "retval.inhctor"); CGM.getCXXABI().EmitInstanceFunctionProlog(*this); CXXThisValue = CXXABIThisValue; // Directly emit the constructor initializers. EmitCtorPrologue(Ctor, CtorType, Params); } void CodeGenFunction::EmitVTableAssumptionLoad(const VPtr &Vptr, Address This) { llvm::Value *VTableGlobal = CGM.getCXXABI().getVTableAddressPoint(Vptr.Base, Vptr.VTableClass); if (!VTableGlobal) return; // We can just use the base offset in the complete class. CharUnits NonVirtualOffset = Vptr.Base.getBaseOffset(); if (!NonVirtualOffset.isZero()) This = ApplyNonVirtualAndVirtualOffset(*this, This, NonVirtualOffset, nullptr, Vptr.VTableClass, Vptr.NearestVBase); llvm::Value *VPtrValue = GetVTablePtr(This, VTableGlobal->getType(), Vptr.VTableClass); llvm::Value *Cmp = Builder.CreateICmpEQ(VPtrValue, VTableGlobal, "cmp.vtables"); Builder.CreateAssumption(Cmp); } void CodeGenFunction::EmitVTableAssumptionLoads(const CXXRecordDecl *ClassDecl, Address This) { if (CGM.getCXXABI().doStructorsInitializeVPtrs(ClassDecl)) for (const VPtr &Vptr : getVTablePointers(ClassDecl)) EmitVTableAssumptionLoad(Vptr, This); } void CodeGenFunction::EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D, Address This, Address Src, const CXXConstructExpr *E) { const FunctionProtoType *FPT = D->getType()->castAs(); CallArgList Args; // Push the this ptr. Args.add(RValue::get(This.getPointer()), D->getThisType()); // Push the src ptr. QualType QT = *(FPT->param_type_begin()); llvm::Type *t = CGM.getTypes().ConvertType(QT); Src = Builder.CreateBitCast(Src, t); Args.add(RValue::get(Src.getPointer()), QT); // Skip over first argument (Src). EmitCallArgs(Args, FPT, drop_begin(E->arguments(), 1), E->getConstructor(), /*ParamsToSkip*/ 1); EmitCXXConstructorCall(D, Ctor_Complete, /*ForVirtualBase*/false, /*Delegating*/false, This, Args, AggValueSlot::MayOverlap, E->getExprLoc(), /*NewPointerIsChecked*/false); } void CodeGenFunction::EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor, CXXCtorType CtorType, const FunctionArgList &Args, SourceLocation Loc) { CallArgList DelegateArgs; FunctionArgList::const_iterator I = Args.begin(), E = Args.end(); assert(I != E && "no parameters to constructor"); // this Address This = LoadCXXThisAddress(); DelegateArgs.add(RValue::get(This.getPointer()), (*I)->getType()); ++I; // FIXME: The location of the VTT parameter in the parameter list is // specific to the Itanium ABI and shouldn't be hardcoded here. if (CGM.getCXXABI().NeedsVTTParameter(CurGD)) { assert(I != E && "cannot skip vtt parameter, already done with args"); assert((*I)->getType()->isPointerType() && "skipping parameter not of vtt type"); ++I; } // Explicit arguments. for (; I != E; ++I) { const VarDecl *param = *I; // FIXME: per-argument source location EmitDelegateCallArg(DelegateArgs, param, Loc); } EmitCXXConstructorCall(Ctor, CtorType, /*ForVirtualBase=*/false, /*Delegating=*/true, This, DelegateArgs, AggValueSlot::MayOverlap, Loc, /*NewPointerIsChecked=*/true); } namespace { struct CallDelegatingCtorDtor final : EHScopeStack::Cleanup { const CXXDestructorDecl *Dtor; Address Addr; CXXDtorType Type; CallDelegatingCtorDtor(const CXXDestructorDecl *D, Address Addr, CXXDtorType Type) : Dtor(D), Addr(Addr), Type(Type) {} void Emit(CodeGenFunction &CGF, Flags flags) override { // We are calling the destructor from within the constructor. // Therefore, "this" should have the expected type. QualType ThisTy = Dtor->getThisObjectType(); CGF.EmitCXXDestructorCall(Dtor, Type, /*ForVirtualBase=*/false, /*Delegating=*/true, Addr, ThisTy); } }; } // end anonymous namespace void CodeGenFunction::EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor, const FunctionArgList &Args) { assert(Ctor->isDelegatingConstructor()); Address ThisPtr = LoadCXXThisAddress(); AggValueSlot AggSlot = AggValueSlot::forAddr(ThisPtr, Qualifiers(), AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased, AggValueSlot::MayOverlap, AggValueSlot::IsNotZeroed, // Checks are made by the code that calls constructor. AggValueSlot::IsSanitizerChecked); EmitAggExpr(Ctor->init_begin()[0]->getInit(), AggSlot); const CXXRecordDecl *ClassDecl = Ctor->getParent(); if (CGM.getLangOpts().Exceptions && !ClassDecl->hasTrivialDestructor()) { CXXDtorType Type = CurGD.getCtorType() == Ctor_Complete ? Dtor_Complete : Dtor_Base; EHStack.pushCleanup(EHCleanup, ClassDecl->getDestructor(), ThisPtr, Type); } } void CodeGenFunction::EmitCXXDestructorCall(const CXXDestructorDecl *DD, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy) { CGM.getCXXABI().EmitDestructorCall(*this, DD, Type, ForVirtualBase, Delegating, This, ThisTy); } namespace { struct CallLocalDtor final : EHScopeStack::Cleanup { const CXXDestructorDecl *Dtor; Address Addr; QualType Ty; CallLocalDtor(const CXXDestructorDecl *D, Address Addr, QualType Ty) : Dtor(D), Addr(Addr), Ty(Ty) {} void Emit(CodeGenFunction &CGF, Flags flags) override { CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, /*Delegating=*/false, Addr, Ty); } }; } // end anonymous namespace void CodeGenFunction::PushDestructorCleanup(const CXXDestructorDecl *D, QualType T, Address Addr) { EHStack.pushCleanup(NormalAndEHCleanup, D, Addr, T); } void CodeGenFunction::PushDestructorCleanup(QualType T, Address Addr) { CXXRecordDecl *ClassDecl = T->getAsCXXRecordDecl(); if (!ClassDecl) return; if (ClassDecl->hasTrivialDestructor()) return; const CXXDestructorDecl *D = ClassDecl->getDestructor(); assert(D && D->isUsed() && "destructor not marked as used!"); PushDestructorCleanup(D, T, Addr); } void CodeGenFunction::InitializeVTablePointer(const VPtr &Vptr) { // Compute the address point. llvm::Value *VTableAddressPoint = CGM.getCXXABI().getVTableAddressPointInStructor( *this, Vptr.VTableClass, Vptr.Base, Vptr.NearestVBase); if (!VTableAddressPoint) return; // Compute where to store the address point. llvm::Value *VirtualOffset = nullptr; CharUnits NonVirtualOffset = CharUnits::Zero(); if (CGM.getCXXABI().isVirtualOffsetNeededForVTableField(*this, Vptr)) { // We need to use the virtual base offset offset because the virtual base // might have a different offset in the most derived class. VirtualOffset = CGM.getCXXABI().GetVirtualBaseClassOffset( *this, LoadCXXThisAddress(), Vptr.VTableClass, Vptr.NearestVBase); NonVirtualOffset = Vptr.OffsetFromNearestVBase; } else { // We can just use the base offset in the complete class. NonVirtualOffset = Vptr.Base.getBaseOffset(); } // Apply the offsets. Address VTableField = LoadCXXThisAddress(); if (!NonVirtualOffset.isZero() || VirtualOffset) VTableField = ApplyNonVirtualAndVirtualOffset( *this, VTableField, NonVirtualOffset, VirtualOffset, Vptr.VTableClass, Vptr.NearestVBase); // Finally, store the address point. Use the same LLVM types as the field to // support optimization. llvm::Type *VTablePtrTy = llvm::FunctionType::get(CGM.Int32Ty, /*isVarArg=*/true) ->getPointerTo() ->getPointerTo(); VTableField = Builder.CreateBitCast(VTableField, VTablePtrTy->getPointerTo()); VTableAddressPoint = Builder.CreateBitCast(VTableAddressPoint, VTablePtrTy); llvm::StoreInst *Store = Builder.CreateStore(VTableAddressPoint, VTableField); TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTablePtrTy); CGM.DecorateInstructionWithTBAA(Store, TBAAInfo); if (CGM.getCodeGenOpts().OptimizationLevel > 0 && CGM.getCodeGenOpts().StrictVTablePointers) CGM.DecorateInstructionWithInvariantGroup(Store, Vptr.VTableClass); } CodeGenFunction::VPtrsVector CodeGenFunction::getVTablePointers(const CXXRecordDecl *VTableClass) { CodeGenFunction::VPtrsVector VPtrsResult; VisitedVirtualBasesSetTy VBases; getVTablePointers(BaseSubobject(VTableClass, CharUnits::Zero()), /*NearestVBase=*/nullptr, /*OffsetFromNearestVBase=*/CharUnits::Zero(), /*BaseIsNonVirtualPrimaryBase=*/false, VTableClass, VBases, VPtrsResult); return VPtrsResult; } void CodeGenFunction::getVTablePointers(BaseSubobject Base, const CXXRecordDecl *NearestVBase, CharUnits OffsetFromNearestVBase, bool BaseIsNonVirtualPrimaryBase, const CXXRecordDecl *VTableClass, VisitedVirtualBasesSetTy &VBases, VPtrsVector &Vptrs) { // If this base is a non-virtual primary base the address point has already // been set. if (!BaseIsNonVirtualPrimaryBase) { // Initialize the vtable pointer for this base. VPtr Vptr = {Base, NearestVBase, OffsetFromNearestVBase, VTableClass}; Vptrs.push_back(Vptr); } const CXXRecordDecl *RD = Base.getBase(); // Traverse bases. for (const auto &I : RD->bases()) { auto *BaseDecl = cast(I.getType()->castAs()->getDecl()); // Ignore classes without a vtable. if (!BaseDecl->isDynamicClass()) continue; CharUnits BaseOffset; CharUnits BaseOffsetFromNearestVBase; bool BaseDeclIsNonVirtualPrimaryBase; if (I.isVirtual()) { // Check if we've visited this virtual base before. if (!VBases.insert(BaseDecl).second) continue; const ASTRecordLayout &Layout = getContext().getASTRecordLayout(VTableClass); BaseOffset = Layout.getVBaseClassOffset(BaseDecl); BaseOffsetFromNearestVBase = CharUnits::Zero(); BaseDeclIsNonVirtualPrimaryBase = false; } else { const ASTRecordLayout &Layout = getContext().getASTRecordLayout(RD); BaseOffset = Base.getBaseOffset() + Layout.getBaseClassOffset(BaseDecl); BaseOffsetFromNearestVBase = OffsetFromNearestVBase + Layout.getBaseClassOffset(BaseDecl); BaseDeclIsNonVirtualPrimaryBase = Layout.getPrimaryBase() == BaseDecl; } getVTablePointers( BaseSubobject(BaseDecl, BaseOffset), I.isVirtual() ? BaseDecl : NearestVBase, BaseOffsetFromNearestVBase, BaseDeclIsNonVirtualPrimaryBase, VTableClass, VBases, Vptrs); } } void CodeGenFunction::InitializeVTablePointers(const CXXRecordDecl *RD) { // Ignore classes without a vtable. if (!RD->isDynamicClass()) return; // Initialize the vtable pointers for this class and all of its bases. if (CGM.getCXXABI().doStructorsInitializeVPtrs(RD)) for (const VPtr &Vptr : getVTablePointers(RD)) InitializeVTablePointer(Vptr); if (RD->getNumVBases()) CGM.getCXXABI().initializeHiddenVirtualInheritanceMembers(*this, RD); } llvm::Value *CodeGenFunction::GetVTablePtr(Address This, llvm::Type *VTableTy, const CXXRecordDecl *RD) { Address VTablePtrSrc = Builder.CreateElementBitCast(This, VTableTy); llvm::Instruction *VTable = Builder.CreateLoad(VTablePtrSrc, "vtable"); TBAAAccessInfo TBAAInfo = CGM.getTBAAVTablePtrAccessInfo(VTableTy); CGM.DecorateInstructionWithTBAA(VTable, TBAAInfo); if (CGM.getCodeGenOpts().OptimizationLevel > 0 && CGM.getCodeGenOpts().StrictVTablePointers) CGM.DecorateInstructionWithInvariantGroup(VTable, RD); return VTable; } // If a class has a single non-virtual base and does not introduce or override // virtual member functions or fields, it will have the same layout as its base. // This function returns the least derived such class. // // Casting an instance of a base class to such a derived class is technically // undefined behavior, but it is a relatively common hack for introducing member // functions on class instances with specific properties (e.g. llvm::Operator) // that works under most compilers and should not have security implications, so // we allow it by default. It can be disabled with -fsanitize=cfi-cast-strict. static const CXXRecordDecl * LeastDerivedClassWithSameLayout(const CXXRecordDecl *RD) { if (!RD->field_empty()) return RD; if (RD->getNumVBases() != 0) return RD; if (RD->getNumBases() != 1) return RD; for (const CXXMethodDecl *MD : RD->methods()) { if (MD->isVirtual()) { // Virtual member functions are only ok if they are implicit destructors // because the implicit destructor will have the same semantics as the // base class's destructor if no fields are added. if (isa(MD) && MD->isImplicit()) continue; return RD; } } return LeastDerivedClassWithSameLayout( RD->bases_begin()->getType()->getAsCXXRecordDecl()); } void CodeGenFunction::EmitTypeMetadataCodeForVCall(const CXXRecordDecl *RD, llvm::Value *VTable, SourceLocation Loc) { if (SanOpts.has(SanitizerKind::CFIVCall)) EmitVTablePtrCheckForCall(RD, VTable, CodeGenFunction::CFITCK_VCall, Loc); else if (CGM.getCodeGenOpts().WholeProgramVTables && CGM.HasHiddenLTOVisibility(RD)) { llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); llvm::Value *TypeId = llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD); llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); llvm::Value *TypeTest = Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, TypeId}); Builder.CreateCall(CGM.getIntrinsic(llvm::Intrinsic::assume), TypeTest); } } void CodeGenFunction::EmitVTablePtrCheckForCall(const CXXRecordDecl *RD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc) { if (!SanOpts.has(SanitizerKind::CFICastStrict)) RD = LeastDerivedClassWithSameLayout(RD); EmitVTablePtrCheck(RD, VTable, TCK, Loc); } void CodeGenFunction::EmitVTablePtrCheckForCast(QualType T, llvm::Value *Derived, bool MayBeNull, CFITypeCheckKind TCK, SourceLocation Loc) { if (!getLangOpts().CPlusPlus) return; auto *ClassTy = T->getAs(); if (!ClassTy) return; const CXXRecordDecl *ClassDecl = cast(ClassTy->getDecl()); if (!ClassDecl->isCompleteDefinition() || !ClassDecl->isDynamicClass()) return; if (!SanOpts.has(SanitizerKind::CFICastStrict)) ClassDecl = LeastDerivedClassWithSameLayout(ClassDecl); llvm::BasicBlock *ContBlock = nullptr; if (MayBeNull) { llvm::Value *DerivedNotNull = Builder.CreateIsNotNull(Derived, "cast.nonnull"); llvm::BasicBlock *CheckBlock = createBasicBlock("cast.check"); ContBlock = createBasicBlock("cast.cont"); Builder.CreateCondBr(DerivedNotNull, CheckBlock, ContBlock); EmitBlock(CheckBlock); } llvm::Value *VTable; std::tie(VTable, ClassDecl) = CGM.getCXXABI().LoadVTablePtr( *this, Address(Derived, getPointerAlign()), ClassDecl); EmitVTablePtrCheck(ClassDecl, VTable, TCK, Loc); if (MayBeNull) { Builder.CreateBr(ContBlock); EmitBlock(ContBlock); } } void CodeGenFunction::EmitVTablePtrCheck(const CXXRecordDecl *RD, llvm::Value *VTable, CFITypeCheckKind TCK, SourceLocation Loc) { if (!CGM.getCodeGenOpts().SanitizeCfiCrossDso && !CGM.HasHiddenLTOVisibility(RD)) return; SanitizerMask M; llvm::SanitizerStatKind SSK; switch (TCK) { case CFITCK_VCall: M = SanitizerKind::CFIVCall; SSK = llvm::SanStat_CFI_VCall; break; case CFITCK_NVCall: M = SanitizerKind::CFINVCall; SSK = llvm::SanStat_CFI_NVCall; break; case CFITCK_DerivedCast: M = SanitizerKind::CFIDerivedCast; SSK = llvm::SanStat_CFI_DerivedCast; break; case CFITCK_UnrelatedCast: M = SanitizerKind::CFIUnrelatedCast; SSK = llvm::SanStat_CFI_UnrelatedCast; break; case CFITCK_ICall: case CFITCK_NVMFCall: case CFITCK_VMFCall: llvm_unreachable("unexpected sanitizer kind"); } std::string TypeName = RD->getQualifiedNameAsString(); if (getContext().getSanitizerBlacklist().isBlacklistedType(M, TypeName)) return; SanitizerScope SanScope(this); EmitSanitizerStatReport(SSK); llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); llvm::Value *TypeId = llvm::MetadataAsValue::get(getLLVMContext(), MD); llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); llvm::Value *TypeTest = Builder.CreateCall( CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, TypeId}); llvm::Constant *StaticData[] = { llvm::ConstantInt::get(Int8Ty, TCK), EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(QualType(RD->getTypeForDecl(), 0)), }; auto CrossDsoTypeId = CGM.CreateCrossDsoCfiTypeId(MD); if (CGM.getCodeGenOpts().SanitizeCfiCrossDso && CrossDsoTypeId) { EmitCfiSlowPathCheck(M, TypeTest, CrossDsoTypeId, CastedVTable, StaticData); return; } if (CGM.getCodeGenOpts().SanitizeTrap.has(M)) { EmitTrapCheck(TypeTest); return; } llvm::Value *AllVtables = llvm::MetadataAsValue::get( CGM.getLLVMContext(), llvm::MDString::get(CGM.getLLVMContext(), "all-vtables")); llvm::Value *ValidVtable = Builder.CreateCall( CGM.getIntrinsic(llvm::Intrinsic::type_test), {CastedVTable, AllVtables}); EmitCheck(std::make_pair(TypeTest, M), SanitizerHandler::CFICheckFail, StaticData, {CastedVTable, ValidVtable}); } bool CodeGenFunction::ShouldEmitVTableTypeCheckedLoad(const CXXRecordDecl *RD) { if (!CGM.getCodeGenOpts().WholeProgramVTables || !CGM.HasHiddenLTOVisibility(RD)) return false; if (CGM.getCodeGenOpts().VirtualFunctionElimination) return true; if (!SanOpts.has(SanitizerKind::CFIVCall) || !CGM.getCodeGenOpts().SanitizeTrap.has(SanitizerKind::CFIVCall)) return false; std::string TypeName = RD->getQualifiedNameAsString(); return !getContext().getSanitizerBlacklist().isBlacklistedType( SanitizerKind::CFIVCall, TypeName); } llvm::Value *CodeGenFunction::EmitVTableTypeCheckedLoad( const CXXRecordDecl *RD, llvm::Value *VTable, uint64_t VTableByteOffset) { SanitizerScope SanScope(this); EmitSanitizerStatReport(llvm::SanStat_CFI_VCall); llvm::Metadata *MD = CGM.CreateMetadataIdentifierForType(QualType(RD->getTypeForDecl(), 0)); llvm::Value *TypeId = llvm::MetadataAsValue::get(CGM.getLLVMContext(), MD); llvm::Value *CastedVTable = Builder.CreateBitCast(VTable, Int8PtrTy); llvm::Value *CheckedLoad = Builder.CreateCall( CGM.getIntrinsic(llvm::Intrinsic::type_checked_load), {CastedVTable, llvm::ConstantInt::get(Int32Ty, VTableByteOffset), TypeId}); llvm::Value *CheckResult = Builder.CreateExtractValue(CheckedLoad, 1); std::string TypeName = RD->getQualifiedNameAsString(); if (SanOpts.has(SanitizerKind::CFIVCall) && !getContext().getSanitizerBlacklist().isBlacklistedType( SanitizerKind::CFIVCall, TypeName)) { EmitCheck(std::make_pair(CheckResult, SanitizerKind::CFIVCall), SanitizerHandler::CFICheckFail, {}, {}); } return Builder.CreateBitCast( Builder.CreateExtractValue(CheckedLoad, 0), cast(VTable->getType())->getElementType()); } void CodeGenFunction::EmitForwardingCallToLambda( const CXXMethodDecl *callOperator, CallArgList &callArgs) { // Get the address of the call operator. const CGFunctionInfo &calleeFnInfo = CGM.getTypes().arrangeCXXMethodDeclaration(callOperator); llvm::Constant *calleePtr = CGM.GetAddrOfFunction(GlobalDecl(callOperator), CGM.getTypes().GetFunctionType(calleeFnInfo)); // Prepare the return slot. const FunctionProtoType *FPT = callOperator->getType()->castAs(); QualType resultType = FPT->getReturnType(); ReturnValueSlot returnSlot; if (!resultType->isVoidType() && calleeFnInfo.getReturnInfo().getKind() == ABIArgInfo::Indirect && !hasScalarEvaluationKind(calleeFnInfo.getReturnType())) returnSlot = ReturnValueSlot(ReturnValue, resultType.isVolatileQualified()); // We don't need to separately arrange the call arguments because // the call can't be variadic anyway --- it's impossible to forward // variadic arguments. // Now emit our call. auto callee = CGCallee::forDirect(calleePtr, GlobalDecl(callOperator)); RValue RV = EmitCall(calleeFnInfo, callee, returnSlot, callArgs); // If necessary, copy the returned value into the slot. if (!resultType->isVoidType() && returnSlot.isNull()) { if (getLangOpts().ObjCAutoRefCount && resultType->isObjCRetainableType()) { RV = RValue::get(EmitARCRetainAutoreleasedReturnValue(RV.getScalarVal())); } EmitReturnOfRValue(RV, resultType); } else EmitBranchThroughCleanup(ReturnBlock); } void CodeGenFunction::EmitLambdaBlockInvokeBody() { const BlockDecl *BD = BlockInfo->getBlockDecl(); const VarDecl *variable = BD->capture_begin()->getVariable(); const CXXRecordDecl *Lambda = variable->getType()->getAsCXXRecordDecl(); const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); if (CallOp->isVariadic()) { // FIXME: Making this work correctly is nasty because it requires either // cloning the body of the call operator or making the call operator // forward. CGM.ErrorUnsupported(CurCodeDecl, "lambda conversion to variadic function"); return; } // Start building arguments for forwarding call CallArgList CallArgs; QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); Address ThisPtr = GetAddrOfBlockDecl(variable); CallArgs.add(RValue::get(ThisPtr.getPointer()), ThisType); // Add the rest of the parameters. for (auto param : BD->parameters()) EmitDelegateCallArg(CallArgs, param, param->getBeginLoc()); assert(!Lambda->isGenericLambda() && "generic lambda interconversion to block not implemented"); EmitForwardingCallToLambda(CallOp, CallArgs); } void CodeGenFunction::EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD) { const CXXRecordDecl *Lambda = MD->getParent(); // Start building arguments for forwarding call CallArgList CallArgs; QualType ThisType = getContext().getPointerType(getContext().getRecordType(Lambda)); llvm::Value *ThisPtr = llvm::UndefValue::get(getTypes().ConvertType(ThisType)); CallArgs.add(RValue::get(ThisPtr), ThisType); // Add the rest of the parameters. for (auto Param : MD->parameters()) EmitDelegateCallArg(CallArgs, Param, Param->getBeginLoc()); const CXXMethodDecl *CallOp = Lambda->getLambdaCallOperator(); // For a generic lambda, find the corresponding call operator specialization // to which the call to the static-invoker shall be forwarded. if (Lambda->isGenericLambda()) { assert(MD->isFunctionTemplateSpecialization()); const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); FunctionTemplateDecl *CallOpTemplate = CallOp->getDescribedFunctionTemplate(); void *InsertPos = nullptr; FunctionDecl *CorrespondingCallOpSpecialization = CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); assert(CorrespondingCallOpSpecialization); CallOp = cast(CorrespondingCallOpSpecialization); } EmitForwardingCallToLambda(CallOp, CallArgs); } void CodeGenFunction::EmitLambdaStaticInvokeBody(const CXXMethodDecl *MD) { if (MD->isVariadic()) { // FIXME: Making this work correctly is nasty because it requires either // cloning the body of the call operator or making the call operator forward. CGM.ErrorUnsupported(MD, "lambda conversion to variadic function"); return; } EmitLambdaDelegatingInvokeBody(MD); } Index: head/contrib/llvm-project/clang/lib/CodeGen/CGExprCXX.cpp =================================================================== --- head/contrib/llvm-project/clang/lib/CodeGen/CGExprCXX.cpp (revision 359825) +++ head/contrib/llvm-project/clang/lib/CodeGen/CGExprCXX.cpp (revision 359826) @@ -1,2283 +1,2284 @@ //===--- CGExprCXX.cpp - Emit LLVM Code for C++ expressions ---------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This contains code dealing with code generation of C++ expressions // //===----------------------------------------------------------------------===// #include "CGCUDARuntime.h" #include "CGCXXABI.h" #include "CGDebugInfo.h" #include "CGObjCRuntime.h" #include "CodeGenFunction.h" #include "ConstantEmitter.h" #include "TargetInfo.h" #include "clang/Basic/CodeGenOptions.h" #include "clang/CodeGen/CGFunctionInfo.h" #include "llvm/IR/Intrinsics.h" using namespace clang; using namespace CodeGen; namespace { struct MemberCallInfo { RequiredArgs ReqArgs; // Number of prefix arguments for the call. Ignores the `this` pointer. unsigned PrefixSize; }; } static MemberCallInfo commonEmitCXXMemberOrOperatorCall(CodeGenFunction &CGF, const CXXMethodDecl *MD, llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE, CallArgList &Args, CallArgList *RtlArgs) { assert(CE == nullptr || isa(CE) || isa(CE)); assert(MD->isInstance() && "Trying to emit a member or operator call expr on a static method!"); // Push the this ptr. const CXXRecordDecl *RD = CGF.CGM.getCXXABI().getThisArgumentTypeForMethod(MD); Args.add(RValue::get(This), CGF.getTypes().DeriveThisType(RD, MD)); // If there is an implicit parameter (e.g. VTT), emit it. if (ImplicitParam) { Args.add(RValue::get(ImplicitParam), ImplicitParamTy); } const FunctionProtoType *FPT = MD->getType()->castAs(); RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, Args.size()); unsigned PrefixSize = Args.size() - 1; // And the rest of the call args. if (RtlArgs) { // Special case: if the caller emitted the arguments right-to-left already // (prior to emitting the *this argument), we're done. This happens for // assignment operators. Args.addFrom(*RtlArgs); } else if (CE) { // Special case: skip first argument of CXXOperatorCall (it is "this"). unsigned ArgsToSkip = isa(CE) ? 1 : 0; CGF.EmitCallArgs(Args, FPT, drop_begin(CE->arguments(), ArgsToSkip), CE->getDirectCallee()); } else { assert( FPT->getNumParams() == 0 && "No CallExpr specified for function with non-zero number of arguments"); } return {required, PrefixSize}; } RValue CodeGenFunction::EmitCXXMemberOrOperatorCall( const CXXMethodDecl *MD, const CGCallee &Callee, ReturnValueSlot ReturnValue, llvm::Value *This, llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE, CallArgList *RtlArgs) { const FunctionProtoType *FPT = MD->getType()->castAs(); CallArgList Args; MemberCallInfo CallInfo = commonEmitCXXMemberOrOperatorCall( *this, MD, This, ImplicitParam, ImplicitParamTy, CE, Args, RtlArgs); auto &FnInfo = CGM.getTypes().arrangeCXXMethodCall( Args, FPT, CallInfo.ReqArgs, CallInfo.PrefixSize); return EmitCall(FnInfo, Callee, ReturnValue, Args, nullptr, CE ? CE->getExprLoc() : SourceLocation()); } RValue CodeGenFunction::EmitCXXDestructorCall( GlobalDecl Dtor, const CGCallee &Callee, llvm::Value *This, QualType ThisTy, llvm::Value *ImplicitParam, QualType ImplicitParamTy, const CallExpr *CE) { const CXXMethodDecl *DtorDecl = cast(Dtor.getDecl()); assert(!ThisTy.isNull()); assert(ThisTy->getAsCXXRecordDecl() == DtorDecl->getParent() && "Pointer/Object mixup"); LangAS SrcAS = ThisTy.getAddressSpace(); LangAS DstAS = DtorDecl->getMethodQualifiers().getAddressSpace(); if (SrcAS != DstAS) { QualType DstTy = DtorDecl->getThisType(); llvm::Type *NewType = CGM.getTypes().ConvertType(DstTy); This = getTargetHooks().performAddrSpaceCast(*this, This, SrcAS, DstAS, NewType); } CallArgList Args; commonEmitCXXMemberOrOperatorCall(*this, DtorDecl, This, ImplicitParam, ImplicitParamTy, CE, Args, nullptr); return EmitCall(CGM.getTypes().arrangeCXXStructorDeclaration(Dtor), Callee, - ReturnValueSlot(), Args); + ReturnValueSlot(), Args, nullptr, + CE ? CE->getExprLoc() : SourceLocation{}); } RValue CodeGenFunction::EmitCXXPseudoDestructorExpr( const CXXPseudoDestructorExpr *E) { QualType DestroyedType = E->getDestroyedType(); if (DestroyedType.hasStrongOrWeakObjCLifetime()) { // Automatic Reference Counting: // If the pseudo-expression names a retainable object with weak or // strong lifetime, the object shall be released. Expr *BaseExpr = E->getBase(); Address BaseValue = Address::invalid(); Qualifiers BaseQuals; // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. if (E->isArrow()) { BaseValue = EmitPointerWithAlignment(BaseExpr); const auto *PTy = BaseExpr->getType()->castAs(); BaseQuals = PTy->getPointeeType().getQualifiers(); } else { LValue BaseLV = EmitLValue(BaseExpr); BaseValue = BaseLV.getAddress(*this); QualType BaseTy = BaseExpr->getType(); BaseQuals = BaseTy.getQualifiers(); } switch (DestroyedType.getObjCLifetime()) { case Qualifiers::OCL_None: case Qualifiers::OCL_ExplicitNone: case Qualifiers::OCL_Autoreleasing: break; case Qualifiers::OCL_Strong: EmitARCRelease(Builder.CreateLoad(BaseValue, DestroyedType.isVolatileQualified()), ARCPreciseLifetime); break; case Qualifiers::OCL_Weak: EmitARCDestroyWeak(BaseValue); break; } } else { // C++ [expr.pseudo]p1: // The result shall only be used as the operand for the function call // operator (), and the result of such a call has type void. The only // effect is the evaluation of the postfix-expression before the dot or // arrow. EmitIgnoredExpr(E->getBase()); } return RValue::get(nullptr); } static CXXRecordDecl *getCXXRecord(const Expr *E) { QualType T = E->getType(); if (const PointerType *PTy = T->getAs()) T = PTy->getPointeeType(); const RecordType *Ty = T->castAs(); return cast(Ty->getDecl()); } // Note: This function also emit constructor calls to support a MSVC // extensions allowing explicit constructor function call. RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE, ReturnValueSlot ReturnValue) { const Expr *callee = CE->getCallee()->IgnoreParens(); if (isa(callee)) return EmitCXXMemberPointerCallExpr(CE, ReturnValue); const MemberExpr *ME = cast(callee); const CXXMethodDecl *MD = cast(ME->getMemberDecl()); if (MD->isStatic()) { // The method is static, emit it as we would a regular call. CGCallee callee = CGCallee::forDirect(CGM.GetAddrOfFunction(MD), GlobalDecl(MD)); return EmitCall(getContext().getPointerType(MD->getType()), callee, CE, ReturnValue); } bool HasQualifier = ME->hasQualifier(); NestedNameSpecifier *Qualifier = HasQualifier ? ME->getQualifier() : nullptr; bool IsArrow = ME->isArrow(); const Expr *Base = ME->getBase(); return EmitCXXMemberOrOperatorMemberCallExpr( CE, MD, ReturnValue, HasQualifier, Qualifier, IsArrow, Base); } RValue CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr( const CallExpr *CE, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue, bool HasQualifier, NestedNameSpecifier *Qualifier, bool IsArrow, const Expr *Base) { assert(isa(CE) || isa(CE)); // Compute the object pointer. bool CanUseVirtualCall = MD->isVirtual() && !HasQualifier; const CXXMethodDecl *DevirtualizedMethod = nullptr; if (CanUseVirtualCall && MD->getDevirtualizedMethod(Base, getLangOpts().AppleKext)) { const CXXRecordDecl *BestDynamicDecl = Base->getBestDynamicClassType(); DevirtualizedMethod = MD->getCorrespondingMethodInClass(BestDynamicDecl); assert(DevirtualizedMethod); const CXXRecordDecl *DevirtualizedClass = DevirtualizedMethod->getParent(); const Expr *Inner = Base->ignoreParenBaseCasts(); if (DevirtualizedMethod->getReturnType().getCanonicalType() != MD->getReturnType().getCanonicalType()) // If the return types are not the same, this might be a case where more // code needs to run to compensate for it. For example, the derived // method might return a type that inherits form from the return // type of MD and has a prefix. // For now we just avoid devirtualizing these covariant cases. DevirtualizedMethod = nullptr; else if (getCXXRecord(Inner) == DevirtualizedClass) // If the class of the Inner expression is where the dynamic method // is defined, build the this pointer from it. Base = Inner; else if (getCXXRecord(Base) != DevirtualizedClass) { // If the method is defined in a class that is not the best dynamic // one or the one of the full expression, we would have to build // a derived-to-base cast to compute the correct this pointer, but // we don't have support for that yet, so do a virtual call. DevirtualizedMethod = nullptr; } } bool TrivialForCodegen = MD->isTrivial() || (MD->isDefaulted() && MD->getParent()->isUnion()); bool TrivialAssignment = TrivialForCodegen && (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()) && !MD->getParent()->mayInsertExtraPadding(); // C++17 demands that we evaluate the RHS of a (possibly-compound) assignment // operator before the LHS. CallArgList RtlArgStorage; CallArgList *RtlArgs = nullptr; LValue TrivialAssignmentRHS; if (auto *OCE = dyn_cast(CE)) { if (OCE->isAssignmentOp()) { if (TrivialAssignment) { TrivialAssignmentRHS = EmitLValue(CE->getArg(1)); } else { RtlArgs = &RtlArgStorage; EmitCallArgs(*RtlArgs, MD->getType()->castAs(), drop_begin(CE->arguments(), 1), CE->getDirectCallee(), /*ParamsToSkip*/0, EvaluationOrder::ForceRightToLeft); } } } LValue This; if (IsArrow) { LValueBaseInfo BaseInfo; TBAAAccessInfo TBAAInfo; Address ThisValue = EmitPointerWithAlignment(Base, &BaseInfo, &TBAAInfo); This = MakeAddrLValue(ThisValue, Base->getType(), BaseInfo, TBAAInfo); } else { This = EmitLValue(Base); } if (const CXXConstructorDecl *Ctor = dyn_cast(MD)) { // This is the MSVC p->Ctor::Ctor(...) extension. We assume that's // constructing a new complete object of type Ctor. assert(!RtlArgs); assert(ReturnValue.isNull() && "Constructor shouldn't have return value"); CallArgList Args; commonEmitCXXMemberOrOperatorCall( *this, Ctor, This.getPointer(*this), /*ImplicitParam=*/nullptr, /*ImplicitParamTy=*/QualType(), CE, Args, nullptr); EmitCXXConstructorCall(Ctor, Ctor_Complete, /*ForVirtualBase=*/false, /*Delegating=*/false, This.getAddress(*this), Args, AggValueSlot::DoesNotOverlap, CE->getExprLoc(), /*NewPointerIsChecked=*/false); return RValue::get(nullptr); } if (TrivialForCodegen) { if (isa(MD)) return RValue::get(nullptr); if (TrivialAssignment) { // We don't like to generate the trivial copy/move assignment operator // when it isn't necessary; just produce the proper effect here. // It's important that we use the result of EmitLValue here rather than // emitting call arguments, in order to preserve TBAA information from // the RHS. LValue RHS = isa(CE) ? TrivialAssignmentRHS : EmitLValue(*CE->arg_begin()); EmitAggregateAssign(This, RHS, CE->getType()); return RValue::get(This.getPointer(*this)); } assert(MD->getParent()->mayInsertExtraPadding() && "unknown trivial member function"); } // Compute the function type we're calling. const CXXMethodDecl *CalleeDecl = DevirtualizedMethod ? DevirtualizedMethod : MD; const CGFunctionInfo *FInfo = nullptr; if (const auto *Dtor = dyn_cast(CalleeDecl)) FInfo = &CGM.getTypes().arrangeCXXStructorDeclaration( GlobalDecl(Dtor, Dtor_Complete)); else FInfo = &CGM.getTypes().arrangeCXXMethodDeclaration(CalleeDecl); llvm::FunctionType *Ty = CGM.getTypes().GetFunctionType(*FInfo); // C++11 [class.mfct.non-static]p2: // If a non-static member function of a class X is called for an object that // is not of type X, or of a type derived from X, the behavior is undefined. SourceLocation CallLoc; ASTContext &C = getContext(); if (CE) CallLoc = CE->getExprLoc(); SanitizerSet SkippedChecks; if (const auto *CMCE = dyn_cast(CE)) { auto *IOA = CMCE->getImplicitObjectArgument(); bool IsImplicitObjectCXXThis = IsWrappedCXXThis(IOA); if (IsImplicitObjectCXXThis) SkippedChecks.set(SanitizerKind::Alignment, true); if (IsImplicitObjectCXXThis || isa(IOA)) SkippedChecks.set(SanitizerKind::Null, true); } EmitTypeCheck(CodeGenFunction::TCK_MemberCall, CallLoc, This.getPointer(*this), C.getRecordType(CalleeDecl->getParent()), /*Alignment=*/CharUnits::Zero(), SkippedChecks); // C++ [class.virtual]p12: // Explicit qualification with the scope operator (5.1) suppresses the // virtual call mechanism. // // We also don't emit a virtual call if the base expression has a record type // because then we know what the type is. bool UseVirtualCall = CanUseVirtualCall && !DevirtualizedMethod; if (const CXXDestructorDecl *Dtor = dyn_cast(CalleeDecl)) { assert(CE->arg_begin() == CE->arg_end() && "Destructor shouldn't have explicit parameters"); assert(ReturnValue.isNull() && "Destructor shouldn't have return value"); if (UseVirtualCall) { CGM.getCXXABI().EmitVirtualDestructorCall(*this, Dtor, Dtor_Complete, This.getAddress(*this), cast(CE)); } else { GlobalDecl GD(Dtor, Dtor_Complete); CGCallee Callee; if (getLangOpts().AppleKext && Dtor->isVirtual() && HasQualifier) Callee = BuildAppleKextVirtualCall(Dtor, Qualifier, Ty); else if (!DevirtualizedMethod) Callee = CGCallee::forDirect(CGM.getAddrOfCXXStructor(GD, FInfo, Ty), GD); else { Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(GD, Ty), GD); } QualType ThisTy = IsArrow ? Base->getType()->getPointeeType() : Base->getType(); EmitCXXDestructorCall(GD, Callee, This.getPointer(*this), ThisTy, /*ImplicitParam=*/nullptr, - /*ImplicitParamTy=*/QualType(), nullptr); + /*ImplicitParamTy=*/QualType(), CE); } return RValue::get(nullptr); } // FIXME: Uses of 'MD' past this point need to be audited. We may need to use // 'CalleeDecl' instead. CGCallee Callee; if (UseVirtualCall) { Callee = CGCallee::forVirtual(CE, MD, This.getAddress(*this), Ty); } else { if (SanOpts.has(SanitizerKind::CFINVCall) && MD->getParent()->isDynamicClass()) { llvm::Value *VTable; const CXXRecordDecl *RD; std::tie(VTable, RD) = CGM.getCXXABI().LoadVTablePtr( *this, This.getAddress(*this), CalleeDecl->getParent()); EmitVTablePtrCheckForCall(RD, VTable, CFITCK_NVCall, CE->getBeginLoc()); } if (getLangOpts().AppleKext && MD->isVirtual() && HasQualifier) Callee = BuildAppleKextVirtualCall(MD, Qualifier, Ty); else if (!DevirtualizedMethod) Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(MD, Ty), GlobalDecl(MD)); else { Callee = CGCallee::forDirect(CGM.GetAddrOfFunction(DevirtualizedMethod, Ty), GlobalDecl(DevirtualizedMethod)); } } if (MD->isVirtual()) { Address NewThisAddr = CGM.getCXXABI().adjustThisArgumentForVirtualFunctionCall( *this, CalleeDecl, This.getAddress(*this), UseVirtualCall); This.setAddress(NewThisAddr); } return EmitCXXMemberOrOperatorCall( CalleeDecl, Callee, ReturnValue, This.getPointer(*this), /*ImplicitParam=*/nullptr, QualType(), CE, RtlArgs); } RValue CodeGenFunction::EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E, ReturnValueSlot ReturnValue) { const BinaryOperator *BO = cast(E->getCallee()->IgnoreParens()); const Expr *BaseExpr = BO->getLHS(); const Expr *MemFnExpr = BO->getRHS(); const auto *MPT = MemFnExpr->getType()->castAs(); const auto *FPT = MPT->getPointeeType()->castAs(); const auto *RD = cast(MPT->getClass()->castAs()->getDecl()); // Emit the 'this' pointer. Address This = Address::invalid(); if (BO->getOpcode() == BO_PtrMemI) This = EmitPointerWithAlignment(BaseExpr); else This = EmitLValue(BaseExpr).getAddress(*this); EmitTypeCheck(TCK_MemberCall, E->getExprLoc(), This.getPointer(), QualType(MPT->getClass(), 0)); // Get the member function pointer. llvm::Value *MemFnPtr = EmitScalarExpr(MemFnExpr); // Ask the ABI to load the callee. Note that This is modified. llvm::Value *ThisPtrForCall = nullptr; CGCallee Callee = CGM.getCXXABI().EmitLoadOfMemberFunctionPointer(*this, BO, This, ThisPtrForCall, MemFnPtr, MPT); CallArgList Args; QualType ThisType = getContext().getPointerType(getContext().getTagDeclType(RD)); // Push the this ptr. Args.add(RValue::get(ThisPtrForCall), ThisType); RequiredArgs required = RequiredArgs::forPrototypePlus(FPT, 1); // And the rest of the call args EmitCallArgs(Args, FPT, E->arguments()); return EmitCall(CGM.getTypes().arrangeCXXMethodCall(Args, FPT, required, /*PrefixSize=*/0), Callee, ReturnValue, Args, nullptr, E->getExprLoc()); } RValue CodeGenFunction::EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E, const CXXMethodDecl *MD, ReturnValueSlot ReturnValue) { assert(MD->isInstance() && "Trying to emit a member call expr on a static method!"); return EmitCXXMemberOrOperatorMemberCallExpr( E, MD, ReturnValue, /*HasQualifier=*/false, /*Qualifier=*/nullptr, /*IsArrow=*/false, E->getArg(0)); } RValue CodeGenFunction::EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E, ReturnValueSlot ReturnValue) { return CGM.getCUDARuntime().EmitCUDAKernelCallExpr(*this, E, ReturnValue); } static void EmitNullBaseClassInitialization(CodeGenFunction &CGF, Address DestPtr, const CXXRecordDecl *Base) { if (Base->isEmpty()) return; DestPtr = CGF.Builder.CreateElementBitCast(DestPtr, CGF.Int8Ty); const ASTRecordLayout &Layout = CGF.getContext().getASTRecordLayout(Base); CharUnits NVSize = Layout.getNonVirtualSize(); // We cannot simply zero-initialize the entire base sub-object if vbptrs are // present, they are initialized by the most derived class before calling the // constructor. SmallVector, 1> Stores; Stores.emplace_back(CharUnits::Zero(), NVSize); // Each store is split by the existence of a vbptr. CharUnits VBPtrWidth = CGF.getPointerSize(); std::vector VBPtrOffsets = CGF.CGM.getCXXABI().getVBPtrOffsets(Base); for (CharUnits VBPtrOffset : VBPtrOffsets) { // Stop before we hit any virtual base pointers located in virtual bases. if (VBPtrOffset >= NVSize) break; std::pair LastStore = Stores.pop_back_val(); CharUnits LastStoreOffset = LastStore.first; CharUnits LastStoreSize = LastStore.second; CharUnits SplitBeforeOffset = LastStoreOffset; CharUnits SplitBeforeSize = VBPtrOffset - SplitBeforeOffset; assert(!SplitBeforeSize.isNegative() && "negative store size!"); if (!SplitBeforeSize.isZero()) Stores.emplace_back(SplitBeforeOffset, SplitBeforeSize); CharUnits SplitAfterOffset = VBPtrOffset + VBPtrWidth; CharUnits SplitAfterSize = LastStoreSize - SplitAfterOffset; assert(!SplitAfterSize.isNegative() && "negative store size!"); if (!SplitAfterSize.isZero()) Stores.emplace_back(SplitAfterOffset, SplitAfterSize); } // If the type contains a pointer to data member we can't memset it to zero. // Instead, create a null constant and copy it to the destination. // TODO: there are other patterns besides zero that we can usefully memset, // like -1, which happens to be the pattern used by member-pointers. // TODO: isZeroInitializable can be over-conservative in the case where a // virtual base contains a member pointer. llvm::Constant *NullConstantForBase = CGF.CGM.EmitNullConstantForBase(Base); if (!NullConstantForBase->isNullValue()) { llvm::GlobalVariable *NullVariable = new llvm::GlobalVariable( CGF.CGM.getModule(), NullConstantForBase->getType(), /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, NullConstantForBase, Twine()); CharUnits Align = std::max(Layout.getNonVirtualAlignment(), DestPtr.getAlignment()); NullVariable->setAlignment(Align.getAsAlign()); Address SrcPtr = Address(CGF.EmitCastToVoidPtr(NullVariable), Align); // Get and call the appropriate llvm.memcpy overload. for (std::pair Store : Stores) { CharUnits StoreOffset = Store.first; CharUnits StoreSize = Store.second; llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); CGF.Builder.CreateMemCpy( CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), CGF.Builder.CreateConstInBoundsByteGEP(SrcPtr, StoreOffset), StoreSizeVal); } // Otherwise, just memset the whole thing to zero. This is legal // because in LLVM, all default initializers (other than the ones we just // handled above) are guaranteed to have a bit pattern of all zeros. } else { for (std::pair Store : Stores) { CharUnits StoreOffset = Store.first; CharUnits StoreSize = Store.second; llvm::Value *StoreSizeVal = CGF.CGM.getSize(StoreSize); CGF.Builder.CreateMemSet( CGF.Builder.CreateConstInBoundsByteGEP(DestPtr, StoreOffset), CGF.Builder.getInt8(0), StoreSizeVal); } } } void CodeGenFunction::EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest) { assert(!Dest.isIgnored() && "Must have a destination!"); const CXXConstructorDecl *CD = E->getConstructor(); // If we require zero initialization before (or instead of) calling the // constructor, as can be the case with a non-user-provided default // constructor, emit the zero initialization now, unless destination is // already zeroed. if (E->requiresZeroInitialization() && !Dest.isZeroed()) { switch (E->getConstructionKind()) { case CXXConstructExpr::CK_Delegating: case CXXConstructExpr::CK_Complete: EmitNullInitialization(Dest.getAddress(), E->getType()); break; case CXXConstructExpr::CK_VirtualBase: case CXXConstructExpr::CK_NonVirtualBase: EmitNullBaseClassInitialization(*this, Dest.getAddress(), CD->getParent()); break; } } // If this is a call to a trivial default constructor, do nothing. if (CD->isTrivial() && CD->isDefaultConstructor()) return; // Elide the constructor if we're constructing from a temporary. // The temporary check is required because Sema sets this on NRVO // returns. if (getLangOpts().ElideConstructors && E->isElidable()) { assert(getContext().hasSameUnqualifiedType(E->getType(), E->getArg(0)->getType())); if (E->getArg(0)->isTemporaryObject(getContext(), CD->getParent())) { EmitAggExpr(E->getArg(0), Dest); return; } } if (const ArrayType *arrayType = getContext().getAsArrayType(E->getType())) { EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddress(), E, Dest.isSanitizerChecked()); } else { CXXCtorType Type = Ctor_Complete; bool ForVirtualBase = false; bool Delegating = false; switch (E->getConstructionKind()) { case CXXConstructExpr::CK_Delegating: // We should be emitting a constructor; GlobalDecl will assert this Type = CurGD.getCtorType(); Delegating = true; break; case CXXConstructExpr::CK_Complete: Type = Ctor_Complete; break; case CXXConstructExpr::CK_VirtualBase: ForVirtualBase = true; LLVM_FALLTHROUGH; case CXXConstructExpr::CK_NonVirtualBase: Type = Ctor_Base; } // Call the constructor. EmitCXXConstructorCall(CD, Type, ForVirtualBase, Delegating, Dest, E); } } void CodeGenFunction::EmitSynthesizedCXXCopyCtor(Address Dest, Address Src, const Expr *Exp) { if (const ExprWithCleanups *E = dyn_cast(Exp)) Exp = E->getSubExpr(); assert(isa(Exp) && "EmitSynthesizedCXXCopyCtor - unknown copy ctor expr"); const CXXConstructExpr* E = cast(Exp); const CXXConstructorDecl *CD = E->getConstructor(); RunCleanupsScope Scope(*this); // If we require zero initialization before (or instead of) calling the // constructor, as can be the case with a non-user-provided default // constructor, emit the zero initialization now. // FIXME. Do I still need this for a copy ctor synthesis? if (E->requiresZeroInitialization()) EmitNullInitialization(Dest, E->getType()); assert(!getContext().getAsConstantArrayType(E->getType()) && "EmitSynthesizedCXXCopyCtor - Copied-in Array"); EmitSynthesizedCXXCopyCtorCall(CD, Dest, Src, E); } static CharUnits CalculateCookiePadding(CodeGenFunction &CGF, const CXXNewExpr *E) { if (!E->isArray()) return CharUnits::Zero(); // No cookie is required if the operator new[] being used is the // reserved placement operator new[]. if (E->getOperatorNew()->isReservedGlobalPlacementOperator()) return CharUnits::Zero(); return CGF.CGM.getCXXABI().GetArrayCookieSize(E); } static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, const CXXNewExpr *e, unsigned minElements, llvm::Value *&numElements, llvm::Value *&sizeWithoutCookie) { QualType type = e->getAllocatedType(); if (!e->isArray()) { CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, typeSize.getQuantity()); return sizeWithoutCookie; } // The width of size_t. unsigned sizeWidth = CGF.SizeTy->getBitWidth(); // Figure out the cookie size. llvm::APInt cookieSize(sizeWidth, CalculateCookiePadding(CGF, e).getQuantity()); // Emit the array size expression. // We multiply the size of all dimensions for NumElements. // e.g for 'int[2][3]', ElemType is 'int' and NumElements is 6. numElements = ConstantEmitter(CGF).tryEmitAbstract(*e->getArraySize(), e->getType()); if (!numElements) numElements = CGF.EmitScalarExpr(*e->getArraySize()); assert(isa(numElements->getType())); // The number of elements can be have an arbitrary integer type; // essentially, we need to multiply it by a constant factor, add a // cookie size, and verify that the result is representable as a // size_t. That's just a gloss, though, and it's wrong in one // important way: if the count is negative, it's an error even if // the cookie size would bring the total size >= 0. bool isSigned = (*e->getArraySize())->getType()->isSignedIntegerOrEnumerationType(); llvm::IntegerType *numElementsType = cast(numElements->getType()); unsigned numElementsWidth = numElementsType->getBitWidth(); // Compute the constant factor. llvm::APInt arraySizeMultiplier(sizeWidth, 1); while (const ConstantArrayType *CAT = CGF.getContext().getAsConstantArrayType(type)) { type = CAT->getElementType(); arraySizeMultiplier *= CAT->getSize(); } CharUnits typeSize = CGF.getContext().getTypeSizeInChars(type); llvm::APInt typeSizeMultiplier(sizeWidth, typeSize.getQuantity()); typeSizeMultiplier *= arraySizeMultiplier; // This will be a size_t. llvm::Value *size; // If someone is doing 'new int[42]' there is no need to do a dynamic check. // Don't bloat the -O0 code. if (llvm::ConstantInt *numElementsC = dyn_cast(numElements)) { const llvm::APInt &count = numElementsC->getValue(); bool hasAnyOverflow = false; // If 'count' was a negative number, it's an overflow. if (isSigned && count.isNegative()) hasAnyOverflow = true; // We want to do all this arithmetic in size_t. If numElements is // wider than that, check whether it's already too big, and if so, // overflow. else if (numElementsWidth > sizeWidth && numElementsWidth - sizeWidth > count.countLeadingZeros()) hasAnyOverflow = true; // Okay, compute a count at the right width. llvm::APInt adjustedCount = count.zextOrTrunc(sizeWidth); // If there is a brace-initializer, we cannot allocate fewer elements than // there are initializers. If we do, that's treated like an overflow. if (adjustedCount.ult(minElements)) hasAnyOverflow = true; // Scale numElements by that. This might overflow, but we don't // care because it only overflows if allocationSize does, too, and // if that overflows then we shouldn't use this. numElements = llvm::ConstantInt::get(CGF.SizeTy, adjustedCount * arraySizeMultiplier); // Compute the size before cookie, and track whether it overflowed. bool overflow; llvm::APInt allocationSize = adjustedCount.umul_ov(typeSizeMultiplier, overflow); hasAnyOverflow |= overflow; // Add in the cookie, and check whether it's overflowed. if (cookieSize != 0) { // Save the current size without a cookie. This shouldn't be // used if there was overflow. sizeWithoutCookie = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); allocationSize = allocationSize.uadd_ov(cookieSize, overflow); hasAnyOverflow |= overflow; } // On overflow, produce a -1 so operator new will fail. if (hasAnyOverflow) { size = llvm::Constant::getAllOnesValue(CGF.SizeTy); } else { size = llvm::ConstantInt::get(CGF.SizeTy, allocationSize); } // Otherwise, we might need to use the overflow intrinsics. } else { // There are up to five conditions we need to test for: // 1) if isSigned, we need to check whether numElements is negative; // 2) if numElementsWidth > sizeWidth, we need to check whether // numElements is larger than something representable in size_t; // 3) if minElements > 0, we need to check whether numElements is smaller // than that. // 4) we need to compute // sizeWithoutCookie := numElements * typeSizeMultiplier // and check whether it overflows; and // 5) if we need a cookie, we need to compute // size := sizeWithoutCookie + cookieSize // and check whether it overflows. llvm::Value *hasOverflow = nullptr; // If numElementsWidth > sizeWidth, then one way or another, we're // going to have to do a comparison for (2), and this happens to // take care of (1), too. if (numElementsWidth > sizeWidth) { llvm::APInt threshold(numElementsWidth, 1); threshold <<= sizeWidth; llvm::Value *thresholdV = llvm::ConstantInt::get(numElementsType, threshold); hasOverflow = CGF.Builder.CreateICmpUGE(numElements, thresholdV); numElements = CGF.Builder.CreateTrunc(numElements, CGF.SizeTy); // Otherwise, if we're signed, we want to sext up to size_t. } else if (isSigned) { if (numElementsWidth < sizeWidth) numElements = CGF.Builder.CreateSExt(numElements, CGF.SizeTy); // If there's a non-1 type size multiplier, then we can do the // signedness check at the same time as we do the multiply // because a negative number times anything will cause an // unsigned overflow. Otherwise, we have to do it here. But at least // in this case, we can subsume the >= minElements check. if (typeSizeMultiplier == 1) hasOverflow = CGF.Builder.CreateICmpSLT(numElements, llvm::ConstantInt::get(CGF.SizeTy, minElements)); // Otherwise, zext up to size_t if necessary. } else if (numElementsWidth < sizeWidth) { numElements = CGF.Builder.CreateZExt(numElements, CGF.SizeTy); } assert(numElements->getType() == CGF.SizeTy); if (minElements) { // Don't allow allocation of fewer elements than we have initializers. if (!hasOverflow) { hasOverflow = CGF.Builder.CreateICmpULT(numElements, llvm::ConstantInt::get(CGF.SizeTy, minElements)); } else if (numElementsWidth > sizeWidth) { // The other existing overflow subsumes this check. // We do an unsigned comparison, since any signed value < -1 is // taken care of either above or below. hasOverflow = CGF.Builder.CreateOr(hasOverflow, CGF.Builder.CreateICmpULT(numElements, llvm::ConstantInt::get(CGF.SizeTy, minElements))); } } size = numElements; // Multiply by the type size if necessary. This multiplier // includes all the factors for nested arrays. // // This step also causes numElements to be scaled up by the // nested-array factor if necessary. Overflow on this computation // can be ignored because the result shouldn't be used if // allocation fails. if (typeSizeMultiplier != 1) { llvm::Function *umul_with_overflow = CGF.CGM.getIntrinsic(llvm::Intrinsic::umul_with_overflow, CGF.SizeTy); llvm::Value *tsmV = llvm::ConstantInt::get(CGF.SizeTy, typeSizeMultiplier); llvm::Value *result = CGF.Builder.CreateCall(umul_with_overflow, {size, tsmV}); llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); if (hasOverflow) hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); else hasOverflow = overflowed; size = CGF.Builder.CreateExtractValue(result, 0); // Also scale up numElements by the array size multiplier. if (arraySizeMultiplier != 1) { // If the base element type size is 1, then we can re-use the // multiply we just did. if (typeSize.isOne()) { assert(arraySizeMultiplier == typeSizeMultiplier); numElements = size; // Otherwise we need a separate multiply. } else { llvm::Value *asmV = llvm::ConstantInt::get(CGF.SizeTy, arraySizeMultiplier); numElements = CGF.Builder.CreateMul(numElements, asmV); } } } else { // numElements doesn't need to be scaled. assert(arraySizeMultiplier == 1); } // Add in the cookie size if necessary. if (cookieSize != 0) { sizeWithoutCookie = size; llvm::Function *uadd_with_overflow = CGF.CGM.getIntrinsic(llvm::Intrinsic::uadd_with_overflow, CGF.SizeTy); llvm::Value *cookieSizeV = llvm::ConstantInt::get(CGF.SizeTy, cookieSize); llvm::Value *result = CGF.Builder.CreateCall(uadd_with_overflow, {size, cookieSizeV}); llvm::Value *overflowed = CGF.Builder.CreateExtractValue(result, 1); if (hasOverflow) hasOverflow = CGF.Builder.CreateOr(hasOverflow, overflowed); else hasOverflow = overflowed; size = CGF.Builder.CreateExtractValue(result, 0); } // If we had any possibility of dynamic overflow, make a select to // overwrite 'size' with an all-ones value, which should cause // operator new to throw. if (hasOverflow) size = CGF.Builder.CreateSelect(hasOverflow, llvm::Constant::getAllOnesValue(CGF.SizeTy), size); } if (cookieSize == 0) sizeWithoutCookie = size; else assert(sizeWithoutCookie && "didn't set sizeWithoutCookie?"); return size; } static void StoreAnyExprIntoOneUnit(CodeGenFunction &CGF, const Expr *Init, QualType AllocType, Address NewPtr, AggValueSlot::Overlap_t MayOverlap) { // FIXME: Refactor with EmitExprAsInit. switch (CGF.getEvaluationKind(AllocType)) { case TEK_Scalar: CGF.EmitScalarInit(Init, nullptr, CGF.MakeAddrLValue(NewPtr, AllocType), false); return; case TEK_Complex: CGF.EmitComplexExprIntoLValue(Init, CGF.MakeAddrLValue(NewPtr, AllocType), /*isInit*/ true); return; case TEK_Aggregate: { AggValueSlot Slot = AggValueSlot::forAddr(NewPtr, AllocType.getQualifiers(), AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased, MayOverlap, AggValueSlot::IsNotZeroed, AggValueSlot::IsSanitizerChecked); CGF.EmitAggExpr(Init, Slot); return; } } llvm_unreachable("bad evaluation kind"); } void CodeGenFunction::EmitNewArrayInitializer( const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy, Address BeginPtr, llvm::Value *NumElements, llvm::Value *AllocSizeWithoutCookie) { // If we have a type with trivial initialization and no initializer, // there's nothing to do. if (!E->hasInitializer()) return; Address CurPtr = BeginPtr; unsigned InitListElements = 0; const Expr *Init = E->getInitializer(); Address EndOfInit = Address::invalid(); QualType::DestructionKind DtorKind = ElementType.isDestructedType(); EHScopeStack::stable_iterator Cleanup; llvm::Instruction *CleanupDominator = nullptr; CharUnits ElementSize = getContext().getTypeSizeInChars(ElementType); CharUnits ElementAlign = BeginPtr.getAlignment().alignmentOfArrayElement(ElementSize); // Attempt to perform zero-initialization using memset. auto TryMemsetInitialization = [&]() -> bool { // FIXME: If the type is a pointer-to-data-member under the Itanium ABI, // we can initialize with a memset to -1. if (!CGM.getTypes().isZeroInitializable(ElementType)) return false; // Optimization: since zero initialization will just set the memory // to all zeroes, generate a single memset to do it in one shot. // Subtract out the size of any elements we've already initialized. auto *RemainingSize = AllocSizeWithoutCookie; if (InitListElements) { // We know this can't overflow; we check this when doing the allocation. auto *InitializedSize = llvm::ConstantInt::get( RemainingSize->getType(), getContext().getTypeSizeInChars(ElementType).getQuantity() * InitListElements); RemainingSize = Builder.CreateSub(RemainingSize, InitializedSize); } // Create the memset. Builder.CreateMemSet(CurPtr, Builder.getInt8(0), RemainingSize, false); return true; }; // If the initializer is an initializer list, first do the explicit elements. if (const InitListExpr *ILE = dyn_cast(Init)) { // Initializing from a (braced) string literal is a special case; the init // list element does not initialize a (single) array element. if (ILE->isStringLiteralInit()) { // Initialize the initial portion of length equal to that of the string // literal. The allocation must be for at least this much; we emitted a // check for that earlier. AggValueSlot Slot = AggValueSlot::forAddr(CurPtr, ElementType.getQualifiers(), AggValueSlot::IsDestructed, AggValueSlot::DoesNotNeedGCBarriers, AggValueSlot::IsNotAliased, AggValueSlot::DoesNotOverlap, AggValueSlot::IsNotZeroed, AggValueSlot::IsSanitizerChecked); EmitAggExpr(ILE->getInit(0), Slot); // Move past these elements. InitListElements = cast(ILE->getType()->getAsArrayTypeUnsafe()) ->getSize().getZExtValue(); CurPtr = Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(), Builder.getSize(InitListElements), "string.init.end"), CurPtr.getAlignment().alignmentAtOffset(InitListElements * ElementSize)); // Zero out the rest, if any remain. llvm::ConstantInt *ConstNum = dyn_cast(NumElements); if (!ConstNum || !ConstNum->equalsInt(InitListElements)) { bool OK = TryMemsetInitialization(); (void)OK; assert(OK && "couldn't memset character type?"); } return; } InitListElements = ILE->getNumInits(); // If this is a multi-dimensional array new, we will initialize multiple // elements with each init list element. QualType AllocType = E->getAllocatedType(); if (const ConstantArrayType *CAT = dyn_cast_or_null( AllocType->getAsArrayTypeUnsafe())) { ElementTy = ConvertTypeForMem(AllocType); CurPtr = Builder.CreateElementBitCast(CurPtr, ElementTy); InitListElements *= getContext().getConstantArrayElementCount(CAT); } // Enter a partial-destruction Cleanup if necessary. if (needsEHCleanup(DtorKind)) { // In principle we could tell the Cleanup where we are more // directly, but the control flow can get so varied here that it // would actually be quite complex. Therefore we go through an // alloca. EndOfInit = CreateTempAlloca(BeginPtr.getType(), getPointerAlign(), "array.init.end"); CleanupDominator = Builder.CreateStore(BeginPtr.getPointer(), EndOfInit); pushIrregularPartialArrayCleanup(BeginPtr.getPointer(), EndOfInit, ElementType, ElementAlign, getDestroyer(DtorKind)); Cleanup = EHStack.stable_begin(); } CharUnits StartAlign = CurPtr.getAlignment(); for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) { // Tell the cleanup that it needs to destroy up to this // element. TODO: some of these stores can be trivially // observed to be unnecessary. if (EndOfInit.isValid()) { auto FinishedPtr = Builder.CreateBitCast(CurPtr.getPointer(), BeginPtr.getType()); Builder.CreateStore(FinishedPtr, EndOfInit); } // FIXME: If the last initializer is an incomplete initializer list for // an array, and we have an array filler, we can fold together the two // initialization loops. StoreAnyExprIntoOneUnit(*this, ILE->getInit(i), ILE->getInit(i)->getType(), CurPtr, AggValueSlot::DoesNotOverlap); CurPtr = Address(Builder.CreateInBoundsGEP(CurPtr.getPointer(), Builder.getSize(1), "array.exp.next"), StartAlign.alignmentAtOffset((i + 1) * ElementSize)); } // The remaining elements are filled with the array filler expression. Init = ILE->getArrayFiller(); // Extract the initializer for the individual array elements by pulling // out the array filler from all the nested initializer lists. This avoids // generating a nested loop for the initialization. while (Init && Init->getType()->isConstantArrayType()) { auto *SubILE = dyn_cast(Init); if (!SubILE) break; assert(SubILE->getNumInits() == 0 && "explicit inits in array filler?"); Init = SubILE->getArrayFiller(); } // Switch back to initializing one base element at a time. CurPtr = Builder.CreateBitCast(CurPtr, BeginPtr.getType()); } // If all elements have already been initialized, skip any further // initialization. llvm::ConstantInt *ConstNum = dyn_cast(NumElements); if (ConstNum && ConstNum->getZExtValue() <= InitListElements) { // If there was a Cleanup, deactivate it. if (CleanupDominator) DeactivateCleanupBlock(Cleanup, CleanupDominator); return; } assert(Init && "have trailing elements to initialize but no initializer"); // If this is a constructor call, try to optimize it out, and failing that // emit a single loop to initialize all remaining elements. if (const CXXConstructExpr *CCE = dyn_cast(Init)) { CXXConstructorDecl *Ctor = CCE->getConstructor(); if (Ctor->isTrivial()) { // If new expression did not specify value-initialization, then there // is no initialization. if (!CCE->requiresZeroInitialization() || Ctor->getParent()->isEmpty()) return; if (TryMemsetInitialization()) return; } // Store the new Cleanup position for irregular Cleanups. // // FIXME: Share this cleanup with the constructor call emission rather than // having it create a cleanup of its own. if (EndOfInit.isValid()) Builder.CreateStore(CurPtr.getPointer(), EndOfInit); // Emit a constructor call loop to initialize the remaining elements. if (InitListElements) NumElements = Builder.CreateSub( NumElements, llvm::ConstantInt::get(NumElements->getType(), InitListElements)); EmitCXXAggrConstructorCall(Ctor, NumElements, CurPtr, CCE, /*NewPointerIsChecked*/true, CCE->requiresZeroInitialization()); return; } // If this is value-initialization, we can usually use memset. ImplicitValueInitExpr IVIE(ElementType); if (isa(Init)) { if (TryMemsetInitialization()) return; // Switch to an ImplicitValueInitExpr for the element type. This handles // only one case: multidimensional array new of pointers to members. In // all other cases, we already have an initializer for the array element. Init = &IVIE; } // At this point we should have found an initializer for the individual // elements of the array. assert(getContext().hasSameUnqualifiedType(ElementType, Init->getType()) && "got wrong type of element to initialize"); // If we have an empty initializer list, we can usually use memset. if (auto *ILE = dyn_cast(Init)) if (ILE->getNumInits() == 0 && TryMemsetInitialization()) return; // If we have a struct whose every field is value-initialized, we can // usually use memset. if (auto *ILE = dyn_cast(Init)) { if (const RecordType *RType = ILE->getType()->getAs()) { if (RType->getDecl()->isStruct()) { unsigned NumElements = 0; if (auto *CXXRD = dyn_cast(RType->getDecl())) NumElements = CXXRD->getNumBases(); for (auto *Field : RType->getDecl()->fields()) if (!Field->isUnnamedBitfield()) ++NumElements; // FIXME: Recurse into nested InitListExprs. if (ILE->getNumInits() == NumElements) for (unsigned i = 0, e = ILE->getNumInits(); i != e; ++i) if (!isa(ILE->getInit(i))) --NumElements; if (ILE->getNumInits() == NumElements && TryMemsetInitialization()) return; } } } // Create the loop blocks. llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); llvm::BasicBlock *LoopBB = createBasicBlock("new.loop"); llvm::BasicBlock *ContBB = createBasicBlock("new.loop.end"); // Find the end of the array, hoisted out of the loop. llvm::Value *EndPtr = Builder.CreateInBoundsGEP(BeginPtr.getPointer(), NumElements, "array.end"); // If the number of elements isn't constant, we have to now check if there is // anything left to initialize. if (!ConstNum) { llvm::Value *IsEmpty = Builder.CreateICmpEQ(CurPtr.getPointer(), EndPtr, "array.isempty"); Builder.CreateCondBr(IsEmpty, ContBB, LoopBB); } // Enter the loop. EmitBlock(LoopBB); // Set up the current-element phi. llvm::PHINode *CurPtrPhi = Builder.CreatePHI(CurPtr.getType(), 2, "array.cur"); CurPtrPhi->addIncoming(CurPtr.getPointer(), EntryBB); CurPtr = Address(CurPtrPhi, ElementAlign); // Store the new Cleanup position for irregular Cleanups. if (EndOfInit.isValid()) Builder.CreateStore(CurPtr.getPointer(), EndOfInit); // Enter a partial-destruction Cleanup if necessary. if (!CleanupDominator && needsEHCleanup(DtorKind)) { pushRegularPartialArrayCleanup(BeginPtr.getPointer(), CurPtr.getPointer(), ElementType, ElementAlign, getDestroyer(DtorKind)); Cleanup = EHStack.stable_begin(); CleanupDominator = Builder.CreateUnreachable(); } // Emit the initializer into this element. StoreAnyExprIntoOneUnit(*this, Init, Init->getType(), CurPtr, AggValueSlot::DoesNotOverlap); // Leave the Cleanup if we entered one. if (CleanupDominator) { DeactivateCleanupBlock(Cleanup, CleanupDominator); CleanupDominator->eraseFromParent(); } // Advance to the next element by adjusting the pointer type as necessary. llvm::Value *NextPtr = Builder.CreateConstInBoundsGEP1_32(ElementTy, CurPtr.getPointer(), 1, "array.next"); // Check whether we've gotten to the end of the array and, if so, // exit the loop. llvm::Value *IsEnd = Builder.CreateICmpEQ(NextPtr, EndPtr, "array.atend"); Builder.CreateCondBr(IsEnd, ContBB, LoopBB); CurPtrPhi->addIncoming(NextPtr, Builder.GetInsertBlock()); EmitBlock(ContBB); } static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, QualType ElementType, llvm::Type *ElementTy, Address NewPtr, llvm::Value *NumElements, llvm::Value *AllocSizeWithoutCookie) { ApplyDebugLocation DL(CGF, E); if (E->isArray()) CGF.EmitNewArrayInitializer(E, ElementType, ElementTy, NewPtr, NumElements, AllocSizeWithoutCookie); else if (const Expr *Init = E->getInitializer()) StoreAnyExprIntoOneUnit(CGF, Init, E->getAllocatedType(), NewPtr, AggValueSlot::DoesNotOverlap); } /// Emit a call to an operator new or operator delete function, as implicitly /// created by new-expressions and delete-expressions. static RValue EmitNewDeleteCall(CodeGenFunction &CGF, const FunctionDecl *CalleeDecl, const FunctionProtoType *CalleeType, const CallArgList &Args) { llvm::CallBase *CallOrInvoke; llvm::Constant *CalleePtr = CGF.CGM.GetAddrOfFunction(CalleeDecl); CGCallee Callee = CGCallee::forDirect(CalleePtr, GlobalDecl(CalleeDecl)); RValue RV = CGF.EmitCall(CGF.CGM.getTypes().arrangeFreeFunctionCall( Args, CalleeType, /*ChainCall=*/false), Callee, ReturnValueSlot(), Args, &CallOrInvoke); /// C++1y [expr.new]p10: /// [In a new-expression,] an implementation is allowed to omit a call /// to a replaceable global allocation function. /// /// We model such elidable calls with the 'builtin' attribute. llvm::Function *Fn = dyn_cast(CalleePtr); if (CalleeDecl->isReplaceableGlobalAllocationFunction() && Fn && Fn->hasFnAttribute(llvm::Attribute::NoBuiltin)) { CallOrInvoke->addAttribute(llvm::AttributeList::FunctionIndex, llvm::Attribute::Builtin); } return RV; } RValue CodeGenFunction::EmitBuiltinNewDeleteCall(const FunctionProtoType *Type, const CallExpr *TheCall, bool IsDelete) { CallArgList Args; EmitCallArgs(Args, Type->getParamTypes(), TheCall->arguments()); // Find the allocation or deallocation function that we're calling. ASTContext &Ctx = getContext(); DeclarationName Name = Ctx.DeclarationNames .getCXXOperatorName(IsDelete ? OO_Delete : OO_New); for (auto *Decl : Ctx.getTranslationUnitDecl()->lookup(Name)) if (auto *FD = dyn_cast(Decl)) if (Ctx.hasSameType(FD->getType(), QualType(Type, 0))) return EmitNewDeleteCall(*this, FD, Type, Args); llvm_unreachable("predeclared global operator new/delete is missing"); } namespace { /// The parameters to pass to a usual operator delete. struct UsualDeleteParams { bool DestroyingDelete = false; bool Size = false; bool Alignment = false; }; } static UsualDeleteParams getUsualDeleteParams(const FunctionDecl *FD) { UsualDeleteParams Params; const FunctionProtoType *FPT = FD->getType()->castAs(); auto AI = FPT->param_type_begin(), AE = FPT->param_type_end(); // The first argument is always a void*. ++AI; // The next parameter may be a std::destroying_delete_t. if (FD->isDestroyingOperatorDelete()) { Params.DestroyingDelete = true; assert(AI != AE); ++AI; } // Figure out what other parameters we should be implicitly passing. if (AI != AE && (*AI)->isIntegerType()) { Params.Size = true; ++AI; } if (AI != AE && (*AI)->isAlignValT()) { Params.Alignment = true; ++AI; } assert(AI == AE && "unexpected usual deallocation function parameter"); return Params; } namespace { /// A cleanup to call the given 'operator delete' function upon abnormal /// exit from a new expression. Templated on a traits type that deals with /// ensuring that the arguments dominate the cleanup if necessary. template class CallDeleteDuringNew final : public EHScopeStack::Cleanup { /// Type used to hold llvm::Value*s. typedef typename Traits::ValueTy ValueTy; /// Type used to hold RValues. typedef typename Traits::RValueTy RValueTy; struct PlacementArg { RValueTy ArgValue; QualType ArgType; }; unsigned NumPlacementArgs : 31; unsigned PassAlignmentToPlacementDelete : 1; const FunctionDecl *OperatorDelete; ValueTy Ptr; ValueTy AllocSize; CharUnits AllocAlign; PlacementArg *getPlacementArgs() { return reinterpret_cast(this + 1); } public: static size_t getExtraSize(size_t NumPlacementArgs) { return NumPlacementArgs * sizeof(PlacementArg); } CallDeleteDuringNew(size_t NumPlacementArgs, const FunctionDecl *OperatorDelete, ValueTy Ptr, ValueTy AllocSize, bool PassAlignmentToPlacementDelete, CharUnits AllocAlign) : NumPlacementArgs(NumPlacementArgs), PassAlignmentToPlacementDelete(PassAlignmentToPlacementDelete), OperatorDelete(OperatorDelete), Ptr(Ptr), AllocSize(AllocSize), AllocAlign(AllocAlign) {} void setPlacementArg(unsigned I, RValueTy Arg, QualType Type) { assert(I < NumPlacementArgs && "index out of range"); getPlacementArgs()[I] = {Arg, Type}; } void Emit(CodeGenFunction &CGF, Flags flags) override { const auto *FPT = OperatorDelete->getType()->castAs(); CallArgList DeleteArgs; // The first argument is always a void* (or C* for a destroying operator // delete for class type C). DeleteArgs.add(Traits::get(CGF, Ptr), FPT->getParamType(0)); // Figure out what other parameters we should be implicitly passing. UsualDeleteParams Params; if (NumPlacementArgs) { // A placement deallocation function is implicitly passed an alignment // if the placement allocation function was, but is never passed a size. Params.Alignment = PassAlignmentToPlacementDelete; } else { // For a non-placement new-expression, 'operator delete' can take a // size and/or an alignment if it has the right parameters. Params = getUsualDeleteParams(OperatorDelete); } assert(!Params.DestroyingDelete && "should not call destroying delete in a new-expression"); // The second argument can be a std::size_t (for non-placement delete). if (Params.Size) DeleteArgs.add(Traits::get(CGF, AllocSize), CGF.getContext().getSizeType()); // The next (second or third) argument can be a std::align_val_t, which // is an enum whose underlying type is std::size_t. // FIXME: Use the right type as the parameter type. Note that in a call // to operator delete(size_t, ...), we may not have it available. if (Params.Alignment) DeleteArgs.add(RValue::get(llvm::ConstantInt::get( CGF.SizeTy, AllocAlign.getQuantity())), CGF.getContext().getSizeType()); // Pass the rest of the arguments, which must match exactly. for (unsigned I = 0; I != NumPlacementArgs; ++I) { auto Arg = getPlacementArgs()[I]; DeleteArgs.add(Traits::get(CGF, Arg.ArgValue), Arg.ArgType); } // Call 'operator delete'. EmitNewDeleteCall(CGF, OperatorDelete, FPT, DeleteArgs); } }; } /// Enter a cleanup to call 'operator delete' if the initializer in a /// new-expression throws. static void EnterNewDeleteCleanup(CodeGenFunction &CGF, const CXXNewExpr *E, Address NewPtr, llvm::Value *AllocSize, CharUnits AllocAlign, const CallArgList &NewArgs) { unsigned NumNonPlacementArgs = E->passAlignment() ? 2 : 1; // If we're not inside a conditional branch, then the cleanup will // dominate and we can do the easier (and more efficient) thing. if (!CGF.isInConditionalBranch()) { struct DirectCleanupTraits { typedef llvm::Value *ValueTy; typedef RValue RValueTy; static RValue get(CodeGenFunction &, ValueTy V) { return RValue::get(V); } static RValue get(CodeGenFunction &, RValueTy V) { return V; } }; typedef CallDeleteDuringNew DirectCleanup; DirectCleanup *Cleanup = CGF.EHStack .pushCleanupWithExtra(EHCleanup, E->getNumPlacementArgs(), E->getOperatorDelete(), NewPtr.getPointer(), AllocSize, E->passAlignment(), AllocAlign); for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { auto &Arg = NewArgs[I + NumNonPlacementArgs]; Cleanup->setPlacementArg(I, Arg.getRValue(CGF), Arg.Ty); } return; } // Otherwise, we need to save all this stuff. DominatingValue::saved_type SavedNewPtr = DominatingValue::save(CGF, RValue::get(NewPtr.getPointer())); DominatingValue::saved_type SavedAllocSize = DominatingValue::save(CGF, RValue::get(AllocSize)); struct ConditionalCleanupTraits { typedef DominatingValue::saved_type ValueTy; typedef DominatingValue::saved_type RValueTy; static RValue get(CodeGenFunction &CGF, ValueTy V) { return V.restore(CGF); } }; typedef CallDeleteDuringNew ConditionalCleanup; ConditionalCleanup *Cleanup = CGF.EHStack .pushCleanupWithExtra(EHCleanup, E->getNumPlacementArgs(), E->getOperatorDelete(), SavedNewPtr, SavedAllocSize, E->passAlignment(), AllocAlign); for (unsigned I = 0, N = E->getNumPlacementArgs(); I != N; ++I) { auto &Arg = NewArgs[I + NumNonPlacementArgs]; Cleanup->setPlacementArg( I, DominatingValue::save(CGF, Arg.getRValue(CGF)), Arg.Ty); } CGF.initFullExprCleanup(); } llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { // The element type being allocated. QualType allocType = getContext().getBaseElementType(E->getAllocatedType()); // 1. Build a call to the allocation function. FunctionDecl *allocator = E->getOperatorNew(); // If there is a brace-initializer, cannot allocate fewer elements than inits. unsigned minElements = 0; if (E->isArray() && E->hasInitializer()) { const InitListExpr *ILE = dyn_cast(E->getInitializer()); if (ILE && ILE->isStringLiteralInit()) minElements = cast(ILE->getType()->getAsArrayTypeUnsafe()) ->getSize().getZExtValue(); else if (ILE) minElements = ILE->getNumInits(); } llvm::Value *numElements = nullptr; llvm::Value *allocSizeWithoutCookie = nullptr; llvm::Value *allocSize = EmitCXXNewAllocSize(*this, E, minElements, numElements, allocSizeWithoutCookie); CharUnits allocAlign = getContext().getTypeAlignInChars(allocType); // Emit the allocation call. If the allocator is a global placement // operator, just "inline" it directly. Address allocation = Address::invalid(); CallArgList allocatorArgs; if (allocator->isReservedGlobalPlacementOperator()) { assert(E->getNumPlacementArgs() == 1); const Expr *arg = *E->placement_arguments().begin(); LValueBaseInfo BaseInfo; allocation = EmitPointerWithAlignment(arg, &BaseInfo); // The pointer expression will, in many cases, be an opaque void*. // In these cases, discard the computed alignment and use the // formal alignment of the allocated type. if (BaseInfo.getAlignmentSource() != AlignmentSource::Decl) allocation = Address(allocation.getPointer(), allocAlign); // Set up allocatorArgs for the call to operator delete if it's not // the reserved global operator. if (E->getOperatorDelete() && !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { allocatorArgs.add(RValue::get(allocSize), getContext().getSizeType()); allocatorArgs.add(RValue::get(allocation.getPointer()), arg->getType()); } } else { const FunctionProtoType *allocatorType = allocator->getType()->castAs(); unsigned ParamsToSkip = 0; // The allocation size is the first argument. QualType sizeType = getContext().getSizeType(); allocatorArgs.add(RValue::get(allocSize), sizeType); ++ParamsToSkip; if (allocSize != allocSizeWithoutCookie) { CharUnits cookieAlign = getSizeAlign(); // FIXME: Ask the ABI. allocAlign = std::max(allocAlign, cookieAlign); } // The allocation alignment may be passed as the second argument. if (E->passAlignment()) { QualType AlignValT = sizeType; if (allocatorType->getNumParams() > 1) { AlignValT = allocatorType->getParamType(1); assert(getContext().hasSameUnqualifiedType( AlignValT->castAs()->getDecl()->getIntegerType(), sizeType) && "wrong type for alignment parameter"); ++ParamsToSkip; } else { // Corner case, passing alignment to 'operator new(size_t, ...)'. assert(allocator->isVariadic() && "can't pass alignment to allocator"); } allocatorArgs.add( RValue::get(llvm::ConstantInt::get(SizeTy, allocAlign.getQuantity())), AlignValT); } // FIXME: Why do we not pass a CalleeDecl here? EmitCallArgs(allocatorArgs, allocatorType, E->placement_arguments(), /*AC*/AbstractCallee(), /*ParamsToSkip*/ParamsToSkip); RValue RV = EmitNewDeleteCall(*this, allocator, allocatorType, allocatorArgs); // If this was a call to a global replaceable allocation function that does // not take an alignment argument, the allocator is known to produce // storage that's suitably aligned for any object that fits, up to a known // threshold. Otherwise assume it's suitably aligned for the allocated type. CharUnits allocationAlign = allocAlign; if (!E->passAlignment() && allocator->isReplaceableGlobalAllocationFunction()) { unsigned AllocatorAlign = llvm::PowerOf2Floor(std::min( Target.getNewAlign(), getContext().getTypeSize(allocType))); allocationAlign = std::max( allocationAlign, getContext().toCharUnitsFromBits(AllocatorAlign)); } allocation = Address(RV.getScalarVal(), allocationAlign); } // Emit a null check on the allocation result if the allocation // function is allowed to return null (because it has a non-throwing // exception spec or is the reserved placement new) and we have an // interesting initializer will be running sanitizers on the initialization. bool nullCheck = E->shouldNullCheckAllocation() && (!allocType.isPODType(getContext()) || E->hasInitializer() || sanitizePerformTypeCheck()); llvm::BasicBlock *nullCheckBB = nullptr; llvm::BasicBlock *contBB = nullptr; // The null-check means that the initializer is conditionally // evaluated. ConditionalEvaluation conditional(*this); if (nullCheck) { conditional.begin(*this); nullCheckBB = Builder.GetInsertBlock(); llvm::BasicBlock *notNullBB = createBasicBlock("new.notnull"); contBB = createBasicBlock("new.cont"); llvm::Value *isNull = Builder.CreateIsNull(allocation.getPointer(), "new.isnull"); Builder.CreateCondBr(isNull, contBB, notNullBB); EmitBlock(notNullBB); } // If there's an operator delete, enter a cleanup to call it if an // exception is thrown. EHScopeStack::stable_iterator operatorDeleteCleanup; llvm::Instruction *cleanupDominator = nullptr; if (E->getOperatorDelete() && !E->getOperatorDelete()->isReservedGlobalPlacementOperator()) { EnterNewDeleteCleanup(*this, E, allocation, allocSize, allocAlign, allocatorArgs); operatorDeleteCleanup = EHStack.stable_begin(); cleanupDominator = Builder.CreateUnreachable(); } assert((allocSize == allocSizeWithoutCookie) == CalculateCookiePadding(*this, E).isZero()); if (allocSize != allocSizeWithoutCookie) { assert(E->isArray()); allocation = CGM.getCXXABI().InitializeArrayCookie(*this, allocation, numElements, E, allocType); } llvm::Type *elementTy = ConvertTypeForMem(allocType); Address result = Builder.CreateElementBitCast(allocation, elementTy); // Passing pointer through launder.invariant.group to avoid propagation of // vptrs information which may be included in previous type. // To not break LTO with different optimizations levels, we do it regardless // of optimization level. if (CGM.getCodeGenOpts().StrictVTablePointers && allocator->isReservedGlobalPlacementOperator()) result = Address(Builder.CreateLaunderInvariantGroup(result.getPointer()), result.getAlignment()); // Emit sanitizer checks for pointer value now, so that in the case of an // array it was checked only once and not at each constructor call. We may // have already checked that the pointer is non-null. // FIXME: If we have an array cookie and a potentially-throwing allocator, // we'll null check the wrong pointer here. SanitizerSet SkippedChecks; SkippedChecks.set(SanitizerKind::Null, nullCheck); EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall, E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(), result.getPointer(), allocType, result.getAlignment(), SkippedChecks, numElements); EmitNewInitializer(*this, E, allocType, elementTy, result, numElements, allocSizeWithoutCookie); if (E->isArray()) { // NewPtr is a pointer to the base element type. If we're // allocating an array of arrays, we'll need to cast back to the // array pointer type. llvm::Type *resultType = ConvertTypeForMem(E->getType()); if (result.getType() != resultType) result = Builder.CreateBitCast(result, resultType); } // Deactivate the 'operator delete' cleanup if we finished // initialization. if (operatorDeleteCleanup.isValid()) { DeactivateCleanupBlock(operatorDeleteCleanup, cleanupDominator); cleanupDominator->eraseFromParent(); } llvm::Value *resultPtr = result.getPointer(); if (nullCheck) { conditional.end(*this); llvm::BasicBlock *notNullBB = Builder.GetInsertBlock(); EmitBlock(contBB); llvm::PHINode *PHI = Builder.CreatePHI(resultPtr->getType(), 2); PHI->addIncoming(resultPtr, notNullBB); PHI->addIncoming(llvm::Constant::getNullValue(resultPtr->getType()), nullCheckBB); resultPtr = PHI; } return resultPtr; } void CodeGenFunction::EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr, QualType DeleteTy, llvm::Value *NumElements, CharUnits CookieSize) { assert((!NumElements && CookieSize.isZero()) || DeleteFD->getOverloadedOperator() == OO_Array_Delete); const auto *DeleteFTy = DeleteFD->getType()->castAs(); CallArgList DeleteArgs; auto Params = getUsualDeleteParams(DeleteFD); auto ParamTypeIt = DeleteFTy->param_type_begin(); // Pass the pointer itself. QualType ArgTy = *ParamTypeIt++; llvm::Value *DeletePtr = Builder.CreateBitCast(Ptr, ConvertType(ArgTy)); DeleteArgs.add(RValue::get(DeletePtr), ArgTy); // Pass the std::destroying_delete tag if present. if (Params.DestroyingDelete) { QualType DDTag = *ParamTypeIt++; // Just pass an 'undef'. We expect the tag type to be an empty struct. auto *V = llvm::UndefValue::get(getTypes().ConvertType(DDTag)); DeleteArgs.add(RValue::get(V), DDTag); } // Pass the size if the delete function has a size_t parameter. if (Params.Size) { QualType SizeType = *ParamTypeIt++; CharUnits DeleteTypeSize = getContext().getTypeSizeInChars(DeleteTy); llvm::Value *Size = llvm::ConstantInt::get(ConvertType(SizeType), DeleteTypeSize.getQuantity()); // For array new, multiply by the number of elements. if (NumElements) Size = Builder.CreateMul(Size, NumElements); // If there is a cookie, add the cookie size. if (!CookieSize.isZero()) Size = Builder.CreateAdd( Size, llvm::ConstantInt::get(SizeTy, CookieSize.getQuantity())); DeleteArgs.add(RValue::get(Size), SizeType); } // Pass the alignment if the delete function has an align_val_t parameter. if (Params.Alignment) { QualType AlignValType = *ParamTypeIt++; CharUnits DeleteTypeAlign = getContext().toCharUnitsFromBits( getContext().getTypeAlignIfKnown(DeleteTy)); llvm::Value *Align = llvm::ConstantInt::get(ConvertType(AlignValType), DeleteTypeAlign.getQuantity()); DeleteArgs.add(RValue::get(Align), AlignValType); } assert(ParamTypeIt == DeleteFTy->param_type_end() && "unknown parameter to usual delete function"); // Emit the call to delete. EmitNewDeleteCall(*this, DeleteFD, DeleteFTy, DeleteArgs); } namespace { /// Calls the given 'operator delete' on a single object. struct CallObjectDelete final : EHScopeStack::Cleanup { llvm::Value *Ptr; const FunctionDecl *OperatorDelete; QualType ElementType; CallObjectDelete(llvm::Value *Ptr, const FunctionDecl *OperatorDelete, QualType ElementType) : Ptr(Ptr), OperatorDelete(OperatorDelete), ElementType(ElementType) {} void Emit(CodeGenFunction &CGF, Flags flags) override { CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType); } }; } void CodeGenFunction::pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete, llvm::Value *CompletePtr, QualType ElementType) { EHStack.pushCleanup(NormalAndEHCleanup, CompletePtr, OperatorDelete, ElementType); } /// Emit the code for deleting a single object with a destroying operator /// delete. If the element type has a non-virtual destructor, Ptr has already /// been converted to the type of the parameter of 'operator delete'. Otherwise /// Ptr points to an object of the static type. static void EmitDestroyingObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, Address Ptr, QualType ElementType) { auto *Dtor = ElementType->getAsCXXRecordDecl()->getDestructor(); if (Dtor && Dtor->isVirtual()) CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, Dtor); else CGF.EmitDeleteCall(DE->getOperatorDelete(), Ptr.getPointer(), ElementType); } /// Emit the code for deleting a single object. static void EmitObjectDelete(CodeGenFunction &CGF, const CXXDeleteExpr *DE, Address Ptr, QualType ElementType) { // C++11 [expr.delete]p3: // If the static type of the object to be deleted is different from its // dynamic type, the static type shall be a base class of the dynamic type // of the object to be deleted and the static type shall have a virtual // destructor or the behavior is undefined. CGF.EmitTypeCheck(CodeGenFunction::TCK_MemberCall, DE->getExprLoc(), Ptr.getPointer(), ElementType); const FunctionDecl *OperatorDelete = DE->getOperatorDelete(); assert(!OperatorDelete->isDestroyingOperatorDelete()); // Find the destructor for the type, if applicable. If the // destructor is virtual, we'll just emit the vcall and return. const CXXDestructorDecl *Dtor = nullptr; if (const RecordType *RT = ElementType->getAs()) { CXXRecordDecl *RD = cast(RT->getDecl()); if (RD->hasDefinition() && !RD->hasTrivialDestructor()) { Dtor = RD->getDestructor(); if (Dtor->isVirtual()) { bool UseVirtualCall = true; const Expr *Base = DE->getArgument(); if (auto *DevirtualizedDtor = dyn_cast_or_null( Dtor->getDevirtualizedMethod( Base, CGF.CGM.getLangOpts().AppleKext))) { UseVirtualCall = false; const CXXRecordDecl *DevirtualizedClass = DevirtualizedDtor->getParent(); if (declaresSameEntity(getCXXRecord(Base), DevirtualizedClass)) { // Devirtualized to the class of the base type (the type of the // whole expression). Dtor = DevirtualizedDtor; } else { // Devirtualized to some other type. Would need to cast the this // pointer to that type but we don't have support for that yet, so // do a virtual call. FIXME: handle the case where it is // devirtualized to the derived type (the type of the inner // expression) as in EmitCXXMemberOrOperatorMemberCallExpr. UseVirtualCall = true; } } if (UseVirtualCall) { CGF.CGM.getCXXABI().emitVirtualObjectDelete(CGF, DE, Ptr, ElementType, Dtor); return; } } } } // Make sure that we call delete even if the dtor throws. // This doesn't have to a conditional cleanup because we're going // to pop it off in a second. CGF.EHStack.pushCleanup(NormalAndEHCleanup, Ptr.getPointer(), OperatorDelete, ElementType); if (Dtor) CGF.EmitCXXDestructorCall(Dtor, Dtor_Complete, /*ForVirtualBase=*/false, /*Delegating=*/false, Ptr, ElementType); else if (auto Lifetime = ElementType.getObjCLifetime()) { switch (Lifetime) { case Qualifiers::OCL_None: case Qualifiers::OCL_ExplicitNone: case Qualifiers::OCL_Autoreleasing: break; case Qualifiers::OCL_Strong: CGF.EmitARCDestroyStrong(Ptr, ARCPreciseLifetime); break; case Qualifiers::OCL_Weak: CGF.EmitARCDestroyWeak(Ptr); break; } } CGF.PopCleanupBlock(); } namespace { /// Calls the given 'operator delete' on an array of objects. struct CallArrayDelete final : EHScopeStack::Cleanup { llvm::Value *Ptr; const FunctionDecl *OperatorDelete; llvm::Value *NumElements; QualType ElementType; CharUnits CookieSize; CallArrayDelete(llvm::Value *Ptr, const FunctionDecl *OperatorDelete, llvm::Value *NumElements, QualType ElementType, CharUnits CookieSize) : Ptr(Ptr), OperatorDelete(OperatorDelete), NumElements(NumElements), ElementType(ElementType), CookieSize(CookieSize) {} void Emit(CodeGenFunction &CGF, Flags flags) override { CGF.EmitDeleteCall(OperatorDelete, Ptr, ElementType, NumElements, CookieSize); } }; } /// Emit the code for deleting an array of objects. static void EmitArrayDelete(CodeGenFunction &CGF, const CXXDeleteExpr *E, Address deletedPtr, QualType elementType) { llvm::Value *numElements = nullptr; llvm::Value *allocatedPtr = nullptr; CharUnits cookieSize; CGF.CGM.getCXXABI().ReadArrayCookie(CGF, deletedPtr, E, elementType, numElements, allocatedPtr, cookieSize); assert(allocatedPtr && "ReadArrayCookie didn't set allocated pointer"); // Make sure that we call delete even if one of the dtors throws. const FunctionDecl *operatorDelete = E->getOperatorDelete(); CGF.EHStack.pushCleanup(NormalAndEHCleanup, allocatedPtr, operatorDelete, numElements, elementType, cookieSize); // Destroy the elements. if (QualType::DestructionKind dtorKind = elementType.isDestructedType()) { assert(numElements && "no element count for a type with a destructor!"); CharUnits elementSize = CGF.getContext().getTypeSizeInChars(elementType); CharUnits elementAlign = deletedPtr.getAlignment().alignmentOfArrayElement(elementSize); llvm::Value *arrayBegin = deletedPtr.getPointer(); llvm::Value *arrayEnd = CGF.Builder.CreateInBoundsGEP(arrayBegin, numElements, "delete.end"); // Note that it is legal to allocate a zero-length array, and we // can never fold the check away because the length should always // come from a cookie. CGF.emitArrayDestroy(arrayBegin, arrayEnd, elementType, elementAlign, CGF.getDestroyer(dtorKind), /*checkZeroLength*/ true, CGF.needsEHCleanup(dtorKind)); } // Pop the cleanup block. CGF.PopCleanupBlock(); } void CodeGenFunction::EmitCXXDeleteExpr(const CXXDeleteExpr *E) { const Expr *Arg = E->getArgument(); Address Ptr = EmitPointerWithAlignment(Arg); // Null check the pointer. llvm::BasicBlock *DeleteNotNull = createBasicBlock("delete.notnull"); llvm::BasicBlock *DeleteEnd = createBasicBlock("delete.end"); llvm::Value *IsNull = Builder.CreateIsNull(Ptr.getPointer(), "isnull"); Builder.CreateCondBr(IsNull, DeleteEnd, DeleteNotNull); EmitBlock(DeleteNotNull); QualType DeleteTy = E->getDestroyedType(); // A destroying operator delete overrides the entire operation of the // delete expression. if (E->getOperatorDelete()->isDestroyingOperatorDelete()) { EmitDestroyingObjectDelete(*this, E, Ptr, DeleteTy); EmitBlock(DeleteEnd); return; } // We might be deleting a pointer to array. If so, GEP down to the // first non-array element. // (this assumes that A(*)[3][7] is converted to [3 x [7 x %A]]*) if (DeleteTy->isConstantArrayType()) { llvm::Value *Zero = Builder.getInt32(0); SmallVector GEP; GEP.push_back(Zero); // point at the outermost array // For each layer of array type we're pointing at: while (const ConstantArrayType *Arr = getContext().getAsConstantArrayType(DeleteTy)) { // 1. Unpeel the array type. DeleteTy = Arr->getElementType(); // 2. GEP to the first element of the array. GEP.push_back(Zero); } Ptr = Address(Builder.CreateInBoundsGEP(Ptr.getPointer(), GEP, "del.first"), Ptr.getAlignment()); } assert(ConvertTypeForMem(DeleteTy) == Ptr.getElementType()); if (E->isArrayForm()) { EmitArrayDelete(*this, E, Ptr, DeleteTy); } else { EmitObjectDelete(*this, E, Ptr, DeleteTy); } EmitBlock(DeleteEnd); } static bool isGLValueFromPointerDeref(const Expr *E) { E = E->IgnoreParens(); if (const auto *CE = dyn_cast(E)) { if (!CE->getSubExpr()->isGLValue()) return false; return isGLValueFromPointerDeref(CE->getSubExpr()); } if (const auto *OVE = dyn_cast(E)) return isGLValueFromPointerDeref(OVE->getSourceExpr()); if (const auto *BO = dyn_cast(E)) if (BO->getOpcode() == BO_Comma) return isGLValueFromPointerDeref(BO->getRHS()); if (const auto *ACO = dyn_cast(E)) return isGLValueFromPointerDeref(ACO->getTrueExpr()) || isGLValueFromPointerDeref(ACO->getFalseExpr()); // C++11 [expr.sub]p1: // The expression E1[E2] is identical (by definition) to *((E1)+(E2)) if (isa(E)) return true; if (const auto *UO = dyn_cast(E)) if (UO->getOpcode() == UO_Deref) return true; return false; } static llvm::Value *EmitTypeidFromVTable(CodeGenFunction &CGF, const Expr *E, llvm::Type *StdTypeInfoPtrTy) { // Get the vtable pointer. Address ThisPtr = CGF.EmitLValue(E).getAddress(CGF); QualType SrcRecordTy = E->getType(); // C++ [class.cdtor]p4: // If the operand of typeid refers to the object under construction or // destruction and the static type of the operand is neither the constructor // or destructor’s class nor one of its bases, the behavior is undefined. CGF.EmitTypeCheck(CodeGenFunction::TCK_DynamicOperation, E->getExprLoc(), ThisPtr.getPointer(), SrcRecordTy); // C++ [expr.typeid]p2: // If the glvalue expression is obtained by applying the unary * operator to // a pointer and the pointer is a null pointer value, the typeid expression // throws the std::bad_typeid exception. // // However, this paragraph's intent is not clear. We choose a very generous // interpretation which implores us to consider comma operators, conditional // operators, parentheses and other such constructs. if (CGF.CGM.getCXXABI().shouldTypeidBeNullChecked( isGLValueFromPointerDeref(E), SrcRecordTy)) { llvm::BasicBlock *BadTypeidBlock = CGF.createBasicBlock("typeid.bad_typeid"); llvm::BasicBlock *EndBlock = CGF.createBasicBlock("typeid.end"); llvm::Value *IsNull = CGF.Builder.CreateIsNull(ThisPtr.getPointer()); CGF.Builder.CreateCondBr(IsNull, BadTypeidBlock, EndBlock); CGF.EmitBlock(BadTypeidBlock); CGF.CGM.getCXXABI().EmitBadTypeidCall(CGF); CGF.EmitBlock(EndBlock); } return CGF.CGM.getCXXABI().EmitTypeid(CGF, SrcRecordTy, ThisPtr, StdTypeInfoPtrTy); } llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { llvm::Type *StdTypeInfoPtrTy = ConvertType(E->getType())->getPointerTo(); if (E->isTypeOperand()) { llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(E->getTypeOperand(getContext())); return Builder.CreateBitCast(TypeInfo, StdTypeInfoPtrTy); } // C++ [expr.typeid]p2: // When typeid is applied to a glvalue expression whose type is a // polymorphic class type, the result refers to a std::type_info object // representing the type of the most derived object (that is, the dynamic // type) to which the glvalue refers. if (E->isPotentiallyEvaluated()) return EmitTypeidFromVTable(*this, E->getExprOperand(), StdTypeInfoPtrTy); QualType OperandTy = E->getExprOperand()->getType(); return Builder.CreateBitCast(CGM.GetAddrOfRTTIDescriptor(OperandTy), StdTypeInfoPtrTy); } static llvm::Value *EmitDynamicCastToNull(CodeGenFunction &CGF, QualType DestTy) { llvm::Type *DestLTy = CGF.ConvertType(DestTy); if (DestTy->isPointerType()) return llvm::Constant::getNullValue(DestLTy); /// C++ [expr.dynamic.cast]p9: /// A failed cast to reference type throws std::bad_cast if (!CGF.CGM.getCXXABI().EmitBadCastCall(CGF)) return nullptr; CGF.EmitBlock(CGF.createBasicBlock("dynamic_cast.end")); return llvm::UndefValue::get(DestLTy); } llvm::Value *CodeGenFunction::EmitDynamicCast(Address ThisAddr, const CXXDynamicCastExpr *DCE) { CGM.EmitExplicitCastExprType(DCE, this); QualType DestTy = DCE->getTypeAsWritten(); QualType SrcTy = DCE->getSubExpr()->getType(); // C++ [expr.dynamic.cast]p7: // If T is "pointer to cv void," then the result is a pointer to the most // derived object pointed to by v. const PointerType *DestPTy = DestTy->getAs(); bool isDynamicCastToVoid; QualType SrcRecordTy; QualType DestRecordTy; if (DestPTy) { isDynamicCastToVoid = DestPTy->getPointeeType()->isVoidType(); SrcRecordTy = SrcTy->castAs()->getPointeeType(); DestRecordTy = DestPTy->getPointeeType(); } else { isDynamicCastToVoid = false; SrcRecordTy = SrcTy; DestRecordTy = DestTy->castAs()->getPointeeType(); } // C++ [class.cdtor]p5: // If the operand of the dynamic_cast refers to the object under // construction or destruction and the static type of the operand is not a // pointer to or object of the constructor or destructor’s own class or one // of its bases, the dynamic_cast results in undefined behavior. EmitTypeCheck(TCK_DynamicOperation, DCE->getExprLoc(), ThisAddr.getPointer(), SrcRecordTy); if (DCE->isAlwaysNull()) if (llvm::Value *T = EmitDynamicCastToNull(*this, DestTy)) return T; assert(SrcRecordTy->isRecordType() && "source type must be a record type!"); // C++ [expr.dynamic.cast]p4: // If the value of v is a null pointer value in the pointer case, the result // is the null pointer value of type T. bool ShouldNullCheckSrcValue = CGM.getCXXABI().shouldDynamicCastCallBeNullChecked(SrcTy->isPointerType(), SrcRecordTy); llvm::BasicBlock *CastNull = nullptr; llvm::BasicBlock *CastNotNull = nullptr; llvm::BasicBlock *CastEnd = createBasicBlock("dynamic_cast.end"); if (ShouldNullCheckSrcValue) { CastNull = createBasicBlock("dynamic_cast.null"); CastNotNull = createBasicBlock("dynamic_cast.notnull"); llvm::Value *IsNull = Builder.CreateIsNull(ThisAddr.getPointer()); Builder.CreateCondBr(IsNull, CastNull, CastNotNull); EmitBlock(CastNotNull); } llvm::Value *Value; if (isDynamicCastToVoid) { Value = CGM.getCXXABI().EmitDynamicCastToVoid(*this, ThisAddr, SrcRecordTy, DestTy); } else { assert(DestRecordTy->isRecordType() && "destination type must be a record type!"); Value = CGM.getCXXABI().EmitDynamicCastCall(*this, ThisAddr, SrcRecordTy, DestTy, DestRecordTy, CastEnd); CastNotNull = Builder.GetInsertBlock(); } if (ShouldNullCheckSrcValue) { EmitBranch(CastEnd); EmitBlock(CastNull); EmitBranch(CastEnd); } EmitBlock(CastEnd); if (ShouldNullCheckSrcValue) { llvm::PHINode *PHI = Builder.CreatePHI(Value->getType(), 2); PHI->addIncoming(Value, CastNotNull); PHI->addIncoming(llvm::Constant::getNullValue(Value->getType()), CastNull); Value = PHI; } return Value; } Index: head/contrib/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp =================================================================== --- head/contrib/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp (revision 359825) +++ head/contrib/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp (revision 359826) @@ -1,2495 +1,2494 @@ //===--- CodeGenFunction.cpp - Emit LLVM Code from ASTs for a Function ----===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This coordinates the per-function state used while generating code. // //===----------------------------------------------------------------------===// #include "CodeGenFunction.h" #include "CGBlocks.h" #include "CGCUDARuntime.h" #include "CGCXXABI.h" #include "CGCleanup.h" #include "CGDebugInfo.h" #include "CGOpenMPRuntime.h" #include "CodeGenModule.h" #include "CodeGenPGO.h" #include "TargetInfo.h" #include "clang/AST/ASTContext.h" #include "clang/AST/ASTLambda.h" #include "clang/AST/Attr.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclCXX.h" #include "clang/AST/StmtCXX.h" #include "clang/AST/StmtObjC.h" #include "clang/Basic/Builtins.h" #include "clang/Basic/CodeGenOptions.h" #include "clang/Basic/TargetInfo.h" #include "clang/CodeGen/CGFunctionInfo.h" #include "clang/Frontend/FrontendDiagnostic.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/Dominators.h" #include "llvm/IR/FPEnv.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/MDBuilder.h" #include "llvm/IR/Operator.h" #include "llvm/Transforms/Utils/PromoteMemToReg.h" using namespace clang; using namespace CodeGen; /// shouldEmitLifetimeMarkers - Decide whether we need emit the life-time /// markers. static bool shouldEmitLifetimeMarkers(const CodeGenOptions &CGOpts, const LangOptions &LangOpts) { if (CGOpts.DisableLifetimeMarkers) return false; // Sanitizers may use markers. if (CGOpts.SanitizeAddressUseAfterScope || LangOpts.Sanitize.has(SanitizerKind::HWAddress) || LangOpts.Sanitize.has(SanitizerKind::Memory)) return true; // For now, only in optimized builds. return CGOpts.OptimizationLevel != 0; } CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext) : CodeGenTypeCache(cgm), CGM(cgm), Target(cgm.getTarget()), Builder(cgm, cgm.getModule().getContext(), llvm::ConstantFolder(), CGBuilderInserterTy(this)), SanOpts(CGM.getLangOpts().Sanitize), DebugInfo(CGM.getModuleDebugInfo()), PGO(cgm), ShouldEmitLifetimeMarkers(shouldEmitLifetimeMarkers( CGM.getCodeGenOpts(), CGM.getLangOpts())) { if (!suppressNewContext) CGM.getCXXABI().getMangleContext().startNewFunction(); llvm::FastMathFlags FMF; if (CGM.getLangOpts().FastMath) FMF.setFast(); if (CGM.getLangOpts().FiniteMathOnly) { FMF.setNoNaNs(); FMF.setNoInfs(); } if (CGM.getCodeGenOpts().NoNaNsFPMath) { FMF.setNoNaNs(); } if (CGM.getCodeGenOpts().NoSignedZeros) { FMF.setNoSignedZeros(); } if (CGM.getCodeGenOpts().ReciprocalMath) { FMF.setAllowReciprocal(); } if (CGM.getCodeGenOpts().Reassociate) { FMF.setAllowReassoc(); } Builder.setFastMathFlags(FMF); SetFPModel(); } CodeGenFunction::~CodeGenFunction() { assert(LifetimeExtendedCleanupStack.empty() && "failed to emit a cleanup"); // If there are any unclaimed block infos, go ahead and destroy them // now. This can happen if IR-gen gets clever and skips evaluating // something. if (FirstBlockInfo) destroyBlockInfos(FirstBlockInfo); if (getLangOpts().OpenMP && CurFn) CGM.getOpenMPRuntime().functionFinished(*this); } // Map the LangOption for rounding mode into // the corresponding enum in the IR. static llvm::fp::RoundingMode ToConstrainedRoundingMD( LangOptions::FPRoundingModeKind Kind) { switch (Kind) { case LangOptions::FPR_ToNearest: return llvm::fp::rmToNearest; case LangOptions::FPR_Downward: return llvm::fp::rmDownward; case LangOptions::FPR_Upward: return llvm::fp::rmUpward; case LangOptions::FPR_TowardZero: return llvm::fp::rmTowardZero; case LangOptions::FPR_Dynamic: return llvm::fp::rmDynamic; } llvm_unreachable("Unsupported FP RoundingMode"); } // Map the LangOption for exception behavior into // the corresponding enum in the IR. static llvm::fp::ExceptionBehavior ToConstrainedExceptMD( LangOptions::FPExceptionModeKind Kind) { switch (Kind) { case LangOptions::FPE_Ignore: return llvm::fp::ebIgnore; case LangOptions::FPE_MayTrap: return llvm::fp::ebMayTrap; case LangOptions::FPE_Strict: return llvm::fp::ebStrict; } llvm_unreachable("Unsupported FP Exception Behavior"); } void CodeGenFunction::SetFPModel() { auto fpRoundingMode = ToConstrainedRoundingMD( getLangOpts().getFPRoundingMode()); auto fpExceptionBehavior = ToConstrainedExceptMD( getLangOpts().getFPExceptionMode()); if (fpExceptionBehavior == llvm::fp::ebIgnore && fpRoundingMode == llvm::fp::rmToNearest) // Constrained intrinsics are not used. ; else { Builder.setIsFPConstrained(true); Builder.setDefaultConstrainedRounding(fpRoundingMode); Builder.setDefaultConstrainedExcept(fpExceptionBehavior); } } CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo) { return getNaturalTypeAlignment(T->getPointeeType(), BaseInfo, TBAAInfo, /* forPointeeType= */ true); } CharUnits CodeGenFunction::getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo, TBAAAccessInfo *TBAAInfo, bool forPointeeType) { if (TBAAInfo) *TBAAInfo = CGM.getTBAAAccessInfo(T); // Honor alignment typedef attributes even on incomplete types. // We also honor them straight for C++ class types, even as pointees; // there's an expressivity gap here. if (auto TT = T->getAs()) { if (auto Align = TT->getDecl()->getMaxAlignment()) { if (BaseInfo) *BaseInfo = LValueBaseInfo(AlignmentSource::AttributedType); return getContext().toCharUnitsFromBits(Align); } } if (BaseInfo) *BaseInfo = LValueBaseInfo(AlignmentSource::Type); CharUnits Alignment; if (T->isIncompleteType()) { Alignment = CharUnits::One(); // Shouldn't be used, but pessimistic is best. } else { // For C++ class pointees, we don't know whether we're pointing at a // base or a complete object, so we generally need to use the // non-virtual alignment. const CXXRecordDecl *RD; if (forPointeeType && (RD = T->getAsCXXRecordDecl())) { Alignment = CGM.getClassPointerAlignment(RD); } else { Alignment = getContext().getTypeAlignInChars(T); if (T.getQualifiers().hasUnaligned()) Alignment = CharUnits::One(); } // Cap to the global maximum type alignment unless the alignment // was somehow explicit on the type. if (unsigned MaxAlign = getLangOpts().MaxTypeAlign) { if (Alignment.getQuantity() > MaxAlign && !getContext().isAlignmentRequired(T)) Alignment = CharUnits::fromQuantity(MaxAlign); } } return Alignment; } LValue CodeGenFunction::MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T) { LValueBaseInfo BaseInfo; TBAAAccessInfo TBAAInfo; CharUnits Alignment = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo); return LValue::MakeAddr(Address(V, Alignment), T, getContext(), BaseInfo, TBAAInfo); } /// Given a value of type T* that may not be to a complete object, /// construct an l-value with the natural pointee alignment of T. LValue CodeGenFunction::MakeNaturalAlignPointeeAddrLValue(llvm::Value *V, QualType T) { LValueBaseInfo BaseInfo; TBAAAccessInfo TBAAInfo; CharUnits Align = getNaturalTypeAlignment(T, &BaseInfo, &TBAAInfo, /* forPointeeType= */ true); return MakeAddrLValue(Address(V, Align), T, BaseInfo, TBAAInfo); } llvm::Type *CodeGenFunction::ConvertTypeForMem(QualType T) { return CGM.getTypes().ConvertTypeForMem(T); } llvm::Type *CodeGenFunction::ConvertType(QualType T) { return CGM.getTypes().ConvertType(T); } TypeEvaluationKind CodeGenFunction::getEvaluationKind(QualType type) { type = type.getCanonicalType(); while (true) { switch (type->getTypeClass()) { #define TYPE(name, parent) #define ABSTRACT_TYPE(name, parent) #define NON_CANONICAL_TYPE(name, parent) case Type::name: #define DEPENDENT_TYPE(name, parent) case Type::name: #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name: #include "clang/AST/TypeNodes.inc" llvm_unreachable("non-canonical or dependent type in IR-generation"); case Type::Auto: case Type::DeducedTemplateSpecialization: llvm_unreachable("undeduced type in IR-generation"); // Various scalar types. case Type::Builtin: case Type::Pointer: case Type::BlockPointer: case Type::LValueReference: case Type::RValueReference: case Type::MemberPointer: case Type::Vector: case Type::ExtVector: case Type::FunctionProto: case Type::FunctionNoProto: case Type::Enum: case Type::ObjCObjectPointer: case Type::Pipe: return TEK_Scalar; // Complexes. case Type::Complex: return TEK_Complex; // Arrays, records, and Objective-C objects. case Type::ConstantArray: case Type::IncompleteArray: case Type::VariableArray: case Type::Record: case Type::ObjCObject: case Type::ObjCInterface: return TEK_Aggregate; // We operate on atomic values according to their underlying type. case Type::Atomic: type = cast(type)->getValueType(); continue; } llvm_unreachable("unknown type kind!"); } } llvm::DebugLoc CodeGenFunction::EmitReturnBlock() { // For cleanliness, we try to avoid emitting the return block for // simple cases. llvm::BasicBlock *CurBB = Builder.GetInsertBlock(); if (CurBB) { assert(!CurBB->getTerminator() && "Unexpected terminated block."); // We have a valid insert point, reuse it if it is empty or there are no // explicit jumps to the return block. if (CurBB->empty() || ReturnBlock.getBlock()->use_empty()) { ReturnBlock.getBlock()->replaceAllUsesWith(CurBB); delete ReturnBlock.getBlock(); ReturnBlock = JumpDest(); } else EmitBlock(ReturnBlock.getBlock()); return llvm::DebugLoc(); } // Otherwise, if the return block is the target of a single direct // branch then we can just put the code in that block instead. This // cleans up functions which started with a unified return block. if (ReturnBlock.getBlock()->hasOneUse()) { llvm::BranchInst *BI = dyn_cast(*ReturnBlock.getBlock()->user_begin()); if (BI && BI->isUnconditional() && BI->getSuccessor(0) == ReturnBlock.getBlock()) { // Record/return the DebugLoc of the simple 'return' expression to be used // later by the actual 'ret' instruction. llvm::DebugLoc Loc = BI->getDebugLoc(); Builder.SetInsertPoint(BI->getParent()); BI->eraseFromParent(); delete ReturnBlock.getBlock(); ReturnBlock = JumpDest(); return Loc; } } // FIXME: We are at an unreachable point, there is no reason to emit the block // unless it has uses. However, we still need a place to put the debug // region.end for now. EmitBlock(ReturnBlock.getBlock()); return llvm::DebugLoc(); } static void EmitIfUsed(CodeGenFunction &CGF, llvm::BasicBlock *BB) { if (!BB) return; if (!BB->use_empty()) return CGF.CurFn->getBasicBlockList().push_back(BB); delete BB; } void CodeGenFunction::FinishFunction(SourceLocation EndLoc) { assert(BreakContinueStack.empty() && "mismatched push/pop in break/continue stack!"); bool OnlySimpleReturnStmts = NumSimpleReturnExprs > 0 && NumSimpleReturnExprs == NumReturnExprs && ReturnBlock.getBlock()->use_empty(); // Usually the return expression is evaluated before the cleanup // code. If the function contains only a simple return statement, // such as a constant, the location before the cleanup code becomes // the last useful breakpoint in the function, because the simple // return expression will be evaluated after the cleanup code. To be // safe, set the debug location for cleanup code to the location of // the return statement. Otherwise the cleanup code should be at the // end of the function's lexical scope. // // If there are multiple branches to the return block, the branch // instructions will get the location of the return statements and // all will be fine. if (CGDebugInfo *DI = getDebugInfo()) { if (OnlySimpleReturnStmts) DI->EmitLocation(Builder, LastStopPoint); else DI->EmitLocation(Builder, EndLoc); } // Pop any cleanups that might have been associated with the // parameters. Do this in whatever block we're currently in; it's // important to do this before we enter the return block or return // edges will be *really* confused. bool HasCleanups = EHStack.stable_begin() != PrologueCleanupDepth; bool HasOnlyLifetimeMarkers = HasCleanups && EHStack.containsOnlyLifetimeMarkers(PrologueCleanupDepth); bool EmitRetDbgLoc = !HasCleanups || HasOnlyLifetimeMarkers; if (HasCleanups) { // Make sure the line table doesn't jump back into the body for // the ret after it's been at EndLoc. Optional AL; if (CGDebugInfo *DI = getDebugInfo()) { if (OnlySimpleReturnStmts) DI->EmitLocation(Builder, EndLoc); else // We may not have a valid end location. Try to apply it anyway, and // fall back to an artificial location if needed. AL = ApplyDebugLocation::CreateDefaultArtificial(*this, EndLoc); } PopCleanupBlocks(PrologueCleanupDepth); } // Emit function epilog (to return). llvm::DebugLoc Loc = EmitReturnBlock(); if (ShouldInstrumentFunction()) { if (CGM.getCodeGenOpts().InstrumentFunctions) CurFn->addFnAttr("instrument-function-exit", "__cyg_profile_func_exit"); if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining) CurFn->addFnAttr("instrument-function-exit-inlined", "__cyg_profile_func_exit"); } // Emit debug descriptor for function end. if (CGDebugInfo *DI = getDebugInfo()) DI->EmitFunctionEnd(Builder, CurFn); // Reset the debug location to that of the simple 'return' expression, if any // rather than that of the end of the function's scope '}'. ApplyDebugLocation AL(*this, Loc); EmitFunctionEpilog(*CurFnInfo, EmitRetDbgLoc, EndLoc); EmitEndEHSpec(CurCodeDecl); assert(EHStack.empty() && "did not remove all scopes from cleanup stack!"); // If someone did an indirect goto, emit the indirect goto block at the end of // the function. if (IndirectBranch) { EmitBlock(IndirectBranch->getParent()); Builder.ClearInsertionPoint(); } // If some of our locals escaped, insert a call to llvm.localescape in the // entry block. if (!EscapedLocals.empty()) { // Invert the map from local to index into a simple vector. There should be // no holes. SmallVector EscapeArgs; EscapeArgs.resize(EscapedLocals.size()); for (auto &Pair : EscapedLocals) EscapeArgs[Pair.second] = Pair.first; llvm::Function *FrameEscapeFn = llvm::Intrinsic::getDeclaration( &CGM.getModule(), llvm::Intrinsic::localescape); CGBuilderTy(*this, AllocaInsertPt).CreateCall(FrameEscapeFn, EscapeArgs); } // Remove the AllocaInsertPt instruction, which is just a convenience for us. llvm::Instruction *Ptr = AllocaInsertPt; AllocaInsertPt = nullptr; Ptr->eraseFromParent(); // If someone took the address of a label but never did an indirect goto, we // made a zero entry PHI node, which is illegal, zap it now. if (IndirectBranch) { llvm::PHINode *PN = cast(IndirectBranch->getAddress()); if (PN->getNumIncomingValues() == 0) { PN->replaceAllUsesWith(llvm::UndefValue::get(PN->getType())); PN->eraseFromParent(); } } EmitIfUsed(*this, EHResumeBlock); EmitIfUsed(*this, TerminateLandingPad); EmitIfUsed(*this, TerminateHandler); EmitIfUsed(*this, UnreachableBlock); for (const auto &FuncletAndParent : TerminateFunclets) EmitIfUsed(*this, FuncletAndParent.second); if (CGM.getCodeGenOpts().EmitDeclMetadata) EmitDeclMetadata(); for (SmallVectorImpl >::iterator I = DeferredReplacements.begin(), E = DeferredReplacements.end(); I != E; ++I) { I->first->replaceAllUsesWith(I->second); I->first->eraseFromParent(); } // Eliminate CleanupDestSlot alloca by replacing it with SSA values and // PHIs if the current function is a coroutine. We don't do it for all // functions as it may result in slight increase in numbers of instructions // if compiled with no optimizations. We do it for coroutine as the lifetime // of CleanupDestSlot alloca make correct coroutine frame building very // difficult. if (NormalCleanupDest.isValid() && isCoroutine()) { llvm::DominatorTree DT(*CurFn); llvm::PromoteMemToReg( cast(NormalCleanupDest.getPointer()), DT); NormalCleanupDest = Address::invalid(); } // Scan function arguments for vector width. for (llvm::Argument &A : CurFn->args()) if (auto *VT = dyn_cast(A.getType())) LargestVectorWidth = std::max((uint64_t)LargestVectorWidth, VT->getPrimitiveSizeInBits().getFixedSize()); // Update vector width based on return type. if (auto *VT = dyn_cast(CurFn->getReturnType())) LargestVectorWidth = std::max((uint64_t)LargestVectorWidth, VT->getPrimitiveSizeInBits().getFixedSize()); // Add the required-vector-width attribute. This contains the max width from: // 1. min-vector-width attribute used in the source program. // 2. Any builtins used that have a vector width specified. // 3. Values passed in and out of inline assembly. // 4. Width of vector arguments and return types for this function. // 5. Width of vector aguments and return types for functions called by this // function. CurFn->addFnAttr("min-legal-vector-width", llvm::utostr(LargestVectorWidth)); // If we generated an unreachable return block, delete it now. if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty()) { Builder.ClearInsertionPoint(); ReturnBlock.getBlock()->eraseFromParent(); } if (ReturnValue.isValid()) { auto *RetAlloca = dyn_cast(ReturnValue.getPointer()); if (RetAlloca && RetAlloca->use_empty()) { RetAlloca->eraseFromParent(); ReturnValue = Address::invalid(); } } } /// ShouldInstrumentFunction - Return true if the current function should be /// instrumented with __cyg_profile_func_* calls bool CodeGenFunction::ShouldInstrumentFunction() { if (!CGM.getCodeGenOpts().InstrumentFunctions && !CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining && !CGM.getCodeGenOpts().InstrumentFunctionEntryBare) return false; if (!CurFuncDecl || CurFuncDecl->hasAttr()) return false; return true; } /// ShouldXRayInstrument - Return true if the current function should be /// instrumented with XRay nop sleds. bool CodeGenFunction::ShouldXRayInstrumentFunction() const { return CGM.getCodeGenOpts().XRayInstrumentFunctions; } /// AlwaysEmitXRayCustomEvents - Return true if we should emit IR for calls to /// the __xray_customevent(...) builtin calls, when doing XRay instrumentation. bool CodeGenFunction::AlwaysEmitXRayCustomEvents() const { return CGM.getCodeGenOpts().XRayInstrumentFunctions && (CGM.getCodeGenOpts().XRayAlwaysEmitCustomEvents || CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask == XRayInstrKind::Custom); } bool CodeGenFunction::AlwaysEmitXRayTypedEvents() const { return CGM.getCodeGenOpts().XRayInstrumentFunctions && (CGM.getCodeGenOpts().XRayAlwaysEmitTypedEvents || CGM.getCodeGenOpts().XRayInstrumentationBundle.Mask == XRayInstrKind::Typed); } llvm::Constant * CodeGenFunction::EncodeAddrForUseInPrologue(llvm::Function *F, llvm::Constant *Addr) { // Addresses stored in prologue data can't require run-time fixups and must // be PC-relative. Run-time fixups are undesirable because they necessitate // writable text segments, which are unsafe. And absolute addresses are // undesirable because they break PIE mode. // Add a layer of indirection through a private global. Taking its address // won't result in a run-time fixup, even if Addr has linkonce_odr linkage. auto *GV = new llvm::GlobalVariable(CGM.getModule(), Addr->getType(), /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, Addr); // Create a PC-relative address. auto *GOTAsInt = llvm::ConstantExpr::getPtrToInt(GV, IntPtrTy); auto *FuncAsInt = llvm::ConstantExpr::getPtrToInt(F, IntPtrTy); auto *PCRelAsInt = llvm::ConstantExpr::getSub(GOTAsInt, FuncAsInt); return (IntPtrTy == Int32Ty) ? PCRelAsInt : llvm::ConstantExpr::getTrunc(PCRelAsInt, Int32Ty); } llvm::Value * CodeGenFunction::DecodeAddrUsedInPrologue(llvm::Value *F, llvm::Value *EncodedAddr) { // Reconstruct the address of the global. auto *PCRelAsInt = Builder.CreateSExt(EncodedAddr, IntPtrTy); auto *FuncAsInt = Builder.CreatePtrToInt(F, IntPtrTy, "func_addr.int"); auto *GOTAsInt = Builder.CreateAdd(PCRelAsInt, FuncAsInt, "global_addr.int"); auto *GOTAddr = Builder.CreateIntToPtr(GOTAsInt, Int8PtrPtrTy, "global_addr"); // Load the original pointer through the global. return Builder.CreateLoad(Address(GOTAddr, getPointerAlign()), "decoded_addr"); } void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD, llvm::Function *Fn) { if (!FD->hasAttr()) return; llvm::LLVMContext &Context = getLLVMContext(); CGM.GenOpenCLArgMetadata(Fn, FD, this); if (const VecTypeHintAttr *A = FD->getAttr()) { QualType HintQTy = A->getTypeHint(); const ExtVectorType *HintEltQTy = HintQTy->getAs(); bool IsSignedInteger = HintQTy->isSignedIntegerType() || (HintEltQTy && HintEltQTy->getElementType()->isSignedIntegerType()); llvm::Metadata *AttrMDArgs[] = { llvm::ConstantAsMetadata::get(llvm::UndefValue::get( CGM.getTypes().ConvertType(A->getTypeHint()))), llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( llvm::IntegerType::get(Context, 32), llvm::APInt(32, (uint64_t)(IsSignedInteger ? 1 : 0))))}; Fn->setMetadata("vec_type_hint", llvm::MDNode::get(Context, AttrMDArgs)); } if (const WorkGroupSizeHintAttr *A = FD->getAttr()) { llvm::Metadata *AttrMDArgs[] = { llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())), llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())), llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))}; Fn->setMetadata("work_group_size_hint", llvm::MDNode::get(Context, AttrMDArgs)); } if (const ReqdWorkGroupSizeAttr *A = FD->getAttr()) { llvm::Metadata *AttrMDArgs[] = { llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())), llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())), llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))}; Fn->setMetadata("reqd_work_group_size", llvm::MDNode::get(Context, AttrMDArgs)); } if (const OpenCLIntelReqdSubGroupSizeAttr *A = FD->getAttr()) { llvm::Metadata *AttrMDArgs[] = { llvm::ConstantAsMetadata::get(Builder.getInt32(A->getSubGroupSize()))}; Fn->setMetadata("intel_reqd_sub_group_size", llvm::MDNode::get(Context, AttrMDArgs)); } } /// Determine whether the function F ends with a return stmt. static bool endsWithReturn(const Decl* F) { const Stmt *Body = nullptr; if (auto *FD = dyn_cast_or_null(F)) Body = FD->getBody(); else if (auto *OMD = dyn_cast_or_null(F)) Body = OMD->getBody(); if (auto *CS = dyn_cast_or_null(Body)) { auto LastStmt = CS->body_rbegin(); if (LastStmt != CS->body_rend()) return isa(*LastStmt); } return false; } void CodeGenFunction::markAsIgnoreThreadCheckingAtRuntime(llvm::Function *Fn) { if (SanOpts.has(SanitizerKind::Thread)) { Fn->addFnAttr("sanitize_thread_no_checking_at_run_time"); Fn->removeFnAttr(llvm::Attribute::SanitizeThread); } } /// Check if the return value of this function requires sanitization. bool CodeGenFunction::requiresReturnValueCheck() const { return requiresReturnValueNullabilityCheck() || (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) && CurCodeDecl && CurCodeDecl->getAttr()); } static bool matchesStlAllocatorFn(const Decl *D, const ASTContext &Ctx) { auto *MD = dyn_cast_or_null(D); if (!MD || !MD->getDeclName().getAsIdentifierInfo() || !MD->getDeclName().getAsIdentifierInfo()->isStr("allocate") || (MD->getNumParams() != 1 && MD->getNumParams() != 2)) return false; if (MD->parameters()[0]->getType().getCanonicalType() != Ctx.getSizeType()) return false; if (MD->getNumParams() == 2) { auto *PT = MD->parameters()[1]->getType()->getAs(); if (!PT || !PT->isVoidPointerType() || !PT->getPointeeType().isConstQualified()) return false; } return true; } /// Return the UBSan prologue signature for \p FD if one is available. static llvm::Constant *getPrologueSignature(CodeGenModule &CGM, const FunctionDecl *FD) { if (const auto *MD = dyn_cast(FD)) if (!MD->isStatic()) return nullptr; return CGM.getTargetCodeGenInfo().getUBSanFunctionSignature(CGM); } void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc, SourceLocation StartLoc) { assert(!CurFn && "Do not use a CodeGenFunction object for more than one function"); const Decl *D = GD.getDecl(); DidCallStackSave = false; CurCodeDecl = D; if (const auto *FD = dyn_cast_or_null(D)) if (FD->usesSEHTry()) CurSEHParent = FD; CurFuncDecl = (D ? D->getNonClosureContext() : nullptr); FnRetTy = RetTy; CurFn = Fn; CurFnInfo = &FnInfo; assert(CurFn->isDeclaration() && "Function already has body?"); // If this function has been blacklisted for any of the enabled sanitizers, // disable the sanitizer for the function. do { #define SANITIZER(NAME, ID) \ if (SanOpts.empty()) \ break; \ if (SanOpts.has(SanitizerKind::ID)) \ if (CGM.isInSanitizerBlacklist(SanitizerKind::ID, Fn, Loc)) \ SanOpts.set(SanitizerKind::ID, false); #include "clang/Basic/Sanitizers.def" #undef SANITIZER } while (0); if (D) { // Apply the no_sanitize* attributes to SanOpts. for (auto Attr : D->specific_attrs()) { SanitizerMask mask = Attr->getMask(); SanOpts.Mask &= ~mask; if (mask & SanitizerKind::Address) SanOpts.set(SanitizerKind::KernelAddress, false); if (mask & SanitizerKind::KernelAddress) SanOpts.set(SanitizerKind::Address, false); if (mask & SanitizerKind::HWAddress) SanOpts.set(SanitizerKind::KernelHWAddress, false); if (mask & SanitizerKind::KernelHWAddress) SanOpts.set(SanitizerKind::HWAddress, false); } } // Apply sanitizer attributes to the function. if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress)) Fn->addFnAttr(llvm::Attribute::SanitizeAddress); if (SanOpts.hasOneOf(SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress)) Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress); if (SanOpts.has(SanitizerKind::MemTag)) Fn->addFnAttr(llvm::Attribute::SanitizeMemTag); if (SanOpts.has(SanitizerKind::Thread)) Fn->addFnAttr(llvm::Attribute::SanitizeThread); if (SanOpts.hasOneOf(SanitizerKind::Memory | SanitizerKind::KernelMemory)) Fn->addFnAttr(llvm::Attribute::SanitizeMemory); if (SanOpts.has(SanitizerKind::SafeStack)) Fn->addFnAttr(llvm::Attribute::SafeStack); if (SanOpts.has(SanitizerKind::ShadowCallStack)) Fn->addFnAttr(llvm::Attribute::ShadowCallStack); // Apply fuzzing attribute to the function. if (SanOpts.hasOneOf(SanitizerKind::Fuzzer | SanitizerKind::FuzzerNoLink)) Fn->addFnAttr(llvm::Attribute::OptForFuzzing); // Ignore TSan memory acesses from within ObjC/ObjC++ dealloc, initialize, // .cxx_destruct, __destroy_helper_block_ and all of their calees at run time. if (SanOpts.has(SanitizerKind::Thread)) { if (const auto *OMD = dyn_cast_or_null(D)) { IdentifierInfo *II = OMD->getSelector().getIdentifierInfoForSlot(0); if (OMD->getMethodFamily() == OMF_dealloc || OMD->getMethodFamily() == OMF_initialize || (OMD->getSelector().isUnarySelector() && II->isStr(".cxx_destruct"))) { markAsIgnoreThreadCheckingAtRuntime(Fn); } } } // Ignore unrelated casts in STL allocate() since the allocator must cast // from void* to T* before object initialization completes. Don't match on the // namespace because not all allocators are in std:: if (D && SanOpts.has(SanitizerKind::CFIUnrelatedCast)) { if (matchesStlAllocatorFn(D, getContext())) SanOpts.Mask &= ~SanitizerKind::CFIUnrelatedCast; } // Ignore null checks in coroutine functions since the coroutines passes // are not aware of how to move the extra UBSan instructions across the split // coroutine boundaries. if (D && SanOpts.has(SanitizerKind::Null)) if (const auto *FD = dyn_cast(D)) if (FD->getBody() && FD->getBody()->getStmtClass() == Stmt::CoroutineBodyStmtClass) SanOpts.Mask &= ~SanitizerKind::Null; if (D) { // Apply xray attributes to the function (as a string, for now) if (const auto *XRayAttr = D->getAttr()) { if (CGM.getCodeGenOpts().XRayInstrumentationBundle.has( XRayInstrKind::Function)) { if (XRayAttr->alwaysXRayInstrument() && ShouldXRayInstrumentFunction()) Fn->addFnAttr("function-instrument", "xray-always"); if (XRayAttr->neverXRayInstrument()) Fn->addFnAttr("function-instrument", "xray-never"); if (const auto *LogArgs = D->getAttr()) if (ShouldXRayInstrumentFunction()) Fn->addFnAttr("xray-log-args", llvm::utostr(LogArgs->getArgumentCount())); } } else { if (ShouldXRayInstrumentFunction() && !CGM.imbueXRayAttrs(Fn, Loc)) Fn->addFnAttr( "xray-instruction-threshold", llvm::itostr(CGM.getCodeGenOpts().XRayInstructionThreshold)); } unsigned Count, Offset; if (const auto *Attr = D->getAttr()) { Count = Attr->getCount(); Offset = Attr->getOffset(); } else { Count = CGM.getCodeGenOpts().PatchableFunctionEntryCount; Offset = CGM.getCodeGenOpts().PatchableFunctionEntryOffset; } if (Count && Offset <= Count) { Fn->addFnAttr("patchable-function-entry", std::to_string(Count - Offset)); if (Offset) Fn->addFnAttr("patchable-function-prefix", std::to_string(Offset)); } } // Add no-jump-tables value. Fn->addFnAttr("no-jump-tables", llvm::toStringRef(CGM.getCodeGenOpts().NoUseJumpTables)); // Add no-inline-line-tables value. if (CGM.getCodeGenOpts().NoInlineLineTables) Fn->addFnAttr("no-inline-line-tables"); // Add profile-sample-accurate value. if (CGM.getCodeGenOpts().ProfileSampleAccurate) Fn->addFnAttr("profile-sample-accurate"); if (D && D->hasAttr()) Fn->addFnAttr("cfi-canonical-jump-table"); if (getLangOpts().OpenCL) { // Add metadata for a kernel function. if (const FunctionDecl *FD = dyn_cast_or_null(D)) EmitOpenCLKernelMetadata(FD, Fn); } // If we are checking function types, emit a function type signature as // prologue data. if (getLangOpts().CPlusPlus && SanOpts.has(SanitizerKind::Function)) { if (const FunctionDecl *FD = dyn_cast_or_null(D)) { if (llvm::Constant *PrologueSig = getPrologueSignature(CGM, FD)) { // Remove any (C++17) exception specifications, to allow calling e.g. a // noexcept function through a non-noexcept pointer. auto ProtoTy = getContext().getFunctionTypeWithExceptionSpec(FD->getType(), EST_None); llvm::Constant *FTRTTIConst = CGM.GetAddrOfRTTIDescriptor(ProtoTy, /*ForEH=*/true); llvm::Constant *FTRTTIConstEncoded = EncodeAddrForUseInPrologue(Fn, FTRTTIConst); llvm::Constant *PrologueStructElems[] = {PrologueSig, FTRTTIConstEncoded}; llvm::Constant *PrologueStructConst = llvm::ConstantStruct::getAnon(PrologueStructElems, /*Packed=*/true); Fn->setPrologueData(PrologueStructConst); } } } // If we're checking nullability, we need to know whether we can check the // return value. Initialize the flag to 'true' and refine it in EmitParmDecl. if (SanOpts.has(SanitizerKind::NullabilityReturn)) { auto Nullability = FnRetTy->getNullability(getContext()); if (Nullability && *Nullability == NullabilityKind::NonNull) { if (!(SanOpts.has(SanitizerKind::ReturnsNonnullAttribute) && CurCodeDecl && CurCodeDecl->getAttr())) RetValNullabilityPrecondition = llvm::ConstantInt::getTrue(getLLVMContext()); } } // If we're in C++ mode and the function name is "main", it is guaranteed // to be norecurse by the standard (3.6.1.3 "The function main shall not be // used within a program"). if (getLangOpts().CPlusPlus) if (const FunctionDecl *FD = dyn_cast_or_null(D)) if (FD->isMain()) Fn->addFnAttr(llvm::Attribute::NoRecurse); if (const FunctionDecl *FD = dyn_cast_or_null(D)) if (FD->usesFPIntrin()) Fn->addFnAttr(llvm::Attribute::StrictFP); // If a custom alignment is used, force realigning to this alignment on // any main function which certainly will need it. if (const FunctionDecl *FD = dyn_cast_or_null(D)) if ((FD->isMain() || FD->isMSVCRTEntryPoint()) && CGM.getCodeGenOpts().StackAlignment) Fn->addFnAttr("stackrealign"); llvm::BasicBlock *EntryBB = createBasicBlock("entry", CurFn); // Create a marker to make it easy to insert allocas into the entryblock // later. Don't create this with the builder, because we don't want it // folded. llvm::Value *Undef = llvm::UndefValue::get(Int32Ty); AllocaInsertPt = new llvm::BitCastInst(Undef, Int32Ty, "allocapt", EntryBB); ReturnBlock = getJumpDestInCurrentScope("return"); Builder.SetInsertPoint(EntryBB); // If we're checking the return value, allocate space for a pointer to a // precise source location of the checked return statement. if (requiresReturnValueCheck()) { ReturnLocation = CreateDefaultAlignTempAlloca(Int8PtrTy, "return.sloc.ptr"); InitTempAlloca(ReturnLocation, llvm::ConstantPointerNull::get(Int8PtrTy)); } // Emit subprogram debug descriptor. if (CGDebugInfo *DI = getDebugInfo()) { // Reconstruct the type from the argument list so that implicit parameters, // such as 'this' and 'vtt', show up in the debug info. Preserve the calling // convention. CallingConv CC = CallingConv::CC_C; if (auto *FD = dyn_cast_or_null(D)) if (const auto *SrcFnTy = FD->getType()->getAs()) CC = SrcFnTy->getCallConv(); SmallVector ArgTypes; for (const VarDecl *VD : Args) ArgTypes.push_back(VD->getType()); QualType FnType = getContext().getFunctionType( RetTy, ArgTypes, FunctionProtoType::ExtProtoInfo(CC)); DI->EmitFunctionStart(GD, Loc, StartLoc, FnType, CurFn, CurFuncIsThunk, Builder); } if (ShouldInstrumentFunction()) { if (CGM.getCodeGenOpts().InstrumentFunctions) CurFn->addFnAttr("instrument-function-entry", "__cyg_profile_func_enter"); if (CGM.getCodeGenOpts().InstrumentFunctionsAfterInlining) CurFn->addFnAttr("instrument-function-entry-inlined", "__cyg_profile_func_enter"); if (CGM.getCodeGenOpts().InstrumentFunctionEntryBare) CurFn->addFnAttr("instrument-function-entry-inlined", "__cyg_profile_func_enter_bare"); } // Since emitting the mcount call here impacts optimizations such as function // inlining, we just add an attribute to insert a mcount call in backend. // The attribute "counting-function" is set to mcount function name which is // architecture dependent. if (CGM.getCodeGenOpts().InstrumentForProfiling) { // Calls to fentry/mcount should not be generated if function has // the no_instrument_function attribute. if (!CurFuncDecl || !CurFuncDecl->hasAttr()) { if (CGM.getCodeGenOpts().CallFEntry) Fn->addFnAttr("fentry-call", "true"); else { Fn->addFnAttr("instrument-function-entry-inlined", getTarget().getMCountName()); } if (CGM.getCodeGenOpts().MNopMCount) { if (!CGM.getCodeGenOpts().CallFEntry) CGM.getDiags().Report(diag::err_opt_not_valid_without_opt) << "-mnop-mcount" << "-mfentry"; Fn->addFnAttr("mnop-mcount"); } if (CGM.getCodeGenOpts().RecordMCount) { if (!CGM.getCodeGenOpts().CallFEntry) CGM.getDiags().Report(diag::err_opt_not_valid_without_opt) << "-mrecord-mcount" << "-mfentry"; Fn->addFnAttr("mrecord-mcount"); } } } if (CGM.getCodeGenOpts().PackedStack) { if (getContext().getTargetInfo().getTriple().getArch() != llvm::Triple::systemz) CGM.getDiags().Report(diag::err_opt_not_valid_on_target) << "-mpacked-stack"; Fn->addFnAttr("packed-stack"); } if (RetTy->isVoidType()) { // Void type; nothing to return. ReturnValue = Address::invalid(); // Count the implicit return. if (!endsWithReturn(D)) ++NumReturnExprs; } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect) { // Indirect return; emit returned value directly into sret slot. // This reduces code size, and affects correctness in C++. auto AI = CurFn->arg_begin(); if (CurFnInfo->getReturnInfo().isSRetAfterThis()) ++AI; ReturnValue = Address(&*AI, CurFnInfo->getReturnInfo().getIndirectAlign()); if (!CurFnInfo->getReturnInfo().getIndirectByVal()) { ReturnValuePointer = CreateDefaultAlignTempAlloca(Int8PtrTy, "result.ptr"); Builder.CreateStore(Builder.CreatePointerBitCastOrAddrSpaceCast( ReturnValue.getPointer(), Int8PtrTy), ReturnValuePointer); } } else if (CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::InAlloca && !hasScalarEvaluationKind(CurFnInfo->getReturnType())) { // Load the sret pointer from the argument struct and return into that. unsigned Idx = CurFnInfo->getReturnInfo().getInAllocaFieldIndex(); llvm::Function::arg_iterator EI = CurFn->arg_end(); --EI; llvm::Value *Addr = Builder.CreateStructGEP(nullptr, &*EI, Idx); ReturnValuePointer = Address(Addr, getPointerAlign()); Addr = Builder.CreateAlignedLoad(Addr, getPointerAlign(), "agg.result"); ReturnValue = Address(Addr, getNaturalTypeAlignment(RetTy)); } else { ReturnValue = CreateIRTemp(RetTy, "retval"); // Tell the epilog emitter to autorelease the result. We do this // now so that various specialized functions can suppress it // during their IR-generation. if (getLangOpts().ObjCAutoRefCount && !CurFnInfo->isReturnsRetained() && RetTy->isObjCRetainableType()) AutoreleaseResult = true; } EmitStartEHSpec(CurCodeDecl); PrologueCleanupDepth = EHStack.stable_begin(); // Emit OpenMP specific initialization of the device functions. if (getLangOpts().OpenMP && CurCodeDecl) CGM.getOpenMPRuntime().emitFunctionProlog(*this, CurCodeDecl); EmitFunctionProlog(*CurFnInfo, CurFn, Args); if (D && isa(D) && cast(D)->isInstance()) { CGM.getCXXABI().EmitInstanceFunctionProlog(*this); const CXXMethodDecl *MD = cast(D); if (MD->getParent()->isLambda() && MD->getOverloadedOperator() == OO_Call) { // We're in a lambda; figure out the captures. MD->getParent()->getCaptureFields(LambdaCaptureFields, LambdaThisCaptureField); if (LambdaThisCaptureField) { // If the lambda captures the object referred to by '*this' - either by // value or by reference, make sure CXXThisValue points to the correct // object. // Get the lvalue for the field (which is a copy of the enclosing object // or contains the address of the enclosing object). LValue ThisFieldLValue = EmitLValueForLambdaField(LambdaThisCaptureField); if (!LambdaThisCaptureField->getType()->isPointerType()) { // If the enclosing object was captured by value, just use its address. CXXThisValue = ThisFieldLValue.getAddress(*this).getPointer(); } else { // Load the lvalue pointed to by the field, since '*this' was captured // by reference. CXXThisValue = EmitLoadOfLValue(ThisFieldLValue, SourceLocation()).getScalarVal(); } } for (auto *FD : MD->getParent()->fields()) { if (FD->hasCapturedVLAType()) { auto *ExprArg = EmitLoadOfLValue(EmitLValueForLambdaField(FD), SourceLocation()).getScalarVal(); auto VAT = FD->getCapturedVLAType(); VLASizeMap[VAT->getSizeExpr()] = ExprArg; } } } else { // Not in a lambda; just use 'this' from the method. // FIXME: Should we generate a new load for each use of 'this'? The // fast register allocator would be happier... CXXThisValue = CXXABIThisValue; } // Check the 'this' pointer once per function, if it's available. if (CXXABIThisValue) { SanitizerSet SkippedChecks; SkippedChecks.set(SanitizerKind::ObjectSize, true); QualType ThisTy = MD->getThisType(); // If this is the call operator of a lambda with no capture-default, it // may have a static invoker function, which may call this operator with // a null 'this' pointer. if (isLambdaCallOperator(MD) && MD->getParent()->getLambdaCaptureDefault() == LCD_None) SkippedChecks.set(SanitizerKind::Null, true); EmitTypeCheck(isa(MD) ? TCK_ConstructorCall : TCK_MemberCall, Loc, CXXABIThisValue, ThisTy, getContext().getTypeAlignInChars(ThisTy->getPointeeType()), SkippedChecks); } } // If any of the arguments have a variably modified type, make sure to // emit the type size. for (FunctionArgList::const_iterator i = Args.begin(), e = Args.end(); i != e; ++i) { const VarDecl *VD = *i; // Dig out the type as written from ParmVarDecls; it's unclear whether // the standard (C99 6.9.1p10) requires this, but we're following the // precedent set by gcc. QualType Ty; if (const ParmVarDecl *PVD = dyn_cast(VD)) Ty = PVD->getOriginalType(); else Ty = VD->getType(); if (Ty->isVariablyModifiedType()) EmitVariablyModifiedType(Ty); } // Emit a location at the end of the prologue. if (CGDebugInfo *DI = getDebugInfo()) DI->EmitLocation(Builder, StartLoc); // TODO: Do we need to handle this in two places like we do with // target-features/target-cpu? if (CurFuncDecl) if (const auto *VecWidth = CurFuncDecl->getAttr()) LargestVectorWidth = VecWidth->getVectorWidth(); } void CodeGenFunction::EmitFunctionBody(const Stmt *Body) { incrementProfileCounter(Body); if (const CompoundStmt *S = dyn_cast(Body)) EmitCompoundStmtWithoutScope(*S); else EmitStmt(Body); } /// When instrumenting to collect profile data, the counts for some blocks /// such as switch cases need to not include the fall-through counts, so /// emit a branch around the instrumentation code. When not instrumenting, /// this just calls EmitBlock(). void CodeGenFunction::EmitBlockWithFallThrough(llvm::BasicBlock *BB, const Stmt *S) { llvm::BasicBlock *SkipCountBB = nullptr; if (HaveInsertPoint() && CGM.getCodeGenOpts().hasProfileClangInstr()) { // When instrumenting for profiling, the fallthrough to certain // statements needs to skip over the instrumentation code so that we // get an accurate count. SkipCountBB = createBasicBlock("skipcount"); EmitBranch(SkipCountBB); } EmitBlock(BB); uint64_t CurrentCount = getCurrentProfileCount(); incrementProfileCounter(S); setCurrentProfileCount(getCurrentProfileCount() + CurrentCount); if (SkipCountBB) EmitBlock(SkipCountBB); } /// Tries to mark the given function nounwind based on the /// non-existence of any throwing calls within it. We believe this is /// lightweight enough to do at -O0. static void TryMarkNoThrow(llvm::Function *F) { // LLVM treats 'nounwind' on a function as part of the type, so we // can't do this on functions that can be overwritten. if (F->isInterposable()) return; for (llvm::BasicBlock &BB : *F) for (llvm::Instruction &I : BB) if (I.mayThrow()) return; F->setDoesNotThrow(); } QualType CodeGenFunction::BuildFunctionArgList(GlobalDecl GD, FunctionArgList &Args) { const FunctionDecl *FD = cast(GD.getDecl()); QualType ResTy = FD->getReturnType(); const CXXMethodDecl *MD = dyn_cast(FD); if (MD && MD->isInstance()) { if (CGM.getCXXABI().HasThisReturn(GD)) ResTy = MD->getThisType(); else if (CGM.getCXXABI().hasMostDerivedReturn(GD)) ResTy = CGM.getContext().VoidPtrTy; CGM.getCXXABI().buildThisParam(*this, Args); } // The base version of an inheriting constructor whose constructed base is a // virtual base is not passed any arguments (because it doesn't actually call // the inherited constructor). bool PassedParams = true; if (const CXXConstructorDecl *CD = dyn_cast(FD)) if (auto Inherited = CD->getInheritedConstructor()) PassedParams = getTypes().inheritingCtorHasParams(Inherited, GD.getCtorType()); if (PassedParams) { for (auto *Param : FD->parameters()) { Args.push_back(Param); if (!Param->hasAttr()) continue; auto *Implicit = ImplicitParamDecl::Create( getContext(), Param->getDeclContext(), Param->getLocation(), /*Id=*/nullptr, getContext().getSizeType(), ImplicitParamDecl::Other); SizeArguments[Param] = Implicit; Args.push_back(Implicit); } } if (MD && (isa(MD) || isa(MD))) CGM.getCXXABI().addImplicitStructorParams(*this, ResTy, Args); return ResTy; } static bool shouldUseUndefinedBehaviorReturnOptimization(const FunctionDecl *FD, const ASTContext &Context) { QualType T = FD->getReturnType(); // Avoid the optimization for functions that return a record type with a // trivial destructor or another trivially copyable type. if (const RecordType *RT = T.getCanonicalType()->getAs()) { if (const auto *ClassDecl = dyn_cast(RT->getDecl())) return !ClassDecl->hasTrivialDestructor(); } return !T.isTriviallyCopyableType(Context); } void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, const CGFunctionInfo &FnInfo) { const FunctionDecl *FD = cast(GD.getDecl()); CurGD = GD; FunctionArgList Args; QualType ResTy = BuildFunctionArgList(GD, Args); // Check if we should generate debug info for this function. if (FD->hasAttr()) DebugInfo = nullptr; // disable debug info indefinitely for this function // The function might not have a body if we're generating thunks for a // function declaration. SourceRange BodyRange; if (Stmt *Body = FD->getBody()) BodyRange = Body->getSourceRange(); else BodyRange = FD->getLocation(); CurEHLocation = BodyRange.getEnd(); // Use the location of the start of the function to determine where // the function definition is located. By default use the location // of the declaration as the location for the subprogram. A function // may lack a declaration in the source code if it is created by code // gen. (examples: _GLOBAL__I_a, __cxx_global_array_dtor, thunk). SourceLocation Loc = FD->getLocation(); // If this is a function specialization then use the pattern body // as the location for the function. if (const FunctionDecl *SpecDecl = FD->getTemplateInstantiationPattern()) if (SpecDecl->hasBody(SpecDecl)) Loc = SpecDecl->getLocation(); Stmt *Body = FD->getBody(); // Initialize helper which will detect jumps which can cause invalid lifetime // markers. if (Body && ShouldEmitLifetimeMarkers) Bypasses.Init(Body); // Emit the standard function prologue. StartFunction(GD, ResTy, Fn, FnInfo, Args, Loc, BodyRange.getBegin()); // Generate the body of the function. PGO.assignRegionCounters(GD, CurFn); if (isa(FD)) EmitDestructorBody(Args); else if (isa(FD)) EmitConstructorBody(Args); else if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice && FD->hasAttr()) CGM.getCUDARuntime().emitDeviceStub(*this, Args); else if (isa(FD) && cast(FD)->isLambdaStaticInvoker()) { // The lambda static invoker function is special, because it forwards or // clones the body of the function call operator (but is actually static). EmitLambdaStaticInvokeBody(cast(FD)); } else if (FD->isDefaulted() && isa(FD) && (cast(FD)->isCopyAssignmentOperator() || cast(FD)->isMoveAssignmentOperator())) { // Implicit copy-assignment gets the same special treatment as implicit // copy-constructors. emitImplicitAssignmentOperatorBody(Args); } else if (Body) { EmitFunctionBody(Body); } else llvm_unreachable("no definition for emitted function"); // C++11 [stmt.return]p2: // Flowing off the end of a function [...] results in undefined behavior in // a value-returning function. // C11 6.9.1p12: // If the '}' that terminates a function is reached, and the value of the // function call is used by the caller, the behavior is undefined. if (getLangOpts().CPlusPlus && !FD->hasImplicitReturnZero() && !SawAsmBlock && !FD->getReturnType()->isVoidType() && Builder.GetInsertBlock()) { bool ShouldEmitUnreachable = CGM.getCodeGenOpts().StrictReturn || shouldUseUndefinedBehaviorReturnOptimization(FD, getContext()); if (SanOpts.has(SanitizerKind::Return)) { SanitizerScope SanScope(this); llvm::Value *IsFalse = Builder.getFalse(); EmitCheck(std::make_pair(IsFalse, SanitizerKind::Return), SanitizerHandler::MissingReturn, EmitCheckSourceLocation(FD->getLocation()), None); } else if (ShouldEmitUnreachable) { if (CGM.getCodeGenOpts().OptimizationLevel == 0) EmitTrapCall(llvm::Intrinsic::trap); } if (SanOpts.has(SanitizerKind::Return) || ShouldEmitUnreachable) { Builder.CreateUnreachable(); Builder.ClearInsertionPoint(); } } // Emit the standard function epilogue. FinishFunction(BodyRange.getEnd()); // If we haven't marked the function nothrow through other means, do // a quick pass now to see if we can. if (!CurFn->doesNotThrow()) TryMarkNoThrow(CurFn); } /// ContainsLabel - Return true if the statement contains a label in it. If /// this statement is not executed normally, it not containing a label means /// that we can just remove the code. bool CodeGenFunction::ContainsLabel(const Stmt *S, bool IgnoreCaseStmts) { // Null statement, not a label! if (!S) return false; // If this is a label, we have to emit the code, consider something like: // if (0) { ... foo: bar(); } goto foo; // // TODO: If anyone cared, we could track __label__'s, since we know that you // can't jump to one from outside their declared region. if (isa(S)) return true; // If this is a case/default statement, and we haven't seen a switch, we have // to emit the code. if (isa(S) && !IgnoreCaseStmts) return true; // If this is a switch statement, we want to ignore cases below it. if (isa(S)) IgnoreCaseStmts = true; // Scan subexpressions for verboten labels. for (const Stmt *SubStmt : S->children()) if (ContainsLabel(SubStmt, IgnoreCaseStmts)) return true; return false; } /// containsBreak - Return true if the statement contains a break out of it. /// If the statement (recursively) contains a switch or loop with a break /// inside of it, this is fine. bool CodeGenFunction::containsBreak(const Stmt *S) { // Null statement, not a label! if (!S) return false; // If this is a switch or loop that defines its own break scope, then we can // include it and anything inside of it. if (isa(S) || isa(S) || isa(S) || isa(S)) return false; if (isa(S)) return true; // Scan subexpressions for verboten breaks. for (const Stmt *SubStmt : S->children()) if (containsBreak(SubStmt)) return true; return false; } bool CodeGenFunction::mightAddDeclToScope(const Stmt *S) { if (!S) return false; // Some statement kinds add a scope and thus never add a decl to the current // scope. Note, this list is longer than the list of statements that might // have an unscoped decl nested within them, but this way is conservatively // correct even if more statement kinds are added. if (isa(S) || isa(S) || isa(S) || isa(S) || isa(S) || isa(S) || isa(S) || isa(S) || isa(S) || isa(S)) return false; if (isa(S)) return true; for (const Stmt *SubStmt : S->children()) if (mightAddDeclToScope(SubStmt)) return true; return false; } /// ConstantFoldsToSimpleInteger - If the specified expression does not fold /// to a constant, or if it does but contains a label, return false. If it /// constant folds return true and set the boolean result in Result. bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, bool &ResultBool, bool AllowLabels) { llvm::APSInt ResultInt; if (!ConstantFoldsToSimpleInteger(Cond, ResultInt, AllowLabels)) return false; ResultBool = ResultInt.getBoolValue(); return true; } /// ConstantFoldsToSimpleInteger - If the specified expression does not fold /// to a constant, or if it does but contains a label, return false. If it /// constant folds return true and set the folded value. bool CodeGenFunction::ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &ResultInt, bool AllowLabels) { // FIXME: Rename and handle conversion of other evaluatable things // to bool. Expr::EvalResult Result; if (!Cond->EvaluateAsInt(Result, getContext())) return false; // Not foldable, not integer or not fully evaluatable. llvm::APSInt Int = Result.Val.getInt(); if (!AllowLabels && CodeGenFunction::ContainsLabel(Cond)) return false; // Contains a label. ResultInt = Int; return true; } /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an if /// statement) to the specified blocks. Based on the condition, this might try /// to simplify the codegen of the conditional based on the branch. /// void CodeGenFunction::EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock, llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { Cond = Cond->IgnoreParens(); if (const BinaryOperator *CondBOp = dyn_cast(Cond)) { // Handle X && Y in a condition. if (CondBOp->getOpcode() == BO_LAnd) { // If we have "1 && X", simplify the code. "0 && X" would have constant // folded if the case was simple enough. bool ConstantBool = false; if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && ConstantBool) { // br(1 && X) -> br(X). incrementProfileCounter(CondBOp); return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount); } // If we have "X && 1", simplify the code to use an uncond branch. // "X && 0" would have been constant folded to 0. if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && ConstantBool) { // br(X && 1) -> br(X). return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock, TrueCount); } // Emit the LHS as a conditional. If the LHS conditional is false, we // want to jump to the FalseBlock. llvm::BasicBlock *LHSTrue = createBasicBlock("land.lhs.true"); // The counter tells us how often we evaluate RHS, and all of TrueCount // can be propagated to that branch. uint64_t RHSCount = getProfileCount(CondBOp->getRHS()); ConditionalEvaluation eval(*this); { ApplyDebugLocation DL(*this, Cond); EmitBranchOnBoolExpr(CondBOp->getLHS(), LHSTrue, FalseBlock, RHSCount); EmitBlock(LHSTrue); } incrementProfileCounter(CondBOp); setCurrentProfileCount(getProfileCount(CondBOp->getRHS())); // Any temporaries created here are conditional. eval.begin(*this); EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount); eval.end(*this); return; } if (CondBOp->getOpcode() == BO_LOr) { // If we have "0 || X", simplify the code. "1 || X" would have constant // folded if the case was simple enough. bool ConstantBool = false; if (ConstantFoldsToSimpleInteger(CondBOp->getLHS(), ConstantBool) && !ConstantBool) { // br(0 || X) -> br(X). incrementProfileCounter(CondBOp); return EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, TrueCount); } // If we have "X || 0", simplify the code to use an uncond branch. // "X || 1" would have been constant folded to 1. if (ConstantFoldsToSimpleInteger(CondBOp->getRHS(), ConstantBool) && !ConstantBool) { // br(X || 0) -> br(X). return EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, FalseBlock, TrueCount); } // Emit the LHS as a conditional. If the LHS conditional is true, we // want to jump to the TrueBlock. llvm::BasicBlock *LHSFalse = createBasicBlock("lor.lhs.false"); // We have the count for entry to the RHS and for the whole expression // being true, so we can divy up True count between the short circuit and // the RHS. uint64_t LHSCount = getCurrentProfileCount() - getProfileCount(CondBOp->getRHS()); uint64_t RHSCount = TrueCount - LHSCount; ConditionalEvaluation eval(*this); { ApplyDebugLocation DL(*this, Cond); EmitBranchOnBoolExpr(CondBOp->getLHS(), TrueBlock, LHSFalse, LHSCount); EmitBlock(LHSFalse); } incrementProfileCounter(CondBOp); setCurrentProfileCount(getProfileCount(CondBOp->getRHS())); // Any temporaries created here are conditional. eval.begin(*this); EmitBranchOnBoolExpr(CondBOp->getRHS(), TrueBlock, FalseBlock, RHSCount); eval.end(*this); return; } } if (const UnaryOperator *CondUOp = dyn_cast(Cond)) { // br(!x, t, f) -> br(x, f, t) if (CondUOp->getOpcode() == UO_LNot) { // Negate the count. uint64_t FalseCount = getCurrentProfileCount() - TrueCount; // Negate the condition and swap the destination blocks. return EmitBranchOnBoolExpr(CondUOp->getSubExpr(), FalseBlock, TrueBlock, FalseCount); } } if (const ConditionalOperator *CondOp = dyn_cast(Cond)) { // br(c ? x : y, t, f) -> br(c, br(x, t, f), br(y, t, f)) llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true"); llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false"); ConditionalEvaluation cond(*this); EmitBranchOnBoolExpr(CondOp->getCond(), LHSBlock, RHSBlock, getProfileCount(CondOp)); // When computing PGO branch weights, we only know the overall count for // the true block. This code is essentially doing tail duplication of the // naive code-gen, introducing new edges for which counts are not // available. Divide the counts proportionally between the LHS and RHS of // the conditional operator. uint64_t LHSScaledTrueCount = 0; if (TrueCount) { double LHSRatio = getProfileCount(CondOp) / (double)getCurrentProfileCount(); LHSScaledTrueCount = TrueCount * LHSRatio; } cond.begin(*this); EmitBlock(LHSBlock); incrementProfileCounter(CondOp); { ApplyDebugLocation DL(*this, Cond); EmitBranchOnBoolExpr(CondOp->getLHS(), TrueBlock, FalseBlock, LHSScaledTrueCount); } cond.end(*this); cond.begin(*this); EmitBlock(RHSBlock); EmitBranchOnBoolExpr(CondOp->getRHS(), TrueBlock, FalseBlock, TrueCount - LHSScaledTrueCount); cond.end(*this); return; } if (const CXXThrowExpr *Throw = dyn_cast(Cond)) { // Conditional operator handling can give us a throw expression as a // condition for a case like: // br(c ? throw x : y, t, f) -> br(c, br(throw x, t, f), br(y, t, f) // Fold this to: // br(c, throw x, br(y, t, f)) EmitCXXThrowExpr(Throw, /*KeepInsertionPoint*/false); return; } // If the branch has a condition wrapped by __builtin_unpredictable, // create metadata that specifies that the branch is unpredictable. // Don't bother if not optimizing because that metadata would not be used. llvm::MDNode *Unpredictable = nullptr; auto *Call = dyn_cast(Cond->IgnoreImpCasts()); if (Call && CGM.getCodeGenOpts().OptimizationLevel != 0) { auto *FD = dyn_cast_or_null(Call->getCalleeDecl()); if (FD && FD->getBuiltinID() == Builtin::BI__builtin_unpredictable) { llvm::MDBuilder MDHelper(getLLVMContext()); Unpredictable = MDHelper.createUnpredictable(); } } // Create branch weights based on the number of times we get here and the // number of times the condition should be true. uint64_t CurrentCount = std::max(getCurrentProfileCount(), TrueCount); llvm::MDNode *Weights = createProfileWeights(TrueCount, CurrentCount - TrueCount); // Emit the code with the fully general case. llvm::Value *CondV; { ApplyDebugLocation DL(*this, Cond); CondV = EvaluateExprAsBool(Cond); } Builder.CreateCondBr(CondV, TrueBlock, FalseBlock, Weights, Unpredictable); } /// ErrorUnsupported - Print out an error that codegen doesn't support the /// specified stmt yet. void CodeGenFunction::ErrorUnsupported(const Stmt *S, const char *Type) { CGM.ErrorUnsupported(S, Type); } /// emitNonZeroVLAInit - Emit the "zero" initialization of a /// variable-length array whose elements have a non-zero bit-pattern. /// /// \param baseType the inner-most element type of the array /// \param src - a char* pointing to the bit-pattern for a single /// base element of the array /// \param sizeInChars - the total size of the VLA, in chars static void emitNonZeroVLAInit(CodeGenFunction &CGF, QualType baseType, Address dest, Address src, llvm::Value *sizeInChars) { CGBuilderTy &Builder = CGF.Builder; CharUnits baseSize = CGF.getContext().getTypeSizeInChars(baseType); llvm::Value *baseSizeInChars = llvm::ConstantInt::get(CGF.IntPtrTy, baseSize.getQuantity()); Address begin = Builder.CreateElementBitCast(dest, CGF.Int8Ty, "vla.begin"); llvm::Value *end = Builder.CreateInBoundsGEP(begin.getPointer(), sizeInChars, "vla.end"); llvm::BasicBlock *originBB = CGF.Builder.GetInsertBlock(); llvm::BasicBlock *loopBB = CGF.createBasicBlock("vla-init.loop"); llvm::BasicBlock *contBB = CGF.createBasicBlock("vla-init.cont"); // Make a loop over the VLA. C99 guarantees that the VLA element // count must be nonzero. CGF.EmitBlock(loopBB); llvm::PHINode *cur = Builder.CreatePHI(begin.getType(), 2, "vla.cur"); cur->addIncoming(begin.getPointer(), originBB); CharUnits curAlign = dest.getAlignment().alignmentOfArrayElement(baseSize); // memcpy the individual element bit-pattern. Builder.CreateMemCpy(Address(cur, curAlign), src, baseSizeInChars, /*volatile*/ false); // Go to the next element. llvm::Value *next = Builder.CreateInBoundsGEP(CGF.Int8Ty, cur, baseSizeInChars, "vla.next"); // Leave if that's the end of the VLA. llvm::Value *done = Builder.CreateICmpEQ(next, end, "vla-init.isdone"); Builder.CreateCondBr(done, contBB, loopBB); cur->addIncoming(next, loopBB); CGF.EmitBlock(contBB); } void CodeGenFunction::EmitNullInitialization(Address DestPtr, QualType Ty) { // Ignore empty classes in C++. if (getLangOpts().CPlusPlus) { if (const RecordType *RT = Ty->getAs()) { if (cast(RT->getDecl())->isEmpty()) return; } } // Cast the dest ptr to the appropriate i8 pointer type. if (DestPtr.getElementType() != Int8Ty) DestPtr = Builder.CreateElementBitCast(DestPtr, Int8Ty); // Get size and alignment info for this aggregate. CharUnits size = getContext().getTypeSizeInChars(Ty); llvm::Value *SizeVal; const VariableArrayType *vla; // Don't bother emitting a zero-byte memset. if (size.isZero()) { // But note that getTypeInfo returns 0 for a VLA. if (const VariableArrayType *vlaType = dyn_cast_or_null( getContext().getAsArrayType(Ty))) { auto VlaSize = getVLASize(vlaType); SizeVal = VlaSize.NumElts; CharUnits eltSize = getContext().getTypeSizeInChars(VlaSize.Type); if (!eltSize.isOne()) SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(eltSize)); vla = vlaType; } else { return; } } else { SizeVal = CGM.getSize(size); vla = nullptr; } // If the type contains a pointer to data member we can't memset it to zero. // Instead, create a null constant and copy it to the destination. // TODO: there are other patterns besides zero that we can usefully memset, // like -1, which happens to be the pattern used by member-pointers. if (!CGM.getTypes().isZeroInitializable(Ty)) { // For a VLA, emit a single element, then splat that over the VLA. if (vla) Ty = getContext().getBaseElementType(vla); llvm::Constant *NullConstant = CGM.EmitNullConstant(Ty); llvm::GlobalVariable *NullVariable = new llvm::GlobalVariable(CGM.getModule(), NullConstant->getType(), /*isConstant=*/true, llvm::GlobalVariable::PrivateLinkage, NullConstant, Twine()); CharUnits NullAlign = DestPtr.getAlignment(); NullVariable->setAlignment(NullAlign.getAsAlign()); Address SrcPtr(Builder.CreateBitCast(NullVariable, Builder.getInt8PtrTy()), NullAlign); if (vla) return emitNonZeroVLAInit(*this, Ty, DestPtr, SrcPtr, SizeVal); // Get and call the appropriate llvm.memcpy overload. Builder.CreateMemCpy(DestPtr, SrcPtr, SizeVal, false); return; } // Otherwise, just memset the whole thing to zero. This is legal // because in LLVM, all default initializers (other than the ones we just // handled above) are guaranteed to have a bit pattern of all zeros. Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, false); } llvm::BlockAddress *CodeGenFunction::GetAddrOfLabel(const LabelDecl *L) { // Make sure that there is a block for the indirect goto. if (!IndirectBranch) GetIndirectGotoBlock(); llvm::BasicBlock *BB = getJumpDestForLabel(L).getBlock(); // Make sure the indirect branch includes all of the address-taken blocks. IndirectBranch->addDestination(BB); return llvm::BlockAddress::get(CurFn, BB); } llvm::BasicBlock *CodeGenFunction::GetIndirectGotoBlock() { // If we already made the indirect branch for indirect goto, return its block. if (IndirectBranch) return IndirectBranch->getParent(); CGBuilderTy TmpBuilder(*this, createBasicBlock("indirectgoto")); // Create the PHI node that indirect gotos will add entries to. llvm::Value *DestVal = TmpBuilder.CreatePHI(Int8PtrTy, 0, "indirect.goto.dest"); // Create the indirect branch instruction. IndirectBranch = TmpBuilder.CreateIndirectBr(DestVal); return IndirectBranch->getParent(); } /// Computes the length of an array in elements, as well as the base /// element type and a properly-typed first element pointer. llvm::Value *CodeGenFunction::emitArrayLength(const ArrayType *origArrayType, QualType &baseType, Address &addr) { const ArrayType *arrayType = origArrayType; // If it's a VLA, we have to load the stored size. Note that // this is the size of the VLA in bytes, not its size in elements. llvm::Value *numVLAElements = nullptr; if (isa(arrayType)) { numVLAElements = getVLASize(cast(arrayType)).NumElts; // Walk into all VLAs. This doesn't require changes to addr, // which has type T* where T is the first non-VLA element type. do { QualType elementType = arrayType->getElementType(); arrayType = getContext().getAsArrayType(elementType); // If we only have VLA components, 'addr' requires no adjustment. if (!arrayType) { baseType = elementType; return numVLAElements; } } while (isa(arrayType)); // We get out here only if we find a constant array type // inside the VLA. } // We have some number of constant-length arrays, so addr should // have LLVM type [M x [N x [...]]]*. Build a GEP that walks // down to the first element of addr. SmallVector gepIndices; // GEP down to the array type. llvm::ConstantInt *zero = Builder.getInt32(0); gepIndices.push_back(zero); uint64_t countFromCLAs = 1; QualType eltType; llvm::ArrayType *llvmArrayType = dyn_cast(addr.getElementType()); while (llvmArrayType) { assert(isa(arrayType)); assert(cast(arrayType)->getSize().getZExtValue() == llvmArrayType->getNumElements()); gepIndices.push_back(zero); countFromCLAs *= llvmArrayType->getNumElements(); eltType = arrayType->getElementType(); llvmArrayType = dyn_cast(llvmArrayType->getElementType()); arrayType = getContext().getAsArrayType(arrayType->getElementType()); assert((!llvmArrayType || arrayType) && "LLVM and Clang types are out-of-synch"); } if (arrayType) { // From this point onwards, the Clang array type has been emitted // as some other type (probably a packed struct). Compute the array // size, and just emit the 'begin' expression as a bitcast. while (arrayType) { countFromCLAs *= cast(arrayType)->getSize().getZExtValue(); eltType = arrayType->getElementType(); arrayType = getContext().getAsArrayType(eltType); } llvm::Type *baseType = ConvertType(eltType); addr = Builder.CreateElementBitCast(addr, baseType, "array.begin"); } else { // Create the actual GEP. addr = Address(Builder.CreateInBoundsGEP(addr.getPointer(), gepIndices, "array.begin"), addr.getAlignment()); } baseType = eltType; llvm::Value *numElements = llvm::ConstantInt::get(SizeTy, countFromCLAs); // If we had any VLA dimensions, factor them in. if (numVLAElements) numElements = Builder.CreateNUWMul(numVLAElements, numElements); return numElements; } CodeGenFunction::VlaSizePair CodeGenFunction::getVLASize(QualType type) { const VariableArrayType *vla = getContext().getAsVariableArrayType(type); assert(vla && "type was not a variable array type!"); return getVLASize(vla); } CodeGenFunction::VlaSizePair CodeGenFunction::getVLASize(const VariableArrayType *type) { // The number of elements so far; always size_t. llvm::Value *numElements = nullptr; QualType elementType; do { elementType = type->getElementType(); llvm::Value *vlaSize = VLASizeMap[type->getSizeExpr()]; assert(vlaSize && "no size for VLA!"); assert(vlaSize->getType() == SizeTy); if (!numElements) { numElements = vlaSize; } else { // It's undefined behavior if this wraps around, so mark it that way. // FIXME: Teach -fsanitize=undefined to trap this. numElements = Builder.CreateNUWMul(numElements, vlaSize); } } while ((type = getContext().getAsVariableArrayType(elementType))); return { numElements, elementType }; } CodeGenFunction::VlaSizePair CodeGenFunction::getVLAElements1D(QualType type) { const VariableArrayType *vla = getContext().getAsVariableArrayType(type); assert(vla && "type was not a variable array type!"); return getVLAElements1D(vla); } CodeGenFunction::VlaSizePair CodeGenFunction::getVLAElements1D(const VariableArrayType *Vla) { llvm::Value *VlaSize = VLASizeMap[Vla->getSizeExpr()]; assert(VlaSize && "no size for VLA!"); assert(VlaSize->getType() == SizeTy); return { VlaSize, Vla->getElementType() }; } void CodeGenFunction::EmitVariablyModifiedType(QualType type) { assert(type->isVariablyModifiedType() && "Must pass variably modified type to EmitVLASizes!"); EnsureInsertPoint(); // We're going to walk down into the type and look for VLA // expressions. do { assert(type->isVariablyModifiedType()); const Type *ty = type.getTypePtr(); switch (ty->getTypeClass()) { #define TYPE(Class, Base) #define ABSTRACT_TYPE(Class, Base) #define NON_CANONICAL_TYPE(Class, Base) #define DEPENDENT_TYPE(Class, Base) case Type::Class: #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base) #include "clang/AST/TypeNodes.inc" llvm_unreachable("unexpected dependent type!"); // These types are never variably-modified. case Type::Builtin: case Type::Complex: case Type::Vector: case Type::ExtVector: case Type::Record: case Type::Enum: case Type::Elaborated: case Type::TemplateSpecialization: case Type::ObjCTypeParam: case Type::ObjCObject: case Type::ObjCInterface: case Type::ObjCObjectPointer: llvm_unreachable("type class is never variably-modified!"); case Type::Adjusted: type = cast(ty)->getAdjustedType(); break; case Type::Decayed: type = cast(ty)->getPointeeType(); break; case Type::Pointer: type = cast(ty)->getPointeeType(); break; case Type::BlockPointer: type = cast(ty)->getPointeeType(); break; case Type::LValueReference: case Type::RValueReference: type = cast(ty)->getPointeeType(); break; case Type::MemberPointer: type = cast(ty)->getPointeeType(); break; case Type::ConstantArray: case Type::IncompleteArray: // Losing element qualification here is fine. type = cast(ty)->getElementType(); break; case Type::VariableArray: { // Losing element qualification here is fine. const VariableArrayType *vat = cast(ty); // Unknown size indication requires no size computation. // Otherwise, evaluate and record it. if (const Expr *size = vat->getSizeExpr()) { // It's possible that we might have emitted this already, // e.g. with a typedef and a pointer to it. llvm::Value *&entry = VLASizeMap[size]; if (!entry) { llvm::Value *Size = EmitScalarExpr(size); // C11 6.7.6.2p5: // If the size is an expression that is not an integer constant // expression [...] each time it is evaluated it shall have a value // greater than zero. if (SanOpts.has(SanitizerKind::VLABound) && size->getType()->isSignedIntegerType()) { SanitizerScope SanScope(this); llvm::Value *Zero = llvm::Constant::getNullValue(Size->getType()); llvm::Constant *StaticArgs[] = { EmitCheckSourceLocation(size->getBeginLoc()), EmitCheckTypeDescriptor(size->getType())}; EmitCheck(std::make_pair(Builder.CreateICmpSGT(Size, Zero), SanitizerKind::VLABound), SanitizerHandler::VLABoundNotPositive, StaticArgs, Size); } // Always zexting here would be wrong if it weren't // undefined behavior to have a negative bound. entry = Builder.CreateIntCast(Size, SizeTy, /*signed*/ false); } } type = vat->getElementType(); break; } case Type::FunctionProto: case Type::FunctionNoProto: type = cast(ty)->getReturnType(); break; case Type::Paren: case Type::TypeOf: case Type::UnaryTransform: case Type::Attributed: case Type::SubstTemplateTypeParm: case Type::PackExpansion: case Type::MacroQualified: // Keep walking after single level desugaring. type = type.getSingleStepDesugaredType(getContext()); break; case Type::Typedef: case Type::Decltype: case Type::Auto: case Type::DeducedTemplateSpecialization: // Stop walking: nothing to do. return; case Type::TypeOfExpr: // Stop walking: emit typeof expression. EmitIgnoredExpr(cast(ty)->getUnderlyingExpr()); return; case Type::Atomic: type = cast(ty)->getValueType(); break; case Type::Pipe: type = cast(ty)->getElementType(); break; } } while (type->isVariablyModifiedType()); } Address CodeGenFunction::EmitVAListRef(const Expr* E) { if (getContext().getBuiltinVaListType()->isArrayType()) return EmitPointerWithAlignment(E); return EmitLValue(E).getAddress(*this); } Address CodeGenFunction::EmitMSVAListRef(const Expr *E) { return EmitLValue(E).getAddress(*this); } void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E, const APValue &Init) { assert(Init.hasValue() && "Invalid DeclRefExpr initializer!"); if (CGDebugInfo *Dbg = getDebugInfo()) if (CGM.getCodeGenOpts().hasReducedDebugInfo()) Dbg->EmitGlobalVariable(E->getDecl(), Init); } CodeGenFunction::PeepholeProtection CodeGenFunction::protectFromPeepholes(RValue rvalue) { // At the moment, the only aggressive peephole we do in IR gen // is trunc(zext) folding, but if we add more, we can easily // extend this protection. if (!rvalue.isScalar()) return PeepholeProtection(); llvm::Value *value = rvalue.getScalarVal(); if (!isa(value)) return PeepholeProtection(); // Just make an extra bitcast. assert(HaveInsertPoint()); llvm::Instruction *inst = new llvm::BitCastInst(value, value->getType(), "", Builder.GetInsertBlock()); PeepholeProtection protection; protection.Inst = inst; return protection; } void CodeGenFunction::unprotectFromPeepholes(PeepholeProtection protection) { if (!protection.Inst) return; // In theory, we could try to duplicate the peepholes now, but whatever. protection.Inst->eraseFromParent(); } void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue, QualType Ty, SourceLocation Loc, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue) { llvm::Value *TheCheck; llvm::Instruction *Assumption = Builder.CreateAlignmentAssumption( CGM.getDataLayout(), PtrValue, Alignment, OffsetValue, &TheCheck); if (SanOpts.has(SanitizerKind::Alignment)) { EmitAlignmentAssumptionCheck(PtrValue, Ty, Loc, AssumptionLoc, Alignment, OffsetValue, TheCheck, Assumption); } } void CodeGenFunction::EmitAlignmentAssumption(llvm::Value *PtrValue, const Expr *E, SourceLocation AssumptionLoc, llvm::Value *Alignment, llvm::Value *OffsetValue) { if (auto *CE = dyn_cast(E)) E = CE->getSubExprAsWritten(); QualType Ty = E->getType(); SourceLocation Loc = E->getExprLoc(); EmitAlignmentAssumption(PtrValue, Ty, Loc, AssumptionLoc, Alignment, OffsetValue); } llvm::Value *CodeGenFunction::EmitAnnotationCall(llvm::Function *AnnotationFn, llvm::Value *AnnotatedVal, StringRef AnnotationStr, SourceLocation Location) { llvm::Value *Args[4] = { AnnotatedVal, Builder.CreateBitCast(CGM.EmitAnnotationString(AnnotationStr), Int8PtrTy), Builder.CreateBitCast(CGM.EmitAnnotationUnit(Location), Int8PtrTy), CGM.EmitAnnotationLineNo(Location) }; return Builder.CreateCall(AnnotationFn, Args); } void CodeGenFunction::EmitVarAnnotations(const VarDecl *D, llvm::Value *V) { assert(D->hasAttr() && "no annotate attribute"); // FIXME We create a new bitcast for every annotation because that's what // llvm-gcc was doing. for (const auto *I : D->specific_attrs()) EmitAnnotationCall(CGM.getIntrinsic(llvm::Intrinsic::var_annotation), Builder.CreateBitCast(V, CGM.Int8PtrTy, V->getName()), I->getAnnotation(), D->getLocation()); } Address CodeGenFunction::EmitFieldAnnotations(const FieldDecl *D, Address Addr) { assert(D->hasAttr() && "no annotate attribute"); llvm::Value *V = Addr.getPointer(); llvm::Type *VTy = V->getType(); llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::ptr_annotation, CGM.Int8PtrTy); for (const auto *I : D->specific_attrs()) { // FIXME Always emit the cast inst so we can differentiate between // annotation on the first field of a struct and annotation on the struct // itself. if (VTy != CGM.Int8PtrTy) V = Builder.CreateBitCast(V, CGM.Int8PtrTy); V = EmitAnnotationCall(F, V, I->getAnnotation(), D->getLocation()); V = Builder.CreateBitCast(V, VTy); } return Address(V, Addr.getAlignment()); } CodeGenFunction::CGCapturedStmtInfo::~CGCapturedStmtInfo() { } CodeGenFunction::SanitizerScope::SanitizerScope(CodeGenFunction *CGF) : CGF(CGF) { assert(!CGF->IsSanitizerScope); CGF->IsSanitizerScope = true; } CodeGenFunction::SanitizerScope::~SanitizerScope() { CGF->IsSanitizerScope = false; } void CodeGenFunction::InsertHelper(llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB, llvm::BasicBlock::iterator InsertPt) const { LoopStack.InsertHelper(I); if (IsSanitizerScope) CGM.getSanitizerMetadata()->disableSanitizerForInstruction(I); } void CGBuilderInserter::InsertHelper( llvm::Instruction *I, const llvm::Twine &Name, llvm::BasicBlock *BB, llvm::BasicBlock::iterator InsertPt) const { llvm::IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt); if (CGF) CGF->InsertHelper(I, Name, BB, InsertPt); } static bool hasRequiredFeatures(const SmallVectorImpl &ReqFeatures, CodeGenModule &CGM, const FunctionDecl *FD, std::string &FirstMissing) { // If there aren't any required features listed then go ahead and return. if (ReqFeatures.empty()) return false; // Now build up the set of caller features and verify that all the required // features are there. llvm::StringMap CallerFeatureMap; CGM.getContext().getFunctionFeatureMap(CallerFeatureMap, FD); // If we have at least one of the features in the feature list return // true, otherwise return false. return std::all_of( ReqFeatures.begin(), ReqFeatures.end(), [&](StringRef Feature) { SmallVector OrFeatures; Feature.split(OrFeatures, '|'); return llvm::any_of(OrFeatures, [&](StringRef Feature) { if (!CallerFeatureMap.lookup(Feature)) { FirstMissing = Feature.str(); return false; } return true; }); }); } // Emits an error if we don't have a valid set of target features for the // called function. void CodeGenFunction::checkTargetFeatures(const CallExpr *E, const FunctionDecl *TargetDecl) { return checkTargetFeatures(E->getBeginLoc(), TargetDecl); } // Emits an error if we don't have a valid set of target features for the // called function. void CodeGenFunction::checkTargetFeatures(SourceLocation Loc, const FunctionDecl *TargetDecl) { // Early exit if this is an indirect call. if (!TargetDecl) return; // Get the current enclosing function if it exists. If it doesn't // we can't check the target features anyhow. const FunctionDecl *FD = dyn_cast_or_null(CurCodeDecl); if (!FD) return; // Grab the required features for the call. For a builtin this is listed in // the td file with the default cpu, for an always_inline function this is any // listed cpu and any listed features. unsigned BuiltinID = TargetDecl->getBuiltinID(); std::string MissingFeature; if (BuiltinID) { SmallVector ReqFeatures; const char *FeatureList = CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID); // Return if the builtin doesn't have any required features. if (!FeatureList || StringRef(FeatureList) == "") return; StringRef(FeatureList).split(ReqFeatures, ','); if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature)) CGM.getDiags().Report(Loc, diag::err_builtin_needs_feature) << TargetDecl->getDeclName() << CGM.getContext().BuiltinInfo.getRequiredFeatures(BuiltinID); } else if (!TargetDecl->isMultiVersion() && TargetDecl->hasAttr()) { // Get the required features for the callee. const TargetAttr *TD = TargetDecl->getAttr(); ParsedTargetAttr ParsedAttr = CGM.getContext().filterFunctionTargetAttrs(TD); SmallVector ReqFeatures; llvm::StringMap CalleeFeatureMap; - CGM.getContext().getFunctionFeatureMap(CalleeFeatureMap, - GlobalDecl(TargetDecl)); + CGM.getContext().getFunctionFeatureMap(CalleeFeatureMap, TargetDecl); for (const auto &F : ParsedAttr.Features) { if (F[0] == '+' && CalleeFeatureMap.lookup(F.substr(1))) ReqFeatures.push_back(StringRef(F).substr(1)); } for (const auto &F : CalleeFeatureMap) { // Only positive features are "required". if (F.getValue()) ReqFeatures.push_back(F.getKey()); } if (!hasRequiredFeatures(ReqFeatures, CGM, FD, MissingFeature)) CGM.getDiags().Report(Loc, diag::err_function_needs_feature) << FD->getDeclName() << TargetDecl->getDeclName() << MissingFeature; } } void CodeGenFunction::EmitSanitizerStatReport(llvm::SanitizerStatKind SSK) { if (!CGM.getCodeGenOpts().SanitizeStats) return; llvm::IRBuilder<> IRB(Builder.GetInsertBlock(), Builder.GetInsertPoint()); IRB.SetCurrentDebugLocation(Builder.getCurrentDebugLocation()); CGM.getSanStats().create(IRB, SSK); } llvm::Value * CodeGenFunction::FormResolverCondition(const MultiVersionResolverOption &RO) { llvm::Value *Condition = nullptr; if (!RO.Conditions.Architecture.empty()) Condition = EmitX86CpuIs(RO.Conditions.Architecture); if (!RO.Conditions.Features.empty()) { llvm::Value *FeatureCond = EmitX86CpuSupports(RO.Conditions.Features); Condition = Condition ? Builder.CreateAnd(Condition, FeatureCond) : FeatureCond; } return Condition; } static void CreateMultiVersionResolverReturn(CodeGenModule &CGM, llvm::Function *Resolver, CGBuilderTy &Builder, llvm::Function *FuncToReturn, bool SupportsIFunc) { if (SupportsIFunc) { Builder.CreateRet(FuncToReturn); return; } llvm::SmallVector Args; llvm::for_each(Resolver->args(), [&](llvm::Argument &Arg) { Args.push_back(&Arg); }); llvm::CallInst *Result = Builder.CreateCall(FuncToReturn, Args); Result->setTailCallKind(llvm::CallInst::TCK_MustTail); if (Resolver->getReturnType()->isVoidTy()) Builder.CreateRetVoid(); else Builder.CreateRet(Result); } void CodeGenFunction::EmitMultiVersionResolver( llvm::Function *Resolver, ArrayRef Options) { assert(getContext().getTargetInfo().getTriple().isX86() && "Only implemented for x86 targets"); bool SupportsIFunc = getContext().getTargetInfo().supportsIFunc(); // Main function's basic block. llvm::BasicBlock *CurBlock = createBasicBlock("resolver_entry", Resolver); Builder.SetInsertPoint(CurBlock); EmitX86CpuInit(); for (const MultiVersionResolverOption &RO : Options) { Builder.SetInsertPoint(CurBlock); llvm::Value *Condition = FormResolverCondition(RO); // The 'default' or 'generic' case. if (!Condition) { assert(&RO == Options.end() - 1 && "Default or Generic case must be last"); CreateMultiVersionResolverReturn(CGM, Resolver, Builder, RO.Function, SupportsIFunc); return; } llvm::BasicBlock *RetBlock = createBasicBlock("resolver_return", Resolver); CGBuilderTy RetBuilder(*this, RetBlock); CreateMultiVersionResolverReturn(CGM, Resolver, RetBuilder, RO.Function, SupportsIFunc); CurBlock = createBasicBlock("resolver_else", Resolver); Builder.CreateCondBr(Condition, RetBlock, CurBlock); } // If no generic/default, emit an unreachable. Builder.SetInsertPoint(CurBlock); llvm::CallInst *TrapCall = EmitTrapCall(llvm::Intrinsic::trap); TrapCall->setDoesNotReturn(); TrapCall->setDoesNotThrow(); Builder.CreateUnreachable(); Builder.ClearInsertionPoint(); } // Loc - where the diagnostic will point, where in the source code this // alignment has failed. // SecondaryLoc - if present (will be present if sufficiently different from // Loc), the diagnostic will additionally point a "Note:" to this location. // It should be the location where the __attribute__((assume_aligned)) // was written e.g. void CodeGenFunction::EmitAlignmentAssumptionCheck( llvm::Value *Ptr, QualType Ty, SourceLocation Loc, SourceLocation SecondaryLoc, llvm::Value *Alignment, llvm::Value *OffsetValue, llvm::Value *TheCheck, llvm::Instruction *Assumption) { assert(Assumption && isa(Assumption) && cast(Assumption)->getCalledValue() == llvm::Intrinsic::getDeclaration( Builder.GetInsertBlock()->getParent()->getParent(), llvm::Intrinsic::assume) && "Assumption should be a call to llvm.assume()."); assert(&(Builder.GetInsertBlock()->back()) == Assumption && "Assumption should be the last instruction of the basic block, " "since the basic block is still being generated."); if (!SanOpts.has(SanitizerKind::Alignment)) return; // Don't check pointers to volatile data. The behavior here is implementation- // defined. if (Ty->getPointeeType().isVolatileQualified()) return; // We need to temorairly remove the assumption so we can insert the // sanitizer check before it, else the check will be dropped by optimizations. Assumption->removeFromParent(); { SanitizerScope SanScope(this); if (!OffsetValue) OffsetValue = Builder.getInt1(0); // no offset. llvm::Constant *StaticData[] = {EmitCheckSourceLocation(Loc), EmitCheckSourceLocation(SecondaryLoc), EmitCheckTypeDescriptor(Ty)}; llvm::Value *DynamicData[] = {EmitCheckValue(Ptr), EmitCheckValue(Alignment), EmitCheckValue(OffsetValue)}; EmitCheck({std::make_pair(TheCheck, SanitizerKind::Alignment)}, SanitizerHandler::AlignmentAssumption, StaticData, DynamicData); } // We are now in the (new, empty) "cont" basic block. // Reintroduce the assumption. Builder.Insert(Assumption); // FIXME: Assumption still has it's original basic block as it's Parent. } llvm::DebugLoc CodeGenFunction::SourceLocToDebugLoc(SourceLocation Location) { if (CGDebugInfo *DI = getDebugInfo()) return DI->SourceLocToDebugLoc(Location); return llvm::DebugLoc(); }