From owner-freebsd-threads@FreeBSD.ORG Tue May 12 02:08:54 2015 Return-Path: Delivered-To: freebsd-threads@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 3C5B26C0 for ; Tue, 12 May 2015 02:08:54 +0000 (UTC) Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2001:1900:2254:206a::16:76]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 262291CA9 for ; Tue, 12 May 2015 02:08:54 +0000 (UTC) Received: from bugs.freebsd.org ([127.0.1.118]) by kenobi.freebsd.org (8.14.9/8.14.9) with ESMTP id t4C28smp037520 for ; Tue, 12 May 2015 02:08:54 GMT (envelope-from bugzilla-noreply@freebsd.org) From: bugzilla-noreply@freebsd.org To: freebsd-threads@FreeBSD.org Subject: [Bug 200138] [PATCH] Fixed race condition incurred in libthr by some foreign malloc libraries Date: Tue, 12 May 2015 02:08:54 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: threads X-Bugzilla-Version: 10.1-STABLE X-Bugzilla-Keywords: patch X-Bugzilla-Severity: Affects Only Me X-Bugzilla-Who: yuri@rawbw.com X-Bugzilla-Status: New X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: freebsd-threads@FreeBSD.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status keywords bug_severity priority component assigned_to reporter attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 May 2015 02:08:54 -0000 https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=200138 Bug ID: 200138 Summary: [PATCH] Fixed race condition incurred in libthr by some foreign malloc libraries Product: Base System Version: 10.1-STABLE Hardware: Any OS: Any Status: New Keywords: patch Severity: Affects Only Me Priority: --- Component: threads Assignee: freebsd-threads@FreeBSD.org Reporter: yuri@rawbw.com Keywords: patch Created attachment 156674 --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=156674&action=edit patch While investigating the message coming from "Thread %p has exited with %d leftover", I found that _pthread_setspecific is entered twice with the same TCB, _get_curthread() returned the same thread pointer. Second invocation was incurred by the google tcmalloc library (devel/google-perftools port). > (gdb) bt > #0 yuri_enter (serno=60) at /usr/src/lib/libthr/thread/thr_spec.c:60 > #1 0x0000000804e23e89 in _pthread_setspecific (userkey=3, value=0x740a080) at /usr/src/lib/libthr/thread/thr_spec.c:228 > #2 0x000000080408b95c in ?? () from /usr/local/lib/qt4/libQtCore.so.4 > #3 0x0000000804e1da3a in thread_start (curthread=0x73f6800) at /usr/src/lib/libthr/thread/thr_create.c:288 > #4 0x0000000000000000 in ?? () > Backtrace stopped: Cannot access memory at address 0x7fffdf9fb000 > (gdb) bt > #0 yuri_enter (serno=61) at /usr/src/lib/libthr/thread/thr_spec.c:60 > #1 0x0000000804e23e89 in _pthread_setspecific (userkey=2, value=0x7a9520) at /usr/src/lib/libthr/thread/thr_spec.c:228 > #2 0x00000008008ff4de in perftools_pthread_setspecific(int, void*) () from /usr/local/lib/libtcmalloc.so.4 > #3 0x00000008008f2322 in tcmalloc::ThreadCache::CreateCacheIfNecessary() () from /usr/local/lib/libtcmalloc.so.4 > #4 0x00000008008ea666 in ?? () from /usr/local/lib/libtcmalloc.so.4 > #5 0x00000008008ffd92 in tc_calloc () from /usr/local/lib/libtcmalloc.so.4 > #6 0x0000000804e243a1 in pthread_key_allocate_data () at /usr/src/lib/libthr/thread/thr_spec.c:212 > #7 0x0000000804e23f19 in _pthread_setspecific (userkey=3, value=0x740a080) at /usr/src/lib/libthr/thread/thr_spec.c:231 > #8 0x000000080408b95c in ?? () from /usr/local/lib/qt4/libQtCore.so.4 > #9 0x0000000804e1da3a in thread_start (curthread=0x73f6800) at /usr/src/lib/libthr/thread/thr_create.c:288 > #10 0x0000000000000000 in ?? () This causes some memory loss, and stray message about leftover TLS data. The attached patch corrects for such potential problem by double-checking the pthread's "specific" array after allocating it, and frees the newly allocated copy if it happens to be already allocated. Please keep the comment, because I think these lines may appear very confusing and wrong to people without the knowledge of this specific situation. -- You are receiving this mail because: You are the assignee for the bug.