Bug 20186 - Another AOT bug
Summary: Another AOT bug
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler ()
Version: 7.2.2
Hardware: PC Windows
: Normal normal
Target Milestone: C6SR1
Assignee: Zoltan Varga
Depends on:
Reported: 2014-05-30 05:16 UTC by Grigory (Playtika)
Modified: 2016-03-03 17:38 UTC (History)
9 users (show)

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

Source file for the p/invoke bindings (34.35 KB, text/plain)
2016-01-14 11:55 UTC, Egor Shkorov (Playtika)
testcase (3.18 MB, application/x-gzip)
2016-01-14 21:56 UTC, Zoltan Varga

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:

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));


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 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]
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 
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- 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.