<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:DengXian;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"\@DengXian";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
.MsoChpDefault
        {mso-style-type:export-only;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">The fact that it may vary per ABI seems like a pretty big gotcha if the SMM/MM Core was compiled at a different time or on a different system than the module that’s invoking the communication.</p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">- Bret<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="mso-element:para-border-div;border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="border:none;padding:0in"><b>From: </b><a href="mailto:mhaeuser@posteo.de">Marvin Häuser</a><br>
<b>Sent: </b>Monday, June 28, 2021 8:43 AM<br>
<b>To: </b><a href="mailto:lersek@redhat.com">Laszlo Ersek</a>; <a href="mailto:kuqin12@gmail.com">
Kun Qin</a>; <a href="mailto:michael.d.kinney@intel.com">Kinney, Michael D</a>; <a href="mailto:devel@edk2.groups.io">
devel@edk2.groups.io</a><br>
<b>Cc: </b><a href="mailto:jian.j.wang@intel.com">Wang, Jian J</a>; <a href="mailto:hao.a.wu@intel.com">
Wu, Hao A</a>; <a href="mailto:eric.dong@intel.com">Dong, Eric</a>; <a href="mailto:ray.ni@intel.com">
Ni, Ray</a>; <a href="mailto:gaoliming@byosoft.com.cn">Liming Gao</a>; <a href="mailto:zhiguang.liu@intel.com">
Liu, Zhiguang</a>; <a href="mailto:afish@apple.com">Andrew Fish</a>; <a href="mailto:leif@nuviainc.com">
Lindholm, Leif</a>; <a href="mailto:Bret.Barkelew@microsoft.com">Bret Barkelew</a>;
<a href="mailto:Michael.Kubacki@microsoft.com">Michael Kubacki</a><br>
<b>Subject: </b>[EXTERNAL] Re: [edk2-devel] [PATCH v1 0/5] EDK2 Code First: PI Specification: Update EFI_MM_COMMUNICATE_HEADER</p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">On 28.06.21 16:57, Laszlo Ersek wrote:<br>
> On 06/25/21 20:47, Kun Qin wrote:<br>
>> Hi Mike,<br>
>><br>
>> Thanks for the information. I can update the patch and proposed spec<br>
>> change to use flexible array in v-next if there is no other concerns.<br>
>><br>
>> After switching to flexible array, OFFSET_OF (Data) should lead to the<br>
>> same result as sizeof.<br>
> This may be true on specific compilers, but it is *not* guaranteed by<br>
> the C language standard.<br>
<br>
Sorry, for completeness sake... :)<br>
<br>
I don't think it really depends on the compiler (but can vary per ABI), <br>
but it's a conceptual issue with alignment requirements. Assuming <br>
natural alignment and the usual stuff, for "struct s { uint64_t a; <br>
uint32_t b; uint8_t c[]; }" the alignment requirement is 8 Bytes, where <br>
there are 4 Bytes of padding after "b" (as "c" may as well be empty). <br>
"c" however is guaranteed to start after b in the same fashion as if it <br>
was declared with the maximum amount of elements that fit the memory. So <br>
if we take 4 elements for "c", and note that "c" has an alignment <br>
requirement of 1 Byte, c[0 .. 3] will alias the padding after "b". For <br>
"sizeof" this means that the padding is included, whereas for "offsetof" <br>
it is not, yielding "sizeof(struct s) == offsetof(struct s, c) + 4". <br>
That is what I meant by "wasted space" earlier, but this could possibly <br>
be made nicer with macros as necessary.<br>
<br>
As for this specific struct, the values should be identical as it is padded.<br>
<br>
Best regards,<br>
Marvin<br>
<br>
><br>
> Quoting C99 6.7.2.1 "Structure and union specifiers", paragraph 16:<br>
><br>
> "In most situations, the flexible array member is ignored. In<br>
> particular, the size of the structure is as if the flexible array member<br>
> were omitted except that it may have more trailing padding than the<br>
> omission would imply."<br>
><br>
> Quoting footnotes 17 and 19,<br>
><br>
> (17)  [...]<br>
>        struct s { int n; double d[]; };<br>
>        [...]<br>
><br>
> (19)  [...]<br>
>        the expressions:<br>
>        [...]<br>
>        sizeof (struct s) >= offsetof(struct s, d)<br>
><br>
>        are always equal to 1.<br>
><br>
> Thanks<br>
> Laszlo<br>
><br>
><br>
><br>
>> While sizeof would be a preferred way to move<br>
>> forward.<br>
>><br>
>> Regards,<br>
>> Kun<br>
>><br>
>> On 06/24/2021 08:25, Kinney, Michael D wrote:<br>
>>> Hello,<br>
>>><br>
>>> Flexible array members are supported and should be used.  The old style<br>
>>> of adding an array of size [1] at the end of a structure was used at a<br>
>>> time<br>
>>> flexible array members were not supported by all compilers (late 1990's).<br>
>>> The workarounds used to handle the array of size [1] are very<br>
>>> confusing when<br>
>>> reading the C  code and the fact that sizeof() does not produce the<br>
>>> expected<br>
>>> result make it even worse.<br>
>>><br>
>>> If we use flexible array members in this proposed change then there is<br>
>>> no need to use OFFSET_OF().  Correct?<br>
>>><br>
>>> Mike<br>
>>><br>
>>><br>
>>>> -----Original Message-----<br>
>>>> From: Marvin Häuser <mhaeuser@posteo.de><br>
>>>> Sent: Thursday, June 24, 2021 1:00 AM<br>
>>>> To: Kun Qin <kuqin12@gmail.com>; Laszlo Ersek <lersek@redhat.com>;<br>
>>>> devel@edk2.groups.io<br>
>>>> Cc: Wang, Jian J <jian.j.wang@intel.com>; Wu, Hao A<br>
>>>> <hao.a.wu@intel.com>; Dong, Eric <eric.dong@intel.com>; Ni, Ray<br>
>>>> <ray.ni@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>;<br>
>>>> Liming Gao <gaoliming@byosoft.com.cn>; Liu, Zhiguang<br>
>>>> <zhiguang.liu@intel.com>; Andrew Fish <afish@apple.com>; Leif<br>
>>>> Lindholm <leif@nuviainc.com>; Bret Barkelew<br>
>>>> <Bret.Barkelew@microsoft.com>; michael.kubacki@microsoft.com<br>
>>>> Subject: Re: [edk2-devel] [PATCH v1 0/5] EDK2 Code First: PI<br>
>>>> Specification: Update EFI_MM_COMMUNICATE_HEADER<br>
>>>><br>
>>>> Hey Kun,<br>
>>>><br>
>>>> Why would you rely on undefined behaviours? The OFFSET_OF macro is<br>
>>>> well-defined for GCC and Clang as it's implemented by an intrinsic, and<br>
>>>> while the expression for the MSVC compiler is undefined behaviour as per<br>
>>>> the C standard, it is well-defined for MSVC due to their own<br>
>>>> implementation being identical. From my standpoint, all supported<br>
>>>> compilers will yield well-defined behaviour even this way. OFFSET_OF on<br>
>>>> flexible arrays is not UB in any case to my knowledge.<br>
>>>><br>
>>>> However, the same way as your new suggestion, you can replace OFFSET_OF<br>
>>>> with sizeof. While this *can* lead to wasted space with certain<br>
>>>> structure layouts (e.g. when the flexible array overlays padding bytes),<br>
>>>> this is not the case here, and otherwise just loses you a few bytes. I<br>
>>>> think this comes down to preference.<br>
>>>><br>
>>>> The pattern you mentioned arguably is less nice syntax when used<br>
>>>> (involves address calculation and casting), but the biggest problem here<br>
>>>> is alignment constraints. For packed structures, you lose the ability of<br>
>>>> automatic unaligned accesses (irrelevant here because the structure is<br>
>>>> manually padded anyway). For non-packed structures, you still need to<br>
>>>> ensure the alignment requirement of the trailing array data is met<br>
>>>> manually. With flexible array members, the compiler takes care of both<br>
>>>> cases automatically.<br>
>>>><br>
>>>> Best regards,<br>
>>>> Marvin<br>
>>>><br>
>>>> On 24.06.21 02:24, Kun Qin wrote:<br>
>>>>> Hi Marvin,<br>
>>>>><br>
>>>>> I would prefer not to rely on undefined behaviors from different<br>
>>>>> compilers. Instead of using flexible arrays, is it better to remove<br>
>>>>> the `Data` field, pack the structure and follow<br>
>>>>> "VARIABLE_LOCK_ON_VAR_STATE_POLICY" pattern?<br>
>>>>><br>
>>>>> In that case, OFFSET_OF will be forced to change to sizeof, and<br>
>>>>> read/write to `Data` will follow the range indicated by MessageLength.<br>
>>>>> But yes, that will enforce developers to update their platform level<br>
>>>>> implementations accordingly.<br>
>>>>><br>
>>>>> Regards,<br>
>>>>> Kun<br>
>>>>><br>
>>>>> On 06/23/2021 08:26, Laszlo Ersek wrote:<br>
>>>>>> On 06/23/21 08:54, Marvin Häuser wrote:<br>
>>>>>>> On 22.06.21 17:34, Laszlo Ersek wrote:<br>
>>>>>>>> On 06/18/21 11:37, Marvin Häuser wrote:<br>
>>>>>>>>> On 16.06.21 22:58, Kun Qin wrote:<br>
>>>>>>>>>> On 06/16/2021 00:02, Marvin Häuser wrote:<br>
>>>>>>>>>>> 2) Is it feasible yet with the current set of supported<br>
>>>>>>>>>>> compilers to<br>
>>>>>>>>>>> support flexible arrays?<br>
>>>>>>>>>> My impression is that flexible arrays are already supported (as<br>
>>>>>>>>>> seen<br>
>>>>>>>>>> in UnitTestFrameworkPkg/PrivateInclude/UnitTestFrameworkTypes.h).<br>
>>>>>>>>>> Please correct me if I am wrong.<br>
>>>>>>>>>><br>
>>>>>>>>>> Would you mind letting me know why this is applicable here? We are<br>
>>>>>>>>>> trying to seek ideas on how to catch developer mistakes caused by<br>
>>>>>>>>>> this<br>
>>>>>>>>>> change. So any input is appreciated.<br>
>>>>>>>>> Huh, interesting. Last time I tried I was told about<br>
>>>>>>>>> incompatibilities<br>
>>>>>>>>> with MSVC, but I know some have been dropped since then (2005 and<br>
>>>>>>>>> 2008<br>
>>>>>>>>> if I recall correctly?), so that'd be great to allow globally.<br>
>>>>>>>> I too am surprised to see<br>
>>>>>>>> "UnitTestFrameworkPkg/PrivateInclude/UnitTestFrameworkTypes.h". The<br>
>>>>>>>> flexible array member is a C99 feature, and I didn't even know<br>
>>>>>>>> that we<br>
>>>>>>>> disallowed it for the sake of particular VS toolchains -- I<br>
>>>>>>>> thought we<br>
>>>>>>>> had a more general reason than just "not supported by VS versions X<br>
>>>>>>>> and Y".<br>
>>>>>>>><br>
>>>>>>>> The behavior of OFFSET_OF() would be interesting -- the OFFSET_OF()<br>
>>>>>>>> macro definition for non-gcc / non-clang:<br>
>>>>>>>><br>
>>>>>>>> #define OFFSET_OF(TYPE, Field) ((UINTN) &(((TYPE *)0)->Field))<br>
>>>>>>>><br>
>>>>>>>> borders on undefined behavior as far as I can tell, so its<br>
>>>>>>>> behavior is<br>
>>>>>>>> totally up to the compiler. It works thus far okay on Visual<br>
>>>>>>>> Studio, but<br>
>>>>>>>> I couldn't say if it extended correctly to flexible array members.<br>
>>>>>>> Yes, it's UB by the standard, but this is actually how MS implements<br>
>>>>>>> them (or used to anyway?). I don't see why it'd cause issues with<br>
>>>>>>> flexible arrays, as only the start of the array is relevant (which is<br>
>>>>>>> constant for all instances of the structure no matter the amount of<br>
>>>>>>> elements actually stored). Any specific concern? If so, they could be<br>
>>>>>>> addressed by appropriate STATIC_ASSERTs.<br>
>>>>>> No specific concern; my point was that two aspects of the same "class"<br>
>>>>>> of undefined behavior didn't need to be consistent with each other.<br>
>>>>>><br>
>>>>>> Thanks<br>
>>>>>> Laszlo<br>
>>>>>><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>


 <div width="1" style="color:white;clear:both">_._,_._,_</div> <hr>   Groups.io Links:<p>   You receive all messages sent to this group.    <p> <a target="_blank" href="https://edk2.groups.io/g/devel/message/77250">View/Reply Online (#77250)</a> |    |  <a target="_blank" href="https://groups.io/mt/83863450/1813853">Mute This Topic</a>  | <a href="https://edk2.groups.io/g/devel/post">New Topic</a><br>    <a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> | <a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |  <a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>  [edk2-devel-archive@redhat.com]<br> <div width="1" style="color:white;clear:both">_._,_._,_</div>