Bug 58423 - Race condition of metadata.c (mono_metadata_get_canonical_generic_inst)
Summary: Race condition of metadata.c (mono_metadata_get_canonical_generic_inst)
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Armin
URL:
Depends on:
Blocks:
 
Reported: 2017-07-26 22:51 UTC by Armin
Modified: 2017-09-07 12:38 UTC (History)
4 users (show)

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


Attachments
Stack trace (23.28 KB, text/plain)
2017-07-26 22:51 UTC, Armin
Details

Description Armin 2017-07-26 22:51:02 UTC
Created attachment 23830 [details]
Stack trace

If I understand this TSan report correctly, different threads execute `++next_generic_inst_id` (3 ops) "without" mutexes. Yes, there are mutexes but, as it seems, different ones are used. Shouldn't a global lock be used here instead of `mono_image_set_lock (set)` which is dependent on `set`?

I found this with commit 5d9d139e0ac4ecbf02c70cbce5d9a0c65a43faa4, highlighted the affected row with "!" and attached the corresponding stack trace with all the details.

static int next_generic_inst_id = 0;

MonoGenericInst * mono_metadata_get_canonical_generic_inst (MonoGenericInst *candidate) {
  // ...
  mono_image_set_lock (set);
  // ...
#ifndef MONO_SMALL_CONFIG
! ginst->id = ++next_generic_inst_id;
#endif
  // ...
  mono_image_set_unlock (set);
  // ...
}
Comment 1 Ludovic Henry 2017-07-28 17:42:30 UTC
Aleksey, does it matter if this `next_generic_inst_id` is not automatically incremented? If it does matter, should we replace `++next_generic_inst_id` with `InterlockedIncrement(&next_generic_inst_id)`?
Comment 2 Aleksey Kliger 2017-07-28 18:08:07 UTC
It looks to me like the id field in a MonoGenericInst is used in mono_generic_inst_equal_full as a way to short-circuit comparisons.

So yea in principle you could end up with two instantiations of the same class that are instantiated into two separate image sets at the same time but end up with the same id.  This will later cause mono_metadata_type_equal to return the wrong answer.
Comment 3 Armin 2017-07-29 11:59:01 UTC
Should I add `InterlockedIncrement` with a quick PR?
Comment 4 Ludovic Henry 2017-07-31 14:22:07 UTC
Armin, a fix for that wouldn't hurt IMO, so feel free to submit a PR, and add Aleksey and I as reviewers please. Thank you.
Comment 5 Ludovic Henry 2017-09-07 12:38:31 UTC
Fixed with 47535f557d18cf771436ac55578dfb165da0e399

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.