mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Simon <simonhf@gmail.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] Bug report: Reproduction of seg fault caused by musl thread creation race condition
Date: Mon, 27 Jan 2020 12:51:54 -0500	[thread overview]
Message-ID: <20200127175154.GA30412@brightrain.aerifal.cx> (raw)
In-Reply-To: <CABkUXbdOP8d=BzFTpYetmEEKyEwWqwaW7NmmB9vdJacu-wXABQ@mail.gmail.com>

On Sun, Jan 26, 2020 at 04:33:57PM -0800, Simon wrote:
> Hello! I recently had some C code which works normally with glibc but seg
> faults sometimes with musl. I managed to reproduce the seg fault via
> musl-gcc and Alpine Linux and document it here [1]. Seems to be some kind
> of race condition, so hopefully you guys also get a seg fault when you
> follow my reproduction steps. Hope this helps and looking forward to any
> feedback or helping further if possible, Simon
> 
> [1] https://gist.github.com/simonhf/6d8097f4d6caa572cc42354f494b20ef

This behavior was originally intentional. In general, if a function is
specified to modify pointed-to memory as output as a side effect of
success, that does not give it license to modify it on failure. And
since pthread_create can't commit to success until after the thread is
created, it would have to hold back start of the new thread
unnecessarily to guarantee that the result is written before the new
thread starts. (Note that it can't simply write the value from both
the caller and the new thread; the latter could end up writing to the
pthread_t object after the end of its lifetime.)

Moreover, there is no expectation from the application that it should
be able to read the result object from the new thread without
additional synchronization. The wording of the spec is:

    "Upon successful completion, pthread_create() shall store the ID
     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    of the created thread in the location referenced by thread."

Until completion of (observation of & synchronization with return
from) pthread_create, nothing can be said about value of the object;
access to it is unsynchronized.

With that said, the specification for pthread_create does *allow*
implementations that store the value speculatively before success:

    "If pthread_create() fails, no new thread is created and the
    contents of the location referenced by thread are undefined."

I was not aware of this when writing it. So we could change it, but it
doesn't seem like a very good idea to do so; any code relying on it is
non-portable/racy. If the new thread needs its own id, there's an easy
and portable way to obtain it: pthread_self().

Are there reasons you still think the alternate behavior would be
preferable?

Rich

  parent reply	other threads:[~2020-01-27 17:52 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-27  0:33 Simon
2020-01-27  5:28 ` Markus Wichmann
2020-01-27 17:51 ` Rich Felker [this message]
2020-01-27 19:59   ` Simon
2020-01-27 20:37     ` Szabolcs Nagy
2020-01-27 20:46       ` Rich Felker
2020-02-03 21:14 ` [musl] Why does musl printf() use so much more stack than other implementations when printf()ing floating point numbers? Simon
2020-02-03 21:57   ` Rich Felker
2020-02-03 23:05     ` Szabolcs Nagy
2020-02-03 23:52       ` Szabolcs Nagy

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200127175154.GA30412@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=simonhf@gmail.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).