From: Florian Weimer via Libc-alpha <libc-alpha@sourceware.org>
To: Rich Felker <dalias@libc.org>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH 2/2] manual: Document __libc_single_threaded
Date: Fri, 22 May 2020 19:02:01 +0200 [thread overview]
Message-ID: <871rnb3nue.fsf@oldenburg2.str.redhat.com> (raw)
In-Reply-To: <20200522161413.GU1079@brightrain.aerifal.cx> (Rich Felker's message of "Fri, 22 May 2020 12:14:14 -0400")
* Rich Felker:
>> This still has consequences for setxid safety which is why musl now
>> fully synchronizes the existing threads list. But if you're not using
>> the thread count for that, it's not an issue. Indeed I think
>> SYS_membarrier is a solution here, but if it's not supported or
>> blocked by seccomp then __libc_single_threaded must not be made true
>> again at this time.
>
> Uhg, SYS_membarrier is *not* a solution here. The problem is far
> worse, because the user of __libc_single_threaded potentially lacks
> *compiler barriers* too.
>
> Consider something like:
>
> if (!__libc_single_threaded) { lock(); need_unlock=1; }
> x = *p;
> if (need_unlock) unlock();
> /* ... */
> if (!__libc_single_threaded) { lock(); need_unlock=1; }
> x = *p;
> if (need_unlock) unlock();
>
> Here, in the case where __libc_single_threaded is true the second time
> around, there is no (memory or compiler) acquire barrier between the
> first access to *p and the second. Thus the compiler can (and actually
> does! I don't have a minimal PoC but musl actually just hit a bug very
> close to this) omit the second load from memory, and uses the cached
> value, which may be incorrect because the exiting thread modified it.
>
> This could potentially be avoided with complex contracts about
> barriers needed to use __libc_single_threaded, but it seems highly
> error-prone.
Well, yes. It's clearly a data race if the implementation sets
__libc_single_threaded directly from an exiting thread. I don't see a
way around that.
Our discussion focused on the problem that observing a thread count of 1
in pthread_join does not necessarily mean that it is safe to assume at
this point that the process is single-threaded, in glibc's
implementation that uses a simple __nptl_nthreads counter decremented on
the thread itself. This does not cause a low-level data race directly,
but is potentially still incorrect (I'm not quite sure yet).
In glibc, we annotate many functions with __attribute__ ((leaf)),
implicitly via __THROW. None of these functions may reset
__libc_single_threaded. I expect that many compilers have a built-in
list of standard functions they treat as leaf functions. This means
that these functions cannot write in practice to __libc_single_threaded
(or any other global variable apart from errno). Not following this
rule would result in undefined behavior, similar to an actual data race
in the memory model.
A compiler cannot treat pthread_create as a leaf function, so the simple
implementation of __libc_single_threaded I posted should be fine in this
regard.
Thanjs,
Florian
next prev parent reply other threads:[~2020-05-22 17:02 UTC|newest]
Thread overview: 45+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-20 18:12 [PATCH 0/2] Add __libc_single_threaded Florian Weimer via Libc-alpha
2020-05-20 18:12 ` [PATCH 1/2] Add the __libc_single_threaded variable Florian Weimer via Libc-alpha
2020-05-21 13:07 ` Szabolcs Nagy
2020-05-21 13:16 ` Florian Weimer via Libc-alpha
2020-05-21 13:26 ` Szabolcs Nagy
2020-05-20 18:12 ` [PATCH 2/2] manual: Document __libc_single_threaded Florian Weimer via Libc-alpha
2020-05-21 7:52 ` Michael Kerrisk (man-pages) via Libc-alpha
2020-05-21 12:17 ` Florian Weimer via Libc-alpha
2020-05-21 11:18 ` Szabolcs Nagy
2020-05-21 12:16 ` Florian Weimer via Libc-alpha
2020-05-21 12:50 ` Adhemerval Zanella via Libc-alpha
2020-05-21 13:09 ` Szabolcs Nagy
2020-05-21 13:15 ` Adhemerval Zanella via Libc-alpha
2020-05-21 13:30 ` Szabolcs Nagy
2020-05-21 13:44 ` Florian Weimer via Libc-alpha
2020-05-21 13:58 ` Adhemerval Zanella via Libc-alpha
2020-05-21 14:03 ` Florian Weimer via Libc-alpha
2020-05-22 10:01 ` Szabolcs Nagy
2020-05-22 10:05 ` Florian Weimer via Libc-alpha
2020-05-22 10:54 ` Szabolcs Nagy
2020-05-22 11:08 ` Florian Weimer via Libc-alpha
2020-05-22 15:07 ` Rich Felker
2020-05-22 16:14 ` Rich Felker
2020-05-22 16:36 ` Adhemerval Zanella via Libc-alpha
2020-05-22 17:02 ` Florian Weimer via Libc-alpha [this message]
2020-05-22 17:18 ` Florian Weimer via Libc-alpha
2020-05-22 17:28 ` Rich Felker
2020-05-22 17:40 ` Florian Weimer via Libc-alpha
2020-05-22 17:49 ` Rich Felker
2020-05-22 19:22 ` Szabolcs Nagy
2020-05-22 19:53 ` Rich Felker
2020-05-23 6:49 ` Szabolcs Nagy
2020-05-23 16:02 ` Rich Felker
2020-05-25 8:08 ` Florian Weimer via Libc-alpha
2020-05-25 8:08 ` Florian Weimer via Libc-alpha
2020-05-25 17:21 ` Rich Felker
2020-05-27 11:54 ` Florian Weimer via Libc-alpha
2020-05-27 15:36 ` Rich Felker
2020-06-03 15:00 ` Florian Weimer via Libc-alpha
2020-06-03 17:11 ` Rich Felker
2020-05-21 13:56 ` Adhemerval Zanella via Libc-alpha
2020-05-21 13:14 ` Florian Weimer via Libc-alpha
2020-05-21 14:32 ` Adhemerval Zanella via Libc-alpha
2020-06-03 15:48 ` Florian Weimer via Libc-alpha
2020-06-03 17:52 ` Adhemerval Zanella via Libc-alpha
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/libc/involved.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=871rnb3nue.fsf@oldenburg2.str.redhat.com \
--to=libc-alpha@sourceware.org \
--cc=dalias@libc.org \
--cc=fweimer@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).