From patchwork Tue Jan 16 00:05:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 188351 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2614:b0:101:6a76:bbe3 with SMTP id mm20csp2011117dyc; Mon, 15 Jan 2024 16:06:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IGdNOCYPL3I7Wi1sWncHW5ClMzz36HoI4bRfU1S85qPenGcquGJD5nl5D8C0f9q6QzUcqfC X-Received: by 2002:ad4:5d6a:0:b0:680:b48c:b54b with SMTP id fn10-20020ad45d6a000000b00680b48cb54bmr8906417qvb.102.1705363601918; Mon, 15 Jan 2024 16:06:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705363601; cv=pass; d=google.com; s=arc-20160816; b=GHIjE5juOOVyA6/+Ov+gAlY2XeSZud9tUgoikpoGTs5Q0XYD79IlQhX4G7s7eBCEQs uywSGk4NVeX0qOaYZZ+MCccKRF0sR3sqdIAXzJfgPPCR/FioYDI27DexPdX0ULmJ5h1c M0eL/iGLrGXJfYF5xCFCHzBYkIj6RWKoKdQMWcwwqzzgh88GNt5KKupYD3hcirDpJ2aD lQlCBZByze9iU/slplsyiqL/dEdhhv4vKOgaSuSl84VkPdTOVKWVfTKt93dYLCMka/7Y NzYxp6peeMeW++BSqm8dC3M7q6ev7i3h+bSAzOj9vwfsibYpsS/dy2b+lcGq0L+dxKUi oezA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=a+clgiOjjZXmmTH08//s1A8gGzZMA7SMZQrLcYI6Fjk=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=nOMLyCF8h9e/YS9QMCVbXQrVk9qzU8W2JsAgVVfRFGcv7VmNWwSHHBBqfcbpj5Jy7/ W5Ax7LCq9ddIa6VP/BYcUtBchysIw5Jgp9W5FzKez1E37beFh8gJCt8vegZehnUcEK0H paPrkq1bFekkN/L8+6XKIVcZFmZx2D5E5JSsvBmuF6CyuFwdX/8rTY7gSHdhJoxa4Szo 7aHNICCq6H78ilEzRyvI0aDJ8PAz7DWftkJcepFOqXyUC1gxiSKV3vmkahoLsqGo19Zf 5xTKe2v9mwGaqsuCseCIj937uvS6ScDkrPrqbYTNPzt2aB+hYAIBHwOE2JTVYvC9QfqX nvHw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WZgoBOlW; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id p26-20020a05620a057a00b0078338894892si8715509qkp.571.2024.01.15.16.06.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jan 2024 16:06:41 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=WZgoBOlW; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A7CE3858D28 for ; Tue, 16 Jan 2024 00:06:41 +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 01D043858D1E for ; Tue, 16 Jan 2024 00:05:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 01D043858D1E 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 01D043858D1E 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=1705363546; cv=none; b=Ii7cHcCLrD11Zw5qLqN+x5BsAaqFckS8Ft1FChOpKw5Qe0Cj/DjnPYEw/I7QTadPTK2P9t2E+Ow/gwaN8j5Fhg1rXpkiA8UunmP1nKQCcd1JQHR8G9zG/Vr6mJISNoGdf4BYPdS+UPcgTSk+6zp7tA+WrUML3FS4JoZMJTxxEWg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705363546; c=relaxed/simple; bh=MOMZJmmtp6GIWM/qb0z5v3/ooh2FqvNbevd1XK6qD+g=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=RAVf0qRgOjTvp19wPHarXtcQPyUnkznOqhNVtHZZuENar0XM6qgSTf7o64RgYV01Zrdi3qz7nZqv2MLqVjqrbIfoBoWhOiQtsjEHPQmUWLtjHF2CAW+TpoJEUz5596wietBtlr6HeoPYBc/ATlxr2WX8DbavUfal4gfoX2sP7P0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705363543; 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=a+clgiOjjZXmmTH08//s1A8gGzZMA7SMZQrLcYI6Fjk=; b=WZgoBOlWnzTTbugOMBS/G5+F/EJ/QUwySDx+BaIn3fBUDSeHuC6H27Sq1cbUy/KDtCY89I U6RBc/+4W5VeJ14zEW21aD45riSm5tWdxEDx2yNW4qUWlZwdCs/21QgOAcwUQm6pnxmRse B5aGQ0NzoA8wZHl5jxFU7D2W/Xs2Gik= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-510-fmAylgBmMOqJggFb2qTZPA-1; Mon, 15 Jan 2024 19:05:42 -0500 X-MC-Unique: fmAylgBmMOqJggFb2qTZPA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0C3ED102587F for ; Tue, 16 Jan 2024 00:05:42 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.16.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id D6E102026D6F; Tue, 16 Jan 2024 00:05:41 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: casting all zeroes should give all zeroes [PR113333] Date: Mon, 15 Jan 2024 19:05:40 -0500 Message-Id: <20240116000540.1054362-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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_H3, RCVD_IN_MSPIKE_WL, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788203343744881502 X-GMAIL-MSGID: 1788203343744881502 In particular, accessing the result of *calloc (1, SZ) (if non-NULL) should be known to be all zeroes. 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 r14-7265-gd235bf2e807c5f. gcc/analyzer/ChangeLog: PR analyzer/113333 * region-model-manager.cc (region_model_manager::maybe_fold_unaryop): Casting all zeroes should give all zeroes. gcc/testsuite/ChangeLog: PR analyzer/113333 * c-c++-common/analyzer/calloc-1.c: Add tests. * c-c++-common/analyzer/pr96639.c: Update expected results. * gcc.dg/analyzer/data-model-9.c: Likewise. Signed-off-by: David Malcolm --- gcc/analyzer/region-model-manager.cc | 6 ++++ .../c-c++-common/analyzer/calloc-1.c | 34 +++++++++++++++++++ gcc/testsuite/c-c++-common/analyzer/pr96639.c | 2 +- gcc/testsuite/gcc.dg/analyzer/data-model-9.c | 6 ++-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/gcc/analyzer/region-model-manager.cc b/gcc/analyzer/region-model-manager.cc index fc3523f8815c..62f808a81c20 100644 --- a/gcc/analyzer/region-model-manager.cc +++ b/gcc/analyzer/region-model-manager.cc @@ -457,6 +457,12 @@ region_model_manager::maybe_fold_unaryop (tree type, enum tree_code op, && region_sval->get_type () && POINTER_TYPE_P (region_sval->get_type ())) return get_ptr_svalue (type, region_sval->get_pointee ()); + + /* Casting all zeroes should give all zeroes. */ + if (type + && arg->all_zeroes_p () + && (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))) + return get_or_create_int_cst (type, 0); } break; case TRUTH_NOT_EXPR: diff --git a/gcc/testsuite/c-c++-common/analyzer/calloc-1.c b/gcc/testsuite/c-c++-common/analyzer/calloc-1.c index 6bd658ec94a4..cb93fa8987f0 100644 --- a/gcc/testsuite/c-c++-common/analyzer/calloc-1.c +++ b/gcc/testsuite/c-c++-common/analyzer/calloc-1.c @@ -22,3 +22,37 @@ char *test_1 (size_t sz) return p; } + +char ** +test_pr113333_1 (void) +{ + char **p = (char **)calloc (1, sizeof(char *)); + if (p) + { + __analyzer_eval (*p == 0); /* { dg-warning "TRUE" } */ + __analyzer_eval (p[0] == 0); /* { dg-warning "TRUE" } */ + } + return p; +} + +char ** +test_pr113333_2 (void) +{ + char **p = (char **)calloc (2, sizeof(char *)); + if (p) + { + __analyzer_eval (*p == 0); /* { dg-warning "TRUE" } */ + __analyzer_eval (p[0] == 0); /* { dg-warning "TRUE" } */ + __analyzer_eval (p[1] == 0); /* { dg-warning "TRUE" } */ + } + return p; +} + +char ** +test_pr113333_3 (void) +{ + char **vec = (char **)calloc (1, sizeof(char *)); + if (vec) + for (char **p=vec ; *p ; p++); /* { dg-bogus "heap-based buffer over-read" } */ + return vec; +} diff --git a/gcc/testsuite/c-c++-common/analyzer/pr96639.c b/gcc/testsuite/c-c++-common/analyzer/pr96639.c index b95217df6c41..2610ce8d602a 100644 --- a/gcc/testsuite/c-c++-common/analyzer/pr96639.c +++ b/gcc/testsuite/c-c++-common/analyzer/pr96639.c @@ -6,5 +6,5 @@ x7 (void) int **md = (int **) calloc (1, sizeof (void *)); return md[0][0]; /* { dg-warning "possibly-NULL" "unchecked deref" } */ - /* { dg-warning "leak of 'md'" "leak" { target *-*-* } .-1 } */ + /* { dg-warning "Wanalyzer-null-dereference" "deref of NULL" { target *-*-* } .-1 } */ } diff --git a/gcc/testsuite/gcc.dg/analyzer/data-model-9.c b/gcc/testsuite/gcc.dg/analyzer/data-model-9.c index 159bc612576c..2121f20c4f02 100644 --- a/gcc/testsuite/gcc.dg/analyzer/data-model-9.c +++ b/gcc/testsuite/gcc.dg/analyzer/data-model-9.c @@ -14,8 +14,7 @@ void test_1 (void) struct foo *f = calloc (1, sizeof (struct foo)); if (f == NULL) return; - __analyzer_eval (f->i == 0); /* { dg-warning "TRUE" "desired" { xfail *-*-* } } */ - /* { dg-bogus "UNKNOWN" "status quo" { xfail *-*-* } .-1 } */ + __analyzer_eval (f->i == 0); /* { dg-warning "TRUE" } */ free (f); } @@ -27,7 +26,6 @@ void test_2 (void) if (f == NULL) return; memset (f, 0, sizeof (struct foo)); - __analyzer_eval (f->i == 0); /* { dg-warning "TRUE" "desired" { xfail *-*-* } } */ - /* { dg-bogus "UNKNOWN" "status quo" { xfail *-*-* } .-1 } */ + __analyzer_eval (f->i == 0); /* { dg-warning "TRUE" } */ free (f); }