[LLVMdev] Is address space 1 reserved?

David Chisnall David.Chisnall at cl.cam.ac.uk
Fri Jan 9 02:06:27 PST 2015


On 9 Jan 2015, at 00:52, Sanjoy Das <sanjoy at playingwithpointers.com> wrote:
> 
>> C requires that (void*)0 generates a pointer that does not compare equal to any valid pointer.  It does not require that (void*)foo, where foo is an int of value 0 but not an integer constant expression, give the same value,
> 
> Does this mean constant propagation can change program semantics?

Yes, that's one of the issues, if you do not enforce this guarantee for all pointers that are derived from integers that have a numerical value of 0.  A strict reading of the C standard means that:

void *null = 1-1; // Null pointer, 1-1 is an ICE
int zero = 0;
void *c = zero; // Not guaranteed to be null, zero is not an ICE.  Will be null (almost?) everywhere, so programmers expect this to work.
_Bool d = zero == (int)c; // Not guaranteed to be true, but will be (almost?) everywhere so programmers expect it to work.
_Bool e = 0 == (int)null; // Guaranteed to be true

Trivial constant propagation means that c will be a null pointer, but without it then it may be a pointer to some valid object (although whether you're actually allowed to construct a pointer like this is implementation defined).

Some of my colleagues are working on a parameterisable formal specification for C, covering what the standard says, what compilers implement, and what programmers expect.  There's a distressingly large amount that isn't in the intersection of these three.

David





More information about the llvm-dev mailing list