Skip to content

Print more info on bad frees with RUSTRT_TRACK_ALLOCATIONS #7859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/rt/memory_region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ void *memory_region::get_data(alloc_header *ptr) {
return (void*)((char *)ptr + HEADER_SIZE);
}

inline void memory_region::maybe_print_backtrace(const alloc_header *header) const {
# if RUSTRT_TRACK_ALLOCATIONS >= 3
if (_detailed_leaks) {
backtrace_symbols_fd(header->bt + 1, header->btframes - 1, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work on Windows?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. Is the concern that the backtrace_* glibc functions won't be available? At any rate, RUSTRT_TRACK_ALLOCATIONS >= 3 already uses those elsewhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, OK, if it's already used elsewhere then I guess it's fine.

}
# endif
}

memory_region::memory_region(bool synchronized,
bool detailed_leaks,
bool poison_on_free) :
Expand Down Expand Up @@ -174,13 +182,7 @@ memory_region::~memory_region() {
header->tag,
(uintptr_t) get_data(header));
++leak_count;

# if RUSTRT_TRACK_ALLOCATIONS >= 3
if (_detailed_leaks) {
backtrace_symbols_fd(header->bt + 1,
header->btframes - 1, 2);
}
# endif
maybe_print_backtrace(header);
}
}
assert(leak_count == _live_allocations);
Expand All @@ -203,9 +205,16 @@ memory_region::release_alloc(void *mem) {

# if RUSTRT_TRACK_ALLOCATIONS >= 2
if (_synchronized) { _lock.lock(); }
if (((size_t) alloc->index) >= _allocation_list.size()) {
printf("free: ptr 0x%" PRIxPTR " (%s) index %d is beyond allocation_list of size %zu\n",
(uintptr_t) get_data(alloc), alloc->tag, alloc->index, _allocation_list.size());
maybe_print_backtrace(alloc);
assert(false && "index beyond allocation_list");
}
if (_allocation_list[alloc->index] != alloc) {
printf("free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
(uintptr_t) get_data(alloc), alloc->tag);
maybe_print_backtrace(alloc);
assert(false && "not in allocation_list");
}
else {
Expand Down
2 changes: 2 additions & 0 deletions src/rt/memory_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class memory_region {
void release_alloc(void *mem);
void claim_alloc(void *mem);

void maybe_print_backtrace(const alloc_header *) const;

private:
// private and undefined to disable copying
memory_region(const memory_region& rhs);
Expand Down