public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libbacktrace/105240] backtrace_pcinfo leaks memory
Date: Tue, 12 Apr 2022 17:39:14 +0000	[thread overview]
Message-ID: <bug-105240-4-HZVrYLTL87@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-105240-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105240

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The report in comment 0 is from libstdc++ which uses a local static variable to
hold the state.

Modifying the comment 1 example to use a global:

#include <backtrace.h>

int cb_pcinfo(void*, uintptr_t, const char*, int, const char* function)
{ return function != nullptr; }

int cb(void* p, uintptr_t pc) { *static_cast<uintptr_t*>(p) = pc; return 1; }

backtrace_state* state;

int main()
{
  state = backtrace_create_state(nullptr, 1, nullptr, nullptr);
  uintptr_t pc;
  backtrace_simple(state, 0, cb, nullptr, &pc);
  backtrace_pcinfo(state, pc, cb_pcinfo, nullptr, nullptr);
}

I get:

==494586== Memcheck, a memory error detector
==494586== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==494586== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==494586== Command: ./a.out
==494586== 
==494586== 
==494586== HEAP SUMMARY:
==494586==     in use at exit: 112,638,181 bytes in 729,676 blocks
==494586==   total heap usage: 729,861 allocs, 185 frees, 115,412,694 bytes
allocated
==494586== 
==494586== 84 bytes in 3 blocks are definitely lost in loss record 6 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x409283: read_lnct (dwarf.c:2543)
==494586==    by 0x409283: read_line_header_format_entries (dwarf.c:2618)
==494586==    by 0x40B08A: read_line_header (dwarf.c:2712)
==494586==    by 0x40B08A: read_line_info (dwarf.c:2965)
==494586==    by 0x40B08A: dwarf_lookup_pc (dwarf.c:3747)
==494586==    by 0x40C1BE: dwarf_fileline (dwarf.c:3935)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 2,839 bytes in 1 blocks are possibly lost in loss record 19 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40667B: elf_add (elf.c:4381)
==494586==    by 0x406D83: backtrace_initialize (elf.c:4877)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 13,920 bytes in 1 blocks are definitely lost in loss record 23 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40568A: elf_get_view (elf.c:426)
==494586==    by 0x40568A: elf_add (elf.c:4329)
==494586==    by 0x406BC8: phdr_callback (elf.c:4848)
==494586==    by 0x4D2A314: dl_iterate_phdr (dl-iteratephdr.c:75)
==494586==    by 0x406DDD: backtrace_initialize (elf.c:4892)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 69,736 bytes in 2 blocks are possibly lost in loss record 26 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40568A: elf_get_view (elf.c:426)
==494586==    by 0x40568A: elf_add (elf.c:4329)
==494586==    by 0x406BC8: phdr_callback (elf.c:4848)
==494586==    by 0x4D2A314: dl_iterate_phdr (dl-iteratephdr.c:75)
==494586==    by 0x406DDD: backtrace_initialize (elf.c:4892)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== 451,020 bytes in 7 blocks are possibly lost in loss record 31 of 35
==494586==    at 0x484086F: malloc (vg_replace_malloc.c:381)
==494586==    by 0x406FF1: backtrace_alloc (alloc.c:57)
==494586==    by 0x406ED6: backtrace_get_view (read.c:68)
==494586==    by 0x40667B: elf_add (elf.c:4381)
==494586==    by 0x406BC8: phdr_callback (elf.c:4848)
==494586==    by 0x4D2A314: dl_iterate_phdr (dl-iteratephdr.c:75)
==494586==    by 0x406DDD: backtrace_initialize (elf.c:4892)
==494586==    by 0x4014B1: fileline_initialize (fileline.c:261)
==494586==    by 0x401581: backtrace_pcinfo (fileline.c:295)
==494586==    by 0x401357: main (bt.cc:15)
==494586== 
==494586== LEAK SUMMARY:
==494586==    definitely lost: 14,004 bytes in 4 blocks
==494586==    indirectly lost: 0 bytes in 0 blocks
==494586==      possibly lost: 523,595 bytes in 10 blocks
==494586==    still reachable: 112,100,582 bytes in 729,662 blocks
==494586==         suppressed: 0 bytes in 0 blocks
==494586== Reachable blocks (those to which a pointer was found) are not shown.
==494586== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==494586== 
==494586== For lists of detected and suppressed errors, rerun with: -s
==494586== ERROR SUMMARY: 5 errors from 5 contexts (suppressed: 0 from 0)

So that's still 14kB definitely lost, 500kB possibly lost.

  parent reply	other threads:[~2022-04-12 17:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-12 14:49 [Bug libbacktrace/105240] New: " redi at gcc dot gnu.org
2022-04-12 14:50 ` [Bug libbacktrace/105240] " redi at gcc dot gnu.org
2022-04-12 14:52 ` redi at gcc dot gnu.org
2022-04-12 16:31 ` ian at airs dot com
2022-04-12 17:39 ` redi at gcc dot gnu.org [this message]
2022-04-12 21:39 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105240-4-HZVrYLTL87@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).