qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] build qemu with gcc and tsan
@ 2024-08-14 17:11 Pierrick Bouvier
  2024-08-14 17:11 ` [PATCH 1/4] meson: hide tsan related warnings Pierrick Bouvier
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Pierrick Bouvier @ 2024-08-14 17:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: Marcelo Tosatti, kvm, Wainer dos Santos Moschetta, Thomas Huth,
	David Hildenbrand, Philippe Mathieu-Daudé, Ilya Leoshkevich,
	qemu-s390x, Paolo Bonzini, Alex Bennée, Beraldo Leal,
	Marc-André Lureau, Richard Henderson,
	Daniel P. Berrangé, Pierrick Bouvier

While working on a concurrency bug, I gave a try to tsan builds for QEMU. I
noticed it didn't build out of the box with recent gcc, so I fixed compilation.
In more, updated documentation to explain how to build a sanitized glib to avoid
false positives related to glib synchronisation primitives.

Pierrick Bouvier (4):
  meson: hide tsan related warnings
  target/i386: fix build warning (gcc-12 -fsanitize=thread)
  target/s390x: fix build warning (gcc-12 -fsanitize=thread)
  docs/devel: update tsan build documentation

 docs/devel/testing.rst       | 26 ++++++++++++++++++++++----
 meson.build                  | 10 +++++++++-
 target/i386/kvm/kvm.c        |  4 ++--
 target/s390x/tcg/translate.c |  1 -
 4 files changed, 33 insertions(+), 8 deletions(-)

-- 
2.39.2



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] meson: hide tsan related warnings
  2024-08-14 17:11 [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
@ 2024-08-14 17:11 ` Pierrick Bouvier
  2024-08-14 17:11 ` [PATCH 2/4] target/i386: fix build warning (gcc-12 -fsanitize=thread) Pierrick Bouvier
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Pierrick Bouvier @ 2024-08-14 17:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: Marcelo Tosatti, kvm, Wainer dos Santos Moschetta, Thomas Huth,
	David Hildenbrand, Philippe Mathieu-Daudé, Ilya Leoshkevich,
	qemu-s390x, Paolo Bonzini, Alex Bennée, Beraldo Leal,
	Marc-André Lureau, Richard Henderson,
	Daniel P. Berrangé, Pierrick Bouvier

When building with gcc-12 -fsanitize=thread, gcc reports some
constructions not supported with tsan.
Found on debian stable.

qemu/include/qemu/atomic.h:36:52: error: ‘atomic_thread_fence’ is not supported with ‘-fsanitize=thread’ [-Werror=tsan]
   36 | #define smp_mb()                     ({ barrier(); __atomic_thread_fence(__ATOMIC_SEQ_CST); })
      |                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
---
 meson.build | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/meson.build b/meson.build
index c2a050b8443..899660ef020 100644
--- a/meson.build
+++ b/meson.build
@@ -499,7 +499,15 @@ if get_option('tsan')
                          prefix: '#include <sanitizer/tsan_interface.h>')
     error('Cannot enable TSAN due to missing fiber annotation interface')
   endif
-  qemu_cflags = ['-fsanitize=thread'] + qemu_cflags
+  tsan_warn_suppress = []
+  # gcc (>=11) will report constructions not supported by tsan:
+  # "error: ‘atomic_thread_fence’ is not supported with ‘-fsanitize=thread’"
+  # https://gcc.gnu.org/gcc-11/changes.html
+  # However, clang does not support this warning and this triggers an error.
+  if cc.has_argument('-Wno-tsan')
+    tsan_warn_suppress = ['-Wno-tsan']
+  endif
+  qemu_cflags = ['-fsanitize=thread'] + tsan_warn_suppress + qemu_cflags
   qemu_ldflags = ['-fsanitize=thread'] + qemu_ldflags
 endif
 
-- 
2.39.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] target/i386: fix build warning (gcc-12 -fsanitize=thread)
  2024-08-14 17:11 [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
  2024-08-14 17:11 ` [PATCH 1/4] meson: hide tsan related warnings Pierrick Bouvier
@ 2024-08-14 17:11 ` Pierrick Bouvier
  2024-08-14 17:11 ` [PATCH 3/4] target/s390x: " Pierrick Bouvier
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Pierrick Bouvier @ 2024-08-14 17:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: Marcelo Tosatti, kvm, Wainer dos Santos Moschetta, Thomas Huth,
	David Hildenbrand, Philippe Mathieu-Daudé, Ilya Leoshkevich,
	qemu-s390x, Paolo Bonzini, Alex Bennée, Beraldo Leal,
	Marc-André Lureau, Richard Henderson,
	Daniel P. Berrangé, Pierrick Bouvier

