From patchwork Thu Jul 4 18:50:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 1956995 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Xs4qBu5S; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WFQjM4d6Gz1xqb for ; Fri, 5 Jul 2024 04:51:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6456E384A419 for ; Thu, 4 Jul 2024 18:51:30 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 58C9A3861029 for ; Thu, 4 Jul 2024 18:50:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58C9A3861029 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 58C9A3861029 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720119052; cv=none; b=bvZ5+1XwCt2G3H5/VcxxAuCm4XKoqG7/Knw8htkhf5JuBVKMxxqHqvqHgHHed4CBB8v4UOoZE9KQAIzPnONgVYYpwkhZDr5X+TdMxk1D/QYnCCsU4z8FVy3I4AdxnxdiNUSAMmNRTkhw8MBvkIOZSPk2z2vSlFI15x9fuDSrekU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1720119052; c=relaxed/simple; bh=03VjlxOjRltcrPg43U8+LU87lmbgV/kj0i90R5owwOs=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=FGQ56YANyXWLYudVbGA2cOshHdU7SkDL5W1UY2ND1W3cQuVjf7R6Fz8RvlFt/O9ewDIMwn0X74L1j2MB1B+4QiRbWdNlhK0yttSBaikRukSqujN4GwepTeDH2AHDeDX05cahT798PAeg8UVCHs/opi60eOKqlGA6aYw9cFRTd9g= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1720119049; 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: content-transfer-encoding:content-transfer-encoding; bh=98ArCDpbzES0J/qybkyiVN44ViU4yA3vv452Z8QpOA0=; b=Xs4qBu5S2SWhyXrOV55Q9r+JHlqeaZd2DzAdEFWp7ST7LcdH/6mIMxLyHM0YSsvl8xKM7m D3MerX3ci/AWpBXz/q+BGYuYphX0YKzOxMSRifkvYo8TDL7GnW3eVIZ0/keLKQsxfkV+iY wO0NwE4r5i7KOHKzd2vxt7M1vk2RW7Y= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-478-yV9nN4GHPTW_NSGTWoB-LQ-1; Thu, 04 Jul 2024 14:50:48 -0400 X-MC-Unique: yV9nN4GHPTW_NSGTWoB-LQ-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6182A1956088 for ; Thu, 4 Jul 2024 18:50:47 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.16.122]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 991623000180; Thu, 4 Jul 2024 18:50:44 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: handle at -O0 [PR115724] Date: Thu, 4 Jul 2024 14:50:42 -0400 Message-Id: <20240704185042.1954130-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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_H4, RCVD_IN_MSPIKE_WL, 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.30 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 At -O0, glibc's: __extern_always_inline void error (int __status, int __errnum, const char *__format, ...) { if (__builtin_constant_p (__status) && __status != 0) __error_noreturn (__status, __errnum, __format, __builtin_va_arg_pack ()); else __error_alias (__status, __errnum, __format, __builtin_va_arg_pack ()); } becomes just: __extern_always_inline void error (int __status, int __errnum, const char *__format, ...) { if (0) __error_noreturn (__status, __errnum, __format, __builtin_va_arg_pack ()); else __error_alias (__status, __errnum, __format, __builtin_va_arg_pack ()); } and thus calls to "error" are calls to "__error_alias" by the time -fanalyzer "sees" them. Handle them with more special-casing in kf.cc. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r15-1845-ga6fdb1a2a29061. gcc/analyzer/ChangeLog: PR analyzer/115724 * kf.cc (register_known_functions): Add __error_alias and __error_at_line_alias. gcc/testsuite/ChangeLog: PR analyzer/115724 * c-c++-common/analyzer/error-pr115724.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/kf.cc | 4 + .../c-c++-common/analyzer/error-pr115724.c | 86 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 gcc/testsuite/c-c++-common/analyzer/error-pr115724.c diff --git a/gcc/analyzer/kf.cc b/gcc/analyzer/kf.cc index 4213b89ac9fb..5c3a71fbb49c 100644 --- a/gcc/analyzer/kf.cc +++ b/gcc/analyzer/kf.cc @@ -2325,6 +2325,10 @@ register_known_functions (known_function_manager &kfm, kfm.add ("__errno_location", make_unique ()); kfm.add ("error", make_unique (3)); kfm.add ("error_at_line", make_unique (5)); + /* Variants of "error" and "error_at_line" seen by the + analyzer at -O0 (PR analyzer/115724). */ + kfm.add ("__error_alias", make_unique (3)); + kfm.add ("__error_at_line_alias", make_unique (5)); } /* Other implementations of C standard library. */ diff --git a/gcc/testsuite/c-c++-common/analyzer/error-pr115724.c b/gcc/testsuite/c-c++-common/analyzer/error-pr115724.c new file mode 100644 index 000000000000..ae606ad89d6a --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/error-pr115724.c @@ -0,0 +1,86 @@ +/* Verify that the analyzer handles the no-optimization case in + glibc's when error,error_at_line calls become + __error_alias and __error_at_line_alias. */ + +typedef __SIZE_TYPE__ size_t; +#define EXIT_FAILURE 1 +#define __extern_always_inline extern inline __attribute__ ((__always_inline__)) __attribute__ ((__gnu_inline__)) + +int errno; + +/* Adapted from glibc's bits/error.h. */ + +extern void __error_alias (int __status, int __errnum, + const char *__format, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); +extern void __error_noreturn (int __status, int __errnum, + const char *__format, ...) + __attribute__ ((__noreturn__, __format__ (__printf__, 3, 4))); + +/* If we know the function will never return make sure the compiler + realizes that, too. */ +__extern_always_inline void +error (int __status, int __errnum, const char *__format, ...) +{ + if (__builtin_constant_p (__status) && __status != 0) + __error_noreturn (__status, __errnum, __format, __builtin_va_arg_pack ()); + else + __error_alias (__status, __errnum, __format, __builtin_va_arg_pack ()); +} + +extern void __error_at_line_alias (int __status, int __errnum, + const char *__fname, + unsigned int __line, + const char *__format, ...) + __attribute__ ((__format__ (__printf__, 5, 6))); +extern void __error_at_line_noreturn (int __status, int __errnum, + const char *__fname, + unsigned int __line, + const char *__format, + ...) + __attribute__ ((__noreturn__, __format__ (__printf__, 5, 6))); + +/* If we know the function will never return make sure the compiler + realizes that, too. */ +__extern_always_inline void +error_at_line (int __status, int __errnum, const char *__fname, + unsigned int __line, const char *__format, ...) +{ + if (__builtin_constant_p (__status) && __status != 0) + __error_at_line_noreturn (__status, __errnum, __fname, __line, __format, + __builtin_va_arg_pack ()); + else + __error_at_line_alias (__status, __errnum, __fname, __line, + __format, __builtin_va_arg_pack ()); +} + + +struct list { + size_t size; + void (*destroy)(void *data); + struct list_node *head; + struct list_node *tail; +}; + +struct list *list_create(void (*destroy)(void *data)) +{ + struct list *result = (struct list *)__builtin_calloc(1, sizeof(*result)); + if (!result) + error(EXIT_FAILURE,errno,"%s:%d %s()",__FILE__,__LINE__,__func__); + + result->destroy = destroy; /* { dg-bogus "dereference of NULL 'result'" } */ + + return result; +} + +struct list *list_create_using_at_line(void (*destroy)(void *data)) +{ + struct list *result = (struct list *)__builtin_calloc(1, sizeof(*result)); + if (!result) + error_at_line(EXIT_FAILURE,errno,__FILE__,__LINE__, + "%s()", __func__); + + result->destroy = destroy; /* { dg-bogus "dereference of NULL 'result'" } */ + + return result; +}