From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 CDFFA3858D20 for ; Tue, 30 Apr 2024 21:29:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CDFFA3858D20 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 CDFFA3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714512557; cv=none; b=OSsDUeJZbEBZjSp7pFlDeyELuFf39f9XUwZPwhP2KnQb+55poSai1KznDMifKb6LXD5bC6kPMsf+nBeXHMaikJdNu/9Ivofq0tPeJGw5XYnUaIDhLiEYh2XN3vN/OfdsGX79MWt+BA/HQXITudTE316S81TuZVi+kTc0C+Khprs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714512557; c=relaxed/simple; bh=DxbMnTO344KDNFvPXUOi1qfViSPISzn2kl8gvqlFBw0=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=tMrgQntGqYGjDAIQ7CJ2vuGy1I/d3S6seOO18Y/S8Dt4znsBnVGsKNeKB75jkbxXGyZAS0902H2URLD0pBuYpYbHCyRjGXw5hmKNhnjgWjKihkclCtk56dsBiIzdSEKmKvj4AxcbC8PMjMjqs5HQeEAKRoAPL0Ex3dRzO8JhxUE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1714512554; 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; bh=SlqdaEOodhDv7cYwV108kudJYQXcXqloy5UYa1VyLa0=; b=BlPjhMPgMTYmeFhEFsagk+Ygl4BLjPrV1Oly6wG4YsUdA2FW/M4w78ZVCuaoQp70gUIQwo 0UEiwIdaipYQbIJN65Om0JOG7O3uWdl+2KT83zLl324ErlUCvDDVsTl8phZtv6fTm5Fmpu Nji7MyjXJme90LWd/0DO3mKfhEaKh48= Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-97-V_X5WFfgO8Cx2SJZ1uStIw-1; Tue, 30 Apr 2024 17:29:13 -0400 X-MC-Unique: V_X5WFfgO8Cx2SJZ1uStIw-1 Received: by mail-pf1-f197.google.com with SMTP id d2e1a72fcca58-6efefc57621so4191599b3a.2 for ; Tue, 30 Apr 2024 14:29:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714512551; x=1715117351; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=0zg82fiIQWoZoVuQA3av+Kl2FTxKYPYNkg4xMhMXoIU=; b=AoQaVB4voBDdvytJGfNybSHYVN3XUpBQy4NDnFjOOQQzkBDVd3Z9oOnj4FGGzxhmsH +t3qaG35puDhO7Bu0IMjgjOzfmBBjXHKC56qp6rFvFceMvjtTiNY7AVYTv5wumIkh3cZ SG/VZIHW4/PSHVS5Aig9NN26zBEW2zA8i7AIqFat/4DXxXSLu7Ym4WS26wAzdbGXQT6+ AFSToS84XDsIiFjeKfjMqC+p7nHOhU9aj/ZMPgglOA1kfKfKv3OCRKmyPorrj6NkUBq4 sUhKXr3pE9ezX61SRtOmgdHojjUq4FIT6Wl00CgebpZrRPDYRc/1KMybuJnRdkISSDj4 zCdw== X-Forwarded-Encrypted: i=1; AJvYcCVezr9vfkZ5ZgkqUXaE1lakO4olLdIGJslEUVMB9WuyTWjDluZG0lnjSBGlyAbyg4cBZWHxLhTQC6PXcTTtw65qx9rtXJ8hvA== X-Gm-Message-State: AOJu0Yyq2sqx8RMjykcT3oIqE+5pGhsf0UPtPmecTiQQqt/p4NwEhqRa giDyIvOzhekz83czI2CuHPRVtcX1bbMdI7FP+4ji5QjRSTmLPrFME8T1B3zVtBP/nVBrEmasosI Pv5/+lCYAHqEoky5G+dVvAOaVGaT7AKKtmZMTGt2oRpksPKfZ5fdZ18xDPTGlIEU= X-Received: by 2002:a05:6a00:3cca:b0:6ed:5f64:2fef with SMTP id ln10-20020a056a003cca00b006ed5f642fefmr1135942pfb.17.1714512551618; Tue, 30 Apr 2024 14:29:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFr8+YQhzAHn5CsSQeabWEpkZxrsW7Oh4T03g+BDoF02rjjDMMGClSmbmyJ31AJwZJ6grKPOg== X-Received: by 2002:a05:6a00:3cca:b0:6ed:5f64:2fef with SMTP id ln10-20020a056a003cca00b006ed5f642fefmr1135920pfb.17.1714512551253; Tue, 30 Apr 2024 14:29:11 -0700 (PDT) Received: from [192.168.1.130] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id e14-20020aa7824e000000b006edceb4ea9dsm21568155pfn.166.2024.04.30.14.29.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 30 Apr 2024 14:29:10 -0700 (PDT) Message-ID: <28d8b00a-f3a5-44b6-be74-c6fdb56fae70@redhat.com> Date: Tue, 30 Apr 2024 17:29:08 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3 4/4] Update the C FE routine "add_flexible_array_elts_to_size" C++ FE routine "layout_var_decl" to handle the cases when the DECL is union. To: Qing Zhao , josmyers@redhat.com, richard.guenther@gmail.com, siddhesh@gotplt.org Cc: uecker@tugraz.at, keescook@chromium.org, gcc-patches@gcc.gnu.org References: <20240430145833.1366425-1-qing.zhao@oracle.com> <20240430145833.1366425-5-qing.zhao@oracle.com> From: Jason Merrill In-Reply-To: <20240430145833.1366425-5-qing.zhao@oracle.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-24.2 required=5.0 tests=BAYES_00,DKIM_INVALID,DKIM_SIGNED,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,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 List-Id: On 4/30/24 07:58, Qing Zhao wrote: > Add testing cases to test the _bos for flexible array members in unions > or alone in structures. > > gcc/c/ChangeLog: > > * c-decl.cc (add_flexible_array_elts_to_size): Handle the cases > when the DECL is union. > > gcc/cp/ChangeLog: > > * decl.cc (layout_var_decl): Handle the cases when the DECL is > union with a flexible array member initializer. > > gcc/testsuite/ChangeLog: > > * c-c++-common/fam-in-union-alone-in-struct-bos-1.c: New test. > * c-c++-common/fam-in-union-alone-in-struct-bos.c: New test. > --- > gcc/c/c-decl.cc | 32 +++++++-- > gcc/cp/decl.cc | 33 ++++++++-- > .../fam-in-union-alone-in-struct-bos-1.c | 66 +++++++++++++++++++ > .../fam-in-union-alone-in-struct-bos.c | 45 +++++++++++++ > 4 files changed, 162 insertions(+), 14 deletions(-) > create mode 100644 gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos-1.c > create mode 100644 gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos.c > > diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc > index 947f3cd589eb..59302c5cfb1f 100644 > --- a/gcc/c/c-decl.cc > +++ b/gcc/c/c-decl.cc > @@ -5337,9 +5337,9 @@ zero_length_array_type_p (const_tree type) > } > > /* INIT is a constructor that forms DECL's initializer. If the final > - element initializes a flexible array field, add the size of that > - initializer to DECL's size. */ > - > + element initializes a flexible array field, adjust the size of the > + DECL with the initializer based on whether the DECL is a union or > + a structure. */ > static void > add_flexible_array_elts_to_size (tree decl, tree init) > { > @@ -5348,15 +5348,33 @@ add_flexible_array_elts_to_size (tree decl, tree init) > if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) > return; > > + bool is_decl_type_union = (TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE); In both C and C++ front-ends, I don't see any reason for this variable to be declared outside the if, since it's only used inside it. It's not clear to me that it's needed at all since it's only used once, but if you prefer to have it, please move it inside the if. > + > elt = CONSTRUCTOR_ELTS (init)->last ().value; > type = TREE_TYPE (elt); > if (flexible_array_member_type_p (type)) > { > complete_array_type (&type, elt, false); > - DECL_SIZE (decl) > - = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); > - DECL_SIZE_UNIT (decl) > - = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type)); > + /* For a structure, add the size of the initializer to the DECL's > + size. */ > + if (!is_decl_type_union) > + { > + DECL_SIZE (decl) > + = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); > + DECL_SIZE_UNIT (decl) > + = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), > + TYPE_SIZE_UNIT (type)); > + } > + /* For a union, the DECL's size is the maximum of the current size > + and the size of the initializer. */ > + else > + { > + DECL_SIZE (decl) > + = size_binop (MAX_EXPR, DECL_SIZE (decl), TYPE_SIZE (type)); > + DECL_SIZE_UNIT (decl) > + = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl), > + TYPE_SIZE_UNIT (type)); > + } > } > } > > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc > index 9a91c6f80da1..0ea3ef165b66 100644 > --- a/gcc/cp/decl.cc > +++ b/gcc/cp/decl.cc > @@ -6555,8 +6555,9 @@ layout_var_decl (tree decl) > } > } > > - /* If the final element initializes a flexible array field, add the size of > - that initializer to DECL's size. */ > + /* If the final element initializes a flexible array field, adjust > + the size of the DECL with the initializer based on whether the > + DECL is a union or a structure. */ > if (type != error_mark_node > && DECL_INITIAL (decl) > && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR > @@ -6568,6 +6569,7 @@ layout_var_decl (tree decl) > && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type))) > { > constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last (); > + bool is_decl_type_union = (TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE); > if (elt.index) > { > tree itype = TREE_TYPE (elt.index); > @@ -6577,11 +6579,28 @@ layout_var_decl (tree decl) > && TREE_CODE (vtype) == ARRAY_TYPE > && COMPLETE_TYPE_P (vtype)) > { > - DECL_SIZE (decl) > - = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype)); > - DECL_SIZE_UNIT (decl) > - = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), > - TYPE_SIZE_UNIT (vtype)); > + /* For a structure, add the size of the initializer to the DECL's > + size. */ > + if (!is_decl_type_union) > + { > + DECL_SIZE (decl) > + = size_binop (PLUS_EXPR, DECL_SIZE (decl), > + TYPE_SIZE (vtype)); > + DECL_SIZE_UNIT (decl) > + = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), > + TYPE_SIZE_UNIT (vtype)); > + } > + /* a union, the DECL's size is the maximum of the current size > + and the size of the initializer. */ > + else > + { > + DECL_SIZE (decl) > + = size_binop (MAX_EXPR, DECL_SIZE (decl), > + TYPE_SIZE (vtype)); > + DECL_SIZE_UNIT (decl) > + = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl), > + TYPE_SIZE_UNIT (vtype)); > + } > } > } > } > diff --git a/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos-1.c b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos-1.c > new file mode 100644 > index 000000000000..aae9cf39c8c7 > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos-1.c > @@ -0,0 +1,66 @@ > +/* testing flexible array members in unions and alone in structures: > + __bos/__bdos */ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > + > +union with_fam_1 { > + char a; > + int b[]; > +} *with_fam_1_v; > + > +union with_fam_2 { > + int a; > + char b[]; > +} *with_fam_2_v; > + > +union with_fam_3 { > + char a[]; > + int b[]; > +} *with_fam_3_v; > + > +struct only_fam { > + int b[]; > +} *only_fam_v; > + > +struct only_fam_2 { > + unsigned int : 2; > + unsigned int : 3; > + int b[]; > +} *only_fam_2_v; > + > +void __attribute__((__noinline__)) > +setup (int n1, int n2, int n3, int n4, int n5) > +{ > + with_fam_1_v = (union with_fam_1 *) __builtin_malloc (n1 * sizeof (int)); > + with_fam_2_v = (union with_fam_2 *) __builtin_malloc (n2 * sizeof (char)); > + with_fam_3_v = (union with_fam_3 *) __builtin_malloc (n3 * sizeof (int)); > + only_fam_v = (struct only_fam *) __builtin_malloc (n4 * sizeof (int)); > + only_fam_2_v = (struct only_fam_2 *) __builtin_malloc (n5 * sizeof (int)); > + return; > +} > + > +void __attribute__((__noinline__)) stuff( > + union with_fam_1 *with_fam_1_v, > + union with_fam_2 *with_fam_2_v, > + union with_fam_3 *with_fam_3_v, > + struct only_fam *only_fam_v, > + struct only_fam_2 *only_fam_2_v) > +{ > + if (__builtin_object_size(with_fam_1_v->b, 1) != -1) > + __builtin_abort (); > + if (__builtin_object_size(with_fam_2_v->b, 1) != -1) > + __builtin_abort (); > + if (__builtin_object_size(with_fam_3_v->b, 1) != -1) > + __builtin_abort (); > + if (__builtin_object_size(only_fam_v->b, 1) != -1) > + __builtin_abort (); > + if (__builtin_object_size(only_fam_2_v->b, 1) != -1) > + __builtin_abort (); > +} > + > +int main (int argc, char *argv[]) > +{ > + setup (2, 3, 4, 5, 6); > + stuff (with_fam_1_v, with_fam_2_v, with_fam_3_v, only_fam_v, only_fam_2_v); > + return 0; > +} > diff --git a/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos.c b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos.c > new file mode 100644 > index 000000000000..21badc57982b > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/fam-in-union-alone-in-struct-bos.c > @@ -0,0 +1,45 @@ > +/* testing flexible array members in unions and alone in structures: > + __bos/__bdos */ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > + > +union with_fam_1 { > + char a; > + int b[]; > +} with_fam_1_v = {.b = {1, 2, 3, 4, 5}}; > + > +union with_fam_2 { > + int a; > + char b[]; > +} with_fam_2_v = {.a = 0x1f2f3f4f}; > + > +union with_fam_3 { > + char a[]; > + int b[]; > +} with_fam_3_v = {.b = {0x1f2f3f4f, 0x5f6f7f7f}}; > + > +struct only_fam { > + int b[]; > +} only_fam_v = {{7, 11}}; > + > +struct only_fam_2 { > + unsigned int : 2; > + unsigned int : 3; > + int b[]; > +} only_fam_2_v = {{7, 11}}; > + > +int main () > +{ > + if (__builtin_object_size(with_fam_1_v.b, 1) != 20) > + __builtin_abort (); > + if (__builtin_object_size(with_fam_2_v.b, 1) != 4) > + __builtin_abort (); > + if (__builtin_object_size(with_fam_3_v.b, 1) != 8) > + __builtin_abort (); > + if (__builtin_object_size(only_fam_v.b, 1) != 8) > + __builtin_abort (); > + if (__builtin_object_size(only_fam_2_v.b, 1) != 8) > + __builtin_abort (); > + > + return 0; > +}