[PATCH] D136805: [NFC] Make format() more amenable to format attributes

FĂ©lix Cloutier via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 26 17:35:59 PDT 2022


fcloutier created this revision.
fcloutier added a reviewer: ahatanak.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fcloutier requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Second take on https://reviews.llvm.org/rGfb1e90ef07fec0d64a05c0b6d41117a5ea3e8344, which was pulled out because it broke GCC-based builds. The offending code was:

  constexpr uint64_t specifierBit(char C) { return 1 << (C - 0x40); }

The issue is that the shift expression has type `int`, but the shift value has range 0..<0x40. Shifting `(int)1` by more than 30 is undefined behavior.

With GCC, this manifested with test failures, and when sanitizers were enabled, with sanitizer traps. However, because the function is `constexpr`, Clang chose to evaluate it at compile-time even when when its execution was undefined. (Both Clang and GCC reject calls to `specifierBit` and its `specifierMask` caller when using `constinit`.) Unfortunately, somehow, the result was good enough to pass all tests when built with clang; and since the undefined behavior was evaluated at build time, UBSan wouldn't catch it.

I have reproduced the problem with a GCC build of LLVM and verified that this change fixes it:

  constexpr uint64_t specifierBit(char C) { return (uint64_t)1 << (C - 0x40); }


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136805

Files:
  llvm/include/llvm/Support/Format.h
  llvm/lib/Support/CMakeLists.txt
  llvm/lib/Support/Format.cpp
  llvm/lib/TableGen/SetTheory.cpp
  llvm/unittests/Support/CMakeLists.txt
  llvm/unittests/Support/FormatChkTest.cpp
  llvm/utils/TableGen/GlobalISel/GIMatchDag.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136805.470974.patch
Type: text/x-patch
Size: 27847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221027/f6d74a59/attachment.bin>


More information about the llvm-commits mailing list