delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2026/10/04/01:40:07

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 6945e7O82575684
Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com
Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com
DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 6945e7O82575684
Authentication-Results: delorie.com;
dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=IozG9JSH
X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E8F684BA7998
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1791092390;
bh=waz1mirP+B18pHmKi7482+uns+xdbavuljpkcB+z5Q4=;
h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe:
List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
From;
b=IozG9JSHr7IXJImG33MclRaWFdzLrSbEfS1JXC7G+Icydicy3d1q/lwLsWjZxIZSM
nqAT0fT8oCsz6wWnyGh9CC+aL/bM6rc9a0osqiagJj/eYdoX3TBHQdQ5Q/vwZjsezM
bF0G2Els4RWe29uI90oOfW1eWOByE0UNI4wcS51s=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 541E44BA799B
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 541E44BA799B
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791092370; cv=none;
b=oQatAYd2wJnLEZ+bAPjhyFOfhOn4Gp/1XhZgKfgZkLPCfZcXfsZTGLDOp2biRx4Si1vvFyqkxCshy1iiiaKtyxaJb38qajhhBU9QoA7nnwNysh+3mNwJIyeox63PQT/0zykwhNicF/ZjOrPrxJLzUw0NKq+CW2Z1f8pIE8ObAf8=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
t=1791092370; c=relaxed/simple;
bh=HhbsFtQa01VFUMDimNELYUMgY45bEWl6mStFPjsWq1Q=;
h=DKIM-Signature:Message-Id:From:To:Subject:Date:MIME-Version;
b=RmBCVJTw9cp5p9jraGIKZje5c6j3256Czzxtd50R0auo0AsuGcGvKNs9NhN8AwikaYmvqnraNnWMHbjnwR9bf8P1LTRkqnKcIXk3WYOoKaTRDrIUjn7NHTijK5P/5uKnUKR1jUE1q59S9Z1BAm3GEJBrUETJybhUD3VPZ/RyVig=
ARC-Authentication-Results: i=1; sourceware.org;
dkim=pass (2048-bit key, unprotected)
header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104
header.b=gUU2SqQu
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 541E44BA799B
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1791092369; x=1791697169;
h=mime-version:content-transfer-encoding:user-agent:references
:in-reply-to:date:subject:to:from:message-id:content-type:x-gm-gg
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
:content-type;
bh=xSdZFJ6TpFeAAmbN5X69sTBP7Sp4pHpUJvX7P5y8NFI=;
b=PyC1WrOf/LYy4rwxghP6FHItY4WvTehFtYsnMArkdDbOQHssXw4tmRNd5ZRgreKamo
tJImMsDCHydH8Zg8VyAj3taegTGVOclRrAFg+wxXAXuqT9SMH2QXLwxCKpuEP8JaDHYN
67f4sz/8YrwZ4BGWZfKc7+Int1j28aeYyYUlSQU52en4MHiXb6yFV26x+3KVnoh0bC9c
qZviL3TOPQd4FvWqogPUH5200KSKaa2kzNwv5jD79oDu0vi5NkU2gyTaOaIc7OM4QwpT
Vjd8+2Zjv3jeUbBoquj7QQCqrC6TXs8drqLDxT6PlLCZBpxqfQkGDdpxDJnzCay603f8
58/Q==
X-Gm-Message-State: AFuF++lFFbHVEG0yZdd0LJMza1r/thlMDemyAanxn6JAra9EYyhcN3yQ
LAvmh2GXGLpjzrdvv3Eyyg1s7nxblBdjP9moXwy03sPJIryv8P41Ti/AWKS03w==
X-Gm-Gg: AYBFou2ijm/3+xPXzzz2HZHHnzQPs2BX5OPUcvP/rERhpyYomk8mrc0INaSk8suwGmD
ei4rgR6kJ/oiDvjnmX8jJyHktlzcpuHHGV5f+FhO4CcxJgXIOPJailNfA9iou9cAiW81e3cAREX
BXzgIHn5un4i1svr34cyxGTQAlH5dYTxsCE3flQEUVAjOASiv2e1YsIPZa0mDSxzmlvSomsKBwt
3w1jnRxq4OQNbY40hYzaum9FoInJXawTDg4/yJBY7eOXxU7cNfMBXeDIHeKnhkwDW+nnkT3h5sE
xglj680tY7XFzV/dQv3ZB6nJmr6QW1TclMaijb7liz3kZFO+AF2uPpwdkB1VUcqmgzGl2y5XQnl
tp5/iohoSKuYITzYJfhk+p7mZngPptKoR+qdbj53jBGnmXRxfvcSlmxem2x0a17eiMclrqHGuqW
6osjd0IbZGEpfBkG0ACMjAPGtJfCEepleBox4IpdD7DEgLSsmL9Og8gXOEjJjnD6AkoNTmoEkRf
GftYZ+y2fc=
X-Received: by 2002:a05:6a20:3d96:b0:3de:5928:7f33 with SMTP id
adf61e73a8af0-3e0bd00ab45mr6779769637.39.1791092369102;
Sat, 03 Oct 2026 22:39:29 -0700 (PDT)
Message-Id: <1791090664939.2108050499.805519770@gmail.com>
To: cygwin AT cygwin DOT com
Subject: Re: Race during the first use of pthread_rwlock_{rd,wr}lock from
PTHREAD_RWLOCK_INITIALIZER
Date: Sun, 04 Oct 2026 05:39:27 +0000
In-Reply-To: <b9898bd6-5d42-49b3-9d93-362ef4ebe11d@maxrnd.com>
References: <b9898bd6-5d42-49b3-9d93-362ef4ebe11d AT maxrnd DOT com>
X-Mailer: Vivaldi Mail
User-Agent: Vivaldi Mail/8.2.4133.76
MIME-Version: 1.0
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: kikairoya via Cygwin <cygwin AT cygwin DOT com>
Reply-To: kikairoya <kikairoya AT gmail DOT com>
Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>
X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 6945e7O82575684

