Bug Summary

File:lib/IR/User.cpp
Warning:line 138, column 5
Potential leak of memory pointed to by 'Storage'

Annotated Source Code

1//===-- User.cpp - Implement the User class -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/IR/User.h"
11#include "llvm/IR/Constant.h"
12#include "llvm/IR/GlobalValue.h"
13#include "llvm/IR/Operator.h"
14
15namespace llvm {
16class BasicBlock;
17
18//===----------------------------------------------------------------------===//
19// User Class
20//===----------------------------------------------------------------------===//
21
22void User::anchor() {}
23
24void User::replaceUsesOfWith(Value *From, Value *To) {
25 if (From == To) return; // Duh what?
26
27 assert((!isa<Constant>(this) || isa<GlobalValue>(this)) &&(((!isa<Constant>(this) || isa<GlobalValue>(this)
) && "Cannot call User::replaceUsesOfWith on a constant!"
) ? static_cast<void> (0) : __assert_fail ("(!isa<Constant>(this) || isa<GlobalValue>(this)) && \"Cannot call User::replaceUsesOfWith on a constant!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 28, __PRETTY_FUNCTION__))
28 "Cannot call User::replaceUsesOfWith on a constant!")(((!isa<Constant>(this) || isa<GlobalValue>(this)
) && "Cannot call User::replaceUsesOfWith on a constant!"
) ? static_cast<void> (0) : __assert_fail ("(!isa<Constant>(this) || isa<GlobalValue>(this)) && \"Cannot call User::replaceUsesOfWith on a constant!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 28, __PRETTY_FUNCTION__))
;
29
30 for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
31 if (getOperand(i) == From) { // Is This operand is pointing to oldval?
32 // The side effects of this setOperand call include linking to
33 // "To", adding "this" to the uses list of To, and
34 // most importantly, removing "this" from the use list of "From".
35 setOperand(i, To); // Fix it now...
36 }
37}
38
39//===----------------------------------------------------------------------===//
40// User allocHungoffUses Implementation
41//===----------------------------------------------------------------------===//
42
43void User::allocHungoffUses(unsigned N, bool IsPhi) {
44 assert(HasHungOffUses && "alloc must have hung off uses")((HasHungOffUses && "alloc must have hung off uses") ?
static_cast<void> (0) : __assert_fail ("HasHungOffUses && \"alloc must have hung off uses\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 44, __PRETTY_FUNCTION__))
;
45
46 static_assert(alignof(Use) >= alignof(Use::UserRef),
47 "Alignment is insufficient for 'hung-off-uses' pieces");
48 static_assert(alignof(Use::UserRef) >= alignof(BasicBlock *),
49 "Alignment is insufficient for 'hung-off-uses' pieces");
50
51 // Allocate the array of Uses, followed by a pointer (with bottom bit set) to
52 // the User.
53 size_t size = N * sizeof(Use) + sizeof(Use::UserRef);
54 if (IsPhi)
55 size += N * sizeof(BasicBlock *);
56 Use *Begin = static_cast<Use*>(::operator new(size));
57 Use *End = Begin + N;
58 (void) new(End) Use::UserRef(const_cast<User*>(this), 1);
59 setOperandList(Use::initTags(Begin, End));
60}
61
62void User::growHungoffUses(unsigned NewNumUses, bool IsPhi) {
63 assert(HasHungOffUses && "realloc must have hung off uses")((HasHungOffUses && "realloc must have hung off uses"
) ? static_cast<void> (0) : __assert_fail ("HasHungOffUses && \"realloc must have hung off uses\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 63, __PRETTY_FUNCTION__))
;
64
65 unsigned OldNumUses = getNumOperands();
66
67 // We don't support shrinking the number of uses. We wouldn't have enough
68 // space to copy the old uses in to the new space.
69 assert(NewNumUses > OldNumUses && "realloc must grow num uses")((NewNumUses > OldNumUses && "realloc must grow num uses"
) ? static_cast<void> (0) : __assert_fail ("NewNumUses > OldNumUses && \"realloc must grow num uses\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 69, __PRETTY_FUNCTION__))
;
70
71 Use *OldOps = getOperandList();
72 allocHungoffUses(NewNumUses, IsPhi);
73 Use *NewOps = getOperandList();
74
75 // Now copy from the old operands list to the new one.
76 std::copy(OldOps, OldOps + OldNumUses, NewOps);
77
78 // If this is a Phi, then we need to copy the BB pointers too.
79 if (IsPhi) {
80 auto *OldPtr =
81 reinterpret_cast<char *>(OldOps + OldNumUses) + sizeof(Use::UserRef);
82 auto *NewPtr =
83 reinterpret_cast<char *>(NewOps + NewNumUses) + sizeof(Use::UserRef);
84 std::copy(OldPtr, OldPtr + (OldNumUses * sizeof(BasicBlock *)), NewPtr);
85 }
86 Use::zap(OldOps, OldOps + OldNumUses, true);
87}
88
89
90// This is a private struct used by `User` to track the co-allocated descriptor
91// section.
92struct DescriptorInfo {
93 intptr_t SizeInBytes;
94};
95
96ArrayRef<const uint8_t> User::getDescriptor() const {
97 auto MutableARef = const_cast<User *>(this)->getDescriptor();
98 return {MutableARef.begin(), MutableARef.end()};
99}
100
101MutableArrayRef<uint8_t> User::getDescriptor() {
102 assert(HasDescriptor && "Don't call otherwise!")((HasDescriptor && "Don't call otherwise!") ? static_cast
<void> (0) : __assert_fail ("HasDescriptor && \"Don't call otherwise!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 102, __PRETTY_FUNCTION__))
;
103 assert(!HasHungOffUses && "Invariant!")((!HasHungOffUses && "Invariant!") ? static_cast<void
> (0) : __assert_fail ("!HasHungOffUses && \"Invariant!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 103, __PRETTY_FUNCTION__))
;
104
105 auto *DI = reinterpret_cast<DescriptorInfo *>(getIntrusiveOperands()) - 1;
106 assert(DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!")((DI->SizeInBytes != 0 && "Should not have had a descriptor otherwise!"
) ? static_cast<void> (0) : __assert_fail ("DI->SizeInBytes != 0 && \"Should not have had a descriptor otherwise!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 106, __PRETTY_FUNCTION__))
;
107
108 return MutableArrayRef<uint8_t>(
109 reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes, DI->SizeInBytes);
110}
111
112//===----------------------------------------------------------------------===//
113// User operator new Implementations
114//===----------------------------------------------------------------------===//
115
116void *User::allocateFixedOperandUser(size_t Size, unsigned Us,
117 unsigned DescBytes) {
118 assert(Us < (1u << NumUserOperandsBits) && "Too many operands")((Us < (1u << NumUserOperandsBits) && "Too many operands"
) ? static_cast<void> (0) : __assert_fail ("Us < (1u << NumUserOperandsBits) && \"Too many operands\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 118, __PRETTY_FUNCTION__))
;
119
120 static_assert(sizeof(DescriptorInfo) % sizeof(void *) == 0, "Required below");
121
122 unsigned DescBytesToAllocate =
123 DescBytes == 0 ? 0 : (DescBytes + sizeof(DescriptorInfo));
2
Assuming 'DescBytes' is not equal to 0
3
'?' condition is false
124 assert(DescBytesToAllocate % sizeof(void *) == 0 &&((DescBytesToAllocate % sizeof(void *) == 0 && "We need this to satisfy alignment constraints for Uses"
) ? static_cast<void> (0) : __assert_fail ("DescBytesToAllocate % sizeof(void *) == 0 && \"We need this to satisfy alignment constraints for Uses\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 125, __PRETTY_FUNCTION__))
125 "We need this to satisfy alignment constraints for Uses")((DescBytesToAllocate % sizeof(void *) == 0 && "We need this to satisfy alignment constraints for Uses"
) ? static_cast<void> (0) : __assert_fail ("DescBytesToAllocate % sizeof(void *) == 0 && \"We need this to satisfy alignment constraints for Uses\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 125, __PRETTY_FUNCTION__))
;
126
127 uint8_t *Storage = static_cast<uint8_t *>(
128 ::operator new(Size + sizeof(Use) * Us + DescBytesToAllocate));
4
Memory is allocated
129 Use *Start = reinterpret_cast<Use *>(Storage + DescBytesToAllocate);
130 Use *End = Start + Us;
131 User *Obj = reinterpret_cast<User*>(End);
132 Obj->NumUserOperands = Us;
133 Obj->HasHungOffUses = false;
134 Obj->HasDescriptor = DescBytes != 0;
135 Use::initTags(Start, End);
136
137 if (DescBytes != 0) {
5
Taking true branch
138 auto *DescInfo = reinterpret_cast<DescriptorInfo *>(Storage + DescBytes);
6
Potential leak of memory pointed to by 'Storage'
139 DescInfo->SizeInBytes = DescBytes;
140 }
141
142 return Obj;
143}
144
145void *User::operator new(size_t Size, unsigned Us) {
146 return allocateFixedOperandUser(Size, Us, 0);
147}
148
149void *User::operator new(size_t Size, unsigned Us, unsigned DescBytes) {
150 return allocateFixedOperandUser(Size, Us, DescBytes);
1
Calling 'User::allocateFixedOperandUser'
151}
152
153void *User::operator new(size_t Size) {
154 // Allocate space for a single Use*
155 void *Storage = ::operator new(Size + sizeof(Use *));
156 Use **HungOffOperandList = static_cast<Use **>(Storage);
157 User *Obj = reinterpret_cast<User *>(HungOffOperandList + 1);
158 Obj->NumUserOperands = 0;
159 Obj->HasHungOffUses = true;
160 Obj->HasDescriptor = false;
161 *HungOffOperandList = nullptr;
162 return Obj;
163}
164
165//===----------------------------------------------------------------------===//
166// User operator delete Implementation
167//===----------------------------------------------------------------------===//
168
169void User::operator delete(void *Usr) {
170 // Hung off uses use a single Use* before the User, while other subclasses
171 // use a Use[] allocated prior to the user.
172 User *Obj = static_cast<User *>(Usr);
173 if (Obj->HasHungOffUses) {
174 assert(!Obj->HasDescriptor && "not supported!")((!Obj->HasDescriptor && "not supported!") ? static_cast
<void> (0) : __assert_fail ("!Obj->HasDescriptor && \"not supported!\""
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 174, __PRETTY_FUNCTION__))
;
175
176 Use **HungOffOperandList = static_cast<Use **>(Usr) - 1;
177 // drop the hung off uses.
178 Use::zap(*HungOffOperandList, *HungOffOperandList + Obj->NumUserOperands,
179 /* Delete */ true);
180 ::operator delete(HungOffOperandList);
181 } else if (Obj->HasDescriptor) {
182 Use *UseBegin = static_cast<Use *>(Usr) - Obj->NumUserOperands;
183 Use::zap(UseBegin, UseBegin + Obj->NumUserOperands, /* Delete */ false);
184
185 auto *DI = reinterpret_cast<DescriptorInfo *>(UseBegin) - 1;
186 uint8_t *Storage = reinterpret_cast<uint8_t *>(DI) - DI->SizeInBytes;
187 ::operator delete(Storage);
188 } else {
189 Use *Storage = static_cast<Use *>(Usr) - Obj->NumUserOperands;
190 Use::zap(Storage, Storage + Obj->NumUserOperands,
191 /* Delete */ false);
192 ::operator delete(Storage);
193 }
194}
195
196//===----------------------------------------------------------------------===//
197// Operator Class
198//===----------------------------------------------------------------------===//
199
200Operator::~Operator() {
201 llvm_unreachable("should never destroy an Operator")::llvm::llvm_unreachable_internal("should never destroy an Operator"
, "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn301389/lib/IR/User.cpp"
, 201)
;
202}
203
204} // End llvm namespace