From nobody Sun Jan 12 11:31:08 2025 X-Original-To: freebsd-hackers@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4YWCrv5zZyz5kWkL for ; Sun, 12 Jan 2025 11:31:27 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4YWCrt5wtmz4RdZ; Sun, 12 Jan 2025 11:31:26 +0000 (UTC) (envelope-from kostikbel@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: from tom.home (kib@localhost [127.0.0.1] (may be forged)) by kib.kiev.ua (8.18.1/8.18.1) with ESMTP id 50CBV8xK008484; Sun, 12 Jan 2025 13:31:11 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 50CBV8xK008484 Received: (from kostik@localhost) by tom.home (8.18.1/8.18.1/Submit) id 50CBV8rg008483; Sun, 12 Jan 2025 13:31:08 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sun, 12 Jan 2025 13:31:08 +0200 From: Konstantin Belousov To: Yuri Cc: Freebsd hackers list Subject: Re: pthread_mutex_trylock crashes because _get_curthread() returns null Message-ID: References: List-Id: Technical discussions relating to FreeBSD List-Archive: https://lists.freebsd.org/archives/freebsd-hackers List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-hackers@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on tom.home X-Rspamd-Queue-Id: 4YWCrt5wtmz4RdZ X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US] On Sat, Jan 11, 2025 at 07:35:51PM -0800, Yuri wrote: > I am trying to understand this crash: > > 614│ int > 615│ __Tthr_mutex_trylock(pthread_mutex_t *mutex) > 616│ { > 617│         struct pthread *curthread; > 618│         struct pthread_mutex *m; > 619│         uint32_t id; > 620│         int ret, robust; > 621│ > 622│         ret = check_and_init_mutex(mutex, &m); > 623│         if (ret != 0) > 624│                 return (ret); > 625│         curthread = _get_curthread(); > 626│         id = TID(curthread); > 627│         if (m->m_flags & PMUTEX_FLAG_PRIVATE) > 628├───────────────> THR_CRITICAL_ENTER(curthread); > > Program received signal SIGSEGV, Segmentation fault. > Address not mapped to object. > __Tthr_mutex_trylock (mutex=) at > /disk-samsung/freebsd-src/lib/libthr/thread/thr_mutex.c:628 > 628                     THR_CRITICAL_ENTER(curthread); > > The crash occurs at the program startup within the pthread_mutex_trylock() > function. > > The immediate cause is that _get_curthread() returned null in curthread. > > > Testcase: the port databases/qdrant at rev. > e7cee8d22daf5b6360238cad603ca9f96ecd87fd (at version 1.12.5). > > > How can it be that _get_curthread() returns null? > > Shouldn't current thread be always defined? The following patch should help, please check commit fb77577e7a4995f038a5d28f42d4c3771e536fdb Author: Konstantin Belousov Date: Sun Jan 12 13:28:52 2025 +0200 pthread_mutex_trylock(): init libthr if needed Sponsored by: The FreeBSD Foundation MFC after: 1 week diff --git a/lib/libthr/thread/thr_mutex.c b/lib/libthr/thread/thr_mutex.c index ca8971cc720a..32bdc4afe65f 100644 --- a/lib/libthr/thread/thr_mutex.c +++ b/lib/libthr/thread/thr_mutex.c @@ -619,6 +619,7 @@ __Tthr_mutex_trylock(pthread_mutex_t *mutex) uint32_t id; int ret, robust; + _thr_check_init(); ret = check_and_init_mutex(mutex, &m); if (ret != 0) return (ret);