From owner-svn-src-all@freebsd.org Mon Feb 4 06:31:21 2019 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B539F14C0367; Mon, 4 Feb 2019 06:31:21 +0000 (UTC) (envelope-from melounmichal@gmail.com) Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B21F56FD71; Mon, 4 Feb 2019 06:31:20 +0000 (UTC) (envelope-from melounmichal@gmail.com) Received: by mail-wm1-x331.google.com with SMTP id y185so9254030wmd.1; Sun, 03 Feb 2019 22:31:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:reply-to:subject:to:references:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=sC/SLg2pUnbyoBMshbaFsh0dxktRrbThG+5kAAH++2g=; b=d/UCCOjCKVNSGGW+TAJ5vaOpnirIExdVlEeEg1MQjXhPu/R4KJnLjzITdfPpjNr7Gz KOqKBCyIq9uU3Xbly6ThEf8DtbTSEjKLmulUCkFBwU+qn8ubkPMJElEVJoh0Ilgv94Cr 7et7YagcJYDY4NieZCtSQHyR4HfwNZ3rUEtsn2jyVUbN8oSDonYsjjY2wFBnd2tULuqG WR5DfAN7D7DwIQuD2j9zlFxdCsELPr3XcZehg14HSKWARCrMS0j3aHFCLfdXbhbD5jjy 3yOgbXVapTUlPsOpoHd6Extgm7rfAqRT+kMdR5poYjh7ZXaV6OaOrdoDe4qF8cx9yuIv SRoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:reply-to:subject:to:references :openpgp:autocrypt:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=sC/SLg2pUnbyoBMshbaFsh0dxktRrbThG+5kAAH++2g=; b=VET9HF1/ksUbTR5wSPbRv9DIJFqyXbp2o1Z52KrD3/KL7I9eBmvux+IgGIGFpmMdPr OGiIrXYs9MdN+EZcnDFHPD/EkimFDKNY4p/E/aNuWjNXIMiDCo7+hpTAC0WmRG85r1kt aV+4IQjkvjlHLp5SjGwU1VE+voyoampwyXuWJYnCabUJxyvXxPilX1n1chqzifuHHXww mVqB3Io6Czoo+C/L9+o2DY88u3YFlRE12URO/xZfapFcHl+rOgNqw+6aEpUl3WF4sDXl AFk5VgVnc+Ck/nvmtcbCDXs3SIK/tv4XBRCS6U5Jr3/IqHRfVZ7yrYpv75kqO3UG9ekD cblw== X-Gm-Message-State: AHQUAuYNtxiXaX128wQy8/nFb9yg690ghl7W+KpoQIsHWErAdGdGpaQf OL/v75SwHEtEEQiLza3baWqCovqe X-Google-Smtp-Source: AHgI3IYa5PRQnW8uuRLpEzLY6Uqc3teXMrOuZ0MwSExIdHcd4rDT9awmj2R498r7AY9O8/26z5Z8xg== X-Received: by 2002:a1c:f707:: with SMTP id v7mr12149855wmh.18.1549261879163; Sun, 03 Feb 2019 22:31:19 -0800 (PST) Received: from [88.208.79.100] (halouny.humusoft.cz. [88.208.79.100]) by smtp.gmail.com with ESMTPSA id g67sm16429897wmd.38.2019.02.03.22.31.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 03 Feb 2019 22:31:18 -0800 (PST) Sender: Michal Meloun From: Michal Meloun X-Google-Original-From: Michal Meloun Reply-To: mmel@freebsd.org Subject: Re: svn commit: r343566 - in head/lib/libthr: . thread To: Konstantin Belousov , src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org References: <201901292246.x0TMkjQH074121@repo.freebsd.org> Openpgp: preference=signencrypt Autocrypt: addr=mmel@freebsd.org; prefer-encrypt=mutual; keydata= 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 FFob7daQBLxi80oUVRn6eEdzR8tBhIy7sbeD Message-ID: Date: Mon, 4 Feb 2019 07:31:19 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: <201901292246.x0TMkjQH074121@repo.freebsd.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: B21F56FD71 X-Spamd-Bar: ----- Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20161025 header.b=d/UCCOjC; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (mx1.freebsd.org: domain of melounmichal@gmail.com designates 2a00:1450:4864:20::331 as permitted sender) smtp.mailfrom=melounmichal@gmail.com X-Spamd-Result: default: False [-5.69 / 15.00]; RCVD_VIA_SMTP_AUTH(0.00)[]; HAS_REPLYTO(0.00)[mmel@freebsd.org]; TO_DN_SOME(0.00)[]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36]; FREEMAIL_FROM(0.00)[gmail.com]; RCVD_COUNT_THREE(0.00)[3]; DKIM_TRACE(0.00)[gmail.com:+]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; MX_GOOD(-0.01)[cached: alt3.gmail-smtp-in.l.google.com]; NEURAL_HAM_SHORT(-0.97)[-0.966,0]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; TAGGED_FROM(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com.dwl.dnswl.org : 127.0.5.0]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000,0]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20161025]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_THREE(0.00)[4]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; MIME_GOOD(-0.10)[text/plain]; REPLYTO_DOM_NEQ_FROM_DOM(0.00)[]; MID_RHS_MATCH_TO(1.00)[]; IP_SCORE(-2.71)[ip: (-9.27), ipnet: 2a00:1450::/32(-2.30), asn: 15169(-1.93), country: US(-0.07)]; RCVD_IN_DNSWL_NONE(0.00)[1.3.3.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.4.6.8.4.0.5.4.1.0.0.a.2.list.dnswl.org : 127.0.5.0] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Feb 2019 06:31:22 -0000 On 29.01.2019 23:46, Konstantin Belousov wrote: > Author: kib > Date: Tue Jan 29 22:46:44 2019 > New Revision: 343566 > URL: https://svnweb.freebsd.org/changeset/base/343566 > > Log: > Untangle jemalloc and mutexes initialization. > > The need to use libc malloc(3) from some places in libthr always > caused issues. For instance, per-thread key allocation was switched to > use plain mmap(2) to get storage, because some third party mallocs > used keys for implementation of calloc(3). > > Even more important, libthr calls calloc(3) during initialization of > pthread mutexes, and jemalloc uses pthread mutexes. Jemalloc provides > some way to both postpone the initialization, and to make > initialization to use specialized allocator, but this is very fragile > and often breaks. See the referenced PR for another example. > > Add the small malloc implementation used by rtld, to libthr. Use it in > thr_spec.c and for mutexes initialization. This avoids the issues with > mutual dependencies between malloc and libthr in principle. The > drawback is that some more allocations are not interceptable for > alternate malloc implementations. There should be not too much memory > use from this allocator, and the alternative, direct use of mmap(2) is > obviously worse. > > PR: 235211 > MFC after: 2 weeks > Sponsored by: The FreeBSD Foundation > Differential revision: https://reviews.freebsd.org/D18988 > This broke ARM static binaries (at least rescue/rescue). From first look it seems that __pthread_mutex_init() invoked by atomic_init() is called before curthread is set (before _thread_init_hack()). root@tegra124:/usr/src # gdb --args /usr/obj/usr/src/arm.armv7/rescue/rescue/rescue sh ... Reading symbols from /usr/obj/usr/src/arm.armv7/rescue/rescue/rescue...done. (gdb) b _thread_init_hack Breakpoint 1 at 0x67cad0: file /usr/src/lib/libthr/thread/thr_init.c, line 296. (gdb) r Starting program: /usr/obj/usr/src/arm.armv7/rescue/rescue/rescue sh Program received signal SIGSEGV, Segmentation fault. __thr_calloc (num=1, size=) at /usr/src/lib/libthr/thread/thr_malloc.c:82 82 thr_malloc_lock(curthread); (gdb) bt #0 __thr_calloc (num=1, size=) at /usr/src/lib/libthr/thread/thr_malloc.c:82 #1 0x00676e1c in mutex_init (mutex=, mutex_attr=, calloc_cb=) at /usr/src/lib/libthr/thread/thr_mutex.c:294 #2 __pthread_mutex_init (mutex=0xc6e948 , mutex_attr=) at /usr/src/lib/libthr/thread/thr_mutex.c:393 #3 0x001d41f0 in handle_static_init (argc=2, argv=, env=) at /usr/src/lib/csu/common/ignore_init.c:124 #4 0x001d40e8 in __start (argc=2, argv=, env=, ps_strings=, obj=0x0, cleanup=0x0) at /usr/src/lib/csu/arm/crt1.c:112 #5 0x001d4000 in ?? () Backtrace stopped: previous frame identical to this frame (corrupt stack?) (gdb) f 3 #3 0x001d41f0 in handle_static_init (argc=2, argv=, env=) at /usr/src/lib/csu/common/ignore_init.c:124 124 fn(argc, argv, env); (gdb) list 119 _init(); 120 array_size = __init_array_end - __init_array_start; 121 for (n = 0; n < array_size; n++) { 122 fn = __init_array_start[n]; 123 if ((uintptr_t)fn != 0 && (uintptr_t)fn != 1) 124 fn(argc, argv, env); 125 } 126 } (gdb) p n $1 = 20 (gdb) p fn $6 = (void (*)(int, char **, char **)) 0x63f21c (gdb) p (void *)&__init_array_start $19 = (void *) 0xa7ab60 (gdb) p (void *)&__init_array_end $20 = (void *) 0xa7abc0 (gdb) x/24a &__init_array_start 0xa7ab60: 0x1d4270 0x2d926c <_$$hide$$ ifconfig.lo ifconfig_ctor> 0x2d98e0 <_$$hide$$ ifconfig.lo link_ctor> 0x2d9cb4 <_$$hide$$ ifconfig.lo inet_ctor> 0xa7ab70: 0x2da2e0 <_$$hide$$ ifconfig.lo inet6_ctor> 0x2db790 <_$$hide$$ ifconfig.lo clone_ctor> 0x2dbb8c <_$$hide$$ ifconfig.lo mac_ctor> 0x2dbe70 <_$$hide$$ ifconfig.lo ifmedia_ctor> 0xa7ab80: 0x2dced0 <_$$hide$$ ifconfig.lo fib_ctor> 0x2dd118 <_$$hide$$ ifconfig.lo vlan_ctor> 0x2dd668 <_$$hide$$ ifconfig.lo vxlan_ctor> 0x2df45c <_$$hide$$ ifconfig.lo gre_ctor> 0xa7ab90: 0x2df6d8 <_$$hide$$ ifconfig.lo gif_ctor> 0x2df874 <_$$hide$$ ifconfig.lo ipsec_ctor> 0x2e2144 <_$$hide$$ ifconfig.lo ieee80211_ctor> 0x2f0a10 <_$$hide$$ ifconfig.lo carp_ctor> 0xa7aba0: 0x2f0e6c <_$$hide$$ ifconfig.lo group_ctor> 0x2f199c <_$$hide$$ ifconfig.lo pfsync_ctor> 0x2f1a04 <_$$hide$$ ifconfig.lo bridge_ctor> 0x2f35dc <_$$hide$$ ifconfig.lo lagg_ctor> 0xa7abb0: 0x63f21c 0x67cad0 <_thread_init_hack> 0x90e5b4 0x9a6b98 So it's clear that order of static constructors is invalid (moreover, I thing that we are inside undefined area here). Any idea/hint how to fix this. Thanks, Michal > Added: > head/lib/libthr/thread/thr_malloc.c (contents, props changed) > Modified: > head/lib/libthr/Makefile > head/lib/libthr/thread/Makefile.inc > head/lib/libthr/thread/thr_fork.c > head/lib/libthr/thread/thr_init.c > head/lib/libthr/thread/thr_mutex.c > head/lib/libthr/thread/thr_private.h > head/lib/libthr/thread/thr_spec.c > > Modified: head/lib/libthr/Makefile > ============================================================================== > --- head/lib/libthr/Makefile Tue Jan 29 22:45:24 2019 (r343565) > +++ head/lib/libthr/Makefile Tue Jan 29 22:46:44 2019 (r343566) > @@ -27,6 +27,7 @@ CFLAGS+=-I${SRCTOP}/lib/libthread_db > CFLAGS+=-Winline > > CFLAGS.thr_stack.c+= -Wno-cast-align > +CFLAGS.malloc.c+= -Wno-cast-align > .include > .if !(${COMPILER_TYPE} == "gcc" && ${COMPILER_VERSION} < 40300) > CFLAGS.thr_symbols.c+= -Wno-missing-variable-declarations > @@ -50,12 +51,14 @@ CFLAGS+=-D_PTHREADS_INVARIANTS > PRECIOUSLIB= > > .PATH: ${.CURDIR}/arch/${MACHINE_CPUARCH}/${MACHINE_CPUARCH} > +.PATH: ${SRCTOP}/libexec/rtld-elf > > .if exists(${.CURDIR}/arch/${MACHINE_CPUARCH}/Makefile.inc) > .include "${.CURDIR}/arch/${MACHINE_CPUARCH}/Makefile.inc" > .endif > .include "${.CURDIR}/sys/Makefile.inc" > .include "${.CURDIR}/thread/Makefile.inc" > +SRCS+= malloc.c > > .if ${MK_INSTALLLIB} != "no" > SYMLINKS+=lib${LIB}.a ${LIBDIR}/libpthread.a > > Modified: head/lib/libthr/thread/Makefile.inc > ============================================================================== > --- head/lib/libthr/thread/Makefile.inc Tue Jan 29 22:45:24 2019 (r343565) > +++ head/lib/libthr/thread/Makefile.inc Tue Jan 29 22:46:44 2019 (r343566) > @@ -31,6 +31,7 @@ SRCS+= \ > thr_kern.c \ > thr_kill.c \ > thr_main_np.c \ > + thr_malloc.c \ > thr_multi_np.c \ > thr_mutex.c \ > thr_mutexattr.c \ > > Modified: head/lib/libthr/thread/thr_fork.c > ============================================================================== > --- head/lib/libthr/thread/thr_fork.c Tue Jan 29 22:45:24 2019 (r343565) > +++ head/lib/libthr/thread/thr_fork.c Tue Jan 29 22:46:44 2019 (r343566) > @@ -170,6 +170,7 @@ __thr_fork(void) > */ > if (_thr_isthreaded() != 0) { > was_threaded = 1; > + __thr_malloc_prefork(curthread); > _malloc_prefork(); > __thr_pshared_atfork_pre(); > _rtld_atfork_pre(rtld_locks); > @@ -197,6 +198,10 @@ __thr_fork(void) > */ > curthread->tlflags &= ~TLFLAGS_IN_TDLIST; > > + /* before thr_self() */ > + if (was_threaded) > + __thr_malloc_postfork(curthread); > + > /* child is a new kernel thread. */ > thr_self(&curthread->tid); > > @@ -241,6 +246,7 @@ __thr_fork(void) > _thr_signal_postfork(); > > if (was_threaded) { > + __thr_malloc_postfork(curthread); > _rtld_atfork_post(rtld_locks); > __thr_pshared_atfork_post(); > _malloc_postfork(); > > Modified: head/lib/libthr/thread/thr_init.c > ============================================================================== > --- head/lib/libthr/thread/thr_init.c Tue Jan 29 22:45:24 2019 (r343565) > +++ head/lib/libthr/thread/thr_init.c Tue Jan 29 22:46:44 2019 (r343566) > @@ -461,6 +461,7 @@ init_private(void) > */ > if (init_once == 0) { > __thr_pshared_init(); > + __thr_malloc_init(); > /* Find the stack top */ > mib[0] = CTL_KERN; > mib[1] = KERN_USRSTACK; > > Added: head/lib/libthr/thread/thr_malloc.c > ============================================================================== > --- /dev/null 00:00:00 1970 (empty, because file is newly added) > +++ head/lib/libthr/thread/thr_malloc.c Tue Jan 29 22:46:44 2019 (r343566) > @@ -0,0 +1,137 @@ > +/*- > + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD > + * > + * Copyright (c) 2019 The FreeBSD Foundation > + * All rights reserved. > + * > + * This software was developed by Konstantin Belousov > + * under sponsorship from the FreeBSD Foundation. > + * > + * Redistribution and use in source and binary forms, with or without > + * modification, are permitted provided that the following conditions > + * are met: > + * 1. Redistributions of source code must retain the above copyright > + * notice, this list of conditions and the following disclaimer. > + * 2. Redistributions in binary form must reproduce the above copyright > + * notice, this list of conditions and the following disclaimer in the > + * documentation and/or other materials provided with the distribution. > + * > + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND > + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE > + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE > + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE > + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL > + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS > + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) > + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT > + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY > + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF > + * SUCH DAMAGE. > + */ > + > +#include > +__FBSDID("$FreeBSD$"); > + > +#include > +#include > +#include > +#include "thr_private.h" > + > +int npagesizes; > +size_t *pagesizes; > +static size_t pagesizes_d[2]; > +static struct umutex thr_malloc_umtx; > + > +void > +__thr_malloc_init(void) > +{ > + > + npagesizes = getpagesizes(pagesizes_d, nitems(pagesizes_d)); > + if (npagesizes == -1) { > + npagesizes = 1; > + pagesizes_d[0] = PAGE_SIZE; > + } > + pagesizes = pagesizes_d; > + _thr_umutex_init(&thr_malloc_umtx); > +} > + > +static void > +thr_malloc_lock(struct pthread *curthread) > +{ > + > + curthread->locklevel++; > + _thr_umutex_lock(&thr_malloc_umtx, TID(curthread)); > +} > + > +static void > +thr_malloc_unlock(struct pthread *curthread) > +{ > + > + _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); > + curthread->locklevel--; > + _thr_ast(curthread); > +} > + > +void * > +__thr_calloc(size_t num, size_t size) > +{ > + struct pthread *curthread; > + void *res; > + > + curthread = _get_curthread(); > + thr_malloc_lock(curthread); > + res = __crt_calloc(num, size); > + thr_malloc_unlock(curthread); > + return (res); > +} > + > +void > +__thr_free(void *cp) > +{ > + struct pthread *curthread; > + > + curthread = _get_curthread(); > + thr_malloc_lock(curthread); > + __crt_free(cp); > + thr_malloc_unlock(curthread); > +} > + > +void * > +__thr_malloc(size_t nbytes) > +{ > + struct pthread *curthread; > + void *res; > + > + curthread = _get_curthread(); > + thr_malloc_lock(curthread); > + res = __crt_malloc(nbytes); > + thr_malloc_unlock(curthread); > + return (res); > +} > + > +void * > +__thr_realloc(void *cp, size_t nbytes) > +{ > + struct pthread *curthread; > + void *res; > + > + curthread = _get_curthread(); > + thr_malloc_lock(curthread); > + res = __crt_realloc(cp, nbytes); > + thr_malloc_unlock(curthread); > + return (res); > +} > + > +void > +__thr_malloc_prefork(struct pthread *curthread) > +{ > + > + _thr_umutex_lock(&thr_malloc_umtx, TID(curthread)); > +} > + > +void > +__thr_malloc_postfork(struct pthread *curthread) > +{ > + > + _thr_umutex_unlock(&thr_malloc_umtx, TID(curthread)); > +} > > Modified: head/lib/libthr/thread/thr_mutex.c > ============================================================================== > --- head/lib/libthr/thread/thr_mutex.c Tue Jan 29 22:45:24 2019 (r343565) > +++ head/lib/libthr/thread/thr_mutex.c Tue Jan 29 22:46:44 2019 (r343566) > @@ -306,10 +306,11 @@ init_static(struct pthread *thread, pthread_mutex_t *m > THR_LOCK_ACQUIRE(thread, &_mutex_static_lock); > > if (*mutex == THR_MUTEX_INITIALIZER) > - ret = mutex_init(mutex, &_pthread_mutexattr_default, calloc); > + ret = mutex_init(mutex, &_pthread_mutexattr_default, > + __thr_calloc); > else if (*mutex == THR_ADAPTIVE_MUTEX_INITIALIZER) > ret = mutex_init(mutex, &_pthread_mutexattr_adaptive_default, > - calloc); > + __thr_calloc); > else > ret = 0; > THR_LOCK_RELEASE(thread, &_mutex_static_lock); > @@ -390,7 +391,7 @@ __pthread_mutex_init(pthread_mutex_t * __restrict mute > if (mutex_attr == NULL || > (*mutex_attr)->m_pshared == PTHREAD_PROCESS_PRIVATE) { > return (mutex_init(mutex, mutex_attr ? *mutex_attr : NULL, > - calloc)); > + __thr_calloc)); > } > pmtx = __thr_pshared_offpage(__DECONST(void *, mutex), 1); > if (pmtx == NULL) > @@ -483,7 +484,7 @@ _pthread_mutex_destroy(pthread_mutex_t *mutex) > } else { > *mutex = THR_MUTEX_DESTROYED; > mutex_assert_not_owned(_get_curthread(), m); > - free(m); > + __thr_free(m); > ret = 0; > } > } > > Modified: head/lib/libthr/thread/thr_private.h > ============================================================================== > --- head/lib/libthr/thread/thr_private.h Tue Jan 29 22:45:24 2019 (r343565) > +++ head/lib/libthr/thread/thr_private.h Tue Jan 29 22:46:44 2019 (r343566) > @@ -1003,6 +1003,14 @@ void __thr_pshared_destroy(void *key) __hidden; > void __thr_pshared_atfork_pre(void) __hidden; > void __thr_pshared_atfork_post(void) __hidden; > > +void *__thr_calloc(size_t num, size_t size); > +void __thr_free(void *cp); > +void *__thr_malloc(size_t nbytes); > +void *__thr_realloc(void *cp, size_t nbytes); > +void __thr_malloc_init(void); > +void __thr_malloc_prefork(struct pthread *curthread); > +void __thr_malloc_postfork(struct pthread *curthread); > + > __END_DECLS > __NULLABILITY_PRAGMA_POP > > > Modified: head/lib/libthr/thread/thr_spec.c > ============================================================================== > --- head/lib/libthr/thread/thr_spec.c Tue Jan 29 22:45:24 2019 (r343565) > +++ head/lib/libthr/thread/thr_spec.c Tue Jan 29 22:46:44 2019 (r343566) > @@ -155,8 +155,7 @@ _thread_cleanupspecific(void) > } > } > THR_LOCK_RELEASE(curthread, &_keytable_lock); > - munmap(curthread->specific, PTHREAD_KEYS_MAX * sizeof(struct > - pthread_specific_elem)); > + __thr_free(curthread->specific); > curthread->specific = NULL; > if (curthread->specific_data_count > 0) { > stderr_debug("Thread %p has exited with leftover " > @@ -179,10 +178,9 @@ _pthread_setspecific(pthread_key_t userkey, const void > > pthread = _get_curthread(); > if (pthread->specific == NULL) { > - tmp = mmap(NULL, PTHREAD_KEYS_MAX * > - sizeof(struct pthread_specific_elem), > - PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); > - if (tmp == MAP_FAILED) > + tmp = __thr_calloc(PTHREAD_KEYS_MAX, > + sizeof(struct pthread_specific_elem)); > + if (tmp == NULL) > return (ENOMEM); > pthread->specific = tmp; > } >