From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from rn-mailsvcp-ppex-lapp44.apple.com (rn-mailsvcp-ppex-lapp44.apple.com [17.179.253.48]) by mx.groups.io with SMTP id smtpd.web08.29447.1628544789360283433 for ; Mon, 09 Aug 2021 14:33:09 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@apple.com header.s=20180706 header.b=C4k9fmas; spf=pass (domain: apple.com, ip: 17.179.253.48, mailfrom: afish@apple.com) Received: from pps.filterd (rn-mailsvcp-ppex-lapp44.rno.apple.com [127.0.0.1]) by rn-mailsvcp-ppex-lapp44.rno.apple.com (8.16.1.2/8.16.1.2) with SMTP id 179LW3hg010930; Mon, 9 Aug 2021 14:32:54 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=apple.com; h=from : message-id : content-type : mime-version : subject : date : in-reply-to : cc : to : references; s=20180706; bh=baE0ZArEq0VCkf03aeUxQAURJlq3Nrwv4PVVi2kE9LM=; b=C4k9fmas+gSMjEnlX2p7VYtlqjJ33dkPEuLuACHGBjFAN2tzofmve6Vlri5evhCqV6UD 4bpo4ER9MZPLEz+uJWpTrBz98aAe+sDkC5pY8e2UesVCo4O9XUyg+F0QdkVAkF+cYT3O MFTira96ume23pA7I4FSR7D7x8tNcW6rxYT1c+1XSLtoORbUj6q3OsvZDX37B0dpo4Ey x5s62N+wx0qoj9lk5pgPg956UFkNtk8fWAOqQXV+t8bcwH4xNrM3HRKGacQyKiCwY4+H JS2kqK2WoDRrzqScHFc/GZa9f7bh3LfeUDF8PyCd+1VK7EolH8kDkBuc36X/LTdI87jp 9w== Received: from rn-mailsvcp-mta-lapp04.rno.apple.com (rn-mailsvcp-mta-lapp04.rno.apple.com [10.225.203.152]) by rn-mailsvcp-ppex-lapp44.rno.apple.com with ESMTP id 3a9np92wjb-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NO); Mon, 09 Aug 2021 14:32:54 -0700 Received: from rn-mailsvcp-mmp-lapp03.rno.apple.com (rn-mailsvcp-mmp-lapp03.rno.apple.com [17.179.253.16]) by rn-mailsvcp-mta-lapp04.rno.apple.com (Oracle Communications Messaging Server 8.1.0.9.20210415 64bit (built Apr 15 2021)) with ESMTPS id <0QXL00O98D6TF700@rn-mailsvcp-mta-lapp04.rno.apple.com>; Mon, 09 Aug 2021 14:32:54 -0700 (PDT) Received: from process_milters-daemon.rn-mailsvcp-mmp-lapp03.rno.apple.com by rn-mailsvcp-mmp-lapp03.rno.apple.com (Oracle Communications Messaging Server 8.1.0.9.20210415 64bit (built Apr 15 2021)) id <0QXL00B00D0E9U00@rn-mailsvcp-mmp-lapp03.rno.apple.com>; Mon, 09 Aug 2021 14:32:53 -0700 (PDT) X-Va-A: X-Va-T-CD: 70a38c3f5b1d46c4b8dccb3b011be358 X-Va-E-CD: 8b34a1551387158475b014d243cdc9ad X-Va-R-CD: 6def9d696c04dce24c572ed8b427a545 X-Va-CD: 0 X-Va-ID: 2db4a313-b5bd-403c-890a-ab8f3191666b X-V-A: X-V-T-CD: 70a38c3f5b1d46c4b8dccb3b011be358 X-V-E-CD: 8b34a1551387158475b014d243cdc9ad X-V-R-CD: 6def9d696c04dce24c572ed8b427a545 X-V-CD: 0 X-V-ID: 9cc9cbd7-b122-4c12-9514-b653e8ff8a8e X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.790 definitions=2021-08-09_08:2021-08-06,2021-08-09 signatures=0 Received: from [17.235.18.207] (unknown [17.235.18.207]) by rn-mailsvcp-mmp-lapp03.rno.apple.com (Oracle Communications Messaging Server 8.1.0.9.20210415 64bit (built Apr 15 2021)) with ESMTPSA id <0QXL0068VD6R8O00@rn-mailsvcp-mmp-lapp03.rno.apple.com>; Mon, 09 Aug 2021 14:32:53 -0700 (PDT) From: "Andrew Fish" Message-id: MIME-version: 1.0 (Mac OS X Mail 14.0 \(3654.20.0.2.1\)) Subject: Re: [edk2-devel] [PATCH v2 1/2] MdePkg/BaseLib: Fix unaligned API prototypes Date: Mon, 09 Aug 2021 14:32:51 -0700 In-reply-to: Cc: =?utf-8?Q?Marvin_H=C3=A4user?= , Liming Gao , "Liu, Zhiguang" , Vitaly Cheptsov To: edk2-devel-groups-io , Mike Kinney References: X-Mailer: Apple Mail (2.3654.20.0.2.1) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.790 definitions=2021-08-09_08:2021-08-06,2021-08-09 signatures=0 Content-type: multipart/alternative; boundary="Apple-Mail=_2BDE6EE9-72B9-46DA-A44D-6D8B03042EB8" --Apple-Mail=_2BDE6EE9-72B9-46DA-A44D-6D8B03042EB8 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Aug 9, 2021, at 9:15 AM, Michael D Kinney = wrote: >=20 > Hi Marvin, >=20 > Can you provide an example of which C compiler is flagging this as > an error and what error message is generated. >=20 > Please enter a BZ with this background information and add link to the > BZ in the commit message. >=20 > This is a change to the BaseLib class, so we need to make sure there > are no impacts to any existing code. I looks like a safe change > because changing from a pointer to a fixed size type to VOID *=20 > should be compatible. Please add that analysis to the background > in the BZ as well. >=20 MIke, I want to say we had a discussion about this years ago? I don=E2=80=99t rem= ember the outcome.=20 Dereferencing a misaligned pointer is UB (Undefined Behavior) in C [1], but= historically x86 compilers have let it slide. I think the situation we are in is the BaseLib functions don=E2=80=99t cont= ain UB, but it is UB for the caller to use the returned pointer directly.= =20 Here is a simple example with clang UndefinedBehaviorSanitizer (UBSan) .=20 ~/work/Compiler>cat ub.c #include #define EFIAPI #define IN #define OUT typedef unsigned char UINT8; typedef unsigned short UINT16; UINT16 EFIAPI WriteUnaligned16 ( OUT UINT16 *Buffer, IN UINT16 Value ) { // ASSERT (Buffer !=3D NULL); ((volatile UINT8*)Buffer)[0] =3D (UINT8)Value; ((volatile UINT8*)Buffer)[1] =3D (UINT8)(Value >> 8); return Value; } int main() { UINT8 *buffer =3D malloc(64); UINT16 *pointer =3D (UINT16 *)(buffer + 1); =09 WriteUnaligned16 (pointer, 42); =09 // *pointer =3D 42; // Error: misaligned integer pointer assignment return *pointer; } ~/work/Compiler>clang -fsanitize=3Dundefined ub.c ~/work/Compiler>./a.out ub.c:34:9: runtime error: load of misaligned address 0x7feac6405aa1 for typ= e 'UINT16' (aka 'unsigned short'), which requires 2 byte alignment 0x7feac6405aa1: note: pointer points here 00 00 00 64 2a 00 79 6d 28 52 54 4c 44 5f 44 45 46 41 55 4c 54 2c 20 73= 77 69 66 74 5f 64 65 6d ^=20 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ub.c:34:9 in=20 FYI line 39 is `return *pointer`and 42 is 0x2A. So reading an writing to *p= ointer is UB.=20 As you can see in [1] the general advice is to take code that looks like: int8_t *buffer =3D malloc(64); int32_t *pointer =3D (int32_t *)(buffer + 1); *pointer =3D 42; // Error: misaligned integer pointer assignment And replace it with; int8_t *buffer =3D malloc(64); int32_t value =3D 42; memcpy(buffer + 1, &value, sizeof(int32_t)); // Correct But in these cases the result is in a byte aligned buffer=E2=80=A6. [1] https://developer.apple.com/documentation/xcode/misaligned-pointer Thanks, Andrew Fish > Thanks, >=20 > Mike >=20 >=20 >> -----Original Message----- >> From: Marvin H=C3=A4user = > >> Sent: Monday, August 9, 2021 2:51 AM >> To: devel@edk2.groups.io >> Cc: Kinney, Michael D >; Liming Gao >; Liu, Zhiguang >> >; Vitaly Cheptso= v > >> Subject: [PATCH v2 1/2] MdePkg/BaseLib: Fix unaligned API prototypes >>=20 >> C prohibits not only dereferencing but also casting to unaligned >> pointers. Thus, the current set of unaligned APIs cannot be called >> safely. Update their prototypes to take VOID * pointers, which must >> be able to represent any valid pointer. >>=20 >> Cc: Michael D Kinney >> Cc: Liming Gao >> Cc: Zhiguang Liu >> Cc: Vitaly Cheptsov >> Signed-off-by: Marvin H=C3=A4user >> --- >> MdePkg/Library/BaseLib/Arm/Unaligned.c | 14 ++++----- >> MdePkg/Library/BaseLib/Unaligned.c | 32 ++++++++++---------- >> MdePkg/Include/Library/BaseLib.h | 16 +++++----- >> 3 files changed, 31 insertions(+), 31 deletions(-) >>=20 >> diff --git a/MdePkg/Library/BaseLib/Arm/Unaligned.c b/MdePkg/Library/Bas= eLib/Arm/Unaligned.c >> index e9934e7003cb..57f19fc44e0b 100644 >> --- a/MdePkg/Library/BaseLib/Arm/Unaligned.c >> +++ b/MdePkg/Library/BaseLib/Arm/Unaligned.c >> @@ -59,7 +59,7 @@ ReadUnaligned16 ( >> UINT16 >>=20 >> EFIAPI >>=20 >> WriteUnaligned16 ( >>=20 >> - OUT UINT16 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT16 Value >>=20 >> ) >>=20 >> { >>=20 >> @@ -87,7 +87,7 @@ WriteUnaligned16 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> ReadUnaligned24 ( >>=20 >> - IN CONST UINT32 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >> @@ -116,7 +116,7 @@ ReadUnaligned24 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> WriteUnaligned24 ( >>=20 >> - OUT UINT32 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT32 Value >>=20 >> ) >>=20 >> { >>=20 >> @@ -143,7 +143,7 @@ WriteUnaligned24 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> ReadUnaligned32 ( >>=20 >> - IN CONST UINT32 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ) >>=20 >> { >>=20 >> UINT16 LowerBytes; >>=20 >> @@ -175,7 +175,7 @@ ReadUnaligned32 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> WriteUnaligned32 ( >>=20 >> - OUT UINT32 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT32 Value >>=20 >> ) >>=20 >> { >>=20 >> @@ -202,7 +202,7 @@ WriteUnaligned32 ( >> UINT64 >>=20 >> EFIAPI >>=20 >> ReadUnaligned64 ( >>=20 >> - IN CONST UINT64 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ) >>=20 >> { >>=20 >> UINT32 LowerBytes; >>=20 >> @@ -234,7 +234,7 @@ ReadUnaligned64 ( >> UINT64 >>=20 >> EFIAPI >>=20 >> WriteUnaligned64 ( >>=20 >> - OUT UINT64 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT64 Value >>=20 >> ) >>=20 >> { >>=20 >> diff --git a/MdePkg/Library/BaseLib/Unaligned.c b/MdePkg/Library/BaseLib= /Unaligned.c >> index a419cb85e53c..3041adcde606 100644 >> --- a/MdePkg/Library/BaseLib/Unaligned.c >> +++ b/MdePkg/Library/BaseLib/Unaligned.c >> @@ -26,12 +26,12 @@ >> UINT16 >>=20 >> EFIAPI >>=20 >> ReadUnaligned16 ( >>=20 >> - IN CONST UINT16 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - return *Buffer; >>=20 >> + return *(CONST UINT16 *) Buffer; >>=20 >> } >>=20 >>=20 >>=20 >> /** >>=20 >> @@ -52,13 +52,13 @@ ReadUnaligned16 ( >> UINT16 >>=20 >> EFIAPI >>=20 >> WriteUnaligned16 ( >>=20 >> - OUT UINT16 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT16 Value >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - return *Buffer =3D Value; >>=20 >> + return *(UINT16 *) Buffer =3D Value; >>=20 >> } >>=20 >>=20 >>=20 >> /** >>=20 >> @@ -77,12 +77,12 @@ WriteUnaligned16 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> ReadUnaligned24 ( >>=20 >> - IN CONST UINT32 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - return *Buffer & 0xffffff; >>=20 >> + return *(CONST UINT32 *) Buffer & 0xffffff; >>=20 >> } >>=20 >>=20 >>=20 >> /** >>=20 >> @@ -103,13 +103,13 @@ ReadUnaligned24 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> WriteUnaligned24 ( >>=20 >> - OUT UINT32 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT32 Value >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - *Buffer =3D BitFieldWrite32 (*Buffer, 0, 23, Value); >>=20 >> + *(UINT32 *) Buffer =3D BitFieldWrite32 (*(CONST UINT32 *) Buffer, 0, = 23, Value); >>=20 >> return Value; >>=20 >> } >>=20 >>=20 >>=20 >> @@ -129,12 +129,12 @@ WriteUnaligned24 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> ReadUnaligned32 ( >>=20 >> - IN CONST UINT32 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - return *Buffer; >>=20 >> + return *(CONST UINT32 *) Buffer; >>=20 >> } >>=20 >>=20 >>=20 >> /** >>=20 >> @@ -155,13 +155,13 @@ ReadUnaligned32 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> WriteUnaligned32 ( >>=20 >> - OUT UINT32 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT32 Value >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - return *Buffer =3D Value; >>=20 >> + return *(UINT32 *) Buffer =3D Value; >>=20 >> } >>=20 >>=20 >>=20 >> /** >>=20 >> @@ -180,12 +180,12 @@ WriteUnaligned32 ( >> UINT64 >>=20 >> EFIAPI >>=20 >> ReadUnaligned64 ( >>=20 >> - IN CONST UINT64 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - return *Buffer; >>=20 >> + return *(CONST UINT64 *) Buffer; >>=20 >> } >>=20 >>=20 >>=20 >> /** >>=20 >> @@ -206,11 +206,11 @@ ReadUnaligned64 ( >> UINT64 >>=20 >> EFIAPI >>=20 >> WriteUnaligned64 ( >>=20 >> - OUT UINT64 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT64 Value >>=20 >> ) >>=20 >> { >>=20 >> ASSERT (Buffer !=3D NULL); >>=20 >>=20 >>=20 >> - return *Buffer =3D Value; >>=20 >> + return *(UINT64 *) Buffer =3D Value; >>=20 >> } >>=20 >> diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/B= aseLib.h >> index 2452c1d92e51..4d30f0539c6b 100644 >> --- a/MdePkg/Include/Library/BaseLib.h >> +++ b/MdePkg/Include/Library/BaseLib.h >> @@ -3420,7 +3420,7 @@ DivS64x64Remainder ( >> UINT16 >>=20 >> EFIAPI >>=20 >> ReadUnaligned16 ( >>=20 >> - IN CONST UINT16 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ); >>=20 >>=20 >>=20 >>=20 >>=20 >> @@ -3442,7 +3442,7 @@ ReadUnaligned16 ( >> UINT16 >>=20 >> EFIAPI >>=20 >> WriteUnaligned16 ( >>=20 >> - OUT UINT16 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT16 Value >>=20 >> ); >>=20 >>=20 >>=20 >> @@ -3463,7 +3463,7 @@ WriteUnaligned16 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> ReadUnaligned24 ( >>=20 >> - IN CONST UINT32 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ); >>=20 >>=20 >>=20 >>=20 >>=20 >> @@ -3485,7 +3485,7 @@ ReadUnaligned24 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> WriteUnaligned24 ( >>=20 >> - OUT UINT32 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT32 Value >>=20 >> ); >>=20 >>=20 >>=20 >> @@ -3506,7 +3506,7 @@ WriteUnaligned24 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> ReadUnaligned32 ( >>=20 >> - IN CONST UINT32 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ); >>=20 >>=20 >>=20 >>=20 >>=20 >> @@ -3528,7 +3528,7 @@ ReadUnaligned32 ( >> UINT32 >>=20 >> EFIAPI >>=20 >> WriteUnaligned32 ( >>=20 >> - OUT UINT32 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT32 Value >>=20 >> ); >>=20 >>=20 >>=20 >> @@ -3549,7 +3549,7 @@ WriteUnaligned32 ( >> UINT64 >>=20 >> EFIAPI >>=20 >> ReadUnaligned64 ( >>=20 >> - IN CONST UINT64 *Buffer >>=20 >> + IN CONST VOID *Buffer >>=20 >> ); >>=20 >>=20 >>=20 >>=20 >>=20 >> @@ -3571,7 +3571,7 @@ ReadUnaligned64 ( >> UINT64 >>=20 >> EFIAPI >>=20 >> WriteUnaligned64 ( >>=20 >> - OUT UINT64 *Buffer, >>=20 >> + OUT VOID *Buffer, >>=20 >> IN UINT64 Value >>=20 >> ); >>=20 >>=20 >>=20 >> -- >> 2.31.1 >=20 >=20 >=20 >=20 --Apple-Mail=_2BDE6EE9-72B9-46DA-A44D-6D8B03042EB8 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On Aug 9, 202= 1, at 9:15 AM, Michael D Kinney <michael.d.kinney@intel.com> wrote:

