Skip to content

Commit

Permalink
[release/7.0] [wasm] ManagedToNativeGenerator: Skip unmanaged dlls (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Mar 31, 2023
1 parent 88b65f9 commit 641393e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 16 deletions.
2 changes: 1 addition & 1 deletion NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<clear />
<!--Begin: Package sources managed by Dependency Flow automation. Do not edit the sources below.-->
<!-- Begin: Package sources from dotnet-emsdk -->
<add key="darc-pub-dotnet-emsdk-2a1a284" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/darc-pub-dotnet-emsdk-2a1a284c/nuget/v3/index.json" />
<add key="darc-pub-dotnet-emsdk-7fa7119" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/darc-pub-dotnet-emsdk-7fa7119c/nuget/v3/index.json" />
<!-- End: Package sources from dotnet-emsdk -->
<!--End: Package sources managed by Dependency Flow automation. Do not edit the sources above.-->
<!--
Expand Down
8 changes: 4 additions & 4 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
<Uri>https://github.com/dotnet/command-line-api</Uri>
<Sha>5618b2d243ccdeb5c7e50a298b33b13036b4351b</Sha>
</Dependency>
<Dependency Name="Microsoft.NET.Workload.Emscripten.net6.Manifest-7.0.100" Version="7.0.4">
<Dependency Name="Microsoft.NET.Workload.Emscripten.net6.Manifest-7.0.100" Version="7.0.5">
<Uri>https://github.com/dotnet/emsdk</Uri>
<Sha>2a1a284cb9e62959ed261bba6cfc678612fc4559</Sha>
<Sha>7fa7119c1cdf1290aabb2391c3eee6ed42f8851d</Sha>
</Dependency>
<Dependency Name="Microsoft.NET.Workload.Emscripten.net7.Manifest-7.0.100" Version="7.0.4">
<Dependency Name="Microsoft.NET.Workload.Emscripten.net7.Manifest-7.0.100" Version="7.0.5">
<Uri>https://github.com/dotnet/emsdk</Uri>
<Sha>2a1a284cb9e62959ed261bba6cfc678612fc4559</Sha>
<Sha>7fa7119c1cdf1290aabb2391c3eee6ed42f8851d</Sha>
</Dependency>
</ProductDependencies>
<ToolsetDependencies>
Expand Down
4 changes: 2 additions & 2 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<UsingToolIbcOptimization>false</UsingToolIbcOptimization>
<UsingToolXliff>false</UsingToolXliff>
<LastReleasedStableAssemblyVersion>$(AssemblyVersion)</LastReleasedStableAssemblyVersion>
<MicrosoftNETWorkloadEmscriptennet6Manifest70100Version>7.0.4</MicrosoftNETWorkloadEmscriptennet6Manifest70100Version>
<MicrosoftNETWorkloadEmscriptennet7Manifest70100Version>7.0.4</MicrosoftNETWorkloadEmscriptennet7Manifest70100Version>
<MicrosoftNETWorkloadEmscriptennet6Manifest70100Version>7.0.5</MicrosoftNETWorkloadEmscriptennet6Manifest70100Version>
<MicrosoftNETWorkloadEmscriptennet7Manifest70100Version>7.0.5</MicrosoftNETWorkloadEmscriptennet7Manifest70100Version>
</PropertyGroup>
<ItemGroup>
<!-- The bands we want to produce workload manifests for -->
Expand Down
10 changes: 7 additions & 3 deletions src/tasks/WasmAppBuilder/IcallTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public IcallTableGenerator(Func<string, string> fixupSymbolName, TaskLoggingHelp
// The runtime icall table should be generated using
// mono --print-icall-table
//
public IEnumerable<string> Generate(string? runtimeIcallTableFile, string[] assemblies, string? outputPath)
public IEnumerable<string> Generate(string? runtimeIcallTableFile, IEnumerable<string> assemblies, string? outputPath)
{
_icalls.Clear();
_signatures.Clear();
Expand All @@ -44,9 +44,13 @@ public IEnumerable<string> Generate(string? runtimeIcallTableFile, string[] asse

var resolver = new PathAssemblyResolver(assemblies);
using var mlc = new MetadataLoadContext(resolver, "System.Private.CoreLib");
foreach (var aname in assemblies)
foreach (var asmPath in assemblies)
{
var a = mlc.LoadFromAssemblyPath(aname);
if (!File.Exists(asmPath))
throw new LogAsErrorException($"Cannot find assembly {asmPath}");

Log.LogMessage(MessageImportance.Low, $"Loading {asmPath} to scan for icalls");
var a = mlc.LoadFromAssemblyPath(asmPath);
foreach (var type in a.GetTypes())
ProcessType(type);
}
Expand Down
50 changes: 47 additions & 3 deletions src/tasks/WasmAppBuilder/ManagedToNativeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

public class ManagedToNativeGenerator : Task
{
[Required]
public string[]? Assemblies { get; set; }
public string[] Assemblies { get; set; } = Array.Empty<string>();

public string? RuntimeIcallTableFile { get; set; }

Expand Down Expand Up @@ -62,12 +65,14 @@ public override bool Execute()

private void ExecuteInternal()
{
List<string> managedAssemblies = FilterOutUnmanagedBinaries(Assemblies);

var pinvoke = new PInvokeTableGenerator(FixupSymbolName, Log);
var icall = new IcallTableGenerator(FixupSymbolName, Log);

IEnumerable<string> cookies = Enumerable.Concat(
pinvoke.Generate(PInvokeModules, Assemblies!, PInvokeOutputPath!),
icall.Generate(RuntimeIcallTableFile, Assemblies!, IcallOutputPath)
pinvoke.Generate(PInvokeModules, managedAssemblies, PInvokeOutputPath!),
icall.Generate(RuntimeIcallTableFile, managedAssemblies, IcallOutputPath)
);

var m2n = new InterpToNativeGenerator(Log);
Expand Down Expand Up @@ -110,4 +115,43 @@ public string FixupSymbolName(string name)
_symbolNameFixups[name] = fixedName;
return fixedName;
}

private List<string> FilterOutUnmanagedBinaries(string[] assemblies)
{
List<string> managedAssemblies = new(assemblies.Length);
foreach (string asmPath in Assemblies)
{
if (!File.Exists(asmPath))
throw new LogAsErrorException($"Cannot find assembly {asmPath}");

try
{
if (!IsManagedAssembly(asmPath))
{
Log.LogMessage(MessageImportance.Low, $"Skipping unmanaged {asmPath}.");
continue;
}
}
catch (Exception ex)
{
Log.LogMessage(MessageImportance.Low, $"Failed to read assembly {asmPath}: {ex}");
throw new LogAsErrorException($"Failed to read assembly {asmPath}: {ex.Message}");
}

managedAssemblies.Add(asmPath);
}

return managedAssemblies;
}

private static bool IsManagedAssembly(string filePath)
{
if (!File.Exists(filePath))
return false;

using FileStream fileStream = File.OpenRead(filePath);
using PEReader reader = new(fileStream, PEStreamOptions.Default);
return reader.HasMetadata;
}

}
11 changes: 8 additions & 3 deletions src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public PInvokeTableGenerator(Func<string, string> fixupSymbolName, TaskLoggingHe
_fixupSymbolName = fixupSymbolName;
}

public IEnumerable<string> Generate(string[] pinvokeModules, string[] assemblies, string outputPath)
public IEnumerable<string> Generate(string[] pinvokeModules, IEnumerable<string> assemblies, string outputPath)
{
var modules = new Dictionary<string, string>();
foreach (var module in pinvokeModules)
Expand All @@ -37,9 +37,14 @@ public IEnumerable<string> Generate(string[] pinvokeModules, string[] assemblies

var resolver = new PathAssemblyResolver(assemblies);
using var mlc = new MetadataLoadContext(resolver, "System.Private.CoreLib");
foreach (var aname in assemblies)

foreach (var asmPath in assemblies)
{
var a = mlc.LoadFromAssemblyPath(aname);
if (!File.Exists(asmPath))
throw new LogAsErrorException($"Cannot find assembly {asmPath}");

Log.LogMessage(MessageImportance.Low, $"Loading {asmPath} to scan for pinvokes");
var a = mlc.LoadFromAssemblyPath(asmPath);
foreach (var type in a.GetTypes())
CollectPInvokes(pinvokes, callbacks, signatures, type);
}
Expand Down

0 comments on commit 641393e

Please sign in to comment.