C语言 pthread_cond_broadcast broken with dlsym?



我正在尝试使用LD_PRELOAD机制插入对pthread_cond_broadcast的调用。我插入的pthread_cond_broadcast函数只调用原始的pthread _cond_broad。然而,对于一个调用pthread_cond_wait和pthread-cond_broadcast的非常简单的pthread代码,我要么在glibc中出现segfault(对于glibc 2.11.1),要么程序挂起(对于glibc2.15)?

插入代码(编译为共享库):

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <dlfcn.h>
static int (*orig_pthread_cond_broadcast)(pthread_cond_t *cond) = NULL;
__attribute__((constructor))
static void start() {
    orig_pthread_cond_broadcast =
        (int (*)()) dlsym(RTLD_NEXT, "pthread_cond_broadcast");
    if (orig_pthread_cond_broadcast == NULL) {
        printf("pthread_cond_broadcast not found!!!n");
        exit(1);
    }
}
__attribute__((__visibility__("default")))
int pthread_cond_broadcast(pthread_cond_t *cond) {
    return orig_pthread_cond_broadcast(cond);
}

简单的pthread程序:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t cond_mutex;
pthread_cond_t cond_var;
int condition;
void *thread0_work(void *arg) {
    pthread_mutex_lock(&cond_mutex);
    printf("Signaln");
    condition = 1;
    pthread_cond_broadcast(&cond_var);
    pthread_mutex_unlock(&cond_mutex);
    return NULL;
}
void *thread1_work(void *arg) {
    pthread_mutex_lock(&cond_mutex);
    while (condition == 0) {
        printf("Waitn");
        pthread_cond_wait(&cond_var, &cond_mutex);
        printf("Done waitingn");
    }
    pthread_mutex_unlock(&cond_mutex);
    return NULL;
}
int main() {
    pthread_t thread1;
    pthread_mutex_init(&cond_mutex, NULL);
    pthread_cond_init(&cond_var, NULL);
    pthread_create(&thread1, NULL, thread1_work, NULL);
    // Slowdown this thread, so the thread 1 does pthread_cond_wait.
    usleep(1000);
    thread0_work(NULL);
    pthread_join(thread1, NULL);
    return 0;
}

编辑:

对于glibc 2.11.1,gdb-bt给出:

(gdb) set environment LD_PRELOAD=./libintercept.so
(gdb) run
Starting program: /home/seguljac/intercept/main 
[Thread debugging using libthread_db enabled]
[New Thread 0x7ffff7436700 (LWP 19165)]
Wait
Signal
Before pthread_cond_broadcast
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff79ca0e7 in pthread_cond_broadcast@@GLIBC_2.3.2 () from /lib/libpthread.so.0
(gdb) bt
#0  0x00007ffff79ca0e7 in pthread_cond_broadcast@@GLIBC_2.3.2 () from /lib/libpthread.so.0
#1  0x00007ffff7bdb769 in pthread_cond_broadcast () from ./libintercept.so
#2  0x00000000004008e8 in thread0_work ()
#3  0x00000000004009a4 in main ()

编辑2:

(已解决)正如R..(谢谢!)所建议的,问题是在我的平台上pthread_cond_broadcast是一个版本化的符号,而dlsym给出了错误的版本。这个博客非常详细地解释了这种情况:http://blog.fesnel.com/blog/2009/08/25/preloading-with-multiple-symbol-versions/

通过函数的调用似乎最终出现在函数的另一个版本中:

With LD_PRELOAD:    __pthread_cond_broadcast_2_0 (cond=0x804a060) at old_pthread_cond_broadcast.c:37
Without LD_PRELOAD: pthread_cond_broadcast@@GLIBC_2.3.2 () at ../nptl/sysdeps/unix/sysv/linux/i386/i686/../i486/pthread_cond_broadcast.S:39

因此,您的情况与此问题类似,即您得到了pthread函数的不兼容版本:符号版本控制和dlsym

这个页面提供了一种解决问题的方法,尽管有点复杂:http://blog.fesnel.com/blog/2009/08/25/preloading-with-multiple-symbol-versions/

相关内容

  • 没有找到相关文章

最新更新