Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 10 Feb 2020 15:14:48 -0500
From: Rich Felker <>
Subject: Re: 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."

> 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:

Unfortunately the Linux man pages have not corrected this.


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.