Skip to content

Fix a C++ build failure if the C++17 standard is used - #26359

Merged
veblush merged 3 commits into
grpc:masterfrom
hungptit:hd-fix-cxx17-build-failure
Jun 4, 2021
Merged

veblush merged 3 commits into
grpc:masterfrom
hungptit:hd-fix-cxx17-build-failure

Conversation

@hungptit

@hungptit hungptit commented May 25, 2021 •

Copy link
Copy Markdown
Contributor

What is the issue?

Everything works fine if I compile gRPC using C++14, however, I could not build gRPC if the C++17 is used. Below is the build error:

Consolidate compiler generated dependencies of target grpc++_unsecure
[ 66%] Built target grpc++_unsecure
[ 66%] Building CXX object _deps/grpc-build/CMakeFiles/grpc.dir/src/core/lib/security/transport/security_handshaker.cc.o
In file included from /home/hungptit/working/workflow/_deps/grpc-src/src/core/lib/security/transport/security_handshaker.cc:21:
In file included from /home/hungptit/working/workflow/_deps/grpc-src/src/core/lib/security/transport/security_handshaker.h:24:
In file included from /home/hungptit/working/workflow/_deps/grpc-src/src/core/lib/channel/handshaker.h:24:
In file included from /home/hungptit/working/workflow/_deps/grpc-src/third_party/abseil-cpp/absl/container/inlined_vector.h:38:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/algorithm:64:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/bits/ranges_algo.h:35:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/bits/ranges_algobase.h:38:
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/ranges:45:
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:1241:14: error: no matching constructor for initialization of 'optional<grpc_core::channelz::SocketNode::Security::Tls>'
    { return optional<_Tp> { in_place, std::forward<_Args>(__args)... }; }
             ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/hungptit/working/workflow/_deps/grpc-src/src/core/lib/security/transport/security_handshaker.cc:215:25: note: in instantiation of function template specialization 'std::make_optional<grpc_core::channelz::SocketNode::Security::Tls>' requested here
  security->tls = absl::make_optional<channelz::SocketNode::Security::Tls>();
                        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:658:11: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'const std::in_place_t' to 'const std::optional<grpc_core::channelz::SocketNode::Security::Tls>' for 1st argument
    class optional
          ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:658:11: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'const std::in_place_t' to 'std::optional<grpc_core::channelz::SocketNode::Security::Tls>' for 1st argument
    class optional
          ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:692:17: note: candidate constructor not viable: no known conversion from 'const std::in_place_t' to 'std::nullopt_t' for 1st argument
      constexpr optional(nullopt_t) noexcept { }
                ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:700:2: note: candidate template ignored: requirement '__and_v<std::__not_<std::is_same<std::optional<grpc_core::channelz::SocketNode::Security::Tls>, std::in_place_t>>, std::__not_<std::is_same<std::in_place_t, std::in_place_t>>, std::is_constructible<grpc_core::channelz::SocketNode::Security::Tls, const std::in_place_t &>, std::is_convertible<const std::in_place_t &, grpc_core::channelz::SocketNode::Security::Tls>>' was not satisfied [with _Up = const std::in_place_t &]
        optional(_Up&& __t)
        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:708:2: note: candidate template ignored: requirement '__and_v<std::__not_<std::is_same<std::optional<grpc_core::channelz::SocketNode::Security::Tls>, std::in_place_t>>, std::__not_<std::is_same<std::in_place_t, std::in_place_t>>, std::is_constructible<grpc_core::channelz::SocketNode::Security::Tls, const std::in_place_t &>, std::__not_<std::is_convertible<const std::in_place_t &, grpc_core::channelz::SocketNode::Security::Tls>>>' was not satisfied [with _Up = const std::in_place_t &]
        optional(_Up&& __t)
        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:717:2: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'const std::in_place_t'
        optional(const optional<_Up>& __t)
        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:729:2: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'const std::in_place_t'
        optional(const optional<_Up>& __t)
        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:741:2: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'const std::in_place_t'
        optional(optional<_Up>&& __t)
        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:753:2: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'const std::in_place_t'
        optional(optional<_Up>&& __t)
        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:762:2: note: candidate template ignored: requirement '__and_v<std::is_constructible<grpc_core::channelz::SocketNode::Security::Tls>>' was not satisfied [with _Args = <>]
        optional(in_place_t, _Args&&... __args)
        ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:690:17: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
      constexpr optional() = default;
                ^
