Skip to content

Commit

Permalink
[wasm] trim System.Private.Uri.dll (#74826)
Browse files Browse the repository at this point in the history
change interop code so that ILLink would not protect Uri class
  • Loading branch information
pavelsavara authored Aug 31, 2022
1 parent d6ce31b commit d48dd6c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace System.Runtime.InteropServices.JavaScript
Expand Down Expand Up @@ -210,15 +211,34 @@ public static void CreateDateTimeRef(double ticks, out object result)
result = unixTime.DateTime;
}

// we do this via reflection to allow linker to trim dependency on Uri class and it's assembly
// if the user code has methods with Uri signature, they probably also have the Uri constructor
// if they don't have it, they could configure ILLing to protect it after they enabled trimming
// We believe that this code path is probably not even used in the wild
// System.Private.Uri is ~80KB large assembly so it's worth trimming
private static Type? uriType;

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
[Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2077", Justification = "Done on purpose, see comment above.")]
[Diagnostics.CodeAnalysis.UnconditionalSuppressMessage("Trimming", "IL2057", Justification = "Done on purpose, see comment above.")]
public static void CreateUriRef(string uri, out object? result)
{
// we do this via reflection to allow linker to trim dependency on URI and it's assembly
// if the user code has methods with Uri signature, this should work too
// System.Private.Uri is large assembly so it's worth trimming
var uriType = Type.GetType("System.Uri, System.Private.Uri");
if (uriType == null) throw new InvalidProgramException();
result = Activator.CreateInstance(uriType, uri);
if (uriType == null)
{
// StringBuilder to confuse ILLink, which is too smart otherwise
StringBuilder sb = new StringBuilder("System.Uri, System.Private.Uri");
uriType = Type.GetType(sb.ToString());
}
// See: https://devblogs.microsoft.com/dotnet/customizing-trimming-in-net-core-5/
if (uriType == null) throw new InvalidProgramException("The type System.Uri could not be found. Please consider to protect the class and it's constructor from trimming.");
try
{
result = Activator.CreateInstance(uriType, uri);
}
catch (MissingMethodException ex)
{
throw new MissingMethodException("Constructor on type 'System.Uri' not found. Please consider to protect it's constructor from trimming.", ex);
}
}

[MethodImplAttribute(MethodImplOptions.NoInlining)] // https://github.com/dotnet/runtime/issues/71425
Expand Down
8 changes: 8 additions & 0 deletions src/mono/sample/wasm/browser-advanced/ILLink.Descriptors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<linker>
<!-- this is here to show how to configure the trimming -->
<assembly fullname="System.Private.Uri">
<type fullname="System.Uri">
<method signature="System.Void .ctor(System.String)" />
</type>
</assembly>
</linker>
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
<WasmDebugLevel>1</WasmDebugLevel>
<GenerateRunScriptForSample Condition="'$(ArchiveTests)' == 'true'">true</GenerateRunScriptForSample>
<RunScriptCommand>$(ExecXHarnessCmd) wasm test-browser --app=. --browser=Chrome $(XHarnessBrowserPathArg) --html-file=index.html --output-directory=$(XHarnessOutput) -- $(MSBuildProjectName).dll</RunScriptCommand>
<EnableAggressiveTrimming>true</EnableAggressiveTrimming>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
<ItemGroup>
<WasmExtraFilesToDeploy Include="index.html" />
<NativeFileReference Include="fibonacci.c" />
<TrimmerRootDescriptor Include="$(MSBuildThisFileDirectory)ILLink.Descriptors.xml" />
</ItemGroup>
<PropertyGroup>
<_SampleProject>Wasm.Advanced.Sample.csproj</_SampleProject>
Expand Down

0 comments on commit d48dd6c

Please sign in to comment.