From patchwork Mon Dec 18 08:34:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 180202 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp1103358dyi; Mon, 18 Dec 2023 00:35:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IEfH8btbxtMRIXBmOLOM91eFJ0u+8L58ddVCxnla+42vaTtenXGKQOjBh5nIOImlNkb11mC X-Received: by 2002:a05:622a:1711:b0:417:a209:c258 with SMTP id h17-20020a05622a171100b00417a209c258mr27277127qtk.41.1702888514977; Mon, 18 Dec 2023 00:35:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702888514; cv=pass; d=google.com; s=arc-20160816; b=yM9ATdUb++tFkMOtgaxV7WggqYPC4nQ1ysMn8+LYxeKLePJ993FMB+EqlVES9scpC4 rl9T1sHeHRWkUMSagZVue1KyVUpL2+B+sQwaCac+nfOLUXEvCBG7QT/WvsM6f60AZqaR MJCWMwmmYcvukWh91gmjhsIYnjceX7Q5ReOnFONRtDUfR1RaeLUJ/6H4r45cQCL6xXG4 ctnwLiPiyR/kP+d+fRRd8h0ayIHvurulv/cNWagZKgcyO33evhMKdCP7HxIshKzQWshZ dvMxseEckntAZT7wIA3S5cZT/vpEke0SorEXTYtp2KydS1ouf6Cf5t27jslpH27CkORU uoRw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:message-id:subject:cc:to:from:date:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=hsPusHwCn6EPIHpoHdV7amt5KOWwPBrazNmhLBZgGPY=; fh=HK7p3uaEl1MO0C0wAfHdg61+euwFEIgvCV6ZgzcAqDg=; b=MKBG2S7uFtMLIMLZJbJDL1ujYJs6tlJtoO8YeZYaWXuBQS+z4AMpDVIv0HK5D+gPPf U9eDTNogiGhzkg0WzTUNmSd2F9Pie/UgdNWm4d0COomTeKYop3BGy9yH/MqjN5QMruZ6 VeBU9jWqGSCqGgHAFkMms5HY1YmkUzkeEkC/tHGV/XcXMV+PaJYDeVXCsPKYFx2JauuH hpj1+87OLOkMTAFKQShIJkqNcIUCjZuIPZwwnDAPbvqo95BtGcWAB9sbgzLdzz1fxKSr az9CQ+4tR/kGF9v2x6IxkUZq9SV7Pj4/pFBpv+zwT5uo8iPKcKpT5ia7M9Ht7BiJioyP innw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=R6P5nnU+; 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 w21-20020ac87e95000000b004261956e72asi5882314qtj.479.2023.12.18.00.35.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Dec 2023 00:35:14 -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=R6P5nnU+; 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 AD73B3858029 for ; Mon, 18 Dec 2023 08:35:14 +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 4AE7C3858424 for ; Mon, 18 Dec 2023 08:34:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4AE7C3858424 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 4AE7C3858424 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=1702888488; cv=none; b=d8BBOV4Yvd9bW+Xo1qQCI2CZ9Ni4ETUQXjdZs5Asmv7pi6SBscxSXajH5GLNxVRnLDz44ZN2GdHaA81z9UJvaZB8dtMX6Qvv+Uu0OlrFKiQmztHJpEGmEAk49Lj36A4R83iTTAPDjkle+KFa6xuQy7fUb5Eb1ifObxJiSU0xFC4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702888488; c=relaxed/simple; bh=mcIK9nL2nrlabjYtBaSJ59Lis4Vx5RxuspXgQMot5eY=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=gj/MRAeY8NjqXgDvESl88lV7EAxu0wtdGk7PEzmxcKpXjS/BayCWQUjWWKqKOiVp6h/msVKn/EpyX+PU8BYypaMMaopwg/LBiqAWDtII/KSrqzVXGk8cQYGfvlAH31ozWcau2HzaCRH50HTX/W+2IFE9Io/M6TluMUWndVRYqiY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702888485; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=hsPusHwCn6EPIHpoHdV7amt5KOWwPBrazNmhLBZgGPY=; b=R6P5nnU+dPvX7owYJhxUZfh+ZsaO+p8yaqQrJxe63mhXdQ55qRtheWkDr6hBjt7msbSUnr DyS9eMYdCBI8sA1LLvhVYtl3l/BmUa1J96WMmXE+aomo0FV+BW5KgsZgeUzdQRzu/uqpPb 1huc5f3oTQlPwHvCWuLL0LXBI7fkhcA= 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-418-3DinBv_sP0uLKQ8SC9o9dw-1; Mon, 18 Dec 2023 03:34:44 -0500 X-MC-Unique: 3DinBv_sP0uLKQ8SC9o9dw-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 D67B7185A781; Mon, 18 Dec 2023 08:34:43 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.92]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9625340C6EB9; Mon, 18 Dec 2023 08:34:43 +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 3BI8YedM1267845 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Mon, 18 Dec 2023 09:34:41 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3BI8YdKa1267844; Mon, 18 Dec 2023 09:34:39 +0100 Date: Mon, 18 Dec 2023 09:34:39 +0100 From: Jakub Jelinek To: Richard Biener Cc: gcc-patches@gcc.gnu.org, Siddhesh Poyarekar Subject: [PATCH] tree-object-size: Robustify alloc_size attribute handling [PR113013] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.4 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_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: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785608027429451942 X-GMAIL-MSGID: 1785608027429451942 Hi! The following testcase ICEs because we aren't careful enough with alloc_size attribute. We do check that such an argument exists (although wouldn't handle correctly functions with more than INT_MAX arguments), but didn't check that it is scalar integer, the ICE is trying to fold_convert a structure to sizetype. Given that the attribute can also appear on non-prototyped functions where the arguments aren't known, I don't see how the FE could diagnose that and because we already handle the case where argument doesn't exist, I think we should also verify the argument is scalar integer convertible to sizetype. Furthermore, given this is not just in diagnostics but used for code generation, I think it is better to punt on arguments with larger precision then sizetype, the upper bits are then truncated. The patch also fixes some formatting issues and avoids duplication of the fold_convert, plus removes unnecessary check for if (arg1 >= 0), that is always the case after if (arg1 < 0) return ...; Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2023-12-18 Jakub Jelinek PR tree-optimization/113013 * tree-object-size.cc (alloc_object_size): Return size_unknown if corresponding argument(s) don't have integral type or have integral type with higher precision than sizetype. Don't check arg1 >= 0 uselessly. Compare argument indexes against gimple_call_num_args in unsigned type rather than int. Formatting fixes. * gcc.dg/pr113013.c: New test. Jakub --- gcc/tree-object-size.cc.jj 2023-11-02 07:49:20.538817331 +0100 +++ gcc/tree-object-size.cc 2023-12-15 14:18:13.229417305 +0100 @@ -794,21 +794,33 @@ alloc_object_size (const gcall *call, in arg2 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p)))-1; } else if (gimple_call_builtin_p (call, BUILT_IN_NORMAL) - && callfn && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callfn))) - arg1 = 0; + && callfn + && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callfn))) + arg1 = 0; /* Non-const arguments are OK here, let the caller handle constness. */ - if (arg1 < 0 || arg1 >= (int) gimple_call_num_args (call) - || arg2 >= (int) gimple_call_num_args (call)) + if (arg1 < 0 + || (unsigned) arg1 >= gimple_call_num_args (call) + || (arg2 >= 0 && (unsigned) arg2 >= gimple_call_num_args (call))) return size_unknown (object_size_type); + tree targ1 = gimple_call_arg (call, arg1); + if (!INTEGRAL_TYPE_P (TREE_TYPE (targ1)) + || TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (sizetype)) + return size_unknown (object_size_type); + targ1 = fold_convert (sizetype, targ1); tree bytes = NULL_TREE; if (arg2 >= 0) - bytes = size_binop (MULT_EXPR, - fold_convert (sizetype, gimple_call_arg (call, arg1)), - fold_convert (sizetype, gimple_call_arg (call, arg2))); - else if (arg1 >= 0) - bytes = fold_convert (sizetype, gimple_call_arg (call, arg1)); + { + tree targ2 = gimple_call_arg (call, arg2); + if (!INTEGRAL_TYPE_P (TREE_TYPE (targ2)) + || TYPE_PRECISION (TREE_TYPE (targ2)) > TYPE_PRECISION (sizetype)) + return size_unknown (object_size_type); + targ2 = fold_convert (sizetype, targ2); + bytes = size_binop (MULT_EXPR, targ1, targ2); + } + else + bytes = targ1; return bytes ? bytes : size_unknown (object_size_type); } --- gcc/testsuite/gcc.dg/pr113013.c.jj 2023-12-15 14:20:19.889631653 +0100 +++ gcc/testsuite/gcc.dg/pr113013.c 2023-12-15 14:19:19.122488347 +0100 @@ -0,0 +1,14 @@ +/* PR tree-optimization/113013 */ +/* { dg-do compile } */ +/* { dg-options "-std=gnu99 -O2" } */ + +struct S { short x; } s; +void *foo () __attribute__((__alloc_size__(1))); +struct S *p; + +__SIZE_TYPE__ +bar (void) +{ + p = foo (s); + return __builtin_dynamic_object_size (p, 0); +}