[all-commits] [llvm/llvm-project] 4f4690: [libc++] Ensure that std::expected has no tail pad...

Jan Kokemüller via All-commits all-commits at lists.llvm.org
Mon Jan 22 06:05:51 PST 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 4f4690530e8b40cdf3a17c76a352b26c2fb0446c
      https://github.com/llvm/llvm-project/commit/4f4690530e8b40cdf3a17c76a352b26c2fb0446c
  Author: Jan Kokemüller <jan.kokemueller at gmail.com>
  Date:   2024-01-22 (Mon, 22 Jan 2024)

  Changed paths:
    M libcxx/docs/ReleaseNotes/18.rst
    M libcxx/include/__expected/expected.h
    M libcxx/test/libcxx/utilities/expected/expected.expected/no_unique_address.compile.pass.cpp
    M libcxx/test/libcxx/utilities/expected/expected.expected/transform_error.mandates.verify.cpp
    M libcxx/test/libcxx/utilities/expected/expected.void/no_unique_address.compile.pass.cpp
    M libcxx/test/libcxx/utilities/expected/expected.void/transform_error.mandates.verify.cpp
    M libcxx/test/std/utilities/expected/expected.expected/assign/assign.U.pass.cpp
    M libcxx/test/std/utilities/expected/expected.expected/assign/assign.copy.pass.cpp
    M libcxx/test/std/utilities/expected/expected.expected/assign/assign.move.pass.cpp
    M libcxx/test/std/utilities/expected/expected.expected/assign/emplace.pass.cpp
    M libcxx/test/std/utilities/expected/expected.expected/monadic/transform.pass.cpp
    M libcxx/test/std/utilities/expected/expected.expected/monadic/transform_error.pass.cpp
    M libcxx/test/std/utilities/expected/expected.expected/swap/member.swap.pass.cpp
    M libcxx/test/std/utilities/expected/expected.void/assign/assign.copy.pass.cpp
    M libcxx/test/std/utilities/expected/expected.void/assign/assign.move.pass.cpp
    M libcxx/test/std/utilities/expected/expected.void/assign/assign.unexpected.copy.pass.cpp
    M libcxx/test/std/utilities/expected/expected.void/assign/assign.unexpected.move.pass.cpp
    M libcxx/test/std/utilities/expected/expected.void/monadic/transform_error.pass.cpp
    M libcxx/test/std/utilities/expected/expected.void/swap/member.swap.pass.cpp
    M libcxx/test/std/utilities/expected/types.h

  Log Message:
  -----------
  [libc++] Ensure that std::expected has no tail padding (#69673)

Currently std::expected can have some padding bytes in its tail due to
[[no_unique_address]]. Those padding bytes can be used by other objects.
For example, in the current implementation:

  sizeof(std::expected<std::optional<int>, bool>) == 
    sizeof(std::expected<std::expected<std::optional<int>, bool>, bool>)

As a result, the data layout of an
  std::expected<std::expected<std::optional<int>, bool>, bool> 
can look like this:

              +-- optional "has value" flag
              |        +--padding
  /---int---\ |        |
  00 00 00 00 01 00 00 00
                |  |
                |  +- "outer" expected "has value" flag
                |
                +- expected "has value" flag

This is problematic because `emplace()`ing the "inner" expected can not
only overwrite the "inner" expected "has value" flag (issue #68552) but
also the tail padding where other objects might live.

This patch fixes the problem by ensuring that std::expected has no tail
padding, which is achieved by conditional usage of [[no_unique_address]]
based on the tail padding that this would create.

This is an ABI breaking change because the following property changes:

  sizeof(std::expected<std::optional<int>, bool>) <
    sizeof(std::expected<std::expected<std::optional<int>, bool>, bool>)

Before the change, this relation didn't hold. After the change, the relation
does hold, which means that the size of std::expected in these cases increases
after this patch. The data layout will change in the following cases where
tail padding can be reused by other objects:

  class foo : std::expected<std::optional<int>, bool> {
    bool b;
  };

or using [[no_unique_address]]:

  struct foo {
    [[no_unique_address]] std::expected<std::optional<int>, bool> e;
    bool b;
  };

The vendor communication is handled in #70820.
Fixes: #70494

Co-authored-by: philnik777 <nikolasklauser at berlin.de>
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>




More information about the All-commits mailing list