On Aug 6, 2014, at 10:29 AM, Laszlo Ersek <[email protected]> wrote:
> On 08/06/14 19:05, Andrew Fish wrote:
>> LegacyBiosInt86() sets a pointer to 0 and dereferences it. From a C
>> standard point of view this is considered undefined behavior. The clang
>> optimizer emits a trap instruction and throws all the code after the
>> reference away. The trap is replaced via a compiler flag to be a
>> function that does not exist, and thus this is why we get a link failure.
>>
>> The optimizer needs to avoid optimizations on a volatile variable, so
>> the only safe way to write to zero in C is to use a volatile variable.
>> See http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
>>
>> Contributed-under: TianoCore Contribution Agreement 1.0
>> Signed-off-by: Anderw Fish <[email protected] <mailto:[email protected]>>
>>
>> ~/work/src/edk2(master)>git diff
>> IntelFrameworkModulePkg/Csm/LegacyBiosDxe/Thunk.c
>> *diff --git a/IntelFrameworkModulePkg/Csm/LegacyBiosDxe/Thunk.c
>> b/IntelFrameworkModulePkg/Csm/LegacyBiosDxe/Thunk.c*
>> *index 3d9a8b9..a5d2f14 100644*
>> *--- a/IntelFrameworkModulePkg/Csm/LegacyBiosDxe/Thunk.c*
>> *+++ b/IntelFrameworkModulePkg/Csm/LegacyBiosDxe/Thunk.c*
>> @@ -57,7 +57,7 @@ LegacyBiosInt86 (
>> IN EFI_IA32_REGISTER_SET *Regs
>> )
>> {
>> - UINT32 *VectorBase;
>> + volatile UINT32 *VectorBase;
>>
>>
>>
>> Regs->X.Flags.Reserved1 = 1;
>> Regs->X.Flags.Reserved2 = 0;
>>
>>
>> Thanks,
>>
>> Andrew Fish
>>
>> PS I finally got around to testing clang out on OVFM. Right now I’m in a
>> “World of Hurt”(TM) trying to get
>> UefiCpuPkg/Library/CpuExceptionHandlerLib to work. It seems that
>> CpuExceptionHandlerLib assembly has a lot of X64 relocations that the
>> Xcode linker does not support.
>
> (1) Patch
> Reviewed-by: Laszlo Ersek <[email protected]>
>
> (
> (2) Since we're talking volatile, I can't avoid calling attention
> (again) to SVN r13878. That patch introduces undefined behavior:
>
> 6.7.3 Type qualifiers
>
> 5 If an attempt is made to modify an object defined with a
> const-qualified type through use of an lvalue with
> non-const-qualified type, the behavior is undefined. If an attempt
> is made to refer to an object defined with a volatile-qualified
> type through use of an lvalue with non-volatile-qualified type, the
> behavior is undefined.
>
I’m thinking the issue with const is the compiler can assume it never changes,
but casting to something else changes that. For volatile if you mix pointer
types you lose the serialization property of the volatile.
> This is the prototype of ZeroMem():
>
> VOID *
> EFIAPI
> ZeroMem (
> OUT VOID *Buffer,
> IN UINTN Length
> );
>
> Note the absence of volatile. "Request" and "Response" are defined with
> a volatile-qualified type.
> )
>
This is a complex subject. I think the use of --fno-strict-aliasing has an
impact here as it disables the optimization . Also given the compiler could
insert a memset() I’m guessing this may be one of the exceptions? I’ll try and
track down an compiler expert and ask a question about this.
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
Violating Type Rules: It is undefined behavior to cast an int* to a float* and
dereference it (accessing the "int" as if it were a "float"). C requires that
these sorts of type conversions happen through memcpy: using pointer casts is
not correct and undefined behavior results. The rules for this are quite
nuanced and I don't want to go into the details here (there is an exception for
char*, vectors have special properties, unions change things, etc). This
behavior enables an analysis known as "Type-Based Alias Analysis" (TBAA) which
is used by a broad range of memory access optimizations in the compiler, and
can significantly improve performance of the generated code. For example, this
rule allows clang to optimize this function:
float *P;
void zero_array() {
int i;
for (i = 0; i < 10000; ++i)
P[i] = 0.0f;
}
into "memset(P, 0, 40000)". This optimization also allows many loads to be
hoisted out of loops, common subexpressions to be eliminated, etc. This class
of undefined behavior can be disabled by passing the -fno-strict-aliasing flag,
which disallows this analysis. When this flag is passed, Clang is required to
compile this loop into 10000 4-byte stores (which is several times slower),
because it has to assume that it is possible for any of the stores to change
the value of P, as in something like this:
int main() {
P = (float*)&P; // cast causes TBAA violation in zero_array.
zero_array();
}
This sort of type abuse is pretty uncommon, which is why the standard committee
decided that the significant performance wins were worth the unexpected result
for "reasonable" type casts. It is worth pointing out that Java gets the
benefits of type-based optimizations without these drawbacks because it doesn't
have unsafe pointer casting in the language at all.
Thanks,
Andrew Fish
PS I’d have written the code to add a volatile pointers RequestPtr/ResponsePtr,
and make Request/Response just local storage. That way passing them to ZeroMem
() would not require a cast.
> Laszlo
>
> ------------------------------------------------------------------------------
> Infragistics Professional
> Build stunning WinForms apps today!
> Reboot your WinForms applications with our WinForms controls.
> Build a bridge from your legacy apps to the future.
> http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
> _______________________________________________
> edk2-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/edk2-devel
------------------------------------------------------------------------------
Infragistics Professional
Build stunning WinForms apps today!
Reboot your WinForms applications with our WinForms controls.
Build a bridge from your legacy apps to the future.
http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
_______________________________________________
edk2-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-devel