diff options
author | Anders Carlsson <andersca@mac.com> | 2009-09-23 18:59:48 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-09-23 18:59:48 +0000 |
commit | 6ac5fc49ee70b4e3ce3e242c02c66586f652d7ac (patch) | |
tree | bdcec840af1f9b39895ff28d133626e8110b5823 /lib/CodeGen/CGCXXExpr.cpp | |
parent | 4ade6d6eae934f796ca43c81a5aa185e456dde9b (diff) |
Emit new[] cookie when needed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@82642 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGCXXExpr.cpp')
-rw-r--r-- | lib/CodeGen/CGCXXExpr.cpp | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/lib/CodeGen/CGCXXExpr.cpp b/lib/CodeGen/CGCXXExpr.cpp index bf08caa3b4..1ee4ceb136 100644 --- a/lib/CodeGen/CGCXXExpr.cpp +++ b/lib/CodeGen/CGCXXExpr.cpp @@ -16,6 +16,9 @@ using namespace clang; using namespace CodeGen; static uint64_t CalculateCookiePadding(ASTContext &Ctx, const CXXNewExpr *E) { + if (!E->isArray()) + return 0; + QualType T = E->getAllocatedType(); const RecordType *RT = T->getAs<RecordType>(); @@ -34,7 +37,7 @@ static uint64_t CalculateCookiePadding(ASTContext &Ctx, const CXXNewExpr *E) { // Padding is the maximum of sizeof(size_t) and alignof(T) return std::max(Ctx.getTypeSize(Ctx.getSizeType()), - static_cast<uint64_t>(Ctx.getTypeAlign(T))); + static_cast<uint64_t>(Ctx.getTypeAlign(T))) / 8; } static llvm::Value *EmitCXXNewAllocSize(CodeGenFunction &CGF, @@ -115,11 +118,6 @@ static void EmitNewInitializer(CodeGenFunction &CGF, const CXXNewExpr *E, } llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { - if (E->isArray() && CalculateCookiePadding(getContext(), E)) { - ErrorUnsupported(E, "new[] expression"); - return llvm::UndefValue::get(ConvertType(E->getType())); - } - QualType AllocType = E->getAllocatedType(); FunctionDecl *NewFD = E->getOperatorNew(); const FunctionProtoType *NewFTy = NewFD->getType()->getAs<FunctionProtoType>(); @@ -199,6 +197,21 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) { EmitBlock(NewNotNull); } + if (uint64_t CookiePadding = CalculateCookiePadding(getContext(), E)) { + uint64_t CookieOffset = + CookiePadding - getContext().getTypeSize(SizeTy) / 8; + + llvm::Value *NumElementsPtr = + Builder.CreateConstInBoundsGEP1_64(NewPtr, CookieOffset); + + NumElementsPtr = Builder.CreateBitCast(NumElementsPtr, + ConvertType(SizeTy)->getPointerTo()); + Builder.CreateStore(NumElements, NumElementsPtr); + + // Now add the padding to the new ptr. + NewPtr = Builder.CreateConstInBoundsGEP1_64(NewPtr, CookiePadding); + } + NewPtr = Builder.CreateBitCast(NewPtr, ConvertType(E->getType())); EmitNewInitializer(*this, E, NewPtr, NumElements); |