From owner-freebsd-hackers@freebsd.org Sat Dec 19 13:51:40 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 4EA654C842D for ; Sat, 19 Dec 2020 13:51:40 +0000 (UTC) (envelope-from pjfloyd@wanadoo.fr) Received: from smtp.smtpout.orange.fr (smtp04.smtpout.orange.fr [80.12.242.126]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (Client CN "Bizanga Labs SMTP Client Certificate", Issuer "Bizanga Labs CA" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CynG32rjrz3kyY for ; Sat, 19 Dec 2020 13:51:38 +0000 (UTC) (envelope-from pjfloyd@wanadoo.fr) Received: from [192.168.1.17] ([90.112.25.211]) by mwinf5d27 with ME id 6Dra240084ZGaNJ03DraHD; Sat, 19 Dec 2020 14:51:34 +0100 X-ME-Helo: [192.168.1.17] X-ME-Auth: cGpmbG95ZEB3YW5hZG9vLmZy X-ME-Date: Sat, 19 Dec 2020 14:51:34 +0100 X-ME-IP: 90.112.25.211 Subject: Re: pthread_self() problem in DRD To: freebsd-hackers@freebsd.org References: <61236c3a-05b8-7986-e95d-a9369eaf522b@wanadoo.fr> From: Paul Floyd Message-ID: Date: Sat, 19 Dec 2020 14:51:36 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB X-Rspamd-Queue-Id: 4CynG32rjrz3kyY X-Spamd-Bar: / Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=none (mx1.freebsd.org: domain of pjfloyd@wanadoo.fr has no SPF policy when checking 80.12.242.126) smtp.mailfrom=pjfloyd@wanadoo.fr X-Spamd-Result: default: False [-0.10 / 15.00]; FREEMAIL_FROM(0.00)[wanadoo.fr]; TO_DN_NONE(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_TLS_LAST(0.00)[]; R_DKIM_NA(0.00)[]; FREEMAIL_ENVFROM(0.00)[wanadoo.fr]; ASN(0.00)[asn:3215, ipnet:80.12.240.0/20, country:FR]; MID_RHS_MATCH_FROM(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; FROM_HAS_DN(0.00)[]; RBL_DBL_DONT_QUERY_IPS(0.00)[80.12.242.126:from]; TO_MATCH_ENVRCPT_ALL(0.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; MIME_GOOD(-0.10)[text/plain]; DMARC_NA(0.00)[wanadoo.fr]; AUTH_NA(1.00)[]; RCPT_COUNT_ONE(0.00)[1]; SPAMHAUS_ZRD(0.00)[80.12.242.126:from:127.0.2.255]; NEURAL_SPAM_SHORT(1.00)[1.000]; RCVD_IN_DNSWL_NONE(0.00)[80.12.242.126:from]; R_SPF_NA(0.00)[no SPF record]; RWL_MAILSPIKE_POSSIBLE(0.00)[80.12.242.126:from]; RCVD_COUNT_TWO(0.00)[2]; MAILMAN_DEST(0.00)[freebsd-hackers] 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 13:51:40 -0000 > It is impossible to say anything definitive without minimal self-contained > example, but it is possible to make some guess. Here is the source /* dlopen_main.c */ #include #include #include #include "dlopen_lib.h" int main(int argc, char **argv) {   const char *lib = argc > 1 ? argv[1] : "./libfoo.so";   void *handle;   void (*function)();   const char *error;   handle = dlopen(lib, RTLD_NOW);   if (!handle) {     fputs (dlerror(), stderr);     exit(1);   }   function = dlsym(handle, "foo");   error = dlerror();   if (error)  {     fputs(error, stderr);     exit(1);   }   (*function)();   dlclose(handle);   return 0; } /* dlopen_lib.c */ #include #include #include #include "dlopen_lib.h" void *PrintHello(void *threadid) {   const long tid = (uintptr_t)threadid;   printf("Hello World! It's me, thread #%ld!\n", tid);   pthread_exit(NULL); } void foo() {   pthread_t thread;   int rc;   uintptr_t t = 1;   printf("In main: creating thread %ld\n", t);   rc = pthread_create(&thread, NULL, PrintHello, (void *)t);   if (rc)     printf("ERROR; return code from pthread_create() is %d\n", rc);   else     pthread_join(thread, NULL); } Command to execute: valgrind -tool=drd -q ./dlopen_main ./dlopen_lib.so > 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. That sounds quite plausible. The valgrind tools do not link with any libraries themselves. I'll see what effect doing a dlopen of libthr.so in the DRD init function has. A+ Paul