Hi Marvin,

Can you provide an example of whic= h C compiler is flagging this as
an error and what error message is generated.

Ple= ase enter a BZ with this background information and add link to the<= br style=3D"caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 1= 2px; font-style: normal; font-variant-caps: normal; font-weight: normal; le= tter-spacing: normal; text-align: start; text-indent: 0px; text-transform: = none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0p= x; text-decoration: none;" class=3D"">BZ in the commit message.

This is a change to the BaseLib class, so we need to make sure= there
are no impacts t= o any existing code.  I looks like a safe change
because changing from a pointer to a fixed s= ize type to VOID * 
should be compatible.  = Please add that analysis to the background
in the BZ as well.

MIke,

I want to = say we had a discussion about this years ago? I don=E2=80=99t remember the = outcome. 

Dereferencing a misalign= ed pointer is UB (Undefined Behavior) in C [1], but historically x86 compil= ers have let it slide.

I think the situ= ation we are in is the BaseLib functions don=E2=80=99t contain UB, but it i= s UB for the caller to use the returned pointer directly. 
<= br class=3D"">
Here is a simple example with clang UndefinedBehav= iorSanitizer (UBSan) . 

~/work/Compiler= >cat ub.c
#include <stdlib.h>

#define EFIAPI
#define IN
#define OUT

typedef unsigned char UINT8;
typedef unsigned short UINT16;

UINT16
EFIAPI
Wr= iteUnaligned16 (
 = OUT UINT16                  &= nbsp; *Buffer,
  I= N  UINT16                 = ;   Value
  )=
{
  // ASSERT (Buffer !=3D NULL);=

  ((volatile = UINT8*)Buffer)[0] =3D (UINT8)Value;
  ((volatile UINT8*)Buffer)[1] =3D (UINT8)(Value >>= 8);

<= span style=3D"font-variant-ligatures: no-common-ligatures" class=3D""> = ; return Value;
}

<= span style=3D"font-variant-ligatures: no-common-ligatures" class=3D"">int m= ain()
{
<= div style=3D"margin: 0px; font-stretch: normal; font-size: 11px; line-heigh= t: normal; font-family: Menlo;" class=3D""> UINT8 *buffer =3D malloc(64);
UINT16 *pointer =3D (UINT16 *)(buffer + 1);


WriteUnaligned16= (pointer, 42);

<= /span>

// *pointer =3D 42; // Error: misaligned integer pointer assignment
return *pointer;
}
~/work/Compiler>clang = -fsanitize=3Dundefined  ub.c
~/work/Compiler><= /span>./a.out
ub.c:34:9: runtime er= ror: load of misaligned address 0x7feac6405aa1 for typ= e 'UINT16' (aka 'unsigned short'), which requires 2 byte alignment
0x7feac6405aa= 1: note: pointer points here
 00 00 00  64 2a 00 79 6d 28 52 54  4c 44 5f 44 45= 46 41 55  4c 54 2c 20 73 77 69 66  74 5f 64 65 6d
              ^ =
