[TSan] Add tsan-force-options, tsan-runtime-env. 77/156677/5
authorDenis Khalikov <d.khalikov@partner.samsung.com>
Thu, 19 Oct 2017 09:25:31 +0000 (12:25 +0300)
committerDenis Khalikov <d.khalikov@partner.samsung.com>
Wed, 29 Nov 2017 11:05:40 +0000 (14:05 +0300)
For this moment tsan-runtime-env works without
preloading libtsan, because of huge memory consumption
by libtsan, at least 1:4. So, adding libtsan to ld.so.preload
will require a huge VM space available, because libtsan
has interceptors for allocators as well as libasan.

In this case I see only one way to enable TSan for DSO:

Build an executable with TSan, which requires DSO we want
to execute. In this case compiler will add section .preinit with
TSan initialization and rltd will call this section early than .init
section for other DSO.

Change-Id: I0cccb8c447287731a7c2b9f09f84a4a490d52d22

packaging/TSAN_OPTIONS [new file with mode: 0644]
packaging/gcc-contrib.spec

diff --git a/packaging/TSAN_OPTIONS b/packaging/TSAN_OPTIONS
new file mode 100644 (file)
index 0000000..324d142
--- /dev/null
@@ -0,0 +1 @@
+print_cmdline=true:log_path=/tmp/tsan.log:detect_deadlocks=1:second_deadlock_stack=1
index c24b556..756be11 100644 (file)
 %define ubsan_force_options -fsanitize=undefined,bounds-strict,float-divide-by-zero,float-cast-overflow
 %define lsan_force_options -fsanitize=leak -marm -Wa,-mimplicit-it=arm -fno-omit-frame-pointer -Wp,-U_FORTIFY_SOURCE -Wl,--as-needed -ldl -lpthread
 %define isan_force_options -fsanitize=unsigned-integer-overflow
-
+%define tsan_force_options -fsanitize=thread -fno-omit-frame-pointer -Wp,-U_FORTIFY_SOURCE -Wl,--as-needed -ldl -lpthread
 %define asan_runtime_options halt_on_error=false:start_deactivated=true:print_cmdline=true:quarantine_size_mb=1:detect_leaks=0:log_path=/tmp/asan.log:log_exe_name=1
 %define ubsan_runtime_options print_cmdline=true:log_path=/tmp/ubsan.log
 %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
 
 Name:         gcc-contrib%{?cross:-%{cross}}
 # With generated files in src we could drop the following
@@ -64,6 +65,7 @@ Source21: UBSAN_OPTIONS
 Source22: macros.ubsan
 Source23: extract-ubsan-logs
 Source24: lsan.supp
+Source25: TSAN_OPTIONS
 Group:         Development/Building
 Summary:       GCC related scripts
 License:       MIT 
@@ -224,6 +226,53 @@ chmod a-w %{libsubdir}
 /usr/bin/gcc-unforce-options
 [ -d /emul/ ] && chmod a-w /emul/usr/bin/
 
+%package -n tsan-force-options
+Summary:    Scripts to enable automatic package sanitization
+Group:      System Environment
+Requires:   gcc
+Requires:   libtsan
+
+%description -n tsan-force-options
+Scripts for TSan instrumentation
+
+%post -n tsan-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 %tsan_force_options
+# Add TSan-related macros
+cat >> /usr/lib/rpm/tizen_macros << EOF
+
+%%tsan_force_options %{tsan_force_options}
+%%gcc_unforce_options /usr/bin/gcc-unforce-options
+%%gcc_force_options /usr/bin/gcc-force-options
+%%gcc_force_default_options %%gcc_force_options %%tsan_force_options
+EOF
+
+%preun -n tsan-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
+[ -d /emul/ ] && chmod a-w /emul/usr/bin/
+
+%package -n tsan-runtime-env
+Summary:    TSan runtime environment
+Group:      Development/Libraries
+Requires:   libtsan
+Requires(post): smack
+
+%description -n tsan-runtime-env
+TSan runtime environment
+
+%post -n tsan-runtime-env
+echo "%{tsan_runtime_options}" > /TSAN_OPTIONS
+chsmack -a "_" /TSAN_OPTIONS
+
 %package -n asan-build-env
 Summary:    Asan build environment
 Group:      Development/Libraries
@@ -338,6 +387,11 @@ chmod 644 %{buildroot}/LSAN_OPTIONS
 chmod 644 %{buildroot}/lsan.supp
 %endif
 
+%ifarch %tsan_arch
+cp %{SOURCE25} %{buildroot}
+chmod 644 %{buildroot}/TSAN_OPTIONS
+%endif
+
 mkdir -p %{buildroot}/%{_rpmconfigdir}/tizen/
 install -m 0644 %{SOURCE21} %{buildroot}/
 install -m 0644 %{SOURCE22} %{buildroot}/%{_rpmconfigdir}/
@@ -369,6 +423,13 @@ install -m 0755 %{SOURCE23} %{buildroot}/%{_rpmconfigdir}/tizen/
 %{_prefix}/bin/gcc-unforce-options
 %endif
 
+%ifarch %tsan_arch
+%files -n tsan-force-options
+%defattr(-,root,root,-)
+%{_prefix}/bin/gcc-force-options
+%{_prefix}/bin/gcc-unforce-options
+%endif
+
 %files -n asan-build-env
 %defattr(-,root,root,-)
 /ASAN_OPTIONS
@@ -383,6 +444,12 @@ install -m 0755 %{SOURCE23} %{buildroot}/%{_rpmconfigdir}/tizen/
 /lsan.supp
 %endif
 
+%ifarch %tsan_arch
+%files -n tsan-runtime-env
+%defattr(-,root,root,-)
+/TSAN_OPTIONS
+%endif
+
 %ifarch %ubsan_arch
 %files -n ubsan-build-env
 %defattr(-,root,root,-)