/usr/lib/gcc/x86_64-pc-linux-gnu/10.3.0/include/g++-v10/optional:770:2: note: candidate constructor template not viable: requires at least 2 arguments, but 1 was provided
        optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
        ^
1 error generated.
make[2]: *** [_deps/grpc-build/CMakeFiles/grpc.dir/build.make:6684: _deps/grpc-build/CMakeFiles/grpc.dir/src/core/lib/security/transport/security_handshaker.cc.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:1531: _deps/grpc-build/CMakeFiles/grpc.dir/all] Error 2

The solution

Do not use absl::make_optional and we can assign the channelz::SocketNode::Security::Tls() directly to the optional field, i.e security->tls.

@donnadionne
@veblush

@veblush

veblush commented May 25, 2021

Copy link
Copy Markdown
Contributor

@hungptit Could you elaborate why this PR is the right fix? C++17 doesn't allow to use this?

@hungptit

hungptit commented May 25, 2021 •

Copy link
Copy Markdown
Contributor Author

Great question @veblush.

This is the only place in the grpc repo that uses absl::make_optional while there are 196 absl::optional calls so it gives us the hint that there may be a potential issue. Now, let take a look at the std::make_optional document assuming absl::make_optional has similar behaviors. We can see that the empty args will lead us to either option 2 or 3 that requires Tls to be need not be movable for overloads (2-3) due to guaranteed copy elision. In this case Tls move constructor somehow is not visible to the compiler therefore it errors out. The fix is simple, we only assign a Tls object to the optional object instead of using absl::make_optional and let the absl::optional constructor do the work.

@veblush

veblush commented May 26, 2021

Copy link
Copy Markdown
Contributor

Hmm i checked whether g++ 10.3 can compile the following code similar to the original code. absl::optional becomes an alias to std::optional (that's why the error log contains only std source files)

$ docker run -it --rm gcc:10
$ cat > a.cc
#include <optional>
#include <string>

struct Tls {
 enum class NameType { kUnset = 0, kStandardName = 1, kOtherName = 2 };
 std::string name;
 std::string local_certificate;
 std::string remote_certificate;
 void RenderJson();
};

int main(void) {
  std::optional<Tls> a = std::make_optional<Tls>();
  return 0;
}

$ g++ --std=c++17 a.cc

Maybe this was caused by how the build is configured to use C++17. Can you let me how to reproduce this error?

@antoniovicente

Copy link
Copy Markdown

By default abseil aliases absl::optional to std::optional when building in c++ 17 mode.

I ran into this same breakage when trying to update Envoy's grpc dependency to a recent grpc release. See envoyproxy/envoy#16687

antoniovicente added a commit to antoniovicente/envoy that referenced this pull request May 27, 2021
Signed-off-by: Antonio Vicente <[email protected]>
@veblush

veblush commented May 27, 2021

Copy link
Copy Markdown
Contributor

@antoniovicente I want to understand why this code causes the breakage with C++17 mode. I checked the code above and it was compiled well.

@antoniovicente

Copy link
Copy Markdown

@antoniovicente I want to understand why this code causes the breakage with C++17 mode. I checked the code above and it was compiled well.

I don't understand the interaction. My attempts so far to do a standalone build of the grpc library haven't worked even with default settings. The compile failure seems to trigger in cases where abseil uses std::optional as an alias for absl::optional. This issue prevents dependent packages that build with C++ 17 from updating the grpc library dependency.

@hungptit

hungptit commented May 28, 2021 •

Copy link
Copy Markdown
Contributor Author

@veblush Your concern is valid. The document of std::make_optional is incomplete and my explanation is not 100% correct. I think the move constructor of Tls is somehow not visible to clang++ and I do not have a clear answer for that. You can either try below struct or use this repository to reproduce the error. Let me know if you are unable to reproduce the issue.

struct Tls {
  Tls(Tls&& val) = delete;
  std::string name;
  std::string local_certificate;
  std::string remote_certificate;
};

@bijanoviedo

bijanoviedo commented May 28, 2021 •

Copy link
Copy Markdown

Hi, FWIW, I am also facing this issue and managed to create a minimum reproducible example of the build failure: see this godbolt link: https://godbolt.org/z/M17xc6YbE, which fails on clang (e.g. x86-64 clang 11.0.1) but works fine on gcc (e.g. x86-64 gcc 11.1). Really bizarre: I think this is a bug with clang (or a bug with the standard library, or maybe undefined behavior).

@bijanoviedo

bijanoviedo commented May 28, 2021 •

Copy link
Copy Markdown

Looks like it's a bug with the standard library (I would guess there's a problem with the implementation of std::is_constructible in libstdc++): use --std=c++17 -stdlib=libc++ and it compiles just fine, but --std=c++17 -stdlib=libstdc++ causes a compilation failure.


