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 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter to CoreLib #39090

Closed
marek-safar opened this issue Jul 10, 2020 · 7 comments
Labels
arch-wasm WebAssembly architecture area-System.Runtime enhancement Product code improvement that does NOT require public API changes/additions linkable-framework Issues associated with delivering a linker friendly framework
Milestone

Comments

@marek-safar
Copy link
Contributor

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter type is always required by System.Private.CoreLib because it's used for resources serialization/deserialization. The required code after trimming in size sensitive scenarios ends up quite small (about 6kB) which indicates there is not a large chunk of code to be moved.

Moving the implementation would help reduce the number of file transfers for browser and get rid of extra reflection call in

private void InitializeBinaryFormatter()
{
LazyInitializer.EnsureInitialized(ref s_binaryFormatterType, () =>
Type.GetType("System.Runtime.Serialization.Formatters.Binary.BinaryFormatter, System.Runtime.Serialization.Formatters, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
throwOnError: true)!);
LazyInitializer.EnsureInitialized(ref s_deserializeMethod, () =>
{
MethodInfo binaryFormatterDeserialize = s_binaryFormatterType!.GetMethod("Deserialize", new Type[] { typeof(Stream) })!;
// create an unbound delegate that can accept a BinaryFormatter instance as object
return (Func<object?, Stream, object>)typeof(ResourceReader)
.GetMethod(nameof(CreateUntypedDelegate), BindingFlags.NonPublic | BindingFlags.Static)!
.MakeGenericMethod(s_binaryFormatterType)
.Invoke(null, new object[] { binaryFormatterDeserialize })!;
});
_binaryFormatter = Activator.CreateInstance(s_binaryFormatterType!)!;
}

@eerhardt

@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jul 10, 2020
@GrabYourPitchforks
Copy link
Member

BinaryFormatter doesn't work at all in browser after the changes in #38963 come online. Why not ifdef this code away in wasm? Or condition this check on the internal API SerializationInfo.IsBinaryFormatterEnabled (also part of that PR), which will always evaluate to false on wasm and other scenarios where BF is intended to be trimmed away?

@jkotas
Copy link
Member

jkotas commented Jul 10, 2020

I agree with @GrabYourPitchforks . System.Runtime.Serialization.Formatters.Binary.BinaryFormatter has problematic security characteristics. It is better for it to be in separate binary.

@jkotas jkotas added area-System.Runtime linkable-framework Issues associated with delivering a linker friendly framework labels Jul 10, 2020
@eerhardt
Copy link
Member

The required code after trimming in size sensitive scenarios ends up quite small (about 6kB) which indicates there is not a large chunk of code to be moved.

Note that it is that small because the linker is removing all the methods from the type. This is because it can't automatically detect the above reflection usage and produces a warning (which we are currently ignoring all warnings).

See the conversation here: #38432 (comment)

@GrabYourPitchforks
Copy link
Member

If there aren't any objections, I'll experiment with sending a new iteration of #38963 that addresses this code path as well.

@joperezr joperezr added this to the 5.0.0 milestone Jul 13, 2020
@joperezr joperezr added arch-wasm WebAssembly architecture enhancement Product code improvement that does NOT require public API changes/additions and removed untriaged New issue has not been triaged by the area owner labels Jul 13, 2020
@GrabYourPitchforks
Copy link
Member

@marek-safar These code paths should no longer be called in wasm applications, so there should be no remaining references to the Formatters package. Anything else we need to do here?

@eerhardt
Copy link
Member

I don't believe there is anything else we need to do here. After linking a Blazor WASM app with the latest runtime, I am not seeing System.Runtime.Serialization.Formatters.dll in the output at all.
Closing.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-System.Runtime enhancement Product code improvement that does NOT require public API changes/additions linkable-framework Issues associated with delivering a linker friendly framework
Projects
None yet
Development

No branches or pull requests

6 participants