Bug 58422 - Race conditions of memfuncs.c (mono_gc_memmove_aligned)
Summary: Race conditions of memfuncs.c (mono_gc_memmove_aligned)
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Linux
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2017-07-26 21:57 UTC by Armin
Modified: 2017-10-11 17:54 UTC (History)
4 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:

Stack traces (9.52 KB, text/plain)
2017-07-26 21:57 UTC, Armin

Description Armin 2017-07-26 21:57:49 UTC
Created attachment 23828 [details]
Stack traces

According to TSan, `mono_gc_memmove_aligned ()` contains a few races and I am not sure if they are harmless. I found them using commit 5d9d139e0ac4ecbf02c70cbce5d9a0c65a43faa4. I marked the reported lines with "!" and attached TSan's stack traces.

#define MEMMOVE_WORDS_UPWARD(dest,src,words) do {	\
		void * volatile *__d = (void* volatile*)(dest);		\
		void **__s = (void**)(src);		\
		int __n = (int)(words);			\
		int __i;				\
		for (__i = 0; __i < __n; ++__i)		\
			__d [__i] = __s [__i];		\
	} while (0)

void mono_gc_memmove_aligned (void *dest, const void *src, size_t size) {
  // ...
  volatile char *d = (char*)dest;
  const char *s = (const char*)src;
  size_t tail_bytes;
  // ...
! MEMMOVE_WORDS_UPWARD (d, s, bytes_to_words (align_down (size)));
  // ...
  if (tail_bytes) {
    d += (size_t)align_down (size);
    s += (size_t)align_down (size);
    do {
!     *d++ = *s++;
    } while (--tail_bytes);
  // ...

The first race occurs within `MEMMOVE_WORDS_UPWARD` ... two different worker threads work with the same `dest` and `src` memory locations (probably simultaneously?). The second race is similar - but the reported read happens in `*d++ = *s++`. As far as I can tell, there is no direct synchronisation between the two worker threads.
Comment 1 Ludovic Henry 2017-07-28 17:47:27 UTC
Armin, from the stacktraces, it seems it's reading the memory location from 2 different threads, but that might be perfectly fine as it seems it's only reading. Also, all these calls comes from managed method Array.FastCopy, so the race might actually be in managed code.

So could you please check if these calls are only reading, and is there any way to know which managed method called Array.FastCopy? Thank you
Comment 2 Rodrigo Kumpera 2017-10-11 17:54:35 UTC
Those are benign races. Those functions are designed to work fine in face of very racy user code.

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Note You need to log in before you can comment on or make changes to this bug.