runtime checking of thread libraries mixup
Alexey Zelkin
phantom at FreeBSD.org.ua
Fri Feb 6 05:07:25 PST 2004
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 <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/ */
More information about the freebsd-threads
mailing list