Hi Mark,

2026年10月4日 13:41:47 (+09:00) で、Mark Geisert via Cygwin さんが書きました:

> >> I believe your STC is relying on undefined behavior of pthread_rwlock_init().  Because you have four threads contending for the same location in memory, the following sentence from the POSIX standard defining that function applies:
> >>> Results are undefined if pthread_rwlock_init() is called specifying
> >>> an already initialized read-write lock.
> >>
> >> I assume (or you've already said ;-) you're comparing Cygwin with Linux.  Linux may well have a defined action for the situation that Cygwin doesn't.  I'm not sure how to proceed just yet.
> > In my reproducer, pthread_rwlock_init is called once before launching threads when INIT_LOCK_EXPLICITLY is defined; otherwise, it is never called. Therefore, the UB doesn't apply here.
> 
> I could have been more accurate in my comment.  When your STC does not #define INIT_LOCK_EXPLICITLY, the initialization of rwl is handled on first use.  So one of the threads is able to successfully initialize rwl.  Then the other threads encounter an initialized rwl during their initialization attempts and this is why the undefined case applies.

I still don't follow. Why does UB of pthread_rwlock_init (a public API) matter, even if it's never called?
A double-initialization in pthread_rwlock::init (an implementation detail) called from pthread_rwlock_{rd,wr}lock is a runtime's bug, not an UB.


> > pthread_rwlock::init should handle the following situations:
> > - the first use (lock) of PTHREAD_RWLOCK_INITIALIZER
> > - call of pthread_rwlock_init on PTHREAD_RWLOCK_INITIALIZER
> > - call of pthread_rwlock_init on a zero-initialized rwlock
> > - call of pthread_rwlock_init on a destroyed rwlock
> > I believe the third and fourth situations can't be handled by simply checking if is_initializer(rwlock), or *rwlock == PTHREAD_RWLOCK_INITIALIZER.
> 
> Friendly question: POSIX doesn't seem to mention the latter two situations, so why are you depending on them?  Making the changes for pthread_rwlock_init implies the same should be done everywhere it makes sense, i.e., pthread_*_init.  If you know of an OS where this works the way you say, can you tell us?  I'm not promising anything; I'm looking for supporting examples :-).

I'm referencing the following two pages:

https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_rwlock_init.html
https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_rwlock_wrlock.html

As per the documents, pthread_rwlock_wrlock requires an initialized rwlock.
> Results are undefined if any of these functions are called with an uninitialised read-write lock.
> Results are undefined if a read-write lock is used without first being initialised.

An "initialized" rwlock is an rwlock after an exact one call of pthread_rwlock_init.
> The pthread_rwlock_init() function initialises the read-write lock referenced by rwlock with the attributes referenced by attr.

pthread_rwlock_init accepts an uninitialized rwlock,
> Results are undefined if pthread_rwlock_init() is called specifying an already initialised read-write lock.

or an rwlock which has been destroyed.
> A destroyed read-write lock object can be re-initialised using pthread_rwlock_init();

PTHREAD_RWLOCK_INITIALIZER is just a shorthand for a call to pthread_rwlock_init.
> the effect is equivalent to dynamic initialisation by a call to pthread_rwlock_init()

To be precise, the second situation of which I listed is undefined due to double-initialization, and the third might not require zero-initialized.


Thanks,

-- 
Tomohiro Kashiwada (@kikairoya)

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2026   by DJ Delorie     Updated Sep 20 2026