From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id D75DD3856962 for ; Mon, 13 Nov 2023 14:17:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D75DD3856962 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D75DD3856962 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699885029; cv=none; b=cTwnSyMOKmCXsQQLKZ20A0+wrZKn7RapklW36iHfAC+4LOuIaD7kld9vGr0+sxDcAvwbtAaWabGo+s1cQE9dNe5nonwZd1hhC5IZiEfkKfMhNJa6FKSaYLnHOvvg9bi1+F2ylyoga79sW3aHxraSDH4myZ/ehbyYt/kNtkkEBV8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699885029; c=relaxed/simple; bh=GqPUCVKgvJPE/X6PkeABfmJftoLlf7aMR8e3Tct90S4=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=kJhjnfYo91wKURc+AJxbmjTG0HpK1JLYT2nzAACiF27rG72DwWRIucIgIkyzRrQaGS/ZfTIYp/vnYK6i/0WdWjPD84SRK+Qul13cxTzzY821XLrK9tUmxSyoSd7eYf1BySQFZJ5NdHH4k2KELi6layBzIFVQnFF3KYP1TXQM6u8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699885027; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Tk0R8/AN3ectXdp48rS0GsxnTU8h3HdNe7ea0MYZemY=; b=HiAGz8PyJ9tBVHJ79+LINX9AnPFAhHArIN4X2pUtOVMi9dyXsQoOulLxv1mNcKDXx1GI/q pKs3IVGNximxSILfUMauYRY0NGbu2x6K7G2AdG71fReM/UvKwRfsiSkgeUY0ufgnolV4ys oNLnCrpJegbyLlLSQz5IiuxuMiFsGfY= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-621-u7orjdSMOJ-_QCw_NSq2Cw-1; Mon, 13 Nov 2023 09:17:04 -0500 X-MC-Unique: u7orjdSMOJ-_QCw_NSq2Cw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4A762101AA42; Mon, 13 Nov 2023 14:17:04 +0000 (UTC) Received: from calimero.vinschen.de (unknown [10.39.194.184]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 047B1C1596F; Mon, 13 Nov 2023 14:17:03 +0000 (UTC) Received: by calimero.vinschen.de (Postfix, from userid 500) id C6163A80A3D; Mon, 13 Nov 2023 15:17:02 +0100 (CET) Date: Mon, 13 Nov 2023 15:17:02 +0100 From: Corinna Vinschen To: Bruno Haible , newlib@sourceware.org Cc: cygwin@cygwin.com Subject: Re: rand is not ISO C compliant in Cygwin Message-ID: Reply-To: newlib@sourceware.org Mail-Followup-To: Bruno Haible , newlib@sourceware.org, cygwin@cygwin.com References: <9938355.c9vzh5UkMf@nimes> MIME-Version: 1.0 In-Reply-To: <9938355.c9vzh5UkMf@nimes> X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: [redirecting to the newlib mailing list] This is long-standing code in newlib, not actually inside Cygwin. On Nov 10 21:19, Bruno Haible via Cygwin wrote: > ISO C 23 ยง 7.24.2.1 and 7.24.2.2 document how rand() and srand() are > expected to behave. In particular: > "The srand function uses the argument as a seed for a new sequence > of pseudo-random numbers to be returned by subsequent calls to rand. > If srand is then called with the same seed value, the sequence of > pseudo-random numbers shall be repeated. ... > The srand function is not required to avoid data races with other > calls to pseudo-random sequence generation functions. ..." > > The two attached programs call srand() in one thread and then rand() > in another thread. There is no data race, since the second thread > is only created after the call to srand() has returned. The behaviour > in Cygwin is that the values in the second thread ignore the srand() > call done in the first thread. Struct _reent is a kind of per-execution unit datastructure. This could be independent code blocks in bare-metal code, or threads in Cygwin et al. So the _reent struct also holds the seed state of the rand and rand48 generators for a good reason. But that's only half the picture, because newlib actually has two ways of storing _reent data: Either in a pre-thread struct _reent, or (if _REENT_THREAD_LOCAL is defined) as per-member thread_local storage. Theoretically, this could be easily fixed: - Either we maintain a global struct _reent which could be used from rand(). - Or, in case of _REENT_THREAD_LOCAL, by making the rand48 data globally available as static data, rather than as thread_local data. The rand() function would still not use locking but AFAICS that's not actually required by POSIX or ISO C. However, this is something I don't want to decide single-handedly, so I'm asking how to go forward on the newlib ML. As far as Cygwin alone is concerned, a patch like this one would suffice: diff --git a/newlib/libc/stdlib/rand.c b/newlib/libc/stdlib/rand.c index ba9cc80f2b21..2b48e7a725b1 100644 --- a/newlib/libc/stdlib/rand.c +++ b/newlib/libc/stdlib/rand.c @@ -58,6 +58,12 @@ on two different systems. #include #include +#ifdef __CYGWIN__ +#define _RAND_REENT _GLOBAL_REENT +#else +#define _RAND_REENT _REENT +#endif + #ifdef _REENT_THREAD_LOCAL _Thread_local unsigned long long _tls_rand_next = 1; #endif @@ -65,7 +71,7 @@ _Thread_local unsigned long long _tls_rand_next = 1; void srand (unsigned int seed) { - struct _reent *reent = _REENT; + struct _reent *reent = _RAND_REENT; _REENT_CHECK_RAND48(reent); _REENT_RAND_NEXT(reent) = seed; @@ -74,7 +80,7 @@ srand (unsigned int seed) int rand (void) { - struct _reent *reent = _REENT; + struct _reent *reent = _RAND_REENT; /* This multiplier was obtained from Knuth, D.E., "The Art of Computer Programming," Vol 2, Seminumerical Algorithms, Third > > How to reproduce the bug: > > $ x86_64-pc-cygwin-gcc -Wall rand-in-posix-thread.c > $ ./a > > or > > $ x86_64-pc-cygwin-gcc -Wall rand-in-isoc-thread.c > $ ./a > > Expected output: > > Value from main thread: 1583559764 > Value from separate thread: 1583559764 > > Actual output: > > Value from main thread: 1583559764 > Value from separate thread: 1481765933 > > #include > #include > #include > #include > > static void * > rand_invocator_thread (void *arg) > { > printf ("Value from separate thread: %d\n", rand ()); > return NULL; > } > > int > main () > { > unsigned int seed = 19891109; > > srand (seed); > printf ("Value from main thread: %d\n", rand ()); > srand (seed); > pthread_t t; > assert (pthread_create (&t, NULL, rand_invocator_thread, NULL) == 0); > assert (pthread_join (t, NULL) == 0); > > return 0; > } > #include > #include > #include > #include > > static int > rand_invocator_thread (void *arg) > { > printf ("Value from separate thread: %d\n", rand ()); > return 0; > } > > int > main () > { > unsigned int seed = 19891109; > > srand (seed); > printf ("Value from main thread: %d\n", rand ()); > srand (seed); > thrd_t t; > assert (thrd_create (&t, rand_invocator_thread, NULL) == thrd_success); > assert (thrd_join (t, NULL) == thrd_success); > > return 0; > } > > -- > 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