Skip to content

Commit

Permalink
[tools] Add nuget-hash-extractor tool to help produce the runtime ign…
Browse files Browse the repository at this point in the history
…ored assemblies table.

This tool takes a folder full of nupkg file and print macro invocations to add to image.c:ignored_assemblies.

Usage:

make download
make run

The download.sh script downloads all nugets we want to ignore. So update it before running if you have more you want to add.
  • Loading branch information
kumpera committed Jan 17, 2017
1 parent 9ec8ec5 commit 26b60ef
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
13 changes: 13 additions & 0 deletions tools/nuget-hash-extractor/Makefile
@@ -0,0 +1,13 @@
SOURCES = \
nuget-hash-extractor.cs

nuget-hash-extractor.exe: $(SOURCES)
mcs /r:System.Xml.Linq /r:System.IO.Compression -o:nuget-hash-extractor.exe $(SOURCES)

download:
echo "Downloading all the nugets"; \
./download.sh

run: nuget-hash-extractor.exe
mono nuget-hash-extractor.exe nugets
.PHONY: download run
27 changes: 27 additions & 0 deletions tools/nuget-hash-extractor/download.sh
@@ -0,0 +1,27 @@
mkdir nugets

#System.Runtime.InteropServices.RuntimeInformation
wget https://www.nuget.org/api/v2/package/System.Runtime.InteropServices.RuntimeInformation/4.3.0 -O nugets/system.runtime.interopservices.runtimeinformation.4.3.0.nupkg
wget https://www.nuget.org/api/v2/package/System.Runtime.InteropServices.RuntimeInformation/4.0.0 -O nugets/system.runtime.interopservices.runtimeinformation.4.0.0.nupkg

#System.Globalization.Extensions
wget https://www.nuget.org/api/v2/package/System.Globalization.Extensions/4.3.0 -O nugets/system.globalization.extensions.4.3.0.nupkg
wget https://www.nuget.org/api/v2/package/System.Globalization.Extensions/4.0.1 -O nugets/system.globalization.extensions.4.0.1.nupkg
wget https://www.nuget.org/api/v2/package/System.Globalization.Extensions/4.0.0 -O nugets/system.globalization.extensions.4.0.0.nupkg

#System.IO.Compression
wget https://www.nuget.org/api/v2/package/System.IO.Compression/4.3.0 -O nugets/system.io.compression.4.3.0.nupkg
wget https://www.nuget.org/api/v2/package/System.IO.Compression/4.1.0 -O nugets/system.io.compression.4.1.0.nupkg
wget https://www.nuget.org/api/v2/package/System.IO.Compression/4.0.0 -O nugets/system.io.compression.4.0.0.nupkg

#System.Net.Http
wget https://www.nuget.org/api/v2/package/System.Net.Http/4.3.0 -O nugets/system.net.http.4.3.0.nupkg
wget https://www.nuget.org/api/v2/package/System.Net.Http/4.1.1 -O nugets/system.net.http.4.1.1.nupkg
wget https://www.nuget.org/api/v2/package/System.Net.Http/4.1.0 -O nugets/system.net.http.4.1.0.nupkg
wget https://www.nuget.org/api/v2/package/System.Net.Http/4.0.0 -O nugets/system.net.http.4.0.0.nupkg

#System.Text.Encoding.CodePages
wget https://www.nuget.org/api/v2/package/System.Text.Encoding.CodePages/4.3.0 -O nugets/system.text.encoding.codepages.4.3.0.nupkg
wget https://www.nuget.org/api/v2/package/System.Text.Encoding.CodePages/4.0.1 -O nugets/system.text.encoding.codepages.4.0.1.nupkg
wget https://www.nuget.org/api/v2/package/System.Text.Encoding.CodePages/4.0.0 -O nugets/system.text.encoding.codepages.4.0.0.nupkg

85 changes: 85 additions & 0 deletions tools/nuget-hash-extractor/nuget-hash-extractor.cs
@@ -0,0 +1,85 @@
using System;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.IO.Compression;
using System.Reflection;

class Driver {
static ZipArchiveEntry FindSpecFile (ZipArchive zip) {
foreach (var entry in zip.Entries) {
if (entry.Name.EndsWith (".nuspec"))
return entry;
}
throw new Exception ("Could not find nuspec file");
}

static void DumpNuget (string nupkg) {
var zip = new ZipArchive(new FileStream (nupkg, FileMode.Open));

var nuspec = FindSpecFile (zip);
var l = XElement.Load (new StreamReader (nuspec.Open ()));
var version = (from el in l.Descendants() where el.Name.LocalName == "version" select el.Value).FirstOrDefault ();

foreach (var et in from e in zip.Entries where (e.FullName.StartsWith ("lib/net4") || e.FullName.StartsWith ("lib/netcore")) && e.Name.EndsWith (".dll") select e) {
LoadAndDump (et, version);
}
}
static void Main (string[] args) {
foreach (var f in Directory.GetFiles (args [0], "*.nupkg")) {
DumpNuget (f);
}
}

static byte[] StreamToArray (Stream s) {
using(var ms = new MemoryStream ()) {
s.CopyTo (ms);
return ms.ToArray ();
}
}

static int domain_id = 1;
static void LoadAndDump (ZipArchiveEntry entry, string version) {
// Console.WriteLine ("Dumping {0}", entry);
var data = StreamToArray (entry.Open ());
AppDomain ad = AppDomain.CreateDomain ("parse_" + ++domain_id);
DoParse p = (DoParse)ad.CreateInstanceAndUnwrap (typeof (DoParse).Assembly.FullName, typeof (DoParse).FullName);
p.ParseAssembly (data, version, entry.Name, entry.FullName);
AppDomain.Unload (ad);
}
}

class DoParse : MarshalByRefObject {
static int Hash (string str) {
int h = 5381;
for (int i = 0; i < str.Length; ++i)
h = ((h << 5) + h) ^ str[i];
return h;
}
static string FileToEnum (string name) {
switch (name) {
case "System.Runtime.InteropServices.RuntimeInformation.dll": return "SYS_RT_INTEROP_RUNTIME_INFO";
case "System.Globalization.Extensions.dll": return "SYS_GLOBALIZATION_EXT";
case "System.IO.Compression.dll": return "SYS_IO_COMPRESSION";
case "System.Net.Http.dll": return "SYS_NET_HTTP";
case "System.Text.Encoding.CodePages.dll": return "SYS_TEXT_ENC_CODEPAGES";
default: throw new Exception ($"No idea what to do with {name}");
}
}

static string FileToMoniker (string p) {
var parts = p.Split (Path.DirectorySeparatorChar);
return parts[parts.Length - 2];
}

public void ParseAssembly (byte[] data, string version, string name, string fullname) {
var a = Assembly.ReflectionOnlyLoad (data);
var m = a.GetModules ()[0];
var id = m.ModuleVersionId.ToString ().ToUpper ();
var hash_code = Hash (id).ToString ("X");
var str = FileToEnum (name);

string ver_str = version + " " + FileToMoniker (fullname);
Console.WriteLine ($"IGNORED_ASSEMBLY (0x{hash_code}, {str}, \"{id}\", \"{ver_str}\"),");
}
}

0 comments on commit 26b60ef

Please sign in to comment.