Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp444591rwb; Wed, 28 Sep 2022 05:03:44 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4R66WXgVXFNSwvOiWWn4LtRdsB8bPHCO2sjuuSHzFkrF4Mp4B0W2tHJxCWVhQMuD2zG9pn X-Received: by 2002:a17:907:60cf:b0:782:d3ab:55b8 with SMTP id hv15-20020a17090760cf00b00782d3ab55b8mr20747323ejc.6.1664366623985; Wed, 28 Sep 2022 05:03:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664366623; cv=none; d=google.com; s=arc-20160816; b=hwV/fQASj4xSwI4DuMz7D1hd5uhTRaUry6kvWLSArPC6/dTQetFz8luPcGlESa+ydR KmNt49DJcmE25KIawkcyqUxnGc7gcNSNESWkfPdJz8Yk6UiUtCPVHh+MXRDhQeoda4sg wVjctcZrnmyn+4m6jP2R4VHNA3th93rV+gp16GJ4t/xSmeYoCKzj58srH7Z3z+Dk9JAZ qfXB5ZC+bwwduY6IImMA//zB0NLkbwEgkwE7VR3Vip4LxfPQs/qy1ol0U5yV6xsrCGad JK5JokFm6VfRvLGM2Rk8APXjCS4sM54LN9E7X/jPIP1JZDPoKgyV6jstjIfTy6+y5EIr BMyg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=e8ZRmPze3HbD72JVFPHdb7cuAb3u0f2hCbdavPoEMpU=; b=BloQXomwYcTjxIsC/A9yi5/HIdKegOnpYtypJFOFYPMlPLQa/Krr+pQ997VTqKbwkx FkTNPINOtJxnE1nkA7XVskYgAqR4EFmwaYRvDPim4ad/S0xCXAF7r358RcJsTr2FtOFo VRV2CbK4KASGozg1iPrYTQPe6MNuwLCyStSgvDeOIVWqj8JJ2WxiMaYpUFQKRMWP1irj xblNQFYVKiJH8SqbIcHU9riYmlj524PJY6nWNns7WHSOICN/ZH50UYSHYtEn2q1YtM0P oAoJc9TBLNu4lqvuFg/XS52GtIuY6eTm0TzFKs9PkYaycCnukGs8POdrZhKRjV9qxylU gFFw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nb14-20020a1709071c8e00b007828ae31468si4690160ejc.698.2022.09.28.05.03.16; Wed, 28 Sep 2022 05:03:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233683AbiI1LXv (ORCPT + 99 others); Wed, 28 Sep 2022 07:23:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229951AbiI1LXr (ORCPT ); Wed, 28 Sep 2022 07:23:47 -0400 Received: from mail-m118205.qiye.163.com (mail-m118205.qiye.163.com [115.236.118.205]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E31EDDCEBC for ; Wed, 28 Sep 2022 04:23:44 -0700 (PDT) Received: from lyc-workstation.. (unknown [221.212.176.62]) by mail-m118205.qiye.163.com (HMail) with ESMTPA id 342F32C0D19; Wed, 28 Sep 2022 19:23:42 +0800 (CST) From: YingChi Long To: david.laight@aculab.com Cc: bp@alien8.de, chang.seok.bae@intel.com, dave.hansen@linux.intel.com, hpa@zytor.com, linux-kernel@vger.kernel.org, me@inclyc.cn, mingo@redhat.com, ndesaulniers@google.com, pbonzini@redhat.com, tglx@linutronix.de, x86@kernel.org Subject: RE: [PATCH v2] x86/fpu: use _Alignof to avoid UB in TYPE_ALIGN Date: Wed, 28 Sep 2022 19:23:41 +0800 Message-Id: <20220928112341.2255320-1-me@inclyc.cn> X-Mailer: git-send-email 2.35.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-HM-Spam-Status: e1kfGhgUHx5ZQUpXWQgPGg8OCBgUHx5ZQUlOS1dZFg8aDwILHllBWSg2Ly tZV1koWUFPN1dZLVlBSVdZDwkaFQgSH1lBWUIaSkhWHRhMTkNDGU1KHUNLVQIWExYaEhckFA4PWV dZGBILWUFZSUlKVUlKSVVKTE1VTUlZV1kWGg8SFR0UWUFZT0tIVUpKS0hNSlVKS0tVS1kG X-HM-Sender-Digest: e1kMHhlZQR0aFwgeV1kSHx4VD1lBWUc6OhQ6Hgw6NjlNIzYUDAEBFw9D MBkwCg5VSlVKTU1PSE1PSUlISUpPVTMWGhIXVRYeOxIVGBcCGFUYFUVZV1kSC1lBWUlJSlVJSklV SkxNVU1JWVdZCAFZQUhCQks3Bg++ X-HM-Tid: 0a8383d6992c2d27kusn342f32c0d19 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > From: YingChi Long > > Sent: 27 September 2022 17:44 > > > > > Interesting - what justification do they give? > > > Linux kernel requires that the compiler add no unnecessary padding > > > so that structure definitions are well defined. > > > > Yes, that's a clarification given in 2019. > > > > > So using a type definition inside offsetof() won't give a > > > useful value - but it still isn't really UB. > > > > WG14 may worry about commas and the scope of new definitions. So they provide > > new words into the standard and said: > > > > > If the specified type defines a new type or if the specified member is a > > > bit-field, the behavior is undefined. > > > > https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2350.htm > > Except that the kernel requires it to be defined. > > Did they clarify the clause that required offsetof() to return > a compile-time constant? > That stops you doing offsetof(struct x, member->array[expression]). > (Oh and the compiler for a common OS disallows any version of that > even when expression in an integer constant!) WG14 N2350 may just not require implementation offsetof() accepts any type definitions within the first param of (not the second), and no further changes about whether it is compile-time constant? https://godbolt.org/z/9GsEPnPd6 #include struct foo { int a; int b[100]; }; int main() { int i; scanf("%d", &i); printf("%d\n", __builtin_offsetof(struct foo, b[i])); } We consider reject type definitions within the first parameter in clang. For example offsetof(struct { int a, b;}, a) However struct foo { int a; int b[20]; } offsetof(struct foo, b[sizeof(struct { int a, b;})]) Shall be fine. > > > > I've provided this link in the patch. > > > > > Has that ever worked? > > > Given: > > > struct foo { > > > int a; > > > char b; > > > char c; > > > }; > > > > TYPE_ALIGN(struct foo) evaluates to 4 in the previous approach (based on > > offsetof). _Align(struct foo) evaluates to the same value. > > > > See https://godbolt.org/z/sqebhEnsq > > > > > I think CHECK_MEMBER_AT_END_OF_TYPE(struct foo, b) is true. > > > > Hmm, both the previous version and after this patch the macro gives me > > false. (See the godbolt link). > > See https://godbolt.org/z/95shMx44j > > It return 1 for a and 0 for b and c (and char d,e following b). > NFI what it is trying to do! Switch _Alignof back to TYPE_ALIGN, CME seems return exactly the same values. I don't know what CME do here, but seems TYPE_ALIGN and _Alignof have the same semantics here? https://godbolt.org/z/hYcT1M3ed