[mempool] Annotate functions for clang's ThreadSanitizer (#5191)
authorArmin Hasitzka <cherusker@users.noreply.github.com>
Fri, 21 Jul 2017 09:39:04 +0000 (11:39 +0200)
committerBernhard Urban <bernhard.urban@xamarin.com>
Fri, 21 Jul 2017 09:39:04 +0000 (11:39 +0200)
* [mempool] Annotate functions for ThreadSanitizer
Whitelist `mono_mempool_alloc`, `mono_mempool_new_size` and `mono_mempool_destroy` according to https://bugzilla.xamarin.com/show_bug.cgi?id=57936

* [fixup!] Test the NO_SANITIZE_THREAD macro

* [fixup!] Merge comments / documentation

mono/metadata/mempool.c
mono/utils/mono-compiler.h

index e6fe9b25d0ad188eb61a7be7be1dcac149ee548b..3d86989db70470a6bfbcb0c3011ab85f73a31193 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "mempool.h"
 #include "mempool-internals.h"
+#include "utils/mono-compiler.h"
 
 /*
  * MonoMemPool is for fast allocation of memory. We free
@@ -92,9 +93,19 @@ mono_mempool_new (void)
 
 /**
  * mono_mempool_new_size:
+ *
+ *   clang's ThreadSanitizer detects races of total_bytes_allocated and pool->d.allocated throughout the functions
+ *     * mono_mempool_alloc
+ *     * mono_mempool_new_size
+ *     * mono_mempool_destroy
+ *   while these races could lead to wrong values, total_bytes_allocated is just used for debugging / reporting and since
+ *   the mempool.c functions are called quite often, a discussion led the the conclusion of ignoring these races:
+ *   https://bugzilla.xamarin.com/show_bug.cgi?id=57936
+ *
  * \param initial_size the amount of memory to initially reserve for the memory pool.
  * \returns a new memory pool with a specific initial memory reservation.
  */
+MONO_NO_SANITIZE_THREAD
 MonoMemPool *
 mono_mempool_new_size (int initial_size)
 {
@@ -120,10 +131,20 @@ mono_mempool_new_size (int initial_size)
 
 /**
  * mono_mempool_destroy:
+ *
+ *   clang's ThreadSanitizer detects races of total_bytes_allocated and pool->d.allocated throughout the functions
+ *     * mono_mempool_alloc
+ *     * mono_mempool_new_size
+ *     * mono_mempool_destroy
+ *   while these races could lead to wrong values, total_bytes_allocated is just used for debugging / reporting and since
+ *   the mempool.c functions are called quite often, a discussion led the the conclusion of ignoring these races:
+ *   https://bugzilla.xamarin.com/show_bug.cgi?id=57936
+ *
  * \param pool the memory pool to destroy
  *
  * Free all memory associated with this pool.
  */
+MONO_NO_SANITIZE_THREAD
 void
 mono_mempool_destroy (MonoMemPool *pool)
 {
@@ -251,6 +272,15 @@ get_next_size (MonoMemPool *pool, int size)
 
 /**
  * mono_mempool_alloc:
+ *
+ *   clang's ThreadSanitizer detects races of total_bytes_allocated and pool->d.allocated throughout the functions
+ *     * mono_mempool_alloc
+ *     * mono_mempool_new_size
+ *     * mono_mempool_destroy
+ *   while these races could lead to wrong values, total_bytes_allocated is just used for debugging / reporting and since
+ *   the mempool.c functions are called quite often, a discussion led the the conclusion of ignoring these races:
+ *   https://bugzilla.xamarin.com/show_bug.cgi?id=57936
+ *
  * \param pool the memory pool to use
  * \param size size of the memory block
  *
@@ -258,6 +288,7 @@ get_next_size (MonoMemPool *pool, int size)
  *
  * \returns the address of a newly allocated memory block.
  */
+MONO_NO_SANITIZE_THREAD
 gpointer
 mono_mempool_alloc (MonoMemPool *pool, guint size)
 {
index 57fa81aebafb142b19689541f166e661f6d53d59..f4409e384c5166d5a3c753439734a6a4f6e771a9 100644 (file)
@@ -113,5 +113,16 @@ typedef SSIZE_T ssize_t;
 #define MONO_GNUC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
 #endif
 
+/* Used to tell clang's ThreadSanitizer to not report data races that occur within a certain function */
+#if defined(__has_feature)
+#if __has_feature(thread_sanitizer)
+#define MONO_NO_SANITIZE_THREAD __attribute__ ((no_sanitize("thread")))
+#else
+#define MONO_NO_SANITIZE_THREAD
+#endif
+#else
+#define MONO_NO_SANITIZE_THREAD
+#endif
+
 #endif /* __UTILS_MONO_COMPILER_H__*/