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: 82375 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 952F23857C40 for ; Mon, 18 Dec 2023 08:35:06 +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+patchwork=sourceware.org@gcc.gnu.org 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); +}