EDIT: Sure enough: it's a known bug when clang, libstdc++, std::optional (used here since absl::optional is an alias for std::optional in >c++17), nested classes, and default member initialization are combined: https://bugs.llvm.org/show_bug.cgi?id=50346.

I think the best workaround for this is to explicitly define the empty zero-parameter constructor, rather than relying on implicit generation, so:

struct Tls {
      Tls() {}

      enum class NameType { kUnset = 0, kStandardName = 1, kOtherName = 2 };
      NameType type = NameType::kUnset;
      // Holds the value of standard_name or other_names if type is not kUnset.
      std::string name;
      std::string local_certificate;
      std::string remote_certificate;

      Json RenderJson();
    };

This makes absl::make_optional<channelz::SocketNode::Security::Tls>() work as expected while avoiding any potential undefined behavior around whether the implicitly-defined default constructor of the inner class (Tls) is supposed to be defined at the time that std::is_constructible is evaluated in the call to std::make_optional.

@veblush

veblush commented Jun 1, 2021

Copy link
Copy Markdown
Contributor

@bijanoviedo Great! Thank you for the investigation and explanation. It's now clearly understandable. So this needs some workaround to detour this compiler issue. This can be either an assignment with the value which is suggested by this or having an explicit empty constructor to detour this issue. I think the latter would be clearer to say why this is written like this

struct Tls {
      // This is a workaround for some C++17 compilers.
      // (https://github.com/grpc/grpc/pull/26359)
      Tls() {}
      // ....
};

@hungptit Can you try to update the PR like above?

@veblush veblush added release notes: no Indicates if PR should not be in release notes lang/core labels Jun 1, 2021
@hungptit

hungptit commented Jun 2, 2021 •

Copy link
Copy Markdown
Contributor Author

@veblush Let lay out the pros and cons of both approaches then we can pick our solution.

Using assignment operator

Pros

  1. This is a supported semantic and it is not a workaround.
  2. We make no assumption about the tls field type and let the = operator does the work.

Cons

We use = operator to bypass the compiler bug that will be gone in the near future.

Adding a non-trivial constructor

Pros

Show that we need this workaround to bypass the compiler bug.

Cons

There are a couple of issues with this approach

  • Adding a non-trivial empty constructor is not idiomatic. Note that using = default does not fix the compilation error and I am not sure if this solution works with Visual Studio 2015.
  • A follow-up task is required because we need to remove the non-trivial constructor after the LLVM's bug is fixed.

Let me know your thought so we can merge this change ASAP?

@bijanoviedo

Copy link
Copy Markdown

My $0.02: I think both solutions are fine and are certainly better than delaying getting this PR in, but another con of the "assignment operator" approach is that anyone in the future could do absl::make_optional<Tls>() and the automated tests won't catch it: in other words, I'm not too convinced that absl::make_optional<Tls>() can't sneak its way into another grpc release. The "non-trivial constructor" approach makes future build errors around this much less likely.

As an end user, I personally care more that each grpc release builds reliably than I care that the underlying code is idiomatic, but I do recognize the tradeoffs.

@hungptit

hungptit commented Jun 2, 2021

Copy link
Copy Markdown
Contributor Author

@bijanoviedo Your points are valid. Let me do a quick update so we can merge this PR today.

@veblush

veblush commented Jun 2, 2021

Copy link
Copy Markdown
Contributor

@hungptit Thank you for the update. @bijanoviedo I agree with your point.

@hungptit

hungptit commented Jun 4, 2021

Copy link
Copy Markdown
Contributor Author

@veblush I cannot merge my changes. Can you merge it?

@veblush
veblush merged commit 5ea57ec into grpc:master Jun 4, 2021
@veblush

veblush commented Jun 4, 2021

Copy link
Copy Markdown
Contributor

@hungptit Sure I can do! Thank you!

@hungptit
hungptit deleted the hd-fix-cxx17-build-failure branch June 4, 2021 21:50

@gnossen gnossen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retroactive LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lang/core release notes: no Indicates if PR should not be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants