Add support for HWASan force options and environment 76/260176/2 accepted/tizen/base/20210627.210234 submit/tizen_base/20210622.075322
authorAndrey Kazmin <a.kazmin@partner.samsung.com>
Fri, 5 Mar 2021 11:03:11 +0000 (14:03 +0300)
committerDongkyun Son <dongkyun.s@samsung.com>
Mon, 21 Jun 2021 12:00:30 +0000 (12:00 +0000)
Change-Id: Ib1775eeffd4f7a7760a8816d0b339e547b1976a6
Signed-off-by: Andrey Kazmin <a.kazmin@partner.samsung.com>
packaging/gcc-contrib.spec
packaging/hwasan_stub.c [new file with mode: 0644]

index eea78ac..9dc0d0e 100644 (file)
@@ -9,6 +9,7 @@
 %define quadmath_arch %ix86 x86_64 ia64
 %define tsan_arch x86_64 aarch64
 %define asan_arch x86_64 %ix86 ppc ppc64 %sparc %arm aarch64
+%define hwasan_arch aarch64
 %define itm_arch x86_64 %ix86 %arm ppc ppc64 ppc64le s390 s390x %sparc aarch64
 %define atomic_arch x86_64 %ix86 %arm aarch64 ppc ppc64 ppc64le s390 s390x %sparc m68k
 %define lsan_arch x86_64 %ix86 armv7l aarch64
@@ -52,6 +53,9 @@
 %define lsan_runtime_options print_cmdline=true:detect_leaks=1:log_path=/tmp/lsan.log:log_exe_name=1:fast_unwind_on_malloc=false:malloc_context_size=5:suppressions=/lsan.supp:print_suppressions=false
 %define tsan_runtime_options print_cmdline=true:log_path=/tmp/tsan.log:detect_deadlocks=1:second_deadlock_stack=1
 
+%define hwasan_force_options -fsanitize=hwaddress -fsanitize-recover=hwaddress -Wp,-U_FORTIFY_SOURCE -Wl,--as-needed -ldl -lpthread
+%define hwasan_runtime_options halt_on_error=false:print_cmdline=true:handle_segv=0:log_path=/tmp/hwasan.log:log_exe_name=1
+
 Name:         gcc-contrib%{?cross:-%{cross}}
 # With generated files in src we could drop the following
 ## until here, but at least renaming and patching info files breaks this
@@ -74,6 +78,7 @@ Source25: extract-gcc-warning-logs
 Source26: macros.gcov
 Source27: extract-gcov-info
 Source28: asansictl
+Source29: hwasan_stub.c
 Group:         Development/Building
 Summary:       GCC related scripts
 License:       MIT
@@ -129,6 +134,56 @@ sed '/abuild/d' -i /etc/security/limits.conf
 # Remove additional macros from config
 sed -e '\|^\# /etc/rpm/macros.asan$|,\|^\# /etc/rpm/macros.asan ends here|d' -i /usr/lib/rpm/tizen_macros
 
