From 19de09973fae11babb5864e80172f6147d36afbc Mon Sep 17 00:00:00 2001 From: Slava Barinov Date: Thu, 19 Aug 2021 14:05:29 +0300 Subject: [PATCH] [HWASan] Prepare hwasan-enabled build Package glibc-hwasan.rpm provides HWASan-sanitized version of glibc for testing purposes. Change-Id: Ie5a3ba6b80823dbc422b281846d64a6740674d2c Signed-off-by: Slava Barinov --- gcc-wrapper.cc | 21 +++++++++++-- packaging/glibc.spec | 89 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 102 insertions(+), 8 deletions(-) diff --git a/gcc-wrapper.cc b/gcc-wrapper.cc index 3ab9323..ebe31a1 100644 --- a/gcc-wrapper.cc +++ b/gcc-wrapper.cc @@ -8,23 +8,39 @@ #endif const char *GCC = "/usr/bin/gcc"; -static const int ASAN_OPTIONS_MAX = 7; static const int ASAN_OPTIONS_MIN = 3; +#ifndef HWASAN_BUILD +static const int ASAN_OPTIONS_MAX = 7; +#else +static const int ASAN_OPTIONS_MAX = 5; +#endif static std::string blacklist[] = { "rtld", "/dl-", "elf", "string/mem", "time/time", "time/gettimeofday", "time/timegm", "time/timespec_get", "nptl/libc_pthread_init", "nptl/register-atfork", +#ifndef HWASAN_BUILD // In Tizen, ASan tries to write error log to procfs, in case procfs is not // mounted, this action will generate a kernel panic. So, just disable // errors which happens because of dword-aligned reading by glibc. - "string"}; + "string", +#else + "/init-", "ctype-info", "setjmp/", +#endif +}; +#ifndef HWASAN_BUILD static const char *asan_options[ASAN_OPTIONS_MAX] = { "-fno-omit-frame-pointer", "-Wno-error", "-DSKIP_IFUNC", "-fsanitize-recover=address", "-fsanitize=address", "--param", "asan-use-after-return=0", }; +#else +static const char *asan_options[ASAN_OPTIONS_MAX] = { + "-fno-omit-frame-pointer", "-Wno-error", "-DSKIP_IFUNC", + "-fsanitize=hwaddress", "-fsanitize-recover=hwaddress", +}; +#endif static const char *defs_option = "-Wl,-z,defs"; static const size_t defs_option_length = 11; @@ -98,4 +114,3 @@ int main(int argc, const char **argv) { std::cerr << "Errno: " << errno; #endif } - diff --git a/packaging/glibc.spec b/packaging/glibc.spec index f83324b..ae62d02 100644 --- a/packaging/glibc.spec +++ b/packaging/glibc.spec @@ -247,19 +247,41 @@ Results from running the glibc testsuites. Summary: glibc with ASan instrumentation License: LGPL-2.1+ and LGPL-2.1+-with-GCC-exception and GPL-2.0+ Requires: glibc = %{version} +Requires: rpm %description asan This package provides glibc with ASan instrumentation %post asan -ln -s --force /%{_lib}/libc-2.24-asan.so /%{_lib}/libc.so.6 +ln -s --force /%{_lib}/libc-%{version}-asan.so /%{_lib}/libc.so.6 %preun asan -ln -s --force /%{_lib}/libc-2.24.so /%{_lib}/libc.so.6 +ln -s --force /%{_lib}/libc-%{version}.so /%{_lib}/libc.so.6 %endif } +%{?hwasan: +%ifarch %hwasan_arch +%package hwasan +Summary: glibc with Hwasan instrumentation +License: LGPL-2.1+ and LGPL-2.1+-with-GCC-exception and GPL-2.0+ +Requires: glibc = %{version} +Requires: rpm + +%description hwasan +This package provides glibc with Hwasan instrumentation + +%post hwasan +ln -s --force /%{_lib}/libc-%{version}-hwasan.so /%{_lib}/libc.so.6 + +%preun hwasan +ln -s --force /%{_lib}/libc-%{version}.so /%{_lib}/libc.so.6 + +%endif +} + + %{?glibc_dump_configure: %package configure-dump Summary: GNU Glibc configure dump @@ -353,6 +375,7 @@ BuildFlags="$BuildFlags -U_LARGEFILE_SOURCE -U_LARGEFILE64_SOURCE -U_FILE_OFFSET conf_cflags="$BuildFlags -funwind-tables -fPIC" %{?asan: conf_cflags="$conf_cflags -DASAN_INIT_FIRST"} +%{?hwasan: conf_cflags="$conf_cflags -DASAN_INIT_FIRST -Wno-error=pragmas"} profile="--disable-profile" %{?build_profile:profile="--enable-profile"} %{?glibc_dump_configure:patch_configure.pl ..} @@ -431,6 +454,7 @@ unset LDFLAGS # Glibc must have both functions' implementations CPPFLAGS="-U_LARGEFILE_SOURCE -U_LARGEFILE64_SOURCE -U_FILE_OFFSET_BITS" %endif +CPPFLAGS+=" -DASAN_INIT_FIRST" # chmod +x $HERE/asan-glibc-gcc-wrapper.py # build static wrapper, because we don't want asan to intercept allocators @@ -449,7 +473,45 @@ make -r -j $J -C $GLIBC objdir=`pwd` subdir_lib # make -r -j $J -C $GLIBC objdir=`pwd` install_root=$ASAN_INST install-lib popd %endif - } +} + +%{?hwasan: +%ifarch %{hwasan_arch} +configure_flags="--prefix=/usr --without-cvs --without-selinux --enable-stackguard-randomization --enable-obsolete-rpc --disable-mathvec" +configure_flags="$configure_flags --disable-sanity-checks" + +HERE=%{_builddir}/%{name}-%{version} +J=${J:-32} +GLIBC=$HERE +ASAN_BUILD=$HERE/hwasan-build +unset CXXFLAGS +unset CFLAGS +unset LDFLAGS +%ifarch %arm aarch64 +# Glibc must have both functions' implementations +CPPFLAGS="-U_LARGEFILE_SOURCE -U_LARGEFILE64_SOURCE -U_FILE_OFFSET_BITS" +%endif +CPPFLAGS+=" -DASAN_INIT_FIRST" + +# chmod +x $HERE/asan-glibc-gcc-wrapper.py +# build static wrapper, because we don't want asan to intercept allocators +g++ -o gcc-wrapper gcc-wrapper.cc -O3 -funroll-loops -fno-unwind-tables -fno-asynchronous-unwind-tables -fno-exceptions -static -DHWASAN_BUILD + +rm -rf $ASAN_BUILD +mkdir -p $ASAN_BUILD +pushd $ASAN_BUILD +# Legacy python wrapper call was: +# CC=$HERE/asan-glibc-gcc-wrapper.py $GLIBC/configure $configure_flags +CPPFLAGS=$CPPFLAGS \ + CC=$HERE/gcc-wrapper $GLIBC/configure $configure_flags + +# Quick build - builds only libraries, but does not create symlinks. +make -r -j $J -C $GLIBC objdir=`pwd` subdir_lib +# make -r -j $J -C $GLIBC objdir=`pwd` install_root=$ASAN_INST install-lib +popd +%endif +} + %{?glibc_exit_on_make_finish: echo "Glibc build is complete and environment is ready, exiting" @@ -603,7 +665,16 @@ ln -s %{_libexecdir}/getconf/getconf %{buildroot}%{_bindir}/getconf HERE=%{_builddir}/%{name}-%{version} GLIBC=$HERE ASAN_BUILD=$HERE/asan-build -cp $ASAN_BUILD/libc.so %{buildroot}/%{_lib}/libc-2.24-asan.so +cp $ASAN_BUILD/libc.so %{buildroot}/%{_lib}/libc-%{version}-asan.so +%endif +} + +%{?hwasan: +%ifarch %{hwasan_arch} +HERE=%{_builddir}/%{name}-%{version} +GLIBC=$HERE +ASAN_BUILD=$HERE/hwasan-build +cp $ASAN_BUILD/libc.so %{buildroot}/%{_lib}/libc-%{version}-hwasan.so %endif } @@ -761,7 +832,15 @@ done %ifarch %{asan_arch} %files asan %manifest %{name}.manifest -/%{_lib}/libc-2.24-asan.so +/%{_lib}/libc-%{version}-asan.so +%endif +} + +%{?hwasan: +%ifarch %{hwasan_arch} +%files hwasan +%manifest %{name}.manifest +/%{_lib}/libc-%{version}-hwasan.so %endif } -- 2.7.4