Bug 20186

Summary: Another AOT bug
Product: iOS Reporter: Grigory (Playtika) <GrigoryP>
Component: Mono runtime / AOT compilerAssignee: Zoltan Varga <vargaz>
Status: VERIFIED FIXED    
Severity: normal CC: egorsh, gouri.kumari, kumpera, mono-bugs+monotouch, prashantm, rolf, sebastien, udhams, vargaz
Priority: Normal    
Version: 7.2.2   
Target Milestone: C6SR1   
Hardware: PC   
OS: Windows   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Source file for the p/invoke bindings
testcase

Description Grigory (Playtika) 2014-05-30 05:16:07 UTC
[08:27:08] Received exception System.ExecutionEngineException: Attempting to JIT compile method '(wrapper unknown) Monosyne.Common.FreeType.SizeRec:PtrToStructure (intptr,object)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.

  at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:PtrToStructure (intptr,System.Type)
  at Monosyne.Common.FreeType.FreeTypeFont.set_Size (Int32 value) [0x00000] in <filename unknown>:0 


Here is the code:

var ftSize = (SizeRec)Marshal.PtrToStructure(face.size, typeof(SizeRec));

....

[StructLayout(LayoutKind.Sequential)]
internal struct SizeRec
{
    internal IntPtr face;
    internal GenericRec generic;
    internal SizeMetricsRec metrics;
    internal IntPtr @internal;
}

I see these crashes in hockey app but unable to repro locally
Comment 1 Rolf Bjarne Kvinge [MSFT] 2014-05-30 07:22:05 UTC
Can you include the complete definition for SizeRec (i.e. how is GenericRec and SizeMetricsRec defined)?
Comment 2 Egor Shkorov (Playtika) 2016-01-14 11:55:59 UTC
Created attachment 14583 [details]
Source file for the p/invoke bindings
Comment 3 Egor Shkorov (Playtika) 2016-01-14 11:58:11 UTC
Hello, we are beginning to encounter this bug more and more and it's becoming more and more critical for us to fix this or have a workaround for this issue.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2016-01-14 12:54:44 UTC
@Egor, I assume you're not able to reproduce this yourself, but do you have any other information?

* Does it happen on all architectures (armv7, armv7s, arm64), or only some?
* Does it happen on all types of devices, or only some (i.e. only iPads, only iPhones, etc)?
* Does it happen on all iOS versions, or only some?
Comment 5 Egor Shkorov (Playtika) 2016-01-14 13:15:03 UTC
I'll give you a complete list of iOS versions and devices this happens on, it seems to me that this is pretty much every device and version we support.

The iPad devices are:
* iPad 2
* iPad 3
* iPad 4
* iPad AIR
* iPad AIR 2
* iPad MINI
* iPad MINI 2
* iPad MINI 3

iPad iOS versions are:
* 6.0.1
* 6.1.3
* 7.0.4
* 7.1.2
* 8.0.2
* 8.1.1
* 8.1.3
* 8.2
* 8.3
* 8.4
* 8.4.1
* 9.0.2
* 9.1
* 9.2

The iPhone devices are:
* iPhone 4S
* iPhone 5
* iPhone 5C
* iPhone 5S
* iPhone 6
* iPhone 6 Plus
* iPhone 6S
* iPhone 6S Plus

The iPod devices are:
* iPod 5th gen

The iPhone iOS versions are:
* 6.1.3
* 7.0
* 7.0.4
* 7.1.1
* 7.1.2
* 8.0
* 8.0.2
* 8.1
* 8.1.1
* 8.1.2
* 8.1.3
* 8.2
* 8.3
* 8.4
* 8.4.1
* 9.0
* 9.0.1
* 9.0.2
* 9.1
* 9.2

In csproj from which we compile this architecutres: ARMv7, ARM64
Comment 6 Zoltan Varga 2016-01-14 13:35:23 UTC
Does this happen all the time, or its a random problem ? Also, what xamarin.ios version is this ?
Comment 7 Egor Shkorov (Playtika) 2016-01-14 13:45:29 UTC
This is a random problem, we don't have a 100% percent repro on it.
We have encountered this crash on 8.9.1.5 and also on 9.4. We had this bug going on for quite some time, so there are maybe even more versions that are affected.
Comment 8 Egor Shkorov (Playtika) 2016-01-14 14:23:48 UTC
From the statistics I have, it seems that iOS 9.2 is much more susceptible to this kind of bug, but I'm not sure if the sample is big enough.
Comment 9 Zoltan Varga 2016-01-14 15:13:47 UTC
Fixed in mono master 97d9498c0eb30c8dfbb7acf9e40f19580aeed77e/4.3.1/4.3.2 branches.
Comment 10 Zoltan Varga 2016-01-14 15:15:22 UTC
A workaround is to call Marshal.PtrToStructure(p, typeof(SizeRec))
at startup from FinishedLaunching with some dummy argument like 
var p = Marshal.AllocHGlobal (1024);
Comment 12 Egor Shkorov (Playtika) 2016-01-14 15:32:52 UTC
Thanks. We will implement this workaround in our application.
By the way, if it's fixed in 4.3.1, what Xamarin version will have these fixes?
Comment 13 Sebastien Pouliot 2016-01-14 15:47:48 UTC
@Egor the existing fix will be part of C7 (which might be XI version 9.6 or 9.8 depending on iOS 9.3 release date).

I filled a request for it to be included in the next C6 service release (XI 9.4.x) but it might be too late for SR1 (so SR2).
Comment 14 Zoltan Varga 2016-01-14 21:56:11 UTC
Created attachment 14591 [details]
testcase
Comment 15 Zoltan Varga 2016-01-14 21:56:52 UTC
The attached testcase should reproduce the issue, it crashes in about 30% of the cases on startup for me.
Comment 16 Egor Shkorov (Playtika) 2016-01-14 22:10:48 UTC
Thanks, I will look into implementing the workaround. 

As I understand from the source code I read, the problem is that wrappers start to get initialized on multiple threads and that causes marshal to fail?

Anyway, thanks for the fix. I have one similar bug of marshal nature that I will submit  sometime tomorrow, I think it is kinda caused by the same thing but in the different place, but it’s only an assumption.
Comment 17 Zoltan Varga 2016-01-14 22:24:22 UTC
Yes, this is a race condition in the runtime.
Comment 18 Udham Singh 2016-01-15 11:52:41 UTC
***************************************************************************
Reproduce Status : Reproduced
***************************************************************************

I am able to reproduce this issue on windows and Mac using current stable build monotouch 9.4.0.0. 
I am successfully able generate the exception on deploying the attached testcase project on device. 
But in second attempt, I am able to deploy the application on device successfully. 
After that, again for reproducing this issue, I unzip again attached test case project and 
deploy the app on device and hence cauht the issue again.

Screencast : http://www.screencast.com/t/d3FbzJuX
Environment Info : https://gist.github.com/Udham1/a96fbf96d71d5a8f66d0
Device : iPadMini(iOS  9.2)

****************************************************************************
Verified Status : Verified
****************************************************************************
I have checked the same with attached build on card monotouch-9.7.0.36_324d59666f32d0a007f5492e820fbb23bc82d5ff and
 its working fine.
Screencast : http://www.screencast.com/t/FWRvu6cMel
Environment Info: https://gist.github.com/Udham1/e0033aa3b850ff95cb3d


As per target milestone, I will check it once again and verify this issue when patch will merge to C6SR1.
Comment 19 GouriKumari 2016-03-03 17:38:32 UTC
Based on results included in card in comment#11, fix for this bug is already included with our current stable.Hence, I am marking this as verified.