by quelsolaar · 1w ago · 0 comments
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.)
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.
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.
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.
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.
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.
> 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.
A segmented architecture that can not support this should not support intptr_t. It is optional for this reason.
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).
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.)
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.
> 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.
How does that work when I want to set an address high?