Ubuntu-specific pthread issue

Bug #1876958 reported by David Rogers
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
gcc-defaults (Ubuntu)
Invalid
Undecided
Unassigned

Bug Description

The following c++11 program works properly (prints fn1 and fn2) on every system and compiler I have checked except for Ubuntu - where it prints (fn2 and fn2). This happens on both laptop and server installs of Ubuntu 18.04.

All system gcc packages on my Ubuntu 18.04 system show the same (incorrect) output:
g++-5 (Ubuntu 5.5.0-12ubuntu1) 5.5.0 20171010
g++-6 (Ubuntu 6.5.0-2ubuntu1~18.04) 6.5.0 20181026
g++-7 (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
g++-8 (Ubuntu 8.3.0-26ubuntu1~18.04) 8.3.0

Notes:
 - Re-compiling gcc from source eliminates the problem.
 - I have not checked any Ubuntu versions other than 18.04.

Off-topic note:
 - this may be a [libpthread bug](https://bugs.launchpad.net/ubuntu/+source/libpthread-stubs/)?

/* Minimal bug-reproducer for gcc on ubuntu */
#include <functional>
#include <thread>
#include <stdio.h>

struct run {
    const std::function<void()> &fn;
    run(const std::function<void()> &fn_) : fn(fn_) {}

    void operator()() {
        fn();
    }
};

int main() {
    auto fn1 = [](){ printf("fn1\n"); };
    auto r1 = run(fn1);
    auto fn2 = [](){ printf("fn2\n"); };
    auto r2 = run(fn2);
    std::thread t1( r1 );
    std::thread t2( r2 );
    t1.join();
    t2.join();
}

Revision history for this message
David Rogers (wantye) wrote :

Update: this does not involve threads (see new minimal version below). Also, adding -O2 to the above version (with threading) gives no output, but creates a SEGV with the version below. Finally, the bug occurs at compile-time, not link time, since the object file (".o") created from this also errors when run on CentOS.

#include <functional>
#include <stdio.h>

struct run {
    const std::function<void()> &fn;
    run(const std::function<void()> &fn_) : fn(fn_) {}

    void operator()() {
        fn();
    }
};

int main() {
    auto fn1 = [](){ printf("fn1\n"); };
    auto r1 = run(fn1);
    auto fn2 = [](){ printf("fn2\n"); };
    auto r2 = run(fn2);
    r1();
    r2();
}

Revision history for this message
Matthias Klose (doko) wrote :

The Ubuntu GCC turns on -fstack-protector by default. You get the same results when turning this off.

However building and running your program with -fsanitize=address suggests that your program is wrong.

Changed in gcc-defaults (Ubuntu):
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.