From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by mx.groups.io with SMTP id smtpd.web09.1002.1628630085995350317 for ; Tue, 10 Aug 2021 14:14:46 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@posteo.de header.s=2017 header.b=p7alYQar; spf=pass (domain: posteo.de, ip: 185.67.36.65, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id D78C9240028 for ; Tue, 10 Aug 2021 23:14:43 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1628630083; bh=EZJNAVW56zBIP+uTvIuiFey6DNjKo6GAej4ko2DWt7U=; h=Subject:To:Cc:From:Date:From; b=p7alYQar7u7vX6Tb/R7Je4qfuNozNB07HA74JCYhn2Xxcq4BlDcNwS+LgNjDDVsun YgAO0TFWmoHcoRBZR8+KTa4sLFKgrazgI3y4B0CnV6INL4AYAnMlQHXrKOwz2WpWUd OzgXYOyTscHyoGSzU3aM7ljaQpXhmsZgS27s3al7Xe1kEtylKRiUX1HtZLMjUZjTLh b7XfIL4zYz8MSYf2gTRkQaWc2+18JTIM3nBsJolcpghTQ7mSf8cCgPiHZNTpbIKKvO jS7PASwzYS2VQE0l67jcFNcjkUZMHXny/Ez+H/brrDXDL3t3RtRNLDO8hBkPuvDbbM rREXMh8ZbLLog== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Gkm1F4zkrz6tm4; Tue, 10 Aug 2021 23:14:41 +0200 (CEST) Subject: Re: [edk2-devel] [PATCH v2 1/2] MdePkg/BaseLib: Fix unaligned API prototypes To: devel@edk2.groups.io, afish@apple.com Cc: Mike Kinney , Liming Gao , "Liu, Zhiguang" , Vitaly Cheptsov References: <54883a88-199b-2ba7-3d18-db02636358c7@posteo.de> From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= Message-ID: <03391b85-09ae-b1b9-044a-a122517275d3@posteo.de> Date: Tue, 10 Aug 2021 21:14:41 +0000 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-GB Content-Transfer-Encoding: quoted-printable On 10/08/2021 19:36, Andrew Fish via groups.io wrote: > > >> On Aug 10, 2021, at 1:53 AM, Marvin H=C3=A4user > > wrote: >> >> On 09/08/2021 23:32, Andrew Fish viagroups.io wrote: >>> >>> >>>> On Aug 9, 2021, at 9:15 AM, Michael D Kinney=20 >>>> >>> >>> >> wrote: >>>> >>>> Hi Marvin, >>>> >>>> Can you provide an example of which C compiler is flagging this as >>>> an error and what error message is generated. >>>> >>>> Please enter a BZ with this background information and add link to the >>>> BZ in the commit message. >>>> >>>> This is a change to the BaseLib class, so we need to make sure there >>>> are no impacts to any existing code. =C2=A0I looks like a safe change >>>> because changing from a pointer to a fixed size type to VOID * >>>> should be compatible. =C2=A0Please 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= =20 >>> remember the outcome. >>> >>> Dereferencing a misaligned pointer is UB (Undefined Behavior) in C=20 >>> [1], but historically x86 compilers have let it slide. >>> >>> I think the situation we are in is the BaseLib functions don=E2=80=99t= =20 >>> contain UB, but it is UB for the caller to use the returned pointer=20 >>> directly. >> >> They do contain UB, inherently from the combination of their=20 >> prototype and their usage. >> Please refer to the new BZ added for=20 >> V2:https://bugzilla.tianocore.org/show_bug.cgi?id=3D3542=20 >> >> > > I think we are saying the same thing. OK, sorry, misunderstood then. > My point is it is possible to use the library API correctly, but=20 > likely no one does. Changing the API to a VOID also does not fix every=20 > issue, actually it is a drop in to existing code so it does not really=20 > force any fixes... Yes, but at least it does not *force* the caller to do UB casts. I actually have fixed everything I found here:=20 https://github.com/mhaeuser/edk2/commit/7266c31f04634bf3e1861e84f9be9983a60= 84f3c Just I thought I could not possibly defend such a huge patch for an=20 issue nobody encounters and likely never will either. If you want the=20 patch, you totally can have it, just I did not want to increase the=20 burden for no reason. I'm fine if future code does it right, but also to=20 polish and submit above patch. > >> I could only speculate why UBsan does not check cast safety... >> To be fair, I don't know a real-world platform that has issues with=20 >> this UB, but the fix is simple enough in my opinion. >> > > Cast is compile time UB, not runtime. UBSan is doing runtime checks.=20 > You can catch compile time UB with warnings. I've have had way too=20 > many talks with the clang folks about UB and firmware :). Right, that would have been a guess. One could make a point though that=20 it is RT UB, because UB is only invoked if the address is actually not=20 aligned, the compiler not having a proof is not enough. Thanks for the=20 insight! > >> Actually, enabling "-Wcast-align" some day would be great either way. :) >> > > In my example this gets you past the -Wcast-align. We have the same=20 > type of casts for the size of pointers and casting through UINTN. > UINT16 *pointer =3D (UINT16 *)(void *)(buffer+1); Yes, but this looks much more ominous than without the void cast. I=20 sometimes experiment with code style a bit in my own private code, and=20 omitting void pointers as far as possible (exceptions for cases as=20 above) has been one of those experiments. I don't think one can nail C=20 code style perfectly, it's always a game of trade-offs. > > Actually I=E2=80=99m starting to remember why we chose not to fix this be= fore.=20 > It does not really help=E2=80=A6. Notice if we change the API to use `VOI= D *`=20 > vs. `UINT16 *` nothing really changed [1] ? =C2=A0It is still up to the= =20 > caller to do the right thing. Yes-ish, this is not quite a functional fix, it's about guarantees. If=20 the functions take UINT16 pointers, they request a guarantee from every=20 caller, with no docs required, that the pointers must be aligned for=20 this type. It's a guarantee for the functional implementation, but more=20 importantly a guarantee for the compiler. If we change it to a VOID=20 pointer, it will basically be no-op for the binary, as no compiler=20 really makes such assumptions at cast-time (to my knowledge), but the=20 guarantees were shifted. Now, if the function wants an alignment=20 guarantee from the caller, it has to define it in the docs. No such=20 exists, so the caller knows that the input pointer does not impose any=20 alignment constraints, and the compiler knows it cannot assume any such. > The real issue is the functions are not really useful given strict UB=20 > in C=E2=80=A6. > > A better solution is to just tell the compiler this pointer is not=20 > aligned, and at that point you DON'T need the lib functions=E2=80=A6. > > This code passes -Wcast-align=C2=A0and -fsanitize=3Dundefined runtime che= cks: > typedef UINT16 UUINT16 __attribute__ ((aligned (1))); > > int main() > { > UINT8 *buffer =3D malloc(64); > UUINT16 *pointer =3D (UUINT16 *)(buffer+1); > *pointer =3D 42; > return *pointer; > } > > So a better solution maybe to defined unaligned basic types? I=E2=80=99m = not=20 > sure if=C2=A0__declspec(align(1)) does the right thing for VC++,=20 > __attribute__ ((aligned (1))); does the right thing for GCC and clang.=20 > Assuming this works we could defined unaligned version of our basic=20 > types, and over time obsolete the current alignment lib functions. We=20 > could start with a warning comment to use the unaligned types vs.=20 > current library functions. > > I=E2=80=99m thinking for the types in ProcessorBind.h[2] and Base.h[3] we= =20 > could have a byte aligned versions. For example given UINT16 we add=20 > UUINT16 to Base.h. I guess we could also =C2=A0look into a #define=20 > abstraction for ` __attribute__ ((aligned (1)))` so new could could=20 > use that in a portable way? > > Just to be clear if we define UUINT16 then the compiler will do the=20 > the work of=C2=A0WriteUnaligned16() in the code generation, if it is need= ed=20 > by the CPU arch. Given that it is much harder for the coder to =E2=80=9Cm= ess=20 > it up=E2=80=9D. Well, the caller code can cast to aligned types over unaligned types=20 just as well with this concept. But I like it just for the sake of=20 leveraging compiler code over providing own implementations. At the very=20 least for packed structures they sound great. Clang added=20 "-Waddress-of-packed-member" at some point, and unaligned base types=20 allow safe (and warning-free) address retrieval. > > Actually I changed my mind I think we will want to keep the Unaligned=20 > library functions, but update them to use the unaligned pointers. The=20 > reason we need the function is the compiler is generating code that=20 > follow the rules of the CPU, so we need the library functions to talk=20 > to hardware devices that have stricter alignment rules than the CPU.=20 > For example on x86 UUINT16 will end up being a word operation in=20 > assembler. This is the reason the current lib functions use volatile.=20 > Sorry figuring this out as I type. So looks like we need to add=20 > unaligned types and update the libs to use those types? For memory=20 > operations you can use the new unaligned types, and for hardware=20 > access use the lib functions. In this case I would probably rebrand them (docs are enough) to MMIO=20 functions, and encourage new code to use the unaligned pointer types=20 directly? This may actually provide optimisation advantages due to lack=20 of volatile. Thanks for the detailed notes! Best regards, Marvin > > [1] ~/work/Compiler>cat ub.c > #include > > #define EFIAPI > #define IN > #define OUT > #define VOID void > > typedef unsigned char UINT8; > typedef unsigned short UINT16; > > UINT16 > EFIAPI > WriteUnaligned16 ( > =C2=A0 OUT VOID=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 *Buffer, > =C2=A0 IN=C2=A0 UINT16=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 Value > =C2=A0 ) > { > =C2=A0 // ASSERT (Buffer !=3D NULL); > > =C2=A0 ((volatile UINT8*)Buffer)[0] =3D (UINT8)Value; > =C2=A0 ((volatile UINT8*)Buffer)[1] =3D (UINT8)(Value >> 8); > > =C2=A0 return Value; > } > > > int main() > { > UINT8 *buffer =3D malloc(64); > UINT16 *pointer =3D (UINT16 *)(void *)(buffer+1); > > > WriteUnaligned16 (pointer, 42); > > > // *pointer =3D 42; // Error: misaligned integer pointer assignment > return *pointer; > } > ~/work/Compiler>clang -fsanitize=3Dundefined -Wcast-align=C2=A0 ub.c > ~/work/Compiler>./a.out > *ub.c:35:9:**runtime error: **load of misaligned address=20 > 0x7fdd9d405aa1 for type 'UINT16' (aka 'unsigned short'), which=20 > requires 2 byte alignment* > *0x7fdd9d405aa1: note: *pointer points here > =C2=A000 00 00=C2=A0 64 2a 00 79 6d 28 52 54=C2=A0 4c 44 5f 44 45 46 41 5= 5=C2=A0 4c 54 2c=20 > 20 73 77 69 66=C2=A0 74 5f 64 65 6d > *=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ^ * > SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ub.c:35:9 in > ~/work/Compiler> > > > [2]=20 > https://github.com/tianocore/edk2/blob/master/MdePkg/Include/X64/Processo= rBind.h#L127=20 > =20 > > [3]=20 > https://github.com/tianocore/edk2/blob/master/MdePkg/Include/Base.h=20 > > > Thanks, > > Andrew Fish > >> Best regards, >> Marvin >> >>> >>> Here is a simple example with clang UndefinedBehaviorSanitizer (UBSan)= =C2=A0. >>> >>> ~/work/Compiler>cat ub.c >>> #include >>> >>> #define EFIAPI >>> #define IN >>> #define OUT >>> >>> typedef unsigned char UINT8; >>> typedef unsigned short UINT16; >>> >>> UINT16 >>> EFIAPI >>> WriteUnaligned16 ( >>> =C2=A0 OUT UINT16=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 *Buffer, >>> =C2=A0 IN=C2=A0 UINT16=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 Value >>> =C2=A0 ) >>> { >>> =C2=A0 // ASSERT (Buffer !=3D NULL); >>> >>> =C2=A0 ((volatile UINT8*)Buffer)[0] =3D (UINT8)Value; >>> =C2=A0 ((volatile UINT8*)Buffer)[1] =3D (UINT8)(Value >> 8); >>> >>> =C2=A0 return Value; >>> } >>> >>> >>> int main() >>> { >>> UINT8 *buffer =3D malloc(64); >>> UINT16 *pointer =3D (UINT16 *)(buffer + 1); >>> >>> >>> WriteUnaligned16 (pointer, 42); >>> >>> >>> // *pointer =3D 42; // Error: misaligned integer pointer assignment >>> return *pointer; >>> } >>> ~/work/Compiler>clang -fsanitize=3Dundefined=C2=A0 ub.c >>> ~/work/Compiler>./a.out >>> *ub.c:34:9:**runtime error: **load of misaligned address=20 >>> 0x7feac6405aa1 for type 'UINT16' (aka 'unsigned short'), which=20 >>> requires 2 byte alignment* >>> *0x7feac6405aa1: note: *pointer points here >>> =C2=A000 00 00=C2=A0 64 2a 00 79 6d 28 52 54=C2=A0 4c 44 5f 44 45 46 41= 55=C2=A0 4c 54=20 >>> 2c 20 73 77 69 66=C2=A0 74 5f 64 65 6d >>> *=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ^ * >>> SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior ub.c:34:9 in >>> >>> FYI line 39 is `return *pointer`and 42 is 0x2A. So reading an=20 >>> writing to *pointer is UB. >>> >>> >>> As you can see in [1] the general advice is to take code that looks=20 >>> like: >>> int8_t *buffer =3D malloc(64);int32_t *pointer =3D (int32_t *)(buffer += =20 >>> 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,= =20 >>> &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= =20 >>> >> > >>> >>> Thanks, >>> >>> Andrew Fish >>> >>>> Thanks, >>>> >>>> Mike >>>> >>>> >>>>> -----Original Message----- >>>>> From: Marvin H=C3=A4user >>>> >>>> >> >>>>> Sent: Monday, August 9, 2021 2:51 AM >>>>> To:devel@edk2.groups.io=20 >>>>> >>>> > >>>>> Cc: Kinney, Michael D >>>> >>>> >>; Liming Gao=20 >>>>> >>>> >>>> >>; Liu, Zhiguang >>>>> >>>> >>>> >>; Vitaly Cheptsov=20 >>>>> >>>> >>>> >> >>>>> Subject: [PATCH v2 1/2] MdePkg/BaseLib: Fix unaligned API prototypes >>>>> >>>>> 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. >>>>> >>>>> 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 =C2=A0=C2=A0=C2=A0=C2=A0| 32 +++++= +++++---------- >>>>> MdePkg/Include/Library/BaseLib.h =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= | 16 +++++----- >>>>> 3 files changed, 31 insertions(+), 31 deletions(-) >>>>> >>>>> diff --git a/MdePkg/Library/BaseLib/Arm/Unaligned.c=20 >>>>> b/MdePkg/Library/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 ( >>>>> >>>>> - =C2=A0OUT UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> @@ -87,7 +87,7 @@ WriteUnaligned16 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned24 ( >>>>> >>>>> - =C2=A0IN CONST UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> @@ -116,7 +116,7 @@ ReadUnaligned24 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned24 ( >>>>> >>>>> - =C2=A0OUT UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> @@ -143,7 +143,7 @@ WriteUnaligned24 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned32 ( >>>>> >>>>> - =C2=A0IN CONST UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0UINT16 =C2=A0LowerBytes; >>>>> >>>>> @@ -175,7 +175,7 @@ ReadUnaligned32 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned32 ( >>>>> >>>>> - =C2=A0OUT UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> @@ -202,7 +202,7 @@ WriteUnaligned32 ( >>>>> UINT64 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned64 ( >>>>> >>>>> - =C2=A0IN CONST UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0UINT32 =C2=A0LowerBytes; >>>>> >>>>> @@ -234,7 +234,7 @@ ReadUnaligned64 ( >>>>> UINT64 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned64 ( >>>>> >>>>> - =C2=A0OUT UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> diff --git a/MdePkg/Library/BaseLib/Unaligned.c=20 >>>>> 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 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned16 ( >>>>> >>>>> - =C2=A0IN CONST UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0return *Buffer; >>>>> >>>>> + =C2=A0return *(CONST UINT16 *) Buffer; >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> /** >>>>> >>>>> @@ -52,13 +52,13 @@ ReadUnaligned16 ( >>>>> UINT16 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned16 ( >>>>> >>>>> - =C2=A0OUT UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0return *Buffer =3D Value; >>>>> >>>>> + =C2=A0return *(UINT16 *) Buffer =3D Value; >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> /** >>>>> >>>>> @@ -77,12 +77,12 @@ WriteUnaligned16 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned24 ( >>>>> >>>>> - =C2=A0IN CONST UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0return *Buffer & 0xffffff; >>>>> >>>>> + =C2=A0return *(CONST UINT32 *) Buffer & 0xffffff; >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> /** >>>>> >>>>> @@ -103,13 +103,13 @@ ReadUnaligned24 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned24 ( >>>>> >>>>> - =C2=A0OUT UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0*Buffer =3D BitFieldWrite32 (*Buffer, 0, 23, Value); >>>>> >>>>> + =C2=A0*(UINT32 *) Buffer =3D BitFieldWrite32 (*(CONST UINT32 *) Buf= fer,=20 >>>>> 0, 23, Value); >>>>> >>>>> =C2=A0=C2=A0return Value; >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> @@ -129,12 +129,12 @@ WriteUnaligned24 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned32 ( >>>>> >>>>> - =C2=A0IN CONST UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0return *Buffer; >>>>> >>>>> + =C2=A0return *(CONST UINT32 *) Buffer; >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> /** >>>>> >>>>> @@ -155,13 +155,13 @@ ReadUnaligned32 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned32 ( >>>>> >>>>> - =C2=A0OUT UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0return *Buffer =3D Value; >>>>> >>>>> + =C2=A0return *(UINT32 *) Buffer =3D Value; >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> /** >>>>> >>>>> @@ -180,12 +180,12 @@ WriteUnaligned32 ( >>>>> UINT64 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned64 ( >>>>> >>>>> - =C2=A0IN CONST UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0return *Buffer; >>>>> >>>>> + =C2=A0return *(CONST UINT64 *) Buffer; >>>>> >>>>> } >>>>> >>>>> >>>>> >>>>> /** >>>>> >>>>> @@ -206,11 +206,11 @@ ReadUnaligned64 ( >>>>> UINT64 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned64 ( >>>>> >>>>> - =C2=A0OUT UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0) >>>>> >>>>> { >>>>> >>>>> =C2=A0=C2=A0ASSERT (Buffer !=3D NULL); >>>>> >>>>> >>>>> >>>>> - =C2=A0return *Buffer =3D Value; >>>>> >>>>> + =C2=A0return *(UINT64 *) Buffer =3D Value; >>>>> >>>>> } >>>>> >>>>> diff --git a/MdePkg/Include/Library/BaseLib.h=20 >>>>> b/MdePkg/Include/Library/BaseLib.h >>>>> index 2452c1d92e51..4d30f0539c6b 100644 >>>>> --- a/MdePkg/Include/Library/BaseLib.h >>>>> +++ b/MdePkg/Include/Library/BaseLib.h >>>>> @@ -3420,7 +3420,7 @@ DivS64x64Remainder ( >>>>> UINT16 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned16 ( >>>>> >>>>> - =C2=A0IN CONST UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> @@ -3442,7 +3442,7 @@ ReadUnaligned16 ( >>>>> UINT16 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned16 ( >>>>> >>>>> - =C2=A0OUT UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT16 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> @@ -3463,7 +3463,7 @@ WriteUnaligned16 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned24 ( >>>>> >>>>> - =C2=A0IN CONST UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> @@ -3485,7 +3485,7 @@ ReadUnaligned24 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned24 ( >>>>> >>>>> - =C2=A0OUT UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> @@ -3506,7 +3506,7 @@ WriteUnaligned24 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned32 ( >>>>> >>>>> - =C2=A0IN CONST UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> @@ -3528,7 +3528,7 @@ ReadUnaligned32 ( >>>>> UINT32 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned32 ( >>>>> >>>>> - =C2=A0OUT UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT32 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> @@ -3549,7 +3549,7 @@ WriteUnaligned32 ( >>>>> UINT64 >>>>> >>>>> EFIAPI >>>>> >>>>> ReadUnaligned64 ( >>>>> >>>>> - =C2=A0IN CONST UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> + =C2=A0IN CONST VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> >>>>> >>>>> @@ -3571,7 +3571,7 @@ ReadUnaligned64 ( >>>>> UINT64 >>>>> >>>>> EFIAPI >>>>> >>>>> WriteUnaligned64 ( >>>>> >>>>> - =C2=A0OUT UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0*Buffer, >>>>> >>>>> + =C2=A0OUT VOID =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= *Buffer, >>>>> >>>>> =C2=A0=C2=A0IN =C2=A0UINT64 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= Value >>>>> >>>>> =C2=A0=C2=A0); >>>>> >>>>> >>>>> >>>>> -- >>>>> 2.31.1 >>>> >>>> >>>> > >=20