From patchwork Thu Aug 14 20:56:05 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yaakov Selkowitz X-Patchwork-Id: 35418 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f198.google.com (mail-qc0-f198.google.com [209.85.216.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 2CA9F202E4 for ; Thu, 14 Aug 2014 20:56:24 +0000 (UTC) Received: by mail-qc0-f198.google.com with SMTP id r5sf4528050qcx.9 for ; Thu, 14 Aug 2014 13:56:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:mailing-list:precedence:list-id :list-unsubscribe:list-archive:list-post:list-help:sender :delivered-to:message-id:date:from:user-agent:mime-version:to :subject:x-original-sender:x-original-authentication-results :content-type; bh=Lnx36G8fxVXCZIO9vpz35BvQh0qs4ODDM8NebqSwS9Q=; b=HMZA6xNfU67YX12ed3dZtQpsnwHJKCavE1ZvDGLMzwZdvI9g4Rzp4372smzKxSgcMw FjsA5w6gq9T7I+SpGMcWaXcUyOBDnXJ8FLT1X2sbkpIfjc+BW0+fsDS/Nk0r/F7YoogB TH9tT5HT1EGc5dclyXgJnR8gFAoQS+hShXFRRgoypHptaZGgtQt69USvITGR6RcymiZJ xGKsPUkOVFh/eJ8AgKLUyc49YVvTAotWexxRCTg6aAIwTwfxqwZ4nqAi9j8RtFZVotn9 /Om0nNjQuV4au25pc/KaedNL/R/t6T3N/qCmhXmVroKvHCQW0uSGlrP4XWcGhnQvbd5B wr3w== X-Gm-Message-State: ALoCoQmdINrF5U16CNuK2226Y79NFvsKO1kf38Uqvl2KyVtFfHt3pTveF8cLnIJpylUxHHvLxcez X-Received: by 10.42.133.200 with SMTP id i8mr7121036ict.14.1408049783942; Thu, 14 Aug 2014 13:56:23 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.105.202 with SMTP id c68ls1251149qgf.99.gmail; Thu, 14 Aug 2014 13:56:23 -0700 (PDT) X-Received: by 10.224.137.65 with SMTP id v1mr20828670qat.53.1408049783853; Thu, 14 Aug 2014 13:56:23 -0700 (PDT) Received: from mail-vc0-x229.google.com (mail-vc0-x229.google.com [2607:f8b0:400c:c03::229]) by mx.google.com with ESMTPS id k90si8991766qgf.75.2014.08.14.13.56.23 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 14 Aug 2014 13:56:23 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2607:f8b0:400c:c03::229 as permitted sender) client-ip=2607:f8b0:400c:c03::229; Received: by mail-vc0-f169.google.com with SMTP id le20so2106373vcb.14 for ; Thu, 14 Aug 2014 13:56:23 -0700 (PDT) X-Received: by 10.52.120.51 with SMTP id kz19mr2956964vdb.95.1408049783496; Thu, 14 Aug 2014 13:56:23 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp64093vcb; Thu, 14 Aug 2014 13:56:22 -0700 (PDT) X-Received: by 10.66.163.38 with SMTP id yf6mr6804340pab.46.1408049782329; Thu, 14 Aug 2014 13:56:22 -0700 (PDT) Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id gg2si5291853pbb.253.2014.08.14.13.56.21 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Aug 2014 13:56:22 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-return-375318-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Received: (qmail 25793 invoked by alias); 14 Aug 2014 20:56:10 -0000 Mailing-List: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 25776 invoked by uid 89); 14 Aug 2014 20:56:09 -0000 X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 14 Aug 2014 20:56:08 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s7EKu5T1007052 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Aug 2014 16:56:06 -0400 Received: from [10.10.116.31] ([10.10.116.31]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s7EKu4gL006956 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Thu, 14 Aug 2014 16:56:05 -0400 Message-ID: <53ED2265.90201@redhat.com> Date: Thu, 14 Aug 2014 15:56:05 -0500 From: Yaakov Selkowitz User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: Gcc Patch List , libstdc++@gcc.gnu.org Subject: [PATCH] _cxa_thread_atexit fixes for Cygwin/MinGW-w64 X-Original-Sender: yselkowi@redhat.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2607:f8b0:400c:c03::229 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=pass header.i=@gcc.gnu.org X-Google-Group-Id: 836684582541 The attached patch implements premature DLL unloading prevention in __cxa_thread_atexit for Cygwin and MinGW-w64 targets. The mingw.org target is welcome to do the same in their os_defines.h, but this code does require Windows XP/2003, and they have historically catered to older platforms. MinGW cannot practically implement __cxa_thread_atexit_impl because it has no control over the contents of its libc (msvcrt), and implementing this in the (static) mingw32 support library would be of little benefit because it would still end up statically linked into libstdc++. Also, the GPL-with-exceptions libstdc++ implementation cannot be copied as a basis of our own __cxa_thread_atexit_impl due to licensing conflicts with both Cygwin (copyright assignment) and MinGW (public domain). The testsuite shows no regressions with this patch. Index: libstdc++-v3/config/os/mingw32-w64/os_defines.h =================================================================== --- libstdc++-v3/config/os/mingw32-w64/os_defines.h (revision 213759) +++ libstdc++-v3/config/os/mingw32-w64/os_defines.h (working copy) @@ -78,4 +78,9 @@ #define _GLIBCXX_LLP64 1 #endif +// Enable use of GetModuleHandleEx (requires Windows XP/2003) in +// __cxa_thread_atexit to prevent modules from being unloaded before +// their dtors are called +#define _GLIBCXX_THREAD_ATEXIT_WIN32 1 + #endif Index: libstdc++-v3/config/os/newlib/os_defines.h =================================================================== --- libstdc++-v3/config/os/newlib/os_defines.h (revision 213759) +++ libstdc++-v3/config/os/newlib/os_defines.h (working copy) @@ -47,6 +47,12 @@ // See libstdc++/20806. #define _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM 1 + +// Enable use of GetModuleHandleEx (requires Windows XP/2003) in +// __cxa_thread_atexit to prevent modules from being unloaded before +// their dtors are called +#define _GLIBCXX_THREAD_ATEXIT_WIN32 1 + #endif #endif Index: libstdc++-v3/libsupc++/atexit_thread.cc =================================================================== --- libstdc++-v3/libsupc++/atexit_thread.cc (revision 213759) +++ libstdc++-v3/libsupc++/atexit_thread.cc (working copy) @@ -25,6 +25,10 @@ #include #include #include "bits/gthr.h" +#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#endif #if _GLIBCXX_HAVE___CXA_THREAD_ATEXIT_IMPL @@ -47,6 +51,9 @@ void (*destructor)(void *); void *object; elt *next; +#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 + HMODULE dll; +#endif }; // Keep a per-thread list of cleanups in gthread_key storage. @@ -62,6 +69,11 @@ { elt *old_e = e; e->destructor (e->object); +#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 + /* Decrement DLL count */ + if (e->dll) + FreeLibrary (e->dll); +#endif e = e->next; delete (old_e); } @@ -133,6 +145,14 @@ new_elt->destructor = dtor; new_elt->object = obj; new_elt->next = first; +#ifdef _GLIBCXX_THREAD_ATEXIT_WIN32 + /* Store the DLL address for a later call to FreeLibrary in new_elt and + increment DLL load count. This blocks the unloading of the DLL + before the thread-local dtors have been called. This does NOT help + if FreeLibrary/dlclose is called in excess. */ + GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, + (LPCWSTR) dtor, &new_elt->dll); +#endif if (__gthread_active_p ()) __gthread_setspecific (key, new_elt);