From 976720e446de1d3831bc7847b920c397a7ee26b1 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Mon, 30 Sep 2013 14:40:33 -0400 Subject: [PATCH] Fix: i386 compat code duplicated mutex instances compat_arch_x86.c is linked into many .so and even into test programs. The basic problem with this is that it contains a statically defined mutex, which will fail to protect concurrent use of this compat code by different shared objects. Fix this by defining both the mutex (now called __urcu_x86_compat_mutex) and __rcu_cas_avail as weak symbols. Therefore, the first symbol that gets loaded in a program will by used by everyone. Reported-by: Vladimir Nikulichev Signed-off-by: Mathieu Desnoyers --- compat_arch_x86.c | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/compat_arch_x86.c b/compat_arch_x86.c index 7d3b83a..3e73f9c 100644 --- a/compat_arch_x86.c +++ b/compat_arch_x86.c @@ -26,6 +26,14 @@ #include #include +/* + * Using attribute "weak" for __rcu_cas_avail and + * __urcu_x86_compat_mutex. Those are globally visible by the entire + * program, even though many shared objects may have their own version. + * The first version that gets loaded will be used by the entire + * program (executable and all shared objects). + */ + /* * It does not really matter if the constructor is called before using * the library, as long as the caller checks if __rcu_cas_avail < 0 and calls @@ -38,9 +46,11 @@ int __attribute__((constructor)) __rcu_cas_init(void); * 1: available * 0: unavailable */ +__attribute__((weak)) int __rcu_cas_avail = -1; -static pthread_mutex_t compat_mutex = PTHREAD_MUTEX_INITIALIZER; +__attribute__((weak)) +pthread_mutex_t __urcu_x86_compat_mutex = PTHREAD_MUTEX_INITIALIZER; /* * get_eflags/set_eflags/compare_and_swap_is_available imported from glibc @@ -84,7 +94,7 @@ static void mutex_lock_signal_save(pthread_mutex_t *mutex, sigset_t *oldmask) assert(!ret); ret = pthread_sigmask(SIG_BLOCK, &newmask, oldmask); assert(!ret); - ret = pthread_mutex_lock(&compat_mutex); + ret = pthread_mutex_lock(&__urcu_x86_compat_mutex); assert(!ret); } @@ -92,7 +102,7 @@ static void mutex_lock_signal_restore(pthread_mutex_t *mutex, sigset_t *oldmask) { int ret; - ret = pthread_mutex_unlock(&compat_mutex); + ret = pthread_mutex_unlock(&__urcu_x86_compat_mutex); assert(!ret); ret = pthread_sigmask(SIG_SETMASK, oldmask, NULL); assert(!ret); @@ -103,7 +113,7 @@ unsigned long _compat_uatomic_set(void *addr, unsigned long _new, int len) sigset_t mask; unsigned long result; - mutex_lock_signal_save(&compat_mutex, &mask); + mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask); switch (len) { case 1: *(unsigned char *)addr = (unsigned char)_new; @@ -125,7 +135,7 @@ unsigned long _compat_uatomic_set(void *addr, unsigned long _new, int len) result = 0; __asm__ __volatile__("ud2"); } - mutex_lock_signal_restore(&compat_mutex, &mask); + mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask); return result; } @@ -134,7 +144,7 @@ unsigned long _compat_uatomic_xchg(void *addr, unsigned long _new, int len) sigset_t mask; unsigned long retval; - mutex_lock_signal_save(&compat_mutex, &mask); + mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask); switch (len) { case 1: retval = *(unsigned char *)addr; @@ -156,7 +166,7 @@ unsigned long _compat_uatomic_xchg(void *addr, unsigned long _new, int len) retval = 0; /* silence gcc warnings */ __asm__ __volatile__("ud2"); } - mutex_lock_signal_restore(&compat_mutex, &mask); + mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask); return retval; } @@ -166,7 +176,7 @@ unsigned long _compat_uatomic_cmpxchg(void *addr, unsigned long old, unsigned long retval; sigset_t mask; - mutex_lock_signal_save(&compat_mutex, &mask); + mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask); switch (len) { case 1: { @@ -200,7 +210,7 @@ unsigned long _compat_uatomic_cmpxchg(void *addr, unsigned long old, retval = 0; /* silence gcc warnings */ __asm__ __volatile__("ud2"); } - mutex_lock_signal_restore(&compat_mutex, &mask); + mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask); return retval; } @@ -208,7 +218,7 @@ void _compat_uatomic_or(void *addr, unsigned long v, int len) { sigset_t mask; - mutex_lock_signal_save(&compat_mutex, &mask); + mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask); switch (len) { case 1: *(unsigned char *)addr |= (unsigned char)v; @@ -226,14 +236,14 @@ void _compat_uatomic_or(void *addr, unsigned long v, int len) */ __asm__ __volatile__("ud2"); } - mutex_lock_signal_restore(&compat_mutex, &mask); + mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask); } void _compat_uatomic_and(void *addr, unsigned long v, int len) { sigset_t mask; - mutex_lock_signal_save(&compat_mutex, &mask); + mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask); switch (len) { case 1: *(unsigned char *)addr &= (unsigned char)v; @@ -251,7 +261,7 @@ void _compat_uatomic_and(void *addr, unsigned long v, int len) */ __asm__ __volatile__("ud2"); } - mutex_lock_signal_restore(&compat_mutex, &mask); + mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask); } unsigned long _compat_uatomic_add_return(void *addr, unsigned long v, int len) @@ -259,7 +269,7 @@ unsigned long _compat_uatomic_add_return(void *addr, unsigned long v, int len) sigset_t mask; unsigned long result; - mutex_lock_signal_save(&compat_mutex, &mask); + mutex_lock_signal_save(&__urcu_x86_compat_mutex, &mask); switch (len) { case 1: *(unsigned char *)addr += (unsigned char)v; @@ -281,7 +291,7 @@ unsigned long _compat_uatomic_add_return(void *addr, unsigned long v, int len) result = 0; /* silence gcc warnings */ __asm__ __volatile__("ud2"); } - mutex_lock_signal_restore(&compat_mutex, &mask); + mutex_lock_signal_restore(&__urcu_x86_compat_mutex, &mask); return result; } -- 2.34.1