Skip to content

Commit 91ebfbb

Browse files
committed
auto merge of #7859 : kmcallister/rust/rt-diag-messages, r=pcwalton
I added these while tracking down heap corruption in Servo.
2 parents 3514a5a + 2d82d93 commit 91ebfbb

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/rt/memory_region.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ void *memory_region::get_data(alloc_header *ptr) {
3434
return (void*)((char *)ptr + HEADER_SIZE);
3535
}
3636

37+
inline void memory_region::maybe_print_backtrace(const alloc_header *header) const {
38+
# if RUSTRT_TRACK_ALLOCATIONS >= 3
39+
if (_detailed_leaks) {
40+
backtrace_symbols_fd(header->bt + 1, header->btframes - 1, 2);
41+
}
42+
# endif
43+
}
44+
3745
memory_region::memory_region(bool synchronized,
3846
bool detailed_leaks,
3947
bool poison_on_free) :
@@ -174,13 +182,7 @@ memory_region::~memory_region() {
174182
header->tag,
175183
(uintptr_t) get_data(header));
176184
++leak_count;
177-
178-
# if RUSTRT_TRACK_ALLOCATIONS >= 3
179-
if (_detailed_leaks) {
180-
backtrace_symbols_fd(header->bt + 1,
181-
header->btframes - 1, 2);
182-
}
183-
# endif
185+
maybe_print_backtrace(header);
184186
}
185187
}
186188
assert(leak_count == _live_allocations);
@@ -203,9 +205,16 @@ memory_region::release_alloc(void *mem) {
203205

204206
# if RUSTRT_TRACK_ALLOCATIONS >= 2
205207
if (_synchronized) { _lock.lock(); }
208+
if (((size_t) alloc->index) >= _allocation_list.size()) {
209+
printf("free: ptr 0x%" PRIxPTR " (%s) index %d is beyond allocation_list of size %zu\n",
210+
(uintptr_t) get_data(alloc), alloc->tag, alloc->index, _allocation_list.size());
211+
maybe_print_backtrace(alloc);
212+
assert(false && "index beyond allocation_list");
213+
}
206214
if (_allocation_list[alloc->index] != alloc) {
207215
printf("free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
208216
(uintptr_t) get_data(alloc), alloc->tag);
217+
maybe_print_backtrace(alloc);
209218
assert(false && "not in allocation_list");
210219
}
211220
else {

src/rt/memory_region.h

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class memory_region {
6969
void release_alloc(void *mem);
7070
void claim_alloc(void *mem);
7171

72+
void maybe_print_backtrace(const alloc_header *) const;
73+
7274
private:
7375
// private and undefined to disable copying
7476
memory_region(const memory_region& rhs);

0 commit comments

Comments
 (0)