From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from hedgehog.birch.relay.mailchannels.net (hedgehog.birch.relay.mailchannels.net [23.83.209.81]) by sourceware.org (Postfix) with ESMTPS id E9907384476F for ; Wed, 10 Apr 2024 21:48:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E9907384476F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E9907384476F Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=23.83.209.81 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712785714; cv=pass; b=St/R5emXnagaY1Zxmx0+nweNM7Zw3Hym2pCeWx32rlVo9zLusC2IxPwX/1Ipi1TFscL1OQ4OLlMvI6Y9k1xJMm6PMTQX0DrekcmuKZh1OCrFpFazVyHUMtj8hcfiulj5CEQszQh8O9TCFuG+BJdIQ+meHCFXLCf14h3X6+7Gu+4= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712785714; c=relaxed/simple; bh=UJWFL13KdLJsIiOCW+1UDEuwjUT6fFnUz6T6CUEpuWs=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=mSWG1AFmeaFLfTDJK9Jrw5J/2VcZgUFMQrSWBpDKXo5mPKbfIycwNW5Ikytrn6+HB+0Ga+ij4kAJglPzmu8ton2+S1HJCg8zVjuwt74ucFAqlewCrZ1e1GPJpsSUA7loq2n6bDCaKXzx8bQW/Ff6EECEpoVnPCDeXXDNMXq1qMc= ARC-Authentication-Results: i=2; server2.sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id B850C503490; Wed, 10 Apr 2024 21:48:22 +0000 (UTC) Received: from pdx1-sub0-mail-a300.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 4FEBE5034FF; Wed, 10 Apr 2024 21:48:22 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1712785702; a=rsa-sha256; cv=none; b=4XYlXs8kwpHPlvtpMZuTFsQLZqBbyRiahazu7cwbNioRkDmxOXU/KcjXyL54fXLcWIkChn 8Nrf0KZdfOyUOfmnifHSTg0/8mIAExEUALDy9mgztcx2B1XkInRNnnp5cj3+3omyrIIGNn llfhtuC45oiicgdtk0ROm8GVMxVe6Ls4pIubX1gjJEDl7cpl9JQpBq2q95aV86GwyJtSc/ ub4TodoCdqJW4PvQWfbdX8jM+mkP3iF2GSZT+GQbA3J2R9lY2p8gDinhvENoZwy1rDREns gGF+eHK9XXpesonrZ01tPXLn/VVYOyYtER02vEvRR2aAWgkT93MSlxC9RHtwEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1712785702; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DERbSr2uk4fgKKbWHUele8CquWdSa6s7XedABTgnh4s=; b=0lZrSA7eYrEvPM5c2kk3hCf4j1vV30GMxsnRV/SmwWs1Ax3m+jMXWGNSvEdOgP0/6APn5T 3v6EuBlSBlinQQtc2PTLkWJ0O/eoETBmzWZNeF0I5vlvC9Y0mZDzENI6am0aJ/YvKo3QSw GnX9X1EejAt9vFn8HRElhFobcXZlpVjJIe59nAx5gyINh6SJ2cYGgqy34eIw3I04jOAp0h 0GBnoIbyCl4vjP+bwWCmh33uAGaNvdAYlmi8zHsi6qFKvryVFH2qzMEJws/QCX1LXjo5Yn gG8DqxxaT2CM2i1Y+UmbM0RBn5WvlBwMdINVm1XuI2c/YpAvfH1VY4hFZ2/78w== ARC-Authentication-Results: i=1; rspamd-6f64594bc9-pldm8; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Company-Absorbed: 6a84aa236b1623f5_1712785702627_1615971042 X-MC-Loop-Signature: 1712785702627:1919450377 X-MC-Ingress-Time: 1712785702626 Received: from pdx1-sub0-mail-a300.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.125.113.89 (trex/6.9.2); Wed, 10 Apr 2024 21:48:22 +0000 Received: from [192.168.0.182] (bras-base-toroon4859w-grc-87-216-209-248-251.dsl.bell.ca [216.209.248.251]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a300.dreamhost.com (Postfix) with ESMTPSA id 4VFGfY3trrz1Q; Wed, 10 Apr 2024 14:48:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gotplt.org; s=dreamhost; t=1712785702; bh=DERbSr2uk4fgKKbWHUele8CquWdSa6s7XedABTgnh4s=; h=Date:Subject:To:Cc:From:Content-Type:Content-Transfer-Encoding; b=K64G7fJBA+AO3Z+eIeBiHD4kwq0QWcIK9ZqpKWHWTazrGgktYqKFxzMAV1SQU9YvL U+SAEjQZoaEGeOVn9pzodhtSSONr7gY5XwLBnnbv2JU7o666mp+mxxwjih06obbEJc kG6NRG+XJdX4VP2dnjv7UosnSPj8iT88XhZ1AfDBYoXV3VYYeMfCKx21q49hQhAFdJ Do0KWtGuEYEdsNc12Jir0PDp0CT6WOf8vwtlQRUXHjnKAno0sejkDI/e+tyOV8BaR2 +85Kl9h1osA15Ool4zT1ds2+ErGoCCMY2G7DruVOMJZP9lvTZwq1PmIwikYTDyrjPu CTK/DNnsK8+OQ== Message-ID: <6afbe685-f8f1-4dfe-8cee-13ae5c65da28@gotplt.org> Date: Wed, 10 Apr 2024 17:48:11 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v8 5/5] Add the 6th argument to .ACCESS_WITH_SIZE To: Qing Zhao , josmyers@redhat.com, richard.guenther@gmail.com, uecker@tugraz.at Cc: keescook@chromium.org, isanbard@gmail.com, gcc-patches@gcc.gnu.org References: <20240329160703.4012941-1-qing.zhao@oracle.com> <20240329160703.4012941-6-qing.zhao@oracle.com> Content-Language: en-US From: Siddhesh Poyarekar In-Reply-To: <20240329160703.4012941-6-qing.zhao@oracle.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3036.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 List-Id: On 2024-03-29 12:07, Qing Zhao wrote: > to carry the TYPE of the flexible array. > > Such information is needed during tree-object-size.cc. > > We cannot use the result type or the type of the 1st argument > of the routine .ACCESS_WITH_SIZE to decide the element type > of the original array due to possible type casting in the > source code. > > gcc/c/ChangeLog: > > * c-typeck.cc (build_access_with_size_for_counted_by): Add the 6th > argument to .ACCESS_WITH_SIZE. > > gcc/ChangeLog: > > * tree-object-size.cc (access_with_size_object_size): Use the type > of the 6th argument for the type of the element. > > gcc/testsuite/ChangeLog: > > * gcc.dg/flex-array-counted-by-6.c: New test. This version looks fine to me for stage 1, but I'm not a maintainer so you'll need an ack from one to commit. Thanks, Sid > --- > gcc/c/c-typeck.cc | 11 +++-- > gcc/internal-fn.cc | 2 + > .../gcc.dg/flex-array-counted-by-6.c | 46 +++++++++++++++++++ > gcc/tree-object-size.cc | 16 ++++--- > 4 files changed, 66 insertions(+), 9 deletions(-) > create mode 100644 gcc/testsuite/gcc.dg/flex-array-counted-by-6.c > > diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc > index f7b0e08459b0..05948f76039e 100644 > --- a/gcc/c/c-typeck.cc > +++ b/gcc/c/c-typeck.cc > @@ -2608,7 +2608,8 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) > > to: > > - (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1)) > + (*.ACCESS_WITH_SIZE (REF, COUNTED_BY_REF, 1, (TYPE_OF_SIZE)0, -1, > + (TYPE_OF_ARRAY *)0)) > > NOTE: The return type of this function is the POINTER type pointing > to the original flexible array type. > @@ -2620,6 +2621,9 @@ build_counted_by_ref (tree datum, tree subdatum, tree *counted_by_type) > The 4th argument of the call is a constant 0 with the TYPE of the > object pointed by COUNTED_BY_REF. > > + The 6th argument of the call is a constant 0 with the pointer TYPE > + to the original flexible array type. > + > */ > static tree > build_access_with_size_for_counted_by (location_t loc, tree ref, > @@ -2632,12 +2636,13 @@ build_access_with_size_for_counted_by (location_t loc, tree ref, > > tree call > = build_call_expr_internal_loc (loc, IFN_ACCESS_WITH_SIZE, > - result_type, 5, > + result_type, 6, > array_to_pointer_conversion (loc, ref), > counted_by_ref, > build_int_cst (integer_type_node, 1), > build_int_cst (counted_by_type, 0), > - build_int_cst (integer_type_node, -1)); > + build_int_cst (integer_type_node, -1), > + build_int_cst (result_type, 0)); > /* Wrap the call with an INDIRECT_REF with the flexible array type. */ > call = build1 (INDIRECT_REF, TREE_TYPE (ref), call); > SET_EXPR_LOCATION (call, loc); > diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc > index e744080ee670..34e4a4aea534 100644 > --- a/gcc/internal-fn.cc > +++ b/gcc/internal-fn.cc > @@ -3411,6 +3411,8 @@ expand_DEFERRED_INIT (internal_fn, gcall *stmt) > 1: read_only > 2: write_only > 3: read_write > + 6th argument: A constant 0 with the pointer TYPE to the original flexible > + array type. > > Both the return type and the type of the first argument of this > function have been converted from the incomplete array type to > diff --git a/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c > new file mode 100644 > index 000000000000..65fa01443d95 > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/flex-array-counted-by-6.c > @@ -0,0 +1,46 @@ > +/* Test the attribute counted_by and its usage in > + * __builtin_dynamic_object_size: when the type of the flexible array member > + * is casting to another type. */ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > + > +#include "builtin-object-size-common.h" > + > +typedef unsigned short u16; > + > +struct info { > + u16 data_len; > + char data[] __attribute__((counted_by(data_len))); > +}; > + > +struct foo { > + int a; > + int b; > +}; > + > +static __attribute__((__noinline__)) > +struct info *setup () > +{ > + struct info *p; > + size_t bytes = 3 * sizeof(struct foo); > + > + p = (struct info *)malloc (sizeof (struct info) + bytes); > + p->data_len = bytes; > + > + return p; > +} > + > +static void > +__attribute__((__noinline__)) report (struct info *p) > +{ > + struct foo *bar = (struct foo *)p->data; > + EXPECT(__builtin_dynamic_object_size((char *)(bar + 1), 1), 16); > + EXPECT(__builtin_dynamic_object_size((char *)(bar + 2), 1), 8); > +} > + > +int main(int argc, char *argv[]) > +{ > + struct info *p = setup(); > + report(p); > + return 0; > +} > diff --git a/gcc/tree-object-size.cc b/gcc/tree-object-size.cc > index 8de264d1dee2..4c1fa9b555fa 100644 > --- a/gcc/tree-object-size.cc > +++ b/gcc/tree-object-size.cc > @@ -762,9 +762,11 @@ addr_object_size (struct object_size_info *osi, const_tree ptr, > 1: the number of the elements of the object type; > 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE > of the object referenced by REF_TO_SIZE > + 6th argument: A constant 0 with the pointer TYPE to the original flexible > + array type. > > - The size of the element can be retrived from the result type of the call, > - which is the pointer to the array type. */ > + The size of the element can be retrived from the TYPE of the 6th argument > + of the call, which is the pointer to the array type. */ > static tree > access_with_size_object_size (const gcall *call, int object_size_type) > { > @@ -773,10 +775,12 @@ access_with_size_object_size (const gcall *call, int object_size_type) > return size_unknown (object_size_type); > > gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE)); > - /* Result type is a pointer type to the original flexible array type. */ > - tree result_type = gimple_call_return_type (call); > - gcc_assert (POINTER_TYPE_P (result_type)); > - tree element_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (result_type))); > + /* The type of the 6th argument type is the pointer TYPE to the original > + flexible array type. */ > + tree pointer_to_array_type = TREE_TYPE (gimple_call_arg (call, 5)); > + gcc_assert (POINTER_TYPE_P (pointer_to_array_type)); > + tree element_type = TREE_TYPE (TREE_TYPE (pointer_to_array_type)); > + tree element_size = TYPE_SIZE_UNIT (element_type); > tree ref_to_size = gimple_call_arg (call, 1); > unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2)); > tree type = TREE_TYPE (gimple_call_arg (call, 3));