From patchwork Wed Aug 30 08:41:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 137151 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp4406356vqm; Wed, 30 Aug 2023 01:42:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH6ojCaAtXuAk8mIuGRg0zK9JoaT1C7AeTwyPiLwrQoTPdFhVv+a4C9tLAsOanOgcshxNYa X-Received: by 2002:a17:906:ce:b0:9a5:c919:55ca with SMTP id 14-20020a17090600ce00b009a5c91955camr1112839eji.65.1693384965805; Wed, 30 Aug 2023 01:42:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693384965; cv=none; d=google.com; s=arc-20160816; b=gvPIuhrT03MT87MCQmEn01sEEF7eZxuCBOjnSZUlQ6fLkoWqp7Wl5g+niOe+Dq5eBb UErWoK/rret/+2X1wvnLiWwlcAX/iFaStLJYfoM25W9BPWadIO4433MsidpT+0tixmiz kPLphcGsPsR/MEt46FinM7+vE0Oa165veev/nVfnp/BF7QA8AOGLbKFNCHYBLZr7mXFB 2IRajkIdmsgF5IwKcqYCO9Z9NkQeOwZTV1XR3lhCygwy38AZUtz4D4M56Ys63vF9fx8V SfMT3ti46TNOYMoSPy8JdKvqlU7pZwW3Bo1KB+FK+UFer4OnAXG39o6GT1WYIbPl9Y3V l2gQ== 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-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=waBfGl4avRg2mWN83rU2WT+RGxI4R6hDNCxhAoEnMYI=; fh=FCjeRajqaQYHMkQtfIia8KT5yBac53mYOLLyJhYG/AY=; b=hxqH9PwW1fgD1Qh9C4HaJ9ONKpXwM+wdcm9nefoi1LA8CYThBXo3feb78CfU6EsMoK T5T6ldnjigMExFiN7YnQqF/ErvwqIjw3ZN040LR1IJNucYnbVBAfzN3dadNpdd8Q37Dd 7mYUj0eaUbc4/nc84+E9JGOHhtXhKrCkiDimejP4KFo0m7LS9wR3bcbL+WBNLWXcYnpP nSzz4uRh+uAy84xMCa09o9RWFm53nn1+j5dx1qCilVfapdni96L7sl1KUy4gdfOojLQn g8CDO6HQfNInCZMf6pB/19M0ih5b4vihGeZ+JbBRDC7jfHWB+GcDxqD6VXmh60WmGnzX zoWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=lKJeK13q; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h7-20020a170906828700b009a1e73f2b52si6165806ejx.717.2023.08.30.01.42.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Aug 2023 01:42:45 -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=lKJeK13q; 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 94795385843E for ; Wed, 30 Aug 2023 08:42:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 94795385843E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693384964; bh=waBfGl4avRg2mWN83rU2WT+RGxI4R6hDNCxhAoEnMYI=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=lKJeK13qhr/fi+jwVEMjiO4LeN5+A7dVa4fXQ0mVMhC+APsJgiARxr/0R2TmpJEKO tVa7p1ocurlYxenobGeeh7q9j1vLdME7iq9znQOjAXVgjVF+PqnYSUHUQWHsM6oXlw QNmhvbzwPEq34Xa1V2L6gVwuK306GqQYKlYZV4Kc= 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 8318C3858C74 for ; Wed, 30 Aug 2023 08:41:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8318C3858C74 Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-629-SaU0PTTiNlS8dcY65O622Q-1; Wed, 30 Aug 2023 04:41:41 -0400 X-MC-Unique: SaU0PTTiNlS8dcY65O622Q-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 32C0E280AA2B; Wed, 30 Aug 2023 08:41:41 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.45.224.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id ECF3963F6C; Wed, 30 Aug 2023 08:41:40 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 37U8fdJs2049643 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 30 Aug 2023 10:41:39 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 37U8fctO2049642; Wed, 30 Aug 2023 10:41:38 +0200 Date: Wed, 30 Aug 2023 10:41:38 +0200 To: Richard Biener Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] tree-ssa-strlen: Fix up handling of conditionally zero memcpy [PR110914] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, 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: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775642834182716035 X-GMAIL-MSGID: 1775642834182716035 Hi! The following testcase is miscompiled since r279392 aka r10-5451-gef29b12cfbb4979 The strlen pass has adjust_last_stmt function, which performs mainly strcat or strcat-like optimizations (say strcpy (x, "abcd"); strcat (x, p); or equivalent memcpy (x, "abcd", strlen ("abcd") + 1); char *q = strchr (x, 0); memcpy (x, p, strlen (p)); etc. where the first stmt stores '\0' character at the end but next immediately overwrites it and so the first memcpy can be adjusted to store 1 fewer bytes. handle_builtin_memcpy called this function in two spots, the first one guarded like: if (olddsi != NULL && tree_fits_uhwi_p (len) && !integer_zerop (len)) adjust_last_stmt (olddsi, stmt, false); i.e. only for constant non-zero length. The other spot can call it even for non-constant length but in that case we punt before that if that length isn't length of some string + 1, so again non-zero. The r279392 change I assume wanted to add some warning stuff and changed it like if (olddsi != NULL - && tree_fits_uhwi_p (len) && !integer_zerop (len)) - adjust_last_stmt (olddsi, stmt, false); + { + maybe_warn_overflow (stmt, len, rvals, olddsi, false, true); + adjust_last_stmt (olddsi, stmt, false); + } While maybe_warn_overflow possibly handles non-constant length fine, adjust_last_stmt really relies on length to be non-zero, which !integer_zerop (len) alone doesn't guarantee. While we could for len being SSA_NAME ask the ranger or tree_expr_nonzero_p, I think adjust_last_stmt will not benefit from it much, so the following patch just restores the above condition/previous behavior for the adjust_last_stmt call only. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-08-30 Jakub Jelinek PR tree-optimization/110914 * tree-ssa-strlen.cc (strlen_pass::handle_builtin_memcpy): Don't call adjust_last_stmt unless len is known constant. * gcc.c-torture/execute/pr110914.c: New test. Jakub --- gcc/tree-ssa-strlen.cc.jj 2023-04-27 10:17:46.406486796 +0200 +++ gcc/tree-ssa-strlen.cc 2023-08-29 18:13:38.189327203 +0200 @@ -3340,7 +3340,8 @@ strlen_pass::handle_builtin_memcpy (buil && !integer_zerop (len)) { maybe_warn_overflow (stmt, false, len, olddsi, false, true); - adjust_last_stmt (olddsi, stmt, false); + if (tree_fits_uhwi_p (len)) + adjust_last_stmt (olddsi, stmt, false); } int idx = get_stridx (src, stmt); --- gcc/testsuite/gcc.c-torture/execute/pr110914.c.jj 2023-08-29 18:38:33.305699206 +0200 +++ gcc/testsuite/gcc.c-torture/execute/pr110914.c 2023-08-29 18:38:18.678901007 +0200 @@ -0,0 +1,22 @@ +/* PR tree-optimization/110914 */ + +__attribute__ ((noipa)) int +foo (const char *s, unsigned long l) +{ + unsigned char r = 0; + __builtin_memcpy (&r, s, l != 0); + return r; +} + +int +main () +{ + const char *p = "123456"; + int a = foo (p, __builtin_strlen (p) - 5); + int b = foo (p, __builtin_strlen (p) - 6); + if (a != '1') + __builtin_abort (); + if (b != 0) + __builtin_abort (); + return 0; +}