Perhaps this is too tangential or has been discussed elsewhere already, but here goes:
This does also raise another question (related to intptr_t in particular), which is how to do pointer tagging on CHERI. After all such techniques are a time honoured tradition in interpreters and JIT compilers in particular (though found elsewhere as well as I will discuss at the end).
I know of several variations of this:
- Using the least significant bits, relying on alignment to mean they are always zero for all pointers. Mask the off and you know what type your pointer points to (or if it is actually not a pointer but something else).
- Use the high bit, user space pointers on amd64 should always have the most significant bit cleared (otherwise it would be a kernel pointer). Not super-portable.
- Use the high bits, virtual address space is less than 64 bits (and physical is even less). Quite risky as future generations of CPUs are released and not very portable.
- I believe that V8 even goes all in on this and stuffs pointers into floats (counting on the address space being less than the 56 bit mantissa), this being referred to as NaN-stuffing. Really a variant on 3. Not super read up on the details of this.
I seem to remember seeing some libraries in Rust that do SSO (small string optimisation) have variants of this for example. And I have seen it in some other heavily optimised data types (there was a tutorial blog post on reddit a few weeks(?) ago about this, I pointed out that the particular implementation was unsound since it was broken on big endian, forgot to mention CHERI).
And the concept of niches in Rust is of course related, though currently the compiler isn't quite as inventive as humans in this area from my understanding. For example, references don't currently have alignment niches, just the zero niche as I understand it. Proper alignment niches would avoid a lot of need for unsafe.
All of this code (except compiler niches) will presumably fail miserably on CHERI. I would also guess that the V8 developers are unhappy giving up on the performance (and as much as I like Firefox, V8 is a Big Deal).
So what to do about all of this mess (keeping in mind I have always been a fan of bit twiddling and clever low level hacks)?