public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: David Malcolm <dmalcolm@redhat.com>
Cc: gcc-patches@gcc.gnu.org, Joseph Myers <joseph@codesourcery.com>
Subject: Re: [PATCH] c: fix ICE with -fanalyzer and -Wunused-macros [PR107711]
Date: Thu, 17 Nov 2022 10:30:30 -0500	[thread overview]
Message-ID: <Y3ZTltw2gtgSvQOC@redhat.com> (raw)
In-Reply-To: <20221117030530.2995977-1-dmalcolm@redhat.com>

On Wed, Nov 16, 2022 at 10:05:30PM -0500, David Malcolm wrote:
> PR analyzer/107711 reports an ICE since r13-4073-gd8aba860b34203 with
> the combination of -fanalyzer and -Wunused-macros.
> 
> The issue is that in c_translation_unit::consider_macro's call to
> cpp_create_reader I was passing "ident_hash" for use by the the new
> reader, but that takes ownership of that hash_table, so that ident_hash
> erroneously gets freed when c_translation_unit::consider_macro calls
> cpp_destroy, leading to a use-after-free in -Wunused-macros, where:
> 
> (gdb) p pfile->hash_table->pfile == pfile
> $23 = false
> 
> and it's instead pointing at the freed reader from consider_macro,
> leading to a use-after-free ICE.

Ah, sneaky.

> Fixed thusly.
> 
> Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
> 
> OK for trunk?
 
OK.

> Thanks
> Dave
> 
> 
> gcc/c/ChangeLog:
> 	PR analyzer/107711
> 	* c-parser.cc (ana::c_translation_unit::consider_macro): Pass NULL
> 	to cpp_create_reader, rather than ident_hash, so that the new
> 	reader gets its own hash table.
> 
> gcc/testsuite/ChangeLog:
> 	PR analyzer/107711
> 	* gcc.dg/analyzer/named-constants-Wunused-macros.c: New test.
> 
> Signed-off-by: David Malcolm <dmalcolm@redhat.com>
> ---
>  gcc/c/c-parser.cc                             |  2 +-
>  .../analyzer/named-constants-Wunused-macros.c | 19 +++++++++++++++++++
>  2 files changed, 20 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.dg/analyzer/named-constants-Wunused-macros.c
> 
> diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
> index f3c79996fb0..1bbb39f9b08 100644
> --- a/gcc/c/c-parser.cc
> +++ b/gcc/c/c-parser.cc
> @@ -1717,7 +1717,7 @@ private:
>        return NULL_TREE;
>  
>      cpp_reader *old_parse_in = parse_in;
> -    parse_in = cpp_create_reader (CLK_GNUC89, ident_hash, line_table);
> +    parse_in = cpp_create_reader (CLK_GNUC89, NULL, line_table);
>  
>      pretty_printer pp;
>      pp_string (&pp, (const char *) tok.val.str.text);
> diff --git a/gcc/testsuite/gcc.dg/analyzer/named-constants-Wunused-macros.c b/gcc/testsuite/gcc.dg/analyzer/named-constants-Wunused-macros.c
> new file mode 100644
> index 00000000000..0b31cc42d78
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/analyzer/named-constants-Wunused-macros.c
> @@ -0,0 +1,19 @@
> +/* Regression test for interaction of named constants in -fanalyzer with
> +   -Wunused-macros (PR analyzer/107711).  */
> +
> +/* { dg-additional-options "-Wunused-macros" } */
> +
> +#include "analyzer-decls.h"
> +
> +/* Various constants used by the fd state machine.  */
> +
> +#define O_ACCMODE 42   /* { dg-warning "-: macro \"O_ACCMODE\" is not used" } */
> +#define O_RDONLY  0x1  /* { dg-warning "-: macro \"O_RDONLY\" is not used" } */
> +#define O_WRONLY  010  /* { dg-warning "-: macro \"O_WRONLY\" is not used" } */
> +
> +void test_sm_fd_constants (void)
> +{
> +  __analyzer_dump_named_constant ("O_ACCMODE"); /* { dg-warning "named constant 'O_ACCMODE' has value '42'" } */
> +  __analyzer_dump_named_constant ("O_RDONLY"); /* { dg-warning "named constant 'O_RDONLY' has value '1'" } */
> +  __analyzer_dump_named_constant ("O_WRONLY"); /* { dg-warning "named constant 'O_WRONLY' has value '8'" } */
> +}
> -- 
> 2.26.3
> 

Marek


      reply	other threads:[~2022-11-17 15:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-31 19:07 [PATCH] c, analyzer: support named constants in analyzer [PR106302] David Malcolm
2022-11-08  3:02 ` [PATCH] analyzer: add warnings relating to sockets [PR106140] David Malcolm
2022-11-12  3:27   ` [PATCH v2] " David Malcolm
2022-11-15 19:02     ` David Malcolm
2022-11-08  3:06 ` PING: Re: [PATCH] c, analyzer: support named constants in analyzer [PR106302] David Malcolm
2022-11-12  3:23   ` [PATCH v2] " David Malcolm
2022-11-12  3:32     ` David Malcolm
2022-11-14 20:42     ` Marek Polacek
2022-11-15 18:35       ` [PATCH v3] " David Malcolm
2022-11-15 18:39         ` Marek Polacek
2022-11-17  3:05           ` [PATCH] c: fix ICE with -fanalyzer and -Wunused-macros [PR107711] David Malcolm
2022-11-17 15:30             ` Marek Polacek [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=Y3ZTltw2gtgSvQOC@redhat.com \
    --to=polacek@redhat.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    /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).