Skip to content

Commit

Permalink
More BinaryFormatter obsoletions
Browse files Browse the repository at this point in the history
- Move some SYSLIB0011 obsoletions to type level
- Next wave of formatter obsoletions (SYSLIB0049)
- Remove dead code
  • Loading branch information
GrabYourPitchforks committed Apr 5, 2023
1 parent 735ddea commit e7e75e4
Show file tree
Hide file tree
Showing 73 changed files with 218 additions and 355 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@

<!-- Warnings that should be disabled in our test projects. -->
<PropertyGroup Condition="'$(IsTestProject)' == 'true' or '$(IsTestSupportProject)' == 'true' or '$(IsPublishedAppTestProject)' == 'true'">
<!-- don't warn on usage of BinaryFormatter from test projects -->
<NoWarn>$(NoWarn);SYSLIB0011</NoWarn>
<!-- don't warn on usage of BinaryFormatter and legacy formatter infrastructure from test projects -->
<NoWarn>$(NoWarn);SYSLIB0011;SYSLIB0049</NoWarn>
<!-- don't warn about unnecessary trim warning suppressions. can be removed with preview 6. -->
<NoWarn>$(NoWarn);IL2121</NoWarn>
<!-- allow nullable annotated files to be incorporated into tests without warning -->
Expand Down
1 change: 1 addition & 0 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
| __`SYSLIB0047`__ | XmlSecureResolver is obsolete. Use XmlResolver.ThrowingResolver instead when attempting to forbid XML external entity resolution. |
| __`SYSLIB0048`__ | RSA.EncryptValue and DecryptValue are not supported and throw NotSupportedException. Use RSA.Encrypt and RSA.Decrypt instead. |
| __`SYSLIB0049`__ | JsonSerializerOptions.AddContext is obsolete. To register a JsonSerializerContext, use either the TypeInfoResolver or TypeInfoResolverChain properties. |
| __`SYSLIB0049`__ | Legacy formatter-based serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information. |

## Analyzer Warnings

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ public object? this[object key]
{
ArgumentNullException.ThrowIfNull(key);

if (!key.GetType().IsSerializable)
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));

if ((value != null) && (!value.GetType().IsSerializable))
throw new ArgumentException(SR.Argument_NotSerializable, nameof(value));

throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}
}
Expand All @@ -91,12 +85,6 @@ public void Add(object key, object? value)
{
ArgumentNullException.ThrowIfNull(key);

if (!key.GetType().IsSerializable)
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));

if ((value != null) && (!value.GetType().IsSerializable))
throw new ArgumentException(SR.Argument_NotSerializable, nameof(value));

