Bug 22183 - UdpClient.JoinMulticastGroup (IPAddress) throws "SIGSEGV while executing native code" when deployed in release mode
Summary: UdpClient.JoinMulticastGroup (IPAddress) throws "SIGSEGV while executing nati...
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.16.0
Hardware: PC Windows
: Normal normal
Target Milestone: 5.1
Assignee: Radek Doulik
URL:
Depends on:
Blocks:
 
Reported: 2014-08-18 18:27 UTC by Kent Green [MSFT]
Modified: 2017-11-14 17:02 UTC (History)
5 users (show)

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


Attachments
Sample that demonstrates the bug (16.26 KB, application/x-zip-compressed)
2014-08-19 13:46 UTC, Kent Green [MSFT]
Details
Sample to demostrate it on linux (3.74 KB, application/x-zip-compressed)
2017-11-14 16:03 UTC, Michal Dobrodenka
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 Developer Community or GitHub 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:
VERIFIED FIXED

Description Kent Green [MSFT] 2014-08-18 18:27:59 UTC
---Overview---
Using the .JoinMulticastGroup (IPAddress) method on a UdpClient object throws a "SIGSEGV while executing native code" error in release mode; but not in debug mode.

--Build Information--
=== Xamarin Studio ===

Version 5.3 (build 423)
Installation UUID: 7d1f6346-03b4-4b5f-b01b-a16d3a1c25a0
Runtime:
	Microsoft .NET 4.0.30319.18444
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.26

=== Xamarin.Android ===

Version: 4.16.0 (Business Edition)
Android SDK: C:\Program Files (x86)\Android\android-sdk
	Supported Android versions:
		2.2    (API level 8)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		4.5    (API level 21)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_39
java version "1.6.0_39"
Java(TM) SE Runtime Environment (build 1.6.0_39-b04)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode)

=== Build Information ===

Release ID: 503000423
Git revision: f4bb573b38a18c9f5400d8734eada2d5610deae4
Build date: 2014-08-12 17:31:43-04
Xamarin addins: 0961e1860cd1dcb4e47062f25c40d224979a02fb

=== Operating System ===

Windows 6.1.7601.65536 (64-bit)


---Additional Build the issue occurs on---

Microsoft Visual Studio Professional 2012
Version 11.0.60610.01 Update 3
Microsoft .NET Framework
Version 4.5.51641

Installed Version: Professional

Team Explorer for Visual Studio 2012   04938-088-1072927-02719
Microsoft Team Explorer for Visual Studio 2012

Visual Basic 2012   04938-088-1072927-02719
Microsoft Visual Basic 2012

Visual C# 2012   04938-088-1072927-02719
Microsoft Visual C# 2012

Visual C++ 2012   04938-088-1072927-02719
Microsoft Visual C++ 2012

Visual F# 2012   04938-088-1072927-02719
Microsoft Visual F# 2012

Windows Phone SDK 8.0 - ENU   04938-088-1072927-02719
Windows Phone SDK 8.0 - ENU

Microsoft Advertising SDK for Windows Phone 8  
Microsoft Advertising SDK for Windows Phone 8
Build

NuGet Package Manager   2.8.50116.120
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Xamarin   3.3.47.0 (0b2a123923812a88ed3091909478dbe9e0111f00)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android
Comment 1 Kent Green [MSFT] 2014-08-18 18:43:14 UTC
Created attachment 7714 [details]
Customer's Sample that demonstrates the bug

From case: https://xamarin.desk.com/agent/case/84232

To reproduce the issue, simply deploy the sample in release mode without any changes; and the error will show up in the Output/Application Output window.

If you comment out the line noted below from UDPCrash.cs then the error disappears:

static class UDPCrash
{
   public static void joinRoom ()
   {
      try
      {
         IPAddress multicastAddress=IPAddress.Parse("224.0.0.224");
	 var clientWriter=new UdpClient ();

	 //TODO: .JoinMulticastGroup (multicastAddress); seems to be the offending method.
>	 clientWriter.JoinMulticastGroup (multicastAddress); 
      }
      catch (Exception e)
      {
	 System.Diagnostics.Debug.WriteLine ("join room error: "+e.ToString());
	      return;
      }
   }
}
Comment 3 Kent Green [MSFT] 2014-08-19 13:58:03 UTC
Here's what the Stack Trace looks like when the 

>[mono-rt] Stacktrace:
>[mono-rt] 
>[mono-rt]   at <unknown> <0xffffffff>
>[mono-rt]   at (wrapper managed-to-native) System.Net.Sockets.Socket.SetSocketOption_internal (intptr,System.Net.Sockets.SocketOptionLevel,System.Net.Sockets.SocketOptionName,object,byte[],int,int&) <0xffffffff>
>[mono-rt]   at System.Net.Sockets.Socket.SetSocketOption (System.Net.Sockets.SocketOptionLevel,System.Net.Sockets.SocketOptionName,object) <0x0011b>
>[mono-rt]   at System.Net.Sockets.UdpClient.JoinMulticastGroup (System.Net.IPAddress) <0x0006f>
>[mono-rt]   at App1.UDPCrash.joinRoom () <0x0004f>
>[mono-rt]   at App1.GLView1.OnRenderFrame (OpenTK.FrameEventArgs) <0x00023>
>[mono-rt]   at OpenTK.Platform.Android.AndroidGameView.RenderFrameInternal (OpenTK.FrameEventArgs) <0x00043>
>[mono-rt]   at OpenTK.Platform.Android.AndroidGameView.RunIteration (System.Threading.CancellationToken) <0x00327>
>[mono-rt]   at OpenTK.Platform.Android.AndroidGameView/<StartThread>c__AnonStorey0.<>m__0 (object) <0x0001f>
>[mono-rt]   at Android.App.SyncContext/<Send>c__AnonStorey1.<>m__0 () <0x00019>
>[mono-rt]   at Java.Lang.Thread/RunnableImplementor.Run () <0x00028>
>[mono-rt]   at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) <0x0002c>
>[mono-rt]   at (wrapper dynamic-method) object.958efa85-ced4-46e6-8869-923237f02fc9 (intptr,intptr) <0x0001b>
>[mono-rt]   at (wrapper native-to-managed) object.958efa85-ced4-46e6-8869-923237f02fc9 (intptr,intptr) <0xffffffff>
>[mono-rt] 
>[mono-rt] =================================================================
>[mono-rt] Got a SIGSEGV while executing native code. This usually indicates
>[mono-rt] a fatal error in the mono runtime or one of the native libraries 
>[mono-rt] used by your application.
>[mono-rt] =================================================================
>[mono-rt] 
>[libc] Fatal signal 11 (SIGSEGV) at 0x0000000c (code=1), thread 1343 (App1.App1)
Comment 4 Jonathan Pryor 2014-08-19 14:55:36 UTC
I don't get the stack trace in Comment #3.

Instead, I see this on `adb logcat`:

