https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106665
Bug ID: 106665 Summary: Cannot pass barrier by reference Product: gcc Version: 12.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: shihyente at hotmail dot com Target Milestone: --- Hi, The failed case was tested under Ubuntu 22.04.1 LTS g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/home/yt/Utils/gcc-12.2.0-RC-20220812/libexec/gcc/x86_64-pc-linux-gnu/12.1.1/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: ../configure --prefix=/home/yt/Utils/gcc-12.2.0-RC-20220812 --enable-languages=c,c++ --enable-libstdcxx-backtrace=yes --disable-multilib --enable-checking=yes Thread model: posix Supported LTO compression algorithms: zlib gcc version 12.1.1 20220812 (GCC) Code (test.cpp) #include <vector> #include <thread> #include <barrier> using namespace std; void foo(auto& sync_point) { sync_point.arrive_and_wait(); } int main() { vector<jthread> pool; barrier sync_point(8); for (size_t i = 0; i < 8; ++i) pool.emplace_back([&] { foo(sync_point); }); } Commandline to compile (no warning): /home/yt/Utils/gcc-12.2.0-RC-20220812/bin/g++ -std=c++23 -g -O0 -Wall -Wextra -fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations -fsanitize=undefined -pthread -Wl,-rpath=/home/yt/Utils/gcc-12.2.0-RC-20220812/lib64 test.cpp Run (sometimes only showed "Segmentation fault (core dumped)") /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/bits/unique_ptr.h:720:14: runtime error: reference binding to null pointer of type 'struct __state_t' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/barrier:120:8: runtime error: member access within null pointer of type 'struct __state_t' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/barrier:120:17: runtime error: member call on null pointer of type 'struct __tickets_t' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/array:209:26: runtime error: member access within null pointer of type 'struct array' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/array:209:26: runtime error: reference binding to null pointer of type 'const __barrier_phase_t' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/array:61:41: runtime error: load of null pointer of type 'const __barrier_phase_t' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/array:61:41: runtime error: reference binding to null pointer of type '__barrier_phase_t' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/array:61:41: runtime error: load of null pointer of type 'const __barrier_phase_t' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/array:209:39: runtime error: reference binding to null pointer of type 'value_type' /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/barrier:119:24: runtime error: reference binding to null pointer of type '__barrier_phase_t' Segmentation fault (core dumped) GDB (sometimes only showed "exited normally") Thread 9 "a.out" received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7ffff3c09640 (LWP 374398)] 0x000000000040a5bb in std::__atomic_impl::compare_exchange_strong<std::__barrier_phase_t> (__failure=std::memory_order::acquire, __success=std::memory_order::acq_rel, __desired=(unknown: 0x1), __expected=@0x7ffff3c08baf: 0, __ptr=0xc0) at /home/yt/Utils/gcc-12.2.0-RC-20220812/include/c++/12.1.1/bits/atomic_base.h:1005 1005 return __atomic_compare_exchange(__ptr, std::__addressof(__expected), Known workaround: Do not pass barrier by reference. MSVC and Clang++ are working fine, so I am assuming the test code is legal. Thank you very much. Best, John Shih