From patchwork Wed Aug 10 00:03:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 458 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:6a10:20da:b0:2d3:3019:e567 with SMTP id n26csp2832517pxc; Tue, 9 Aug 2022 17:04:07 -0700 (PDT) X-Google-Smtp-Source: AA6agR7VTKJJRnGPx+o2k2XkpETmuu6MP1/sn2oMIOJdiRxODGaagfdfb4NT28ppReNgmN9sYGVg X-Received: by 2002:a17:907:72d1:b0:730:a0c4:2aaa with SMTP id du17-20020a17090772d100b00730a0c42aaamr18192359ejc.560.1660089847115; Tue, 09 Aug 2022 17:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660089847; cv=none; d=google.com; s=arc-20160816; b=WxuAqqj+LN12B8Ihx2swLPaYOSLmuvpw8PqeiCQX526QmLNQGRlNnfY2MzrwV11Lfm JpHSHm38iqyyI5OpERqhKlQdL6hEi/iFD3RPAVqzSd94ewrq4G+TDneDeP5yZzmPCo24 1VARQk6PEPnSwIg8lVoJ+7rihtvH/TGOkkRA6QMmh20K8lrKJz/bvttG1ZoTUZyLa2h3 WkguNkxZ+JoPY3tqYVz26j8yI9FdQjSb7XxITHKDW1LwwGxrF3VtvMEm8umpQGtJ3m/u mRmfJ+qKd9yewVnBSesQTLkEAxgzPevhenPEd4uaSmQ0BMnSgFdsN2FkFrLmX3YSA90A Bl6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ShPdpxuxePyUfbZVx2fuLFi9OKu0nveCySUc3ORpBSw=; b=icY3vZGJ0wVTwW/YkBzGdV7MI7cKzgQkSXm+KxC+awJCeraQhbUntNd6UOywNL1ZB+ apsajUmK56zKqnF51hQPyWJyP0RUSmLbIYfg0MgLkokorMWcXPAh+83yLWNTV5bukI8z dUsSRB/eyuBk1LYN1Mhtjgipvd+cu1ASvIS/+vwiZG8EbjJQU5dPl3jE6qr/06HzoJzj GME6mgeb5c5vekDIU5g+yZQn7r1ud/HKiKmJ5QAojTUs01JtcRmmbO48y6kUAu7RIr2a ld2TTy7Vy53KL4X1gTK1vZcRAnayII+M43IIBsf5Bxo0a3yfxpeJaRkDremUDB3g7t73 PK3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=DHuhbPI0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id m19-20020a056402431300b0043d2f04a703si11170165edc.40.2022.08.09.17.04.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Aug 2022 17:04:07 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=DHuhbPI0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id DD7923857C40 for ; Wed, 10 Aug 2022 00:04:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DD7923857C40 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660089845; bh=ShPdpxuxePyUfbZVx2fuLFi9OKu0nveCySUc3ORpBSw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=DHuhbPI0NJSUwSzd5FZ0l7r+gd8D+TDsylcgQnOCoVYVRrdSPbAnUmJdou1xdTwhQ 5n+tiFf71LjGql/o3IBPpW0fan7MasXMOAe6LnlHxMVG74qObUfm4ynSgzV83jQiiP G+2OoUZeyGNgV6gpNn2hJlfmkDUatS7qeQRfccaA= 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 67DC13858401 for ; Wed, 10 Aug 2022 00:03:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 67DC13858401 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-333-AWu_QmXgNjqhXzR5nWiwkg-1; Tue, 09 Aug 2022 20:03:19 -0400 X-MC-Unique: AWu_QmXgNjqhXzR5nWiwkg-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 8B2911C06EF7 for ; Wed, 10 Aug 2022 00:03:13 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.2.17.170]) by smtp.corp.redhat.com (Postfix) with ESMTP id DC7EA1121314; Wed, 10 Aug 2022 00:03:06 +0000 (UTC) To: gcc-patches@gcc.gnu.org Subject: [committed] analyzer: fix missing -Wanalyzer-use-of-uninitialized-value on special-cased functions [PR106573] Date: Tue, 9 Aug 2022 20:03:04 -0400 Message-Id: <20220810000304.3059351-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1740730371259648874?= X-GMAIL-MSGID: =?utf-8?q?1740730371259648874?= We were missing checks for uninitialized params on calls to functions that the analyzer has hardcoded knowledge of - both for those that are handled just by state machines, and for those that are handled in region-model-impl-calls.cc (for those arguments for which the svalue wasn't accessed in handling the call). Fixed thusly. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r13-2007-gbddd8d86e3036e. gcc/analyzer/ChangeLog: PR analyzer/106573 * region-model.cc (region_model::on_call_pre): Ensure that we call get_arg_svalue on all arguments. gcc/testsuite/ChangeLog: PR analyzer/106573 * gcc.dg/analyzer/error-uninit.c: New test. * gcc.dg/analyzer/fd-uninit-1.c: New test. * gcc.dg/analyzer/file-uninit-1.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 8 +++ gcc/testsuite/gcc.dg/analyzer/error-uninit.c | 29 +++++++++++ gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c | 21 ++++++++ gcc/testsuite/gcc.dg/analyzer/file-uninit-1.c | 52 +++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/analyzer/error-uninit.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c create mode 100644 gcc/testsuite/gcc.dg/analyzer/file-uninit-1.c diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index a140f4d5088..8393c7ddbf7 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -1355,6 +1355,14 @@ region_model::on_call_pre (const gcall *call, region_model_context *ctxt, && gimple_call_internal_fn (call) == IFN_DEFERRED_INIT) return false; + /* Get svalues for all of the arguments at the callsite, to ensure that we + complain about any uninitialized arguments. This might lead to + duplicates if any of the handling below also looks up the svalues, + but the deduplication code should deal with that. */ + if (ctxt) + for (unsigned arg_idx = 0; arg_idx < cd.num_args (); arg_idx++) + cd.get_arg_svalue (arg_idx); + /* Some of the cases below update the lhs of the call based on the return value, but not all. Provide a default value, which may get overwritten below. */ diff --git a/gcc/testsuite/gcc.dg/analyzer/error-uninit.c b/gcc/testsuite/gcc.dg/analyzer/error-uninit.c new file mode 100644 index 00000000000..8d52a177b11 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/error-uninit.c @@ -0,0 +1,29 @@ +/* Verify that we check for uninitialized values passed to functions + that we have special-cased region-model handling for. */ + +extern void error (int __status, int __errnum, const char *__format, ...) + __attribute__ ((__format__ (__printf__, 3, 4))); + +void test_uninit_status (int arg) +{ + int st; + error (st, 42, "test: %s", arg); /* { dg-warning "use of uninitialized value 'st'" } */ +} + +void test_uninit_errnum (int st) +{ + int num; + error (st, num, "test"); /* { dg-warning "use of uninitialized value 'num'" } */ +} + +void test_uninit_fmt (int st) +{ + const char *fmt; + error (st, 42, fmt); /* { dg-warning "use of uninitialized value 'fmt'" } */ +} + +void test_uninit_vargs (int st) +{ + int arg; + error (st, 42, "test: %s", arg); /* { dg-warning "use of uninitialized value 'arg'" } */ +} diff --git a/gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c b/gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c new file mode 100644 index 00000000000..b5b189ece98 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/fd-uninit-1.c @@ -0,0 +1,21 @@ +/* Verify that we check for uninitialized values passed to functions + that we have special-cased state-machine handling for. */ + +int dup (int old_fd); +int not_dup (int old_fd); + +int +test_1 () +{ + int m; + return dup (m); /* { dg-warning "use of uninitialized value 'm'" "uninit" } */ + /* { dg-bogus "'dup' on possibly invalid file descriptor 'm'" "invalid fd false +ve" { xfail *-*-* } .-1 } */ + /* XFAIL: probably covered by fix for PR analyzer/106551. */ +} + +int +test_2 () +{ + int m; + return not_dup (m); /* { dg-warning "use of uninitialized value 'm'" } */ +} diff --git a/gcc/testsuite/gcc.dg/analyzer/file-uninit-1.c b/gcc/testsuite/gcc.dg/analyzer/file-uninit-1.c new file mode 100644 index 00000000000..0f8ac5442b1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/analyzer/file-uninit-1.c @@ -0,0 +1,52 @@ +/* Verify that we check for uninitialized values passed to functions + that we have special-cased state-machine handling for. */ + +typedef struct FILE FILE; + +FILE* fopen (const char*, const char*); +int fclose (FILE*); +int fseek (FILE *, long, int); + +FILE * +test_fopen_uninit_path (void) +{ + const char *path; + FILE *f = fopen (path, "r"); /* { dg-warning "use of uninitialized value 'path'" } */ + return f; +} + +FILE * +test_fopen_uninit_mode (const char *path) +{ + const char *mode; + FILE *f = fopen (path, mode); /* { dg-warning "use of uninitialized value 'mode'" } */ + return f; +} + +void +test_fclose_uninit (void) +{ + FILE *f; + fclose (f); /* { dg-warning "use of uninitialized value 'f'" } */ +} + +int +test_fseek_uninit_stream (void) +{ + FILE *stream; + return fseek (stream, 0, 0); /* { dg-warning "use of uninitialized value 'stream'" } */ +} + +int +test_fseek_uninit_offset (FILE *stream, int whence) +{ + long offset; + return fseek (stream, offset, whence); /* { dg-warning "use of uninitialized value 'offset'" } */ +} + +int +test_fseek_uninit_whence (FILE *stream, long offset) +{ + int whence; + return fseek (stream, offset, whence); /* { dg-warning "use of uninitialized value 'whence'" } */ +}