Skip to content

[feat] New assembly resolving pipeline + some nuget resolving heuristics #169

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 40 additions & 6 deletions VSharp.API/VSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static Statistics StartExploration(List<MethodBase> methods, string resu

void HandleInternalFail(MethodBase method, Exception exception)
{
Console.WriteLine($"EXCEPTION | {method.DeclaringType}.{method.Name} | {exception.GetType().Name} {exception.Message}");
Logger.printLogString(Logger.Error, $"Internal exception | {method.DeclaringType}.{method.Name} | {exception.GetType().Name} {exception.Message}");
}

foreach (var method in methods)
Expand Down Expand Up @@ -172,18 +172,52 @@ public static Statistics Cover(Type type, int timeout = -1, string outputDirecto
public static Statistics Cover(Assembly assembly, int timeout = -1, string outputDirectory = "")
{
AssemblyManager.Load(assembly);
List<MethodBase> methods;
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
BindingFlags.DeclaredOnly;
methods = new List<MethodBase>();

foreach (var t in assembly.GetTypes())
var methods = new List<MethodBase>();
var types = new List<Type>();

try
{
types.AddRange(assembly.GetTypes());
}
catch (ReflectionTypeLoadException e)
{
foreach (var loaderException in e.LoaderExceptions)
{
Logger.printLogString(Logger.Error, $"Cannot load type: {loaderException?.Message}");
}

foreach (var type in e.Types)
{
if (type is not null)
{
types.Add(type);
}
}
}

foreach (var t in types)
{
if (t.IsPublic)
if (t.IsPublic || t.IsNestedPublic)
{
foreach (var m in t.GetMethods(bindingFlags))
{
if (m.GetMethodBody() != null)
global::System.Reflection.MethodBody methodBody = null;
var hasByRefLikes = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, что по умолчанию лучше это выставить в false, и назвать "hasRefLikes", если имеются ввиду ref-like структуры

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В .NET оно называется именно "ByRefLike", поэтому название можно оставить


try
{
methodBody = m.GetMethodBody();
hasByRefLikes = Reflection.hasByRefLikes(m);
Comment on lines +212 to +213
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Насколько я понял, эксепшен может произойти во время "m.GetMethodBody()", тогда логичнее вначале вычислить "Reflection.hasByRefLikes(m)", если это необходимо

}
catch (Exception e)
{
Logger.printLogString(Logger.Error, $"Cannot get method info: {e.Message}");
}

if (methodBody is not null && !hasByRefLikes)
methods.Add(m);
}
}
Expand Down
72 changes: 72 additions & 0 deletions VSharp.CSharpUtils/AssemblyResolving/AssemblyResolverUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;

namespace VSharp.CSharpUtils.AssemblyResolving
{
public static class AssemblyResolverUtils
{
public static string FindAssemblyWithName(DirectoryInfo directory, AssemblyName targetName)
{
bool IsTargetAssembly(FileInfo assemblyFile)
{
try
{
var foundName = AssemblyName.GetAssemblyName(assemblyFile.FullName);
return foundName.Name == targetName.Name &&
foundName.Version == targetName.Version &&
foundName.ContentType == targetName.ContentType;
}
catch (Exception)
{
return false;
}
}

var found = directory.EnumerateFiles("*.dll").FirstOrDefault(IsTargetAssembly);

if (found is not null)
{
return found.FullName;
}

foreach (var subDir in directory.EnumerateDirectories())
{
var foundInSubDir = FindAssemblyWithName(subDir, targetName);
if (foundInSubDir is not null)
{
return foundInSubDir;
}
}

return null;
}

public static string GetBaseNuGetDirectory()
{
var baseDirectory = Environment.GetEnvironmentVariable("NUGET_PACKAGES");

if (!string.IsNullOrEmpty(baseDirectory))
{
return baseDirectory;
}

if (OperatingSystem.IsWindows())
{
baseDirectory = Environment.GetEnvironmentVariable("USERPROFILE");
}
else
{
baseDirectory = Environment.GetEnvironmentVariable("HOME");
}

if (string.IsNullOrEmpty(baseDirectory))
{
return null;
}

return Path.Combine(baseDirectory, ".nuget", "packages");
}
}
}
29 changes: 29 additions & 0 deletions VSharp.CSharpUtils/AssemblyResolving/CompositeAssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using System.Reflection;

namespace VSharp.CSharpUtils.AssemblyResolving
{
public class CompositeAssemblyResolver : IAssemblyResolver
{
private readonly List<IAssemblyResolver> _resolvers = new();

public CompositeAssemblyResolver(params IAssemblyResolver[] resolvers)
{
_resolvers.AddRange(resolvers);
}

public string Resolve(AssemblyName assemblyName)
{
foreach (var resolver in _resolvers)
{
var resolved = resolver.Resolve(assemblyName);
if (resolved is not null)
{
return resolved;
}
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System.Reflection;

namespace VSharp.CSharpUtils.AssemblyResolving
{
public class CurrentDirectoryAssemblyResolver : IAssemblyResolver
{
private readonly string _directoryPath;

public CurrentDirectoryAssemblyResolver(string directoryPath)
{
_directoryPath = directoryPath;
}

public string Resolve(AssemblyName assemblyName)
{
var dllPath = Path.Combine(_directoryPath, $"{assemblyName.Name}.dll");

if (File.Exists(dllPath))
{
return dllPath;
}

return null;
}
}
}
9 changes: 9 additions & 0 deletions VSharp.CSharpUtils/AssemblyResolving/IAssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Reflection;

namespace VSharp.CSharpUtils.AssemblyResolving
{
public interface IAssemblyResolver
{
string Resolve(AssemblyName assemblyName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.DependencyModel.Resolution;

namespace VSharp.CSharpUtils.AssemblyResolving
{
public class MicrosoftDependencyModelAssemblyResolver : IAssemblyResolver
{
private readonly List<ICompilationAssemblyResolver> _resolvers = new();
private readonly DependencyContext _context;

public MicrosoftDependencyModelAssemblyResolver(DependencyContext context, params ICompilationAssemblyResolver[] resolvers)
{
_resolvers.AddRange(resolvers);
_context = context;
}

public string Resolve(AssemblyName assemblyName)
{
if (_context is null)
{
return null;
}

var compilationLibrary = _context.CompileLibraries.FirstOrDefault(l =>
l.Name.Equals(assemblyName.Name, StringComparison.OrdinalIgnoreCase));

if (compilationLibrary is null)
{
var runtimeLibrary = _context.RuntimeLibraries.FirstOrDefault(l =>
l.Name.Equals(assemblyName.Name, StringComparison.OrdinalIgnoreCase));

if (runtimeLibrary is not null)
{
compilationLibrary = new CompilationLibrary(
runtimeLibrary.Type,
runtimeLibrary.Name,
runtimeLibrary.Version,
runtimeLibrary.Hash,
runtimeLibrary.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
runtimeLibrary.Dependencies,
runtimeLibrary.Serviceable
);
}
}

if (compilationLibrary is null)
{
return null;
}

var assemblies = new List<string>();

foreach (ICompilationAssemblyResolver resolver in _resolvers)
{
try
{
if (resolver.TryResolveAssemblyPaths(compilationLibrary, assemblies))
{
return assemblies[0];
}
}
catch { }
}

return null;
}
}
}
Loading