Hacker News new | ask | show | jobs
by quelsolaar 10 days ago
The argument that this is valid defined C is not entirely correct. You can not subtract one pointer form another that doesn't come from the same allocation, that is explicitly UB. But the user here subtracts one integer from the other and that is valid. However C only guarantees you can you can convert a integer to a valid pointer, if that integer has been converted from a pointer, and not been modified in any way. If the integer has been modified its implementation defined what happens. Its no longer valid standard C.

C supports pointers that can have memory banks, or pointers that are consist of a hash and and an offset. Real world hardware architectures like CHERI does this and standard C supports this. In hardware like that, this would break, even if the subtraction is done on integers, and don't violate the no subtraction of pointer of different allocation rule. So no, Pointers are really not integers, even if they may look like integers on your machine.

E (Member of the wg14 memory model study group.)

5 comments

If you convert a pointer to `intptr_t`/`uintptr_t` and back again to a pointer, the standard only guarantees that it compares equal to the original - but it makes no guarantee that you can dereference that pointer.

For example, if you had a system with segmented memory, you could cast a pointer used in one segment to `intptr_t`, change the segment register, then attempt to cast back to a pointer and dereference it, which of course is UB.

Pointers are not merely bit vectors. Their representation depends on architecture, and there's no guarantee that they even have an integer representation (intptr_t/uintptr_t are optional) - and even if they do have an integer representation, there's no guarantee that this is a complete enough representation to use the pointer - eg, with the segmentation example above - the `intptr_t` does not contain the segment information.

The examples work as one would expect because they're being run on a system with a single linear virtual address space, where pointers can be represented by an integer. The compiler for such platforms may specify what behavior is to occur in these cases - but the C standard does not.

This is in theory true, but almost all the machines for which pointers were not integers have died out. There were a lot of them during the 16 to 32 bit transition, the Intel 286 being the best known. There were tagged machines, such as Burroughs mainframes and Symbolics LISP machines, but those are mostly forgotten.

Where this is likely to bite today is on machines where some memory is shared between processors, and some isn't. That's non-uniform memory access.

That takes us back to banked memory. We see this most commonly when GPU memory is exposed to the CPU, but there are Intel processors which support multiprocessor mixed shared and non-shared access. Linux apparently supports this. Pointer arithmetic between pointers from different banks is not meaningful.

The general safe rule is that, at some level, something allocates memory, and it returns something for which arithmetic is valid only on a part. There may be suballocators, as with Vulkan. Vulkan has an allocator for big blocks, and applications then need an allocator which subdivides those big blocks.

This leads to slices. If a slice is safe, a subslice is also safe. If all operations are on slices, you're good. That's the Rust philosophy.

Historically, slices came very late. If someone had thought of slices in the 1970s, they probably would have been in C, which would have eliminated most of the need for pointer arithmetic. Slices are just bounded pointer arithmetic, really. That C lacks slices is the underlying cause of many headaches.

>This is in theory true, but almost all the machines for which pointers were not integers have died out.

Yes and this went all the way to the point where even foreign device memory is now mapped into that address space. The address space is linear but its backing memory is not.

Linear address spaces are perfectly logical on the machine level because it is just an arrangement of bits which is easy for silicon to process.

The actual problem that we are hitting is that this linear address space is carved up into (possibly nested) bounded regions and you are not allowed to go outside the region for a given pointer. This is what CHERI, etc do, they add the region information back on the machine level which clears up the confusion.

The standard copies the same language used for defining void pointer conversions, that you can convert any object pointer to a void pointer and back again and the result will compare equal. So your interpretation would also necessarily imply the standard doesn't guarantee you can dereference such a pointer. In legal textual analysis that would count as an absurdity, meaning it's obviously not a reasonable interpretation.

Should the language be made more explicit? Maybe, but any such change opens the door to errors. For example, modified language may need to explicitly account for object lifetimes. Pandering to every imaginative interpretation is a losing game; it's a balancing act, and there are many other higher priority tasks to attend to.

> So your interpretation would also necessarily imply the standard doesn't guarantee you can dereference such a pointer.

It's not the same argument because the type in this case is still a pointer (which is not strictly an integer).

The standard is quite clear about conversion between pointers and integers.

> 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.

> Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result is not required to be in the range of values of any integer type.

The purpose of `intptr_t` is to solve that last issue: it's an integer type sufficiently wide to hold a pointer, so that the result is in the range of values held by the intptr_t - and thus the round trip compares equal to the original pointer - but there's nothing else specified in the spec about intptr_t solving the other issues outlined in these clauses.

From C23 (N3220), compare

> 6.3.2.3 Pointers

> A pointer to void may be converted to or from a pointer to any object type. A pointer to any object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

and

> 7.22.1.4 Integer types capable of holding object pointers

> The following type designates a signed integer type, other than a bit-precise integer type, with the property that any valid pointer to void can be converted to this type, then converted back to pointer to void, and the result will compare equal to the original pointer:

> intptr_t

intptr_t is not like other integer types. It has special properties and guarantees that other integer types might not, even when they're the same width.

And I think your interpretation about dereferencing may be ruled out by 6.5.10p7:

> Two pointers compare equal if and only if both are null pointers, both are pointers to the same object....

That is, if they compare equal and one is a valid object pointer then the other is a valid object pointer, regardless of whether it was round-tripped through intptr_t/uintptr_t.

I hear this claim sometimes, but I think this is a far-fetched interpretation that you can only compare the pointers after backconversion but not use them.

A segmented architecture that can not support this should not support intptr_t. It is optional for this reason.

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).

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.
FTR C++ makes the guarantee. I'm not familiar enough with C standard wording to comment on that.
There are also Harvard architecture machines like microcontrollers where a function pointer points to ROM and a data pointer points to RAM.

Feels a little like Harvard architecture says there is not global axiom of choice and von Neuman architecture and single tape Turing machines explicitly count everything in the same memory (except for the logic in the Turing machine control head.)

> However C only guarantees you can you can convert a integer to a valid pointer, if that integer has been converted from a pointer, and not been modified in any way. If the integer has been modified its implementation defined what happens. Its no longer valid standard C.

The C standard doesn't even guarantee that. It only guarantees the conversion from 0 to a null pointer. Any other integer-to-pointer conversion is implementation defined.

What implementations _want_ to define is a different question though.

I'm not a C expert, however I do follow Rust's discussions about pointer provenance. It might be unspecified but people do rely on this (e.g. pointer tagging is the same under your explanation), and even CHERI uses hacks to not break them, at least whenever possible. Furthermore the standard says in a footnote:

> The mapping functions for converting a pointer to an integer or an integer to a pointer are intended to be consistent with the addressing structure of the execution environment.

So even if it's not mandatory, it is expected to work.

> However C only guarantees you can you can convert a integer to a valid pointer, if that integer has been converted from a pointer, and not been modified in any way.

How does that work when I want to set an address high?

You cant in standard C, but many implementations do let you manipulate pointers in all kinds ow ways, but then your code depends on an extension of the language and is not portable. C is designed so that you can write both portable and non portable code. I would argue you shouldn't because often these extensions are very poorly documented and are based mostly on "it seems to work". Try when possible to stay within the standard.