From 9d72aa9006e907b3930dbdb794693af42b24fc4b Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Fri, 20 Jul 2018 18:35:06 +0000 Subject: [PATCH] [Demangler] Correctly factor in assignment when allocating. Incidentally all allocations that we currently perform were properly aligned, but this was only an accident. Thanks to Erik Pilkington for catching this. llvm-svn: 337596 --- llvm/lib/Demangle/MicrosoftDemangle.cpp | 53 ++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/llvm/lib/Demangle/MicrosoftDemangle.cpp b/llvm/lib/Demangle/MicrosoftDemangle.cpp index 8977b9f..09892cf 100644 --- a/llvm/lib/Demangle/MicrosoftDemangle.cpp +++ b/llvm/lib/Demangle/MicrosoftDemangle.cpp @@ -46,21 +46,28 @@ public: } } - void *alloc(size_t Size) { + template T *alloc(Args &&... ConstructorArgs) { + + size_t Size = sizeof(T); assert(Size < Unit); assert(Head && Head->Buf); - uint8_t *P = Head->Buf + Head->Used; - Head->Used += Size; + size_t P = (size_t)Head->Buf + Head->Used; + uintptr_t AlignedP = + (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1)); + uint8_t *PP = (uint8_t *)AlignedP; + size_t Adjustment = AlignedP - P; + + Head->Used += Size + Adjustment; if (Head->Used < Unit) - return P; + return new (PP) T(std::forward(ConstructorArgs)...); AllocatorNode *NewHead = new AllocatorNode; NewHead->Buf = new uint8_t[ArenaAllocator::Unit]; NewHead->Next = Head; Head = NewHead; NewHead->Used = Size; - return NewHead->Buf; + return new (NewHead->Buf) T(std::forward(ConstructorArgs)...); } private: @@ -84,8 +91,6 @@ static void outputSpaceIfNecessary(OutputStream &OS) { OS << " "; } -void *operator new(size_t Size, ArenaAllocator &A) { return A.alloc(Size); } - // Storage classes enum Qualifiers : uint8_t { Q_None = 0, @@ -374,7 +379,7 @@ static void outputName(OutputStream &OS, const Name *TheName) { namespace { Type *Type::clone(ArenaAllocator &Arena) const { - return new (Arena) Type(*this); + return Arena.alloc(*this); } // Write the "first half" of a given type. @@ -471,7 +476,7 @@ void Type::outputPre(OutputStream &OS) { void Type::outputPost(OutputStream &OS) {} Type *PointerType::clone(ArenaAllocator &Arena) const { - return new (Arena) PointerType(*this); + return Arena.alloc(*this); } void PointerType::outputPre(OutputStream &OS) { @@ -509,7 +514,7 @@ void PointerType::outputPost(OutputStream &OS) { } Type *FunctionType::clone(ArenaAllocator &Arena) const { - return new (Arena) FunctionType(*this); + return Arena.alloc(*this); } void FunctionType::outputPre(OutputStream &OS) { @@ -536,7 +541,7 @@ void FunctionType::outputPost(OutputStream &OS) { } Type *UdtType::clone(ArenaAllocator &Arena) const { - return new (Arena) UdtType(*this); + return Arena.alloc(*this); } void UdtType::outputPre(OutputStream &OS) { @@ -561,7 +566,7 @@ void UdtType::outputPre(OutputStream &OS) { } Type *ArrayType::clone(ArenaAllocator &Arena) const { - return new (Arena) ArrayType(*this); + return Arena.alloc(*this); } void ArrayType::outputPre(OutputStream &OS) { @@ -659,9 +664,9 @@ private: void Demangler::parse() { // MSVC-style mangled symbols must start with '?'. if (!MangledName.consumeFront("?")) { - SymbolName = new (Arena) Name; + SymbolName = Arena.alloc(); SymbolName->Str = MangledName; - SymbolType = new (Arena) Type; + SymbolType = Arena.alloc(); SymbolType->Prim = PrimTy::Unknown; } @@ -832,7 +837,7 @@ Name *Demangler::demangleName() { Name *Head = nullptr; while (!MangledName.consumeFront("@")) { - Name *Elem = new (Arena) Name; + Name *Elem = Arena.alloc(); assert(!Error); demangleNamePiece(*Elem, Head == nullptr); @@ -1239,7 +1244,7 @@ ReferenceKind Demangler::demangleReferenceKind() { } Type *Demangler::demangleFunctionEncoding() { - FunctionType *FTy = new (Arena) FunctionType; + FunctionType *FTy = Arena.alloc(); FTy->Prim = PrimTy::Function; FTy->FunctionClass = (FuncClass)demangleFunctionClass(); @@ -1265,7 +1270,7 @@ Type *Demangler::demangleFunctionEncoding() { // Reads a primitive type. Type *Demangler::demangleBasicType() { - Type *Ty = new (Arena) Type; + Type *Ty = Arena.alloc(); switch (MangledName.popFront()) { case 'X': @@ -1335,7 +1340,7 @@ Type *Demangler::demangleBasicType() { } UdtType *Demangler::demangleClassType() { - UdtType *UTy = new (Arena) UdtType; + UdtType *UTy = Arena.alloc(); switch (MangledName.popFront()) { case 'T': @@ -1365,7 +1370,7 @@ UdtType *Demangler::demangleClassType() { // ::= E? // # the E is required for 64-bit non-static pointers PointerType *Demangler::demanglePointerType() { - PointerType *Pointer = new (Arena) PointerType; + PointerType *Pointer = Arena.alloc(); Pointer->Quals = Q_None; switch (MangledName.popFront()) { @@ -1392,7 +1397,7 @@ PointerType *Demangler::demanglePointerType() { } if (MangledName.consumeFront("6")) { - FunctionType *FTy = new (Arena) FunctionType; + FunctionType *FTy = Arena.alloc(); FTy->Prim = PrimTy::Function; FTy->CallConvention = demangleCallingConvention(); @@ -1435,12 +1440,12 @@ ArrayType *Demangler::demangleArrayType() { return nullptr; } - ArrayType *ATy = new (Arena) ArrayType; + ArrayType *ATy = Arena.alloc(); ArrayType *Dim = ATy; for (int I = 0; I < Dimension; ++I) { Dim->Prim = PrimTy::Array; Dim->ArrayDimension = demangleNumber(); - Dim->NextDimension = new (Arena) ArrayType; + Dim->NextDimension = Arena.alloc(); Dim = Dim->NextDimension; } @@ -1476,7 +1481,7 @@ ParamList Demangler::demangleParameterList() { } MangledName = MangledName.dropFront(); - *Current = new (Arena) ParamList; + *Current = Arena.alloc(); (*Current)->Current = BackRef[N]->clone(Arena); Current = &(*Current)->Next; continue; @@ -1484,7 +1489,7 @@ ParamList Demangler::demangleParameterList() { size_t ArrayDimension = MangledName.size(); - *Current = new (Arena) ParamList; + *Current = Arena.alloc(); (*Current)->Current = demangleType(QualifierMangleMode::Drop); // Single-letter types are ignored for backreferences because -- 2.7.4