https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81880

            Bug ID: 81880
           Summary: thread_local static member template initialisation
                    fails
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: maiphi.public at gmx dot net
  Target Milestone: ---

Created attachment 41997
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=41997&action=edit
preprocessed source

thread_local static member templates do not get initialised.

Example code:

#include <unordered_map>
#include <iostream>

class A {
public:
  template<typename T>
  thread_local static std::unordered_map<int,T> m;
};

template<typename T>
thread_local std::unordered_map<int,T> A::m{};

int main() {
  // A::m<int> = std::unordered_map<int,int>{}; // workaround
  std::cout << A::m<int>.bucket_count() << std::endl; // returns zero.
  A::m<int>.insert({1,2}); // causes SIGPFE (hash modulo bucket_count)
}

The unordered_map is not initialised and has a bucket count of zero. This leads
to a zero division when the hash is taken modulo the bucket count. Without the
thread_local or without the template it works fine. A workaround is to
initialise the member manually in every thread which uses it (commented line).

OS: opensuse Leap 42.3, kernel 4.4.79-19-default x64
CPU: Intel Core i7-4790

$ g++ -o bug -v -save-temps -std=c++14 -Wall -Wextra -pedantic bug.cpp 
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc-svn/configure --prefix=/path/to/gcc/svn
--disable-multilib --enable-languages=c,c++,fortran
Thread model: posix
gcc version 8.0.0 20170817 (experimental) (GCC) 
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall'
'-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/cc1plus -E -quiet -v
-D_GNU_SOURCE bug.cpp -mtune=generic -march=x86-64 -std=c++14 -Wall -Wextra
-Wpedantic -fpch-preprocess -o bug.ii
ignoring nonexistent directory
"/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0

/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0/x86_64-pc-linux-gnu

/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../include/c++/8.0.0/backward
 /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/include
 /usr/local/include
 /path/to/gcc/svn/include
 /path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall'
'-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/cc1plus -fpreprocessed
bug.ii -quiet -dumpbase bug.cpp -mtune=generic -march=x86-64 -auxbase bug -Wall
-Wextra -Wpedantic -std=c++14 -version -o bug.s
GNU C++14 (GCC) version 8.0.0 20170817 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 8.0.0 20170817 (experimental), GMP version
6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
GNU C++14 (GCC) version 8.0.0 20170817 (experimental) (x86_64-pc-linux-gnu)
        compiled by GNU C version 8.0.0 20170817 (experimental), GMP version
6.1.2, MPFR version 3.1.5, MPC version 1.0.3, isl version none
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 7aa65dd64f7a61cf2867713db376431f
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall'
'-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o bug.o bug.s
GNU assembler version 2.26.1 (x86_64-suse-linux) using BFD version (GNU
Binutils; openSUSE Leap 42.3) 2.26.1
COMPILER_PATH=/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:./:/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall'
'-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/collect2 -plugin
/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/liblto_plugin.so
-plugin-opt=/path/to/gcc/svn/lib/gcc/x86_64-pc-linux-gnu/8.0.0/lto-wrapper
-plugin-opt=-fresolution=bug.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc
--eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o bug
/usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o
/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/crtbegin.o
-L/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0
-L/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../../../lib64
-L/lib/../lib64 -L/usr/lib/../lib64 -L.
-L/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/../../.. bug.o -lstdc++
-lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/path/to/gcc/svn/lib64/gcc/x86_64-pc-linux-gnu/8.0.0/crtend.o
/usr/lib/../lib64/crtn.o
COLLECT_GCC_OPTIONS='-o' 'bug' '-v' '-save-temps' '-std=c++14' '-Wall'
'-Wextra' '-Wpedantic' '-shared-libgcc' '-mtune=generic' '-march=x86-64'

$ ./bug
0
Floating point exception (core dumped)

(also confirmed with gcc 7.1.1 and gcc 5.2.0)

Reply via email to