Found on debian stable.

../target/i386/kvm/kvm.c: In function ‘kvm_handle_rdmsr’:
../target/i386/kvm/kvm.c:5345:1: error: control reaches end of non-void function [-Werror=return-type]
 5345 | }
      | ^
../target/i386/kvm/kvm.c: In function ‘kvm_handle_wrmsr’:
../target/i386/kvm/kvm.c:5364:1: error: control reaches end of non-void function [-Werror=return-type]
 5364 | }
---
 target/i386/kvm/kvm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 31f149c9902..ddec27edd5b 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -5770,7 +5770,7 @@ static int kvm_handle_rdmsr(X86CPU *cpu, struct kvm_run *run)
         }
     }
 
-    assert(false);
+    g_assert_not_reached();
 }
 
 static int kvm_handle_wrmsr(X86CPU *cpu, struct kvm_run *run)
@@ -5789,7 +5789,7 @@ static int kvm_handle_wrmsr(X86CPU *cpu, struct kvm_run *run)
         }
     }
 
-    assert(false);
+    g_assert_not_reached();
 }
 
 static bool has_sgx_provisioning;
-- 
2.39.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] target/s390x: fix build warning (gcc-12 -fsanitize=thread)
  2024-08-14 17:11 [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
  2024-08-14 17:11 ` [PATCH 1/4] meson: hide tsan related warnings Pierrick Bouvier
  2024-08-14 17:11 ` [PATCH 2/4] target/i386: fix build warning (gcc-12 -fsanitize=thread) Pierrick Bouvier
@ 2024-08-14 17:11 ` Pierrick Bouvier
  2024-08-14 17:11 ` [PATCH 4/4] docs/devel: update tsan build documentation Pierrick Bouvier
  2024-08-14 22:43 ` [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
  4 siblings, 0 replies; 6+ messages in thread
From: Pierrick Bouvier @ 2024-08-14 17:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: Marcelo Tosatti, kvm, Wainer dos Santos Moschetta, Thomas Huth,
	David Hildenbrand, Philippe Mathieu-Daudé, Ilya Leoshkevich,
	qemu-s390x, Paolo Bonzini, Alex Bennée, Beraldo Leal,
	Marc-André Lureau, Richard Henderson,
	Daniel P. Berrangé, Pierrick Bouvier

Found on debian stable.

../target/s390x/tcg/translate.c: In function ‘get_mem_index’:
../target/s390x/tcg/translate.c:398:1: error: control reaches end of non-void function [-Werror=return-type]
  398 | }
---
 target/s390x/tcg/translate.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/target/s390x/tcg/translate.c b/target/s390x/tcg/translate.c
index c81e035dea4..bcfff40b255 100644
--- a/target/s390x/tcg/translate.c
+++ b/target/s390x/tcg/translate.c
@@ -392,7 +392,6 @@ static int get_mem_index(DisasContext *s)
         return MMU_HOME_IDX;
     default:
         g_assert_not_reached();
-        break;
     }
 #endif
 }
-- 
2.39.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] docs/devel: update tsan build documentation
  2024-08-14 17:11 [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
                   ` (2 preceding siblings ...)
  2024-08-14 17:11 ` [PATCH 3/4] target/s390x: " Pierrick Bouvier
@ 2024-08-14 17:11 ` Pierrick Bouvier
  2024-08-14 22:43 ` [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
  4 siblings, 0 replies; 6+ messages in thread
From: Pierrick Bouvier @ 2024-08-14 17:11 UTC (permalink / raw)
  To: qemu-devel
  Cc: Marcelo Tosatti, kvm, Wainer dos Santos Moschetta, Thomas Huth,
	David Hildenbrand, Philippe Mathieu-Daudé, Ilya Leoshkevich,
	qemu-s390x, Paolo Bonzini, Alex Bennée, Beraldo Leal,
	Marc-André Lureau, Richard Henderson,
	Daniel P. Berrangé, Pierrick Bouvier

Mention it's now possible to build with gcc, instead of clang, and
explain how to build a sanitized glib version.
---
 docs/devel/testing.rst | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/docs/devel/testing.rst b/docs/devel/testing.rst
index af73d3d64fb..f10cfc3f786 100644
--- a/docs/devel/testing.rst
+++ b/docs/devel/testing.rst
@@ -634,20 +634,38 @@ Building and Testing with TSan
 It is possible to build and test with TSan, with a few additional steps.
 These steps are normally done automatically in the docker.
 
-There is a one time patch needed in clang-9 or clang-10 at this time:
+TSan is supported for clang and gcc.
+One particularity of sanitizers is that all the code, including shared objects
+dependencies, should be built with it.
+In the case of TSan, any synchronization primitive from glib (GMutex for
+instance) will not be recognized, and will lead to false positives.
+
+To build a tsan version of glib:
 
 .. code::
 
-  sed -i 's/^const/static const/g' \
-      /usr/lib/llvm-10/lib/clang/10.0.0/include/sanitizer/tsan_interface.h
+   $ git clone --depth=1 --branch=2.81.0 https://github.com/GNOME/glib.git
+   $ cd glib
+   $ CFLAGS="-O2 -g -fsanitize=thread" meson build
+   $ ninja -C build
 
 To configure the build for TSan:
 
 .. code::
 
-  ../configure --enable-tsan --cc=clang-10 --cxx=clang++-10 \
+  ../configure --enable-tsan \
                --disable-werror --extra-cflags="-O0"
 
+When executing qemu, don't forget to point to tsan glib:
+
+.. code::
+
+   $ glib_dir=/path/to/glib
+   $ export LD_LIBRARY_PATH=$glib_dir/build/gio:$glib_dir/build/glib:$glib_dir/build/gmodule:$glib_dir/build/gobject:$glib_dir/build/gthread
+   # check correct version is used
+   $ ldd build/qemu-x86_64 | grep glib
+   $ qemu-system-x86_64 ...
+
 The runtime behavior of TSAN is controlled by the TSAN_OPTIONS environment
 variable.
 
-- 
2.39.2



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/4] build qemu with gcc and tsan
  2024-08-14 17:11 [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
                   ` (3 preceding siblings ...)
  2024-08-14 17:11 ` [PATCH 4/4] docs/devel: update tsan build documentation Pierrick Bouvier
@ 2024-08-14 22:43 ` Pierrick Bouvier
  4 siblings, 0 replies; 6+ messages in thread
From: Pierrick Bouvier @ 2024-08-14 22:43 UTC (permalink / raw)
  To: qemu-devel
  Cc: Marcelo Tosatti, kvm, Wainer dos Santos Moschetta, Thomas Huth,
	David Hildenbrand, Philippe Mathieu-Daudé, Ilya Leoshkevich,
	qemu-s390x, Paolo Bonzini, Alex Bennée, Beraldo Leal,
	Marc-André Lureau, Richard Henderson,
	Daniel P. Berrangé

Sent v2 (forgot to signoff commits).

On 8/14/24 10:11, Pierrick Bouvier wrote:
> While working on a concurrency bug, I gave a try to tsan builds for QEMU. I
> noticed it didn't build out of the box with recent gcc, so I fixed compilation.
> In more, updated documentation to explain how to build a sanitized glib to avoid
> false positives related to glib synchronisation primitives.
> 
> Pierrick Bouvier (4):
>    meson: hide tsan related warnings
>    target/i386: fix build warning (gcc-12 -fsanitize=thread)
>    target/s390x: fix build warning (gcc-12 -fsanitize=thread)
>    docs/devel: update tsan build documentation
> 
>   docs/devel/testing.rst       | 26 ++++++++++++++++++++++----
>   meson.build                  | 10 +++++++++-
>   target/i386/kvm/kvm.c        |  4 ++--
>   target/s390x/tcg/translate.c |  1 -
>   4 files changed, 33 insertions(+), 8 deletions(-)
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-08-14 22:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-14 17:11 [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier
2024-08-14 17:11 ` [PATCH 1/4] meson: hide tsan related warnings Pierrick Bouvier
2024-08-14 17:11 ` [PATCH 2/4] target/i386: fix build warning (gcc-12 -fsanitize=thread) Pierrick Bouvier
2024-08-14 17:11 ` [PATCH 3/4] target/s390x: " Pierrick Bouvier
2024-08-14 17:11 ` [PATCH 4/4] docs/devel: update tsan build documentation Pierrick Bouvier
2024-08-14 22:43 ` [PATCH 0/4] build qemu with gcc and tsan Pierrick Bouvier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).