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 ()
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)

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

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

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.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

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.