Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp903906pxb; Tue, 1 Feb 2022 12:50:43 -0800 (PST) X-Google-Smtp-Source: ABdhPJzu09JyTq31dlTsb9OTPuny2qxjAKrkBPjPaAWP1wOoHkGfELZ2iRqcFvOHXA+WNc6KhpBY X-Received: by 2002:a17:90a:e64e:: with SMTP id ep14mr4361459pjb.111.1643748642948; Tue, 01 Feb 2022 12:50:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643748642; cv=none; d=google.com; s=arc-20160816; b=XQcWb+4jZCqTXANndZsGpQn2fXlKi1HlyhSTnUhTZ4vFC1qNqOdDsV2j7GoPN9kaLf KOzk08oIV1t2h24elU9OQJrFLZfd9XGeiG9qJJZoylRsbXqH1HKPHwVdourIpaz6x8ap G51nde6H+VEOLh0s6SUwiT7J4LOJGRgjsZ1dmkZA1tmeket5HI6mjO925Rn/cWdBPlDE b3MPcY8axP8tvPNvKGGp5cKtbp0r1uhtmIM390CoXVRbOYQNmLWc+vr3iyqhPT2JfcwB 2fTvy/c6XCtzqCrYJWoisXW7ZekDWmIZHFToOuaNRU6pizgwDsYnBq5DsMg5r2yRsSKc pN1A== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=dvY93mLK+gCkLFyqPjkC7SO502H46e/BmHAFAde7pmA=; b=Q7IiHEeRZJlvPOHY8ulYpjekAqFUODWg+Fqlj7zNys85d4XtITDxtrfCtAco0CY6fA hb7WSATXsi8WyQARfhbqyTfCnrNJbPwZlbbSasCc0wbwVqw4q88cY6WkoiCyWloDv/pR A8WzLmU/XXzh1uVhm2hJkJEiLdFbm+HM4SOWhyFaKUIpUfM4FHOq+9WbtMnX1KPAa4GM H9mQepmA1f1o6QH1QONDflg2dr7yl+AgRdn0ByeZPz7Ta7puuCNfZdWhCUK2RmfLz7oo /ZUZAeOMj0hslXJyMOtu8nFqhhNkT76tYioIH+Aq9uXuMaQIP3/aONriDs2WKyD5dAZm 8Mkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=VnaPaLUE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d5si16388687pls.160.2022.02.01.12.50.31; Tue, 01 Feb 2022 12:50:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=VnaPaLUE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350845AbiAaUoB (ORCPT + 99 others); Mon, 31 Jan 2022 15:44:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38788 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240076AbiAaUoA (ORCPT ); Mon, 31 Jan 2022 15:44:00 -0500 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2356C06173D for ; Mon, 31 Jan 2022 12:43:59 -0800 (PST) Received: by mail-pf1-x42f.google.com with SMTP id v74so13923063pfc.1 for ; Mon, 31 Jan 2022 12:43:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dvY93mLK+gCkLFyqPjkC7SO502H46e/BmHAFAde7pmA=; b=VnaPaLUE79ByM8yxlggAAFwqCLbFIwHYSnwowqWnqMYsC3OgGiS1ToIUXd8N7wK5WS dIFaCM5pOMfQ1J/hUl7v9c5JoiyS6MZaxuMQw2gMtg7ucEwuKSG9xdMypFTCXm3IDQoA 7CeWl1HJ3RfJ+RvxNluYPe/tjQw4aj7Gfleog= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=dvY93mLK+gCkLFyqPjkC7SO502H46e/BmHAFAde7pmA=; b=QBBLOw+h+rMjmxpfhuIqWetMxNnJPEJutDC7vrklxqlnmfts7mNz9SdCOws/Bo2SwB I+Kp3hTbp9NloXSjwjUq3H+VlTX8JBwJioEcSqhz7Wl2/aMQXvF+7GbiCDf9MpZ/RqoK zjC5CdPaan//TCHwhPZI3W+Ve19dVW/9vEoLSTDmBFEhiL7KMbqBzNIumCCG/Vn5tS6r E8LUPwJ1UIbFmXQihVJ+qlLCNIpvdKH64ioWiQNFsN37KewctsEMpvxR/PLksIuQe86P iyJfWb2zvDZAY/5O19WlttitKmde1qddV5JqhtALnqHtWgjX8xGA69t0HbEKhIpJhZzh 1HJQ== X-Gm-Message-State: AOAM532lgtkIRy2oukmXap39WbJiU5B0XK1F9ITyUz8p9FMx2k3ZZVRk fUCYL41qg/T7gohjdKGqtZONow== X-Received: by 2002:a63:9346:: with SMTP id w6mr18350241pgm.65.1643661839508; Mon, 31 Jan 2022 12:43:59 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id h18sm19549618pfh.51.2022.01.31.12.43.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 12:43:59 -0800 (PST) From: Kees Cook To: Jonathan Corbet Cc: Kees Cook , Linus Torvalds , Martin Uecker , Ingo Molnar , Miguel Ojeda , Rikard Falkeborn , Arnd Bergmann , linux-doc@vger.kernel.org, Tetsuo Handa , Andrew Morton , Andy Shevchenko , Nick Desaulniers , "Gustavo A. R. Silva" , linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH] linux/const.h: Explain how __is_constexpr() works Date: Mon, 31 Jan 2022 12:43:57 -0800 Message-Id: <20220131204357.1133674-1-keescook@chromium.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2675; h=from:subject; bh=WlNK1x9BZiL8PdVBf+UDla5QbcDoyx4RBmBre4o9x84=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBh+EoMr8eEAxIe/o1NGdfWCDk8aFcQzwQVoNeu5FZ/ ZAhP5D+JAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCYfhKDAAKCRCJcvTf3G3AJtgoD/ sGCz9a8AssZAQB5fVsmWfX5b+plDMlTy+hmWrQ1KL04znEA2Ju1fh43Iv1MvyfWYxO06Oxl5ZFfMvs F9K+sfSnb5IE91FXjDx+mBjQ57kzHI9anob0KWKPxmIeCXfr1RFTTbLepyF7vQU83jlttFMvPGjiLV 1Okkt9dxBFoFXstRu9x/nx5v/El7bDRdrsk/nka+ZvuAFiqJQXL9RLAkuSgV+2l/P60RLOB3g6SFLs Nbf3EFlIUcN76X+QfhqcYHeohmywr0hCJtX8kvwhEpb0QSUocOPVXjZf6qDCI5HWDr/fNGtfHn1yEd RiQei0uJLVmH1EKHt2ZcahLv4LQyQq/cFG1u6VUnRJXVJRBL89zdqW8iiYirdHTmJ2wg4Ay8SJo69E 7tZpQ9HfQS9QwbIMeQwGYA8sHB90DoJgFehqMxnwoXDFJRCKSmIOzesglOo9B+4yqFJw3UnTeSldre rLSGF217GOQfzzcXXMgGRIeyxYRwwzqTyMGM5+U8SAlcswtDbakvP7aT9bctGV1LfUUUpI6RlicNK0 uOk07acYE93Cu2a5NUMH5GJhA8xb7SxT4Asp7IQZgzlTtBjcv2DYx4rMI/0VpbA4/Xa5b/gwaSWFgD DnHzo+Vxqd8CcB7ztHxX+Za2OqabUhA50217kdRVCT8PRtH0ZO+ZrdKqKHhw== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The __is_constexpr() macro is dark magic. Shed some light on it with a comment to explain how and why it works. Cc: Jonathan Corbet Cc: Linus Torvalds Cc: Martin Uecker Cc: Ingo Molnar Cc: Miguel Ojeda Cc: Rikard Falkeborn Cc: Arnd Bergmann Cc: linux-doc@vger.kernel.org Signed-off-by: Kees Cook --- Jon, since this is pure comment, do you want to take it through the docs tree? --- include/linux/const.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/const.h b/include/linux/const.h index 435ddd72d2c4..7122d6a1f8ce 100644 --- a/include/linux/const.h +++ b/include/linux/const.h @@ -7,6 +7,30 @@ * This returns a constant expression while determining if an argument is * a constant expression, most importantly without evaluating the argument. * Glory to Martin Uecker + * + * Details: + * - sizeof() is an integer constant expression, and does not evaluate the + * value of its operand; it only examines the type of its operand. + * - The results of comparing two integer constant expressions is also + * an integer constant expression. + * - The use of literal "8" is to avoid warnings about unaligned pointers; + * these could otherwise just be "1"s. + * - (long)(x) is used to avoid warnings about 64-bit types on 32-bit + * architectures. + * - The C standard defines an "integer constant expression" as different + * from a "null pointer constant" (an integer constant 0 pointer). + * - The conditional operator ("... ? ... : ...") returns the type of the + * operand that isn't a null pointer constant. This behavior is the + * central mechanism of the macro. + * - If (x) is an integer constant expression, then the "* 0l" resolves it + * into a null pointer constant, which forces the conditional operator + * to return the type of the last operand: "(int *)". + * - If (x) is not an integer constant expression, then the type of the + * conditional operator is from the first operand: "(void *)". + * - sizeof(int) == 4 and sizeof(void) == 1. + * - The ultimate comparison to "sizeof(int)" chooses between either: + * sizeof(*((int *) (8)) == sizeof(int) (x was a constant expression) + * sizeof(*((void *)(8)) == sizeof(void) (x was not a constant expression) */ #define __is_constexpr(x) \ (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8))) -- 2.30.2