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

Move the working BinaryFormatter implementation to a NuGet package #103255

Merged
merged 12 commits into from
Jun 17, 2024

Conversation

bartonjs
Copy link
Member

This change makes System.Runtime.Serialization.Formatters.dll build for both NetCoreAppMinimum and NetCoreAppCurrent.

  • The NetCoreAppCurrent version has a copy of BinaryFormatter that unconditionally throws PNSE from calls to Serialize or Deserialize.
    • It is included in the shared runtime
    • It is permanently AssemblyVersion locked to 8.1 ("bigger than 8.0, smaller than 9.0")
    • It is not included in the new NuGet package.
  • The NetCoreAppMinimum version(s) are exactly what this library was in .NET 8
    • The AppContext switch is still required to use it
    • These TFMs are included in a new System.Runtime.Serialization.Formatters nupkg
    • The AssemblyVersion floats with the repository (so 9.0 now, and we could stop producing the package in 10... or make a 10.0 then)
    • Because the net8 build can't reference System.Private.Corelib (it only builds for the current version), it can't call the SerializationGuard APIs without using reflection.

Points of contention:

  • It still uses the AppContext switch. (Some wanted it removed, some wanted it to remain... and remaining was a smaller diff, so I left it in)
  • We can't currently run tests for both varieties, as we can't force a downlevel runtime in tests (Support testing previous non-current .NETCoreApp targeted libraries #54639)
    • Removing the $(NetCoreAppCurrent) TFM from the src.csproj allows one to run the tests for the DLL(s) that go into the nupkg
  • Because of the ILLink Substitutions file that's in the mobile varieties, I can't figure out how to collapse all of the NetCoreAppMinimum builds down into one build with a runtime check. If someone tells me how to do this, or we can agree to get rid of the AppContext opt-in, the package size will drop by 80%.

Contributes to #98245.

@bartonjs bartonjs added area-System.Runtime binaryformatter-migration Issues related to the removal of BinaryFormatter and migrations away from it labels Jun 10, 2024
@bartonjs bartonjs added this to the 9.0.0 milestone Jun 10, 2024
@bartonjs bartonjs self-assigned this Jun 10, 2024
@ViktorHofer
Copy link
Member

Because of the ILLink Substitutions file that's in the mobile varieties, I can't figure out how to collapse all of the NetCoreAppMinimum builds down into one build with a runtime check. If someone tells me how to do this, or we can agree to get rid of the AppContext opt-in, the package size will drop by 80%.

Yes, I think we need to do this. We shouldn't ship a new package with RID specific assets in it. Either option sounds fine to me.

@jkotas
Copy link
Member

jkotas commented Jun 11, 2024

Because of the ILLink Substitutions file that's in the mobile varieties,

The mobile was special-cases since the BF was unconditionally disabled inbox for mobile, while the non-mobile platforms allowed opt-in. We should get rid of all special casing of mobile now that the BF is unconditionally disable inbox for all RIDs:

  • Delete all RID-specific builds
  • Delete all Condition="'$(TargetPlatformIdentifier)' == ''" and `Condition="'$(TargetPlatformIdentifier)' != ''" conditions
  • Delete System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.PlatformNotSupported.cs
  • If you are going to keep AppContext opt-in, rename ILLink.Substitutions.NonMobile.xml to ILLink.Substitutions.xml


if (IsNativeAot)
{
Console.WriteLine("BinaryFormatter is disabled in NativeAOT");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to leave these Console.WriteLines in?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do/did. When a test wants to use BinaryFormatter it leaves a place where it becomes clear why it's there or not.

But, if there's objection, I can certainly remove them.

@@ -1,20 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)-android</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum)</TargetFrameworks>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to build this for .NET 8?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the best/only way I could find for having two builds for the DLL, one where BF works (net8/NuGet) and one where it doesn't (net9/shared runtime).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are the problems with building the non-working inbox version via independent project?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. The original plan was to typeforward BinaryFormatter out into a new library and do the package trick there (which would still have required making two copies), but it turns out that System.Runtime.Serialization.Formatters.dll is itself basically the minimum closure of BinaryFormatter.

It sounds like your suggestion is to sort of do that again, making something like

src/libraries/System.Runtime.Serialization.Formatters/src/  :  IsPackable, net9
src/libraries/System.Runtime.Serialization.Formatters.Inbox/src/ : !IsPackable, net9, also claim to be System.Runtime.Serialization.Formatters.dll, <Compile Include=../../System.Runtime.Serialization.Formatters/src/AllTheThingsThatArentBinaryFormatter />

I don't know how well our build system would like that. And since the preview snap date is Tuesday, I don't know that I have the time to find out before then. We could explore it after.

Copy link
Member

@jkotas jkotas Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, something like that. The current scheme where the build for NetCoreAppMinimum is meant for NetCoreAppCurrent is not easy to understand. It is fine to explore something better later. It is possible that what you have put together is the best out of all bad solutions.

Copy link
Member

@ViktorHofer ViktorHofer Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NuGet limits us here. It doesn't support specifying multiple TFMs that map to the same TargetFrameworkMoniker+TargetPlatformMoniker tuple in a project. There's a proposal up to fix that eventually but until then, separate projects need to be used in such scenarios.

We have multiple source generator project files per rdifferent roslyn targeting pack version because of the exact same reason.

I would agree with Jan that the current solution might be good enough for what's proposed.

MethodInfo? targetMethod = typeof(SerializationInfo).GetMethod(
"StartDeserialization",
BindingFlags.Public | BindingFlags.Static,
Type.EmptyTypes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth caching (or caching a delegate created to it)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be even better to use UnsafeAccessor - no reflection, no delegates, no caching. UnsafeAccessor
shipped in 8.0 that is the current NetCoreAppMinimum.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StartDeserialization returns a public type that isn't in the reference assembly; it looks like UnsafeAccessor requires an exact signature match. So, unless there's an unsafe typeref I don't think we can use it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateDelegate is similarly not an option, it refuses to loose-bind the return type (struct).

Slow invoke works, or we can add another method in corelib that does the boxing to IDisposable there so it can be bound with UnsafeAccessor. I'm guessing we don't like that option, so I'm guessing we're sticking with slow-invoke. But either way I've moved this to a different type, and am at least saving the MethodInfo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rather get rid of this (it would be my preference), or turn it into a public API?

The non-public dependencies of OOB nuget packages like this one are always factory for problems.

{
internal sealed class SerializationGuard
{
private static readonly MethodInfo? s_startDeserialization =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok for now but should probably be removed before RTM. Serialization Guard has been a contentious feature and there's no guarantee it will retain its current API shape (or even exist at all!) going forward. Private reflection from an OOB package back into the shared framework adds unnecessary risk.

Copy link
Member

@stephentoub stephentoub Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. Can we just remove the whole thing, both from here and anywhere that's enlisting in serialization guard?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove the invocation from here, since Levi's already cool with that. I'll let the "can we delete SG altogether" fight continue elsewhere, since BinaryFormatter isn't the only thing activating it.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. There are some imperfections, but I am fine with them.

@bartonjs
Copy link
Member Author

/ba-g The one reported test failure is known issue #101409, so the Build Analysis should be green.

@bartonjs bartonjs merged commit 4a7ac0e into dotnet:main Jun 17, 2024
82 of 84 checks passed
@bartonjs bartonjs deleted the bf_package branch June 17, 2024 23:57
@github-actions github-actions bot locked and limited conversation to collaborators Jul 18, 2024
@jeffhandley jeffhandley added the breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. label Jul 25, 2024
@dotnet-policy-service dotnet-policy-service bot added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime binaryformatter-migration Issues related to the removal of BinaryFormatter and migrations away from it breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants