On Mar 8, 2013, at 4:14 AM, Paolo Bonzini <[email protected]> wrote:

> Il 08/03/2013 01:14, Andrew Fish ha scritto:
>> We just ran into an issue that I thought was worth sharing with the
>> group. We just updated our compiler and a memory test got optimized
>> away. At first we thought it was a compiler bug. But then we were
>> informed that in the C language the dereference of a NULL pointer is
>> undefined behavior. So technically it is legal for the compiler to
>> optimize it way.
>> 
>> So to be pedantic in C if you ever dereference a NULL point on
>> purpose you need to make it a volatile to conform to the C standard.
>> A memory test may want to access address  0, and code that writes the
>> ARM or 8086 vector table may also end up dereferencing a NULL
>> pointer.
>> 
>> I think folks will find Chris Lattner's blog interesting
>> http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
> 
> Do you know what MSVC does about this?

No I don't. But given that the operation is not defined by C the compiler is 
free to do what ever it wants. A future version of MSVC could change its 
behavior. 

>  For GCC/clang, it is probably
> best to add -fno-delete-null-pointer-checks to the command line, because
> a NULL pointer dereference will not cause abnormal termination in
> TianoCore.  The Linux kernel uses the same option.
> 

What we do is is add this flag to clang:
-ftrap-function=undefined_behavior_has_been_optimized_away_by_clang

When clang optimizes undefined behavior away it will insert a ud2 (trap on ARM) 
instruction. The -ftrap-function replaces the ud2 instruction with a call to 
the function. This will give you a link error when your code contains undefined 
behavior. Do you know what gcc does in this case? 

> Paolo


------------------------------------------------------------------------------
Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester  
Wave(TM): Endpoint Security, Q1 2013 and "remains a good choice" in the  
endpoint security space. For insight on selecting the right partner to 
tackle endpoint security challenges, access the full report. 
http://p.sf.net/sfu/symantec-dev2dev
_______________________________________________
edk2-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/edk2-devel

Reply via email to