From patchwork Mon Nov 19 10:31:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 151466 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2487991ljp; Mon, 19 Nov 2018 02:32:51 -0800 (PST) X-Google-Smtp-Source: AJdET5eH0Ri10hYbfkXeJr1NdW/XrA/th4BFTy5v4LlSjcUlSzNslt820/7Fj/f8vpLOCLRWtvaW X-Received: by 2002:a62:e704:: with SMTP id s4mr22417730pfh.124.1542623571011; Mon, 19 Nov 2018 02:32:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542623571; cv=none; d=google.com; s=arc-20160816; b=Ql0VSwhLJ9uBP/ZTVVG62GYdwzjs9J43FeNMzDbS7rPsZ7lCUnMNhtAClUx1GSWMVu TUP55xH5OxpiMvfNxayE7ouhqCSQFk2QnowgRvBQTACyph0RU57eW3aDXDfYWUdrodKH Sw4oesfvfSlrXAfefQrGqriKHc311kk3KYYfw76OS4jIKO78MviV47xliLXheSjBpShm IdqDWVChnmkopMNxYVbmiTwx4osSjqmevXeECQjLztA3ADB21qRGBti9odPPmIYSfYqF vWWWZ1luiNGdPWkKwMxPAV8bQsYn0N67J63npPSj+YnYx5SG0xItRptaVu5iMCJnA6IM g48w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=+LKuroFmzxn4Iv2kghmjy3Fv1P/6HtUAjZsqkO5MVRA=; b=fQzceRhcb1+WET0wA9koyzxbbFayT5HfFvtBG/ZPHysJnQphcj01nmDZUpXXGslfyC j4EpI22t8MyBYwOkU1PoIa6yzZ1lC4WPb3lH/m09vV4do2IUB/CTi9AZxlj4aGZv83wO S0ED4yb/5NPBnZwje43lUSy/giN3PR356NdqULhc1qgkHczxsEe8BbCv20/s/T8RksVb bkTRzx6t4fPPnHmsfY6wfgGtiDbDMycETtwjKWslRbJnMVqAu4QmOmvJoXJVZGYAVwI2 O4bX7nZry7L0ciJ9OmLwAW5obyK97nBCKkijYHsp9fLZouuG4c17xicEtB1CqNytvM7X XfcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b="d8aQTDU/"; 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 m3si10196157pfh.58.2018.11.19.02.32.50; Mon, 19 Nov 2018 02:32:51 -0800 (PST) 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=@nifty.com header.s=dec2015msa header.b="d8aQTDU/"; 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 S1727790AbeKSU4A (ORCPT + 32 others); Mon, 19 Nov 2018 15:56:00 -0500 Received: from conuserg-11.nifty.com ([210.131.2.78]:59555 "EHLO conuserg-11.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727352AbeKSUz7 (ORCPT ); Mon, 19 Nov 2018 15:55:59 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-11.nifty.com with ESMTP id wAJAVrOA009918; Mon, 19 Nov 2018 19:31:53 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-11.nifty.com wAJAVrOA009918 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1542623513; bh=+LKuroFmzxn4Iv2kghmjy3Fv1P/6HtUAjZsqkO5MVRA=; h=From:To:Cc:Subject:Date:From; b=d8aQTDU/NnCbJq5jalDLUcMb2UiQovuCMj/IjNJ5UQNLASRDggGDCEDPn5dyVcKK3 We7Ydll0cr8h2P/91yTboxcU+ochBe6DlhOQCuDFIAs+TVHegFR5l/dWFgwXrLUNSz MxFV3e/YIZgTMyii7Rn+wvgukt4zxMRJxAvS8NER9gkCJ0UUfMbRvNFbnvnIIoYyji THUH3cFofmWWpJi3UjBS3J1vcdlWudY111h50gWE9MRTFSXtLREIm2mm24hGd3GJfi 8vqZleQT2N882diV6fYdWtRJFwgKvQGTiKaF09jDkNzyRaxVBN34ep7BivZGVDgCbl qXGrlm+hv/ZkQ== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Andrew Morton Cc: Luc Van Oostenryck , Nick Desaulniers , Kees Cook , Josh Triplett , Masahiro Yamada , Miguel Ojeda , Arnd Bergmann , linux-kernel@vger.kernel.org, Paul Burton Subject: [PATCH v3 1/3] compiler_types.h: make __builtin_types_compatible_p() noop for Sparse Date: Mon, 19 Nov 2018 19:31:41 +0900 Message-Id: <1542623503-3755-1-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When I tried to delete BUILD_BUG_ON stubs for sparse, the kbuild test robot reported lots of Sparse warnings from container_of(), which seem false positive. The following checker in container_of() seems to be causing something strange for Sparse. BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ !__same_type(*(ptr), void), \ "pointer type mismatch in container_of()"); \ I narrowed down the problem into the following test code: --------------------(test_code.c begin)-------------------- struct foo { int (*callback)(void); }; void assert(int); static inline struct foo *get_foo(void) { assert(__builtin_types_compatible_p(void, void)); return (struct foo *)0; } int test(void); int test(void) { return get_foo()->callback(); } ---------------------(test_code.c end)--------------------- Of course, GCC (and Clang as well) can compile it: $ gcc -Wall -c -o test_code.o test_code.c However, Sparse complains about this obviously correct code: $ sparse test_code.c test_code.c:9:45: warning: unknown expression (4 0) test_code.c:9:51: warning: unknown expression (4 0) Interstingly, just removing the 'inline' keyword in the test code makes Sparse happy. I concluded that Sparse cannot handle __builtin_types_compatible_p() correctly. Make it no-op. Signed-off-by: Masahiro Yamada --- Changes in v3: - New patch Changes in v2: None include/linux/compiler_types.h | 1 + 1 file changed, 1 insertion(+) -- 2.7.4 diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 4a3f9c0..9e7da0b 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -23,6 +23,7 @@ extern void __chk_user_ptr(const volatile void __user *); extern void __chk_io_ptr(const volatile void __iomem *); # define ACCESS_PRIVATE(p, member) (*((typeof((p)->member) __force *) &(p)->member)) +# define __builtin_types_compatible_p(t1, t2) (1) #else /* __CHECKER__ */ # ifdef STRUCTLEAK_PLUGIN # define __user __attribute__((user)) From patchwork Mon Nov 19 10:31:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 151464 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2487826ljp; Mon, 19 Nov 2018 02:32:42 -0800 (PST) X-Google-Smtp-Source: AJdET5dKN56xAAVP6wwH5r5Fsy0zT79o6fTNArdJlNRe9Z+8TVQ3L1xiaJm/5blmMG7+zE38wEH1 X-Received: by 2002:a62:cd41:: with SMTP id o62mr7809655pfg.222.1542623562536; Mon, 19 Nov 2018 02:32:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542623562; cv=none; d=google.com; s=arc-20160816; b=UWFA7JkCvn/Yznv7z/cLeAWLEivRv/dpRHi8ajqJ5+vmcpieQFXxO4BGrNVwZlPbxA RKTz66JXkwRT6Dbw4bmeMd4BZxWVwkEoCeMMdv7uLj/dLRuP76HPUHwAk6hlj9entUKr jvIrXe0Vd6XnG27UX1oNEvBfPFkIJ4A2nnd4bY0Va4JFcg7Tj6X9jwtS/bP2G7wp13A1 J2GGvDm/hwzPCvIPPaQKabn4OzG4EewlDBXUOwNFL6fo+MD9zFtTSuzF2sGz6sofViRU MRdukphyZ9ktvEyijyvra60ZgBPEnvJUCeRhbTzedAyN6Vd0H687rBBmHgAdRQ0HC//V 1S2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=8Od9yITiN3CpQ9ID8oxmfcb1MGMov3ZoJ+/Zebs+qw4=; b=QKosRccViNRAXNciiNbK1COTIBdTwLF0+tM2/nBTUSX4NKar+8EjIYkSAu0T3xTIOn C35HEEFAq91aDVdoP81KUhtRT+yBa5y4jiPfpxGN7sds1QKkeqheiSNpWyOArB2zDCbU TojiTd0Qd/VpgOLz7SrxOTIcxN/LNwkYqZ3JI1LflTdRZQatnpf2rufLBNwpBNVgRJIJ iZ73heAun4de2tQAxki2OFvBvoCAW6DL3Un1Z0vckHXYQwPTLgljsU2lSXkP3nx1VgB0 iiUQu/AU+1YNQVHWdP7JN00HO7s+CtSI7BU4I4iRe/ULpCg0tKDlaz7lH+VhMbR/m3rD nHHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=es473xh3; 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 j189si36668017pgd.185.2018.11.19.02.32.41; Mon, 19 Nov 2018 02:32:42 -0800 (PST) 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=@nifty.com header.s=dec2015msa header.b=es473xh3; 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 S1727677AbeKSUzv (ORCPT + 32 others); Mon, 19 Nov 2018 15:55:51 -0500 Received: from conuserg-11.nifty.com ([210.131.2.78]:59354 "EHLO conuserg-11.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727489AbeKSUzv (ORCPT ); Mon, 19 Nov 2018 15:55:51 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-11.nifty.com with ESMTP id wAJAVrOB009918; Mon, 19 Nov 2018 19:31:54 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-11.nifty.com wAJAVrOB009918 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1542623514; bh=8Od9yITiN3CpQ9ID8oxmfcb1MGMov3ZoJ+/Zebs+qw4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=es473xh3BXGrESilENTJ82S6FUNHsottnp1X6xGXkAXtiFTf537ER62oQzK6A14UU K964ZaGfZhg1NiylVuxj9rzo8dwG1ULrBwTPRQN03iDH+GDytfoVtZGnpxzAMx5TP/ DjEH9b3s7YZP4M7SwxrkY0gk8TB+19Fae4tg52EyrKZvw6xSTG9C+tUcCHe4KMuHMU n7qPbT5pzvCIDSWfhbjvj6YYzof43pnhyffPA7EPxwB3sEvTThg21gGtNJGcaADaI6 xYKsRzPuMihOygCymhCceaf66UA/Ozx6BnkvLf/uDhdM/Lwk16Iv+gelvkJ6G7MNdW phzVg16pCvM7w== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Andrew Morton Cc: Luc Van Oostenryck , Nick Desaulniers , Kees Cook , Josh Triplett , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH v3 2/3] build_bug.h: remove negative-array fallback for BUILD_BUG_ON() Date: Mon, 19 Nov 2018 19:31:42 +0900 Message-Id: <1542623503-3755-2-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1542623503-3755-1-git-send-email-yamada.masahiro@socionext.com> References: <1542623503-3755-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The kernel can only be compiled with an optimization option (-O2, -Os, or the currently proposed -Og). Hence, __OPTIMIZE__ is always defined in the kernel source. The fallback for the -O0 case is just hypothetical and pointless. Moreover, commit 0bb95f80a38f ("Makefile: Globally enable VLA warning") enabled -Wvla warning. The use of variable length arrays is banned. Signed-off-by: Masahiro Yamada Acked-by: Kees Cook --- Changes in v3: None Changes in v2: None include/linux/build_bug.h | 14 -------------- 1 file changed, 14 deletions(-) -- 2.7.4 Reviewed-by: Nick Desaulniers Tested-by: Nick Desaulniers diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h index 43d1fd5..d415c64 100644 --- a/include/linux/build_bug.h +++ b/include/linux/build_bug.h @@ -51,23 +51,9 @@ * If you have some code which relies on certain constants being equal, or * some other compile-time-evaluated condition, you should use BUILD_BUG_ON to * detect if someone changes it. - * - * The implementation uses gcc's reluctance to create a negative array, but gcc - * (as of 4.4) only emits that error for obvious cases (e.g. not arguments to - * inline functions). Luckily, in 4.3 they added the "error" function - * attribute just for this type of case. Thus, we use a negative sized array - * (should always create an error on gcc versions older than 4.4) and then call - * an undefined function with the error attribute (should always create an - * error on gcc 4.3 and later). If for some reason, neither creates a - * compile-time error, we'll still have a link-time error, which is harder to - * track down. */ -#ifndef __OPTIMIZE__ -#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) -#else #define BUILD_BUG_ON(condition) \ BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition) -#endif /** * BUILD_BUG - break compile if used. From patchwork Mon Nov 19 10:31:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 151465 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp2487887ljp; Mon, 19 Nov 2018 02:32:44 -0800 (PST) X-Google-Smtp-Source: AJdET5fSR1DCl3MUiOURbw2C5bxVSvlB1AB1Mb0vGJTRpYhFwf8wPwhrIoc5CY0BS9ewcj/nNSZI X-Received: by 2002:a62:7892:: with SMTP id t140mr22345941pfc.237.1542623564625; Mon, 19 Nov 2018 02:32:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542623564; cv=none; d=google.com; s=arc-20160816; b=D7gjNPXqF49zSieBRf8m66Q6BmO8uEoC6Oh5peb/u/pfV0GDV1Z0kPN1uH336GwSR+ 6CBWj+CfmHxbMalaiP7bHzPsJCyMXrk9+5/sU0UyAgEhrAON7nyb6f76+HTJRNhaVEa7 c5EJQ96bIr5lhLWjOcSW0OSXae3AGU8qvU5f4qs2Ug+kF/ZmSBTO1f6oQi0R8/TlkfSB IAlHOjCSkVZJ4ziL2TRv5F9SVd20q8Wh0qHqxj7VQaqKasB31HmkQsSgFdE1o9x3eVWs wxEloSCOV3qIEKvQI/hW3BMCKTuxdmRkFPYxr/H5I8RctFXoY37fsOJI88iHispyS3kL nVhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:dkim-filter; bh=UxpwZPndNpZuNEqiuBMWdLIBfZYlxrIZKZ+04Pdc4gw=; b=LIwxpVx2Ljgb37iSjtwZEq2MVSMs+ggj3NoMP7kPltb09x/kDzn+xCiAaGsD+eWBXV rakmbjHTC15Jbi/utPB0JbC4CwVOghQEK5GdMut3DLcmweXNv+z8UMr6SScL1JpcpyAc 5H3sHsNMC/CXWs8dWfVorhHDo1yXtKOKF1pUoUuUgQ3EvIFSuvV28381tSv7JqrjaSSn IK+ld3zVaRGO4t6KXftuArUlQoP2zdNX7w6Mzq4jADt1RWtTg3hS6GsqlqBgSQeQGiLf HaDuq2wddg0FfXvhTPudGp1nTqo51ZyR5rAlZvI46/9M3Nz6cSMWcuIM+h2QawODXiJo US7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=1oefpl2Q; 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 j189si36668017pgd.185.2018.11.19.02.32.42; Mon, 19 Nov 2018 02:32:44 -0800 (PST) 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=@nifty.com header.s=dec2015msa header.b=1oefpl2Q; 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 S1727744AbeKSUzw (ORCPT + 32 others); Mon, 19 Nov 2018 15:55:52 -0500 Received: from conuserg-11.nifty.com ([210.131.2.78]:59353 "EHLO conuserg-11.nifty.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727352AbeKSUzv (ORCPT ); Mon, 19 Nov 2018 15:55:51 -0500 Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-11.nifty.com with ESMTP id wAJAVrOC009918; Mon, 19 Nov 2018 19:31:54 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-11.nifty.com wAJAVrOC009918 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1542623515; bh=UxpwZPndNpZuNEqiuBMWdLIBfZYlxrIZKZ+04Pdc4gw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1oefpl2QWCyEz5uu0/A6mfOYMdJ0IDson0waVI4TK4a7e5WXqkCak10TdFlluYITQ O0gPBU8v77j6f0k9eJ5i7rfwx6Qsw96qh7of+O+6u8B9gtYbMLAzSUJLXpBWb9pUK7 YvmE8CD/uOf8h9r4JC5SAGmozsxqRu8Ywje2IAJe3Wc3Sn5YlsvFW+J3nnG6nc3HMw ZFNqGZfOYgblDQhwEABM6Oy2Poq/H4PK3j7jYcMSYJIy7BUQSLw0YI7n0HpybyRODc 4XjO/i9G1es3BrA4tBD3bs0dblcZQOMXogogxzbuvctDj3/mOaKw5q3d/E+7xUMSIn hGLlP5VwfDzTQ== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: Andrew Morton Cc: Luc Van Oostenryck , Nick Desaulniers , Kees Cook , Josh Triplett , Masahiro Yamada , linux-kernel@vger.kernel.org Subject: [PATCH v3 3/3] build_bug.h: remove most of dummy BUILD_BUG_ON stubs for sparse Date: Mon, 19 Nov 2018 19:31:43 +0900 Message-Id: <1542623503-3755-3-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1542623503-3755-1-git-send-email-yamada.masahiro@socionext.com> References: <1542623503-3755-1-git-send-email-yamada.masahiro@socionext.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The introduction of these dummy BUILD_BUG_ON stubs dates back to commit 903c0c7cdc21 ("sparse: define dummy BUILD_BUG_ON definition for sparse"). At that time, BUILD_BUG_ON() was implemented with the negative array trick *and* the link-time trick, like this: extern int __build_bug_on_failed; #define BUILD_BUG_ON(condition) \ do { \ ((void)sizeof(char[1 - 2*!!(condition)])); \ if (condition) __build_bug_on_failed = 1; \ } while(0) Sparse is more strict about the negative array trick than GCC because Sparse requires the array length to be really constant. Here is the simple test code for the macro above: static const int x = 0; BUILD_BUG_ON(x); GCC is absolutely fine with it (-Wvla was not enabled at that time), but Sparse warns like this: error: bad constant expression error: cannot size expression (If you are using a newer version of Sparse, you will see a different warning message, "warning: Variable length array is used".) Anyway, Sparse was producing many false positive warnings, hence silenced. With the previous commit, the leftover negative array trick is gone. Sparse is fine with the current BUILD_BUG_ON(), which is implemented by using the 'error' attribute. (assuming your Sparse version supports -Wno-unknown-attribute option) I am keeping the stub for BUILD_BUG_ON_ZERO(). Otherwise, Sparse would complain about the following code, which GCC is fine with: static const int x = 0; int y = BUILD_BUG_ON_ZERO(x); Signed-off-by: Masahiro Yamada Acked-by: Kees Cook --- Changes in v3: - Add Kees' Acked-by - Clarify log that BUILD_BUG_ON() *was* producing false positives - Keep the stub for BUILD_BUG_ON_ZERO() Changes in v2: - Fix a coding style error (two consecutive blank lines) include/linux/build_bug.h | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) -- 2.7.4 Reviewed-by: Luc Van Oostenryck Reviewed-by: Nick Desaulniers Tested-by: Nick Desaulniers diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h index d415c64..faeec74 100644 --- a/include/linux/build_bug.h +++ b/include/linux/build_bug.h @@ -5,21 +5,8 @@ #include #ifdef __CHECKER__ -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) (0) #define BUILD_BUG_ON_ZERO(e) (0) -#define BUILD_BUG_ON_INVALID(e) (0) -#define BUILD_BUG_ON_MSG(cond, msg) (0) -#define BUILD_BUG_ON(condition) (0) -#define BUILD_BUG() (0) #else /* __CHECKER__ */ - -/* Force a compilation error if a constant expression is not a power of 2 */ -#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ - BUILD_BUG_ON(((n) & ((n) - 1)) != 0) -#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ - BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) - /* * Force a compilation error if condition is true, but also produce a * result (of value 0 and type size_t), so the expression can be used @@ -27,6 +14,13 @@ * aren't permitted). */ #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:(-!!(e)); })) +#endif /* __CHECKER__ */ + +/* Force a compilation error if a constant expression is not a power of 2 */ +#define __BUILD_BUG_ON_NOT_POWER_OF_2(n) \ + BUILD_BUG_ON(((n) & ((n) - 1)) != 0) +#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ + BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) /* * BUILD_BUG_ON_INVALID() permits the compiler to check the validity of the @@ -64,6 +58,4 @@ */ #define BUILD_BUG() BUILD_BUG_ON_MSG(1, "BUILD_BUG failed") -#endif /* __CHECKER__ */ - #endif /* _LINUX_BUILD_BUG_H */