From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by inbox.vuxu.org (OpenSMTPD) with SMTP id 404ff23b for ; Mon, 27 Jan 2020 17:52:15 +0000 (UTC) Received: (qmail 19943 invoked by uid 550); 27 Jan 2020 17:52:13 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 19925 invoked from network); 27 Jan 2020 17:52:12 -0000 Date: Mon, 27 Jan 2020 12:51:54 -0500 From: Rich Felker To: Simon Cc: musl@lists.openwall.com Message-ID: <20200127175154.GA30412@brightrain.aerifal.cx> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Sender: Rich Felker Subject: Re: [musl] Bug report: Reproduction of seg fault caused by musl thread creation race condition 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