SUMMARY: UndefinedBehaviorSanitizer= : undefined-behavior ub.c:34:9 in 

FYI line 39 is `return *pointer`and 42 is 0x2= A. So reading an writing to *pointer is UB. 

=

As you can see in [1] the general advi= ce is to take code that looks like:
int8_t *buf= fer =3D malloc(64);int32_t *pointer =3D (int32_t *)(buffer + = 1);*pointer =3D 42; // Error: misaligned integer point= er assignment
And replace it with;
int8_t *buffer =3D malloc(64);int32_t value =3D 42;memcpy(buffer + 1, &value, sizeof(= int32_t)); // Correct
But in these cases the result is in a byte ali= gned buffer=E2=80=A6.



Andrew Fish

Thanks,

Mike


-----Original Message----= -
From: Marvin H=C3=A4user <
mhaeuser@posteo.de>
Sent: Monday, A= ugust 9, 2021 2:51 AM
To: devel@edk= 2.groups.io
Cc: Kinney, Michael D <michael.d.kinney@intel.com>; L= iming Gao <gaolim= ing@byosoft.com.cn>; Liu, Zhiguang
<zhiguang.liu@intel.com>; Vital= y Cheptsov <vit9696= @protonmail.com>
Subject: [PATCH v2 1/2] MdePkg/BaseLi= b: Fix unaligned API prototypes

