From owner-freebsd-hackers@freebsd.org Sat Dec 19 12:20:59 2020 Return-Path: Delivered-To: freebsd-hackers@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C28F84C5EAE for ; Sat, 19 Dec 2020 12:20:59 +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 4CylFQ5nsxz3NLN for ; Sat, 19 Dec 2020 12:20:58 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.16.1/8.16.1) with ESMTPS id 0BJCKiM3045378 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Sat, 19 Dec 2020 14:20:47 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 0BJCKiM3045378 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 0BJCKhHP045377; Sat, 19 Dec 2020 14:20:43 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Sat, 19 Dec 2020 14:20:43 +0200 From: Konstantin Belousov To: Paul Floyd Cc: FreeBSD Hackers Subject: Re: pthread_self() problem in DRD Message-ID: References: <61236c3a-05b8-7986-e95d-a9369eaf522b@wanadoo.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <61236c3a-05b8-7986-e95d-a9369eaf522b@wanadoo.fr> 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=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on tom.home X-Rspamd-Queue-Id: 4CylFQ5nsxz3NLN X-Spamd-Bar: --- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=fail reason="No valid SPF, No valid DKIM" header.from=gmail.com (policy=none); spf=softfail (mx1.freebsd.org: 2001:470:d5e7:1::1 is neither permitted nor denied by domain of kostikbel@gmail.com) smtp.mailfrom=kostikbel@gmail.com X-Spamd-Result: default: False [-3.00 / 15.00]; RCVD_TLS_ALL(0.00)[]; ARC_NA(0.00)[]; DMARC_POLICY_SOFTFAIL(0.10)[gmail.com : No valid SPF, No valid DKIM,none]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; FROM_HAS_DN(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[2001:470:d5e7:1::1:from]; FREEMAIL_FROM(0.00)[gmail.com]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; HAS_XAW(0.00)[]; R_SPF_SOFTFAIL(0.00)[~all]; SPAMHAUS_ZRD(0.00)[2001:470:d5e7:1::1:from:127.0.2.255]; TO_MATCH_ENVRCPT_SOME(0.00)[]; TO_DN_ALL(0.00)[]; NEURAL_HAM_SHORT(-1.00)[-1.000]; RCPT_COUNT_TWO(0.00)[2]; FREEMAIL_TO(0.00)[wanadoo.fr]; FROM_EQ_ENVFROM(0.00)[]; R_DKIM_NA(0.00)[]; FREEMAIL_ENVFROM(0.00)[gmail.com]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US]; MIME_TRACE(0.00)[0:+]; MAILMAN_DEST(0.00)[freebsd-hackers]; RCVD_COUNT_TWO(0.00)[2] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 19 Dec 2020 12:20:59 -0000 On Sat, Dec 19, 2020 at 01:02:46PM +0100, Paul Floyd wrote: > Hi > > I'm trying to debug an issue in DRD (a thread hazard detection tool, part of > Valgrind). There is an issue when using shared libraries. > > A detailed description is here > > https://github.com/paulfloyd/freebsd_valgrind/issues/57 > > In short, the flow of events is > > 1. [guest] The DRD init function gets called (as it is marked > __attribute__((constructor)) > 2. [guest] DRD init sets the current thread id, calling pthread_self() > and using a client request. > 3. [host] DRD_(thread_set_pthreadid)() gets called, and it is passed > the DRD tid and the value obtained from pthread_self(). > 4. [guest] The testcase does a dlopen() and then calls a function in > the shared library > 5. [guest] the shard lib function calls pthread_create > 6. [host] pthread_create gets intercepted and performs another call to > DRD_(set_pthread_id)() > > > I've marked thinks as guest/host, in reality there is only one process > executing. > > > The problem is that the thread id obtained in step 3 is not the same as the > one obtained in step 6. The result is an assert. > > > Two questions > > 1. Is there any reason why pthread_self() should return different > values when executed from a constructor and then from a dlopen()'d > function? > 2. Is there any initialization code that affects the value returned by > pthread_self()? It's possible that something is missing. > It is impossible to say anything definitive without minimal self-contained example, but it is possible to make some guess. In FreeBSD, libc exports pthread stubs which main purpose is to satisfy linkage requirements of the shared objects that can work in multithreaded environment but do not require it. For instance, pthread_self() is exported from libc, and it returns a pointer to some internal non-sensical memory when called, until libthr is loaded and initialized. At the moment libthr is initialized, pthread_self() starts returning 'real' pthread_t. In your case, my guess is that one of two things happen: 1. you have first pthread_self() call from the process that does not have libthr loaded, then later dlopen(3) loads dso which is linked with libhtr. Second call to pthread_self() returns real pthread_t for main thread. 2. (less likely but possible to arrange) You process has libthr linked in, but your first call to pthread_self() from constructor occurs before constructors for libthr are run. Visible outcome is same.