Bug Summary

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