Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 3dd57df

Browse files
author
Zachary Turner
committed
[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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@337596 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7891d70 commit 3dd57df

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

lib/Demangle/MicrosoftDemangle.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,28 @@ class ArenaAllocator {
4646
}
4747
}
4848

49-
void *alloc(size_t Size) {
49+
template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
50+
51+
size_t Size = sizeof(T);
5052
assert(Size < Unit);
5153
assert(Head && Head->Buf);
5254

53-
uint8_t *P = Head->Buf + Head->Used;
54-
Head->Used += Size;
55+
size_t P = (size_t)Head->Buf + Head->Used;
56+
uintptr_t AlignedP =
57+
(((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
58+
uint8_t *PP = (uint8_t *)AlignedP;
59+
size_t Adjustment = AlignedP - P;
60+
61+
Head->Used += Size + Adjustment;
5562
if (Head->Used < Unit)
56-
return P;
63+
return new (PP) T(std::forward<Args>(ConstructorArgs)...);
5764

5865
AllocatorNode *NewHead = new AllocatorNode;
5966
NewHead->Buf = new uint8_t[ArenaAllocator::Unit];
6067
NewHead->Next = Head;
6168
Head = NewHead;
6269
NewHead->Used = Size;
63-
return NewHead->Buf;
70+
return new (NewHead->Buf) T(std::forward<Args>(ConstructorArgs)...);
6471
}
6572

6673
private:
@@ -84,8 +91,6 @@ static void outputSpaceIfNecessary(OutputStream &OS) {
8491
OS << " ";
8592
}
8693

87-
void *operator new(size_t Size, ArenaAllocator &A) { return A.alloc(Size); }
88-
8994
// Storage classes
9095
enum Qualifiers : uint8_t {
9196
Q_None = 0,
@@ -374,7 +379,7 @@ static void outputName(OutputStream &OS, const Name *TheName) {
374379
namespace {
375380

376381
Type *Type::clone(ArenaAllocator &Arena) const {
377-
return new (Arena) Type(*this);
382+
return Arena.alloc<Type>(*this);
378383
}
379384

380385
// Write the "first half" of a given type.
@@ -471,7 +476,7 @@ void Type::outputPre(OutputStream &OS) {
471476
void Type::outputPost(OutputStream &OS) {}
472477

473478
Type *PointerType::clone(ArenaAllocator &Arena) const {
474-
return new (Arena) PointerType(*this);
479+
return Arena.alloc<PointerType>(*this);
475480
}
476481

477482
void PointerType::outputPre(OutputStream &OS) {
@@ -509,7 +514,7 @@ void PointerType::outputPost(OutputStream &OS) {
509514
}
510515

511516
Type *FunctionType::clone(ArenaAllocator &Arena) const {
512-
return new (Arena) FunctionType(*this);
517+
return Arena.alloc<FunctionType>(*this);
513518
}
514519

515520
void FunctionType::outputPre(OutputStream &OS) {
@@ -536,7 +541,7 @@ void FunctionType::outputPost(OutputStream &OS) {
536541
}
537542

538543
Type *UdtType::clone(ArenaAllocator &Arena) const {
539-
return new (Arena) UdtType(*this);
544+
return Arena.alloc<UdtType>(*this);
540545
}
541546

542547
void UdtType::outputPre(OutputStream &OS) {
@@ -561,7 +566,7 @@ void UdtType::outputPre(OutputStream &OS) {
561566
}
562567

563568
Type *ArrayType::clone(ArenaAllocator &Arena) const {
564-
return new (Arena) ArrayType(*this);
569+
return Arena.alloc<ArrayType>(*this);
565570
}
566571

567572
void ArrayType::outputPre(OutputStream &OS) {
@@ -659,9 +664,9 @@ class Demangler {
659664
void Demangler::parse() {
660665
// MSVC-style mangled symbols must start with '?'.
661666
if (!MangledName.consumeFront("?")) {
662-
SymbolName = new (Arena) Name;
667+
SymbolName = Arena.alloc<Name>();
663668
SymbolName->Str = MangledName;
664-
SymbolType = new (Arena) Type;
669+
SymbolType = Arena.alloc<Type>();
665670
SymbolType->Prim = PrimTy::Unknown;
666671
}
667672

@@ -832,7 +837,7 @@ Name *Demangler::demangleName() {
832837
Name *Head = nullptr;
833838

834839
while (!MangledName.consumeFront("@")) {
835-
Name *Elem = new (Arena) Name;
840+
Name *Elem = Arena.alloc<Name>();
836841

837842
assert(!Error);
838843
demangleNamePiece(*Elem, Head == nullptr);
@@ -1239,7 +1244,7 @@ ReferenceKind Demangler::demangleReferenceKind() {
12391244
}
12401245

12411246
Type *Demangler::demangleFunctionEncoding() {
1242-
FunctionType *FTy = new (Arena) FunctionType;
1247+
FunctionType *FTy = Arena.alloc<FunctionType>();
12431248

12441249
FTy->Prim = PrimTy::Function;
12451250
FTy->FunctionClass = (FuncClass)demangleFunctionClass();
@@ -1265,7 +1270,7 @@ Type *Demangler::demangleFunctionEncoding() {
12651270

12661271
// Reads a primitive type.
12671272
Type *Demangler::demangleBasicType() {
1268-
Type *Ty = new (Arena) Type;
1273+
Type *Ty = Arena.alloc<Type>();
12691274

12701275
switch (MangledName.popFront()) {
12711276
case 'X':
@@ -1335,7 +1340,7 @@ Type *Demangler::demangleBasicType() {
13351340
}
13361341

13371342
UdtType *Demangler::demangleClassType() {
1338-
UdtType *UTy = new (Arena) UdtType;
1343+
UdtType *UTy = Arena.alloc<UdtType>();
13391344

13401345
switch (MangledName.popFront()) {
13411346
case 'T':
@@ -1365,7 +1370,7 @@ UdtType *Demangler::demangleClassType() {
13651370
// <pointer-type> ::= E? <pointer-cvr-qualifiers> <ext-qualifiers> <type>
13661371
// # the E is required for 64-bit non-static pointers
13671372
PointerType *Demangler::demanglePointerType() {
1368-
PointerType *Pointer = new (Arena) PointerType;
1373+
PointerType *Pointer = Arena.alloc<PointerType>();
13691374

13701375
Pointer->Quals = Q_None;
13711376
switch (MangledName.popFront()) {
@@ -1392,7 +1397,7 @@ PointerType *Demangler::demanglePointerType() {
13921397
}
13931398

13941399
if (MangledName.consumeFront("6")) {
1395-
FunctionType *FTy = new (Arena) FunctionType;
1400+
FunctionType *FTy = Arena.alloc<FunctionType>();
13961401
FTy->Prim = PrimTy::Function;
13971402
FTy->CallConvention = demangleCallingConvention();
13981403

@@ -1435,12 +1440,12 @@ ArrayType *Demangler::demangleArrayType() {
14351440
return nullptr;
14361441
}
14371442

1438-
ArrayType *ATy = new (Arena) ArrayType;
1443+
ArrayType *ATy = Arena.alloc<ArrayType>();
14391444
ArrayType *Dim = ATy;
14401445
for (int I = 0; I < Dimension; ++I) {
14411446
Dim->Prim = PrimTy::Array;
14421447
Dim->ArrayDimension = demangleNumber();
1443-
Dim->NextDimension = new (Arena) ArrayType;
1448+
Dim->NextDimension = Arena.alloc<ArrayType>();
14441449
Dim = Dim->NextDimension;
14451450
}
14461451

@@ -1476,15 +1481,15 @@ ParamList Demangler::demangleParameterList() {
14761481
}
14771482
MangledName = MangledName.dropFront();
14781483

1479-
*Current = new (Arena) ParamList;
1484+
*Current = Arena.alloc<ParamList>();
14801485
(*Current)->Current = BackRef[N]->clone(Arena);
14811486
Current = &(*Current)->Next;
14821487
continue;
14831488
}
14841489

14851490
size_t ArrayDimension = MangledName.size();
14861491

1487-
*Current = new (Arena) ParamList;
1492+
*Current = Arena.alloc<ParamList>();
14881493
(*Current)->Current = demangleType(QualifierMangleMode::Drop);
14891494

14901495
// Single-letter types are ignored for backreferences because

0 commit comments

Comments
 (0)