Skip to content

[browser] Calculate hash for runtime assets #82891

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

Merged
merged 5 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<EnableAggressiveTrimming>true</EnableAggressiveTrimming>
<PublishTrimmed>true</PublishTrimmed>
<WasmEnableWebcil>true</WasmEnableWebcil>
<!-- add OpenGL emulation -->
<EmccExtraLDFlags> -s USE_CLOSURE_COMPILER=1 -s LEGACY_GL_EMULATION=1 -lGL -lSDL -lidbfs.js</EmccExtraLDFlags>
<!-- just to prove we don't do JS eval() -->
Expand Down
2 changes: 1 addition & 1 deletion src/mono/sample/wasm/browser-advanced/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="preload" href="./mono-config.json" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./dotnet.wasm" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./icudt.dat" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./managed/System.Private.CoreLib.dll" as="fetch" crossorigin="anonymous">
<link rel="prefetch" href="./managed/System.Private.CoreLib.webcil" as="fetch" crossorigin="anonymous">
</head>

<body>
Expand Down
10 changes: 9 additions & 1 deletion src/mono/wasm/runtime/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function get_preferred_icu_asset(): string | null {
return OTHERS;
}

export function shouldLoadIcuAsset(asset : AssetEntryInternal, preferredIcuAsset: string | null) : boolean{
export function shouldLoadIcuAsset(asset: AssetEntryInternal, preferredIcuAsset: string | null): boolean {
return !(asset.behavior == "icu" && asset.name != preferredIcuAsset);
}

Expand Down Expand Up @@ -293,6 +293,14 @@ async function start_asset_download_sources(asset: AssetEntryInternal): Promise<
return response;
}
catch (err) {
if (!response) {
response = {
ok: false,
url: attemptUrl,
status: 0,
statusText: "" + err,
} as any;
}
continue; //next source
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/dotnet.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ type MonoConfig = {
* initial number of workers to add to the emscripten pthread pool
*/
pthreadPoolSize?: number;
/**
* hash of assets
*/
assetsHash?: string;
};
interface ResourceRequest {
name: string;
Expand Down
4 changes: 4 additions & 0 deletions src/mono/wasm/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export type MonoConfig = {
* initial number of workers to add to the emscripten pthread pool
*/
pthreadPoolSize?: number,
/**
* hash of assets
*/
assetsHash?: string,
};

export type MonoConfigInternal = MonoConfig & {
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/AndroidAppBuilder/AndroidAppBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn),CA1050</NoWarn>
<NoWarn>$(NoWarn),CA1050,CA1850</NoWarn>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Templates\*.*" />
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/AotCompilerTask/MonoAOTCompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn),CA1050</NoWarn>
<NoWarn>$(NoWarn),CA1050,CA1850</NoWarn>

<!-- Ignore nullable warnings on net4* -->
<NoWarn Condition="$(TargetFramework.StartsWith('net4'))">$(NoWarn),CS8604,CS8602</NoWarn>
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/AppleAppBuilder/AppleAppBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn),CA1050</NoWarn>
<NoWarn>$(NoWarn),CA1050,CA1850</NoWarn>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Templates\*.*" />
Expand Down
69 changes: 69 additions & 0 deletions src/tasks/Common/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -237,6 +240,32 @@ public static string ComputeHash(string filepath)
return Convert.ToBase64String(hash);
}

public static string ComputeIntegrity(string filepath)
{
using var stream = File.OpenRead(filepath);
using HashAlgorithm hashAlgorithm = SHA256.Create();

byte[] hash = hashAlgorithm.ComputeHash(stream);
return "sha256-" + Convert.ToBase64String(hash);
}

public static string ComputeIntegrity(byte[] bytes)
{
using HashAlgorithm hashAlgorithm = SHA256.Create();

byte[] hash = hashAlgorithm.ComputeHash(bytes);
return "sha256-" + Convert.ToBase64String(hash);
}

public static string ComputeTextIntegrity(string str)
{
using HashAlgorithm hashAlgorithm = SHA256.Create();

var bytes = Encoding.UTF8.GetBytes(str);
byte[] hash = hashAlgorithm.ComputeHash(bytes);
return "sha256-" + Convert.ToBase64String(hash);
}

#if NETCOREAPP
public static void DirectoryCopy(string sourceDir, string destDir, Func<string, bool>? predicate=null)
{
Expand All @@ -258,4 +287,44 @@ public static void DirectoryCopy(string sourceDir, string destDir, Func<string,
}
}
#endif

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

// Try to read CLI metadata from the PE file.
using FileStream fileStream = File.OpenRead(filePath);
using PEReader peReader = new(fileStream, PEStreamOptions.Default);
return IsManagedAssembly(peReader);
}

public static bool IsManagedAssembly(byte[] bytes)
{
using var peReader = new PEReader(ImmutableArray.Create(bytes));
return IsManagedAssembly(peReader);
}

private static bool IsManagedAssembly(PEReader peReader)
{
try
{
if (!peReader.HasMetadata)
{
return false; // File does not have CLI metadata.
}

// Check that file has an assembly manifest.
MetadataReader reader = peReader.GetMetadataReader();
return reader.IsAssembly;
}
catch (BadImageFormatException)
{
return false;
}
catch (FileNotFoundException)
{
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/tasks/MonoTargetsTasks/MonoTargetsTasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>$(TargetFrameworkForNETCoreTasks);$(TargetFrameworkForNETFrameworkTasks)</TargetFrameworks>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn),CA1050</NoWarn>
<NoWarn>$(NoWarn),CA1050,CA1850</NoWarn>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(TargetFrameworkForNETCoreTasks)'">
<PackageReference Include="Microsoft.Build" Version="$(MicrosoftBuildVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<Nullable>enable</Nullable>
<NoWarn>$(NoWarn),CA1050</NoWarn>
<NoWarn>$(NoWarn),CA1050,CA1850</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 1 addition & 13 deletions src/tasks/WasmAppBuilder/ManagedToNativeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;
Expand Down Expand Up @@ -128,7 +127,7 @@ private List<string> FilterOutUnmanagedBinaries(string[] assemblies)

try
{
if (!IsManagedAssembly(asmPath))
if (!Utils.IsManagedAssembly(asmPath))
{
Log.LogMessage(MessageImportance.Low, $"Skipping unmanaged {asmPath}.");
continue;
Expand All @@ -145,15 +144,4 @@ private List<string> FilterOutUnmanagedBinaries(string[] assemblies)

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;
}

}
Loading