Skip to content

Strict aliasing issue in threads #2340

@thesamesam

Description

@thesamesam

Originally reported downstream in Gentoo at https://bugs.gentoo.org/973164. We build with -Werror=strict-aliasing to help catch future compiler incompatibilities.

x86_64-pc-linux-gnu-g++ -c -O2 -pipe -march=x86-64 -mtune=generic -g -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -Wno-unused-parameter -fPIC -Wall -Wextra -mno-direct-extern-access -D_REENTRANT -DCAPNP_LITE -DQT_NO_DEBUG -I/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj -I/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/plugins/streamers/lstream/runtime/kj -I/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/capnp -I/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj -I/usr/lib64/qt6/mkspecs/linux-g++ -o thread.o /var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc
/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc: In destructor ‘kj::Thread::~Thread()’:
/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc:82:39: error: dereferencing type-punned pointer will break strict-aliasing rules
[-Werror=strict-aliasing]
   82 |     int pthreadResult = pthread_join(*reinterpret_cast<pthread_t*>(&threadId), nullptr);
      |                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc: In member function ‘void kj::Thread::sendSignal(int)’:
/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc:96:37: error: dereferencing type-punned pointer will break strict-aliasing rules
[-Werror=strict-aliasing]
   96 |   int pthreadResult = pthread_kill(*reinterpret_cast<pthread_t*>(&threadId), signo);
      |                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc: In member function ‘void kj::Thread::detach()’:
/var/tmp/portage/sci-electronics/klayout-0.30.7/work/ruby33/klayout-0.30.7/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc:103:39: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
  103 |   int pthreadResult = pthread_detach(*reinterpret_cast<pthread_t*>(&threadId));
      |                                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: some warnings being treated as errors
make[5]: *** [Makefile:1227: thread.o] Error 1
make[5]: *** Waiting for unfinished jobs....

The violation is at

int pthreadResult = pthread_create(reinterpret_cast<pthread_t*>(&threadId),

pthread_t is an opaque type, it shouldn't be assumed to be unsigned long long:

unsigned long long threadId; // actually pthread_t

I assume this is done for compatibility with Windows, otherwise pthread_t would've been used. But pthread_t is an opaque type: https://stackoverflow.com/questions/33285562/why-and-in-what-sense-is-pthread-t-an-opaque-type

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions