Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 6 Feb 2004 09:34:14 -0500 (EST)
From:      Daniel Eischen <eischen@vigrid.com>
To:        Alexey Zelkin <phantom@FreeBSD.org.ua>
Cc:        freebsd-threads@freebsd.org
Subject:   Re: runtime checking of thread libraries mixup
Message-ID:  <Pine.GSO.4.10.10402060925470.7873-100000@pcnet5.pcnet.com>
In-Reply-To: <20040206130800.GA29843@phantom.cris.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Fri, 6 Feb 2004, Alexey Zelkin wrote:

> hi,
> 
> Some time ago (after switch of jdk14 port to libkse as default thread
> library) a lot of people reported problems about strange things happened
> after jdk14 port upgrade.  Most of these problems were related to
> libc_r/libkse libraries mixup (i.e. one library was linked against
> libc_r and another against libkse).  Short term fix to them was suggestion
> to map (via libmap) libkse -> libc_r.  Long term way is to recompile
> everything.  This problematic time caused me to think -- why no runtime
> checks against this issue are present.
> 
> Today I got an idea and implemented it in below patch.  Unfortunately
> this patch is not even compile tested since my -CURRENT machine is down
> now, so I unable to test in action as well.  But it would be nice to hear
> that do you think about concept and realization of this idea.
> 
> Attached patch is against libc_r, but code for libpthread and libthr should
> be absolutely same, with small 'THIS' and 'another' values tweaking.

I was working on something similar but different ;-)  I added
"extern int __thrlib_type" to libc/include/libc_private.h and
set it to 0 in libc/stdlib/exit.c (just like __isthreaded).
Then I had libc_r and libpthread check it and set it to different
unique values in their versions of uthread_init.c.  This didn't
quite work correctly because spinlocks were still being confused
between libraries (still got same spinlock errors).  I think
it could be fixed by adding checks to the spinlock functions
but I didn't really want to added additional code to them.
In the end, I thought that the spinlock error messages
were just as good an indication that you had linked
to multiple libraries as "you have linked to multiple
thread libraries" ;-)

> 
> Index: uthread_init.c
> ===================================================================
> RCS file: /home/ncvs/src/lib/libc_r/uthread/uthread_init.c,v
> retrieving revision 1.46
> diff -u -r1.46 uthread_init.c
> --- uthread_init.c	3 Dec 2003 06:54:40 -0000	1.46
> +++ uthread_init.c	6 Feb 2004 12:58:55 -0000
> @@ -64,6 +64,10 @@
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
> +#ifdef CHECK_LIBRARIES_MIX
> +#include <link.h>
> +#include <dlfcn.h>
> +#endif
>  #include "un-namespace.h"
>  
>  #include "libc_private.h"
> @@ -72,6 +76,9 @@
>  int	__pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
>  int	__pthread_mutex_lock(pthread_mutex_t *);
>  int	__pthread_mutex_trylock(pthread_mutex_t *);
> +#ifdef CHECK_LIBRARIES_MIX
> +static void check_libraries_mix(void);
> +#endif
>  
>  /*
>   * All weak references used within libc should be in this table.
> @@ -221,6 +228,9 @@
>  		/* Only initialise the threaded application once. */
>  		return;
>  
> +#ifdef CHECK_LIBRARIES_MIX
> +	check_libraries_mix();
> +#endif
>  	_pthread_page_size = getpagesize();;
>  	_pthread_guard_default = _pthread_page_size;
>  	sched_stack_size = 4 * _pthread_page_size;
> @@ -563,5 +573,37 @@
>  {
>  	_thread_init();
>  	return (main(argc, argv, env));
> +}
> +#endif
> +
> +/*
> + * Check if threading libraries are mixed
> + */
> +#ifdef CHECK_LIBRARIES_MIX
> +static void
> +check_libraries_mix(void)
> +{
> +	Link_map *map;
> +	char *another [] = { "libpthread", "libkse", "libthr" };
> +	char *s[80];
> +	int i;
> +
> +#define ANOTHERSZ (sizeof(another)/sizeof(another[0]))
> +#define THIS "libc_r"
> +#define MSG "DANGER: Thread libraries mixup: both " THIS " and %s are used\n"
> +
> +	dlinfo(RTLD_SELF, RTLD_DI_LINKMAP, &map);
> +	while (map != NULL) {
> +		if (strstr(map->l_name, "lib") != NULL) {
> +			for (i = 0; i++; i < ANOTHERSZ) {
> +				if (strstr(map->l_name, another[i])) {
> +					snprintf(s, sizeof(s), MSG, another[i]);
> +					__sys_write(2, s, strlen(s));
> +					return;
> +				}
> +			}
> +		}
> +		map = map->l_next;
> +	}
>  }
>  #endif
> 
> -- 
> /* Alexey Zelkin             && Independent Contractor      */
> /* phantom(at)FreeBSD.org    && http://www.FreeBSD.org/java */
> /* phantom(at)cris.net       && http://www.FreeBSD.org.ua/  */
> _______________________________________________
> freebsd-threads@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-threads
> To unsubscribe, send any mail to "freebsd-threads-unsubscribe@freebsd.org"
> 

-- 
"Some folks are into open source, but me, I'm into open bar."
                                          -- Spencer F. Katt



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.GSO.4.10.10402060925470.7873-100000>