From patchwork Thu Nov 17 03:05:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 60729 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B941338A814A for ; Thu, 17 Nov 2022 03:06:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B941338A814A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668654368; bh=t2erIErg1jm6z204slC3W98XAt2U/w4hNL9g9ntDyuU=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=l4IZrgiuZyNH5cjrZZB3zZB5m7FPg+7kCApplp5VAxYqrzX8r2vj9wtP2jUgX6Sd6 JR/mBWrTK0A2BFmP+vuO07EUbv7osYLckeR3CXkHkQOQpoVR7zNh000qS0a+BY6DqD k8Hlu9p1Rr37cwyAk7OQcYsT94sX+2Eu1mZDMdu8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0524D3832371 for ; Thu, 17 Nov 2022 03:05:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0524D3832371 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-435-kE4f0wzxN9SiUeQcBTECKQ-1; Wed, 16 Nov 2022 22:05:35 -0500 X-MC-Unique: kE4f0wzxN9SiUeQcBTECKQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CA3A0800B23; Thu, 17 Nov 2022 03:05:34 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.189]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4640F40C6EC3; Thu, 17 Nov 2022 03:05:33 +0000 (UTC) To: gcc-patches@gcc.gnu.org, Marek Polacek Cc: Joseph Myers , David Malcolm Subject: [PATCH] c: fix ICE with -fanalyzer and -Wunused-macros [PR107711] Date: Wed, 16 Nov 2022 22:05:30 -0500 Message-Id: <20221117030530.2995977-1-dmalcolm@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: David Malcolm via Gcc-patches From: David Malcolm Reply-To: David Malcolm Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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. Fixed thusly. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. OK for trunk? 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 --- 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'" } */ +}