[PATCH] D124571: Avoid strict aliasing violation on type punning inside llvm::PointerIntPair
Eli Friedman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue May 3 13:37:49 PDT 2022
efriedma added a comment.
In D124571#3489362 <https://reviews.llvm.org/D124571#3489362>, @brenoguim wrote:
>> I think strictly speaking, to avoid UB you might need to declare "Data" as a union: otherwise, the compiler might assume the value it contains is aligned. I don't think clang does, in practice, but probably you want to be conservative.
>
> Do you mean that, in the current code, the compiler could see that I'm reading `Data` through a memcpy into `intptr_t` and then checking the last bits and say: "Hey, if these bits came from Data, and Data is a pointer to something that must be aligned, then those bits are certainly zero!" ?
I'm thinking something more like: you pass a PunnedPointer as an argument to a function. It's passed in a register, and that register can be marked up with alignment. There was a proposed clang patch for this at one point, but it didn't land because we were concerned about breaking things.
> In that case my conclusion would be to use an aligned char array instead of a pointer. I didn't understand how using a union (union with which members?) would help. I would also have to track the active member of the union to avoid tripping in another UB as well.
I was thinking a `union { Ptr Data; void* VoidPtr }` or something like that. If you access the union via memcpy, it shouldn't matter which member of the union is active, I think?
An aligned char array is probably also fine, though.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D124571/new/
https://reviews.llvm.org/D124571
More information about the llvm-commits
mailing list