Hacker News new | ask | show | jobs
by sparkie 10 days ago
The C standard does not make any guarantees that you can dereference it.

And x86-64 is arguably still "segmented" in that you can use the FS and GC segment registers (they're typically used for TLS). Eg, if you cast a `thread_local` pointer to `intptr_t` - the standard makes no requirement that it point to the absolute virtual address (though this is the common implementation choice by GCC etc).

2 comments

The standard is English text which may not alway be perfectly precise, but the wording there clearly was meant to allow this.
I think it's quite clear.

> An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, may not be correctly aligned, may not point to an entity of the referenced type, and can produce an indeterminate representation when stored into an object.

That isn't to say we can't do the cast - compilers specify the behavior, and most modern architectures have a linear virtual address space where the round trip is valid - though it may not always be - for example, with the fs/gs segment registers - a cast of pointer to integer isn't just a simple conversion of type - it must also fetch the value from the FS register and offset the pointer by this to produce an integer containing the absolute address. This is entirely implementation defined.

Producing the absolute virtual address of a `thread_local` is potential UB though - if you attempt to dereference it in a different thread.

Another example would be casting a qualified pointer (const, restrict, volatile) to an integer, then back to a pointer with a different qualifier or no qualifier - then dereferencing such pointer would of course be UB as per the standard - the cast to `intptr_t` loses information about its qualification - but the pointers would still compare equal - as the standard guarantees that a pointer cast to a qualified pointer compares equal.

There are stricter requirements for casting a void pointer to (u)intptr_t and back. Again, a segmented architecture should not provide (u)intptr_t. But if an implementation has it, in my opinion, this is expected to work.
The only stricter requirement in the standard is that they compare equal after the round trip.

The intended purpose of these types is to allow the round trip, yes, and the requirement they compare equal is part of that. The standard doesn't put stricter requirements. I don't think that is an oversight, but a deliberate omission to allow for flexibility in implementation.

It's up to the compiler to specify that it's safe to dereference the pointer after the round trip - the standard does not want to put such constraints on implementors.

This is where I disagree. In the context when this was written, I think it was clear that two pointers that compare equally were meant to be interchangeable.
It would've been trivial for the committee to add such clause in the spec - why only the requirement that they compare equal and not that they dereference the same object?

The standard leaves a lot of things implementation defined deliberately to allow for flexibility in both processor design and compiler implementation.

I don't think for example, a segmented architecture shouldn't provide an `intptr_t` - it's still useful to be able to cast a pointer to integer and back even in the case of segmentation - but is obviously more prone to error if we dereference it in a different segment (which would be UB).

I completely agree that it is the intended case that a pointer that has had this round trip is intended to be dereferenced - but I disagree that the requirement to do so is an oversight and not a deliberate omission.

There are potential cases where you would want to dereference the pointer but to a different object (which is UB by the standard, but could be well defined for a specific compiler/arch). Consider for example, if you have a bunch of threads which all have the same structure in their thread local storage - it can be useful to be able to specify an offset that is going fetch a value specific value from the TLS - but obviously a different one in each thread - ie, we want the pointer to be an offset, and not an absolute address.

FTR C++ makes the guarantee. I'm not familiar enough with C standard wording to comment on that.