This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
building GLIBC with -fsanitize=address
- From: Konstantin Serebryany <konstantin dot s dot serebryany at gmail dot com>
- To: GNU C Library <libc-alpha at sourceware dot org>, Roland McGrath <roland at hack dot frob dot com>, "Carlos O'Donell" <carlos at redhat dot com>
- Date: Tue, 26 Aug 2014 15:27:17 -0700
- Subject: building GLIBC with -fsanitize=address
- Authentication-results: sourceware.org; auth=none
Hello,
I want to ask some assistance with building the GLIBC with AddressSanitizer
(aka asan, GCC's -fsanitize=address flag).
The end goal is to have libc-asan.so, a shared libc library with code
instrumented by asan.
Simply doing CFLAGS='-g -O1 -fsanitize=address' ../glibc/configure ..
does not work -- it fails at configure time with
configure: error: Need linker with .init_array/.fini_array support.
Even if we bypass this error there will be others and, finally, we
don't want to instrument
every file we build (e.g. there are object files that go into the
ld.so which we don't want to instrument).
One of the things we discussed at the Cauldron was to add a configure
option (e.g. --enable-asan)
similar to the existing --enable-profile which adds the -pg build flag.
This is indeed similar with one major difference: --enable-profile
builds a static libc and
for asan we need a DSO. I've managed to make a patch that builds
libc-asan.a (attached)
but I don't see how to get libc-asan.so from it.
Thoughts?
Thanks,
--kcc
diff --git a/Makeconfig b/Makeconfig
index cef0f06..d2f4506 100644
--- a/Makeconfig
+++ b/Makeconfig
@@ -846,7 +846,7 @@ endif
# The compilation rules use $(CPPFLAGS-${SUFFIX}) and $(CFLAGS-${SUFFIX})
# to pass different flags for each flavor.
libtypes = $(foreach o,$(object-suffixes-for-libc),$(libtype$o))
-all-object-suffixes := .o .os .op .og .oS
+all-object-suffixes := .o .os .op .og .oS .oasan
object-suffixes :=
CPPFLAGS-.o = $(pic-default)
CFLAGS-.o = $(filter %frame-pointer,$(+cflags))
@@ -877,6 +877,15 @@ CFLAGS-.op = -pg
libtype.op = lib%_p.a
endif
+ifeq (yes,$(build-asan))
+# Under --enable-asan, TODO.
+object-suffixes += .op
+CPPFLAGS-.op = $(pic-default)
+CFLAGS-.op = -fsanitize=address
+libtype.op = lib%_asan.a
+endif
+
+
# Convenience variable for when we want to treat shared-library cases
# differently from the rest.
object-suffixes-noshared := $(filter-out .os,$(object-suffixes))
diff --git a/config.make.in b/config.make.in
index 6bcab8a..eef62a9 100644
--- a/config.make.in
+++ b/config.make.in
@@ -82,6 +82,7 @@ nss-crypt = @libc_cv_nss_crypt@
build-shared = @shared@
build-pic-default= @libc_cv_pic_default@
build-profile = @profile@
+build-asan = @asan@
build-static-nss = @static_nss@
add-ons = @add_ons@
add-on-subdirs = @add_on_subdirs@
diff --git a/configure b/configure
index c8d2967..c928def 100755
--- a/configure
+++ b/configure
@@ -576,6 +576,7 @@ mach_interface_list
DEFINES
static_nss
profile
+asan
libc_cv_pic_default
shared
static
@@ -737,6 +738,7 @@ with_default_link
enable_sanity_checks
enable_shared
enable_profile
+enable_asan
enable_oldest_abi
enable_hardcoded_path_in_tests
enable_stackguard_randomization
@@ -1390,6 +1392,8 @@ Optional Features:
in special situations) [default=yes]
--enable-shared build shared library [default=yes if GNU ld]
--enable-profile build profiled library [default=no]
+ --enable-asan build library instrumented with AddressSanitizer
+ (ASan) [default=no]
--enable-oldest-abi=ABI configure the oldest ABI supported [e.g. 2.2]
[default=glibc default]
--enable-hardcoded-path-in-tests
@@ -3431,6 +3435,13 @@ else
profile=no
fi
+# Check whether --enable-asan was given.
+if test "${enable_asan+set}" = set; then :
+ enableval=$enable_asan; asan=$enableval
+else
+ asan=no
+fi
+
# Check whether --enable-oldest-abi was given.
if test "${enable_oldest_abi+set}" = set; then :
diff --git a/configure.ac b/configure.ac
index 566ecb2..cb4bae4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -150,6 +150,11 @@ AC_ARG_ENABLE([profile],
[build profiled library @<:@default=no@:>@]),
[profile=$enableval],
[profile=no])
+AC_ARG_ENABLE([asan],
+ AC_HELP_STRING([--enable-asan],
+ [build library instrumented with AddressSanitizer (ASan) @<:@default=no@:>@]),
+ [asan=$enableval],
+ [asan=no])
AC_ARG_ENABLE([oldest-abi],
AC_HELP_STRING([--enable-oldest-abi=ABI],
@@ -1996,6 +2001,7 @@ rm -f conftest.*])
AC_SUBST(libc_cv_pic_default)
AC_SUBST(profile)
+AC_SUBST(asan)
AC_SUBST(static_nss)
AC_SUBST(DEFINES)