From c3641771bad91e80355e300d11ad879b7b5aebf7 Mon Sep 17 00:00:00 2001 From: Denis Khalikov Date: Tue, 26 Sep 2017 21:51:35 +0300 Subject: [asan_build] Enable asaned glibc work. Modify GetPageSize () function to return 4096 for arm and arch64 architectures. We can not call sysconf from libc befor libc is inited. Mark libasan as initfirst, in this case "patched" rtld will be able to initiazlie libasan before libc.so Change-Id: I56a582fdeafcf55b9dbab8fcd1b39be850cccc04 --- libsanitizer/asan/Makefile.am | 3 ++- libsanitizer/asan/Makefile.in | 3 ++- libsanitizer/asan/asan_linux.cc | 8 ++++++++ libsanitizer/sanitizer_common/sanitizer_linux.cc | 3 ++- packaging/gcc-aarch64.spec | 2 +- packaging/gcc-armv7l.spec | 2 +- packaging/linaro-gcc.spec | 2 +- 7 files changed, 17 insertions(+), 6 deletions(-) diff --git a/libsanitizer/asan/Makefile.am b/libsanitizer/asan/Makefile.am index bea23e54497..e535f40d30e 100644 --- a/libsanitizer/asan/Makefile.am +++ b/libsanitizer/asan/Makefile.am @@ -54,7 +54,8 @@ libasan_la_LIBADD += $(top_builddir)/libbacktrace/libsanitizer_libbacktrace.la endif libasan_la_LIBADD += $(LIBSTDCXX_RAW_CXX_LDFLAGS) -libasan_la_LDFLAGS = -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(link_libasan) +libasan_la_LDFLAGS = -Wl,-z,initfirst +libasan_la_LDFLAGS += -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(link_libasan) libasan_preinit.o: asan_preinit.o cp $< $@ diff --git a/libsanitizer/asan/Makefile.in b/libsanitizer/asan/Makefile.in index 2a183db3625..7adfa5b744f 100644 --- a/libsanitizer/asan/Makefile.in +++ b/libsanitizer/asan/Makefile.in @@ -341,7 +341,8 @@ libasan_la_LIBADD = \ $(top_builddir)/sanitizer_common/libsanitizer_common.la \ $(top_builddir)/lsan/libsanitizer_lsan.la $(am__append_2) \ $(am__append_3) $(LIBSTDCXX_RAW_CXX_LDFLAGS) -libasan_la_LDFLAGS = -version-info `grep -v '^\#' $(srcdir)/libtool-version` $(link_libasan) +libasan_la_LDFLAGS = -Wl,-z,initfirst -version-info `grep -v '^\#' \ + $(srcdir)/libtool-version` $(link_libasan) # Work around what appears to be a GNU make bug handling MAKEFLAGS # values defined in terms of make variables, as is the case for CC and diff --git a/libsanitizer/asan/asan_linux.cc b/libsanitizer/asan/asan_linux.cc index 9f058df71a5..7533f4566b0 100644 --- a/libsanitizer/asan/asan_linux.cc +++ b/libsanitizer/asan/asan_linux.cc @@ -83,6 +83,8 @@ void AsanApplyToGlobals(globals_op_fptr op, const void *needle) { void AsanCheckDynamicRTPrereqs() {} void AsanCheckIncompatibleRT() {} #else + +#ifndef ASAN_INIT_FIRST static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size, void *data) { // Continue until the first dynamic library is found @@ -96,6 +98,7 @@ static int FindFirstDSOCallback(struct dl_phdr_info *info, size_t size, *(const char **)data = info->dlpi_name; return 1; } +#endif static bool IsDynamicRTName(const char *libname) { return internal_strstr(libname, "libclang_rt.asan") || @@ -111,6 +114,10 @@ void AsanCheckDynamicRTPrereqs() { if (!ASAN_DYNAMIC) return; +// Can not use dl_iterate_phdr before libc is inited. +// Anyway if we have patched rtld we will get libasan.so +// inited early than other libraries. +#ifndef ASAN_INIT_FIRST // Ensure that dynamic RT is the first DSO in the list const char *first_dso_name = nullptr; dl_iterate_phdr(FindFirstDSOCallback, &first_dso_name); @@ -120,6 +127,7 @@ void AsanCheckDynamicRTPrereqs() { "manually preload it with LD_PRELOAD.\n"); Die(); } +#endif } void AsanCheckIncompatibleRT() { diff --git a/libsanitizer/sanitizer_common/sanitizer_linux.cc b/libsanitizer/sanitizer_common/sanitizer_linux.cc index 5d17bf7bd49..769cba7fc66 100644 --- a/libsanitizer/sanitizer_common/sanitizer_linux.cc +++ b/libsanitizer/sanitizer_common/sanitizer_linux.cc @@ -799,7 +799,8 @@ bool ThreadLister::GetDirectoryEntries() { uptr GetPageSize() { // Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array. -#if SANITIZER_ANDROID +#if SANITIZER_ANDROID || \ + (SANITIZER_LINUX && (defined(__arm__) || defined(__aarch64__))) return 4096; #elif SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__)) return EXEC_PAGESIZE; diff --git a/packaging/gcc-aarch64.spec b/packaging/gcc-aarch64.spec index 4cd7eeea003..ed47e43d6ab 100644 --- a/packaging/gcc-aarch64.spec +++ b/packaging/gcc-aarch64.spec @@ -679,7 +679,7 @@ RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/-m\(arch\|tune\|cpu\)=[^ ]*//g'` RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/-m\(sse\|fpmath\)[^ ]*//g'` } RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/ */ /g'` - +%{?asan: RPM_OPT_FLAGS=$(echo $RPM_OPT_FLAGS -DASAN_INIT_FIRST) } CFLAGS="$RPM_OPT_FLAGS" CXXFLAGS="$RPM_OPT_FLAGS" XCFLAGS="$RPM_OPT_FLAGS" \ diff --git a/packaging/gcc-armv7l.spec b/packaging/gcc-armv7l.spec index 81846a50f88..356244850d6 100644 --- a/packaging/gcc-armv7l.spec +++ b/packaging/gcc-armv7l.spec @@ -679,7 +679,7 @@ RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/-m\(arch\|tune\|cpu\)=[^ ]*//g'` RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/-m\(sse\|fpmath\)[^ ]*//g'` } RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/ */ /g'` - +%{?asan: RPM_OPT_FLAGS=$(echo $RPM_OPT_FLAGS -DASAN_INIT_FIRST) } CFLAGS="$RPM_OPT_FLAGS" CXXFLAGS="$RPM_OPT_FLAGS" XCFLAGS="$RPM_OPT_FLAGS" \ diff --git a/packaging/linaro-gcc.spec b/packaging/linaro-gcc.spec index 72c9b170de6..31ffa153f0e 100644 --- a/packaging/linaro-gcc.spec +++ b/packaging/linaro-gcc.spec @@ -676,7 +676,7 @@ RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/-m\(arch\|tune\|cpu\)=[^ ]*//g'` RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/-m\(sse\|fpmath\)[^ ]*//g'` } RPM_OPT_FLAGS=`echo $RPM_OPT_FLAGS|sed -e 's/ */ /g'` - +%{?asan: RPM_OPT_FLAGS=$(echo $RPM_OPT_FLAGS -DASAN_INIT_FIRST) } CFLAGS="$RPM_OPT_FLAGS" CXXFLAGS="$RPM_OPT_FLAGS" XCFLAGS="$RPM_OPT_FLAGS" \ -- cgit v1.2.3