From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 23750386197D; Tue, 13 Jul 2021 15:43:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 23750386197D From: "doodspav at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/101439] New: std::atomic<__uint128_t>::load() crashes - possible fix with mutable Date: Tue, 13 Jul 2021 15:43:18 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 4.8.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: doodspav at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Jul 2021 15:43:19 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101439 Bug ID: 101439 Summary: std::atomic<__uint128_t>::load() crashes - possible fix with mutable Product: gcc Version: 4.8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: doodspav at gmail dot com Target Milestone: --- The following code will cause a segfault on x86 from g++4.8 up to g++11.1 (currently the latest non-trunk version on Godbolt): ``` #include struct S { long i, j; }; const std::atomic x{ {0, 0} }; int f(const std::atomic* p) { S y =3D *p; return y.i; } int main() { return f(&x); } ``` (demonstrated here: https://godbolt.org/z/eWcTv5fdY for however long the li= nk remains alive) Loading the value from `x` causes libatomic to use `lock cmpxchg16b` on read-only memory, crashing the program (since `lock cmpxchg16b` always perf= orms a write).=20 This has been reported in bug report 95722 (and touched upon in 70490), how= ever those treat it as a libatomic bug (which it technically is), whereas here I seek to treat it as a libstdc++ bug. A "simple" workaround for C++ that doesn't involve changing anything in libatomic would be to mark all members in any 16 byte specialisation of `std::atomic` as `mutable`, preventing the compiler from placing it in read-only memory. This is what MSVC does in their implementation (as does Boost.Atomic I think). This shouldn't break anything as far as I can tell. This code (using `_Atomic`) also causes a segfault in C. I can't really thi= nk of a similar fix there (maybe just act as if DR459 never happened?).=