Bug 45371

Summary: SIGSEGV occurs when making call from native to managed code
Product: iOS Reporter: kleinc
Component: Mono runtime / AOT compilerAssignee: Zoltan Varga <vargaz>
Severity: normal CC: jungmok.han, kleinc, kumpera, mono-bugs+monotouch, narayanp, rolf, saurabhd
Priority: ---    
Version: XI 10.0 (iOS10)   
Target Milestone: (C9)   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: Yes
Last known good build: Xamarin.iOS
Attachments: A sample project demonstrating the issue.
test project

Description kleinc 2016-10-13 17:02:49 UTC
We're experiencing a SIGSEGV when the native library of our application attempts to call into the managed code of our application via a callback. This issue started to occur only after updating Xamarin.iOS to 10.x, and occurs only on 32-bit platforms (iPhone 4s/5). The contents of the application output are:

Native stacktrace:
2016-10-13 11:49:45.276 DTNADriver[298:33034] critical: 	0   DTNADriver                          0x001f9b79 mono_handle_native_sigsegv + 240
2016-10-13 11:49:45.276 DTNADriver[298:33034] critical: 	1   DTNADriver                          0x0020232d mono_sigsegv_signal_handler + 150
2016-10-13 11:49:45.276 DTNADriver[298:33034] critical: 	2   libsystem_platform.dylib            0x1c5e726d _sigtramp + 24
2016-10-13 11:49:45.277 DTNADriver[298:33034] critical: 	3   DTNADriver                          0x005e8b74 generic_trampoline_aot_plt + 48
2016-10-13 11:49:45.277 DTNADriver[298:33034] critical: 	4   DTNADriver                          0x00189005 _ZN17CClientNotifySink6NotifyEllmPhjxl + 150
2016-10-13 11:49:45.277 DTNADriver[298:33034] critical: 	5   DTNADriver                          0x0019b1cf _ZN26CNotificationManagerModule17CNotificationItem6NotifyEl17ENotificationType + 174
2016-10-13 11:49:45.277 DTNADriver[298:33034] critical: 	6   DTNADriver                          0x0019b0d3 _ZN26CNotificationManagerModule3RunEv + 160
2016-10-13 11:49:45.278 DTNADriver[298:33034] critical: 	7   DTNADriver                          0x00198bc7 _ZN26CNotificationManagerModule12ThreadLaunchEPv + 18
2016-10-13 11:49:45.278 DTNADriver[298:33034] critical: 	8   DTNADriver                          0x00120299 _ZN9nexiq_osa10ThreadMainEPv + 100
2016-10-13 11:49:45.278 DTNADriver[298:33034] critical: 	9   libsystem_pthread.dylib             0x1c5eba27 <redacted> + 216
2016-10-13 11:49:45.278 DTNADriver[298:33034] critical: 	10  libsystem_pthread.dylib             0x1c5eb94d _pthread_start + 234
2016-10-13 11:49:45.279 DTNADriver[298:33034] critical: 	11  libsystem_pthread.dylib             0x1c5e94a8 thread_start + 8
2016-10-13 11:49:45.279 DTNADriver[298:33034] critical: 
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2016-10-13 17:31:38 UTC
Could you please attach a test project we can use to reproduce this?
Comment 2 kleinc 2016-10-17 19:40:12 UTC
Created attachment 18092 [details]
A sample project demonstrating the issue.

The issue occurs when the callback methods are in a referenced PCL. A workaround is to move the callbacks into the Xamarin.iOS project.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2016-10-18 09:00:36 UTC
Created attachment 18100 [details]
test project

Smaller test project
Comment 4 Rolf Bjarne Kvinge [MSFT] 2016-10-18 09:03:02 UTC
I can reproduce the crash with the supplied test project (with xamarin-macios/master), and I can confirm it only happens on ARMv7 (ARM64 works fine).

Initially targeting C9 since this is a regression.
Comment 5 Zoltan Varga 2016-10-28 20:53:01 UTC
This happens when a call is made back to native code on a thread not registered with the runtime. The auto registration code has a bug where it depends on the thread already registered, causing this crash.
Comment 6 Zoltan Varga 2016-10-28 23:17:57 UTC
Hopefully going to get fixed by:
Comment 7 Zoltan Varga 2016-11-02 02:09:54 UTC
Fixed in mono master 0eda1938a4e38fcb18bee96df60cd8734cec497b.
Comment 8 narayanp 2016-11-18 10:18:01 UTC
I can also reproduce this Issue with XI using project mentioned in comment#3. This is the Application output for the same: https://gist.github.com/saurabh360/4b2b0a369b3c0e25f6a2bbf522c86f96

Environment Info and Logs: https://gist.github.com/81015040f83a236897dc2fea4c39f2b2

I have checked this with Cycle 9 (XI and Master XI, I am not seeing any SIGSEGV in attached project. This is the application output for the same: https://gist.github.com/saurabh360/568864e7b27d0cbf3f82995a4e73c079

Environment Info: https://gist.github.com/saurabh360/c67fe34a59fd1e0a749a8a3519e3e75d
Comment 9 Jungmok Han 2016-12-13 20:00:59 UTC
We also ran into this issue on 32 bit iOS devices when calling managed code from native.  Thanks for fixing this one, Xamarin.