public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rguenth at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/110501] [13/14 regression] Invalid -Wuse-after-free / realloc with a store/load happening
Date: Thu, 04 Apr 2024 07:39:48 +0000	[thread overview]
Message-ID: <bug-110501-4-6oKn8jBdE0@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110501-4@http.gcc.gnu.org/bugzilla/>

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.3

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to John Baldwin from comment #7)
> I believe I am hitting this same issue using GCC 13 to compile FreeBSD's
> userland. (I looked through several of the other bugs for the
> Wuse-after-free metabug and this one seems the closest to what I'm
> encountering).  The code in question does not trigger the -Wuse-after-free
> warning when using GCC 12, but now triggers the warning with GCC 13.  It is
> just calling free on the old value after realloc fails which should be safe.
> The simplest version I've encountered so far in FreeBSD's tree is this:
> 
> static int
> group_resize(void)
> {
> 	char *buf;
> 
> 	if (gbufsize == 0)
> 		gbufsize = 1024;
> 	else
> 		gbufsize *= 2;
> 
> 	buf = gbuffer;
> 	gbuffer = realloc(buf, gbufsize);
> 	if (gbuffer == NULL) {
> 		free(buf);

the problem with this is that 'gbuffer' is a global and we diagnose this
early before applying memory CSE so we don't see the 'gbuffer' being
stored and 'gbuffer' being tested against NULL as equal.

IIRC with GCC 13 we moved some diagnostics early from late as late has
issues with jump threading exposing very many false positives.

Testcase (which I believe we have a duplicate for):

void *gbuffer;
unsigned long gbufsize;
void foo()
{
  char *buf = gbuffer;
  gbuffer = __builtin_realloc(buf, gbufsize);
  if (!gbuffer) {
      __builtin_free(buf);
      return;
  }
  __builtin_memset (gbuffer, 0, gbufsize);
}

I'll note that for the early -Wuninitialized I added cheap value numbering
to prune unreachable code paths.  The plan was to wrap all of the early
diagnostics with such cheap VN _analysis_ (but not do any code changes)
which would allow these cases to be identified as equal.  The diagnostic
passes would need to apply some valueization then of course.

  parent reply	other threads:[~2024-04-04  7:39 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
2023-06-30 15:11 ` [Bug analyzer/110501] " cheyenne.wills at gmail dot com
2023-06-30 16:10 ` [Bug tree-optimization/110501] Invalid use-after-free / realloc at -O0 pinskia at gcc dot gnu.org
2023-06-30 16:12 ` [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening pinskia at gcc dot gnu.org
2023-06-30 16:16 ` cheyenne.wills at gmail dot com
2023-07-03  6:24 ` rguenth at gcc dot gnu.org
2023-07-06 20:49 ` cheyenne.wills at gmail dot com
2023-10-31 16:18 ` jhb at FreeBSD dot org
2024-03-10 14:03 ` [Bug tree-optimization/110501] [13/14 regression] Invalid -Wuse-after-free " sjames at gcc dot gnu.org
2024-04-04  7:39 ` rguenth at gcc dot gnu.org [this message]
2024-04-04  7:39 ` rguenth at gcc dot gnu.org
2024-05-21  9:16 ` [Bug tree-optimization/110501] [13/14/15 " jakub 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-110501-4-6oKn8jBdE0@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).