Skip to content

Commit

Permalink
Update linker substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
GrabYourPitchforks committed Jul 15, 2020
1 parent af0f08e commit bcda8cb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<type fullname="System.LocalAppContextSwitches">
<method signature="System.Boolean get_EnableUnsafeUTF7Encoding()" body="stub" value="false" feature="System.Text.Encoding.EnableUnsafeUTF7Encoding" featurevalue="false" />
</type>
<type fullname="System.LocalAppContextSwitches">
<method signature="System.Boolean get_BinaryFormatterEnabled()" body="stub" value="false" feature="System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization" featurevalue="false" />
<type fullname="System.Resources.ResourceReader">
<method signature="System.Boolean InitializeBinaryFormatter()" body="stub" value="false" feature="System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization" featurevalue="false" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ public static bool PreserveEventListnerObjectIdentity
get => GetCachedSwitchValue("Switch.System.Diagnostics.EventSource.PreserveEventListnerObjectIdentity", ref s_preserveEventListnerObjectIdentity);
}

private static int s_binaryFormatterEnabled;
public static bool BinaryFormatterEnabled
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => GetCachedSwitchValue("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", ref s_binaryFormatterEnabled);
}

private static int s_serializationGuard;
public static bool SerializationGuard
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ private object DeserializeObject(int typeIndex)

if (_binaryFormatter == null)
{
InitializeBinaryFormatter();
if (!InitializeBinaryFormatter())
{
// The linker trimmed away the BinaryFormatter implementation and we can't call into it.
// We'll throw an exception with the same text that BinaryFormatter would have thrown
// had we been able to call into it. Keep this resource string in sync with the same
// resource from the Formatters assembly.
throw new NotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
}
}

Type type = FindType(typeIndex);
Expand All @@ -66,19 +73,10 @@ private object DeserializeObject(int typeIndex)
}

// Issue https://github.com/dotnet/runtime/issues/39290 tracks finding an alternative to BinaryFormatter
private void InitializeBinaryFormatter()
private bool InitializeBinaryFormatter()
{
// If BinaryFormatter support is hardcoded to disabled (e.g., wasm projects), early-exit now so
// that we don't run any of the reflection logic before. This also prevents the linker from seeing
// the GetType call below and keeping an unneeded reference to the Formatters assembly.
//
// Use a unique resource string so that we can provide a better error message in the case of
// "I tried to call into BinaryFormatter but was blocked." Keep this message text in sync with
// the same resource in the Formatters assembly.
if (!LocalAppContextSwitches.BinaryFormatterEnabled)
{
throw new NotSupportedException(SR.BinaryFormatter_SerializationDisallowed);
}
// If BinaryFormatter support is disabled for the app, the linker will replace this entire
// method body with "return false;", skipping all reflection code below.

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",
Expand All @@ -96,6 +94,8 @@ private void InitializeBinaryFormatter()
});

_binaryFormatter = Activator.CreateInstance(s_binaryFormatterType!)!;

return true; // initialization successful
}

// generic method that we specialize at runtime once we've loaded the BinaryFormatter type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<type fullname="System.Runtime.CompilerServices.RuntimeFeature">
<method signature="System.Boolean get_IsDynamicCodeCompiled()" body="stub" value="false" />
</type>
<type fullname="System.LocalAppContextSwitches">
<method signature="System.Boolean get_BinaryFormatterEnabled()" body="stub" value="false" />
<type fullname="System.Resources.ResourceReader">
<method signature="System.Boolean InitializeBinaryFormatter()" body="stub" value="false" />
</type>
</assembly>
</linker>

0 comments on commit bcda8cb

Please sign in to comment.