[all-commits] [llvm/llvm-project] 875391: Avoid strict aliasing violation on type punning in...

Breno Rodrigues GuimarĂ£es via All-commits all-commits at lists.llvm.org
Thu Feb 23 09:03:42 PST 2023

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 875391728c11339c8a6cd3338bcaa5ec0ffc2496
  Author: Breno GuimarĂ£es <brenorg at gmail.com>
  Date:   2023-02-23 (Thu, 23 Feb 2023)

  Changed paths:
    M llvm/include/llvm/ADT/PointerIntPair.h
    M llvm/unittests/ADT/PointerIntPairTest.cpp

  Log Message:
  Avoid strict aliasing violation on type punning inside llvm::PointerIntPair

llvm::PointerIntPair has methods that when used together can invoke
undefined behavior by violating strict aliasing.

`getPointer()` uses the underlying storage as it's declared: `intptr_t`
`getAddrOfPointer()` casts the underlying storage as if it was a

This violates strict aliasing, so depending on how they are used, it's
possible to have the compiler to optimize the code in unwanted ways.
See the unit test in the patch. We declare a `PointerIntPair` and use
the `getAddrOfPointer` method to fill in the a pointer value.  Then,
when we use `getPointer` the compiler is thrown off, thinking that
`intptr_t` storage could not have possibly be changed, and the check

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D124571

More information about the All-commits mailing list