Received: by 10.213.65.68 with SMTP id h4csp608080imn; Fri, 16 Mar 2018 13:04:46 -0700 (PDT) X-Google-Smtp-Source: AG47ELv2DMIt0pFRxpmYJ2/G1d0mDoU5uy+U60n26PFwTACinhocDxmW38YFkenyaf2aJsseVmZb X-Received: by 2002:a17:902:6ac1:: with SMTP id i1-v6mr611170plt.152.1521230686013; Fri, 16 Mar 2018 13:04:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521230685; cv=none; d=google.com; s=arc-20160816; b=dj8LjrB0c912bKwgmF/g8WLbiV9Wxyo1UMwfm3ro42R7R8MEal/AwsYrWqstS2hhIU K8aUrTwQGqozBalluLDhZ4xuAixI3NsRMhbCmIbFCVUTigJDi8GppMh8Gywj5KTsp75J qB8G2cUCo1GxR5ETthBi52zZiaRy9QyM0rT+tlL/O+CF0mfXDQkLsFOVK/5+8tK48xjD Yg7LodoGY60xLyToe6ICRhKwmeGFyG5TeE/9uu7zSrBXttpIa3ZtC5a+Mt7kAlmc85jJ lJp/rHXDIU6Pgd+x5hj0rIno1wAZYcoRZunCiBZJprXX7aWWEuPWM60UjoPo2LTXdBlG Xaeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=ZF+F2hSuIXKl9ezswQLM5At2Gh4VNzx1PaDEH4AkMPg=; b=ZDd94ws7XF7rElqdOGWV6Pcez3hkvtdn2ohjSvdZeiMtSklK4JW64NV/iUeOWcxzx6 4Mn6iDlHKb7qp601Bm9c72OojVPCtfeP2zWlAmYOMb4V+k7e+GSqa1m7prH/Fnja6+9G pKjy33sX9aYqGM/9L4pe5lnfjoQolIB1IofbQ/JXqjdatvfEXL1SHdSTrjFL9LzMk3pE Aw18JmRycMppsiQsbQL9iA64GkW5mA7UK/Ck/soQnuQDp+hc2cxwIInKjN43vZJpuX5q m2nM7r5SRSotw/ro8gvGEOGB9eIvbOzRsveu1ap977lBwdCQGL0Y9QETqI6lUN7YngD0 JmNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Km/x3f5q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h10si3473495pgc.296.2018.03.16.13.04.31; Fri, 16 Mar 2018 13:04:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Km/x3f5q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752269AbeCPUDg (ORCPT + 99 others); Fri, 16 Mar 2018 16:03:36 -0400 Received: from mail-qk0-f193.google.com ([209.85.220.193]:43206 "EHLO mail-qk0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750826AbeCPUDe (ORCPT ); Fri, 16 Mar 2018 16:03:34 -0400 Received: by mail-qk0-f193.google.com with SMTP id g184so12317067qkd.10; Fri, 16 Mar 2018 13:03:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=ZF+F2hSuIXKl9ezswQLM5At2Gh4VNzx1PaDEH4AkMPg=; b=Km/x3f5qgA739Zy5BTq+ILzzj24IwpB1TWPfvTSQHBgEWmOZQiFjz38F84/4SbHY2u 9OXCj5EIieAyg2zUesAmAESCfvVA2eTckhfAaDXqP6h4TLikegG0fu3PIbnp3i9IX3aj cw6WXAAVWHcHhW0ZDG+zlpR5Y7ZPP9sLhRC04sp/wvWh+H9CUneoxKKThH27BOUXkzgA oBEbhC399flvTUv3aAE6u4Rkuorp2qLqSUbrExU6Nl/m33R5Wr932CplxRf9OmVjUFJw dreZEoD1wu7kb0uzvnLLnqniJL7/ZE20dpTxu9t0k+DXsHXgiLgJQefSP8pSF7afJawr zBlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=ZF+F2hSuIXKl9ezswQLM5At2Gh4VNzx1PaDEH4AkMPg=; b=VGOW7/wNh4oX2YoJ4RMNHEanB1oO3WrB58JeI6L4m0go/mRBCrQB4r06JMrpsRmGos 2JMpejtDCe/38Jv4iVg1REywgmv+QdT7YtyukzAD/7lwW3I5ngEva6w290keMNI9kQcD YN7cZ3A11IZTZ6rbAwr4Qa3uwmlw2LkBtcL+poNnmyju76FclN970yP6EDVhXToiEiOF N97KVb4qdV+n3dQROuZOePxHtrzRSPWKlOKsgyqeB7GwVA9OQMbpbbji3n21O3uP4Qm6 42FY0ZvYg8SS5OjWQZTzylsf0HS0JPuNEI3cO/4Tw9AcZHooLDlfzfGR/FtYopPwtuOS OIaQ== X-Gm-Message-State: AElRT7HDppHibUFx/N7d79a284wxhHB8ymZp3qxwOvKINTn7F1KuWeam NYjgoOCQCcKXEz8yFHliDO34uDlVlABv93mSLq0= X-Received: by 10.55.170.67 with SMTP id t64mr4446378qke.54.1521230613517; Fri, 16 Mar 2018 13:03:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.200.52.227 with HTTP; Fri, 16 Mar 2018 13:03:13 -0700 (PDT) In-Reply-To: References: <1521174359-46392-1-git-send-email-keescook@chromium.org> <20180316175502.GE30522@ZenIV.linux.org.uk> From: Miguel Ojeda Date: Fri, 16 Mar 2018 21:03:13 +0100 Message-ID: Subject: Re: [PATCH v5 0/2] Remove false-positive VLAs when using max() To: Linus Torvalds Cc: Al Viro , Florian Weimer , Kees Cook , Andrew Morton , Josh Poimboeuf , Rasmus Villemoes , Randy Dunlap , Ingo Molnar , David Laight , Ian Abbott , linux-input , linux-btrfs , Network Development , Linux Kernel Mailing List , Kernel Hardening Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 16, 2018 at 8:27 PM, Linus Torvalds wrote: > On Fri, Mar 16, 2018 at 10:55 AM, Al Viro wrote= : >> >> That's not them, that's C standard regarding ICE. > > Yes. The C standard talks about "integer constant expression". I know. > It's come up in this very thread before. > > The C standard at no point talks about - or forbids - "variable length > arrays". That never comes up in the whole standard, I checked. > > So we are right now hindered by a _syntactic_ check, without any way > to have a _semantic_ check. > > That's my problem. The warnings are misleading and imply semantics. > > And apparently there is no way to actually check semantics. > >> 1,100 is *not* a constant expression as far as the standard is concerned= , > > I very much know. > > But it sure isn't "variable" either as far as the standard is > concerned, because the standard doesn't even have that concept (it > uses "variable" for argument numbers and for variables). > > So being pedantic doesn't really change anything. > >> Would you argue that in >> void foo(char c) >> { >> int a[(c<<1) + 10 - c + 2 - c]; > > Yeah, I don't think that even counts as a constant value, even if it > can be optimized to one. I would not at all be unhppy to see > __builtin_constant_p() to return zero. > > But that is very much different from the syntax issue. > > So I would like to get some way to get both type-checking and constant > checking without the annoying syntax issue. > >> expr, constant_expression is not a constant_expression. And in >> this particular case the standard is not insane - the only reason >> for using that is typechecking and _that_ can be achieved without >> violating 6.6p6: >> sizeof(expr,0) * 0 + ICE >> *is* an integer constant expression, and it gives you exact same >> typechecking. So if somebody wants to play odd games, they can >> do that just fine, without complicating the logics for compilers... > > Now that actually looks like a good trick. Maybe we can use that > instead of the comma expression that causes problems. > > And using sizeof() to make sure that __builtin_choose_expression() > really gets an integer constant expression and that there should be no > ambiguity looks good. > > Hmm. > > This works for me, and I'm being *very* careful (those casts to > pointer types are inside that sizeof, because it's not an integral > type, and non-integral casts are not valid in an ICE either) but > somebody needs to check gcc-4.4: > > #define __typecheck(a,b) \ > (!!(sizeof((typeof(a)*)1=3D=3D(typeof(b)*)1))) > > #define __no_side_effects(a,b) \ > (__builtin_constant_p(a)&&__builtin_constant_p(b)) > > #define __safe_cmp(a,b) \ > (__typecheck(a,b) && __no_side_effects(a,b)) > > #define __cmp(a,b,op) ((a)op(b)?(a):(b)) > > #define __cmp_once(a,b,op) ({ \ > typeof(a) __a =3D (a); \ > typeof(b) __b =3D (b); \ > __cmp(__a,__b,op); }) > > #define __careful_cmp(a,b,op) \ > __builtin_choose_expr(__safe_cmp(a,b), __cmp(a,b,op), > __cmp_once(a,b,op)) > > #define min(a,b) __careful_cmp(a,b,<) > #define max(a,b) __careful_cmp(a,b,>) > #define min_t(t,a,b) __careful_cmp((t)(a),(t)(b),<) > #define max_t(t,a,b) __careful_cmp((t)(a),(t)(b),>) > > and yes, it does cause new warnings for that > > comparison between =E2=80=98enum tis_defaults=E2=80=99 and =E2=80=98e= num tpm2_const=E2=80=99 > > in drivers/char/tpm/tpm_tis_core.h due to > > #define TIS_TIMEOUT_A_MAX max(TIS_SHORT_TIMEOUT, TPM2_TIMEOUT_A) > > but technically that warning is actually correct, I'm just confused > why gcc cares about the cast placement or something. > > That warning is easy to fix by turning it into a "max_t(int, enum1, > enum2)' and that is technically the right thing to do, it's just not > warned about for some odd reason with the current code. > > Kees - is there some online "gcc-4.4 checker" somewhere? This does > seem to work with my gcc. I actually tested some of those files you > pointed at now. I use this one: https://godbolt.org/ Cheers, Miguel