Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1794681rwn; Fri, 9 Sep 2022 04:18:33 -0700 (PDT) X-Google-Smtp-Source: AA6agR5WKte1paaMGiNWF6DqTBBHcBcIoydlL6PEuGGtDiW4eRQZtV6KxagNahTm6kmdnnqvOcG5 X-Received: by 2002:a63:86c6:0:b0:434:fe98:9f8a with SMTP id x189-20020a6386c6000000b00434fe989f8amr9376832pgd.541.1662722313329; Fri, 09 Sep 2022 04:18:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662722313; cv=none; d=google.com; s=arc-20160816; b=ZuHWlOHv3AmCsw3jV2gHeIGjarISdp+MStc0PN6CbjmLbbbX3TNy9J9yG8tU8XP2sS xOMZm1c/xufwZ8Awi9xm4oTLsRRIsM0j09G/a1A0wBJ9LeYv7VmeojKDDoW7H2u7itsD 8KJNoPpHkDUK/bUujSKdT+aZAGy37Pux2zcdmBQiwJk1nMbt+ZJ6Da+xJkpKAgyCYNEE B3IiA2+vWVeYxPGOQRuAefBNjrDpmP4SLarQap0jGmm3s9QqYcamzQV8pDzTIXsIrLC1 XnQ+xtmQERGGKDJqqhZ5zzUb0T1dXgs2t8L++8jyIk4DmSPNfh5fOGoXbRzfTvEKJVT6 mIZQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uZ2jx/RrEMDtLMDNW2lSaYygYLs6UutQIOA+ZzWYOl8=; b=JBN0zo+oyO34BlWQD8HBV4VhHZ4cFHq8eQv2H2X8GLExpDnKvz4Gf0STUBhRf1FQ2I FTKQoYZQmxYyIdABQb9FgpV75xLLlm2252x3ZcgzEsNbU4SdrgdYGVFViWevDovMo0Jf 6fmpr0FpRe+0kRB7nInBVTFwyCQEUQwkAjOS4WEF3Jvw5GBi/OTDLfSAjpsSNxhuDQXU EOYDAG8Z/ruvhQAhb5vk6lKRPuu9wpyiIw4OmdH8SNOiv1AeiDZSnmvbLDtmDsYvCOqH N3C6Er4FF1alc4CXxfd1zKTVt6KxiM6CciuaXgAVa6vVaoL/qkY77s6OXubs7eUVU3lv r3UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ct8QHmet; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g9-20020a170902868900b0016d69207784si281708plo.162.2022.09.09.04.18.21; Fri, 09 Sep 2022 04:18:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ct8QHmet; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229637AbiIILAB (ORCPT + 99 others); Fri, 9 Sep 2022 07:00:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45352 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbiIIK7t (ORCPT ); Fri, 9 Sep 2022 06:59:49 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 752C72B252 for ; Fri, 9 Sep 2022 03:59:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1662721188; x=1694257188; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=sw25/VEVNzYbUpj2DtWImbIBzsU8rdEcHgaL9vecVhE=; b=ct8QHmetWfR8WzJaq7kqPk/4t+uAhLFr1UkjyVveHgMAD5JVIgA+SShy LW/hQ2LrR7q8STv7zxfJq+yCqOGsBaxriw1xY+O2OQ5vc1Tew7RX9cNsL kmf0GMXA0+PIfjPZOhdO1H1zHtpiRcql2mnx1QkogYkt7z5UDLXf2MkKx 3Q5JAJ47r4S1yDZtHUvP4xq/Txvmq1tD84OlZb+GkgP9hhWmyCHc765oN dBdBLzDU8grxiYXXLbbOFytD6KnOVSs+ISsMqAoTUbGqE2WypQL0/lpQM MUy6xLWaxo2wrLBRl7JHtW9pkKoU1sQ6WYFceXoftNNvzQEg44lsZTU69 A==; X-IronPort-AV: E=McAfee;i="6500,9779,10464"; a="297449535" X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="297449535" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 03:59:48 -0700 X-IronPort-AV: E=Sophos;i="5.93,303,1654585200"; d="scan'208";a="704370341" Received: from clbarnes-mobl.amr.corp.intel.com (HELO paris.amr.corp.intel.com) ([10.254.42.92]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Sep 2022 03:59:44 -0700 From: Gwan-gyeong Mun To: intel-gfx@lists.freedesktop.org Cc: linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, mchehab@kernel.org, chris@chris-wilson.co.uk, matthew.auld@intel.com, thomas.hellstrom@linux.intel.com, jani.nikula@intel.com, nirmoy.das@intel.com, airlied@redhat.com, daniel@ffwll.ch, andi.shyti@linux.intel.com, andrzej.hajda@intel.com, keescook@chromium.org, mauro.chehab@linux.intel.com, linux@rasmusvillemoes.dk, vitor@massaru.org, dlatypov@google.com, ndesaulniers@google.com Subject: [PATCH v10 3/9] compiler_types.h: Add assert_type to catch type mis-match while compiling Date: Fri, 9 Sep 2022 19:59:07 +0900 Message-Id: <20220909105913.752049-4-gwan-gyeong.mun@intel.com> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220909105913.752049-1-gwan-gyeong.mun@intel.com> References: <20220909105913.752049-1-gwan-gyeong.mun@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org It adds assert_type and assert_typable macros to catch type mis-match while compiling. The existing typecheck() macro outputs build warnings, but the newly added assert_type() macro uses the _Static_assert() keyword (which is introduced in C11) to generate a build break when the types are different and can be used to detect explicit build errors. Unlike the assert_type() macro, assert_typable() macro allows a constant value as the second argument. Suggested-by: Kees Cook Signed-off-by: Gwan-gyeong Mun Cc: Thomas Hellström Cc: Matthew Auld Cc: Nirmoy Das Cc: Jani Nikula Cc: Andi Shyti Cc: Mauro Carvalho Chehab Cc: Andrzej Hajda Cc: Kees Cook --- include/linux/compiler_types.h | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h index 4f2a819fd60a..19cc125918bb 100644 --- a/include/linux/compiler_types.h +++ b/include/linux/compiler_types.h @@ -294,6 +294,45 @@ struct ftrace_likely_data { /* Are two types/vars the same type (ignoring qualifiers)? */ #define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) +/** + * assert_type - break compile if the first argument's data type and the second + * argument's data type are not the same + * + * @t1: data type or variable + * @t2: data type or variable + * + * The first and second arguments can be data types or variables or mixed (the + * first argument is the data type and the second argument is variable or vice + * versa). It determines whether the first argument's data type and the second + * argument's data type are the same while compiling, and it breaks compile if + * the two types are not the same. + * See also assert_typable(). + */ +#define assert_type(t1, t2) _Static_assert(__same_type(t1, t2)) + +/** + * assert_typable - break compile if the first argument's data type and the + * second argument's data type are not the same + * + * @t: data type or variable + * @n: data type or variable or constant value + * + * The first and second arguments can be data types or variables or mixed (the + * first argument is the data type and the second argument is variable or vice + * versa). Unlike the assert_type() macro, this macro allows a constant value + * as the second argument. And if the second argument is a constant value, it + * always passes. And it doesn't mean that the types are explicitly the same. + * When a constant value is used as the second argument, if you need an + * overflow check when assigning a constant value to a variable of the type of + * the first argument, you can use the overflows_type() macro. When a constant + * value is not used as a second argument, it determines whether the first + * argument's data type and the second argument's data type are the same while + * compiling, and it breaks compile if the two types are not the same. + * See also assert_type() and overflows_type(). + */ +#define assert_typable(t, n) _Static_assert(__builtin_constant_p(n) || \ + __same_type(t, typeof(n))) + /* * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving * non-scalar types unchanged. -- 2.37.1