Strict aliasing issue in threads

Open
#2,340 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
cpp

Research direction

Start with src/plugins/streamers/lstream/runtime/kj/kj/thread.cc around the reported calls and thread.h around the threadId declaration. Check how the thread identifier is represented and used across platforms, then build with -Werror=strict-aliasing to reproduce the reported diagnostics. Done means the strict-aliasing errors are resolved without assuming a particular representation for pthread_t.

Written by the indexing model from the issue text.

Description

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 https://github.com/KLayout/klayout/blob/5ccf6260f1769e7431901101bf27d01b68df177e/src/plugins/streamers/lstream/runtime/kj/kj/thread.cc#L70

pthread_t is an opaque type, it shouldn't be assumed to be unsigned long long:
https://github.com/KLayout/klayout/blob/5ccf6260f1769e7431901101bf27d01b68df177e/src/plugins/streamers/lstream/runtime/kj/kj/thread.h#L69

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

Dominant language
C++
Stars
1.2k
Forks
305
Avg merge
3d 2h
Merged PRs (30d)
11

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from KLayout/klayout

All issues in KLayout/klayout

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.