Received: by 10.213.65.68 with SMTP id h4csp1128887imn; Sun, 18 Mar 2018 16:02:42 -0700 (PDT) X-Google-Smtp-Source: AG47ELt6dwCprWPdFPLlDM+EKYXoeNme+iXJvnHaypPP3tHzugBYuKjOqmM8F4YS/wzc8lTsN9xn X-Received: by 2002:a17:902:6f01:: with SMTP id w1-v6mr10079128plk.196.1521414162180; Sun, 18 Mar 2018 16:02:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521414162; cv=none; d=google.com; s=arc-20160816; b=LBDrrZ0hTu2Fr/TOm5vPXJI4+O9daHK57wech/vgKMM9FPSfreYinxyc3cLDx/rYpX 42HGMjo+kuDkeVpVZsA4VEOcX7UKSTlunxzsJjoD+kEg3aB4/co8fX8DOcpbHKBqvlsq YSsFOqum/lyCvKqr/w3TfGjwmcC0lQKT/XsA3rnkPb94fGxfV6UOXKK7rXHhidU0rTar DXQE1I+0pIkenmp9lvIFPk/WlxGVNMOdYLs5WrZJWa9gqwDhQkeBs+T/0hXZJGn0PbhM MzRn/BtHuP3mCfZ/3bsEapzCDEQXJGyi12MJYUhk3bhgIUrjiUjde2bbXoubdU4MrvsS Hm1g== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature :arc-authentication-results; bh=4fTSjYDuL7r6lx5mtIweyhbko8vHW1PaeyuknksK65I=; b=HnkkPaetgLzJ9FRd8GvUEAq6RAX4Fb04Nt7fZ6UvLJaGUxfXTAoGgLOLYdVoL/93+t Zu24f6yNHWTqDjxQVyjDHVe0HdaijNqHl71jGdXgaxlg8BmpHzW+MBS7tsoUW5YFJ9zK lcoT896rPkWKdA/943AfUqeuhtBAL+m+uz0kmSuhM/JCu9RcMP9YFu/HU5imNihDnCf6 ajXBs5VoYJbMj/razBCenNZh6bgASFsO7IG9JJGVIyDsojmngFEIKXwQMAO4mWzQnBI+ zJtrh8CYQMXRWiWKKHFkEDSSKrdIoXHEXgazTCLGmIvbkT5f/lpN2VHiT0WN4ylmJO87 khhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rasmusvillemoes.dk header.s=google header.b=Sfwi0JAM; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m1-v6si10751736pls.673.2018.03.18.16.02.28; Sun, 18 Mar 2018 16:02:42 -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=@rasmusvillemoes.dk header.s=google header.b=Sfwi0JAM; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754966AbeCRW72 (ORCPT + 99 others); Sun, 18 Mar 2018 18:59:28 -0400 Received: from mail-wm0-f47.google.com ([74.125.82.47]:38000 "EHLO mail-wm0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754281AbeCRW7Z (ORCPT ); Sun, 18 Mar 2018 18:59:25 -0400 Received: by mail-wm0-f47.google.com with SMTP id l16so2815480wmh.3 for ; Sun, 18 Mar 2018 15:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=4fTSjYDuL7r6lx5mtIweyhbko8vHW1PaeyuknksK65I=; b=Sfwi0JAMhrhPJQwEr7w9VXkigpIjAzkcxRnjsMWAyN+Ld7WwmEEJr76k4fkGVf1Q8a nOquuJz3Gnm9KNHEbfSayYPqVGxH0I4LOWehe4Sn3UM9Xj90sbhH/UFF3phL6IJhA1xs 4aYNfppdX9zHJSilnjRlyoU24cPuvoJG8Y0jI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=4fTSjYDuL7r6lx5mtIweyhbko8vHW1PaeyuknksK65I=; b=Zzp5LTyESTNCB/DmvjMzAq/G99dsUZsqzSpoo7phv0MQZbVi1TYCzMX5diBQlpKcjl EAwXCpkHQf+kENBVVnPXP9mrw97iPUEqvCHpdoMxxw0WiBy3MpKxNBX8OCxji6BgRnj6 asYtx5bQ4uOX34mMwzATZMm1vCQZOB1RmdHlK8n2ZgWyxj5kll23FmCharRD0qWEnQs4 XqE7MQ0dFcDKOX0wukaPYN0Jkx2s61+krtq43ZAHiRctqqAhVDmNfh65MgtjOn+dFlAX 7Ew05B2wmqoCy7Q5UDAzmyFN+b86DKnJV1mM9QDT//eD93W6f187ECS7q9HnEkSTMFDl UMDg== X-Gm-Message-State: AElRT7GrndyB3JTjn53yRVDCZJjLfyikyOwEvgDo4HRDtxK8OB/C50nD 4kVTMG8aJL8HUMJX6uAAJeXkrA== X-Received: by 10.80.158.43 with SMTP id z40mr11058536ede.7.1521413964156; Sun, 18 Mar 2018 15:59:24 -0700 (PDT) Received: from [192.168.0.189] (dhcp-5-186-126-104.cgn.ip.fibianet.dk. [5.186.126.104]) by smtp.gmail.com with ESMTPSA id k5sm7320556edc.3.2018.03.18.15.59.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 18 Mar 2018 15:59:23 -0700 (PDT) Subject: Re: [PATCH v5 0/2] Remove false-positive VLAs when using max() To: Linus Torvalds , Rasmus Villemoes Cc: Kees Cook , Al Viro , Florian Weimer , Andrew Morton , Josh Poimboeuf , Randy Dunlap , Miguel Ojeda , Ingo Molnar , David Laight , Ian Abbott , linux-input , linux-btrfs , Network Development , Linux Kernel Mailing List , Kernel Hardening References: <1521174359-46392-1-git-send-email-keescook@chromium.org> <20180316175502.GE30522@ZenIV.linux.org.uk> <42b4342b-aefc-a16a-0d43-9f9c0d63ba7a@rasmusvillemoes.dk> From: Rasmus Villemoes Message-ID: <38b6da49-1138-017e-7307-f39ff067d6d2@rasmusvillemoes.dk> Date: Sun, 18 Mar 2018 23:59:21 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2018-03-18 22:33, Linus Torvalds wrote: > On Sun, Mar 18, 2018 at 2:13 PM, Rasmus Villemoes > wrote: >> On 2018-03-17 19:52, Linus Torvalds wrote: >>> >>> Ok, so it really looks like that same "__builtin_constant_p() doesn't >>> return a constant". >>> >>> Which is really odd, but there you have it. >> >> Not really. We do rely on builtin_constant_p not being folded too >> quickly to a 0/1 answer, so that gcc still generates good code even if >> the argument is only known to be constant at a late(r) optimization >> stage (through inlining and all). > > Hmm. That makes sense. It just doesn't work for our case when we > really want to choose the expression based on side effects or not. > >> So unlike types_compatible_p, which >> can obviously be answered early during parsing, builtin_constant_p is >> most of the time a yes/no/maybe/it's complicated thing. > > The silly thing is, the thing we *really* want to know _is_ actually > easily accessible during the early parsing, exactly like > __builtin_compatible_p(): it's not really that we care about the > expressions being constant, as much as the "can this have side > effects" question. OK, I missed where this was made about side effects of x and y, but I suppose the idea was to use no_side_effects(x) && no_side_effects(y) ? trivial_max(x, y) : old_max(x, y) or the same thing spelled with b_c_e? Yes, I think that would work, if we indeed had that way of checking an expression. > We only really use __builtin_constant_p() as a (bad) approximation of > that in this case, since it's the best we can do. I don't think you should parenthesize bad, rather capitalize it. ({x++; 0;}) is constant in the eyes of __builtin_constant_p, but not side-effect free. Sure, that's very contrived, but I can easily imagine some max(f(foo), -1) call where f is sometimes an external function, but for other .configs it's a static inline that always returns 0, but still has some non-trivial side-effect before that. And this would all depend on which optimizations gcc applies before it decides to evaluate builtin_constant_p, so could be some fun debugging. Good thing that that didn't work out... > So the real use would be to say "can we use the simple direct macro > that just happens to also fold into a nice integer constant > expression" for __builtin_choose_expr(). > > I tried to find something like that, but it really doesn't exist, even > though I would actually have expected it to be a somewhat common > concern for macro writers: write a macro that works in any arbitrary > environment. Yeah, I think the closest is a five year old suggestion (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57612) to add a __builtin_assert_no_side_effects, that could be used in macros that for some reason cannot be implemented without evaluating some argument multiple times. It would be more useful to have the predicate version, which one could always turn into a build bug version. But we have neither, unfortunately. Rasmus