0

I am trying to compile with address sanitizer.

gcc12 -fsanitize=address -fsanitize=leak -static-libasan my_copy.c -o asan -lpthread

Getting this error:

/usr/local/bin/ld: my_copy.o: in function `pthread_mutex_lock':
my_copy.c:(.text+0x22): multiple definition of `pthread_mutex_lock'; /usr/local/lib/gcc12/gcc/x86_64-portbld-freebsd11.4/12.0.0/../../../libasan.a(asan_interceptors.o):(.text+0x3a70): first defined here
/usr/local/bin/ld: my_copy.o: in function `pthread_mutex_unlock':
my_copy.c:(.text+0x31): multiple definition of `pthread_mutex_unlock'; /usr/local/lib/gcc12/gcc/x86_64-portbld-freebsd11.4/12.0.0/../../../libasan.a(asan_interceptors.o):(.text+0x3ab0): first defined here
collect2: error: ld returned 1 exit status

But when I remove -static-libasan, it compiles fine. I want to link libasan as static.

my_copy.c is just a snippent used to recreate this issue. And this is part of legacy library.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>


int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) {
    (void)mutex;
    (void)attr;
    return 0;
}

int pthread_mutex_destroy(pthread_mutex_t *mutex) {
    (void)mutex;
    return 0;
}

int pthread_mutex_lock(pthread_mutex_t *mutex) {
    (void)mutex;
    return 0;
}

int pthread_mutex_unlock(pthread_mutex_t *mutex) {
    (void)mutex;
    return 0;
}

int main()
{
    return EXIT_SUCCESS;
}

I understand why the error is coming, but don't know how get around it.

Nabz C
  • 538
  • 1
  • 9
  • 28

1 Answers1

1

my_copy.c is just a snippent used to recreate this issue. And this is part of legacy library.

This legacy library is trying to replace pthread_mutex* functions with code which does nothing. If your program doesn't use threads, it shouldn't need these functions.

But the fact that you are linking with -lpthread suggests that your program (or one of the libraries it uses) does use threads, in which case the legacy library makes your program completely unsafe.

Your best bet is to remove whatever object is defining pthread_mutex* functions from the legacy library -- what it's doing is a very wrong thing to do!

If you don't want to do that, no reasonable code will work (address sanitizer included), and you may as well just give up.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • @mployed Russian, I get you explanation. But this only happes when I used this option `-static-libasan`, once I remove that, it works. Just that, I need to provied the libasan library path while execution – Nabz C Mar 29 '23 at 18:52