I am playing around with a programming language implementation, and I'm wondering how (ill) advised it is to press into service the least significant bits of a function pointer to store data.
Are there any major platforms (AMD64/{Windows/Linux/MacOS}, Arm/{iOS,Android}) in which the 2 least significant bits are ever non-zero in function pointers? That is, is the alignment for the code at least 4 on major platforms?
I can tell you that Apple's 64-bit runtime (both ARM64 and Intel, I think) uses the least significant bits for flags broadly as you propose. In Objective-C everything is an object and, to be compatible with C, pretty much every object lives on the heap and is recorded by its pointer. In 64-bit mode they've allowed very small objects to live on the stack by fitting them into 62 bits and using the low two to indicate that this isn't really a pointer but a literal object. So you can get short strings, object-wrapped 32-bit and below numbers, etc, directly into the 'pointer' and not put anything on the heap.
However Apple does not do this with the 32-bit runtime (event the 'modern' one as on iOS). So it might be worth researching why that is. Admittedly it could just be because of some architectural quirk carried over from the PowerPC.
As has been pointed out to me in the comments (and why this is now tagged as community wiki), the C standard differentiates between the storage of function pointers specifically and all other kinds of pointer. So the above comment may or may not be relevant — I nevertheless believe it is because closures are a separate thing again from data and from functions, in compiled languages the code itself usually having been compiled in advance and the closure itself just being the data to fill the gaps. But the point I'm trying to make is that there are shipping, robust systems out there that assume they can reuse the least significant bits of pointers on systems that require alignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With