> W/        ( 3337): Missing method .ctor in assembly /storage/emulated/0/Android/data/Scratch.Bxc22183/files/.__override__/Scratch.Bxc22183.dll, type System.Runtime.Versioning.TargetFrameworkAttribute
> W/        ( 3337): Can't find custom attr constructor image: /storage/emulated/0/Android/data/Scratch.Bxc22183/files/.__override__/Scratch.Bxc22183.dll mtoken: 0x0a00001c
> F/        ( 3337): * Assertion at /Users/builder/data/lanes/1131/e30a1175/source/mono/mono/metadata/class.c:5607, condition `!mono_loader_get_last_error ()' not met
> F/libc    ( 3337): Fatal signal 6 (SIGABRT) at 0x00000d09 (code=-6), thread 3337 (cratch.Bxc22183)
Comment 6 Kent Green [MSFT] 2014-08-20 14:33:36 UTC
Original customer reported that they found a workaround for their project here:

http://stackoverflow.com/questions/22571883/monodroid-linking-sdk-assemblies-causing-sigsegv-in-mono-library

---

In my own tests; I noticed two changes to the Android Build options that suppressed the error in the sample project:

1) Adding "System" to the "Ignore Assemblies" in the Linker option.
AND/OR 
2) Selecting "Use shared Mono runtime" (w/o Fast assembly deployment)
Comment 7 Sadik Ali 2014-08-25 10:12:32 UTC
I have tried to reproduce this issue by following the suggestions mentioned in comment 1 but not able to reproduce this. I have also tried to run the app attached in comment 2 on both device and emulator in release mode without doing any change in the app and it deploy the app on device/emulator successfully without showing any error in application output.

Screencast : http://www.screencast.com/t/tclfjpYtRU

Please let me know if I need to follow any specific step to reproduce this.

Note : I have checked this issue with Xamarin 3.3.47.0 and 3.5.39.0, And not able to reproduce with both builds.

Environment Info :

=== Xamarin Studio ===

Version 5.2.1 (build 1)
Installation UUID: 312d4e9a-339c-433f-a27b-88ce0777af8f
Runtime:
	Microsoft .NET 4.0.30319.34014
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.25

=== Xamarin.Android ===

Version: 4.16.0 (Enterprise Edition)
Android SDK: D:\SDK\android-sdk
	Supported Android versions:
		1.6   (API level 4)
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		3.2   (API level 13)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_31
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode, sharing)

=== Build Information ===

Release ID: 502010001
Git revision: d06832ce9807d6be24aca225457e8b37c7669f6f
Build date: 2014-08-07 14:35:09-04
Xamarin addins: 1de032531be4cecf2f39dbee3b87aac78204058c

=== Operating System ===

Windows 6.2.9200.0 (64-bit)
Comment 8 Jonathan Pryor 2014-11-20 17:43:51 UTC
Comment #16 makes this sound like a linker bug; assigning to Radek.

I'm unfortunately not able to reproduce.

One oddity is that the .csproj within Attachment #7724 [details] only builds a Release .apk with x86, not armeabi-v7a support. @Kent: Is this deliberate? Are you able to repro this on any ARM devices?
Comment 9 Radek Doulik 2014-11-21 06:00:40 UTC
The original bug is duplicate of #21578 and is already fixed. I will look into the issue Jonathan found in Comment #4 as well.
Comment 10 Radek Doulik 2014-11-21 06:35:03 UTC
Wasn't able to reproduce the issue from Comment 4, assuming it is already fixed, the original issue is fixed => FIXED
Comment 11 Danish Akhtar 2015-02-06 06:43:42 UTC
As per comment 10,closing this issue.
Comment 12 Michal Dobrodenka 2017-11-14 16:01:45 UTC
I can reproduce this bug on Mono 5.4 (tested on Raspberry Pi 1):

Attaching sample project which can crash on start, probability like 40-50%.
Crashing only in Release. It crashes on JoinMulticastGroup when some other UDP socket operation is in progress or something like that. I will probably use workaround and start multicast after broadcast or something like it. Must be Release mode and without --trace or --debug options.

Mono JIT compiler version 5.4.0.201 (tarball Fri Oct 13 23:06:45 UTC 2017):
* Assertion at w32socket.c:2234, condition `field' not met

Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) System.Net.Sockets.Socket.SetSocketOption_internal (intptr,System.Net.Sockets.SocketOptionLevel,System.Net.Sockets.SocketOptionName,object,byte[],int,int&) <0x0005b>
  at System.Net.Sockets.Socket.SetSocketOption_internal (System.Net.Sockets.SafeSocketHandle,System.Net.Sockets.SocketOptionLevel,System.Net.Sockets.SocketOptionName,object,byte[],int,int&) [0x00010] in <8a8abae728c244359683ef777047ab9e>:0
  at System.Net.Sockets.Socket.SetSocketOption (System.Net.Sockets.SocketOptionLevel,System.Net.Sockets.SocketOptionName,object) [0x0007c] in <8a8abae728c244359683ef777047ab9e>:0
  at System.Net.Sockets.UdpClient.JoinMulticastGroup (System.Net.IPAddress) [0x0006e] in <8a8abae728c244359683ef777047ab9e>:0
  at Multicast.DiscoveryServer.MulticastListenerJob () [0x0004d] in <c50f17768c824676a9a8c0b4c1a35028>:0
  at System.Threading.ThreadHelper.ThreadStart_Context (object) [0x00014] in <52f79f713f22467ab348e590c85015fb>:0
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00071] in <52f79f713f22467ab348e590c85015fb>:0
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00000] in <52f79f713f22467ab348e590c85015fb>:0
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) [0x0002b] in <52f79f713f22467ab348e590c85015fb>:0
  at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <52f79f713f22467ab348e590c85015fb>:0
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) [0x0004d] in <52f79f713f22467ab348e590c85015fb>:0
/proc/self/maps:
00010000-00339000 r-xp 00000000 b3:02 18132      /usr/bin/mono-sgen
00349000-0034a000 r--p 00329000 b3:02 18132      /usr/bin/mono-sgen
0034a000-0034c000 rw-p 0032a000 b3:02 18132      /usr/bin/mono-sgen
0034c000-00378000 rw-p 00000000 00:00 0
0197d000-01ab1000 rw-p 00000000 00:00 0          [heap]
b3f00000-b3f01000 ---p 00000000 00:00 0
b3f01000-b4100000 rw-p 00000000 00:00 0          [stack:9028]
b4162000-b4400000 r--p 00000000 b3:02 18461      /usr/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll
b4400000-b4401000 ---p 00000000 00:00 0
b4401000-b4500000 rw-p 00000000 00:00 0          [stack:9027]
b4500000-b4501000 ---p 00000000 00:00 0
b4501000-b4600000 rw-p 00000000 00:00 0          [stack:9026]
b4600000-b4601000 ---p 00000000 00:00 0
b4601000-b4700000 rw-p 00000000 00:00 0          [stack:9025]
b4700000-b475f000 rw-p 00000000 00:00 0
b475f000-b4800000 ---p 00000000 00:00 0
b4809000-b4813000 r-xp 00000000 b3:02 1945       /lib/arm-linux-gnueabihf/libnss_files-2.19.so
b4813000-b4822000 ---p 0000a000 b3:02 1945       /lib/arm-linux-gnueabihf/libnss_files-2.19.so
b4822000-b4823000 r--p 00009000 b3:02 1945       /lib/arm-linux-gnueabihf/libnss_files-2.19.so
b4823000-b4824000 rw-p 0000a000 b3:02 1945       /lib/arm-linux-gnueabihf/libnss_files-2.19.so
b4828000-b482a000 rw-p 00000000 00:00 0
b482a000-b484a000 rwxp 00000000 00:00 0
b484a000-b4c00000 r--p 00000000 b3:02 18367      /usr/lib/mono/4.5/mscorlib.dll
b4c00000-b5400000 rw-p 00000000 00:00 0
b5400000-b5401000 ---p 00000000 00:00 0
b5401000-b6000000 rw-p 00000000 00:00 0          [stack:9022]
b6000000-b6001000 ---p 00000000 00:00 0
b6001000-b6100000 rw-p 00000000 00:00 0          [stack:9024]
b6100000-b612c000 rw-p 00000000 00:00 0
b612c000-b6200000 ---p 00000000 00:00 0
b6200000-b6220000 rwxp 00000000 00:00 0
b6220000-b6221000 ---p 00000000 00:00 0
b6221000-b63a1000 rw-p 00000000 00:00 0          [stack:9023]
b63a1000-b63a2000 rw-p 00000000 00:00 0
b63a2000-b63b2000 rwxp 00000000 00:00 0
b63b2000-b63b3000 ---p 00000000 00:00 0
b63b3000-b6bb3000 rw-p 00000000 00:00 0          [stack:9021]
b6bb3000-b6be2000 ---p 00000000 00:00 0
b6be2000-b6bf2000 rwxp 00000000 00:00 0
b6bf2000-b6d7b000 r--p 00000000 b3:02 6122       /usr/lib/locale/locale-archive
b6d7b000-b6ea3000 r-xp 00000000 b3:02 1910       /lib/arm-linux-gnueabihf/libc-2.19.so
b6ea3000-b6eb2000 ---p 00128000 b3:02 1910       /lib/arm-linux-gnueabihf/libc-2.19.so
b6eb2000-b6eb4000 r--p 00127000 b3:02 1910       /lib/arm-linux-gnueabihf/libc-2.19.so
b6eb4000-b6eb5000 rw-p 00129000 b3:02 1910       /lib/arm-linux-gnueabihf/libc-2.19.so
b6eb5000-b6eb8000 rw-p 00000000 00:00 0
b6eb8000-b6ed5000 r-xp 00000000 b3:02 1923       /lib/arm-linux-gnueabihf/libgcc_s.so.1
b6ed5000-b6ee4000 ---p 0001d000 b3:02 1923       /lib/arm-linux-gnueabihf/libgcc_s.so.1
b6ee4000-b6ee5000 rw-p 0001c000 b3:02 1923       /lib/arm-linux-gnueabihf/libgcc_s.so.1
b6ee5000-b6ef9000 r-xp 00000000 b3:02 1957       /lib/arm-linux-gnueabihf/libpthread-2.19.so
b6ef9000-b6f09000 ---p 00014000 b3:02 1957       /lib/arm-linux-gnueabihf/libpthread-2.19.so
b6f09000-b6f0a000 r--p 00014000 b3:02 1957       /lib/arm-linux-gnueabihf/libpthread-2.19.so
b6f0a000-b6f0b000 rw-p 00015000 b3:02 1957       /lib/arm-linux-gnueabihf/libpthread-2.19.so
b6f0b000-b6f0d000 rw-p 00000000 00:00 0
b6f0d000-b6f0f000 r-xp 00000000 b3:02 1918       /lib/arm-linux-gnueabihf/libdl-2.19.so
b6f0f000-b6f1e000 ---p 00002000 b3:02 1918       /lib/arm-linux-gnueabihf/libdl-2.19.so
b6f1e000-b6f1f000 r--p 00001000 b3:02 1918       /lib/arm-linux-gnueabihf/libdl-2.19.so
b6f1f000-b6f20000 rw-p 00002000 b3:02 1918       /lib/arm-linux-gnueabihf/libdl-2.19.so
b6f20000-b6f26000 r-xp 00000000 b3:02 1960       /lib/arm-linux-gnueabihf/librt-2.19.so
b6f26000-b6f35000 ---p 00006000 b3:02 1960       /lib/arm-linux-gnueabihf/librt-2.19.so
b6f35000-b6f36000 r--p 00005000 b3:02 1960       /lib/arm-linux-gnueabihf/librt-2.19.so
b6f36000-b6f37000 rw-p 00006000 b3:02 1960       /lib/arm-linux-gnueabihf/librt-2.19.so
b6f37000-b6fa0000 r-xp 00000000 b3:02 1935       /lib/arm-linux-gnueabihf/libm-2.19.so
b6fa0000-b6fb0000 ---p 00069000 b3:02 1935       /lib/arm-linux-gnueabihf/libm-2.19.so
b6fb0000-b6fb1000 r--p 00069000 b3:02 1935       /lib/arm-linux-gnueabihf/libm-2.19.so
b6fb1000-b6fb2000 rw-p 0006a000 b3:02 1935       /lib/arm-linux-gnueabihf/libm-2.19.so
b6fb2000-b6fd2000 r-xp 00000000 b3:02 1900       /lib/arm-linux-gnueabihf/ld-2.19.so
b6fd2000-b6fd4000 r--p 00000000 00:15 202632     /tmp/mixal/Multicast.exe
b6fd4000-b6fda000 rw-p 00000000 00:00 0
b6fda000-b6fdc000 rw-p 00000000 00:00 0
b6fdc000-b6fdd000 rw-s 00000000 00:13 203039     /run/shm/mono.9020
b6fdd000-b6fde000 ---p 00000000 00:00 0
b6fde000-b6fdf000 r--p 00000000 00:00 0
b6fdf000-b6fe1000 rw-p 00000000 00:00 0
b6fe1000-b6fe2000 r--p 0001f000 b3:02 1900       /lib/arm-linux-gnueabihf/ld-2.19.so
b6fe2000-b6fe3000 rw-p 00020000 b3:02 1900       /lib/arm-linux-gnueabihf/ld-2.19.so
beca4000-becc5000 rw-p 00000000 00:00 0          [stack]
bedcc000-bedcd000 r-xp 00000000 00:00 0          [sigpage]
ffff0000-ffff1000 r-xp 00000000 00:00 0          [vectors]

Native stacktrace:


Debug info from gdb:

mono_gdb_render_native_backtraces not supported on this platform, unable to find gdb or lldb

=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================

Aborted
Comment 13 Michal Dobrodenka 2017-11-14 16:03:20 UTC
Created attachment 25700 [details]
Sample to demostrate it on linux

40% probability to crash on startup when compiled in release mode.
Comment 14 Radek Doulik 2017-11-14 17:02:24 UTC
@Michal: This bug was opened for Android. If that happen on desktop, please open a new bug for it, with Product set to the right value.

I think you can workaround that bug by creating custom linker xml description to preserve these fields:


<type fullname="System.Net.Sockets.MulticastOption" preserve="fields" />


Here you can find description, how the linker description file looks like:

https://developer.xamarin.com/guides/cross-platform/advanced/custom_linking/