Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add BinaryFormatter to corefx #10088

Closed
wants to merge 2 commits into from

Conversation

stephentoub
Copy link
Member

@stephentoub stephentoub commented Jul 15, 2016

This ports BinaryFormatter from desktop, along with all of the supporting types and general Formatter-related types. I've added a bunch of tests, bringing code coverage up to ~80%, though there's still more that can/should be done. I did not port remoting-specific functionality, though there are still some remnants I've left as they were enabled in the runtime even when remoting wasn't compiled in, as it wasn't clear if they were serving additional purposes. I did a bunch of cleanup, but there's still more that can be done in the fullness of time.

I already made several changes to CoreCLR to enable this:

  • Stop BclRewriter from trimming serializable metadata coreclr#6253 fixed the BclRewriter to stop removing the serialization metadata from types in coreclr. Without that, attempting to serialize even primitive types was failing.
  • Add #if FEATURE_SERIALIZATION around customized [Serializable]s coreclr#6263 then removed [Serializable] from types that performed custom serialization (e.g. ISerializable), because currently the ISerializable in CoreFx is a different runtime type from the one in coreclr, which meant that formatters trying to serialize such types would use default serialization rather than picking up the custom implementation. Once we move ISerializable and friends into System.Runtime as type forwards rather than redefinitions of the interfaces, we can re-enable the serialization of those types.

(This PR is currently going against master. If that turns out to be the wrong branch for it, I'll resubmit against the correct branch prior to merging.)

cc: @danmosemsft, @jkotas, @KrzysztofCwalina, @ianhays, @bartonjs
Fixes https://github.com/dotnet/corefx/issues/7938
Fixes https://github.com/dotnet/corefx/issues/9582

@@ -137,3 +143,199 @@ public sealed class SerializationInfoEnumerator : IEnumerator
public void Reset() { throw null; }
}
}
namespace System.Runtime.Serialization
{
[System.CLSCompliantAttribute(false)]
Copy link
Member

Choose a reason for hiding this comment

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

Are we not scrubbing these out?

Copy link
Member Author

Choose a reason for hiding this comment

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

CLSCompliant? No. If something isn't compliant and isn't attributed to say so, it'll produce a build error.

@danmoseley
Copy link
Member

how much should we be reviewing the binaryformatter itself - how much is essentially the desktoip code verbatim?


//If the id that we need to place is greater than our current length, and less
//than the maximum allowable size of the array. We need to double the size
//of the array. If the array has already reached it's maximum allowable size,
Copy link
Member

Choose a reason for hiding this comment

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

If the array has already reached it's maximum allowable size, [](start = 28, length = 62)

is there a test for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

is there a test for this?

You mean if we try to serialize two billion objects? No.

@stephentoub
Copy link
Member Author

how much should we be reviewing the binaryformatter itself - how much is essentially the desktoip code verbatim?

From a logic perspective, it's the desktop code. I had to supply replacements where there were dependencies that aren't exposed, remove code that wasn't relevant, plus a lot of style stuff, but for the most part it's the same code and doesn't need to be reviewed in depth... honestly there are parts I'm not entirely sure what they're doing ;)

[Serializable]
internal sealed class IntSizedArray : ICloneable
{
internal int[] objects = new int[16];
Copy link
Member

Choose a reason for hiding this comment

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

objects [](start = 23, length = 7)

leading underscores?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup, somehow missed these two.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

@danmoseley
Copy link
Member

Thank you for doing this, great stuff.

@danmoseley
Copy link
Member

:shipit:

internal FormatterTypeStyle _FEtypeFormat;
internal FormatterAssemblyStyle _FEassemblyFormat;
internal TypeFilterLevel _FEsecurityLevel;
internal InternalSerializerTypeE _FEserializerTypeEnum;
Copy link
Member

Choose a reason for hiding this comment

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

The casing on the names here (and in NameInfo) seems off. In Desktop they had no underscore, presenting as property names. Now they are underscore-pascal; which I don't think is any part of our style rules. Bad CodeFormatter edit?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will fix.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

This ports BinaryFormatter from desktop, along with all of the supporting types and general Formatter-related types.  I've added a bunch of tests, bringing code coverage up to ~80%, though there's still more that can/should be done.  I did not port remoting-specific functionality, though there are still some remnants I've left as they were enabled in the runtime even when remoting wasn't compiled in, and it wasn't clear if they were serving additional purposes.
@stephentoub
Copy link
Member Author

Thanks for all the feedback. I believe I've addressed everything.

@jkotas
Copy link
Member

jkotas commented Jul 17, 2016

👍

{
try
{
Assembly.Load(new AssemblyName(assemblyName));
Copy link
Member

Choose a reason for hiding this comment

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

Are you missing an assignment to assm 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.

Yup.

@stephentoub
Copy link
Member Author

Replaced by #10144

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compat work for Serialization FormatterServices.GetUninitializedObject replacement?
7 participants