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 c5421172 for ; Mon, 10 Feb 2020 20:15:02 +0000 (UTC) Received: (qmail 18020 invoked by uid 550); 10 Feb 2020 20:15:01 -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 17996 invoked from network); 10 Feb 2020 20:15:00 -0000 Date: Mon, 10 Feb 2020 15:14:48 -0500 From: Rich Felker To: musl@lists.openwall.com Message-ID: <20200210201448.GS1663@brightrain.aerifal.cx> References: <20200210193427.GR1663@brightrain.aerifal.cx> <14a8dbf3-1382-e61c-3b4e-3a3174dd75b2@bell-sw.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <14a8dbf3-1382-e61c-3b4e-3a3174dd75b2@bell-sw.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: Rich Felker Subject: Re: [musl] No such process return value in pthread_getcpuclockid On Mon, Feb 10, 2020 at 10:57:22PM +0300, Alexander Scherbatiy wrote: > On 10.02.2020 22:34, Rich Felker wrote: > > >On Mon, Feb 10, 2020 at 10:29:02PM +0300, Alexander Scherbatiy wrote: > >>Hello, > >> > >>It seems that pthread_getcpuclockid does not properly handle non > >>valid thread id argument. > >> > >>Below is a sample which calls pthread_getcpuclockid with NULL thread > >>id. The expected result is ESRCH (No thread with the ID thread could > >>be found). It crashes in my docker with Alpine Linux 3.11.3 (musl > >>libc x86_64 1.1.24). It returns ESRCH on my Ubuntu system. > >There's no such thing as a "null thread id". A pthread_t value is > >either the id of a thread which is still valid (still running or > >joinable and not yet joined), or *any* use of it produces undefined > >behavior. There is no value reserved for a sentinel. If you need an > >optional thread id variable/field, you need a separate validity flag > >alongside it. > > > >None of this is unique to musl; it's the way the POSIX threads > >interfaces are designed. > > I can create a thread, join to it and use the thread id in > pthread_getcpuclockid function after that. That has undefined behavior, and the fact that it does is *inherent* - reusable resource identifiers can be reused as soon as they're freed (and nonreusable ones only admit a finite number of resource ever to exist). Expecting use of pthread_t after its lifetime to give a meaningful error is analogous to expecting free() or close() to give you a meaningful error. If you need a citation, it's XSH 2.9.2 Thread IDs: "Although implementations may have thread IDs that are unique in a system, applications should only assume that thread IDs are usable and unique within a single process. The effect of calling any of the functions defined in this volume of POSIX.1-2017 and passing as an argument the thread ID of a thread from another process is unspecified. The lifetime of a thread ID ends after the thread terminates if it was created with the detachstate attribute set to PTHREAD_CREATE_DETACHED or if pthread_detach() or pthread_join() has been called for that thread. A conforming implementation is free to reuse a thread ID after its lifetime has ended. If an ^^^^^ application attempts to use a thread ID whose lifetime has ended, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the behavior is undefined." ^^^^^^^^^^^^^^^^^^^^^^^^^^ https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_09_02 > The Linux Programmer's Manual has the following errors section: > "ESRCH  No thread with the ID thread could be found." > > Does pthread_getcpuclockid function from musl follows the similar > errors handling approach? The specification of ESRCH for pthread interfaces was a bug, because a "shall fail" or even "may fail" condition makes no sense with a behavior that's explicitly undefined (in which case the implementation is allowed to do anything at all). This was clarified in POSIX 2008 as a result of Austin Group interpretation 142: https://collaboration.opengroup.org/austin/interps/documents/14366/AI-142.txt Unfortunately the Linux man pages have not corrected this. Rich