Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp175072lqg; Fri, 1 Mar 2024 01:32:36 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUmD1Lbg9rwzb5zQqX5bDg599eTUuVL6FPHlOtQL+YDk8rs+zQDl5dJZhx8TflST/AuCw8ZNL9kO9rsResMkax0iE5BQn9WTo0TrsYKGA== X-Google-Smtp-Source: AGHT+IHmXEqcuD8O8+EoNQ/bQ0WsctoAwP6ZCDO9KZdSCy7uzeInALD31xL82juTUE8WriAEiIkG X-Received: by 2002:a05:6870:230b:b0:21e:bbdc:cc80 with SMTP id w11-20020a056870230b00b0021ebbdccc80mr1153935oao.8.1709285556213; Fri, 01 Mar 2024 01:32:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709285556; cv=pass; d=google.com; s=arc-20160816; b=xowDP4ZXervESrfQF8wBuLjsVyrsbbFsctCe27VOnHN5KrmvFMlAivHfE8639z8aYB /2IWBakP1dTYNi/IG/tB39e/dea74uq1pUmF2JDE8TbeheFpgPRK2GzCTSr79kHvYNTQ fLZKPBWTfSpWPCxaKV4qsqYtyDRui/fWbPPHHgxBnAYbLhios5nmL4Cu50ogB2Xfw+/Z Lu0uL1U5fZtCu1OEmR6guRGHmZkHBksVhGi4AM7lgVUXIWng4FLRhOCA1Mj2br6MnIch R+H3OhVRGz5EW9keCTq4+MRqke8cZel1euFU5uFK7KSFLKFIFRlCm2WeZFZIWdCADyd7 Pv9Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:accept-language :in-reply-to:references:message-id:date:thread-index:thread-topic :subject:cc:to:from; bh=pY/JAuUkdZ3AyIyGff0Ip5uQfvmqurSeKSVYl4idjR0=; fh=b/6vixMZwXrPPH05+h3Iypv+acUqwDYwzuFPu8o9BjQ=; b=lTXEi8R3q+x0rOXhVEyZoGmCXOovU1HWJw+uToQkPEsPM2FEVzWhciRAHKNhCTn4Xr A1HuGMirnKlV2Z2y9K+bPVmXKiXBk6ytlRm3WDBz/+1fsPyv2LScg67w4bHv0HFF2vZS 0pia8t9emQkMhOGycOmqRgk9fjA2s3Dn0JIe169Rdqu3dHIaCbigW2X3a9L8o5ZATX3P RfzpsXX55EpeOzinQ3LBrQiLIeKWKqjLlxspxnA4VOEINvQOwMHULf96FTwaVqv2h7xZ jMa6jSYH2yxfUJMANp2SV8UrYy6VJTJQRtWiUCIHe39w4mhfZ5Fho9X4/McIFpFg1RgB 2jqw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-88188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ay5-20020a056a00300500b006e59c2be34dsi1192388pfb.100.2024.03.01.01.32.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Mar 2024 01:32:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-88188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=aculab.com dmarc=pass fromdomain=aculab.com); spf=pass (google.com: domain of linux-kernel+bounces-88188-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-88188-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 34F362843F5 for ; Fri, 1 Mar 2024 09:32:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 260A96A8B9; Fri, 1 Mar 2024 09:32:23 +0000 (UTC) Received: from eu-smtp-delivery-151.mimecast.com (eu-smtp-delivery-151.mimecast.com [185.58.85.151]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D6B946A34D for ; Fri, 1 Mar 2024 09:32:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.58.85.151 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709285542; cv=none; b=VMjyV1t0ghaLn9HytA77CtUtLtld9YkdMEd+afdl5ohIM6Kr13D/eLWgccmtJZdWxk+0i4687Ld0iStAmZiXguNT05F6v7TnqQlF6zkZA8nE8g2mDM/6TqtTSMIiveM9yFWWDDdp16f8dJ7NwE2qN70gyiuKhAVFn96LMuu9+hk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709285542; c=relaxed/simple; bh=pY/JAuUkdZ3AyIyGff0Ip5uQfvmqurSeKSVYl4idjR0=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: MIME-Version:Content-Type; b=H9pCv8Vz8xFBbUl2+xm5CRUS6S9MZZnr1qczY6lSE5dFmwO/FBLzmebQgicR6YNscJtSTWmZb7vQd9vu6mS0brT9s5FWR+oJ/kBHxNLUk4v5LxE2OER9NBmjOUdzieCUqYz7xcJ7jHCZS24VQz4qnPrQp+Q/pGGlRRZ2QpWYRkA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM; spf=pass smtp.mailfrom=aculab.com; arc=none smtp.client-ip=185.58.85.151 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=ACULAB.COM Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=aculab.com Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) by relay.mimecast.com with ESMTP with both STARTTLS and AUTH (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id uk-mta-144-cAe80tt0OtKldFFkrcHoig-1; Fri, 01 Mar 2024 09:32:17 +0000 X-MC-Unique: cAe80tt0OtKldFFkrcHoig-1 Received: from AcuMS.Aculab.com (10.202.163.6) by AcuMS.aculab.com (10.202.163.6) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 1 Mar 2024 09:32:16 +0000 Received: from AcuMS.Aculab.com ([::1]) by AcuMS.aculab.com ([::1]) with mapi id 15.00.1497.048; Fri, 1 Mar 2024 09:32:16 +0000 From: David Laight To: 'Kees Cook' , Rasmus Villemoes CC: "Gustavo A . R . Silva" , Miguel Ojeda , Jani Nikula , Nick Desaulniers , Martin Uecker , Jonathan Corbet , "linux-doc@vger.kernel.org" , Miguel Ojeda , "linux-kernel@vger.kernel.org" , "linux-hardening@vger.kernel.org" Subject: RE: [PATCH] compiler.h: Explain how __is_constexpr() works Thread-Topic: [PATCH] compiler.h: Explain how __is_constexpr() works Thread-Index: AQHaa5MvghvgXFQBskOAdfSAj6aDybEilNag Date: Fri, 1 Mar 2024 09:32:16 +0000 Message-ID: References: <20240301044428.work.411-kees@kernel.org> In-Reply-To: <20240301044428.work.411-kees@kernel.org> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable From: Kees Cook > Sent: 01 March 2024 04:45 > To: Rasmus Villemoes >=20 > The __is_constexpr() macro is dark magic. Shed some light on it with > a comment to explain how and why it works. All the 8s don't help... I don't think you need that much explanation. Perhaps just saying that the type of ?: depends on the types of the values and is independent of the condition. The type of (0 ? (void *)p : (foo *)q) is normally 'void *' (so that both values can be assigned to it). But if 'p' is 'an integer constant expression with value 0' then (void *)p is NULL and the type is 'foo *'. The type can then be checked to find out it 'p' is constant 0. A non-zero constant 'p' can be multiples by 0. I need to replace the definition with (the more portable): #define __if_constexpr(cond, if_const, if_not_const) \ =09_Generic(0 ? (void *)((long)(cond) * 0) : (char *)0, \ =09=09char *: (if_const), \ =09=09void *: (if_not_const)) which is arguably less cryptic. #define __is_constexpr(cond) __if_constexpr(cond, 1, 0) So that I can write: #define is_non_neg_const(x) (__if_constexpr(x, x , -1) >=3D 0) and avoid the compiler bleating about some comparisons in unreachable code. =09David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1= PT, UK Registration No: 1397386 (Wales)