Dwarves Archive on lore.kernel.org
 help / color / Atom feed
From: Arnaldo Carvalho de Melo <acme-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
To: Tavis Ormandy <taviso-1TlbntoI6+xF6kxbq+BtvQ@public.gmane.org>
Cc: dwarves-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: libdwarves memory leaks in the stealer interface
Date: Thu, 14 Jul 2016 21:04:18 -0300
Message-ID: <20160715000418.GB3021@kernel.org> (raw)
In-Reply-To: <CABD==127dOrixY6KaHKBnycQV_+h1aMXPgT8h67Tkfka6GboGQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

Em Thu, Jul 14, 2016 at 12:58:07PM -0700, Tavis Ormandy escreveu:
> Hello, I have a fun project that I'm trying to use libdwarves with,
> but had a couple of questions.
> 
> I'm using the "steal" callback interface to search for a specific cu.
> If the cu matches the one I was looking for, I process it and then
> return LSK__STOP_LOADING to abort processing this file. If it's not
> the cu I wanted, I just return LSK__DELETE.
> 
> This seems to do what I want, but leaks memory.
> 
> 1) LSK__DELETE doesn't seem to clean up the dwarf_cu obstack, I think
> this wasn't intended.
> 2) If die__process_and_recode() fails in finalize_cu_immediately(),
> the cu is leaked. This one is definitely a bug :)
> 
> I'm not 100% sure if I'm using LSK__STOP_LOADING correctly. It doesn't
> cleanup the cu like LSK__DELETE does, and it doesn't call cus__add()
> like LSK__KEEPIT does, so was the stealer supposed to clean it up?
> 
> If so, how do I clean up the dcu? I think I can pull it out of
> cu->priv, but I think you didn't intend to expose that to callbacks?
> 
> The patch I'm using is below.

At first sight it is ok, i.e. it seems to fix the problem, but I think
the right place to do the free will be at:

  cu__delete()
     cu->dfops->cu__delete(cu);

Which for DWARF, maps to... nothing, I'll continue looking at this
tomorrow, its been a while I haven't touched this code :-\
 
> Thanks for libdwarves, it's a really great library!

Glad you liked it!
 
> Tavis.
> 
> diff --git a/src/struct/dwarf_loader.c b/src/struct/dwarf_loader.c
> index 163cb51..ba715a9 100644
> --- a/src/struct/dwarf_loader.c
> +++ b/src/struct/dwarf_loader.c
> @@ -2169,9 +2169,12 @@ static int finalize_cu_immediately(struct cus
> *cus, struct cu *cu,
>   int lsk = finalize_cu(cus, cu, dcu, conf);
>   switch (lsk) {
>   case LSK__DELETE:
> + obstack_free(&dcu->obstack, NULL);
>   cu__delete(cu);
>   break;
>   case LSK__STOP_LOADING:
> + obstack_free(&dcu->obstack, NULL);
> + cu__delete(cu);
>   break;
>   case LSK__KEEPIT:
>   if (!cu->extra_dbg_info)
> @@ -2304,8 +2307,11 @@ static int cus__load_module(struct cus *cus,
> struct conf_load *conf,
>   cu->priv = &dcu;
>   cu->dfops = &dwarf__ops;
> 
> - if (die__process_and_recode(cu_die, cu) != 0)
> + if (die__process_and_recode(cu_die, cu) != 0) {
> + obstack_free(&dcu.obstack, NULL);
> + cu__delete(cu);
>   return DWARF_CB_ABORT;
> + }
> 
>   if (finalize_cu_immediately(cus, cu, &dcu, conf)
>      == LSK__STOP_LOADING)
> --
> To unsubscribe from this list: send the line "unsubscribe dwarves" in
> the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe dwarves" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

      parent reply index

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-14 19:58 Tavis Ormandy
     [not found] ` <CABD==127dOrixY6KaHKBnycQV_+h1aMXPgT8h67Tkfka6GboGQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-07-15  0:04   ` Arnaldo Carvalho de Melo [this message]

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=20160715000418.GB3021@kernel.org \
    --to=acme-dgejt+ai2ygdnm+yrofe0a@public.gmane.org \
    --cc=dwarves-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=taviso-1TlbntoI6+xF6kxbq+BtvQ@public.gmane.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

Dwarves Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/dwarves/0 dwarves/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 dwarves dwarves/ https://lore.kernel.org/dwarves \
		dwarves@vger.kernel.org
	public-inbox-index dwarves

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.dwarves


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git