From owner-freebsd-threads@FreeBSD.ORG Fri Feb 6 05:07:24 2004 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C92E416A4CE for ; Fri, 6 Feb 2004 05:07:24 -0800 (PST) Received: from phantom.cris.net (phantom.cris.net [212.110.130.74]) by mx1.FreeBSD.org (Postfix) with ESMTP id 011B543D2F for ; Fri, 6 Feb 2004 05:07:07 -0800 (PST) (envelope-from phantom@FreeBSD.org.ua) Received: from phantom.cris.net (phantom@localhost [127.0.0.1]) by phantom.cris.net (8.12.10/8.12.10) with ESMTP id i16D80oh029882; Fri, 6 Feb 2004 15:08:00 +0200 (EET) (envelope-from phantom@FreeBSD.org.ua) Received: (from phantom@localhost) by phantom.cris.net (8.12.10/8.12.10/Submit) id i16D809H029881; Fri, 6 Feb 2004 15:08:00 +0200 (EET) (envelope-from phantom) Date: Fri, 6 Feb 2004 15:08:00 +0200 From: Alexey Zelkin To: freebsd-threads@FreeBSD.org Message-ID: <20040206130800.GA29843@phantom.cris.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Operating-System: FreeBSD 4.9-STABLE i386 User-Agent: Mutt/1.5.5.1i Subject: runtime checking of thread libraries mixup X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 06 Feb 2004 13:07:25 -0000 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. 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 #include #include +#ifdef CHECK_LIBRARIES_MIX +#include +#include +#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/ */