Bug 25671 - Invalid IL code generated by System.Text.RegularExpressions
Summary: Invalid IL code generated by System.Text.RegularExpressions
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Miguel de Icaza [MSFT]
Depends on:
Reported: 2014-12-31 13:38 UTC by Craig Minihan
Modified: 2017-10-10 14:34 UTC (History)
4 users (show)

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

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 Craig Minihan 2014-12-31 13:38:59 UTC
I see the following error when running my app against master:

Invalid IL code in (wrapper dynamic-method) System.Text.RegularExpressions.CompiledRegexRunner:Go6 (System.Text.RegularExpressions.RegexRunner): IL_0419: dup

Stack Trace:
  at (wrapper managed-to-native) System.Delegate:CreateDelegate_internal (System.Type,object,System.Reflection.MethodInfo,bool)
  at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) [0x00000] in <filename unknown>:0
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in <filename unknown>:0
  at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0
  at System.Reflection.Emit.DynamicMethod.CreateDelegate (System.Type delegateType) [0x00000] in <filename unknown>:0
  at System.Text.RegularExpressions.CompiledRegexRunnerFactory.CreateInstance () [0x00000] in <filename unknown>:0
  at System.Text.RegularExpressions.Regex.Run (Boolean quick, Int32 prevlen, System.String input, Int32 beginning, Int32 length, Int32 startat) [0x00000] in <filename unknown>:0
  at System.Text.RegularExpressions.MatchCollection.GetMatch (Int32 i) [0x00000] in <filename unknown>:0
  at System.Text.RegularExpressions.MatchEnumerator.MoveNext () [0x00000] in <filename unknown>:0

The app runs without this error under 3.8/tar.
Comment 1 Marek Safar 2015-01-01 04:49:33 UTC
Do you have sample code we can run to reproduce the issue
Comment 2 Craig Minihan 2015-01-02 07:10:18 UTC
This is highly repeatable - when it wants to be - but not in test code yet. The invalid IL reported can be almost anything - nop, bre, etc. - so something is very broken here.

The obvious change is the switch from mcs/class/System/System.Text.RegularExpressions to the referencesource impl for regex handling.

The observed scenarios are:
* app starts and always fails on regex execution
* app starts, fails a few regexs and then seems to work
* app starts, fails a few regexs and then SIGSEGVs
Comment 3 Craig Minihan 2015-01-02 07:43:55 UTC
Looks like a threading issue to me. I reckon the MS code gets poked by the MS runtime before the first call, consider this working scenario:
1) start app
2) call regex for the first time on only one thread
3) call regex on any number of threads

and all is good, however:
1) start app
2) call regex on any number of threads - BANG!

100% repeatable.
Comment 4 Miguel de Icaza [MSFT] 2015-01-10 00:16:39 UTC

Can you share the code that causes this?

Alternatively, can you turn off the IL code generation?   In Mono's previous incarnation, that was a no-op, now it actually does something.
Comment 5 Marek Safar 2015-01-11 18:02:23 UTC
This is most likely cause by our SRE implementation not being thread-safe at all compared to .net SRE which is.
Comment 6 Miguel de Icaza [MSFT] 2015-01-12 23:23:41 UTC
I disabled the IL generator for now.
Comment 7 Egorbo 2017-09-30 01:13:23 UTC
I wonder if it's still an issue in mono 5.7 - do we have a repro?
Comment 8 Marek Safar 2017-10-10 14:34:00 UTC
I am closing the issue as we disabled the IL generator a long time ago. We are going to import CoreFX tests for RegularExpressions which hopefully will help us to reproduce the issue