C++ app crashes immediately somewhere in clone.S when creating an std::thread
Asked Answered
C

0

8

Why is /sysdeps/unix/sysv/linux/x86_64/clone.S crashing with SIGSTOP at (disassembled) "test %rax,%rax" on Ubuntu 20.10 when running this auto generated minimal (no-qt) C++ application in Qt Creator 4.14.2 in DEBUG mode:

#include <iostream>
#include <thread>

void foo()
{
}

int main()
{
    std::thread t(foo); // Crash happens after stepping over this line

    t.join();

    std::cout << "All good!" << std::endl;

    return 0;
}

Callstack

1 clone clone.S         78  0x7f28abaef215 
2 ??    allocatestack.c 313 0x7f28abbc94b0 
3 ??                        0x7f28ab881640 
4 ??                                       

Disassembler for allocatestack.c stopped at:

pthread_create.c:
0x7f28abbc94b0                  f3 0f 1e fa                    endbr64

Disassembler for clone.S stopped at "test %rax,%rax":

        50 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891e0                  f3 0f 1e fa           endbr64
        52 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891e4  <+    4>        48 c7 c0 ea ff ff ff  mov    $0xffffffffffffffea,%rax
        53 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891eb  <+   11>        48 85 ff              test   %rdi,%rdi
        54 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891ee  <+   14>        74 3d                 je     0x7f2f1638922d <clone+77>
        55 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f0  <+   16>        48 85 f6              test   %rsi,%rsi
        56 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f3  <+   19>        74 38                 je     0x7f2f1638922d <clone+77>
        59 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f5  <+   21>        48 83 ee 10           sub    $0x10,%rsi
        60 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891f9  <+   25>        48 89 4e 08           mov    %rcx,0x8(%rsi)
        64 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f163891fd  <+   29>        48 89 3e              mov    %rdi,(%rsi)
        67 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389200  <+   32>        48 89 d7              mov    %rdx,%rdi
        68 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389203  <+   35>        4c 89 c2              mov    %r8,%rdx
        69 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389206  <+   38>        4d 89 c8              mov    %r9,%r8
        70 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389209  <+   41>        4c 8b 54 24 08        mov    0x8(%rsp),%r10
        71 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f1638920e  <+   46>        b8 38 00 00 00        mov    $0x38,%eax
        76 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389213  <+   51>        0f 05                 syscall
        78 [1]  in ../sysdeps/unix/sysv/linux/x86_64/clone.S
0x7f2f16389215  <+   53>        48 85 c0              test   %rax,%rax

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project(testThread LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# These 3 lines were added by me to the auto generated CMakeLists.txt
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE) # Ensures that -pthread is added to the compile flags
find_package(Threads REQUIRED)

add_executable(testThread main.cpp)

target_link_libraries(testThread Threads::Threads)

CMake output

Running /usr/bin/cmake -S /home/myhome/testThread -B /home/myhome/testThread/build-debug '-GCodeBlocks - Ninja' -DCMAKE_BUILD_TYPE:String=Debug -DQT_QMAKE_EXECUTABLE:STRING= -DCMAKE_PREFIX_PATH:STRING= -DCMAKE_C_COMPILER:STRING=/usr/bin/gcc -DCMAKE_CXX_COMPILER:STRING=/usr/bin/g++ in /home/myhome/testThread/build-debug.
-- The CXX compiler identification is GNU 10.2.0
-- Check for working CXX compiler: /usr/bin/g++
-- Check for working CXX compiler: /usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    QT_QMAKE_EXECUTABLE


-- Build files have been written to: /home/myhome/testThread/build-debug
Elapsed time: 00:00.

CMake build output

17:24:26: Running steps for project testThread...
17:24:26: Starting: "/usr/bin/cmake" --build . --target all --verbose
[1/2 3.9/sec] /usr/bin/g++    -g   -pthread -std=gnu++11 -MD -MT CMakeFiles/testThread.dir/main.cpp.o -MF CMakeFiles/testThread.dir/main.cpp.o.d -o CMakeFiles/testThread.dir/main.cpp.o -c ../main.cpp
[2/2 6.9/sec] : && /usr/bin/g++  -g   CMakeFiles/testThread.dir/main.cpp.o  -o testThread  -pthread && :
17:24:26: The process "/usr/bin/cmake" exited normally.
17:24:26: Elapsed time: 00:00.

g++ Version

~$ g++ --version
g++ (Ubuntu 10.2.0-13ubuntu1) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.

So far what I tried was creating this minimal example without using ccache and with an empty build folder but the issue persists. I am out of ideas.

Help is very appreciated!

UPDATE 1

  • Running in RELEASE mode in Qt Creator works fine

  • Running from terminal as suggested in the comments works fine

    g++ -pthread -o testThread main.cpp

    ./testThread

  • Running from terminal with gdb works fine

    $ gdb ./testThread

    GNU gdb (Ubuntu 9.2-0ubuntu2) 9.2

    Copyright (C) 2020 Free Software Foundation, Inc.

    Reading symbols from ./testThread...

    (gdb) r

    Starting program: testThread

    [Thread debugging using libthread_db enabled]

    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

    [New Thread 0x7ffff7a51640 (LWP 5946)]

    [Thread 0x7ffff7a51640 (LWP 5946) exited]

    All good!

    [Inferior 1 (process 5942) exited normally]

    (gdb)

UPDATE 2

Seems the issue is related to Qt Creator. There are two workarounds.

  1. Add this to the "Debugging Helper Customization":

    handle SIGSTOP pass nostop noprint

enter image description here

  1. Or disable the checkbox "Run in terminal" in the project's Run configuration
Chalky answered 9/4, 2021 at 16:6 Comment(4)
Do you still see the crash when you compile your MRE without cmake? g++ -pthread -o testThread main.cppHydrocortisone
I have added new information with respect to your comment (see "UPDATE 1").Chalky
You should convert UPDATE 2 into answer and accept it. Great work.Art
SIGSTOP can’t be generated by a program’s own behavior (like SIGSEGV or SIGILL), so it’s not a “crash” at all.Garnierite

© 2022 - 2024 — McMap. All rights reserved.