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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ public static int SlowRuntimeTimeoutModifier
public static bool IsThreadingSupported => (!IsWasi && !IsBrowser) || IsWasmThreadingSupported;
public static bool IsWasmThreadingSupported => IsBrowser && IsEnvironmentVariableTrue("IsBrowserThreadingSupported");
public static bool IsNotWasmThreadingSupported => !IsWasmThreadingSupported;
public static bool IsBinaryFormatterSupported => IsNotMobile && !IsNativeAot;

private static readonly Lazy<bool> s_isBinaryFormatterSupported = new Lazy<bool>(DetermineBinaryFormatterSupport);
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
public static bool IsBinaryFormatterSupported => s_isBinaryFormatterSupported.Value;

public static bool IsStartingProcessesSupported => !IsiOS && !IstvOS;

Expand Down Expand Up @@ -721,5 +723,34 @@ private static bool GetSupportsSha3()

return false;
}

private static bool DetermineBinaryFormatterSupport()
{
if (IsNetFramework)
{
return true;
}
else if (IsNativeAot)
{
return false;
}

Assembly assembly = typeof(System.Runtime.Serialization.Formatters.Binary.BinaryFormatter).Assembly;
AssemblyName name = assembly.GetName();
Version assemblyVersion = name.Version;

bool isSupported = true;

// Version 8.1 is the version in the shared runtime (.NET 9+) that has the type disabled with no config.
// Assembly versions beyond 8.1 are the fully functional version from NuGet.
// Assembly versions before 8.1 probably won't be encountered, since that's the past.

if (assemblyVersion.Major == 8 && assemblyVersion.Minor == 1)
{
isSupported = false;
}

return isSupported;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1590,7 +1590,10 @@ public void SerializationFormat_Binary_does_not_work_by_default()
#pragma warning restore SYSLIB0038
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public static bool RemoteExecutorBinaryFormatter =>
RemoteExecutor.IsSupported && PlatformDetection.IsBinaryFormatterSupported;

[ConditionalFact(nameof(RemoteExecutorBinaryFormatter))]
public void SerializationFormat_Binary_works_with_appconfig_switch()
{
RemoteExecutor.Invoke(RunTest).Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ public void SerializationFormat_Binary_does_not_work_by_default()
#pragma warning restore SYSLIB0038
}

[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
public static bool RemoteExecutorBinaryFormatter =>
RemoteExecutor.IsSupported && PlatformDetection.IsBinaryFormatterSupported;

[ConditionalFact(nameof(RemoteExecutorBinaryFormatter))]
public void SerializationFormat_Binary_works_with_appconfig_switch()
{
RemoteExecutor.Invoke(RunTest).Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
</PropertyGroup>

<ItemGroup>
<!-- TODO 103447: These tests all require BinaryFormatter, which won't work until there's a published NuGet package.
<Compile Include="**\*.cs" />
-->
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project>
<Import Project="..\Directory.Build.targets" />
<PropertyGroup>

<!--
The real implementation of this library is built for NetCoreAppMinimum,
the NetCoreAppCurrent build has a non-functional copy of BinaryFormatter.
The NetCoreAppCurrent build is only included in the shared runtime,
and should always lose to the package for unification; so it is pinned
at an assembly version that will always lose.
-->
<AssemblyVersion Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">8.1.0.0</AssemblyVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TargetFrameworks>$(NetCoreAppMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="System.Runtime.Serialization.Formatters.cs" />
<Compile Include="System.Runtime.Serialization.Formatters.Forwards.cs" />
</ItemGroup>
<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<ProjectReference Include="..\..\System.Collections.NonGeneric\ref\System.Collections.NonGeneric.csproj" />
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## About

Provides the legacy BinaryFormatter class for workloads which still require it.

## Main Types

The main types provided by this library are:

* `System.Runtime.Serialization.Formatters.Binary.BinaryFormatter`

## Additional Documentation

* [Obsoletion Notice](https://aka.ms/binaryformatter)

## Feedback & Contributing

System.Runtime.Serialization.Formatters is released as open source under the [MIT license](https://licenses.nuget.org/MIT). Bug reports are welcome at [the GitHub repository](https://github.com/dotnet/runtime). This package is considered legacy, and we only consider low-risk, high-impact fixes that are necessary to maintain functionality.
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,7 @@
<data name="BinaryFormatter_SerializationNotSupportedOnThisPlatform" xml:space="preserve">
<value>BinaryFormatter serialization and deserialization are not supported on this platform. See https://aka.ms/binaryformatter for more information.</value>
</data>
<data name="BinaryFormatter_Removed" xml:space="preserve">
<value>BinaryFormatter serialization and deserialization have been removed. See https://aka.ms/binaryformatter for more information.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
<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.

<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
<!-- TODO: Enable validation after the .NET 9 package release -->
<EnablePackageValidation>false</EnablePackageValidation>
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>

<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
<TargetPlatformIdentifier Condition="$(TargetFramework.Contains('-'))">$(TargetFramework.SubString($([MSBuild]::Add($(TargetFramework.IndexOf('-')), 1))))</TargetPlatformIdentifier>
<!-- TargetsMobile: When we replace implementations with PNSE, need to suppress some "field is never assigned to" warnings. -->
<NoWarn Condition="'$(TargetPlatformIdentifier)' != ''">$(NoWarn);CS0649</NoWarn>
<NoWarn>$(NoWarn);CA1822;IDE0060</NoWarn>

<FunctioningBinaryFormatter Condition="'$(TargetFramework)' == '$(NetCoreAppMinimum)'">true</FunctioningBinaryFormatter>
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
<!-- The GenerateNuspec target fails if this property is keyed off of FunctioningBinaryFormatter, so keep it logically in sync -->
<IncludeBuildOutput Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">false</IncludeBuildOutput>
<SuppressDependenciesWhenPacking Condition="'$(FunctioningBinaryFormatter)' != 'true'">true</SuppressDependenciesWhenPacking>
</PropertyGroup>

<ItemGroup>
<ILLinkSubstitutionsXmls Condition="'$(TargetPlatformIdentifier)' == ''" Include="$(ILLinkDirectory)ILLink.Substitutions.NonMobile.xml" />
<ItemGroup Condition="'$(FunctioningBinaryFormatter)' == 'true'">
<ILLinkSubstitutionsXmls Include="$(ILLinkDirectory)ILLink.Substitutions.xml" />
</ItemGroup>

<ItemGroup>
Expand All @@ -37,55 +42,55 @@
<Compile Include="System\Runtime\Serialization\ValueTypeFixupInfo.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\CommonEnums.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\IFieldInfo.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\HashHelpers.cs"
Link="Common\System\Collections\HashHelpers.cs" />
</ItemGroup>

<ItemGroup Condition="'$(FunctioningBinaryFormatter)' == 'true'">
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryArray.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryAssembly.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryAssemblyInfo.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryTypeConverter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryCrossAppDomainAssembly.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryCrossAppDomainMap.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryCrossAppDomainString.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryEnums.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.Core.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatterEventSource.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatterWriter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObject.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectInfo.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectReader.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectString.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectWithMap.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectWithMapTyped.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectWriter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryParser.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryTypeConverter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryUtilClasses.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\Converter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\IStreamable.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\MemberPrimitiveTyped.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\MemberPrimitiveUntyped.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\MemberReference.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\MessageEnd.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\ObjectMap.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\ObjectNull.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\SerializationHeaderRecord.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\ObjectProgress.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\Converter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryEnums.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatterEventSource.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatterWriter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectInfo.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectReader.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryObjectWriter.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryParser.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryUtilClasses.cs" />
<Compile Include="$(CommonPath)System\Obsoletions.cs"
Link="Common\System\Obsoletions.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\HashHelpers.cs"
Link="Common\System\Collections\HashHelpers.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' == ''">
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.Core.cs" />
<Compile Include="System\Runtime\Serialization\Formatters\Binary\SerializationHeaderRecord.cs" />
<Compile Include="System\Runtime\Serialization\LocalAppContextSwitches.cs" />
<Compile Include="$(CommonPath)System\LocalAppContextSwitches.Common.cs">
<Link>Common\System\LocalAppContextSwitches.Common.cs</Link>
</Compile>
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.PlatformNotSupported.cs" />
<ItemGroup Condition="'$(FunctioningBinaryFormatter)' != 'true'">
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.Removed.cs" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<ProjectReference Include="$(CoreLibProject)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\src\System.Runtime.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\src\System.Collections.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace System.Runtime.Serialization.Formatters.Binary
{
public sealed partial class BinaryFormatter : IFormatter
{
private static readonly ConcurrentDictionary<Type, TypeInformation> s_typeNameCache = new ConcurrentDictionary<Type, TypeInformation>();
bartonjs marked this conversation as resolved.
Show resolved Hide resolved

internal object[]? _crossAppDomainArray;

[RequiresDynamicCode(IFormatter.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public object Deserialize(Stream serializationStream)
Expand Down Expand Up @@ -88,5 +93,12 @@ public void Serialize(Stream serializationStream, object graph)
BinaryFormatterEventSource.Log.SerializationStop();
}
}

internal static TypeInformation GetTypeInformation(Type type) =>
s_typeNameCache.GetOrAdd(type, t =>
{
string assemblyName = FormatterServices.GetClrAssemblyName(t, out bool hasTypeForwardedFrom);
return new TypeInformation(FormatterServices.GetClrTypeFullName(t), assemblyName, hasTypeForwardedFrom);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public sealed partial class BinaryFormatter : IFormatter
[RequiresDynamicCode(IFormatter.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public object Deserialize(Stream serializationStream)
=> throw new PlatformNotSupportedException(SR.BinaryFormatter_SerializationNotSupportedOnThisPlatform);
=> throw new PlatformNotSupportedException(SR.BinaryFormatter_Removed);

[RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public void Serialize(Stream serializationStream, object graph)
=> throw new PlatformNotSupportedException(SR.BinaryFormatter_SerializationNotSupportedOnThisPlatform);
=> throw new PlatformNotSupportedException(SR.BinaryFormatter_Removed);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Concurrent;

namespace System.Runtime.Serialization.Formatters.Binary
{
[Obsolete(Obsoletions.BinaryFormatterMessage, DiagnosticId = Obsoletions.BinaryFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public sealed partial class BinaryFormatter : IFormatter
{
private static readonly ConcurrentDictionary<Type, TypeInformation> s_typeNameCache = new ConcurrentDictionary<Type, TypeInformation>();

internal ISurrogateSelector? _surrogates;
internal StreamingContext _context;
internal SerializationBinder? _binder;
internal FormatterTypeStyle _typeFormat = FormatterTypeStyle.TypesAlways; // For version resiliency, always put out types
internal FormatterAssemblyStyle _assemblyFormat = FormatterAssemblyStyle.Simple;
internal TypeFilterLevel _securityLevel = TypeFilterLevel.Full;
internal object[]? _crossAppDomainArray;

public FormatterTypeStyle TypeFormat { get { return _typeFormat; } set { _typeFormat = value; } }
public FormatterAssemblyStyle AssemblyFormat { get { return _assemblyFormat; } set { _assemblyFormat = value; } }
Expand All @@ -34,12 +29,5 @@ public BinaryFormatter(ISurrogateSelector? selector, StreamingContext context)
_surrogates = selector;
_context = context;
}

internal static TypeInformation GetTypeInformation(Type type) =>
s_typeNameCache.GetOrAdd(type, t =>
{
string assemblyName = FormatterServices.GetClrAssemblyName(t, out bool hasTypeForwardedFrom);
return new TypeInformation(FormatterServices.GetClrTypeFullName(t), assemblyName, hasTypeForwardedFrom);
});
}
}
Loading
Loading