DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 5AIDBKGq600573 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 5AIDBKGq600573 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=JMGxgwMp X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 48B74385734D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1763471478; bh=ueJKXEtGFmdKQmSOVkiz1H8DkG/MRT4JHjbHylWBhgc=; h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=JMGxgwMpcE21xEYY8mT4AioLXJTnsOdC+p+yno8fi1TIZK5HjeKTs3Nj63R0DO3JF 9VCfqjJDxa6NWxyYrtN8EO0i7L3QcG5JUOJxIO4vFKXqyF34VzPjehLO1/MM/KAfCj aQxlvQ4ubTB0azulEaWgc9DmLSsk59zcD5Tk4N5w= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA64C3857718 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BA64C3857718 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1763471451; cv=none; b=sNUW3vRFvEYFkd8bdsqpndFZjTKb4P/D0LXYxWRXM18gBjUk9zyDrQPkma0piFS0LRQXPE6AjHF6G5siyYHztZBYx8tqMNkJtrLcPE0UmbbUFamwKin2TMkhq/Wylsu5NLjwXlBWHpbgI5f5zA0AsQ5GQnIP3ZPL5ffxdHfeS2A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1763471451; c=relaxed/simple; bh=RhFLY7mhxvRIRafeT+DgxMF2f0gi/HF17ILeFIZQc2I=; h=Date:From:To:Subject:Message-Id:Mime-Version:DKIM-Signature; b=ZLMyZKR09lTxL/Iov2M4oomPImjcBFofffRw9nnlPOiC+w0UnwCbNm3B3xE/cNvxPkZh8bpL9bLZ2z4JRTw0EZDICJr96JZF9QRJonaWv37tjgt0IABbu9voTjeXqFyW2PlM6E6Fb38SkyeOcnKBp/ppLI1Z2B1dfBKAnTEoLko= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA64C3857718 Date: Tue, 18 Nov 2025 22:10:47 +0900 To: cygwin AT cygwin DOT com Subject: Re: [gcc] Bug in emutls? Message-Id: <20251118221047.9635f02cf9c77fe08993b6bd@nifty.ne.jp> In-Reply-To: <10ce5538-1f7d-e1d8-71e2-64d63e5634b0@jdrake.com> References: <20251108210156 DOT 20eadc80a8161eece6810175 AT nifty DOT ne DOT jp> <10ce5538-1f7d-e1d8-71e2-64d63e5634b0 AT jdrake DOT com> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Mime-Version: 1.0 X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Takashi Yano via Cygwin Reply-To: Takashi Yano Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" Hi Jeremy, On Mon, 17 Nov 2025 11:16:41 -0800 (PST) Jeremy Drake wrote: > On Sat, 8 Nov 2025, Takashi Yano via Cygwin wrote: > > > I looked into the problem, and found that the executable for > > the following code registers two pthread_keys with each destructor; > > one is void emutls_destroy(void *ptr) in libgcc/emutls.c, and the > > other is void run(void *p) in libstdc++-v3/libsupc++/atexit_thread.cc. > > emutls_destroy() free's the memory erea of static thread_local X, > > that is accessed from X::~X() which is called from run(). As a result, > > if the emutls_destroy() is called before run(), run() referres to > > the memory erea already free'ed. > > > > I think this is a bug of gcc. This issue does not occur in Linux, > > because Linux does not use emutls. > > > There is a similar longstanding issue in mingw-w64. The problem there is > that the pthread_key destructors run before the native Windows TLS > callbacks. emutls still uses pthread_key to manage static thread_locals, > but C++ destructors are called from the Windows TLS callbacks (by way of > __cxa_thread_atexit if memory serves). Thanks for the information. When I compile my reproducer with mingw compiler, the issue does not seem to happen. How does mingw handle this issue? > Cygwin should have it comparatively easy: it controls all the pieces (it > doesn't need to care about when Windows TLS callbacks happen because if > somebody calls ExitThread they get the undefined behavior they deserve). > Couldn't Cygwin simply provide its own __cxa_thread_atexit and ensure > destructors registered there run before pthread_key destructors? It is not difficult to add a workaround for this issue in cygwin side. However, IIRC, BSD libc does the same with cygwin 3.7.0-dev. I don't think it is good idea to add workaround to cygwin for a bug of apps on cygwin. > Regardless, is it really undefined in what order pthread_key destructors > run? I would expect they'd run in reverse order of registration (most > recently registered first). Wouldn't that prevent this issue too > (without mucking about with the Itanium C++ ABI)? https://pubs.opengroup.org/onlinepubs/9799919799/ says: "The order of destructor calls is unspecified if more than one destructor exists for a thread when it exits." As you expected, the reverse-order'ed destructor-call hides the issue. (That is what 3.6.5 does.) -- Takashi Yano -- 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