Bug 58728 - [Found Problem] Native crashes when trying to single-step over async await
Summary: [Found Problem] Native crashes when trying to single-step over async await
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Debugger ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2017-08-11 19:00 UTC by Martin Baulig
Modified: 2017-08-31 11:43 UTC (History)
4 users (show)

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


Attachments
Broken (616.21 KB, text/plain)
2017-08-11 19:01 UTC, Martin Baulig
Details
LLDB (2.38 KB, text/plain)
2017-08-11 19:01 UTC, Martin Baulig
Details


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:
Status:
RESOLVED FIXED

Comment 1 Martin Baulig 2017-08-11 19:01:10 UTC
Created attachment 24151 [details]
Broken
Comment 2 Martin Baulig 2017-08-11 19:01:33 UTC
Created attachment 24152 [details]
LLDB
Comment 3 Martin Baulig 2017-08-11 19:05:25 UTC
I spent some time playing around with lldb and when I set a breakpoint at the beginning of

System.Runtime.CompilerServices.AsyncMethodBuilderCore/MoveNextRunner:InvokeMoveNext

and when I use "step over" in the IDE, register rdx is already messed up - rdx is what later ends in r14.
Comment 4 Martin Baulig 2017-08-11 19:08:09 UTC
Using Mono from
https://github.com/baulig/mono/tree/work-bug-58728

My test case is

====
		static void TestProxy ()
		{
			var credentials = new NetworkCredential ("test", "m0nkey-", "LAST-HOPE");
			var proxy = new WebProxy ("http://last-hope.baulig.net:3128");
			proxy.Credentials = credentials;

			var uri = "https://tlstest-12.xamdev.com/";
			var request = (HttpWebRequest)WebRequest.Create (uri);
			request.Proxy = proxy;

			var response = (HttpWebResponse)request.GetResponse ();
			Console.WriteLine ($"GOT RESPONSE: {response.StatusCode}");
		}
====

Set breakpoints in Xamarin Studio in mcs/class/System/System.Net/WebConnectionTunnel.cs, at the beginning of the Initialize() method (line 112) and on the line before the "await stream.WriteAsync (connectBytes, 0, connectBytes.Length, cancellationToken).ConfigureAwait (false)" (line 168).

Hit continue in the IDE after hitting the breakpoint on line 168 and it's working fine.  Hit step over and it's crashing.

This requires a server in my azure vm, unfortunately.  I'll see whether I can get a test case without that.
Comment 5 Martin Baulig 2017-08-11 19:23:33 UTC
Ok, you can actually use this:

=====
		static void TestProxy2 ()
		{
			var socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
			socket.Bind (new IPEndPoint (IPAddress.Loopback, 8888)); 
			socket.Listen (5);

			ThreadPool.QueueUserWorkItem (_ => {
				var accepted = socket.Accept ();
				var reader = new StreamReader (new NetworkStream (accepted));
				reader.ReadLine ();
			});

			var proxy = new WebProxy ("http://127.0.0.1:8888/");
			var uri = "https://tlstest-12.xamdev.com/";
			var request = (HttpWebRequest)WebRequest.Create (uri);
			request.Proxy = proxy;

			var response = (HttpWebResponse)request.GetResponse ();
			Console.WriteLine ($"GOT RESPONSE: {response.StatusCode}");
		}
=====

This won't require an external server.

Use the Mono from my branch, set those two breakpoints in WebConnectionTunnel.cs and try to step over the stream.WriteAsync().

https://github.com/baulig/mono/blob/work-bug-58728/mcs/class/System/System.Net/WebConnectionTunnel.cs#L169
Comment 6 Martin Baulig 2017-08-18 17:03:15 UTC
I believe the problem is in get_this():
https://github.com/mono/mono/blob/master/mono/mini/debugger-agent.c#L4753.

If I see this correctly, then the `addr` won't point to a `MonoObject *` if we're inside a valuetype's instance method.

Then get_this_async_id() will call get_objref() on the returned pointer (which is not a MonoObject *), which calls mono_object_hash().

We won't crash there immediately, but when mono_object_hash() tried to modify the `synchronization` field, it will overwrite random memory.  This causes obscure random crashes later on.
Comment 7 Marek Safar 2017-08-24 17:09:35 UTC
David, Zoltan any action on this? This looks like serious issue I'd like to get fixed for dev 15.5
Comment 8 Zoltan Varga 2017-08-24 20:35:02 UTC
https://github.com/mono/mono/pull/5442
Comment 9 Zoltan Varga 2017-08-25 01:47:17 UTC
It turns out roslyn generates different code for async state machines in optimized mode. Instead of being objects, they are valuetypes, which the current async code in debugger-agent.c can't handle.
Comment 10 Marek Safar 2017-08-25 08:08:44 UTC
mcs does it for both debug and release as well
Comment 11 Zoltan Varga 2017-08-31 11:43:27 UTC
Should be fixed by mono master 19046502886ad7697b4c6642337441a7fdafd28a.