Thread: BUG #18899: FreeBSD, assembly by means of GCC with ASAN ends with error: undef reference to backtrace_symbols_fd

The following bug has been logged on the website:

Bug reference:      18899
Logged by:          Alexander Mitrokhin
Email address:      a.mitrokhin@postgrespro.ru
PostgreSQL version: 17.4
Operating system:   FreeBSD 14.2
Description:

Hello.

After `configure' we have $BUILDDIR/src/pg_config.h:

/* Define to 1 if you have the `backtrace_symbols' function. */
#define HAVE_BACKTRACE_SYMBOLS 1

When checking, such configtest.c is used:

extern char backtrace_symbols ();
int main () {
    return backtrace_symbols ();
}

With ASAN, configtest.c is built without the need for some additional
libraries, because `backtrace_symbols' defined in
/usr/local/lib/gcc13/libasan.so.8.

$ nm -D /usr/local/lib/gcc13/libasan.so.8 | grep backtrace_symbols
000000000007e6c0 T __interceptor_backtrace_symbols
000000000007e6c0 T backtrace_symbols

But when linking src/backend/postgres we will get:

src/backend/utils/error/assert.c:53:(.text+0x19a): undefined reference to
`backtrace_symbols_fd'

 46         /* If we have support for it, dump a simple backtrace */
 47 #ifdef HAVE_BACKTRACE_SYMBOLS
 48         {
 49                 void       *buf[100];
 50                 int                     nframes;
 51 
 52                 nframes = backtrace(buf, lengthof(buf));
 53                 backtrace_symbols_fd(buf, nframes, fileno(stderr));
 54         }
 55 #endif

The `configure' code that checks whether it is necessary to use the
libexecinfo:

for ac_lib in '' execinfo; do
        if test -z "$ac_lib"; then
                ac_res="none required"
        else
                ac_res=-l$ac_lib
                LIBS="-l$ac_lib $ac_func_search_save_LIBS"
        fi
        if ac_fn_c_try_link "$LINENO"; then
                ac_cv_search_backtrace_symbols=$ac_res
        fi
        if ${ac_cv_search_backtrace_symbols+:} false; then
                break
        fi
done
...
LIBS=$ac_func_search_save_LIBS

With ASAN, the check to -lexecinfo does not reach and this library is not
added to LIBS.

PS:
This corrects the problem for me:

diff --git a/configure.ac b/configure.ac
index 22441728459..fcf9c00c590 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1373,7 +1373,7 @@ AC_SEARCH_LIBS(clock_gettime, rt)
 # Cygwin:
 AC_SEARCH_LIBS(shmget, cygipc)
 # *BSD:
-AC_SEARCH_LIBS(backtrace_symbols, execinfo)
+AC_SEARCH_LIBS(backtrace_symbols_fd, execinfo)
 
 AC_SEARCH_LIBS(pthread_barrier_wait, pthread)

/foo


PG Bug reporting form <noreply@postgresql.org> writes:
> With ASAN, configtest.c is built without the need for some additional
> libraries, because `backtrace_symbols' defined in
> /usr/local/lib/gcc13/libasan.so.8.

> $ nm -D /usr/local/lib/gcc13/libasan.so.8 | grep backtrace_symbols
> 000000000007e6c0 T __interceptor_backtrace_symbols
> 000000000007e6c0 T backtrace_symbols

Isn't this a flat-out bug/oversight in libasan?  It's hard to believe
that they have a reason for overriding backtrace_symbols that would
not also apply to backtrace_symbols_fd.  Nor that it's a good idea
to risk applications seeing different behaviors from those functions.

> This corrects the problem for me:

> -AC_SEARCH_LIBS(backtrace_symbols, execinfo)
> +AC_SEARCH_LIBS(backtrace_symbols_fd, execinfo)

I do not love this fix.  It's certainly arguable that we shouldn't
assume that backtrace_symbols_fd is available just because
backtrace_symbols is, but the reverse assumption is not much better.
If we were to do anything about this, I think what it ought to be
is to make separate configure probes for the two functions and then
have assert.c say "#ifdef HAVE_BACKTRACE_SYMBOLS_FD".  But on the
whole I think this is libasan's problem not ours.  While you're
waiting for them to fix it you could just manually add
LDFLAGS=-lexecinfo in ASAN builds.

            regards, tom lane