-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
The 'seal' optimization fails with XmlSerializer #2266
Comments
It's very likely a bug in linker where we don't track correctly reflection-emit APIs as something that requires the base type to subclass. @vitek-karas does the data flow analysis have such data? |
Two options I can think of:
The second DAM(All) is because we use it in the TypeBuilder.DefineType - and it's possible to flow this through multiple layers correctly. The most precise would obviously be to have a special data flow annotation for this - but it's a rather rare case, not sure it's worth the complexity. I think the DAM(All) solution sounds reasonable. @MichalStrehovsky , @sbomer - ideas/opinions |
We don't know what types are accessed through reflection. One can call DAM(All) would probably work but it's a bit of a hack - we would be making an assumption on how I thought sealer optimization is unsupported whenever Reflection.Emit is allowed - it would be the safest position to take on this. |
I agree that it's a bit of a hack... it just felt like the least "evil". But if we think it's OK to simply disable the optimization all up, that works obviously as well. @rolfbjarne is there a reason you were trying to enable the optimization on this app? |
We enable it by default in a few scenarios to minimize app size. This is our current logic: |
<!-- If a release build and the app is not extensible (no interpreter or JIT/code-loading for macOS) then the sealer optimization can be used --> If illink were to make that one line work, would it fix the scenario? (Btw, XmlSerializer does have codepaths that work without Reflection.Emit - we would just need to expose them as a feature switch: dotnet/runtimelab#593 and dotnet/runtimelab#600 should be all that's needed.) |
+1 to what @MichalStrehovsky says above. I incorrectly thought that we're using pre-generated serialization assemblies, but that's not the case. @MichalStrehovsky would it make sense to modify the XmlSerializer to behave similarly to RegEx where it avoids Ref.Emit if the runtime states that it can't do that? Maybe we don't even need a feature switch for this then. |
That would be more of a question to the team that owns If we expose the "no JIT/no interpreter" option as a supported thing, there should probably be a .NET 6 issue on doing something with this. @rolfbjarne is the no JIT/no interpreter option supported officially or is it just a power user feature that we offer to close partners that know how to deal with this, but don't otherwise document? |
If we agree on this we should make it a rule in the code and warn/fail. We would probably need a feature switch for this though (linker itself can hardly tell if ref.emit will work or not). |
Yeah, linker is not in the position to know whether this is a safe optimization to do and whether it was paired with something that makes it okay. AFAIK the general .NET SDK doesn't expose sealing as an option at all (ability to inject arbitrary arguments into linker command line is not a supported territory). It is the responsibility of the SDK to pair it with something that makes the optimization okay (like the Xamarin SDK does, where it enables it on iPlatforms only if interpreter got disabled). |
In fact it seems that's the problem: XmlSerializer shouldn't use Reflection.Emit in this case (I believe that's supposed to be automatic after dotnet/runtime#56604, but it isn't working). |
I've filed dotnet/runtime#59167 to track the problem with XmlSerializer, so I'm closing this one, since there doesn't seem to be any problems with the linker. |
I'm not sure if this is a bug in the linker or the BCL, but if I enable the seal optimization:
then the xml serializer stops working with:
Repro:
consoleapp-18dd81d.zip
Download & extract & run "make linked notlinked" (you might have to adjust/remove NuGet.config depending on the version of .NET installed):
Binlogs:
binlogs.zip
The text was updated successfully, but these errors were encountered: