Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: Attempting to JIT compile method '(wrapper dynamic-method) void object:WriteArrayOfintToXml (System.Runtime.Serialization.XmlWriterDelegator,object,System.Runtime.Serialization.XmlObjectSerializerWriteContext,System.Runtime.Serialization.CollectionDataContract)' while running in aot-only mode. #47114

Closed
rolfbjarne opened this issue Jan 18, 2021 · 7 comments
Assignees
Milestone

Comments

@rolfbjarne
Copy link
Member

Description

The following code throws an exception at runtime when running on an iOS device:

var ds = new DataContractSerializer (typeof (IEnumerable<int>));
using (var xw = XmlWriter.Create (System.IO.Stream.Null))
	ds.WriteObject (xw, new int [] { 1, 2, 3 });

Exception:

System.ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) void object:WriteArrayOfintToXml (System.Runtime.Serialization.XmlWriterDelegator,object,System.Runtime.Serialization.XmlObjectSerializerWriteContext,System.Runtime.Serialization.CollectionDataContract)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed)
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
    at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType)
    at System.Runtime.Serialization.CodeGenerator.EndMethod()
    at System.Runtime.Serialization.XmlFormatWriterGenerator.CriticalHelper.GenerateCollectionWriter(CollectionDataContract collectionContract)
    at System.Runtime.Serialization.XmlFormatWriterGenerator.GenerateCollectionWriter(CollectionDataContract collectionContract)
    at System.Runtime.Serialization.CollectionDataContract.CreateXmlFormatWriterDelegate()
    at System.Runtime.Serialization.CollectionDataContract.get_XmlFormatWriterDelegate()
    at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
    at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
    at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
    at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
    at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
    at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
    at MySingleView.AppDelegate.FinishedLaunching(UIApplication app, NSDictionary options) in /Users/rolf/work/maccore/onedotnet/xamarin-macios/tests/dotnet/MySingleView/AppDelegate.cs:line 45

Repro:

git clone https://github.com/rolfbjarne/xamarin-macios
cd xamarin-macios
git checkout dotnet-attempting-to-jit-compile-method-WriteArrayOfintToXml
./configure --enable-dotnet
make reset
make all -j8
make install -j8
make singleview -C tests/dotnet

Complete output

Configuration

.NET 6.0.100-alpha.1.21060.3

Binlog:
MySingleView.binlog.zip

Regression?

No (new feature)

@dotnet-issue-labeler dotnet-issue-labeler bot added area-Serialization untriaged New issue has not been triaged by the area owner labels Jan 18, 2021
@marek-safar marek-safar added os-ios Apple iOS and removed untriaged New issue has not been triaged by the area owner labels Jan 20, 2021
@marek-safar marek-safar added this to the 6.0.0 milestone Jan 20, 2021
@steveisok
Copy link
Member

@vargaz should this work?

@vargaz
Copy link
Contributor

vargaz commented Jun 29, 2021

Not likely, dynamic methods will only work with the interpreter.

@rolfbjarne
Copy link
Member Author

@vargaz should this work?

The test case is C# code that works in current versions of Xamarin.iOS, so one possibility is that it's the BCL that has changed.

@steveisok
Copy link
Member

steveisok commented Jun 29, 2021

I suspect we may want to hit this code path instead

if (DataContractSerializer.Option == SerializationOption.ReflectionOnly)
{
return CreateReflectionXmlFormatCollectionWriterDelegate();
}

Which may mean setting this to be ReflectionOnly for iOS/tvOS/MacCatalyst

private static SerializationOption _option = IsReflectionBackupAllowed() ? SerializationOption.ReflectionAsBackup : SerializationOption.CodeGenOnly;

@danmoseley who would be the best person to help us confirm this is the right approach? For iOS devices, we basically can't generate any IL on the fly.

@rolfbjarne
Copy link
Member Author

Which may mean setting this to be ReflectionOnly for iOS/tvOS/MacCatalyst

Maybe make it depend on RuntimeFeature.IsDynamicCodeSupported?

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.runtimefeature.isdynamiccodesupported?view=net-5.0

@MaximLipnin
Copy link
Contributor

It's reproducible against the simulator when executing

1. ./build.sh -os iOSSimulator -arch x64 -c Release -s mono+libs
2. ./dotnet.sh build /t:Test /p:TargetOS=iOSSimulator /p:TargetArchitecture=x64 /p:Configuration=Release /p:RunAOTCompilation=true  /p:EnableAggressiveTrimming=true src/tests/FunctionalTests/iOS/Simulator/AOT/iOS.Simulator.Aot.Test.csproj

and using the code from the issue description as a functional test

MaximLipnin added a commit that referenced this issue Jul 13, 2021
…cCatalyst (#55503)

An attempt to address #47114 by making the System.Runtime.Serialization.DataContractSerializer.Option property return ReflectionOnly value when RuntimeFeature.IsDynamicCodeSupported is true.
@MaximLipnin
Copy link
Contributor

The issue should be fixed now by #55503, so closing it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants