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 1 commit
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
@@ -1,12 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TargetFrameworks>$(NetCoreAppMinimum);$(NetCoreAppCurrent)</TargetFrameworks>
</PropertyGroup>
<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>
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
<IncludeBuildOutput Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">false</IncludeBuildOutput>
<SuppressDependenciesWhenPacking Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">true</SuppressDependenciesWhenPacking>
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
</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
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,16 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)-android</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);$(NetCoreAppMinimum)-browser;$(NetCoreAppMinimum)-ios;$(NetCoreAppMinimum)-tvos;$(NetCoreAppMinimum)-android</TargetFrameworks>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<IsPackable>true</IsPackable>
<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>

<!--
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>
<IncludeBuildOutput Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">false</IncludeBuildOutput>
<SuppressDependenciesWhenPacking Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">true</SuppressDependenciesWhenPacking>
<FunctioningBinaryFormatter Condition="'$(TargetFramework)' == '$(NetCoreAppMinimum)'">true</FunctioningBinaryFormatter>
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>

<ItemGroup>
Expand All @@ -37,55 +50,59 @@
<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="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.Removed.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetPlatformIdentifier)' != ''">
<Compile Include="System\Runtime\Serialization\Formatters\Binary\BinaryFormatter.PlatformNotSupported.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
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.IO;

namespace System.Runtime.Serialization.Formatters.Binary
{
public sealed partial class BinaryFormatter : IFormatter
{
[RequiresDynamicCode(IFormatter.RequiresDynamicCodeMessage)]
[RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public object Deserialize(Stream serializationStream)
=> throw new PlatformNotSupportedException(SR.BinaryFormatter_Removed);

[RequiresUnreferencedCode(IFormatter.RequiresUnreferencedCodeMessage)]
public void Serialize(Stream serializationStream, object graph)
=> 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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ internal ObjectReader(Stream stream, ISurrogateSelector? selector, StreamingCont
_formatterEnums = formatterEnums;
}

[RequiresDynamicCode(ObjectReaderUnreferencedCodeMessage)]
[RequiresUnreferencedCode("Types might be removed")]
private static IDisposable? StartDeserialization()
{
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.


// It might have been removed in AoT.
bartonjs marked this conversation as resolved.
Show resolved Hide resolved
if (targetMethod is null)
{
throw new InvalidOperationException();
}

return (IDisposable?)targetMethod.Invoke(null, null);
}

[RequiresDynamicCode(ObjectReaderUnreferencedCodeMessage)]
[RequiresUnreferencedCode("Types might be removed")]
internal object Deserialize(BinaryParser serParser)
Expand All @@ -87,7 +105,7 @@ internal object Deserialize(BinaryParser serParser)

_isSimpleAssembly = (_formatterEnums._assemblyFormat == FormatterAssemblyStyle.Simple);

using (DeserializationToken token = SerializationInfo.StartDeserialization())
using (StartDeserialization())
{
if (_fullDeserialization)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace System.Runtime.Serialization.Formatters.Tests
{
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
[ConditionalClass(typeof(TestConfiguration), nameof(TestConfiguration.IsBinaryFormatterEnabled))]
public static class BinaryFormatterEventSourceTests
{
private const string BinaryFormatterEventSourceName = "System.Runtime.Serialization.Formatters.Binary.BinaryFormatterEventSource";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace System.Runtime.Serialization.Formatters.Tests
{
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsBinaryFormatterSupported))]
[ConditionalClass(typeof(TestConfiguration), nameof(TestConfiguration.IsBinaryFormatterEnabled))]
public partial class BinaryFormatterTests : FileCleanupTestBase
{
// On 32-bit we can't test these high inputs as they cause OutOfMemoryExceptions.
Expand Down
Loading
Loading