From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 BC7B93AA8C05 for ; Thu, 17 Nov 2022 15:30:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BC7B93AA8C05 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1668699034; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=JJncn6ZHzWuI6VZ9SNJUpONJBDB8JBj8Q+oe4RYSVT4=; b=COgamT+xVcl8EbiBREnOILJ7q97UxjHWRWKINC97bhbVYISlr1z7rVscPzVasQQFQJB5sb HhFNmEduoSX6rnOo6recTG7H2ODsvL/hraaHQX2jBJVy/Mhvuc4Vyb4wxNccNnFVCi34Sl MysNiFj2tNL7EvF6WTUQ5RNIExgU7po= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-371-R8FTaH9fNYK4leEafPa0aQ-1; Thu, 17 Nov 2022 10:30:33 -0500 X-MC-Unique: R8FTaH9fNYK4leEafPa0aQ-1 Received: by mail-qk1-f198.google.com with SMTP id bs7-20020a05620a470700b006fac7447b1cso2468408qkb.17 for ; Thu, 17 Nov 2022 07:30:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JJncn6ZHzWuI6VZ9SNJUpONJBDB8JBj8Q+oe4RYSVT4=; b=7RaaIIOpMbKoqLpXmfCgeTgZfqyc3bBNB1LjEaMk4uAGmMx/3K7ehNuySxThuOH8Dr GBxNuONxBpFX4J0eEH+Nute7Xt68JAEMg8soeCs9fPtmtMnzU2M8K1jRZay6y/ndbPxL 7dGckoYbPsD7uZ1Q4tyZUDErBzQ8KZTFWfEt6hS6FPYCJixbjUY8MCB86EDlxWQKZGuU jZpmvswVmu7bhN1L3yXdhF+VZQKkF3B2r5nRLcMECvw2gGLmFae4PM0KVkJ+8suZKRT3 iCcsf7cxTt6+mNoIxmZth15tbz/5fq1RyIx8IiA6h3haN92pflRXfZGh4hrZG2pobS7B wBGg== X-Gm-Message-State: ANoB5pnfeVsGt9tPrfrhOCjrky7uzHYUsD6r0YQMlP1SCK0fuBFW722k 4PEKQf/cOz0HS2sQaCOG5Z2LElFFxAKbLD56c96MgNK9KepBWSOeP1uUSTnL33GxWV75TRwi8eX nhYbaK7bYjc4soRbGaw== X-Received: by 2002:a37:6c3:0:b0:6ef:131e:9c52 with SMTP id 186-20020a3706c3000000b006ef131e9c52mr2097240qkg.735.1668699032800; Thu, 17 Nov 2022 07:30:32 -0800 (PST) X-Google-Smtp-Source: AA0mqf6ZpbmKASrDftLikcxTRryM2os0h6oJU5B63rZlsnxSOba6GqQIZS9DiXL0iOEHMyAwnc0USQ== X-Received: by 2002:a37:6c3:0:b0:6ef:131e:9c52 with SMTP id 186-20020a3706c3000000b006ef131e9c52mr2097222qkg.735.1668699032568; Thu, 17 Nov 2022 07:30:32 -0800 (PST) Received: from redhat.com (2603-7000-9500-2e39-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:2e39::1db4]) by smtp.gmail.com with ESMTPSA id q7-20020a05620a0d8700b006f8665f483fsm621485qkl.85.2022.11.17.07.30.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 07:30:32 -0800 (PST) Date: Thu, 17 Nov 2022 10:30:30 -0500 From: Marek Polacek To: David Malcolm Cc: gcc-patches@gcc.gnu.org, Joseph Myers Subject: Re: [PATCH] c: fix ICE with -fanalyzer and -Wunused-macros [PR107711] Message-ID: References: <20221117030530.2995977-1-dmalcolm@redhat.com> MIME-Version: 1.0 In-Reply-To: <20221117030530.2995977-1-dmalcolm@redhat.com> User-Agent: Mutt/2.2.7 (2022-08-07) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.2 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 List-Id: 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 > --- > 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