Skip to content

Commit bf08512

Browse files
committed
Revert: Fixes leak of duplicate command line environment strings
For bot crashes on Mac Review URL: https://codereview.chromium.org/2229973002 .
1 parent 92abe83 commit bf08512

8 files changed

+43
-113
lines changed

runtime/bin/eventhandler.h

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,6 @@ class CircularLinkedList {
132132
public:
133133
CircularLinkedList() : head_(NULL) {}
134134

135-
typedef void (*ClearFun) (void* value);
136-
137135
// Returns true if the list was empty.
138136
bool Add(T t) {
139137
Entry* e = new Entry(t);
@@ -153,7 +151,7 @@ class CircularLinkedList {
153151
}
154152
}
155153

156-
void RemoveHead(ClearFun clear = NULL) {
154+
void RemoveHead() {
157155
ASSERT(head_ != NULL);
158156

159157
Entry* e = head_;
@@ -164,9 +162,6 @@ class CircularLinkedList {
164162
e->next_->prev_ = e->prev_;
165163
head_ = e->next_;
166164
}
167-
if (clear != NULL) {
168-
clear(reinterpret_cast<void*>(e->t));
169-
}
170165
delete e;
171166
}
172167

@@ -200,9 +195,9 @@ class CircularLinkedList {
200195
}
201196
}
202197

203-
void RemoveAll(ClearFun clear = NULL) {
198+
void RemoveAll() {
204199
while (HasHead()) {
205-
RemoveHead(clear);
200+
RemoveHead();
206201
}
207202
}
208203

@@ -418,9 +413,7 @@ class DescriptorInfoMultipleMixin : public DI {
418413
: DI(fd), tokens_map_(&SamePortValue, kTokenCount),
419414
disable_tokens_(disable_tokens) {}
420415

421-
virtual ~DescriptorInfoMultipleMixin() {
422-
RemoveAllPorts();
423-
}
416+
virtual ~DescriptorInfoMultipleMixin() {}
424417

425418
virtual bool IsListeningSocket() const { return true; }
426419

@@ -504,16 +497,14 @@ class DescriptorInfoMultipleMixin : public DI {
504497
}
505498

506499
virtual void RemoveAllPorts() {
500+
active_readers_.RemoveAll();
507501
for (HashMap::Entry *entry = tokens_map_.Start();
508502
entry != NULL;
509503
entry = tokens_map_.Next(entry)) {
510504
PortEntry* pentry = reinterpret_cast<PortEntry*>(entry->value);
511-
entry->value = NULL;
512-
active_readers_.Remove(pentry);
513505
delete pentry;
514506
}
515507
tokens_map_.Clear();
516-
active_readers_.RemoveAll(DeletePortEntry);
517508
}
518509

519510
virtual Dart_Port NextNotifyDartPort(intptr_t events_ready) {
@@ -594,11 +585,6 @@ class DescriptorInfoMultipleMixin : public DI {
594585
}
595586

596587
private:
597-
static void DeletePortEntry(void* data) {
598-
PortEntry* entry = reinterpret_cast<PortEntry*>(data);
599-
delete entry;
600-
}
601-
602588
// The [Dart_Port]s which are not paused (i.e. are interested in read events,
603589
// i.e. `mask == (1 << kInEvent)`) and we have enough tokens to communicate
604590
// with them.

runtime/bin/eventhandler_android.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,7 @@ EventHandlerImplementation::EventHandlerImplementation()
118118
}
119119

120120

121-
static void DeleteDescriptorInfo(void* info) {
122-
DescriptorInfo* di = reinterpret_cast<DescriptorInfo*>(info);
123-
di->Close();
124-
delete di;
125-
}
126-
127-
128121
EventHandlerImplementation::~EventHandlerImplementation() {
129-
socket_map_.Clear(DeleteDescriptorInfo);
130122
VOID_TEMP_FAILURE_RETRY(close(epoll_fd_));
131123
VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0]));
132124
VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1]));

runtime/bin/eventhandler_linux.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,7 @@ EventHandlerImplementation::EventHandlerImplementation()
127127
}
128128

129129

