From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web10.36976.1679468581485252140 for ; Wed, 22 Mar 2023 00:03:01 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XpYy5qD0; spf=pass (domain: redhat.com, ip: 170.10.133.124, mailfrom: kraxel@redhat.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679468580; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=I+7fyLqF/54KGRh53iCyqFluKcZOIpzB4wRm9GhWirI=; b=XpYy5qD0dtYuwJ3NRTewmtOrowJL/fIaRoRhdxJlAF/cXmFGEaRqQgsHTSoAVvKkknPcYo zyieZxwncmnt+Ouki7GSyaYrEf58XESNOkS94JgulnPPbZUJiTfDZg1/q3q+RYHbqpgRU8 J8wEITyociBhzKiQrvN/Nn5U6lDj+j4= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-416-EU_sat1KPbOI7qw6J-7nTQ-1; Wed, 22 Mar 2023 03:02:55 -0400 X-MC-Unique: EU_sat1KPbOI7qw6J-7nTQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D79923C5426F; Wed, 22 Mar 2023 07:02:54 +0000 (UTC) Received: from sirius.home.kraxel.org (unknown [10.39.192.53]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 581E92027040; Wed, 22 Mar 2023 07:02:54 +0000 (UTC) Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id C62B7180039F; Wed, 22 Mar 2023 08:02:43 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Michael D Kinney , Erdem Aktas , James Bottomley , Liming Gao , =?UTF-8?q?Marvin=20H=C3=A4user?= , Pawel Polawski , Michael Roth , Tom Lendacky , Gerd Hoffmann , Jiewen Yao , Jian J Wang , Ard Biesheuvel , Jordan Justen , Hao A Wu , Zhiguang Liu , Ray Ni , Min Xu , Oliver Steffen , Vitaly Cheptsov Subject: [PATCH v2 3/6] MdePkg/Base.h: Introduce various alignment-related macros Date: Wed, 22 Mar 2023 08:02:40 +0100 Message-Id: <20230322070243.410903-4-kraxel@redhat.com> In-Reply-To: <20230322070243.410903-1-kraxel@redhat.com> References: <20230322070243.410903-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Marvin Häuser ALIGNOF: Determining the alignment requirement of data types is crucial to ensure safe memory accesses when parsing untrusted data. IS_POW2: Determining whether a value is a power of two is important to verify whether untrusted values are valid alignment values. IS_ALIGNED: In combination with ALIGNOF data offsets can be verified. A more general version of the IS_ALIGNED macro previously defined by several modules. ADDRESS_IS_ALIGNED: Variant of IS_ALIGNED for pointers and addresses. Replaces module-specific definitions throughout the codebase. ALIGN_VALUE_ADDEND: The addend to align up can be used to directly determine the required offset for data alignment. Cc: Michael D Kinney Cc: Liming Gao Cc: Zhiguang Liu Cc: Vitaly Cheptsov Signed-off-by: Marvin Häuser Signed-off-by: Gerd Hoffmann --- MdePkg/Include/Base.h | 98 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/MdePkg/Include/Base.h b/MdePkg/Include/Base.h index e89c84962ab2..6597e441a6e2 100644 --- a/MdePkg/Include/Base.h +++ b/MdePkg/Include/Base.h @@ -758,6 +758,40 @@ typedef UINTN *BASE_LIST; #define OFFSET_OF(TYPE, Field) ((UINTN) &(((TYPE *)0)->Field)) #endif +/** + Returns the alignment requirement of a type. + + @param TYPE The name of the type to retrieve the alignment requirement of. + + @return Alignment requirement, in Bytes, of TYPE. +**/ +#if defined (__cplusplus) +// +// Standard C++ operator. +// +#define ALIGNOF(TYPE) alignof (TYPE) +#elif defined (__GNUC__) || defined (__clang__) || (defined (_MSC_VER) && _MSC_VER >= 1900) +// +// All supported versions of GCC and Clang, as well as MSVC 2015 and later, +// support the standard operator _Alignof. +// +#define ALIGNOF(TYPE) _Alignof (TYPE) +#elif defined (_MSC_EXTENSIONS) +// +// Earlier versions of MSVC, at least MSVC 2008 and later, support the vendor +// extension __alignof. +// +#define ALIGNOF(TYPE) __alignof (TYPE) +#else +// +// For compilers that do not support inbuilt alignof operators, use OFFSET_OF. +// CHAR8 is known to have both a size and an alignment requirement of 1 Byte. +// As such, A must be located exactly at the offset equal to its alignment +// requirement. +// +#define ALIGNOF(TYPE) OFFSET_OF (struct { CHAR8 C; TYPE A; }, A) +#endif + /** Portable definition for compile time assertions. Equivalent to C11 static_assert macro from assert.h. @@ -793,6 +827,21 @@ STATIC_ASSERT (sizeof (CHAR16) == 2, "sizeof (CHAR16) does not meet UEFI Specif STATIC_ASSERT (sizeof (L'A') == 2, "sizeof (L'A') does not meet UEFI Specification Data Type requirements"); STATIC_ASSERT (sizeof (L"A") == 4, "sizeof (L\"A\") does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (BOOLEAN) == sizeof (BOOLEAN), "Alignment of BOOLEAN does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT8) == sizeof (INT8), "Alignment of INT8 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT8) == sizeof (UINT8), "Alignment of INT16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT16) == sizeof (INT16), "Alignment of INT16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT16) == sizeof (UINT16), "Alignment of UINT16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT32) == sizeof (INT32), "Alignment of INT32 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT32) == sizeof (UINT32), "Alignment of UINT32 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INT64) == sizeof (INT64), "Alignment of INT64 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINT64) == sizeof (UINT64), "Alignment of UINT64 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (CHAR8) == sizeof (CHAR8), "Alignment of CHAR8 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (CHAR16) == sizeof (CHAR16), "Alignment of CHAR16 does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (INTN) == sizeof (INTN), "Alignment of INTN does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (UINTN) == sizeof (UINTN), "Alignment of UINTN does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (VOID *) == sizeof (VOID *), "Alignment of VOID * does not meet UEFI Specification Data Type requirements"); + // // The following three enum types are used to verify that the compiler // configuration for enum types is compliant with Section 2.3.1 of the @@ -816,6 +865,10 @@ STATIC_ASSERT (sizeof (__VERIFY_UINT8_ENUM_SIZE) == 4, "Size of enum does not me STATIC_ASSERT (sizeof (__VERIFY_UINT16_ENUM_SIZE) == 4, "Size of enum does not meet UEFI Specification Data Type requirements"); STATIC_ASSERT (sizeof (__VERIFY_INT32_ENUM_SIZE) == 4, "Size of enum does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (__VERIFY_UINT8_ENUM_SIZE) == sizeof (__VERIFY_UINT8_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (__VERIFY_UINT16_ENUM_SIZE) == sizeof (__VERIFY_UINT16_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Data Type requirements"); +STATIC_ASSERT (ALIGNOF (__VERIFY_INT32_ENUM_SIZE) == sizeof (__VERIFY_INT32_ENUM_SIZE), "Alignment of enum does not meet UEFI Specification Data Type requirements"); + /** Macro that returns a pointer to the data structure that contains a specified field of that data structure. This is a lightweight method to hide information by placing a @@ -837,6 +890,49 @@ STATIC_ASSERT (sizeof (__VERIFY_INT32_ENUM_SIZE) == 4, "Size of enum does not me **/ #define BASE_CR(Record, TYPE, Field) ((TYPE *) ((CHAR8 *) (Record) - OFFSET_OF (TYPE, Field))) +/** + Checks whether a value is a power of two. + + @param Value The value to check. + + @retval TRUE Value is a power of two. + @retval FALSE Value is not a power of two. +**/ +#define IS_POW2(Value) ((Value) != 0U && ((Value) & ((Value) - 1U)) == 0U) + +/** + Checks whether a value is aligned by a specified alignment. + + @param Value The value to check. + @param Alignment The alignment boundary used to check against. + + @retval TRUE Value is aligned by Alignment. + @retval FALSE Value is not aligned by Alignment. +**/ +#define IS_ALIGNED(Value, Alignment) (((Value) & ((Alignment) - 1U)) == 0U) + +/** + Checks whether a pointer or address is aligned by a specified alignment. + + @param Address The pointer or address to check. + @param Alignment The alignment boundary used to check against. + + @retval TRUE Address is aligned by Alignment. + @retval FALSE Address is not aligned by Alignment. +**/ +#define ADDRESS_IS_ALIGNED(Address, Alignment) IS_ALIGNED ((UINTN) (Address), Alignment) + +/** + Determines the addend to add to a value to round it up to the next boundary of + a specified alignment. + + @param Value The value to round up. + @param Alignment The alignment boundary used to return the addend. + + @return Addend to round Value up to alignment boundary Alignment. +**/ +#define ALIGN_VALUE_ADDEND(Value, Alignment) (((Alignment) - (Value)) & ((Alignment) - 1U)) + /** Rounds a value up to the next boundary using a specified alignment. @@ -849,7 +945,7 @@ STATIC_ASSERT (sizeof (__VERIFY_INT32_ENUM_SIZE) == 4, "Size of enum does not me @return A value up to the next boundary. **/ -#define ALIGN_VALUE(Value, Alignment) ((Value) + (((Alignment) - (Value)) & ((Alignment) - 1))) +#define ALIGN_VALUE(Value, Alignment) ((Value) + ALIGN_VALUE_ADDEND (Value, Alignment)) /** Adjust a pointer by adding the minimum offset required for it to be aligned on -- 2.39.2