From patchwork Wed Jul 28 05:04:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 1510727 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=gotplt.org header.i=@gotplt.org header.a=rsa-sha1 header.s=gotplt.org header.b=iMSJPYBc; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GZM6y1Q7mz9sPf for ; Wed, 28 Jul 2021 15:05:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DE6B73985808 for ; Wed, 28 Jul 2021 05:05:22 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from olivedrab.birch.relay.mailchannels.net (olivedrab.birch.relay.mailchannels.net [23.83.209.135]) by sourceware.org (Postfix) with ESMTPS id 268B4383D03A for ; Wed, 28 Jul 2021 05:04:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 268B4383D03A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 7B3FB321439; Wed, 28 Jul 2021 05:04:56 +0000 (UTC) Received: from pdx1-sub0-mail-a75.g.dreamhost.com (100-101-162-68.trex.outbound.svc.cluster.local [100.101.162.68]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 1DAA4320F78; Wed, 28 Jul 2021 05:04:56 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a75.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.101.162.68 (trex/6.3.3); Wed, 28 Jul 2021 05:04:56 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Daffy-Wide-Eyed: 587c906b31f4b948_1627448696334_3101237957 X-MC-Loop-Signature: 1627448696334:1169479021 X-MC-Ingress-Time: 1627448696334 Received: from pdx1-sub0-mail-a75.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a75.g.dreamhost.com (Postfix) with ESMTP id D86668C383; Tue, 27 Jul 2021 22:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gotplt.org; h=from:to:cc :subject:date:message-id:mime-version:content-transfer-encoding; s=gotplt.org; bh=S9RmH/49jdCS6OCcQEie6sWf+VA=; b=iMSJPYBcdocSkB q7yKUfUVG2dpR0v1VmcwjHjANy+6VbTaey0u7SUw6419FxX0wvgR/aLQ58Gu0l7E zwMxPJTOGPQf4bnWtYcl4pm8noYNfEhv6aVEa6FYPKAsj8ThhRDrY82TitUaSJ5g rNkkcmAvfLDG61OC2/HbAPn9XonRw= Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a75.g.dreamhost.com (Postfix) with ESMTPSA id 47D748C382; Tue, 27 Jul 2021 22:04:52 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a75 From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Subject: [PATCH] analyzer: Recognize __builtin_free as a matching deallocator Date: Wed, 28 Jul 2021 10:34:44 +0530 Message-Id: <20210728050444.3843432-1-siddhesh@gotplt.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3037.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Recognize __builtin_free as being equivalent to free when passed into __attribute__((malloc ())), similar to how it is treated when it is encountered as a call. This fixes spurious warnings in glibc where xmalloc family of allocators as well as reallocarray, memalign, etc. are declared to have __builtin_free as the free function. gcc/analyzer/ChangeLog: * sm-malloc.cc (malloc_state_machine::get_or_create_deallocator): Recognize __builtin_free. gcc/testsuite/ChangeLog: * gcc.dg/analyzer/attr-malloc-1.c (compatible_alloc, compatible_alloc2): New extern allocator declarations. (test_9, test_10): New tests. --- gcc/analyzer/sm-malloc.cc | 3 ++- gcc/testsuite/gcc.dg/analyzer/attr-malloc-1.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/gcc/analyzer/sm-malloc.cc b/gcc/analyzer/sm-malloc.cc index 9707a6863ce..1d69d57df0e 100644 --- a/gcc/analyzer/sm-malloc.cc +++ b/gcc/analyzer/sm-malloc.cc @@ -1511,7 +1511,8 @@ malloc_state_machine::get_or_create_deallocator (tree deallocator_fndecl) /* Reuse "free". */ deallocator *d; if (is_named_call_p (deallocator_fndecl, "free") - || is_std_named_call_p (deallocator_fndecl, "free")) + || is_std_named_call_p (deallocator_fndecl, "free") + || is_named_call_p (deallocator_fndecl, "__builtin_free")) d = &m_free.m_deallocator; else { diff --git a/gcc/testsuite/gcc.dg/analyzer/attr-malloc-1.c b/gcc/testsuite/gcc.dg/analyzer/attr-malloc-1.c index 3de32b1b14b..a9a2a3dee85 100644 --- a/gcc/testsuite/gcc.dg/analyzer/attr-malloc-1.c +++ b/gcc/testsuite/gcc.dg/analyzer/attr-malloc-1.c @@ -11,6 +11,12 @@ extern struct foo *foo_acquire (void) extern void use_foo (const struct foo *) __attribute__((nonnull)); +extern struct foo *compatible_alloc (void) + __attribute__ ((malloc (__builtin_free))); + +extern struct foo *compatible_alloc2 (void) + __attribute__ ((malloc (free))); + void test_1 (void) { struct foo *p = foo_acquire (); @@ -73,3 +79,16 @@ int test_8 (struct foo *p) foo_release (p); return p->m_int; /* { dg-warning "use after 'foo_release' of 'p'" } */ } + +/* Recognize that __builtin_free and free are the same thing. */ +void test_9 (void) +{ + struct foo *p = compatible_alloc (); + free (p); +} + +void test_10 (void) +{ + struct foo *p = compatible_alloc2 (); + __builtin_free (p); +}