From patchwork Tue Nov 22 22:36:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 60992 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 C3BA538518A3 for ; Tue, 22 Nov 2022 22:37:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C3BA538518A3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669156650; bh=p/3vIIHvOX8bpJFc/AqUq0wXVm8RDNev1uCbPoEHjYA=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iuu5rzGJraZDGnUmNLuHTNaKy8Klq13G07qWOBSWEKQrWaYh/x+tOh+lQWIjY5zT2 CGQDZNATgH/bXzN++1rOtBj3DtclPjiMtv3mHbXlq6L0sPLPGjMk6DEGnvImnxsmxD HMZfvo14AkU8akyzXjA2aesqn0zsk8498NQD8Ko8= 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 9D7183858C3A for ; Tue, 22 Nov 2022 22:36:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9D7183858C3A Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-658-GIioqg55NZe5QerAW6dOzA-1; Tue, 22 Nov 2022 17:36:53 -0500 X-MC-Unique: GIioqg55NZe5QerAW6dOzA-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D363C29ABA2A for ; Tue, 22 Nov 2022 22:36:52 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.16.65]) by smtp.corp.redhat.com (Postfix) with ESMTP id AF759112132D; Tue, 22 Nov 2022 22:36:52 +0000 (UTC) To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [committed] analyzer: fix 'errno' on Solaris and OS X [PR107807] Date: Tue, 22 Nov 2022 17:36:49 -0500 Message-Id: <20221122223649.3308793-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 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" Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-4247-g7c9717fcb5cf94. gcc/analyzer/ChangeLog: PR analyzer/107807 * region-model-impl-calls.cc (register_known_functions): Register "___errno" and "__error" as synonyms for "__errno_location". gcc/testsuite/ChangeLog: PR analyzer/107807 * gcc.dg/analyzer/errno-___errno.c: New test. * gcc.dg/analyzer/errno-__error.c: New test. * gcc.dg/analyzer/errno-global-var.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/region-model-impl-calls.cc | 14 +++++++++ .../gcc.dg/analyzer/errno-___errno.c | 29 +++++++++++++++++++ gcc/testsuite/gcc.dg/analyzer/errno-__error.c | 28 ++++++++++++++++++ .../gcc.dg/analyzer/errno-global-var.c | 26 +++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/errno-___errno.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/errno-__error.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/errno-global-var.c diff --git a/gcc/analyzer/region-model-impl-calls.cc b/gcc/analyzer/region-model-impl-calls.cc index 6962ffd400f..23a21d752cf 100644 --- a/gcc/analyzer/region-model-impl-calls.cc +++ b/gcc/analyzer/region-model-impl-calls.cc @@ -1953,6 +1953,20 @@ register_known_functions (known_function_manager &kfm) kfm.add ("error_at_line", make_unique (5)); } + /* Other implementations of C standard library. */ + { + /* According to PR 107807 comment #2, Solaris implements "errno" + like this: + extern int *___errno(void) __attribute__((__const__)); + #define errno (*(___errno())) + and OS X like this: + extern int * __error(void); + #define errno (*__error()) + Add these as synonyms for "__errno_location". */ + kfm.add ("___errno", make_unique ()); + kfm.add ("__error", make_unique ()); + } + /* C++ support functions. */ { kfm.add ("operator new", make_unique ()); diff --git a/gcc/testsuite/gcc.dg/analyzer/errno-___errno.c b/gcc/testsuite/gcc.dg/analyzer/errno-___errno.c new file mode 100644 index 00000000000..17ff8b7de9d --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/errno-___errno.c @@ -0,0 +1,29 @@ +#include "analyzer-decls.h" + +/* According to PR 107807 comment #2, Solaris implements "errno" + like this: */ + +extern int *___errno(void) __attribute__((__const__)); +#define errno (*(___errno())) + + +extern void external_fn (void); + +int test_reading_errno (void) +{ + return errno; +} + +void test_setting_errno (int val) +{ + errno = val; +} + +void test_storing_to_errno (int val) +{ + __analyzer_eval (errno == val); /* { dg-warning "UNKNOWN" } */ + errno = val; + __analyzer_eval (errno == val); /* { dg-warning "TRUE" } */ + external_fn (); + __analyzer_eval (errno == val); /* { dg-warning "UNKNOWN" } */ +} diff --git a/gcc/testsuite/gcc.dg/analyzer/errno-__error.c b/gcc/testsuite/gcc.dg/analyzer/errno-__error.c new file mode 100644 index 00000000000..19bc4f937f6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/errno-__error.c @@ -0,0 +1,28 @@ +#include "analyzer-decls.h" + +/* According to PR 107807 comment #2, OS X implements "errno" + like this: */ + +extern int * __error(void); +#define errno (*__error()) + +extern void external_fn (void); + +int test_reading_errno (void) +{ + return errno; +} + +void test_setting_errno (int val) +{ + errno = val; +} + +void test_storing_to_errno (int val) +{ + __analyzer_eval (errno == val); /* { dg-warning "UNKNOWN" } */ + errno = val; + __analyzer_eval (errno == val); /* { dg-warning "TRUE" } */ + external_fn (); + __analyzer_eval (errno == val); /* { dg-warning "UNKNOWN" } */ +} diff --git a/gcc/testsuite/gcc.dg/analyzer/errno-global-var.c b/gcc/testsuite/gcc.dg/analyzer/errno-global-var.c new file mode 100644 index 00000000000..fdf1b17cecc --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/errno-global-var.c @@ -0,0 +1,26 @@ +#include "analyzer-decls.h" + +/* "errno" declared as a global var. */ + +extern int errno; + +extern void external_fn (void); + +int test_reading_errno (void) +{ + return errno; +} + +void test_setting_errno (int val) +{ + errno = val; +} + +void test_storing_to_errno (int val) +{ + __analyzer_eval (errno == val); /* { dg-warning "UNKNOWN" } */ + errno = val; + __analyzer_eval (errno == val); /* { dg-warning "TRUE" } */ + external_fn (); + __analyzer_eval (errno == val); /* { dg-warning "UNKNOWN" } */ +}