+%package -n libhwasan-stub
+Summary:    HWASan stub library to enable cross build
+Group:      System Environment
+Requires:   rpm
+
+%description -n libhwasan-stub
+HWASan stub library for cross build
+
+%package -n hwasan-force-options
+Summary:    Scripts to enable automatic package sanitization
+Group:      System Environment
+Requires:   gcc rpm
+Requires:   libhwasan
+
+%description -n hwasan-force-options
+Scripts for HWASan instrumentation
+
+%post -n hwasan-force-options
+# Change mode to allow all users to run gcc-force/unforce-options
+chmod a+w /usr/bin
+chmod a+w %{libsubdir}
+[ -d /emul/ ] && chmod a+w /emul/usr/bin/ && chmod a+w /emul/home/abuild/rpmbuild/BUILD/gcc-*/obj/gcc/ \
+              && find -L /emul/usr/*/gcc -name 'collect2' | xargs dirname | xargs chmod a+w
+/usr/bin/gcc-force-options %hwasan_force_options -fno-common
+# Add ASan-related macros
+cat >> /usr/lib/rpm/tizen_macros << EOF
+
+%%hwasan_force_options %{hwasan_force_options}
+%%gcc_unforce_options %%{lua: rpm.define("hwasan_unforced 1"); print("/usr/bin/gcc-unforce-options")}
+%%gcc_force_options /usr/bin/gcc-force-options
+%%gcc_force_default_options %%gcc_force_options %%hwasan_force_options -fno-common
+%%restore_fcommon \\
+  %%gcc_unforce_options \\
+  %%gcc_force_options %%hwasan_force_options -fcommon
+EOF
+
+# HWASan needs a whole shadow address space
+# Using ulimit can only set the value for current user so we need to set /etc/security/limits.conf directly.
+sed '/End of file/i\abuild\tsoft\tas\t-1\nabuild\thard\tas\t-1' -i /etc/security/limits.conf
+
+%preun -n hwasan-force-options
+# Restore read-only mode
+chmod a-w /usr/bin
+chmod a-w %{libsubdir}
+[ -d /emul/ ] && chmod a-w /emul/usr/bin/ && chmod a-w /emul/home/abuild/rpmbuild/BUILD/gcc-*/obj/gcc/ \
+              && find -L /emul/usr/*/gcc -name 'collect2' | xargs dirname | xargs chmod a-w
+/usr/bin/gcc-unforce-options
+# Restore limit configurations set for HWASan
+sed '/abuild/d' -i /etc/security/limits.conf
+
 %package -n ubsan-force-options
 Summary:    Scripts to enable automatic package sanitization
 Group:      System Environment
@@ -335,6 +390,14 @@ echo "libasan.so" > /etc/ld.so.preload
 # Restore /etc/ld.so.preload
 [ -f /etc/ld.so.preload.orig ] && mv -v /etc/ld.so.preload.orig /etc/ld.so.preload
 
+%package -n hwasan-build-env
+Summary:    HWAsan build environment
+Group:      Development/Libraries
+Requires:   libhwasan
+
+%description -n hwasan-build-env
+HWAsan build environment
+
 %package -n lsan-runtime-env
 Summary:    LSan runtime environment
 Group:      Development/Libraries
@@ -384,6 +447,14 @@ Requires:   asan-runtime-env
 %description -n asansi-runtime-env
 Tool for switching interceptors state of libasan
 
+%package -n hwasan-runtime-env
+Summary:    HWAsan runtime environment for target device
+Group:      Development/Libraries
+Requires:   libhwasan
+
+%description -n hwasan-runtime-env
+HWAsan runtime environment
+
 %package -n ubsan-runtime-env
 Summary:    UBSan runtime environment for target device
 Group:      Development/Libraries
@@ -457,15 +528,24 @@ fi
 
 %prep
 
+%ifarch x86_64
+cp %{SOURCE29} .
+%endif
+
 %build
 # Generate runtime options for sanitizers
-echo "%{asan_runtime_options}"  | tee ASAN_OPTIONS
+echo "%{asan_runtime_options}"   | tee ASAN_OPTIONS
+echo "%{hwasan_runtime_options}" | tee HWASAN_OPTIONS
 echo "%{lsan_runtime_options}"  | tee LSAN_OPTIONS
 echo "%{tsan_runtime_options}"  | tee TSAN_OPTIONS
 echo "%{ubsan_runtime_options}" | tee UBSAN_OPTIONS
 
 echo "%{libdir}/libasan.so" | tee asan-runtime-ld.so.preload
 
+%ifarch x86_64
+gcc -shared -o libhwasan.so -fPIC hwasan_stub.c
+%endif
+
 %install
 mkdir -p %buildroot%_prefix
 mkdir -p %buildroot%_prefix/bin
@@ -479,6 +559,15 @@ install -m 0755 %{SOURCE17} %{buildroot}%{_prefix}/bin/
 install -m 0644 ASAN_OPTIONS %{buildroot}
 install -m 0644 asan-runtime-ld.so.preload %{buildroot}/etc/ld.so.preload
 
+%ifarch x86_64
+mkdir -p %{buildroot}/%{_libdir}
+install -m 755 libhwasan.so %{buildroot}/%{_libdir}
+%endif
+
+%ifarch %hwasan_arch
+install -m 0644 HWASAN_OPTIONS %{buildroot}
+%endif
+
 %ifarch %lsan_arch
 install -m 0644 LSAN_OPTIONS %{buildroot}
 install -m 0644 %{SOURCE21} %{buildroot}
@@ -513,6 +602,13 @@ install -m 0755 %{SOURCE28} %{buildroot}%{_prefix}/sbin/
 %{_prefix}/bin/gcc-unforce-options
 %{_rpmconfigdir}/macros.asan
 
+%ifarch %hwasan_arch
+%files -n hwasan-force-options
+%defattr(-,root,root,-)
+%{_prefix}/bin/gcc-force-options
+%{_prefix}/bin/gcc-unforce-options
+%endif
+
 %files -n ubsan-force-options
 %defattr(-,root,root,-)
 %{_prefix}/bin/gcc-force-options
@@ -551,6 +647,22 @@ install -m 0755 %{SOURCE28} %{buildroot}%{_prefix}/sbin/
 %defattr(-,root,root,-)
 %{_prefix}/sbin/asansictl
 
+%ifarch %hwasan_arch
+%files -n hwasan-build-env
+%defattr(-,root,root,-)
+/HWASAN_OPTIONS
+
+%files -n hwasan-runtime-env
+%defattr(-,root,root,-)
+/HWASAN_OPTIONS
+%endif
+
+%ifarch x86_64
+%files -n libhwasan-stub
+%defattr(-,root,root,-)
+%{libdir}/libhwasan.so
+%endif
+
 %ifarch %lsan_arch
 %files -n lsan-runtime-env
 %defattr(-,root,root,-)
diff --git a/packaging/hwasan_stub.c b/packaging/hwasan_stub.c
new file mode 100644 (file)
index 0000000..758ab46
--- /dev/null
@@ -0,0 +1,4 @@
+void __attribute__ ((constructor)) init(void)
+{
+  return;
+}