130-
static void DeleteDescriptorInfo(void* info) {
131-
DescriptorInfo* di = reinterpret_cast<DescriptorInfo*>(info);
132-
di->Close();
133-
delete di;
134-
}
135-
136-
137130
EventHandlerImplementation::~EventHandlerImplementation() {
138-
socket_map_.Clear(DeleteDescriptorInfo);
139131
VOID_TEMP_FAILURE_RETRY(close(epoll_fd_));
140132
VOID_TEMP_FAILURE_RETRY(close(timer_fd_));
141133
VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0]));

runtime/bin/eventhandler_macos.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,7 @@ EventHandlerImplementation::EventHandlerImplementation()
140140
}
141141

142142

143-
static void DeleteDescriptorInfo(void* info) {
144-
DescriptorInfo* di = reinterpret_cast<DescriptorInfo*>(info);
145-
di->Close();
146-
delete di;
147-
}
148-
149-
150143
EventHandlerImplementation::~EventHandlerImplementation() {
151-
socket_map_.Clear(DeleteDescriptorInfo);
152144
VOID_TEMP_FAILURE_RETRY(close(kqueue_fd_));
153145
VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[0]));
154146
VOID_TEMP_FAILURE_RETRY(close(interrupt_fds_[1]));

runtime/bin/socket.cc

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -127,57 +127,42 @@ Dart_Handle ListeningSocketRegistry::CreateBindListen(Dart_Handle socket_object,
127127
}
128128

129129

130-
bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket) {
130+
bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) {
131131
ASSERT(!mutex_->TryLock());
132-
ASSERT(os_socket != NULL);
133-
ASSERT(os_socket->ref_count > 0);
134-
os_socket->ref_count--;
135-
if (os_socket->ref_count > 0) {
136-
return false;
137-
}
138-
// We free the OS socket by removing it from two datastructures.
139-
sockets_by_fd_.erase(os_socket->socketfd);
140-
141-
OSSocket *prev = NULL;
142-
OSSocket *current = sockets_by_port_[os_socket->port];
143-
while (current != os_socket) {
144-
ASSERT(current != NULL);
145-
prev = current;
146-
current = current->next;
147-
}
148-
149-
if ((prev == NULL) && (current->next == NULL)) {
150-
// Remove last element from the list.
151-
sockets_by_port_.erase(os_socket->port);
152-
} else if (prev == NULL) {
153-
// Remove first element of the list.
154-
sockets_by_port_[os_socket->port] = current->next;
155-
} else {
156-
// Remove element from the list which is not the first one.
157-
prev->next = os_socket->next;
158-
}
159-
160-
ASSERT(os_socket->ref_count == 0);
161-
delete os_socket;
162-
return true;
163-
}
164132

