Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp6341208imm; Mon, 27 Aug 2018 14:05:28 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbtxDu4Y5FXQ7t/uDU0LneCgG4oHc5RMMlYJu2v2ajbshRpmp1mdTc9ylCwlvMfmoZ7GXI8 X-Received: by 2002:a62:4add:: with SMTP id c90-v6mr16034732pfj.65.1535403928118; Mon, 27 Aug 2018 14:05:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535403928; cv=none; d=google.com; s=arc-20160816; b=g2ifjPHM1bcAFOLPuoSN1oQM15+YuOG+5GqRjlNBAVNvxhmGbL/jkNR/yt4FUsf7Lb UM70LttJMA9hk0jBv6E6d9q+g+BeqbqppskGZGujsC7Aq9M+YTbbjpfTi2PZw9LocHcT lzuPtlpejj0TqTNTvHq7ngQrU5CnXCkcfMhXG3+YjTqyl6qUZV9uGHSQQ4QsigubSNnA qP0VXuJKpiQcMXmXh2hV0z2rFWN5dGJ8TqnJp6oPfEBW9ff0pCrx/J6WeMhgdJVHmLKo VU3dg6l1JvQXMRZjYocIoH/Nbn/0kOPw8SjjzF2WYQU8Uc8As1IQg3qYnEKQFTGJ+dur h1rQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature :arc-authentication-results; bh=KHsJhpH8yi6LYKXdGfv7SX1vxH/+vzTAGIg8HnDNG7k=; b=YtDqv+f1vN8fSC+dIZ7+E2i0vEzu6uwMEUq3dWGroca7bTROjsB116+18wHKktzCYt bhv8aSHg/GJ2ZqO+Dk30wec0F45CFnH0nQ4o288luwP2YY9jyw6KERAu0fvnGelRCVrF UpmuD43E2EAFDtl7E0EnlmKUZjg0QgbmP6eNVEK8y7VWSerCX6bWCul/hpE7m2+xhHAF BtivtvA+m+DNAL1IHrqjNM+JTKwpbZV39xBbFbVRLQ4i5Xn6GOMgKfOq2wJtASarSADu +Fkvo14kS3NkLXjdrJRcnSSd/lz+Rt9H1/YQW+eg/ex5i7HtzCkej6G2zUtAxhiaLLoS pUtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=lrB5h4i2; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v15-v6si188635pgl.656.2018.08.27.14.04.49; Mon, 27 Aug 2018 14:05:28 -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=@google.com header.s=20161025 header.b=lrB5h4i2; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727490AbeH1AuH (ORCPT + 99 others); Mon, 27 Aug 2018 20:50:07 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:33175 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726958AbeH1AuH (ORCPT ); Mon, 27 Aug 2018 20:50:07 -0400 Received: by mail-pl1-f194.google.com with SMTP id 60-v6so149288ple.0 for ; Mon, 27 Aug 2018 14:01:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=KHsJhpH8yi6LYKXdGfv7SX1vxH/+vzTAGIg8HnDNG7k=; b=lrB5h4i28mhLY0n9fazdo2kFHOve6FmTGUMNADCpzLtSzn4/AvOvN7g0gwMKMxKcIX 8d8AnP/mEU1sYeBE5fxHuzssYH/5Dd6KospWRSqxowYhc/pUQP9Wj9pWH//1TB10H3oe 79tNTfPqvwlCoA8fmayMe8/4p7S9hLUUxnAANFpWBPfgOt0uuLrRM61J8DXUSb+YZohy CjN/o7D2TjvAt/B0cv+cgJdQjv8OzQ7Vlh88GJ+h0U/Mv2Toaffc2a+fiPIylkrDyfWA PeZqtPofEiMo0MJQECcqsPrlNLidEFIZMb72EPUJAzu1gTyGleplGap0DFeuLg0G9fMZ zBAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=KHsJhpH8yi6LYKXdGfv7SX1vxH/+vzTAGIg8HnDNG7k=; b=T2L4zagFwVdtx4mJRflTr3SLrOATC7j82jn5fUTKHNW4V+wPjTYUKFWY0wBmxUl9/z eX103fA/W24FMXEznTX8fAqfHVp+6rMIHkM7Dz9ceiu4vZasFBqEVXqjyjQxbtj0gjVs ABRVDbnRJFfXMDHne4rmttqovZ6nUKbOF38Gw/2iju2BeoisPD46IZJQnrIvSrct0sO8 BItFJtrW2WH5tNkrNlsb8LHJnVCl1hHUqVzJ3ChHmGdA0eSLpoN+iMqhUENXOYrPDqe3 KTh7USDfo8rPh1E45ImmAAlLd3HrI9gkNnls3FP1v2BoBZFdfzyUpgVFgtH4nOgq3bJy dYgw== X-Gm-Message-State: APzg51D2Et5iXfxOzBo7dsxjqCr8sStpiRsjeVPGutlifQF96GGTSkJV nXoXmB/4zgtP1EjJcbYdbxyoNN8KKGzzyLqhiEzkag== X-Received: by 2002:a17:902:b7c3:: with SMTP id v3-v6mr14562935plz.238.1535403707572; Mon, 27 Aug 2018 14:01:47 -0700 (PDT) MIME-Version: 1.0 References: <1535220989-27645-1-git-send-email-yamada.masahiro@socionext.com> <84cf6ae0-97c8-6b73-ca86-b3d3b3daba5b@pobox.com> <8d5cf8c6-556a-96a1-610d-c92355783a9f@pobox.com> In-Reply-To: <8d5cf8c6-556a-96a1-610d-c92355783a9f@pobox.com> From: Nick Desaulniers Date: Mon, 27 Aug 2018 14:01:36 -0700 Message-ID: Subject: Re: [PATCH v2] compiler.h: give up __compiletime_assert_fallback() To: daniel.santos@pobox.com Cc: Masahiro Yamada , Linus Torvalds , Kees Cook , sparse@chrisli.org, linux-sparse@vger.kernel.org, LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 27, 2018 at 1:42 PM Daniel Santos wrote: > > Hello Nick, > > On 08/27/2018 03:09 PM, Nick Desaulniers wrote: > >> Now we're back to the question of "what do you mean by 'constant'"? If > >> you mean a C constant expression (as defined in the C standard) than > >> almost none of this code fits that criteria. For these compile-time > >> assertions to work, we are concerned with the data flow analysis and > >> constant propagation performed by the compiler during optimization. You > >> will notice in include/linux/compiler.h that __compiletime_assert is a > >> no-op when __OPTIMIZE__ is not defined. > > Depending on optimizations for static assertions sounds problematic. > > (with my best Palpatine voice) It is unavoidable. LOL > > Actually it's theoretically possible, but the compiler would have to do > something akin to copying it's control flow graph et. al, run -O2-ish > optimizations, perform the static assertions and then throw away the > optimized control flow graph and emit code based upon the original. > > > >>> As the comment in > >>> says, GCC (and Clang as well) only emits the error for obvious cases. > >>> > >>> When '__cond' is a variable, > >>> > >>> ((void)sizeof(char[1 - 2 * __cond])) > >>> > >>> ... is not obvious for the compiler to know the array size is negative. > >>> > >>> Reverting that commit would break BUILD_BUG() because negative-size-array > >>> is evaluated before the code is optimized out. > >>> > >>> Let's give up __compiletime_assert_fallback(). This commit does not > >>> change the current behavior since it just rips off the useless code. > >> Clang is not the only target audience of > >> __compiletime_assert_fallback(). Instead of ripping out something that > >> may benefit builds with gcc 4.2 and earlier, why not override its > > Note that with commit cafa0010cd51 ("Raise the minimum required gcc > > version to 4.6") that gcc < 4.6 is irrelevant. > > Ah, I guess I'm not keeping up, that's wonderful news! Considering that > I guess I would be OK with its removal, but I still think it would be > better if a similar mechanism to break the Clang build could be found. > > >> definition in compiler-clang.h with something that will break the build > >> for Clang? It would need an #ifndef __compiletime_error_fallback here > >> though. > >> > >>> Signed-off-by: Masahiro Yamada > >>> Reviewed-by: Kees Cook > >>> Reviewed-by: Nick Desaulniers > >>> --- > >>> > >>> Changes in v2: > >>> - Rebase > >>> > >>> include/linux/compiler.h | 17 +---------------- > >>> 1 file changed, 1 insertion(+), 16 deletions(-) > >>> > >>> diff --git a/include/linux/compiler.h b/include/linux/compiler.h > >>> index 681d866..87c776c 100644 > >>> --- a/include/linux/compiler.h > >>> +++ b/include/linux/compiler.h > >>> @@ -314,29 +314,14 @@ static inline void *offset_to_ptr(const int *off) > >>> #endif > >>> #ifndef __compiletime_error > >>> # define __compiletime_error(message) > >>> -/* > >>> - * Sparse complains of variable sized arrays due to the temporary variable in > >>> - * __compiletime_assert. Unfortunately we can't just expand it out to make > >>> - * sparse see a constant array size without breaking compiletime_assert on old > >>> - * versions of GCC (e.g. 4.2.4), so hide the array from sparse altogether. > >>> - */ > >>> -# ifndef __CHECKER__ > >>> -# define __compiletime_error_fallback(condition) \ > >>> - do { ((void)sizeof(char[1 - 2 * condition])); } while (0) > >>> -# endif > >>> -#endif > >>> -#ifndef __compiletime_error_fallback > >>> -# define __compiletime_error_fallback(condition) do { } while (0) > >>> #endif > >>> > >>> #ifdef __OPTIMIZE__ > >>> # define __compiletime_assert(condition, msg, prefix, suffix) \ > >>> do { \ > >>> - int __cond = !(condition); \ > >>> extern void prefix ## suffix(void) __compiletime_error(msg); \ > >>> - if (__cond) \ > >>> + if (!(condition)) \ > >>> prefix ## suffix(); \ > >>> - __compiletime_error_fallback(__cond); \ > >>> } while (0) > >>> #else > >>> # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0) > >> To give any more meaningful feedback I think I will need to experiment > >> with Clang, older GCC versions and icc. It occurred to me that I should > >> probably clean up and publish my __builtin_constant_p test program and > >> also generate results for more recent compilers. I can extend it to > >> test various negative sized array constructs and it could help inform > >> this decision. > >> > >> IMO, the most ideal solution would be a set of C2x (or future) > >> extensions providing something similar to C++'s constexpr, GCC's > >> __builtin_constant_p and our BUILD_BUG_ON. This would cross deeply into > > Note that __builtin_constant_p is a wild beast with lots of edge > > cases, and dependencies on compiler and optimization level. I'm > > trying to sort out some of these differences right now with llvm > > developers. > > Yes it is. IIRC, I even found cases where __builtin_constant_p returned > false, but the emitted code replaced the value in question with a > constant. So at least at one point, constant propagation and > __builtin_constant_p weren't always entirely coherent. What?! Do you have a link to a repro on godbolt or a gcc bugreport? Here's a different case I found that looks problematic: https://godbolt.org/z/g_iqwh > > I was working on a GCC extension that would solve this problem earlier > this year but bills and paying work interrupted me. :) The solution > involved adding a "const" attribute for function parameters and > variables that would simply create a warning or error if the value > wasn't compiled away at the time middle-end optimizations completed and > RTL emitted. It isn't finished -- maybe for gcc10. > Speaking with a coworker internally now, discussing Clang's diagnose_if/enable_if attributes. It seems that _Static_assert always (between compilers) considers parameters non-ICE, which sounds like a defect to me. -- Thanks, ~Nick Desaulniers