throw new InvalidOperationException(SR.InvalidOperation_ReadOnly);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ partial void RestoreRemoteStackTrace(SerializationInfo info, StreamingContext co
_watsonBuckets = (byte[]?)info.GetValueNoThrow("WatsonBuckets", typeof(byte[])); // Do not rename (binary serialization)

// If we are constructing a new exception after a cross-appdomain call...
#pragma warning disable SYSLIB0049 // StreamingContextStates is obsolete
if (context.State == StreamingContextStates.CrossAppDomain)
#pragma warning restore SYSLIB0049
{
// ...this new exception may get thrown. It is logically a re-throw, but
// physically a brand-new exception. Since the stack trace is cleared
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1604,11 +1604,13 @@ internal static void GetCustomAttributes(RuntimeType type, RuntimeType caType, r
if (!all && !s_pca.Contains(caType))
return;

#pragma warning disable SYSLIB0049 // Legacy serialization infrastructure is obsolete
if (all || caType == typeof(SerializableAttribute))
{
if ((type.Attributes & TypeAttributes.Serializable) != 0)
pcas.Add(new SerializableAttribute());
}
#pragma warning restore SYSLIB0049
if (all || caType == typeof(ComImportAttribute))
{
if ((type.Attributes & TypeAttributes.Import) != 0)
Expand All @@ -1621,11 +1623,13 @@ internal static bool IsDefined(RuntimeType type, RuntimeType? caType)
if (!all && !s_pca.Contains(caType!))
return false;

#pragma warning disable SYSLIB0049 // Legacy serialization infrastructure is obsolete
if (all || caType == typeof(SerializableAttribute))
{
if ((type.Attributes & TypeAttributes.Serializable) != 0)
return true;
}
#pragma warning restore SYSLIB0049
if (all || caType == typeof(ComImportAttribute))
{
if ((type.Attributes & TypeAttributes.Import) != 0)
Expand Down Expand Up @@ -1752,11 +1756,13 @@ internal static void GetCustomAttributes(RuntimeFieldInfo field, RuntimeType caT
pca = GetFieldOffsetCustomAttribute(field);
if (pca is not null) pcas.Add(pca);
}
#pragma warning disable SYSLIB0049 // Legacy serialization infrastructure is obsolete
if (all || caType == typeof(NonSerializedAttribute))
{
if ((field.Attributes & FieldAttributes.NotSerialized) != 0)
pcas.Add(new NonSerializedAttribute());
}
#pragma warning restore SYSLIB0049
}
internal static bool IsDefined(RuntimeFieldInfo field, RuntimeType? caType)
{
Expand All @@ -1772,11 +1778,13 @@ internal static bool IsDefined(RuntimeFieldInfo field, RuntimeType? caType)
{
if (GetFieldOffsetCustomAttribute(field) is not null) return true;
}
#pragma warning disable SYSLIB0049 // Legacy serialization infrastructure is obsolete
if (all || caType == typeof(NonSerializedAttribute))
{
if ((field.Attributes & FieldAttributes.NotSerialized) != 0)
return true;
}
#pragma warning restore SYSLIB0049

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ public sealed override IEnumerable<CustomAttributeData> CustomAttributes
}

FieldAttributes attributes = Attributes;
#pragma warning disable SYSLIB0049 // Legacy serialization infrastructure is obsolete
if (0 != (attributes & FieldAttributes.NotSerialized))
{
yield return new RuntimePseudoCustomAttributeData(typeof(NonSerializedAttribute), null);
}
#pragma warning restore SYSLIB0049
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public sealed override int GetArrayRank()

protected sealed override TypeAttributes GetAttributeFlagsImpl()
{
return TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Serializable;
TypeAttributes attrs = TypeAttributes.AutoLayout | TypeAttributes.AnsiClass | TypeAttributes.Class | TypeAttributes.Public | TypeAttributes.Sealed;
#pragma warning disable SYSLIB0049 // Legacy serialization infrastructure is obsolete
attrs |= TypeAttributes.Serializable;
#pragma warning restore SYSLIB0049
return attrs;
}

internal sealed override IEnumerable<RuntimeConstructorInfo> SyntheticConstructors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public sealed override IEnumerable<CustomAttributeData> CustomAttributes
if (0 != (attributes & TypeAttributes.Import))
yield return new RuntimePseudoCustomAttributeData(typeof(ComImportAttribute), null);

#pragma warning disable SYSLIB0049 // Legacy serialization infrastructure is obsolete
if (0 != (attributes & TypeAttributes.Serializable))
yield return new RuntimePseudoCustomAttributeData(typeof(SerializableAttribute), null);
#pragma warning restore SYSLIB0049
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/Common/src/System/Obsoletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,8 @@ internal static class Obsoletions

internal const string JsonSerializerOptionsAddContextMessage = "JsonSerializerOptions.AddContext is obsolete. To register a JsonSerializerContext, use either the TypeInfoResolver or TypeInfoResolverChain properties.";
internal const string JsonSerializerOptionsAddContextDiagId = "SYSLIB0049";

internal const string LegacyFormatterMessage = "Legacy formatter-based serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.";
internal const string LegacyFormatterDiagId = "SYSLIB0049";
}
}
3 changes: 3 additions & 0 deletions src/libraries/Common/tests/System/MockType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ internal class MockType : Type
public override bool IsSecurityCritical => throw Unexpected;
public override bool IsSecuritySafeCritical => throw Unexpected;
public override bool IsSecurityTransparent => throw Unexpected;
#if NET8_0_OR_GREATER
[Obsolete("Legacy formatter-based serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0049", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
#endif
public override bool IsSerializable => throw Unexpected;
public override bool IsSubclassOf(Type c) => throw Unexpected;
protected override bool IsValueTypeImpl() => throw Unexpected;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public static void Serialize(Stream o, string cryptoKey, DesigntimeLicenseContex

private static void SerializeWithBinaryFormatter(Stream o, string cryptoKey, DesigntimeLicenseContext context)
{
var formatter = new BinaryFormatter();
#pragma warning disable SYSLIB0011
#pragma warning disable IL2026 // suppressed in ILLink.Suppressions.LibraryBuild.xml
var formatter = new BinaryFormatter();
formatter.Serialize(o, new object[] { cryptoKey, context._savedLicenseKeys });
#pragma warning restore IL2026
#pragma warning restore SYSLIB0011
Expand Down
8 changes: 6 additions & 2 deletions src/libraries/System.Data.Common/src/System/Data/DataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,12 @@ private void SerializeDataSet(SerializationInfo info, StreamingContext context,
//Tables, Columns, Rows
for (int i = 0; i < Tables.Count; i++)
{
BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(context.State, false));
MemoryStream memStream = new MemoryStream();
#pragma warning disable SYSLIB0011 // Issue https://github.com/dotnet/runtime/issues/39289 tracks finding an alternative to BinaryFormatter
#pragma warning disable SYSLIB0049 // StreamingContext ctor is obsolete
BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(context.State, false));
bf.Serialize(memStream, Tables[i]);
#pragma warning restore SYSLIB0049
#pragma warning restore SYSLIB0011
memStream.Position = 0;
info.AddValue(string.Format(CultureInfo.InvariantCulture, "DataSet.Tables_{0}", i), memStream.GetBuffer());
Expand Down Expand Up @@ -421,9 +423,11 @@ private void DeserializeDataSetSchema(SerializationInfo info, StreamingContext c
byte[] buffer = (byte[])info.GetValue(string.Format(CultureInfo.InvariantCulture, "DataSet.Tables_{0}", i), typeof(byte[]))!;
MemoryStream memStream = new MemoryStream(buffer);
memStream.Position = 0;
BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(context.State, false));
#pragma warning disable SYSLIB0011 // Issue https://github.com/dotnet/runtime/issues/39289 tracks finding an alternative to BinaryFormatter
#pragma warning disable SYSLIB0049 // StreamingContext ctor is obsolete
BinaryFormatter bf = new BinaryFormatter(null, new StreamingContext(context.State, false));
DataTable dt = (DataTable)bf.Deserialize(memStream);
#pragma warning restore SYSLIB0049
#pragma warning restore SYSLIB0011
Tables.Add(dt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal OrdinalComparer() { }
public override int GetHashCode() { throw null; }
public override int GetHashCode(string obj) { throw null; }
}
[System.ObsoleteAttribute("Legacy formatter-based serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId = "SYSLIB0049", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
public sealed partial class UnitySerializationHolder : System.Runtime.Serialization.IObjectReference, System.Runtime.Serialization.ISerializable
{
public UnitySerializationHolder(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1447,9 +1447,6 @@
<data name="Argument_NotMethodCallOpcode" xml:space="preserve">
<value>The specified opcode cannot be passed to EmitCall.</value>
</data>
<data name="Argument_NotSerializable" xml:space="preserve">
<value>Argument passed in is not serializable.</value>
</data>
<data name="Argument_NoUninitializedStrings" xml:space="preserve">
<value>Uninitialized Strings cannot be created.</value>
</data>
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/DBNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ private DBNull(SerializationInfo info, StreamingContext context)

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
#pragma warning disable SYSLIB0049 // UnitySerializationHolder is obsolete
UnitySerializationHolder.GetUnitySerializationInfo(info, UnitySerializationHolder.NullUnity);
#pragma warning restore SYSLIB0049
}

public override string ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum FieldAttributes
Static = 0x0010, // Defined on type, else per instance.
InitOnly = 0x0020, // Field may only be initialized, not written to after init.
Literal = 0x0040, // Value is compile time constant.
[Obsolete(Obsoletions.LegacyFormatterMessage, DiagnosticId = Obsoletions.LegacyFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
NotSerialized = 0x0080, // Field does not have to be serialized when type is remoted.

SpecialName = 0x0200, // field is special. Name describes how.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected FieldInfo() { }

public bool IsInitOnly => (Attributes & FieldAttributes.InitOnly) != 0;
public bool IsLiteral => (Attributes & FieldAttributes.Literal) != 0;
[Obsolete(Obsoletions.LegacyFormatterMessage, DiagnosticId = Obsoletions.LegacyFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public bool IsNotSerialized => (Attributes & FieldAttributes.NotSerialized) != 0;
public bool IsPinvokeImpl => (Attributes & FieldAttributes.PinvokeImpl) != 0;
public bool IsSpecialName => (Attributes & FieldAttributes.SpecialName) != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace System.Reflection
{
public class ParameterInfo : ICustomAttributeProvider, IObjectReference
public class ParameterInfo : ICustomAttributeProvider
#pragma warning disable SYSLIB0049 // IObjectReference is obsolete
#pragma warning disable SA1001 // CommasMustBeSpacedCorrectly
, IObjectReference
#pragma warning restore SA1001
#pragma warning restore SYSLIB0049
{
protected ParameterInfo() { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public sealed override Type MakeArrayType(int rank)
public sealed override bool IsSecurityCritical => throw new NotSupportedException(SR.NotSupported_SignatureType);
public sealed override bool IsSecuritySafeCritical => throw new NotSupportedException(SR.NotSupported_SignatureType);
public sealed override bool IsSecurityTransparent => throw new NotSupportedException(SR.NotSupported_SignatureType);
[Obsolete(Obsoletions.LegacyFormatterMessage, DiagnosticId = Obsoletions.LegacyFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public sealed override bool IsSerializable => throw new NotSupportedException(SR.NotSupported_SignatureType);
public sealed override bool IsSubclassOf(Type c) => throw new NotSupportedException(SR.NotSupported_SignatureType);
protected sealed override bool IsValueTypeImpl() => throw new NotSupportedException(SR.NotSupported_SignatureType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum TypeAttributes

// Implementation attributes.
Import = 0x00001000, // Class / interface is imported
[Obsolete(Obsoletions.LegacyFormatterMessage, DiagnosticId = Obsoletions.LegacyFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
Serializable = 0x00002000, // The class is Serializable.
WindowsRuntime = 0x00004000, // Type is a Windows Runtime type.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace System.Runtime.Serialization
{
[CLSCompliant(false)]
[Obsolete(Obsoletions.LegacyFormatterMessage, DiagnosticId = Obsoletions.LegacyFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public interface IFormatterConverter
{
object Convert(object value, Type type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace System.Runtime.Serialization
{
[Obsolete(Obsoletions.LegacyFormatterMessage, DiagnosticId = Obsoletions.LegacyFormatterDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
public interface IObjectReference
{
object GetRealObject(StreamingContext context);
Expand Down
Loading

0 comments on commit e7e75e4

Please sign in to comment.