133+
SocketsIterator it = sockets_by_fd_.find(socketfd);
134+
if (it != sockets_by_fd_.end()) {
135+
OSSocket *os_socket = it->second;
165136

166-
void ListeningSocketRegistry::CloseAllSafe() {
167-
MutexLocker ml(mutex_);
168-
SocketsIterator it = sockets_by_fd_.begin();
169-
while (it != sockets_by_fd_.end()) {
170-
CloseOneSafe(it->second);
171-
it++;
172-
}
173-
}
137+
ASSERT(os_socket->ref_count > 0);
138+
os_socket->ref_count--;
139+
if (os_socket->ref_count == 0) {
140+
// We free the OS socket by removing it from two datastructures.
141+
sockets_by_fd_.erase(socketfd);
142+
143+
OSSocket *prev = NULL;
144+
OSSocket *current = sockets_by_port_[os_socket->port];
145+
while (current != os_socket) {
146+
ASSERT(current != NULL);
147+
prev = current;
148+
current = current->next;
149+
}
174150

151+
if ((prev == NULL) && (current->next == NULL)) {
152+
// Remove last element from the list.
153+
sockets_by_port_.erase(os_socket->port);
154+
} else if (prev == NULL) {
155+
// Remove first element of the list.
156+
sockets_by_port_[os_socket->port] = current->next;
157+
} else {
158+
// Remove element from the list which is not the first one.
159+
prev->next = os_socket->next;
160+
}
175161

176-
bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) {
177-
ASSERT(!mutex_->TryLock());
178-
SocketsIterator it = sockets_by_fd_.find(socketfd);
179-
if (it != sockets_by_fd_.end()) {
180-
return CloseOneSafe(it->second);
162+
delete os_socket;
163+
return true;
164+
}
165+
return false;
181166
} else {
182167
// It should be impossible for the event handler to close something that
183168
// hasn't been created before.

runtime/bin/socket.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,6 @@ class ListeningSocketRegistry {
402402
ListeningSocketRegistry() : mutex_(new Mutex()) {}
403403

404404
~ListeningSocketRegistry() {
405-
CloseAllSafe();
406405
delete mutex_;
407406
mutex_ = NULL;
408407
}
@@ -438,10 +437,6 @@ class ListeningSocketRegistry {
438437
return NULL;
439438
}
440439

441-
bool CloseOneSafe(OSSocket* os_socket);
442-
void CloseAllSafe();
443-
444-
// TODO(zra): Replace std::map with the HashMap in platform/hashmap.h.
445440
std::map<intptr_t, OSSocket*> sockets_by_port_;
446441
std::map<intptr_t, OSSocket*> sockets_by_fd_;
447442
Mutex *mutex_;

runtime/platform/hashmap.cc

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ HashMap::HashMap(MatchFun match, uint32_t initial_capacity) {
1515

1616

1717
HashMap::~HashMap() {
18-
delete[] map_;
18+
free(map_);
1919
}
2020

2121

@@ -106,19 +106,14 @@ void HashMap::Remove(void* key, uint32_t hash) {
106106

107107
// Clear the candidate which will not break searching the hash table.
108108
candidate->key = NULL;
109-
candidate->value = NULL;
110109
occupancy_--;
111110
}
112111

113112

114-
void HashMap::Clear(ClearFun clear) {
113+
void HashMap::Clear() {
115114
// Mark all entries as empty.
116115
const Entry* end = map_end();
117116
for (Entry* p = map_; p < end; p++) {
118-
if ((clear != NULL) && (p->key != NULL)) {
119-
clear(p->value);
120-
}
121-
p->value = NULL;
122117
p->key = NULL;
123118
}
124119
occupancy_ = 0;
@@ -164,14 +159,14 @@ HashMap::Entry* HashMap::Probe(void* key, uint32_t hash) {
164159

165160
void HashMap::Initialize(uint32_t capacity) {
166161
ASSERT(dart::Utils::IsPowerOfTwo(capacity));
167-
map_ = new Entry[capacity];
162+
map_ = reinterpret_cast<Entry*>(malloc(capacity * sizeof(Entry)));
168163
if (map_ == NULL) {
169164
// TODO(sgjesse): Handle out of memory.
170165
FATAL("Cannot allocate memory for hashmap");
171166
return;
172167
}
173168
capacity_ = capacity;
174-
occupancy_ = 0;
169+
Clear();
175170
}
176171

177172

@@ -191,7 +186,7 @@ void HashMap::Resize() {
191186
}
192187

193188
// Delete old map.
194-
delete[] map;
189+
free(map);
195190
}
196191

197192
} // namespace dart

runtime/platform/hashmap.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ class HashMap {
1313
public:
1414
typedef bool (*MatchFun) (void* key1, void* key2);
1515

16-
typedef void (*ClearFun) (void* value);
17-
1816
static bool SamePointerValue(void* key1, void* key2) {
1917
return key1 == key2;
2018
}
@@ -50,7 +48,6 @@ class HashMap {
5048
// Some clients may not need to use the value slot
5149
// (e.g. implementers of sets, where the key is the value).
5250
struct Entry {
53-
Entry() : key(NULL), value(NULL), hash(0) {}
5451
void* key;
5552
void* value;
5653
uint32_t hash; // The full hash value for key.
@@ -66,12 +63,8 @@ class HashMap {
6663
// Removes the entry with matching key.
6764
void Remove(void* key, uint32_t hash);
6865

69-
// Empties the hash map (occupancy() == 0), and calls the function 'clear' on
70-
// each of the values if given.
71-
void Clear(ClearFun clear = NULL);
72-
73-
// The number of entries stored in the table.
74-
intptr_t size() const { return occupancy_; }
66+
// Empties the hash map (occupancy() == 0).
67+
void Clear();
7568

7669
// The capacity of the table. The implementation
7770
// makes sure that occupancy is at most 80% of

0 commit comments

Comments
 (0)