C prohibits no= t only dereferencing but also casting to unaligned
pointers. = Thus, the current set of unaligned APIs cannot be called
safe= ly. Update their prototypes to take VOID * pointers, which must
be able to represent any valid pointer.

Cc:= Michael D Kinney <michael.d.kinney@intel.com>
Cc: Liming Gao <<= a href=3D"mailto:gaoliming@byosoft.com.cn" class=3D"">gaoliming@byosoft.com= .cn>
Cc: Zhiguang Liu <zhiguang.liu@intel.com>
Cc: V= italy Cheptsov <vit= 9696@protonmail.com>
Signed-off-by: Marvin H=C3=A4user= <mhaeuser@posteo.de>
---
MdePkg/Library/BaseLib/Arm/Unaligned.= c | 14 ++++-----
MdePkg/Library/BaseLib/Unaligned.c  &nb= sp;  | 32 ++++++++++----------
MdePkg/Include/Libra= ry/BaseLib.h       | 16 +++++-----
3 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/MdePkg/Library/BaseLib/Arm/Unaligned.c b/MdePkg/Lib= rary/BaseLib/Arm/Unaligned.c
index e9934e7003cb..57f19fc44e0b= 100644
--- a/MdePkg/Library/BaseLib/Arm/Unaligned.c
+++ b/MdePkg/Library/BaseLib/Arm/Unaligned.c
@@ -59,7 += 59,7 @@ ReadUnaligned16 (
UINT16

EFIAPI

WriteUnaligned16 (

-  OUT UINT16        &nbs= p;           *Buffer= ,

+  OUT VOID     &nb= sp;            =     *Buffer,

  I= N  UINT16           =          Value
<= br class=3D"">  )

{
@@ -87,7 +87,7 @@ WriteUnaligned16 (
UINT32

EFIAPI

ReadUnaligned2= 4 (

-  IN CONST UINT32    =           *Buffer

+  IN CONST VOID      &= nbsp;         *Buffer

  )

{

  ASSERT (Buffer !=3D NULL);
<= br class=3D"">@@ -116,7 +116,7 @@ ReadUnaligned24 (
UINT32
EFIAPI

WriteUnalign= ed24 (

-  OUT UINT32    &n= bsp;            = ;   *Buffer,

+  OUT VOID &= nbsp;           &nbs= p;        *Buffer,
  IN  UINT32      &nbs= p;            &= nbsp;Value

  )

{

@@ -143,7 +143,7 @@ WriteUnaligned24= (
UINT32

EFIAPI
<= br class=3D"">ReadUnaligned32 (

-  IN CON= ST UINT32            = ;  *Buffer

+  IN CONST VOID &nb= sp;            =   *Buffer

  )

{

  UINT16  Lo= werBytes;

@@ -175,7 +175,7 @@ ReadUnaligned32 = (
UINT32

EFIAPI
WriteUnaligned32 (

-  OUT UI= NT32            &nbs= p;       *Buffer,

+  OUT VOID         &nb= sp;            = *Buffer,

  IN  UINT32  &nb= sp;            =      Value

 &nbs= p;)

{

@@ -202,7 += 202,7 @@ WriteUnaligned32 (
UINT64

EFIAPI

ReadUnaligned64 (

-  IN CONST UINT64       &nbs= p;      *Buffer

= +  IN CONST VOID          = ;      *Buffer

&= nbsp; )

{

&n= bsp; UINT32  LowerBytes;

@@ -234,7 += 234,7 @@ ReadUnaligned64 (
UINT64

EFIAPI

WriteUnaligned64 (

-  OUT UINT64        &nb= sp;           *Buffe= r,

+  OUT VOID     &n= bsp;            = ;    *Buffer,

  = IN  UINT64           = ;         Value
=
  )

{
<= br class=3D"">diff --git a/MdePkg/Library/BaseLib/Unaligned.c b/MdePkg/Libr= ary/BaseLib/Unaligned.c
index a419cb85e53c..3041adcde606 1006= 44
--- a/MdePkg/Library/BaseLib/Unaligned.c
+++= b/MdePkg/Library/BaseLib/Unaligned.c
@@ -26,12 +26,12 @@
UINT16

EFIAPI

ReadUnaligned16 (

-  IN CONST UIN= T16             = ; *Buffer

+  IN CONST VOID  &nb= sp;            =  *Buffer

  )

{

  ASSERT (Buffer !=3D N= ULL);



-  re= turn *Buffer;

+  return *(CONST UINT16 *)= Buffer;

}



/**

@@ -52,13 +52,13 @= @ ReadUnaligned16 (
UINT16

EFIAP= I

WriteUnaligned16 (

-  OUT UINT16         &n= bsp;          *Buffer,
+  OUT VOID      &= nbsp;           &nbs= p;   *Buffer,

  IN &n= bsp;UINT16           &nbs= p;        Value

  )

{

  ASSERT (Buffer !=3D NULL);



-  return *Buffer =3D Value;

+  return *(UINT16 *) Buffer =3D Value;

}



/**

@@ -77,12 +77,12 @@ WriteUnaligned1= 6 (
UINT32

EFIAPI
=
ReadUnaligned24 (

-  IN CO= NST UINT32           &nbs= p;  *Buffer

+  IN CONST VOID &n= bsp;            = ;  *Buffer

  )

{

  ASSERT (Buffer= !=3D NULL);



- &= nbsp;return *Buffer & 0xffffff;

+  re= turn *(CONST UINT32 *) Buffer & 0xffffff;

= }



/**

@@ -103,13 +103,13 @@ ReadUnaligned24 (
= UINT32

EFIAPI

Wri= teUnaligned24 (

-  OUT UINT32   = ;            &n= bsp;    *Buffer,

+  O= UT VOID            &= nbsp;         *Buffer,

  IN  UINT32     &= nbsp;           &nbs= p;  Value

  )

{

  ASSERT (Buffer = !=3D NULL);



- &n= bsp;*Buffer =3D BitFieldWrite32 (*Buffer, 0, 23, Value);

+  *(UINT32 *) Buffer =3D BitFieldWrite32 (*(CONST UINT32 *= ) Buffer, 0, 23, Value);

  return Va= lue;

}



@@ -129,12 +129,12 @@ WriteUnaligned24 (
UINT32

EFIAPI

Re= adUnaligned32 (

-  IN CONST UINT32  =             *Bu= ffer

+  IN CONST VOID    &= nbsp;           *Buf= fer

  )

{

  ASSERT (Buffer !=3D NULL);



-  return *Buff= er;

+  return *(CONST UINT32 *) Buffer;
}


<= br class=3D"">/**

@@ -155,13 +155,13 @@ ReadUn= aligned32 (
UINT32

EFIAPI

WriteUnaligned32 (

- &n= bsp;OUT UINT32           =          *Buffer,

+  OUT VOID        = ;            &n= bsp; *Buffer,

  IN  UINT32=             &n= bsp;      Value

=   )

{

&= nbsp; ASSERT (Buffer !=3D NULL);



-  return *Buffer =3D Value;

+  return *(UINT32 *) Buffer =3D Value;

}



/**
@@ -180,12 +180,12 @@ WriteUnaligned32 (
UINT64

EFIAPI

ReadUnaligned64 (

-  IN CONST UINT6= 4             &= nbsp;*Buffer

+  IN CONST VOID   = ;            &n= bsp;*Buffer

  )

{

  ASSERT (Buffer !=3D NUL= L);



-  retu= rn *Buffer;

+  return *(CONST UINT64 *) B= uffer;

}



/**

@@ -206,11 +206,11 @= @ ReadUnaligned64 (
UINT64

EFIAP= I

WriteUnaligned64 (

-  OUT UINT64         &n= bsp;          *Buffer,
+  OUT VOID      &= nbsp;           &nbs= p;   *Buffer,

  IN &n= bsp;UINT64           &nbs= p;        Value

  )

{

  ASSERT (Buffer !=3D NULL);



-  return *Buffer =3D Value;

+  return *(UINT64 *) Buffer =3D Value;

}

diff --git a/MdePkg/Inc= lude/Library/BaseLib.h b/MdePkg/Include/Library/BaseLib.h
ind= ex 2452c1d92e51..4d30f0539c6b 100644
--- a/MdePkg/Include/Lib= rary/BaseLib.h
+++ b/MdePkg/Include/Library/BaseLib.h
@@ -3420,7 +3420,7 @@ DivS64x64Remainder (
UINT16

EFIAPI

ReadUnaligned1= 6 (

-  IN CONST UINT16    =           *Buffer

+  IN CONST VOID      &= nbsp;         *Buffer

  );





@@ -3442,7 +3442,7 @@ Re= adUnaligned16 (
UINT16

EFIAPI
WriteUnaligned16 (

= -  OUT UINT16          &n= bsp;         *Buffer,

+  OUT VOID       &= nbsp;           &nbs= p;  *Buffer,

  IN  UI= NT16            &nbs= p;       Value

  );



@@ -3463,7 +3463,7 @@ WriteUnaligned16 (
UINT32

EFIAPI

ReadUnaligned24 (=

-  IN CONST UINT32    &nb= sp;         *Buffer

+  IN CONST VOID      &n= bsp;         *Buffer

  );





@@ -3485,7 +3485,7 @@ Re= adUnaligned24 (
UINT32

EFIAPI
WriteUnaligned24 (

= -  OUT UINT32          &n= bsp;         *Buffer,

+  OUT VOID       &= nbsp;           &nbs= p;  *Buffer,

  IN  UI= NT32            &nbs= p;       Value

  );



@@ -3506,7 +3506,7 @@ WriteUnaligned24 (
UINT32

EFIAPI

ReadUnaligned32 (=

-  IN CONST UINT32    &nb= sp;         *Buffer

+  IN CONST VOID      &n= bsp;         *Buffer

  );





@@ -3528,7 +3528,7 @@ Re= adUnaligned32 (
UINT32

EFIAPI
WriteUnaligned32 (

= -  OUT UINT32          &n= bsp;         *Buffer,

+  OUT VOID       &= nbsp;           &nbs= p;  *Buffer,

  IN  UI= NT32            &nbs= p;       Value

  );



@@ -3549,7 +3549,7 @@ WriteUnaligned32 (
UINT64

EFIAPI

ReadUnaligned64 (=

-  IN CONST UINT64    &nb= sp;         *Buffer

+  IN CONST VOID      &n= bsp;         *Buffer

  );





@@ -3571,7 +3571,7 @@ Re= adUnaligned64 (
UINT64

EFIAPI
WriteUnaligned64 (

= -  OUT UINT64          &n= bsp;         *Buffer,

+  OUT VOID       &= nbsp;           &nbs= p;  *Buffer,

  IN  UI= NT64            &nbs= p;       Value

  );



--
2.31.1




--Apple-Mail=_2BDE6EE9-72B9-46DA-A44D-6D8B03042EB8--